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 "StatusItemHack.h"
14 @interface NSMenu (MenuImpl)
18 @interface NSCarbonMenuImpl:NSObject
24 + (void)setupForNoMenuBar;
28 - (void)itemChanged:fp8;
29 - (void)itemAdded:fp8;
30 - (void)itemRemoved:fp8;
31 - (void)performActionWithHighlightingForItemAtIndex:(int)fp8;
32 - (void)performMenuAction:(SEL)fp8 withTarget:fp12;
33 - (void)setupCarbonMenuBar;
34 - (void)setAsMainCarbonMenuBar;
35 - (void)clearAsMainCarbonMenuBar;
36 - (void)popUpMenu:fp8 atLocation:(NSPoint)fp12 width:(float)fp20 forView:fp24 withSelectedItem:(int)fp28 withFont:fp32;
37 - (void)_popUpContextMenu:fp8 withEvent:fp12 forView:fp16 withFont:fp20;
38 - (void)_popUpContextMenu:fp8 withEvent:fp12 forView:fp16;
42 @implementation NSImage (SmoothAdditions)
44 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
47 NSImageRep *rep = [self bestRepresentationForDevice:nil];
49 newImage = [[NSImage alloc] initWithSize:scaledSize];
52 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
53 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
54 [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
56 [newImage unlockFocus];
57 return [newImage autorelease];
62 @interface MainController(Private)
63 - (ITMTRemote *)loadRemote;
64 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
65 - (void)applicationLaunched:(NSNotification *)note;
66 - (void)applicationTerminated:(NSNotification *)note;
69 static MainController *sharedController;
71 @implementation MainController
73 + (MainController *)sharedController
75 return sharedController;
78 /*************************************************************************/
80 #pragma mark INITIALIZATION/DEALLOCATION METHODS
81 /*************************************************************************/
85 if ( ( self = [super init] ) ) {
86 sharedController = self;
88 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
89 [[PreferencesController sharedPrefs] setController:self];
90 statusWindowController = [StatusWindowController sharedController];
91 menuController = [[MenuController alloc] init];
92 df = [[NSUserDefaults standardUserDefaults] retain];
99 - (void)applicationDidFinishLaunching:(NSNotification *)note
101 NSString *iTunesPath = [df stringForKey:@"CustomPlayerPath"];
102 NSDictionary *iTunesInfoPlist;
105 //Turn on debug mode if needed
106 /*if ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0)
107 if ((GetCurrentKeyModifiers() & (optionKey | rightOptionKey)) != 0)
108 if ((GetCurrentKeyModifiers() & (shiftKey | rightShiftKey)) != 0)*/
109 if ([df boolForKey:@"ITDebugMode"] || ((GetCurrentKeyModifiers() & (controlKey | rightControlKey)) != 0)) {
111 [[StatusWindowController sharedController] showDebugModeEnabledWindow];
114 //Check if iTunes 4.7 or later is installed
116 iTunesPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"];
118 iTunesInfoPlist = [[NSBundle bundleWithPath:iTunesPath] infoDictionary];
119 iTunesVersion = [[iTunesInfoPlist objectForKey:@"CFBundleVersion"] floatValue];
120 ITDebugLog(@"iTunes version found: %f.", iTunesVersion);
121 if (iTunesVersion >= 4.7) {
127 if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) {
128 [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
130 [[PreferencesController sharedPrefs] registerDefaults];
131 [[StatusWindowController sharedController] showPreferencesUpdateWindow];
134 currentRemote = [self loadRemote];
135 [[self currentRemote] begin];
137 //Turn on network stuff if needed
138 networkController = [[NetworkController alloc] init];
139 if ([df boolForKey:@"enableSharing"]) {
140 [self setServerStatus:YES];
141 } else if ([df boolForKey:@"useSharedPlayer"]) {
142 [self checkForRemoteServerAndConnectImmediately:YES];
145 //Setup for notification of the remote player launching or quitting
146 [[[NSWorkspace sharedWorkspace] notificationCenter]
148 selector:@selector(applicationTerminated:)
149 name:NSWorkspaceDidTerminateApplicationNotification
152 [[[NSWorkspace sharedWorkspace] notificationCenter]
154 selector:@selector(applicationLaunched:)
155 name:NSWorkspaceDidLaunchApplicationNotification
158 if (![df objectForKey:@"menu"]) { // If this is nil, defaults have never been registered.
159 [[PreferencesController sharedPrefs] registerDefaults];
162 if ([df boolForKey:@"ITMTNoStatusItem"]) {
165 [StatusItemHack install];
166 statusItem = [[ITStatusItem alloc]
167 initWithStatusBar:[NSStatusBar systemStatusBar]
168 withLength:NSSquareStatusItemLength];
171 /*bling = [[MTBlingController alloc] init];
173 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
175 selector:@selector(blingTime)
177 repeats:YES] retain];*/
180 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
181 [self applicationLaunched:nil];
183 if ([df boolForKey:@"LaunchPlayerWithMT"])
186 [self applicationTerminated:nil];
189 [self networkError:localException];
192 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
193 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
195 [networkController startRemoteServerSearch];
197 [self performSelector:@selector(rawr) withObject:nil afterDelay:1.0];
199 bling = [[MTBlingController alloc] init];
201 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
203 selector:@selector(blingTime)
205 repeats:YES] retain];
213 - (ITMTRemote *)loadRemote
215 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
216 ITDebugLog(@"Gathering remotes.");
218 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
219 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
220 NSString *bundlePath;
222 while ( (bundlePath = [enumerator nextObject]) ) {
223 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
226 Class remoteClass = [remoteBundle principalClass];
228 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
229 [(NSObject *)remoteClass isKindOfClass:[NSObject class]]) {
230 id remote = [remoteClass remote];
231 ITDebugLog(@"Adding remote at path %@", bundlePath);
232 [remoteArray addObject:remote];
237 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
238 // if ( [remoteArray count] > 1 ) {
239 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
241 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
244 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
245 return [remoteArray objectAtIndex:0];
248 /*************************************************************************/
250 #pragma mark INSTANCE METHODS
251 /*************************************************************************/
253 /*- (void)startTimerInNewThread
255 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
256 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
257 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
259 selector:@selector(timerUpdate)
261 repeats:YES] retain];
263 ITDebugLog(@"Timer started.");
267 - (void)setBlingTime:(NSDate*)date
269 NSMutableDictionary *globalPrefs;
271 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
273 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
274 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
276 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
277 [globalPrefs removeObjectForKey:@"ITMTTrialVers"];
279 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
281 [globalPrefs release];
284 - (NSDate*)getBlingTime
287 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
292 NSDate *now = [NSDate date];
293 if (![self blingBling]) {
294 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
295 [self setBlingTime:now];
296 } else if ([[[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialVers"] intValue] < MT_CURRENT_VERSION) {
297 if ([now timeIntervalSinceDate:[self getBlingTime]] >= 345600) {
298 [self setBlingTime:[now addTimeInterval:-259200]];
300 NSMutableDictionary *globalPrefs;
302 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
303 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
304 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
306 [globalPrefs release];
310 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
312 [statusItem setEnabled:NO];
313 [[ITHotKeyCenter sharedCenter] setEnabled:NO];
314 if ([refreshTimer isValid]) {
315 [refreshTimer invalidate];
316 [refreshTimer release];
319 [statusWindowController showRegistrationQueryWindow];
323 [statusItem setEnabled:YES];
324 [[ITHotKeyCenter sharedCenter] setEnabled:YES];
325 if (_needsPolling && ![refreshTimer isValid]) {
326 [refreshTimer release];
327 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
329 selector:@selector(timerUpdate)
331 repeats:YES] retain];
335 [self setBlingTime:nil];
346 if ( ! ([bling checkDone] == 2475) ) {
353 - (BOOL)songIsPlaying
355 NSString *identifier = nil;
357 identifier = [[self currentRemote] playerStateUniqueIdentifier];
359 [self networkError:localException];
361 return ( ! ([identifier isEqualToString:@"0-0"]) );
364 - (BOOL)radioIsPlaying
366 ITMTRemotePlayerPlaylistClass class = nil;
368 class = [[self currentRemote] currentPlaylistClass];
370 [self networkError:localException];
372 return (class == ITMTRemotePlayerRadioPlaylist );
377 NSString *identifier = nil;
379 identifier = [[self currentRemote] playerStateUniqueIdentifier];
381 [self networkError:localException];
383 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
386 - (NSString *)latestSongIdentifier
388 return _latestSongIdentifier;
391 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
393 ITDebugLog(@"Setting latest song identifier:");
394 ITDebugLog(@" - Identifier: %@", newIdentifier);
395 [_latestSongIdentifier autorelease];
396 _latestSongIdentifier = [newIdentifier retain];
401 NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier];
402 if (refreshTimer && identifier == nil) {
403 if ([statusItem isEnabled]) {
404 [statusItem setToolTip:@"iTunes not responding."];
406 [statusItem setEnabled:NO];
408 } else if (![statusItem isEnabled]) {
409 [statusItem setEnabled:YES];
410 [statusItem setToolTip:_toolTip];
414 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
415 ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
416 if ([df boolForKey:@"runScripts"]) {
417 NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
418 NSEnumerator *scriptsEnum = [scripts objectEnumerator];
419 NSString *nextScript;
420 ITDebugLog(@"Running AppleScripts for song change.");
421 while ( (nextScript = [scriptsEnum nextObject]) ) {
423 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
424 ITDebugLog(@"Running script: %@", nextScript);
425 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
426 ITDebugLog(@"Error running script %@.", nextScript);
428 [currentScript release];
433 [statusItem setEnabled:NO];
436 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
438 if ([menuController rebuildSubmenus]) {
439 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
440 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
442 [self setLatestSongIdentifier:identifier];
443 //Create the tooltip for the status item
444 if ( [df boolForKey:@"showToolTip"] ) {
445 NSString *artist = [[self currentRemote] currentSongArtist];
446 NSString *title = [[self currentRemote] currentSongTitle];
447 ITDebugLog(@"Creating status item tooltip.");
449 _toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
453 _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
455 [statusItem setToolTip:_toolTip];
457 [statusItem setToolTip:nil];
461 [self networkError:localException];
464 [statusItem setEnabled:YES];
467 if ([networkController isConnectedToServer]) {
468 [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
474 ITDebugLog(@"Menu clicked.");
476 if (([[self currentRemote] playerStateUniqueIdentifier] == nil) && playerRunningState == ITMTRemotePlayerRunning) {
478 if ([statusItem isEnabled]) {
479 [statusItem setToolTip:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.")];
481 [statusItem setEnabled:NO];
483 NSMenu *menu = [[NSMenu alloc] init];
484 [menu addItemWithTitle:NSLocalizedString(@"iTunesNotResponding", @"iTunes is not responding.") action:nil keyEquivalent:@""];
485 [statusItem setMenu:[menu autorelease]];
488 } else if (![statusItem isEnabled]) {
489 [statusItem setEnabled:YES];
490 [statusItem setToolTip:_toolTip];
494 if ([networkController isConnectedToServer]) {
495 //Used the cached version
500 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
501 [statusItem setMenu:[menuController menu]];
503 [statusItem setMenu:[menuController menuForNoPlayer]];
506 [self networkError:localException];
510 - (void)trackChanged:(NSNotification *)note
512 //If we're running the timer, shut it off since we don't need it!
513 /*if (refreshTimer && [refreshTimer isValid]) {
514 ITDebugLog(@"Invalidating refresh timer.");
515 [refreshTimer invalidate];
516 [refreshTimer release];
520 if (![self songChanged]) {
523 NSString *identifier = [[self currentRemote] playerStateUniqueIdentifier];
524 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
525 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
527 [_lastTrackInfo release];
528 _lastTrackInfo = [[note userInfo] retain];
530 [self setLatestSongIdentifier:identifier];
531 ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
532 if ([df boolForKey:@"runScripts"]) {
533 NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
534 NSEnumerator *scriptsEnum = [scripts objectEnumerator];
535 NSString *nextScript;
536 ITDebugLog(@"Running AppleScripts for song change.");
537 while ( (nextScript = [scriptsEnum nextObject]) ) {
539 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
540 ITDebugLog(@"Running script: %@", nextScript);
541 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
542 ITDebugLog(@"Error running script %@.", nextScript);
544 [currentScript release];
548 [statusItem setEnabled:NO];
551 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
553 if ([menuController rebuildSubmenus]) {
554 /*if ( [df boolForKey:@"showSongInfoOnChange"] ) {
555 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
557 [self setLatestSongIdentifier:identifier];
558 //Create the tooltip for the status item
559 if ( [df boolForKey:@"showToolTip"] ) {
560 ITDebugLog(@"Creating status item tooltip.");
561 NSString *artist = [_lastTrackInfo objectForKey:@"Artist"], *title = [_lastTrackInfo objectForKey:@"Name"];
563 _toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
567 _toolTip = NSLocalizedString(@"noSongPlaying", @"No song is playing.");;
569 [statusItem setToolTip:_toolTip];
571 [statusItem setToolTip:nil];
575 [self networkError:localException];
578 [statusItem setEnabled:YES];
580 if ([networkController isConnectedToServer]) {
581 [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
594 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
595 ITDebugLog(@"Play/Pause toggled");
596 if (state == ITMTRemotePlayerPlaying) {
597 [[self currentRemote] pause];
598 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
599 [[self currentRemote] pause];
600 [[self currentRemote] play];
602 [[self currentRemote] play];
605 [self networkError:localException];
615 ITDebugLog(@"Going to next song.");
617 [[self currentRemote] goToNextSong];
619 [self networkError:localException];
628 ITDebugLog(@"Going to previous song.");
630 [[self currentRemote] goToPreviousSong];
632 [self networkError:localException];
641 ITDebugLog(@"Fast forwarding.");
643 [[self currentRemote] forward];
645 [self networkError:localException];
654 ITDebugLog(@"Rewinding.");
656 [[self currentRemote] rewind];
658 [self networkError:localException];
665 - (void)selectPlaylistAtIndex:(int)index
667 ITDebugLog(@"Selecting playlist %i", index);
669 [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
670 //[[self currentRemote] switchToPlaylistAtIndex:index];
672 [self networkError:localException];
679 - (void)selectSongAtIndex:(int)index
681 ITDebugLog(@"Selecting song %i", index);
683 [[self currentRemote] switchToSongAtIndex:index];
685 [self networkError:localException];
692 - (void)selectSongRating:(int)rating
694 ITDebugLog(@"Selecting song rating %i", rating);
696 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
698 [self networkError:localException];
705 - (void)selectEQPresetAtIndex:(int)index
707 ITDebugLog(@"Selecting EQ preset %i", index);
710 [[self currentRemote] setEqualizerEnabled:![[self currentRemote] equalizerEnabled]];
712 [[self currentRemote] switchToEQAtIndex:index];
715 [self networkError:localException];
722 - (void)makePlaylistWithTerm:(NSString *)term ofType:(int)type
724 ITDebugLog(@"Making playlist with term %@, type %i", term, type);
726 [[self currentRemote] makePlaylistWithTerm:term ofType:type];
728 [self networkError:localException];
730 ITDebugLog(@"Done making playlist");
735 ITDebugLog(@"Beginning show player.");
736 //if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
737 ITDebugLog(@"Showing player interface.");
739 [[self currentRemote] showPrimaryInterface];
741 [self networkError:localException];
744 ITDebugLog(@"Launching player.");
747 if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
749 pathITDebugLog(@"Showing player interface."); = [[self currentRemote] playerFullName];
751 if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
752 ITDebugLog(@"Error Launching Player");
755 [self networkError:localException];
758 ITDebugLog(@"Finished show player.");
761 - (void)showPreferences
763 ITDebugLog(@"Show preferences.");
764 [[PreferencesController sharedPrefs] showPrefsWindow:self];
767 - (void)showPreferencesAndClose
769 ITDebugLog(@"Show preferences.");
770 [[PreferencesController sharedPrefs] showPrefsWindow:self];
771 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
772 [[StatusWindow sharedWindow] vanish:self];
773 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
776 - (void)showTestWindow
778 [self showCurrentTrackInfo];
781 - (void)quitMenuTunes
783 ITDebugLog(@"Quitting MenuTunes.");
784 [NSApp terminate:self];
790 - (MenuController *)menuController
792 return menuController;
795 - (void)closePreferences
797 ITDebugLog(@"Preferences closed.");
798 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
803 - (ITMTRemote *)currentRemote
805 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
806 [self networkError:nil];
809 return currentRemote;
820 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
821 ITHotKey *nextHotKey;
822 ITDebugLog(@"Clearing hot keys.");
823 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
824 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
826 ITDebugLog(@"Done clearing hot keys.");
832 ITDebugLog(@"Setting up hot keys.");
834 if (playerRunningState == ITMTRemotePlayerNotRunning && ![[NetworkController sharedController] isConnectedToServer]) {
838 if ([df objectForKey:@"PlayPause"] != nil) {
839 ITDebugLog(@"Setting up play pause hot key.");
840 hotKey = [[ITHotKey alloc] init];
841 [hotKey setName:@"PlayPause"];
842 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
843 [hotKey setTarget:self];
844 [hotKey setAction:@selector(playPause)];
845 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
848 if ([df objectForKey:@"NextTrack"] != nil) {
849 ITDebugLog(@"Setting up next track hot key.");
850 hotKey = [[ITHotKey alloc] init];
851 [hotKey setName:@"NextTrack"];
852 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
853 [hotKey setTarget:self];
854 [hotKey setAction:@selector(nextSong)];
855 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
858 if ([df objectForKey:@"PrevTrack"] != nil) {
859 ITDebugLog(@"Setting up previous track hot key.");
860 hotKey = [[ITHotKey alloc] init];
861 [hotKey setName:@"PrevTrack"];
862 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
863 [hotKey setTarget:self];
864 [hotKey setAction:@selector(prevSong)];
865 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
868 if ([df objectForKey:@"FastForward"] != nil) {
869 ITDebugLog(@"Setting up fast forward hot key.");
870 hotKey = [[ITHotKey alloc] init];
871 [hotKey setName:@"FastForward"];
872 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"FastForward"]]];
873 [hotKey setTarget:self];
874 [hotKey setAction:@selector(fastForward)];
875 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
878 if ([df objectForKey:@"Rewind"] != nil) {
879 ITDebugLog(@"Setting up rewind hot key.");
880 hotKey = [[ITHotKey alloc] init];
881 [hotKey setName:@"Rewind"];
882 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"Rewind"]]];
883 [hotKey setTarget:self];
884 [hotKey setAction:@selector(rewind)];
885 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
888 if ([df objectForKey:@"ShowPlayer"] != nil) {
889 ITDebugLog(@"Setting up show player hot key.");
890 hotKey = [[ITHotKey alloc] init];
891 [hotKey setName:@"ShowPlayer"];
892 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
893 [hotKey setTarget:self];
894 [hotKey setAction:@selector(showPlayer)];
895 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
898 if ([df objectForKey:@"TrackInfo"] != nil) {
899 ITDebugLog(@"Setting up track info hot key.");
900 hotKey = [[ITHotKey alloc] init];
901 [hotKey setName:@"TrackInfo"];
902 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
903 [hotKey setTarget:self];
904 [hotKey setAction:@selector(showCurrentTrackInfo)];
905 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
908 if ([df objectForKey:@"UpcomingSongs"] != nil) {
909 ITDebugLog(@"Setting up upcoming songs hot key.");
910 hotKey = [[ITHotKey alloc] init];
911 [hotKey setName:@"UpcomingSongs"];
912 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
913 [hotKey setTarget:self];
914 [hotKey setAction:@selector(showUpcomingSongs)];
915 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
918 if ([df objectForKey:@"ToggleLoop"] != nil) {
919 ITDebugLog(@"Setting up toggle loop hot key.");
920 hotKey = [[ITHotKey alloc] init];
921 [hotKey setName:@"ToggleLoop"];
922 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
923 [hotKey setTarget:self];
924 [hotKey setAction:@selector(toggleLoop)];
925 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
928 if ([df objectForKey:@"ToggleShuffle"] != nil) {
929 ITDebugLog(@"Setting up toggle shuffle hot key.");
930 hotKey = [[ITHotKey alloc] init];
931 [hotKey setName:@"ToggleShuffle"];
932 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
933 [hotKey setTarget:self];
934 [hotKey setAction:@selector(toggleShuffle)];
935 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
938 if ([df objectForKey:@"IncrementVolume"] != nil) {
939 ITDebugLog(@"Setting up increment volume hot key.");
940 hotKey = [[ITHotKey alloc] init];
941 [hotKey setName:@"IncrementVolume"];
942 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
943 [hotKey setTarget:self];
944 [hotKey setAction:@selector(incrementVolume)];
945 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
948 if ([df objectForKey:@"DecrementVolume"] != nil) {
949 ITDebugLog(@"Setting up decrement volume hot key.");
950 hotKey = [[ITHotKey alloc] init];
951 [hotKey setName:@"DecrementVolume"];
952 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
953 [hotKey setTarget:self];
954 [hotKey setAction:@selector(decrementVolume)];
955 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
958 if ([df objectForKey:@"IncrementRating"] != nil) {
959 ITDebugLog(@"Setting up increment rating hot key.");
960 hotKey = [[ITHotKey alloc] init];
961 [hotKey setName:@"IncrementRating"];
962 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
963 [hotKey setTarget:self];
964 [hotKey setAction:@selector(incrementRating)];
965 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
968 if ([df objectForKey:@"DecrementRating"] != nil) {
969 ITDebugLog(@"Setting up decrement rating hot key.");
970 hotKey = [[ITHotKey alloc] init];
971 [hotKey setName:@"DecrementRating"];
972 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
973 [hotKey setTarget:self];
974 [hotKey setAction:@selector(decrementRating)];
975 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
978 if ([df objectForKey:@"ToggleShufflability"] != nil) {
979 ITDebugLog(@"Setting up toggle song shufflability hot key.");
980 hotKey = [[ITHotKey alloc] init];
981 [hotKey setName:@"ToggleShufflability"];
982 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShufflability"]]];
983 [hotKey setTarget:self];
984 [hotKey setAction:@selector(toggleSongShufflable)];
985 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
988 if ([df objectForKey:@"PopupMenu"] != nil) {
989 ITDebugLog(@"Setting up popup menu hot key.");
990 hotKey = [[ITHotKey alloc] init];
991 [hotKey setName:@"PopupMenu"];
992 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PopupMenu"]]];
993 [hotKey setTarget:self];
994 [hotKey setAction:@selector(popupMenu)];
995 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
999 for (i = 0; i <= 5; i++) {
1000 NSString *curName = [NSString stringWithFormat:@"SetRating%i", i];
1001 if ([df objectForKey:curName] != nil) {
1002 ITDebugLog(@"Setting up set rating %i hot key.", i);
1003 hotKey = [[ITHotKey alloc] init];
1004 [hotKey setName:curName];
1005 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:curName]]];
1006 [hotKey setTarget:self];
1007 [hotKey setAction:@selector(setRating:)];
1008 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1011 ITDebugLog(@"Finished setting up hot keys.");
1014 - (void)showCurrentTrackInfo
1016 ITMTRemotePlayerSource source = 0;
1017 NSString *title = nil;
1018 NSString *album = nil;
1019 NSString *artist = nil;
1020 NSString *composer = nil;
1021 NSString *time = nil;
1022 NSString *track = nil;
1027 ITDebugLog(@"Showing track info status window.");
1030 source = [[self currentRemote] currentSource];
1031 title = [[self currentRemote] currentSongTitle];
1033 [self networkError:localException];
1037 if ( [df boolForKey:@"showAlbumArtwork"] ) {
1038 NSSize oldSize, newSize;
1040 art = [[self currentRemote] currentSongAlbumArt];
1041 oldSize = [art size];
1042 if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
1043 else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
1044 art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
1046 [self networkError:localException];
1050 if ( [df boolForKey:@"showAlbum"] ) {
1052 album = [[self currentRemote] currentSongAlbum];
1054 [self networkError:localException];
1058 if ( [df boolForKey:@"showArtist"] ) {
1060 artist = [[self currentRemote] currentSongArtist];
1062 [self networkError:localException];
1066 if ( [df boolForKey:@"showComposer"] ) {
1068 composer = [[self currentRemote] currentSongComposer];
1070 [self networkError:localException];
1074 if ( [df boolForKey:@"showTime"] ) {
1076 time = [NSString stringWithFormat:@"%@: %@ / %@",
1077 NSLocalizedString(@"time", @"Time"),
1078 [[self currentRemote] currentSongElapsed],
1079 [[self currentRemote] currentSongLength]];
1081 [self networkError:localException];
1083 _timeUpdateCount = 0;
1084 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
1087 if ( [df boolForKey:@"showTrackNumber"] ) {
1092 trackNo = [[self currentRemote] currentSongTrack];
1093 trackCount = [[self currentRemote] currentAlbumTrackCount];
1095 [self networkError:localException];
1098 if ( (trackNo > 0) || (trackCount > 0) ) {
1099 track = [NSString stringWithFormat:@"%@: %i %@ %i",
1100 @"Track", trackNo, @"of", trackCount];
1104 if ( [df boolForKey:@"showTrackRating"] ) {
1105 float currentRating = 0;
1108 currentRating = [[self currentRemote] currentSongRating];
1110 [self networkError:localException];
1113 if (currentRating >= 0.0) {
1114 rating = ( currentRating * 5 );
1118 if ( [df boolForKey:@"showPlayCount"] && ![self radioIsPlaying] && [[self currentRemote] currentSource] == ITMTRemoteLibrarySource ) {
1120 playCount = [[self currentRemote] currentSongPlayCount];
1122 [self networkError:localException];
1126 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
1128 ITDebugLog(@"Showing current track info status window.");
1129 [statusWindowController showSongInfoWindowWithSource:source
1141 - (void)updateTime:(NSTimer *)timer
1144 if (_timeUpdateCount > (int)[df floatForKey:@"statusWindowVanishDelay"] - 1) {
1145 NSString *time = nil;
1147 time = [NSString stringWithFormat:@"%@: %@ / %@",
1148 NSLocalizedString(@"time", @"Time"),
1149 [[self currentRemote] currentSongElapsed],
1150 [[self currentRemote] currentSongLength]];
1151 [[StatusWindowController sharedController] updateTime:time];
1153 [self networkError:localException];
1158 - (void)showUpcomingSongs
1162 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
1164 [self networkError:localException];
1167 ITDebugLog(@"Showing upcoming songs status window.");
1170 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
1171 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
1172 int curTrack = [[self currentRemote] currentSongIndex];
1175 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
1176 if (i <= numSongs) {
1177 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
1181 if ([songList count] == 0) {
1182 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
1185 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
1187 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
1190 [self networkError:localException];
1199 NSMenu *menu = [statusItem menu];
1200 [(NSCarbonMenuImpl *)[menu _menuImpl] popUpMenu:menu atLocation:[NSEvent mouseLocation] width:1 forView:nil withSelectedItem:-30 withFont:[NSFont menuFontOfSize:32]];
1205 - (void)incrementVolume
1208 float volume = [[self currentRemote] volume];
1209 float dispVol = volume;
1210 ITDebugLog(@"Incrementing volume.");
1219 ITDebugLog(@"Setting volume to %f", volume);
1220 [[self currentRemote] setVolume:volume];
1222 // Show volume status window
1223 [statusWindowController showVolumeWindowWithLevel:dispVol];
1225 [self networkError:localException];
1229 - (void)decrementVolume
1232 float volume = [[self currentRemote] volume];
1233 float dispVol = volume;
1234 ITDebugLog(@"Decrementing volume.");
1243 ITDebugLog(@"Setting volume to %f", volume);
1244 [[self currentRemote] setVolume:volume];
1246 //Show volume status window
1247 [statusWindowController showVolumeWindowWithLevel:dispVol];
1249 [self networkError:localException];
1253 - (void)incrementRating
1256 float rating = [[self currentRemote] currentSongRating];
1257 ITDebugLog(@"Incrementing rating.");
1259 if ([[self currentRemote] currentPlaylistIndex] == 0) {
1260 ITDebugLog(@"No song playing, rating change aborted.");
1268 ITDebugLog(@"Setting rating to %f", rating);
1269 [[self currentRemote] setCurrentSongRating:rating];
1271 //Show rating status window
1272 [statusWindowController showRatingWindowWithRating:rating];
1274 [self networkError:localException];
1278 - (void)decrementRating
1281 float rating = [[self currentRemote] currentSongRating];
1282 ITDebugLog(@"Decrementing rating.");
1284 if ([[self currentRemote] currentPlaylistIndex] == 0) {
1285 ITDebugLog(@"No song playing, rating change aborted.");
1293 ITDebugLog(@"Setting rating to %f", rating);
1294 [[self currentRemote] setCurrentSongRating:rating];
1296 //Show rating status window
1297 [statusWindowController showRatingWindowWithRating:rating];
1299 [self networkError:localException];
1303 - (void)setRating:(ITHotKey *)sender
1305 int stars = [[sender name] characterAtIndex:9] - 48;
1306 [self selectSongRating:stars * 20];
1307 [statusWindowController showRatingWindowWithRating:(float)stars / 5.0];
1313 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
1314 ITDebugLog(@"Toggling repeat mode.");
1315 switch (repeatMode) {
1316 case ITMTRemotePlayerRepeatOff:
1317 repeatMode = ITMTRemotePlayerRepeatAll;
1319 case ITMTRemotePlayerRepeatAll:
1320 repeatMode = ITMTRemotePlayerRepeatOne;
1322 case ITMTRemotePlayerRepeatOne:
1323 repeatMode = ITMTRemotePlayerRepeatOff;
1326 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
1327 [[self currentRemote] setRepeatMode:repeatMode];
1329 //Show loop status window
1330 [statusWindowController showRepeatWindowWithMode:repeatMode];
1332 [self networkError:localException];
1336 - (void)toggleShuffle
1339 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
1340 ITDebugLog(@"Toggling shuffle mode.");
1341 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
1342 //Show shuffle status window
1343 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
1344 [statusWindowController showShuffleWindow:newShuffleEnabled];
1346 [self networkError:localException];
1350 - (void)toggleSongShufflable
1353 BOOL flag = ![[self currentRemote] currentSongShufflable];
1354 ITDebugLog(@"Toggling shufflability.");
1355 [[self currentRemote] setCurrentSongShufflable:flag];
1356 //Show song shufflability status window
1357 //[statusWindowController showSongShuffabilityWindow:flag];
1359 [self networkError:localException];
1363 - (void)registerNowOK
1365 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1366 [[StatusWindow sharedWindow] vanish:self];
1367 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1372 - (void)registerNowCancel
1374 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1375 [[StatusWindow sharedWindow] vanish:self];
1376 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1378 [NSApp terminate:self];
1381 /*************************************************************************/
1383 #pragma mark NETWORK HANDLERS
1384 /*************************************************************************/
1386 - (void)setServerStatus:(BOOL)newStatus
1390 [networkController setServerStatus:YES];
1393 [networkController setServerStatus:NO];
1397 - (int)connectToServer
1400 ITDebugLog(@"Attempting to connect to shared remote.");
1401 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1404 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1405 currentRemote = [[[networkController networkObject] remote] retain];
1407 [self setupHotKeys];
1408 //playerRunningState = ITMTRemotePlayerRunning;
1409 playerRunningState = [[self currentRemote] playerRunningState];
1410 if (_needsPolling) {
1412 [refreshTimer invalidate];
1414 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1416 selector:@selector(timerUpdate)
1418 repeats:YES] retain];
1421 ITDebugLog(@"Connection successful.");
1423 } else if (result == 0) {
1424 ITDebugLog(@"Connection failed.");
1425 currentRemote = [remoteArray objectAtIndex:0];
1428 //Do something about the password being invalid
1429 ITDebugLog(@"Connection failed.");
1430 currentRemote = [remoteArray objectAtIndex:0];
1435 - (BOOL)disconnectFromServer
1437 ITDebugLog(@"Disconnecting from shared remote.");
1439 [currentRemote release];
1440 currentRemote = [remoteArray objectAtIndex:0];
1441 [networkController disconnect];
1443 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
1444 [self applicationLaunched:nil];
1446 [self applicationTerminated:nil];
1454 - (void)checkForRemoteServer
1456 [self checkForRemoteServerAndConnectImmediately:NO];
1459 - (void)checkForRemoteServerAndConnectImmediately:(BOOL)connectImmediately
1461 ITDebugLog(@"Checking for remote server.");
1462 if (!_checkingForServer) {
1463 if (!_serverCheckLock) {
1464 _serverCheckLock = [[NSLock alloc] init];
1466 [_serverCheckLock lock];
1467 _checkingForServer = YES;
1468 [_serverCheckLock unlock];
1469 [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:[NSNumber numberWithBool:connectImmediately]];
1473 - (void)runRemoteServerCheck:(id)sender
1475 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1476 ITDebugLog(@"Remote server check running.");
1477 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1478 ITDebugLog(@"Remote server found.");
1479 if ([sender boolValue]) {
1480 [self performSelectorOnMainThread:@selector(connectToServer) withObject:nil waitUntilDone:NO];
1482 [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
1485 ITDebugLog(@"Remote server not found.");
1486 [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO];
1488 [_serverCheckLock lock];
1489 _checkingForServer = NO;
1490 [_serverCheckLock unlock];
1494 - (void)remoteServerFound:(id)sender
1496 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1497 [[StatusWindowController sharedController] showReconnectQueryWindow];
1501 - (void)remoteServerNotFound:(id)sender
1503 if (![[NetworkController sharedController] isConnectedToServer]) {
1504 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1508 - (void)networkError:(NSException *)exception
1510 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1511 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1512 //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);
1513 [[StatusWindowController sharedController] showNetworkErrorQueryWindow];
1514 if ([self disconnectFromServer]) {
1515 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1516 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1518 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1525 /*if ([self connectToServer] == 0) {
1526 [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1528 [self checkForRemoteServerAndConnectImmediately:YES];
1529 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1530 [[StatusWindow sharedWindow] vanish:self];
1531 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1534 - (void)cancelReconnect
1536 [(StatusWindow *)[StatusWindow sharedWindow] setLocked:NO];
1537 [[StatusWindow sharedWindow] vanish:self];
1538 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1541 /*************************************************************************/
1543 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1544 /*************************************************************************/
1546 - (void)applicationLaunched:(NSNotification *)note
1549 if (!note || ([[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer])) {
1550 ITDebugLog(@"Remote application launched.");
1551 playerRunningState = ITMTRemotePlayerRunning;
1552 [[self currentRemote] begin];
1553 [self setLatestSongIdentifier:@""];
1555 if (_needsPolling) {
1556 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1558 selector:@selector(timerUpdate)
1560 repeats:YES] retain];
1562 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1563 if (![df boolForKey:@"UsePollingOnly"]) {
1564 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackChanged:) name:@"ITMTTrackChanged" object:nil];
1566 [self setupHotKeys];
1569 [self networkError:localException];
1573 - (void)applicationTerminated:(NSNotification *)note
1576 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer]) {
1577 ITDebugLog(@"Remote application terminated.");
1578 playerRunningState = ITMTRemotePlayerNotRunning;
1579 [[self currentRemote] halt];
1580 [refreshTimer invalidate];
1581 [refreshTimer release];
1583 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ITMTTrackChanged" object:nil];
1584 [statusItem setEnabled:YES];
1585 [statusItem setToolTip:@"iTunes not running."];
1586 [self clearHotKeys];
1589 if ([df objectForKey:@"ShowPlayer"] != nil) {
1591 ITDebugLog(@"Setting up show player hot key.");
1592 hotKey = [[ITHotKey alloc] init];
1593 [hotKey setName:@"ShowPlayer"];
1594 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1595 [hotKey setTarget:self];
1596 [hotKey setAction:@selector(showPlayer)];
1597 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1601 [self networkError:localException];
1606 /*************************************************************************/
1608 #pragma mark NSApplication DELEGATE METHODS
1609 /*************************************************************************/
1611 - (void)applicationWillTerminate:(NSNotification *)note
1613 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
1614 [networkController stopRemoteServerSearch];
1615 [self clearHotKeys];
1616 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1619 - (void)applicationDidBecomeActive:(NSNotification *)note
1621 //This appears to not work in 10.4
1622 if (_open && !blinged && ![[ITAboutWindowController sharedController] isVisible] && ![NSApp mainWindow] && ([[StatusWindow sharedWindow] exitMode] == ITTransientStatusWindowExitAfterDelay)) {
1623 [[MainController sharedController] showPreferences];
1627 /*************************************************************************/
1629 #pragma mark DEALLOCATION METHOD
1630 /*************************************************************************/
1634 [self applicationTerminated:nil];
1636 [statusItem release];
1637 [statusWindowController release];
1638 [menuController release];
1639 [networkController release];
1640 [_serverCheckLock release];