Fixed it so network players will use the refresh timer to update properly again.
[MenuTunes.git] / MainController.m
index db0269f..b2ea25e 100755 (executable)
@@ -140,6 +140,8 @@ static MainController *sharedController;
     currentRemote = [self loadRemote];
     [[self currentRemote] begin];
     
+       [[self currentRemote] currentSongElapsed];
+       
     //Turn on network stuff if needed
     networkController = [[NetworkController alloc] init];
     if ([df boolForKey:@"enableSharing"]) {
@@ -198,6 +200,12 @@ static MainController *sharedController;
     [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
     [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
 
+       if ([df boolForKey:@"audioscrobblerEnabled"]) {
+               if ([PreferencesController getKeychainItemPasswordForUser:[df stringForKey:@"audioscrobblerUser"]] != nil) {
+                       [[AudioscrobblerController sharedController] attemptHandshake:NO];
+               }
+       }
+
     [networkController startRemoteServerSearch];
     [NSApp deactivate];
        [self performSelector:@selector(rawr) withObject:nil afterDelay:1.0];
@@ -404,7 +412,12 @@ static MainController *sharedController;
 
 - (void)timerUpdate
 {
-       NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier];
+       NSString *identifier = nil;
+       NS_DURING
+               identifier = [[self currentRemote] playerStateUniqueIdentifier];
+       NS_HANDLER
+               [self networkError:localException];
+       NS_ENDHANDLER
        if (refreshTimer && identifier == nil) {
                if ([statusItem isEnabled]) {
                        [statusItem setToolTip:@"iTunes not responding."];
@@ -469,8 +482,8 @@ static MainController *sharedController;
                                if (_audioscrobblerTimer) {
                                        [_audioscrobblerTimer invalidate];
                                }
-                               if (length > 0) {
-                                       _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                               if (length > 30) {
+                                       _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                                }
                        } else {
                                _audioscrobblerTimer = nil;
@@ -595,8 +608,8 @@ static MainController *sharedController;
                        if (_audioscrobblerTimer) {
                                [_audioscrobblerTimer invalidate];
                        }
-                       if (length > 0) {
-                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                       if (length > 30) {
+                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                        }
                } else {
                        _audioscrobblerTimer = nil;
@@ -614,20 +627,29 @@ static MainController *sharedController;
 
 - (void)submitAudioscrobblerTrack:(NSTimer *)timer
 {
+       int interval = [timer timeInterval];
+       [timer invalidate];
+       _audioscrobblerTimer = nil;
+       ITDebugLog(@"Audioscrobbler: Attempting to submit current track");
        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]];
+                       int elapsed = [[self currentRemote] currentSongPlayed];
+                       if ((abs(elapsed - interval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) {
+                               NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist];
+                               if (title && artist) {
+                                       ITDebugLog(@"Audioscrobbler: Submitting current track");
+                                       [[AudioscrobblerController sharedController] submitTrack:title
+                                                                                                                                       artist:artist
+                                                                                                                                       album:[[self currentRemote] currentSongAlbum]
+                                                                                                                                       length:[[self currentRemote] currentSongDuration]];
+                               }
+                       } else if (interval - elapsed > 0) {
+                               ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", interval - elapsed);
+                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:(interval - elapsed) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                        }
                NS_HANDLER
                        [self networkError:localException];
                NS_ENDHANDLER
-               [timer invalidate];
-               _audioscrobblerTimer = nil;
        }
 }
 
@@ -1204,9 +1226,9 @@ static MainController *sharedController;
 
 - (void)updateTime:(NSTimer *)timer
 {
-       StatusWindow *sw = [StatusWindow sharedWindow];
+       StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow];
        _timeUpdateCount++;
-       if (_timeUpdateCount < (int)[sw exitDelay] + (int)[[sw exitEffect] effectTime] + (int)[[sw entryEffect] effectTime]) {
+       if ([sw visibilityState] != ITWindowHiddenState) {
                NSString *time = nil, *length;
                NS_DURING
                        length = [[self currentRemote] currentSongLength];
@@ -1504,12 +1526,14 @@ static MainController *sharedController;
                        if (refreshTimer) {
                                [refreshTimer invalidate];
                        }
-                       refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
-                                                                        target:self
-                                                                        selector:@selector(timerUpdate)
-                                                                        userInfo:nil
-                                                                        repeats:YES] retain];
                }
+               
+               refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
+                                                                target:self
+                                                                selector:@selector(timerUpdate)
+                                                                userInfo:nil
+                                                                repeats:YES] retain];
+               
         [self timerUpdate];
         ITDebugLog(@"Connection successful.");
         return 1;
@@ -1534,10 +1558,12 @@ static MainController *sharedController;
     [networkController disconnect];
     
     if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
+               refreshTimer = nil;
         [self applicationLaunched:nil];
     } else {
         [self applicationTerminated:nil];
     }
+       
     if (refreshTimer) {
                [self timerUpdate];
        };