The hotkey for showing track info now calls a different method that will hide the...
[MenuTunes.git] / MainController.m
index 927a278..db0269f 100755 (executable)
@@ -9,6 +9,7 @@
 #import <ITKit/ITCategory-NSMenu.h>
 #import "StatusWindow.h"
 #import "StatusWindowController.h"
+#import "AudioscrobblerController.h"
 #import "StatusItemHack.h"
 
 @interface NSMenu (MenuImpl)
@@ -88,6 +89,7 @@ static MainController *sharedController;
         sharedController = self;
         
                _statusWindowUpdateTimer = nil;
+               _audioscrobblerTimer = nil;
                
         remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
         [[PreferencesController sharedPrefs] setController:self];
@@ -461,6 +463,18 @@ static MainController *sharedController;
                                        [statusItem setToolTip:nil];
                                }
                        }
+                       
+                       if ([df boolForKey:@"audioscrobblerEnabled"]) {
+                               int length = [[self currentRemote] currentSongDuration];
+                               if (_audioscrobblerTimer) {
+                                       [_audioscrobblerTimer invalidate];
+                               }
+                               if (length > 0) {
+                                       _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                               }
+                       } else {
+                               _audioscrobblerTimer = nil;
+                       }
         NS_HANDLER
             [self networkError:localException];
         NS_ENDHANDLER
@@ -575,6 +589,18 @@ static MainController *sharedController;
                                [statusItem setToolTip:nil];
                        }
                }
+               
+               if ([df boolForKey:@"audioscrobblerEnabled"]) {
+                       int length = [[self currentRemote] currentSongDuration];
+                       if (_audioscrobblerTimer) {
+                               [_audioscrobblerTimer invalidate];
+                       }
+                       if (length > 0) {
+                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                       }
+               } else {
+                       _audioscrobblerTimer = nil;
+               }
        NS_HANDLER
                [self networkError:localException];
        NS_ENDHANDLER
@@ -586,6 +612,25 @@ static MainController *sharedController;
     }
 }
 
+- (void)submitAudioscrobblerTrack:(NSTimer *)timer
+{
+       if ([df boolForKey:@"audioscrobblerEnabled"]) {
+               NS_DURING
+                       NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist];
+                       if (title && artist) {
+                               [[AudioscrobblerController sharedController] submitTrack:title
+                                                                                                                               artist:artist
+                                                                                                                               album:[[self currentRemote] currentSongAlbum]
+                                                                                                                               length:[[self currentRemote] currentSongDuration]];
+                       }
+               NS_HANDLER
+                       [self networkError:localException];
+               NS_ENDHANDLER
+               [timer invalidate];
+               _audioscrobblerTimer = nil;
+       }
+}
+
 //
 //
 // Menu Selectors
@@ -905,7 +950,7 @@ static MainController *sharedController;
         [hotKey setName:@"TrackInfo"];
         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
         [hotKey setTarget:self];
-        [hotKey setAction:@selector(showCurrentTrackInfo)];
+        [hotKey setAction:@selector(showCurrentTrackInfoHotKey)];
         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
     }
     
@@ -1015,6 +1060,18 @@ static MainController *sharedController;
     ITDebugLog(@"Finished setting up hot keys.");
 }
 
+- (void)showCurrentTrackInfoHotKey
+{
+       //If we're already visible and the setting says so, vanish instead of displaying again.
+       if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) {
+               ITDebugLog(@"Track window is already visible, hiding track window.");
+               [self invalidateStatusWindowUpdateTimer];
+               [[StatusWindow sharedWindow] vanish:nil];
+               return;
+       }
+       [self showCurrentTrackInfo];
+}
+
 - (void)showCurrentTrackInfo
 {
     ITMTRemotePlayerSource  source      = 0;
@@ -1028,14 +1085,6 @@ static MainController *sharedController;
     int                     rating      = -1;
     int                     playCount   = -1;
        
-       //If we're already visible and the setting says so, vanish instead of displaying again.
-       if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) {
-               ITDebugLog(@"Track window is already visible, hiding track window.");
-               [self invalidateStatusWindowUpdateTimer];
-               [[StatusWindow sharedWindow] vanish:nil];
-               return;
-       }
-       
     ITDebugLog(@"Showing track info status window.");
     
     NS_DURING