From 34a2a6ce5cce484c5b8dcd413efa1181130fbab0 Mon Sep 17 00:00:00 2001 From: Kent Sutherland Date: Mon, 26 Dec 2005 03:38:35 +0000 Subject: [PATCH] Fixed a bug in updateTime: Added an album art status window and hot key Added more status loggings that are sent to the Audioscrobbler status field --- AudioscrobblerController.m | 13 +++++++ English.lproj/Localizable.strings | 3 ++ MainController.h | 1 + MainController.m | 65 ++++++++++++++++++++++++++++++- PreferencesController.m | 2 + StatusWindow.h | 1 + StatusWindow.m | 24 ++++++++++-- StatusWindowController.h | 3 ++ StatusWindowController.m | 19 +++++++++ 9 files changed, 126 insertions(+), 5 deletions(-) diff --git a/AudioscrobblerController.m b/AudioscrobblerController.m index 47b4a8b..270704b 100644 --- a/AudioscrobblerController.m +++ b/AudioscrobblerController.m @@ -91,6 +91,10 @@ static AudioscrobblerController *_sharedController = nil; if (!_handshakeCompleted && user) { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=%@&v=%@&u=%@", AUDIOSCROBBLER_ID, AUDIOSCROBBLER_VERSION, user]]; + [_lastStatus release]; + _lastStatus = [NSLocalizedString(@"audioscrobbler_handshaking", @"Attempting to handshake with server") retain]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerStatusChanged" object:nil userInfo:[NSDictionary dictionaryWithObject:_lastStatus forKey:@"StatusString"]]; + _currentStatus = AudioscrobblerRequestingHandshakeStatus; _responseData = [[NSMutableData alloc] init]; [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15] delegate:self]; @@ -210,6 +214,9 @@ static AudioscrobblerController *_sharedController = nil; } ITDebugLog(@"Audioscrobbler: Sending track submission request"); + [_lastStatus release]; + _lastStatus = [NSLocalizedString(@"audioscrobbler_submitting", @"Submitting tracks to server") retain]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerStatusChanged" object:nil userInfo:[NSDictionary dictionaryWithObject:_lastStatus forKey:@"StatusString"]]; //Create and send the request NSMutableURLRequest *request = [[NSURLRequest requestWithURL:_postURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15] mutableCopy]; @@ -337,4 +344,10 @@ static AudioscrobblerController *_sharedController = nil; [_responseData release]; } +-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse +{ + //Don't cache any Audioscrobbler communication + return nil; +} + @end diff --git a/English.lproj/Localizable.strings b/English.lproj/Localizable.strings index fce169b..4e41fa1 100755 --- a/English.lproj/Localizable.strings +++ b/English.lproj/Localizable.strings @@ -34,6 +34,7 @@ repeatOff = "Repeat Off"; repeatPlaylist = "Repeat Playlist"; repeatOneTrack = "Repeat One Track"; noSongPlaying = "No song is playing."; +noAlbumArt = "No art for current song."; iTunesNotResponding = "iTunes is not responding."; noUpcomingSongs = "No upcoming songs."; autolaunch = "Auto-launch MenuTunes"; @@ -72,10 +73,12 @@ iPod = "iPod"; cd = "CD"; sharedLibrary = "Shared Library"; debugmodeenabled = "Debug Mode Enabled\nAll actions MenuTunes performs will be logged\nto the console. To disable debug mode, quit\nand relaunch MenuTunes.\n\n(debug mode is enabled by holding down\nthe control key while MenuTunes opens)"; +audioscrobbler_handshaking = "Attempting to handshake with server"; audioscrobbler_handshake_complete = "Handshake complete"; audioscrobbler_handshake_failed = "Handshake failed"; audioscrobbler_bad_user = "Handshake failed - invalid user name"; audioscrobbler_protocol_error = "Internal protocol error"; +audioscrobbler_submitting = "Sending data to server"; audioscrobbler_submission_ok = "Last track submission successful"; audioscrobbler_bad_password = "Last track submission failed - invalid password"; audioscrobbler_submission_failed = "Last track submission failed - see console for error"; diff --git a/MainController.h b/MainController.h index c4ed84b..10949f8 100755 --- a/MainController.h +++ b/MainController.h @@ -93,6 +93,7 @@ - (void)closePreferences; - (MenuController *)menuController; +- (void)showCurrentAlbumArt; - (void)showCurrentTrackInfo; @end diff --git a/MainController.m b/MainController.m index 20f4381..8a6144e 100755 --- a/MainController.m +++ b/MainController.m @@ -631,7 +631,7 @@ static MainController *sharedController; { ITDebugLog(@"Audioscrobbler: Attempting to submit current track"); [timer invalidate]; - if ([df boolForKey:@"audioscrobblerEnabled"]) { + if ([df boolForKey:@"audioscrobblerEnabled"] && [[AudioscrobblerController sharedController] handshakeCompleted]) { NS_DURING 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)) { @@ -983,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]; @@ -1101,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; @@ -1235,7 +1296,7 @@ static MainController *sharedController; { StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow]; _timeUpdateCount++; - if ([sw visibilityState] != ITWindowHiddenState) { + if ([statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [sw visibilityState] != ITWindowHiddenState) { NSString *time = nil, *length; NS_DURING length = [[self currentRemote] currentSongLength]; diff --git a/PreferencesController.m b/PreferencesController.m index 106b9b6..230bf05 100755 --- a/PreferencesController.m +++ b/PreferencesController.m @@ -239,6 +239,7 @@ static PreferencesController *prefs = nil; @"Rewind", @"ShowPlayer", @"TrackInfo", + @"AlbumArt", @"UpcomingSongs", @"IncrementVolume", @"DecrementVolume", @@ -263,6 +264,7 @@ static PreferencesController *prefs = nil; @"Rewind", @"Show Player", @"Track Info", + @"Album Art", @"Upcoming Songs", @"Increment Volume", @"Decrement Volume", diff --git a/StatusWindow.h b/StatusWindow.h index d44de93..22c4d8f 100755 --- a/StatusWindow.h +++ b/StatusWindow.h @@ -27,6 +27,7 @@ - (void)setImage:(NSImage *)newImage; - (void)setLocked:(BOOL)flag; +- (void)buildImageWindowWithImage:(NSImage *)image; - (void)buildTextWindowWithString:(id)text; - (void)buildMeterWindowWithCharacter:(NSString *)character size:(float)size diff --git a/StatusWindow.m b/StatusWindow.m index 6e4768f..3a84d8b 100755 --- a/StatusWindow.m +++ b/StatusWindow.m @@ -116,7 +116,7 @@ float maxHeight = ( screenHeight - (SW_BORDER * 2) ); float excessWidth = 0.0; float excessHeight = 0.0; - NSPoint windowOrigin; + NSPoint windowOrigin = NSZeroPoint; ITImageView *imageView; BOOL shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) || ([self visibilityState] == ITWindowVanishingState)) ); @@ -135,7 +135,7 @@ contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight ); // Setup the Window, and remove all its contentview's subviews. - windowWidth = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) ); + windowWidth = ( (SW_PAD / divisor) + imageWidth + ((dataWidth > 0) ? (SW_SPACE / divisor) + dataWidth : 0) + (SW_PAD / divisor) ); windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) ); // Constrain size to max limits. Adjust data sizes accordingly. @@ -177,7 +177,7 @@ [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; // Setup, position, fill, and add the image view to the content view. - imageRect = NSMakeRect( (SW_PAD / divisor) + 4, + imageRect = NSMakeRect( (SW_PAD / divisor) + ((dataWidth > 0) ? 4 : 0), ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)), imageWidth, imageHeight ); @@ -193,6 +193,24 @@ dataHeight); } +- (void)buildImageWindowWithImage:(NSImage *)image +{ + if (!_locked) { + float divisor = 1.0; + NSRect dataRect; + + if (_sizing == ITTransientStatusWindowSmall) { + divisor = SMALL_DIVISOR; + } else if (_sizing == ITTransientStatusWindowMini) { + divisor = MINI_DIVISOR; + } + + [self setImage:image]; + dataRect = [self setupWindowWithDataSize:NSMakeSize(0, 0)]; //We have no text, so there is no data + [[self contentView] setNeedsDisplay:YES]; + } +} + - (void)buildTextWindowWithString:(id)text { if ( ! _locked ) { diff --git a/StatusWindowController.h b/StatusWindowController.h index 18443dc..ea9faef 100755 --- a/StatusWindowController.h +++ b/StatusWindowController.h @@ -26,6 +26,7 @@ typedef enum { typedef enum { StatusWindowNoType = -1, StatusWindowTrackInfoType, + StatusWindowAlbumArtType, StatusWindowUpcomingSongsType, StatusWindowVolumeType, StatusWindowRatingType, @@ -62,6 +63,8 @@ typedef enum { - (void)showPreferencesUpdateWindow; - (void)showDebugModeEnabledWindow; +- (void)showAlbumArtWindowWithImage:(NSImage *)image; +- (void)showAlbumArtWindowWithErrorText:(NSString *)string; - (void)showSongInfoWindowWithSource:(ITMTRemotePlayerSource)source title: (NSString *)title album: (NSString *)album diff --git a/StatusWindowController.m b/StatusWindowController.m index 437487b..7663d39 100755 --- a/StatusWindowController.m +++ b/StatusWindowController.m @@ -199,6 +199,25 @@ static StatusWindowController *sharedController; [text release]; } +- (void)showAlbumArtWindowWithImage:(NSImage *)image +{ + if (image) { + _currentType = StatusWindowAlbumArtType; + [_window setImage:[NSImage imageNamed:@"Library"]]; + [_window buildImageWindowWithImage:image]; + [_window appear:self]; + } +} + +- (void)showAlbumArtWindowWithErrorText:(NSString *)string +{ + if (string && [string length] > 0) { + _currentType = StatusWindowAlbumArtType; + [_window buildTextWindowWithString:string]; + [_window appear:self]; + } +} + - (void)showUpcomingSongsWindowWithTitles:(NSArray *)titleStrings { // NSString *bull = [NSString stringWithUTF8String:"‣ "]; -- 2.20.1