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;
#import <openssl/evp.h>
#import <ITFoundation/ITDebug.h>
-#define AUDIOSCROBBLER_ID @"tst"
+#define AUDIOSCROBBLER_ID @"mtu"
#define AUDIOSCROBBLER_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
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];
[super dealloc];
}
+- (NSString *)lastStatus
+{
+ return _lastStatus;
+}
+
- (void)attemptHandshake
{
[self attemptHandshake:NO];
- (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];
}
}
- (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);
}
{
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];
_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
}
}
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];
}
artistCheckbox = NSButton;
audioscrobblerEnabledCheckbox = NSButton;
audioscrobblerPasswordTextField = NSSecureTextField;
+ audioscrobblerStatusTextField = NSTextField;
audioscrobblerUseCacheCheckbox = NSButton;
audioscrobblerUserTextField = NSTextField;
backgroundColorPopup = NSPopUpButton;
<string>386 450 380 122 0 0 1152 746 </string>
</dict>
<key>IBFramework Version</key>
- <string>439.0</string>
+ <string>443.0</string>
<key>IBLockedObjects</key>
<array>
<integer>281</integer>
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;
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;
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;
- (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");
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];
IBOutlet NSButton *artistCheckbox;
IBOutlet NSButton *audioscrobblerEnabledCheckbox;
IBOutlet NSTextField *audioscrobblerPasswordTextField;
+ IBOutlet NSTextField *audioscrobblerStatusTextField;
IBOutlet NSButton *audioscrobblerUseCacheCheckbox;
IBOutlet NSTextField *audioscrobblerUserTextField;
IBOutlet NSPopUpButton *backgroundStylePopup;
@"Toggle Loop",
@"Toggle Song Included In Shuffle",
@"Pop-up status menu",
- [NSString stringWithUTF8String:"Set Rating: â\98\86â\98\86â\98\86â\98\86â\98\86"],
- [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\86â\98\86â\98\86â\98\86"],
- [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\86â\98\86â\98\86"],
- [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\86â\98\86"],
- [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\85â\98\86"],
- [NSString stringWithUTF8String:"Set Rating: â\98\85â\98\85â\98\85â\98\85â\98\85"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9câ\80\9aòÃ\9c"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9câ\80\9aòÃ\9c"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\9c"],
+ [NSString stringWithUTF8String:"Set Rating: â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96â\80\9aòÃ\96"],
nil];
hotKeysDictionary = [[NSMutableDictionary alloc] init];
controller = 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];
- (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) {
#pragma mark PRIVATE METHOD IMPLEMENTATIONS
/*************************************************************************/
+- (void)audioscrobblerStatusChanged:(NSNotification *)note
+{
+ [audioscrobblerStatusTextField setStringValue:[[note userInfo] objectForKey:@"StatusString"]];
+}
+
- (void)setupWindow
{
ITDebugLog(@"Loading Preferences.nib.");
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.");
- (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"];
- (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"];