1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import "NetworkController.h"
5 #import "NetworkObject.h"
6 #import <ITKit/ITHotKeyCenter.h>
7 #import <ITKit/ITHotKey.h>
8 #import <ITKit/ITKeyCombo.h>
9 #import <ITKit/ITCategory-NSMenu.h>
10 #import "StatusWindow.h"
11 #import "StatusWindowController.h"
12 #import "AudioscrobblerController.h"
13 #import "StatusItemHack.h"
15 @interface NSMenu (MenuImpl)
19 @interface NSCarbonMenuImpl:NSObject
25 + (void)setupForNoMenuBar;
29 - (void)itemChanged:fp8;
30 - (void)itemAdded:fp8;
31 - (void)itemRemoved:fp8;
32 - (void)performActionWithHighlightingForItemAtIndex:(int)fp8;
33 - (void)performMenuAction:(SEL)fp8 withTarget:fp12;
34 - (void)setupCarbonMenuBar;
35 - (void)setAsMainCarbonMenuBar;
36 - (void)clearAsMainCarbonMenuBar;
37 - (void)popUpMenu:fp8 atLocation:(NSPoint)fp12 width:(float)fp20 forView:fp24 withSelectedItem:(int)fp28 withFont:fp32;
38 - (void)_popUpContextMenu:fp8 withEvent:fp12 forView:fp16 withFont:fp20;
39 - (void)_popUpContextMenu:fp8 withEvent:fp12 forView:fp16;
43 @implementation NSImage (SmoothAdditions)
45 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
48 NSImageRep *rep = [self bestRepresentationForDevice:nil];
50 newImage = [[NSImage alloc] initWithSize:scaledSize];
53 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
54 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
55 [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
57 [newImage unlockFocus];
58 return [newImage autorelease];
63 @interface MainController(Private)
64 - (ITMTRemote *)loadRemote;
65 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
66 - (void)applicationLaunched:(NSNotification *)note;
67 - (void)applicationTerminated:(NSNotification *)note;
69 - (void)invalidateStatusWindowUpdateTimer;
72 static MainController *sharedController;
74 @implementation MainController
76 + (MainController *)sharedController
78 return sharedController;
81 /*************************************************************************/
83 #pragma mark INITIALIZATION/DEALLOCATION METHODS
84 /*************************************************************************/
88 if ( ( self = [super init] ) ) {
89 sharedController = self;
91 _statusWindowUpdateTimer = nil;
92 _audioscrobblerTimer = nil;
94 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
95 [[PreferencesController sharedPrefs] setController:self];
96 statusWindowController = [StatusWindowController sharedController];
97 menuController = [[MenuController alloc] init];
98 df = [[NSUserDefaults standardUserDefaults] retain];
105 - (void)applicationDidFinishLaunching:(NSNotification *)note
107 NSString *iTunesPath = [df stringForKey:@"CustomPlayerPath"];
108 NSDictionary *iTunesInfoPlist;
111 //Turn on debug mode if needed
112 /*if ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0)
113 if ((GetCurrentKeyModifiers() & (optionKey | rightOptionKey)) != 0)
114 if ((GetCurrentKeyModifiers() & (shiftKey | rightShiftKey)) != 0)*/
115 if ([df boolForKey:@"ITDebugMode"] || ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0)) {
117 [[StatusWindowController sharedController] showDebugModeEnabledWindow];
120 //Check if iTunes 4.7 or later is installed
122 iTunesPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"];
124 iTunesInfoPlist = [[NSBundle bundleWithPath:iTunesPath] infoDictionary];
125 iTunesVersion = [[iTunesInfoPlist objectForKey:@"CFBundleVersion"] floatValue];
126 ITDebugLog(@"iTunes version found: %f.", iTunesVersion);
127 if (iTunesVersion >= 4.7) {
133 if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) {
134 [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
136 [[PreferencesController sharedPrefs] registerDefaults];
137 [[StatusWindowController sharedController] showPreferencesUpdateWindow];
140 currentRemote = [self loadRemote];
141 [[self currentRemote] begin];
143 [[self currentRemote] currentSongElapsed];
145 //Turn on network stuff if needed
146 networkController = [[NetworkController alloc] init];
147 if ([df boolForKey:@"enableSharing"]) {
148 [self setServerStatus:YES];
149 } else if ([df boolForKey:@"useSharedPlayer"]) {
150 [self checkForRemoteServerAndConnectImmediately:YES];
153 //Setup for notification of the remote player launching or quitting
154 [[[NSWorkspace sharedWorkspace] notificationCenter]
156 selector:@selector(applicationTerminated:)
157 name:NSWorkspaceDidTerminateApplicationNotification
160 [[[NSWorkspace sharedWorkspace] notificationCenter]
162 selector:@selector(applicationLaunched:)
163 name:NSWorkspaceDidLaunchApplicationNotification
166 if (![df objectForKey:@"menu"]) { // If this is nil, defaults have never been registered.
167 [[PreferencesController sharedPrefs] registerDefaults];
170 if ([df boolForKey:@"ITMTNoStatusItem"]) {
173 [StatusItemHack install];
174 statusItem = [[ITStatusItem alloc]
175 initWithStatusBar:[NSStatusBar systemStatusBar]
176 withLength:NSSquareStatusItemLength];
179 /*bling = [[MTBlingController alloc] init];
181 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
183 selector:@selector(blingTime)
185 repeats:YES] retain];*/
188 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
189 [self applicationLaunched:nil];
191 if ([df boolForKey:@"LaunchPlayerWithMT"])
194 [self applicationTerminated:nil];
197 [self networkError:localException];
200 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
201 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
203 if ([df boolForKey:@"audioscrobblerEnabled"]) {
204 if ([PreferencesController getKeychainItemPasswordForUser:[df stringForKey:@"audioscrobblerUser"]] != nil) {
205 [[AudioscrobblerController sharedController] attemptHandshake:NO];
209 [networkController startRemoteServerSearch];
211 [self performSelector:@selector(rawr) withObject:nil afterDelay:1.0];
213 bling = [[MTBlingController alloc] init];
215 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
217 selector:@selector(blingTime)
219 repeats:YES] retain];
227 - (ITMTRemote *)loadRemote
229 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
230 ITDebugLog(@"Gathering remotes.");
232 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
233 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
234 NSString *bundlePath;
236 while ( (bundlePath = [enumerator nextObject]) ) {
237 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
240 Class remoteClass = [remoteBundle principalClass];
242 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
243 [(NSObject *)remoteClass isKindOfClass:[NSObject class]]) {
244 id remote = [remoteClass remote];
245 ITDebugLog(@"Adding remote at path %@", bundlePath);
246 [remoteArray addObject:remote];
251 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
252 // if ( [remoteArray count] > 1 ) {
253 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
255 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
258 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
259 return [remoteArray objectAtIndex:0];
262 /*************************************************************************/
264 #pragma mark INSTANCE METHODS
265 /*************************************************************************/
267 /*- (void)startTimerInNewThread
269 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
270 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
271 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
273 selector:@selector(timerUpdate)
275 repeats:YES] retain];
277 ITDebugLog(@"Timer started.");
281 - (void)setBlingTime:(NSDate*)date
283 NSMutableDictionary *globalPrefs;
285 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
287 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
288 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
290 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
291 [globalPrefs removeObjectForKey:@"ITMTTrialVers"];
293 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
295 [globalPrefs release];
298 - (NSDate*)getBlingTime
301 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
306 NSDate *now = [NSDate date];
307 if (![self blingBling]) {
308 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
309 [self setBlingTime:now];
310 } else if ([[[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialVers"] intValue] < MT_CURRENT_VERSION) {
311 if ([now timeIntervalSinceDate:[self getBlingTime]] >= 345600) {
312 [self setBlingTime:[now addTimeInterval:-259200]];
314 NSMutableDictionary *globalPrefs;
316 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
317 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
318 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
320 [globalPrefs release];
324 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
326 [statusItem setEnabled:NO];
327 [[ITHotKeyCenter sharedCenter] setEnabled:NO];
328 if ([refreshTimer isValid]) {
329 [refreshTimer invalidate];
330 [refreshTimer release];
333 [statusWindowController showRegistrationQueryWindow];
337 [statusItem setEnabled:YES];
338 [[ITHotKeyCenter sharedCenter] setEnabled:YES];
339 if (_needsPolling && ![refreshTimer isValid]) {
340 [refreshTimer release];
341 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
343 selector:@selector(timerUpdate)
345 repeats:YES] retain];
349 [self setBlingTime:nil];
360 if ( ! ([bling checkDone] == 2475) ) {
367 - (BOOL)songIsPlaying
369 NSString *identifier = nil;
371 identifier = [[self currentRemote] playerStateUniqueIdentifier];
373 [self networkError:localException];
375 return ( ! ([identifier isEqualToString:@"0-0"]) );
378 - (BOOL)radioIsPlaying
380 ITMTRemotePlayerPlaylistClass class = nil;
382 class = [[self currentRemote] currentPlaylistClass];
384 [self networkError:localException];
386 return (class == ITMTRemotePlayerRadioPlaylist );
391 NSString *identifier = nil;
393 identifier = [[self currentRemote] playerStateUniqueIdentifier];
395 [self networkError:localException];
397 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
400 - (NSString *)latestSongIdentifier
402 return _latestSongIdentifier;
405 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
407 ITDebugLog(@"Setting latest song identifier:");
408 ITDebugLog(@" - Identifier: %@", newIdentifier);
409 [_latestSongIdentifier autorelease];
410 _latestSongIdentifier = [newIdentifier retain];
415 NSString *identifier = nil;
417 identifier = [[self currentRemote] playerStateUniqueIdentifier];
419 [self networkError:localException];
421 if (refreshTimer && identifier == nil) {
422 if ([statusItem isEnabled]) {
423 [statusItem setToolTip:@"iTunes not responding."];
425 [statusItem setEnabled:NO];
427 } else if (![statusItem isEnabled]) {
428 [statusItem setEnabled:YES];
429 [statusItem setToolTip:_toolTip];
433 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
434 ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
435 if ([df boolForKey:@"runScripts"]) {
436 NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
437 NSEnumerator *scriptsEnum = [scripts objectEnumerator];
438 NSString *nextScript;
439 ITDebugLog(@"Running AppleScripts for song change.");
440 while ( (nextScript = [scriptsEnum nextObject]) ) {
442 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
443 ITDebugLog(@"Running script: %@", nextScript);
444 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
445 ITDebugLog(@"Error running script %@.", nextScript);
447 [currentScript release];
452 [statusItem setEnabled:NO];
455 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
457 if ([menuController rebuildSubmenus]) {
458 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
459 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
461 [self setLatestSongIdentifier:identifier];
462 //Create the tooltip for the status item
463 if ( [df boolForKey:@"showToolTip"] ) {
464 NSString *artist = [[self currentRemote] currentSongArtist];
465 NSString *title = [[self currentRemote] currentSongTitle];
466 ITDebugLog(@"Creating status item tooltip.");
468 _toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
472 _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
474 [statusItem setToolTip:_toolTip];
476 [statusItem setToolTip:nil];
480 if ([df boolForKey:@"audioscrobblerEnabled"]) {
481 int length = [[self currentRemote] currentSongDuration];
483 _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240);
484 [_audioscrobblerTimer invalidate];
485 [_audioscrobblerTimer release];
486 _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:nil target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
487 [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode];
490 _audioscrobblerTimer = nil;
493 [self networkError:localException];
496 [statusItem setEnabled:YES];
499 if ([networkController isConnectedToServer]) {
500 [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
506 ITDebugLog(@"Menu clicked.");
508 if (([[self currentRemote] playerStateUniqueIdentifier] == nil) && playerRunningState == ITMTRemotePlayerRunning) {
510 if ([statusItem isEnabled]) {
511 [statusItem setToolTip:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.")];
513 [statusItem setEnabled:NO];
515 NSMenu *menu = [[NSMenu alloc] init];
516 [menu addItemWithTitle:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.") action:nil keyEquivalent:@""];
517 [statusItem setMenu:[menu autorelease]];
520 } else if (![statusItem isEnabled]) {
521 [statusItem setEnabled:YES];
522 [statusItem setToolTip:_toolTip];
526 if ([networkController isConnectedToServer]) {
527 //Used the cached version
532 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
533 [statusItem setMenu:[menuController menu]];
535 [statusItem setMenu:[menuController menuForNoPlayer]];
538 [self networkError:localException];
542 - (void)trackChanged:(NSNotification *)note
544 //If we're running the timer, shut it off since we don't need it!
545 /*if (refreshTimer && [refreshTimer isValid]) {
546 ITDebugLog(@"Invalidating refresh timer.");
547 [refreshTimer invalidate];
548 [refreshTimer release];
552 if (![self songChanged]) {
555 NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier];
556 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
557 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
559 [_lastTrackInfo release];
560 _lastTrackInfo = [[note userInfo] retain];
562 [self setLatestSongIdentifier:identifier];
563 ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
564 if ([df boolForKey:@"runScripts"]) {
565 NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
566 NSEnumerator *scriptsEnum = [scripts objectEnumerator];
567 NSString *nextScript;
568 ITDebugLog(@"Running AppleScripts for song change.");
569 while ( (nextScript = [scriptsEnum nextObject]) ) {
571 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
572 ITDebugLog(@"Running script: %@", nextScript);
573 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
574 ITDebugLog(@"Error running script %@.", nextScript);
576 [currentScript release];
580 [statusItem setEnabled:NO];
583 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
585 if ([menuController rebuildSubmenus]) {
586 /*if ( [df boolForKey:@"showSongInfoOnChange"] ) {
587 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
589 [self setLatestSongIdentifier:identifier];
590 //Create the tooltip for the status item
591 if ( [df boolForKey:@"showToolTip"] ) {
592 ITDebugLog(@"Creating status item tooltip.");
593 NSString *artist = [_lastTrackInfo objectForKey:@"Artist"], *title = [_lastTrackInfo objectForKey:@"Name"];
595 _toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
599 _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");;
601 [statusItem setToolTip:_toolTip];
603 [statusItem setToolTip:nil];
607 if ([df boolForKey:@"audioscrobblerEnabled"]) {
608 int length = [[self currentRemote] currentSongDuration];
610 _audioscrobblerInterval = ((length / 2 < 240) ? length / 2 : 240);
611 [_audioscrobblerTimer invalidate];
612 [_audioscrobblerTimer release];
613 _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:1.0 target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
614 [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode];
617 _audioscrobblerTimer = nil;
620 [self networkError:localException];
623 [statusItem setEnabled:YES];
625 if ([networkController isConnectedToServer]) {
626 [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
630 - (void)submitAudioscrobblerTrack:(NSTimer *)timer
632 ITDebugLog(@"Audioscrobbler: Attempting to submit current track");
634 if ([df boolForKey:@"audioscrobblerEnabled"]) {
636 int elapsed = [[self currentRemote] currentSongPlayed];
637 if ((abs(elapsed - _audioscrobblerInterval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) {
638 NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist];
639 if (title && artist) {
640 ITDebugLog(@"Audioscrobbler: Submitting current track");
641 [[AudioscrobblerController sharedController] submitTrack:title
643 album:[[self currentRemote] currentSongAlbum]
644 length:[[self currentRemote] currentSongDuration]];
646 } else if (_audioscrobblerInterval - elapsed > 0) {
647 ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", _audioscrobblerInterval - elapsed);
648 _audioscrobblerInterval -= elapsed;
649 [_audioscrobblerTimer release];
650 _audioscrobblerTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:_audioscrobblerInterval] interval:nil target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
651 [[NSRunLoop currentRunLoop] addTimer:_audioscrobblerTimer forMode:NSDefaultRunLoopMode];
654 [self networkError:localException];
668 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
669 ITDebugLog(@"Play/Pause toggled");
670 if (state == ITMTRemotePlayerPlaying) {
671 [[self currentRemote] pause];
672 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
673 [[self currentRemote] pause];
674 [[self currentRemote] play];
676 [[self currentRemote] play];
679 [self networkError:localException];
689 ITDebugLog(@"Going to next song.");
691 [[self currentRemote] goToNextSong];
693 [self networkError:localException];
702 ITDebugLog(@"Going to previous song.");
704 [[self currentRemote] goToPreviousSong];
706 [self networkError:localException];
715 ITDebugLog(@"Fast forwarding.");
717 [[self currentRemote] forward];
719 [self networkError:localException];
728 ITDebugLog(@"Rewinding.");
730 [[self currentRemote] rewind];
732 [self networkError:localException];
739 - (void)selectPlaylistAtIndex:(int)index
741 ITDebugLog(@"Selecting playlist %i", index);
743 [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
744 //[[self currentRemote] switchToPlaylistAtIndex:index];
746 [self networkError:localException];
753 - (void)selectSongAtIndex:(int)index
755 ITDebugLog(@"Selecting song %i", index);
757 [[self currentRemote] switchToSongAtIndex:index];
759 [self networkError:localException];
766 - (void)selectSongRating:(int)rating
768 ITDebugLog(@"Selecting song rating %i", rating);
770 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
772 [self networkError:localException];
779 - (void)selectEQPresetAtIndex:(int)index
781 ITDebugLog(@"Selecting EQ preset %i", index);
784 [[self currentRemote] setEqualizerEnabled:![[self currentRemote] equalizerEnabled]];
786 [[self currentRemote] switchToEQAtIndex:index];
789 [self networkError:localException];
796 - (void)makePlaylistWithTerm:(NSString *)term ofType:(int)type
798 ITDebugLog(@"Making playlist with term %@, type %i", term, type);
800 [[self currentRemote] makePlaylistWithTerm:term ofType:type];
802 [self networkError:localException];
804 ITDebugLog(@"Done making playlist");
809 ITDebugLog(@"Beginning show player.");
810 //if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
811 ITDebugLog(@"Showing player interface.");
813 [[self currentRemote] showPrimaryInterface];
815 [self networkError:localException];
818 ITDebugLog(@"Launching player.");
821 if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
823 pathITDebugLog(@"Showing player interface."); = [[self currentRemote] playerFullName];
825 if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
826 ITDebugLog(@"Error Launching Player");
829 [self networkError:localException];
832 ITDebugLog(@"Finished show player.");
835 - (void)showPreferences
837 ITDebugLog(@"Show preferences.");
838 [[PreferencesController sharedPrefs] showPrefsWindow:self];
841 - (void)showPreferencesAndClose
843 ITDebugLog(@"Show preferences.");
844 [[PreferencesController sharedPrefs] showPrefsWindow:self];
845 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
846 [[StatusWindow sharedWindow] vanish:self];
847 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
850 - (void)showTestWindow
852 [self showCurrentTrackInfo];
855 - (void)quitMenuTunes
857 ITDebugLog(@"Quitting MenuTunes.");
858 [NSApp terminate:self];
864 - (MenuController *)menuController
866 return menuController;
869 - (void)closePreferences
871 ITDebugLog(@"Preferences closed.");
872 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
877 - (ITMTRemote *)currentRemote
879 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
880 [self networkError:nil];
883 return currentRemote;
894 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
895 ITHotKey *nextHotKey;
896 ITDebugLog(@"Clearing hot keys.");
897 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
898 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
900 ITDebugLog(@"Done clearing hot keys.");
906 ITDebugLog(@"Setting up hot keys.");
908 if (playerRunningState == ITMTRemotePlayerNotRunning && ![[NetworkController sharedController] isConnectedToServer]) {
912 if ([df objectForKey:@"PlayPause"] != nil) {
913 ITDebugLog(@"Setting up play pause hot key.");
914 hotKey = [[ITHotKey alloc] init];
915 [hotKey setName:@"PlayPause"];
916 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
917 [hotKey setTarget:self];
918 [hotKey setAction:@selector(playPause)];
919 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
922 if ([df objectForKey:@"NextTrack"] != nil) {
923 ITDebugLog(@"Setting up next track hot key.");
924 hotKey = [[ITHotKey alloc] init];
925 [hotKey setName:@"NextTrack"];
926 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
927 [hotKey setTarget:self];
928 [hotKey setAction:@selector(nextSong)];
929 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
932 if ([df objectForKey:@"PrevTrack"] != nil) {
933 ITDebugLog(@"Setting up previous track hot key.");
934 hotKey = [[ITHotKey alloc] init];
935 [hotKey setName:@"PrevTrack"];
936 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
937 [hotKey setTarget:self];
938 [hotKey setAction:@selector(prevSong)];
939 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
942 if ([df objectForKey:@"FastForward"] != nil) {
943 ITDebugLog(@"Setting up fast forward hot key.");
944 hotKey = [[ITHotKey alloc] init];
945 [hotKey setName:@"FastForward"];
946 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"FastForward"]]];
947 [hotKey setTarget:self];
948 [hotKey setAction:@selector(fastForward)];
949 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
952 if ([df objectForKey:@"Rewind"] != nil) {
953 ITDebugLog(@"Setting up rewind hot key.");
954 hotKey = [[ITHotKey alloc] init];
955 [hotKey setName:@"Rewind"];
956 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"Rewind"]]];
957 [hotKey setTarget:self];
958 [hotKey setAction:@selector(rewind)];
959 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
962 if ([df objectForKey:@"ShowPlayer"] != nil) {
963 ITDebugLog(@"Setting up show player hot key.");
964 hotKey = [[ITHotKey alloc] init];
965 [hotKey setName:@"ShowPlayer"];
966 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
967 [hotKey setTarget:self];
968 [hotKey setAction:@selector(showPlayer)];
969 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
972 if ([df objectForKey:@"TrackInfo"] != nil) {
973 ITDebugLog(@"Setting up track info hot key.");
974 hotKey = [[ITHotKey alloc] init];
975 [hotKey setName:@"TrackInfo"];
976 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
977 [hotKey setTarget:self];
978 [hotKey setAction:@selector(showCurrentTrackInfoHotKey)];
979 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
982 if ([df objectForKey:@"UpcomingSongs"] != nil) {
983 ITDebugLog(@"Setting up upcoming songs hot key.");
984 hotKey = [[ITHotKey alloc] init];
985 [hotKey setName:@"UpcomingSongs"];
986 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
987 [hotKey setTarget:self];
988 [hotKey setAction:@selector(showUpcomingSongs)];
989 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
992 if ([df objectForKey:@"ToggleLoop"] != nil) {
993 ITDebugLog(@"Setting up toggle loop hot key.");
994 hotKey = [[ITHotKey alloc] init];
995 [hotKey setName:@"ToggleLoop"];
996 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
997 [hotKey setTarget:self];
998 [hotKey setAction:@selector(toggleLoop)];
999 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1002 if ([df objectForKey:@"ToggleShuffle"] != nil) {
1003 ITDebugLog(@"Setting up toggle shuffle hot key.");
1004 hotKey = [[ITHotKey alloc] init];
1005 [hotKey setName:@"ToggleShuffle"];
1006 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
1007 [hotKey setTarget:self];
1008 [hotKey setAction:@selector(toggleShuffle)];
1009 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1012 if ([df objectForKey:@"IncrementVolume"] != nil) {
1013 ITDebugLog(@"Setting up increment volume hot key.");
1014 hotKey = [[ITHotKey alloc] init];
1015 [hotKey setName:@"IncrementVolume"];
1016 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
1017 [hotKey setTarget:self];
1018 [hotKey setAction:@selector(incrementVolume)];
1019 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1022 if ([df objectForKey:@"DecrementVolume"] != nil) {
1023 ITDebugLog(@"Setting up decrement volume hot key.");
1024 hotKey = [[ITHotKey alloc] init];
1025 [hotKey setName:@"DecrementVolume"];
1026 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
1027 [hotKey setTarget:self];
1028 [hotKey setAction:@selector(decrementVolume)];
1029 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1032 if ([df objectForKey:@"IncrementRating"] != nil) {
1033 ITDebugLog(@"Setting up increment rating hot key.");
1034 hotKey = [[ITHotKey alloc] init];
1035 [hotKey setName:@"IncrementRating"];
1036 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
1037 [hotKey setTarget:self];
1038 [hotKey setAction:@selector(incrementRating)];
1039 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1042 if ([df objectForKey:@"DecrementRating"] != nil) {
1043 ITDebugLog(@"Setting up decrement rating hot key.");
1044 hotKey = [[ITHotKey alloc] init];
1045 [hotKey setName:@"DecrementRating"];
1046 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
1047 [hotKey setTarget:self];
1048 [hotKey setAction:@selector(decrementRating)];
1049 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1052 if ([df objectForKey:@"ToggleShufflability"] != nil) {
1053 ITDebugLog(@"Setting up toggle song shufflability hot key.");
1054 hotKey = [[ITHotKey alloc] init];
1055 [hotKey setName:@"ToggleShufflability"];
1056 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShufflability"]]];
1057 [hotKey setTarget:self];
1058 [hotKey setAction:@selector(toggleSongShufflable)];
1059 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1062 if ([df objectForKey:@"PopupMenu"] != nil) {
1063 ITDebugLog(@"Setting up popup menu hot key.");
1064 hotKey = [[ITHotKey alloc] init];
1065 [hotKey setName:@"PopupMenu"];
1066 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PopupMenu"]]];
1067 [hotKey setTarget:self];
1068 [hotKey setAction:@selector(popupMenu)];
1069 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1073 for (i = 0; i <= 5; i++) {
1074 NSString *curName = [NSString stringWithFormat:@"SetRating%i", i];
1075 if ([df objectForKey:curName] != nil) {
1076 ITDebugLog(@"Setting up set rating %i hot key.", i);
1077 hotKey = [[ITHotKey alloc] init];
1078 [hotKey setName:curName];
1079 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:curName]]];
1080 [hotKey setTarget:self];
1081 [hotKey setAction:@selector(setRating:)];
1082 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1085 ITDebugLog(@"Finished setting up hot keys.");
1088 - (void)showCurrentTrackInfoHotKey
1090 //If we're already visible and the setting says so, vanish instead of displaying again.
1091 if ([df boolForKey:@"ToggleTrackInfoWithHotKey"] && [statusWindowController currentStatusWindowType] == StatusWindowTrackInfoType && [[StatusWindow sharedWindow] visibilityState] == ITWindowVisibleState) {
1092 ITDebugLog(@"Track window is already visible, hiding track window.");
1093 [self invalidateStatusWindowUpdateTimer];
1094 [[StatusWindow sharedWindow] vanish:nil];
1097 [self showCurrentTrackInfo];
1100 - (void)showCurrentTrackInfo
1102 ITMTRemotePlayerSource source = 0;
1103 NSString *title = nil;
1104 NSString *album = nil;
1105 NSString *artist = nil;
1106 NSString *composer = nil;
1107 NSString *time = nil;
1108 NSString *track = nil;
1113 ITDebugLog(@"Showing track info status window.");
1116 source = [[self currentRemote] currentSource];
1117 title = [[self currentRemote] currentSongTitle];
1119 [self networkError:localException];
1123 if ( [df boolForKey:@"showAlbumArtwork"] ) {
1124 NSSize oldSize, newSize;
1126 art = [[self currentRemote] currentSongAlbumArt];
1127 oldSize = [art size];
1128 if (oldSize.width > oldSize.height) {
1129 newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
1131 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
1132 art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
1134 [self networkError:localException];
1138 if ( [df boolForKey:@"showAlbum"] ) {
1140 album = [[self currentRemote] currentSongAlbum];
1142 [self networkError:localException];
1146 if ( [df boolForKey:@"showArtist"] ) {
1148 artist = [[self currentRemote] currentSongArtist];
1150 [self networkError:localException];
1154 if ( [df boolForKey:@"showComposer"] ) {
1156 composer = [[self currentRemote] currentSongComposer];
1158 [self networkError:localException];
1162 if ( [df boolForKey:@"showTime"] ) {
1164 time = [NSString stringWithFormat:@"%@: %@ / %@",
1165 NSLocalizedString(@"time", @"Time"),
1166 [[self currentRemote] currentSongElapsed],
1167 [[self currentRemote] currentSongLength]];
1169 [self networkError:localException];
1171 _timeUpdateCount = 0;
1172 [self invalidateStatusWindowUpdateTimer];
1173 _statusWindowUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
1176 if ( [df boolForKey:@"showTrackNumber"] ) {
1181 trackNo = [[self currentRemote] currentSongTrack];
1182 trackCount = [[self currentRemote] currentAlbumTrackCount];
1184 [self networkError:localException];
1187 if ( (trackNo > 0) || (trackCount > 0) ) {
1188 track = [NSString stringWithFormat:@"%@: %i %@ %i",
1189 @"Track", trackNo, @"of", trackCount];
1193 if ( [df boolForKey:@"showTrackRating"] ) {
1194 float currentRating = 0;
1197 currentRating = [[self currentRemote] currentSongRating];
1199 [self networkError:localException];
1202 if (currentRating >= 0.0) {
1203 rating = ( currentRating * 5 );
1207 if ( [df boolForKey:@"showPlayCount"] && ![self radioIsPlaying] && [[self currentRemote] currentSource] == ITMTRemoteLibrarySource ) {
1209 playCount = [[self currentRemote] currentSongPlayCount];
1211 [self networkError:localException];
1215 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
1217 ITDebugLog(@"Showing current track info status window.");
1218 [statusWindowController showSongInfoWindowWithSource:source
1230 - (void)updateTime:(NSTimer *)timer
1232 StatusWindow *sw = (StatusWindow *)[StatusWindow sharedWindow];
1234 if ([sw visibilityState] != ITWindowHiddenState) {
1235 NSString *time = nil, *length;
1237 length = [[self currentRemote] currentSongLength];
1239 time = [NSString stringWithFormat:@"%@: %@ / %@",
1240 NSLocalizedString(@"time", @"Time"),
1241 [[self currentRemote] currentSongElapsed],
1243 [[StatusWindowController sharedController] updateTime:time];
1246 [self networkError:localException];
1249 [self invalidateStatusWindowUpdateTimer];
1253 - (void)invalidateStatusWindowUpdateTimer
1255 if (_statusWindowUpdateTimer) {
1256 [_statusWindowUpdateTimer invalidate];
1257 _statusWindowUpdateTimer = nil;
1261 - (void)showUpcomingSongs
1265 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
1267 [self networkError:localException];
1270 [self invalidateStatusWindowUpdateTimer];
1272 ITDebugLog(@"Showing upcoming songs status window.");
1275 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
1276 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
1277 int curTrack = [[self currentRemote] currentSongIndex];
1280 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance && i <= numSongs; i++) {
1281 if ([[self currentRemote] songEnabledAtIndex:i]) {
1282 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
1284 numSongsInAdvance++;
1288 if ([songList count] == 0) {
1289 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
1292 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
1294 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
1297 [self networkError:localException];
1306 NSMenu *menu = [statusItem menu];
1307 [(NSCarbonMenuImpl *)[menu _menuImpl] popUpMenu:menu atLocation:[NSEvent mouseLocation] width:1 forView:nil withSelectedItem:-30 withFont:[NSFont menuFontOfSize:32]];
1312 - (void)incrementVolume
1315 float volume = [[self currentRemote] volume];
1316 float dispVol = volume;
1317 ITDebugLog(@"Incrementing volume.");
1326 ITDebugLog(@"Setting volume to %f", volume);
1327 [[self currentRemote] setVolume:volume];
1329 // Show volume status window
1330 [self invalidateStatusWindowUpdateTimer];
1331 [statusWindowController showVolumeWindowWithLevel:dispVol];
1333 [self networkError:localException];
1337 - (void)decrementVolume
1340 float volume = [[self currentRemote] volume];
1341 float dispVol = volume;
1342 ITDebugLog(@"Decrementing volume.");
1351 ITDebugLog(@"Setting volume to %f", volume);
1352 [[self currentRemote] setVolume:volume];
1354 //Show volume status window
1355 [self invalidateStatusWindowUpdateTimer];
1356 [statusWindowController showVolumeWindowWithLevel:dispVol];
1358 [self networkError:localException];
1362 - (void)incrementRating
1365 float rating = [[self currentRemote] currentSongRating];
1366 ITDebugLog(@"Incrementing rating.");
1368 if ([[self currentRemote] currentPlaylistIndex] == 0) {
1369 ITDebugLog(@"No song playing, rating change aborted.");
1377 ITDebugLog(@"Setting rating to %f", rating);
1378 [[self currentRemote] setCurrentSongRating:rating];
1380 //Show rating status window
1381 [self invalidateStatusWindowUpdateTimer];
1382 [statusWindowController showRatingWindowWithRating:rating];
1384 [self networkError:localException];
1388 - (void)decrementRating
1391 float rating = [[self currentRemote] currentSongRating];
1392 ITDebugLog(@"Decrementing rating.");
1394 if ([[self currentRemote] currentPlaylistIndex] == 0) {
1395 ITDebugLog(@"No song playing, rating change aborted.");
1403 ITDebugLog(@"Setting rating to %f", rating);
1404 [[self currentRemote] setCurrentSongRating:rating];
1406 //Show rating status window
1407 [self invalidateStatusWindowUpdateTimer];
1408 [statusWindowController showRatingWindowWithRating:rating];
1410 [self networkError:localException];
1414 - (void)setRating:(ITHotKey *)sender
1416 if ([self songIsPlaying]) {
1417 int stars = [[sender name] characterAtIndex:9] - 48;
1418 [self selectSongRating:stars * 20];
1419 [statusWindowController showRatingWindowWithRating:(float)stars / 5.0];
1426 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
1427 ITDebugLog(@"Toggling repeat mode.");
1428 switch (repeatMode) {
1429 case ITMTRemotePlayerRepeatOff:
1430 repeatMode = ITMTRemotePlayerRepeatAll;
1432 case ITMTRemotePlayerRepeatAll:
1433 repeatMode = ITMTRemotePlayerRepeatOne;
1435 case ITMTRemotePlayerRepeatOne:
1436 repeatMode = ITMTRemotePlayerRepeatOff;
1439 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
1440 [[self currentRemote] setRepeatMode:repeatMode];
1442 //Show loop status window
1443 [self invalidateStatusWindowUpdateTimer];
1444 [statusWindowController showRepeatWindowWithMode:repeatMode];
1446 [self networkError:localException];
1450 - (void)toggleShuffle
1453 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
1454 ITDebugLog(@"Toggling shuffle mode.");
1455 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
1456 //Show shuffle status window
1457 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
1458 [self invalidateStatusWindowUpdateTimer];
1459 [statusWindowController showShuffleWindow:newShuffleEnabled];
1461 [self networkError:localException];
1465 - (void)toggleSongShufflable
1467 if ([self songIsPlaying]) {
1469 BOOL flag = ![[self currentRemote] currentSongShufflable];
1470 ITDebugLog(@"Toggling shufflability.");
1471 [[self currentRemote] setCurrentSongShufflable:flag];
1472 //Show song shufflability status window
1473 [self invalidateStatusWindowUpdateTimer];
1474 [statusWindowController showSongShufflabilityWindow:flag];
1476 [self networkError:localException];
1481 - (void)registerNowOK
1483 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1484 [[StatusWindow sharedWindow] vanish:self];
1485 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1490 - (void)registerNowCancel
1492 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1493 [[StatusWindow sharedWindow] vanish:self];
1494 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1496 [NSApp terminate:self];
1499 /*************************************************************************/
1501 #pragma mark NETWORK HANDLERS
1502 /*************************************************************************/
1504 - (void)setServerStatus:(BOOL)newStatus
1508 [networkController setServerStatus:YES];
1511 [networkController setServerStatus:NO];
1515 - (int)connectToServer
1518 ITDebugLog(@"Attempting to connect to shared remote.");
1519 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1522 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1523 currentRemote = [[[networkController networkObject] remote] retain];
1525 [self setupHotKeys];
1526 //playerRunningState = ITMTRemotePlayerRunning;
1527 playerRunningState = [[self currentRemote] playerRunningState];
1528 if (_needsPolling) {
1530 [refreshTimer invalidate];
1534 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1536 selector:@selector(timerUpdate)
1538 repeats:YES] retain];
1541 ITDebugLog(@"Connection successful.");
1543 } else if (result == 0) {
1544 ITDebugLog(@"Connection failed.");
1545 currentRemote = [remoteArray objectAtIndex:0];
1548 //Do something about the password being invalid
1549 ITDebugLog(@"Connection failed.");
1550 currentRemote = [remoteArray objectAtIndex:0];
1555 - (BOOL)disconnectFromServer
1557 ITDebugLog(@"Disconnecting from shared remote.");
1559 [currentRemote release];
1560 currentRemote = [remoteArray objectAtIndex:0];
1561 [networkController disconnect];
1563 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
1565 [self applicationLaunched:nil];
1567 [self applicationTerminated:nil];
1576 - (void)checkForRemoteServer
1578 [self checkForRemoteServerAndConnectImmediately:NO];
1581 - (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately
1583 ITDebugLog(@"Checking for remote server.");
1584 if (!_checkingForServer) {
1585 if (!_serverCheckLock) {
1586 _serverCheckLock = [[NSLock alloc] init];
1588 [_serverCheckLock lock];
1589 _checkingForServer = YES;
1590 [_serverCheckLock unlock];
1591 [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:[NSNumber numberWithBool:connectImmediately]];
1595 - (void)runRemoteServerCheck:(id)sender
1597 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1598 ITDebugLog(@"Remote server check running.");
1599 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1600 ITDebugLog(@"Remote server found.");
1601 if ([sender boolValue]) {
1602 [self performSelectorOnMainThread:@selector(connectToServer) withObject:nil waitUntilDone:NO];
1604 [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
1607 ITDebugLog(@"Remote server not found.");
1608 [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO];
1610 [_serverCheckLock lock];
1611 _checkingForServer = NO;
1612 [_serverCheckLock unlock];
1616 - (void)remoteServerFound:(id)sender
1618 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1619 [self invalidateStatusWindowUpdateTimer];
1620 [[StatusWindowController sharedController] showReconnectQueryWindow];
1624 - (void)remoteServerNotFound:(id)sender
1626 if (![[NetworkController sharedController] isConnectedToServer]) {
1627 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1631 - (void)networkError:(NSException *)exception
1633 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1634 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1635 //NSRunCriticalAlertPanel(@"Remote MenuTunes Disconnected", @"The MenuTunes server you were connected to stopped responding or quit. MenuTunes will revert back to the local player.", @"OK", nil, nil);
1636 [self invalidateStatusWindowUpdateTimer];
1637 [[StatusWindowController sharedController] showNetworkErrorQueryWindow];
1638 if ([self disconnectFromServer]) {
1639 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1640 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1642 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1649 /*if ([self connectToServer] == 0) {
1650 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1652 [self checkForRemoteServerAndConnectImmediately:YES];
1653 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1654 [[StatusWindow sharedWindow] vanish:self];
1655 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1658 - (void)cancelReconnect
1660 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1661 [[StatusWindow sharedWindow] vanish:self];
1662 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1665 /*************************************************************************/
1667 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1668 /*************************************************************************/
1670 - (void)applicationLaunched:(NSNotification *)note
1673 if (!note || ([[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer])) {
1674 ITDebugLog(@"Remote application launched.");
1675 playerRunningState = ITMTRemotePlayerRunning;
1676 [[self currentRemote] begin];
1677 [self setLatestSongIdentifier:@""];
1679 if (_needsPolling) {
1680 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1682 selector:@selector(timerUpdate)
1684 repeats:YES] retain];
1686 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1687 if (![df boolForKey:@"UsePollingOnly"]) {
1688 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackChanged:) name:@"ITMTTrackChanged" object:nil];
1690 [self setupHotKeys];
1693 [self networkError:localException];
1697 - (void)applicationTerminated:(NSNotification *)note
1700 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer]) {
1701 ITDebugLog(@"Remote application terminated.");
1702 playerRunningState = ITMTRemotePlayerNotRunning;
1703 [[self currentRemote] halt];
1704 [refreshTimer invalidate];
1705 [refreshTimer release];
1707 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ITMTTrackChanged" object:nil];
1708 [statusItem setEnabled:YES];
1709 [statusItem setToolTip:@"iTunes not running."];
1710 [self clearHotKeys];
1713 if ([df objectForKey:@"ShowPlayer"] != nil) {
1715 ITDebugLog(@"Setting up show player hot key.");
1716 hotKey = [[ITHotKey alloc] init];
1717 [hotKey setName:@"ShowPlayer"];
1718 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1719 [hotKey setTarget:self];
1720 [hotKey setAction:@selector(showPlayer)];
1721 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1725 [self networkError:localException];
1730 /*************************************************************************/
1732 #pragma mark NSApplication DELEGATE METHODS
1733 /*************************************************************************/
1735 - (void)applicationWillTerminate:(NSNotification *)note
1737 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
1738 [networkController stopRemoteServerSearch];
1739 [self clearHotKeys];
1740 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1743 - (void)applicationDidBecomeActive:(NSNotification *)note
1745 //This appears to not work in 10.4
1746 if (_open && !blinged && ![[ITAboutWindowController sharedController] isVisible] && ![NSApp mainWindow] && ([[StatusWindow sharedWindow] exitMode] == ITTransientStatusWindowExitAfterDelay)) {
1747 [[MainController sharedController] showPreferences];
1751 /*************************************************************************/
1753 #pragma mark DEALLOCATION METHOD
1754 /*************************************************************************/
1758 [self applicationTerminated:nil];
1760 [statusItem release];
1761 [statusWindowController release];
1762 [menuController release];
1763 [networkController release];
1764 [_serverCheckLock release];