From: Kent Sutherland Date: Fri, 23 Dec 2005 09:36:21 +0000 (+0000) Subject: Added a status field to the audioscrobbler preferences. Using alloc'd NSTimers in... X-Git-Tag: v1.6.5~13 X-Git-Url: http://git.ithinksw.org/MenuTunes.git/commitdiff_plain/865eb081d76064bc6ec16c44f6ac976a9b1a62b3 Added a status field to the audioscrobbler preferences. Using alloc'd NSTimers in order to avoid crashing, hopefully. Disabling caching of tracks in order to simplify things. --- diff --git a/AudioscrobblerController.h b/AudioscrobblerController.h index 78cfdb3..a2dc6c4 100644 --- a/AudioscrobblerController.h +++ b/AudioscrobblerController.h @@ -27,12 +27,13 @@ typedef enum { NSMutableArray *_tracks, *_submitTracks; NSDate *_delayDate; - NSString *_md5Challenge; + NSString *_md5Challenge, *_lastStatus; NSURL *_postURL; NSMutableData *_responseData; } + (AudioscrobblerController *)sharedController; +- (NSString *)lastStatus; - (void)attemptHandshake; - (void)attemptHandshake:(BOOL)force; - (BOOL)handshakeCompleted; diff --git a/AudioscrobblerController.m b/AudioscrobblerController.m index a3d3b6b..3864a0c 100644 --- a/AudioscrobblerController.m +++ b/AudioscrobblerController.m @@ -16,7 +16,7 @@ #import #import -#define AUDIOSCROBBLER_ID @"tst" +#define AUDIOSCROBBLER_ID @"mtu" #define AUDIOSCROBBLER_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] static AudioscrobblerController *_sharedController = nil; @@ -46,13 +46,14 @@ static AudioscrobblerController *_sharedController = nil; _responseData = nil; _tracks = [[NSMutableArray alloc] init]; _submitTracks = [[NSMutableArray alloc] init]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioscrobblerNotification:) name:nil object:self]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioscrobblerNotification:) name:@"AudioscrobblerHandshakeComplete" object:self]; } return self; } - (void)dealloc { + [_lastStatus release]; [_md5Challenge release]; [_postURL release]; [_responseData release]; @@ -62,6 +63,11 @@ static AudioscrobblerController *_sharedController = nil; [super dealloc]; } +- (NSString *)lastStatus +{ + return _lastStatus; +} + - (void)attemptHandshake { [self attemptHandshake:NO]; @@ -221,10 +227,8 @@ static AudioscrobblerController *_sharedController = nil; - (void)handleAudioscrobblerNotification:(NSNotification *)note { - if ([[note name] isEqualToString:@"AudioscrobblerHandshakeComplete"]) { - if ([_tracks count] > 0) { - [self performSelector:@selector(submitTracks) withObject:nil afterDelay:2]; - } + if ([_tracks count] > 0) { + [self performSelector:@selector(submitTracks) withObject:nil afterDelay:2]; } } @@ -232,6 +236,9 @@ static AudioscrobblerController *_sharedController = nil; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + [_lastStatus release]; + _lastStatus = [[NSString stringWithFormat:NSLocalizedString(@"audioscrobbler_error", @"Error - %@"), [error localizedDescription]] retain]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerStatusChanged" object:self userInfo:[NSDictionary dictionaryWithObject:_lastStatus forKey:@"StatusString"]]; ITDebugLog(@"Audioscrobbler: Connection error \"%@\"", error); } @@ -244,7 +251,7 @@ static AudioscrobblerController *_sharedController = nil; { NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding]; NSArray *lines = [string componentsSeparatedByString:@"\n"]; - NSString *responseAction = nil; + NSString *responseAction = nil, *key = nil, *comment = nil; if ([lines count] > 0) { responseAction = [lines objectAtIndex:0]; @@ -261,33 +268,52 @@ static AudioscrobblerController *_sharedController = nil; _postURL = [[NSURL alloc] initWithString:[lines objectAtIndex:2]]; _handshakeCompleted = YES; [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerHandshakeComplete" object:self]; + key = @"audioscrobbler_handshake_complete"; + comment = @"Handshake complete"; } else { //We have a protocol error } } else if (([responseAction length] > 5) && [[responseAction substringToIndex:5] isEqualToString:@"FAILED"]) { ITDebugLog(@"Audioscrobbler: Handshake failed (%@)", [responseAction substringFromIndex:6]); + key = @"audioscrobbler_handshake_failed"; + comment = @"Handshake failed"; //We have a error } else if ([responseAction isEqualToString:@"BADUSER"]) { ITDebugLog(@"Audioscrobbler: Bad user name"); + key = @"audioscrobbler_bad_user"; + comment = @"Handshake failed - invalid user name"; //We have a bad user } else { ITDebugLog(@"Audioscrobbler: Handshake failed, protocol error"); + key = @"audioscrobbler_protocol_error"; + comment = @"Internal protocol error"; //We have a protocol error } } else if (_currentStatus == AudioscrobblerSubmittingTracksStatus) { + //For now we're not going to cache results, as it is less of a headache + [_tracks removeObjectsInArray:_submitTracks]; + [_submitTracks removeAllObjects]; + if ([responseAction isEqualToString:@"OK"]) { ITDebugLog(@"Audioscrobbler: Submission successful, clearing queue."); - [_tracks removeObjectsInArray:_submitTracks]; - [_submitTracks removeAllObjects]; + /*[_tracks removeObjectsInArray:_submitTracks]; + [_submitTracks removeAllObjects];*/ if ([_tracks count] > 0) { ITDebugLog(@"Audioscrobbler: Tracks remaining in queue, submitting remaining tracks"); [self performSelector:@selector(submitTracks) withObject:nil afterDelay:2]; } + key = @"audioscrobbler_submission_ok"; + comment = @"Last track submission successful"; } else if ([responseAction isEqualToString:@"BADAUTH"]) { ITDebugLog(@"Audioscrobbler: Bad password"); + key = @"audioscrobbler_bad_password"; + comment = @"Last track submission failed - invalid password"; //Bad auth } else if (([responseAction length] > 5) && [[responseAction substringToIndex:5] isEqualToString:@"FAILED"]) { ITDebugLog(@"Audioscrobbler: Submission failed (%@)", [responseAction substringFromIndex:6]); + NSLog(@"Audioscrobbler: Submission failed (%@)", [responseAction substringFromIndex:6]); + key = @"audioscrobbler_submission_failed"; + comment = @"Last track submission failed - see console for error"; //Failed } } @@ -302,7 +328,9 @@ static AudioscrobblerController *_sharedController = nil; ITDebugLog(@"No interval response."); //We have a protocol error } - + [_lastStatus release]; + _lastStatus = [NSLocalizedString(key, comment) retain]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerStatusChanged" object:nil userInfo:[NSDictionary dictionaryWithObject:_lastStatus forKey:@"StatusString"]]; [string release]; [_responseData release]; } diff --git a/English.lproj/Preferences.nib/classes.nib b/English.lproj/Preferences.nib/classes.nib index 3f7a192..9ddd004 100755 --- a/English.lproj/Preferences.nib/classes.nib +++ b/English.lproj/Preferences.nib/classes.nib @@ -23,6 +23,7 @@ artistCheckbox = NSButton; audioscrobblerEnabledCheckbox = NSButton; audioscrobblerPasswordTextField = NSSecureTextField; + audioscrobblerStatusTextField = NSTextField; audioscrobblerUseCacheCheckbox = NSButton; audioscrobblerUserTextField = NSTextField; backgroundColorPopup = NSPopUpButton; diff --git a/English.lproj/Preferences.nib/info.nib b/English.lproj/Preferences.nib/info.nib index 33225c9..4c9701e 100755 --- a/English.lproj/Preferences.nib/info.nib +++ b/English.lproj/Preferences.nib/info.nib @@ -12,7 +12,7 @@ 386 450 380 122 0 0 1152 746 IBFramework Version - 439.0 + 443.0 IBLockedObjects 281 diff --git a/English.lproj/Preferences.nib/keyedobjects.nib b/English.lproj/Preferences.nib/keyedobjects.nib index 48b0873..351b863 100755 Binary files a/English.lproj/Preferences.nib/keyedobjects.nib and b/English.lproj/Preferences.nib/keyedobjects.nib differ diff --git a/MainController.h b/MainController.h index 7348a81..c4ed84b 100755 --- a/MainController.h +++ b/MainController.h @@ -47,6 +47,7 @@ BOOL timerUpdating, _checkingForServer, _popped, _open, _needsPolling; BOOL blinged; int _timeUpdateCount; //Keeps track of how many times the time has been updated in the info status window + int _audioscrobblerInterval; NSLock *_serverCheckLock; } + (MainController *)sharedController; diff --git a/MainController.m b/MainController.m index 14bfb59..b39d34d 100755 --- a/MainController.m +++ b/MainController.m @@ -479,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 / 2 < 240) ? length / 2 : 240) 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; @@ -605,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 / 2 < 240) ? length / 2 : 240) 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; @@ -627,14 +629,12 @@ 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)) { + if ((abs(elapsed - _audioscrobblerInterval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) { NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist]; if (title && artist) { ITDebugLog(@"Audioscrobbler: Submitting current track"); @@ -643,9 +643,12 @@ 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 (_audioscrobblerInterval - elapsed > 0) { + ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval - elapsed); + _audioscrobblerInterval -= elapsed; + [_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]; } NS_HANDLER [self networkError:localException]; diff --git a/PreferencesController.h b/PreferencesController.h index 06841ef..5b2810b 100755 --- a/PreferencesController.h +++ b/PreferencesController.h @@ -26,6 +26,7 @@ IBOutlet NSButton *artistCheckbox; IBOutlet NSButton *audioscrobblerEnabledCheckbox; IBOutlet NSTextField *audioscrobblerPasswordTextField; + IBOutlet NSTextField *audioscrobblerStatusTextField; IBOutlet NSButton *audioscrobblerUseCacheCheckbox; IBOutlet NSTextField *audioscrobblerUserTextField; IBOutlet NSPopUpButton *backgroundStylePopup; diff --git a/PreferencesController.m b/PreferencesController.m index 638be40..106b9b6 100755 --- a/PreferencesController.m +++ b/PreferencesController.m @@ -272,12 +272,12 @@ static PreferencesController *prefs = nil; @"Toggle Loop", @"Toggle Song Included In Shuffle", @"Pop-up status menu", - [NSString stringWithUTF8String:"Set Rating: ☆☆☆☆☆"], - [NSString stringWithUTF8String:"Set Rating: ★☆☆☆☆"], - [NSString stringWithUTF8String:"Set Rating: ★★☆☆☆"], - [NSString stringWithUTF8String:"Set Rating: ★★★☆☆"], - [NSString stringWithUTF8String:"Set Rating: ★★★★☆"], - [NSString stringWithUTF8String:"Set Rating: ★★★★★"], + [NSString stringWithUTF8String:"Set Rating: ‚òÜ‚òÜ‚òÜ‚òÜ‚òÜ"], + [NSString stringWithUTF8String:"Set Rating: ‚òÖ‚òÜ‚òÜ‚òÜ‚òÜ"], + [NSString stringWithUTF8String:"Set Rating: ‚òÖ‚òÖ‚òÜ‚òÜ‚òÜ"], + [NSString stringWithUTF8String:"Set Rating: ‚òÖ‚òÖ‚òÖ‚òÜ‚òÜ"], + [NSString stringWithUTF8String:"Set Rating: ‚òÖ‚òÖ‚òÖ‚òÖ‚òÜ"], + [NSString stringWithUTF8String:"Set Rating: ‚òÖ‚òÖ‚òÖ‚òÖ‚òÖ"], nil]; hotKeysDictionary = [[NSMutableDictionary alloc] init]; controller = nil; @@ -426,6 +426,9 @@ static PreferencesController *prefs = nil; [audioscrobblerUseCacheCheckbox setEnabled:SENDER_STATE]; [audioscrobblerUserTextField setEnabled:SENDER_STATE]; [audioscrobblerPasswordTextField setEnabled:SENDER_STATE]; + if (SENDER_STATE) { + [[AudioscrobblerController sharedController] attemptHandshake:NO]; + } } else if ( [sender tag ] == 6015) { //Here we create a new keychain item if needed and deletes the keychain item if the field is cleared. NSString *currentAccount = [df stringForKey:@"audioscrobblerUser"], *newAccount = [sender stringValue]; @@ -607,7 +610,7 @@ static PreferencesController *prefs = nil; - (IBAction)changeStatusWindowSetting:(id)sender { - StatusWindow *sw = [StatusWindow sharedWindow]; + StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow]; ITDebugLog(@"Changing status window setting of tag %i", [sender tag]); if ( [sender tag] == 2010) { @@ -890,6 +893,11 @@ static PreferencesController *prefs = nil; #pragma mark PRIVATE METHOD IMPLEMENTATIONS /*************************************************************************/ +- (void)audioscrobblerStatusChanged:(NSNotification *)note +{ + [audioscrobblerStatusTextField setStringValue:[[note userInfo] objectForKey:@"StatusString"]]; +} + - (void)setupWindow { ITDebugLog(@"Loading Preferences.nib."); @@ -975,6 +983,12 @@ static PreferencesController *prefs = nil; int selectedBGStyle; id anItem; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioscrobblerStatusChanged:) name:@"AudioscrobblerStatusChanged" object:nil]; + if ([df boolForKey:@"audioscrobblerEnabled"]) { + NSString *status = [[AudioscrobblerController sharedController] lastStatus]; + [audioscrobblerStatusTextField setStringValue:(status == nil) ? @"Idle" : status]; + } + [df setInteger:MT_CURRENT_VERSION forKey:@"appVersion"]; ITDebugLog(@"Setting up preferences UI."); @@ -1166,7 +1180,7 @@ static PreferencesController *prefs = nil; - (void)setStatusWindowEntryEffect:(Class)effectClass { - StatusWindow *sw = [StatusWindow sharedWindow]; + StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow]; float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8); [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowAppearanceEffect"]; @@ -1177,7 +1191,7 @@ static PreferencesController *prefs = nil; - (void)setStatusWindowExitEffect:(Class)effectClass { - StatusWindow *sw = [StatusWindow sharedWindow]; + StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow]; float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8); [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowVanishEffect"];