X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/d33e9b61891ddd4d4e70422f3f20c693d0ee030c..d7c39a05d1ea7368700d2140c7f042cc72cf0d0d:/MainController.m diff --git a/MainController.m b/MainController.m index 4994a7d..294e52c 100755 --- a/MainController.m +++ b/MainController.m @@ -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"]) { @@ -410,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."]; @@ -472,11 +479,12 @@ static MainController *sharedController; if ([df boolForKey:@"audioscrobblerEnabled"]) { int length = [[self currentRemote] currentSongDuration]; - if (_audioscrobblerTimer) { - [_audioscrobblerTimer invalidate]; - } if (length > 30) { - _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES]; + _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240); + [_audioscrobblerTimer invalidate]; + [_audioscrobblerTimer release]; + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:nil target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; } } else { _audioscrobblerTimer = nil; @@ -598,11 +606,12 @@ static MainController *sharedController; if ([df boolForKey:@"audioscrobblerEnabled"]) { int length = [[self currentRemote] currentSongDuration]; - if (_audioscrobblerTimer) { - [_audioscrobblerTimer invalidate]; - } if (length > 30) { - _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES]; + _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240); + [_audioscrobblerTimer invalidate]; + [_audioscrobblerTimer release]; + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:1.0 target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; } } else { _audioscrobblerTimer = nil; @@ -620,15 +629,14 @@ static MainController *sharedController; - (void)submitAudioscrobblerTrack:(NSTimer *)timer { - int interval = [timer timeInterval]; - [timer invalidate]; - _audioscrobblerTimer = nil; ITDebugLog(@"Audioscrobbler: Attempting to submit current track"); + [timer invalidate]; if ([df boolForKey:@"audioscrobblerEnabled"]) { NS_DURING - int elapsed = [[self currentRemote] currentSongPlayed]; - if ((abs(elapsed - interval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) { + int elapsed = [[self currentRemote] currentSongPlayed], length = [[self currentRemote] currentSongDuration], requiredInterval = ((length / 2 < 240) ? length / 2 : 240); + if ((abs(elapsed - requiredInterval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) { NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist]; + NSLog(@"Audioscrobbler: Submitting"); if (title && artist) { ITDebugLog(@"Audioscrobbler: Submitting current track"); [[AudioscrobblerController sharedController] submitTrack:title @@ -636,9 +644,15 @@ static MainController *sharedController; 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]; + } else if (requiredInterval - elapsed > 0) { + _audioscrobblerInterval = requiredInterval - elapsed; + [_audioscrobblerTimer release]; + NSLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval); + ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval); + _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:1.0 target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode]; + } else { + NSLog(@"Audioscrobbler: Skipping submission. %i, %i", requiredInterval, elapsed); } NS_HANDLER [self networkError:localException]; @@ -969,6 +983,16 @@ static MainController *sharedController; [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; } + if ([df objectForKey:@"AlbumArt"] != nil) { + ITDebugLog(@"Setting up album art hot key."); + hotKey = [[ITHotKey alloc] init]; + [hotKey setName:@"AlbumArt"]; + [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"AlbumArt"]]]; + [hotKey setTarget:self]; + [hotKey setAction:@selector(showCurrentAlbumArtHotKey)]; + [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]]; + } + if ([df objectForKey:@"UpcomingSongs"] != nil) { ITDebugLog(@"Setting up upcoming songs hot key."); hotKey = [[ITHotKey alloc] init]; @@ -1087,6 +1111,57 @@ static MainController *sharedController; [self showCurrentTrackInfo]; } +- (void)showCurrentAlbumArtHotKey +{ + //If we're already visible and the setting says so, vanish instead of displaying again. + if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowAlbumArtType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) { + ITDebugLog(@"Art window is already visible, hiding track window."); + [[StatusWindow sharedWindow] vanish:nil]; + return; + } + [self showCurrentAlbumArt]; +} + +- (void)showCurrentAlbumArt +{ + NSImage *art = nil; + NS_DURING + art = [[self currentRemote] currentSongAlbumArt]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + + if (art) { + NSSize oldSize = [art size], newSize; + if (oldSize.width > 300 && oldSize.height > 300) { + if (oldSize.width > oldSize.height) { + newSize = NSMakeSize(300, oldSize.height * (300.0f / oldSize.width)); + } else { + newSize = NSMakeSize(oldSize.width * (300.0f / oldSize.height), 300); + } + } else { + newSize = oldSize; + } + + art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; + + [statusWindowController showAlbumArtWindowWithImage:art]; + } else { + NSString *string = nil; + NS_DURING + if ([[self currentRemote] currentSongTitle]) { + string = NSLocalizedString(@"noAlbumArt", @"No art for current song."); + } else { + string = NSLocalizedString(@"noSongPlaying", @"No song is playing."); + } + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER + //Show the no song playing window if there is no album art or no track is playing + [statusWindowController showAlbumArtWindowWithErrorText:string]; + } +} + - (void)showCurrentTrackInfo { ITMTRemotePlayerSource source = 0; @@ -1219,9 +1294,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 ([statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [sw visibilityState] != ITWindowHiddenState) { NSString *time = nil, *length; NS_DURING length = [[self currentRemote] currentSongLength]; @@ -1519,12 +1594,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; @@ -1549,10 +1626,12 @@ static MainController *sharedController; [networkController disconnect]; if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) { + refreshTimer = nil; [self applicationLaunched:nil]; } else { [self applicationTerminated:nil]; } + if (refreshTimer) { [self timerUpdate]; };