+- (void)trackChanged:(NSNotification *)note
+{
+ //If we're running the timer, shut it off since we don't need it!
+ /*if (refreshTimer && [refreshTimer isValid]) {
+ ITDebugLog(@"Invalidating refresh timer.");
+ [refreshTimer invalidate];
+ [refreshTimer release];
+ refreshTimer = nil;
+ }*/
+
+ if (![self songChanged]) {
+ return;
+ }
+ NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier];
+ if ( [df boolForKey:@"showSongInfoOnChange"] ) {
+ [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
+ }
+ [_lastTrackInfo release];
+ _lastTrackInfo = [[note userInfo] retain];
+
+ [self setLatestSongIdentifier:identifier];
+ ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
+ if ([df boolForKey:@"runScripts"]) {
+ NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
+ NSEnumerator *scriptsEnum = [scripts objectEnumerator];
+ NSString *nextScript;
+ ITDebugLog(@"Running AppleScripts for song change.");
+ while ( (nextScript = [scriptsEnum nextObject]) ) {
+ NSDictionary *error;
+ NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
+ ITDebugLog(@"Running script: %@", nextScript);
+ if (!currentScript || ![currentScript executeAndReturnError:nil]) {
+ ITDebugLog(@"Error running script %@.", nextScript);
+ }
+ [currentScript release];
+ }
+ }
+
+ [statusItem setEnabled:NO];
+
+ NS_DURING
+ latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
+
+ if ([menuController rebuildSubmenus]) {
+ /*if ( [df boolForKey:@"showSongInfoOnChange"] ) {
+ [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
+ }*/
+ [self setLatestSongIdentifier:identifier];
+ //Create the tooltip for the status item
+ if ( [df boolForKey:@"showToolTip"] ) {
+ ITDebugLog(@"Creating status item tooltip.");
+ NSString *artist = [_lastTrackInfo objectForKey:@"Artist"], *title = [_lastTrackInfo objectForKey:@"Name"];
+ if (artist) {
+ _toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
+ } else if (title) {
+ _toolTip = title;
+ } else {
+ _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");;
+ }
+ [statusItem setToolTip:_toolTip];
+ } else {
+ [statusItem setToolTip:nil];
+ }
+ }
+
+ if ([df boolForKey:@"audioscrobblerEnabled"]) {
+ int length = [[self currentRemote] currentSongDuration];
+ if (_audioscrobblerTimer) {
+ [_audioscrobblerTimer invalidate];
+ }
+ if (length > 30) {
+ _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length / 2 < 240) ? length / 2 : 240) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
+ }
+ } else {
+ _audioscrobblerTimer = nil;
+ }
+ NS_HANDLER
+ [self networkError:localException];
+ NS_ENDHANDLER
+ timerUpdating = NO;
+ [statusItem setEnabled:YES];
+
+ if ([networkController isConnectedToServer]) {
+ [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
+ }
+}
+
+- (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
+ 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
+ }
+}
+