X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/6f3e95b38759c72706e83ff140ae17667cdfad22..3af6b8277d3305427112b099523abeab3c231816:/MainController.m diff --git a/MainController.m b/MainController.m index cfc9518..4994a7d 100755 --- a/MainController.m +++ b/MainController.m @@ -9,6 +9,7 @@ #import #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]; @@ -196,6 +198,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]; @@ -461,6 +469,18 @@ static MainController *sharedController; [statusItem setToolTip:nil]; } } + + 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]; + } + } else { + _audioscrobblerTimer = nil; + } NS_HANDLER [self networkError:localException]; NS_ENDHANDLER @@ -575,6 +595,18 @@ static MainController *sharedController; [statusItem setToolTip:nil]; } } + + 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]; + } + } else { + _audioscrobblerTimer = nil; + } NS_HANDLER [self networkError:localException]; NS_ENDHANDLER @@ -586,6 +618,34 @@ 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 + 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 + } +} + // // // Menu Selectors @@ -905,7 +965,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 +1075,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; @@ -1039,16 +1111,18 @@ static MainController *sharedController; if ( title ) { if ( [df boolForKey:@"showAlbumArtwork"] ) { - NSSize oldSize, newSize; - NS_DURING - art = [[self currentRemote] currentSongAlbumArt]; - oldSize = [art size]; - if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width)); - else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110); - art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; - NS_HANDLER - [self networkError:localException]; - NS_ENDHANDLER + NSSize oldSize, newSize; + NS_DURING + art = [[self currentRemote] currentSongAlbumArt]; + oldSize = [art size]; + if (oldSize.width > oldSize.height) { + newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width)); + } + else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110); + art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize]; + NS_HANDLER + [self networkError:localException]; + NS_ENDHANDLER } if ( [df boolForKey:@"showAlbum"] ) { @@ -1329,9 +1403,11 @@ static MainController *sharedController; - (void)setRating:(ITHotKey *)sender { - int stars = [[sender name] characterAtIndex:9] - 48; - [self selectSongRating:stars * 20]; - [statusWindowController showRatingWindowWithRating:(float)stars / 5.0]; + if ([self songIsPlaying]) { + int stars = [[sender name] characterAtIndex:9] - 48; + [self selectSongRating:stars * 20]; + [statusWindowController showRatingWindowWithRating:(float)stars / 5.0]; + } } - (void)toggleLoop