#import <ITKit/ITCategory-NSMenu.h>
#import "StatusWindow.h"
#import "StatusWindowController.h"
+#import "AudioscrobblerController.h"
#import "StatusItemHack.h"
@interface NSMenu (MenuImpl)
sharedController = self;
_statusWindowUpdateTimer = nil;
+ _audioscrobblerTimer = nil;
remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
[[PreferencesController sharedPrefs] setController:self];
[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
[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
}
}
+- (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
[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]];
}
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;
int rating = -1;
int playCount = -1;
- //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;
- }
-
ITDebugLog(@"Showing track info status window.");
NS_DURING