1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import "NetworkController.h"
5 #import <ITKit/ITHotKeyCenter.h>
6 #import <ITKit/ITHotKey.h>
7 #import <ITKit/ITKeyCombo.h>
8 #import "StatusWindow.h"
9 #import "StatusWindowController.h"
10 #import "StatusItemHack.h"
12 @interface MainController(Private)
13 - (ITMTRemote *)loadRemote;
15 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
16 - (void)showCurrentTrackInfo;
17 - (void)applicationLaunched:(NSNotification *)note;
18 - (void)applicationTerminated:(NSNotification *)note;
21 static MainController *sharedController;
23 @implementation MainController
25 + (MainController *)sharedController
27 return sharedController;
30 /*************************************************************************/
32 #pragma mark INITIALIZATION/DEALLOCATION METHODS
33 /*************************************************************************/
37 if ( ( self = [super init] ) ) {
38 sharedController = self;
40 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
41 statusWindowController = [StatusWindowController sharedController];
42 menuController = [[MenuController alloc] init];
43 df = [[NSUserDefaults standardUserDefaults] retain];
50 - (void)applicationDidFinishLaunching:(NSNotification *)note
52 //Turn on debug mode if needed
53 if ([df boolForKey:@"ITDebugMode"]) {
57 currentRemote = [self loadRemote];
58 [[self currentRemote] begin];
60 //Turn on network stuff if needed
61 networkController = [[NetworkController alloc] init];
62 if ([df boolForKey:@"enableSharing"]) {
63 [self setServerStatus:YES];
64 } else if ([df boolForKey:@"useSharedPlayer"] && [df boolForKey:@"alwaysUseSharedPlayer"]) {
65 [self connectToServer];
68 //Setup for notification of the remote player launching or quitting
69 [[[NSWorkspace sharedWorkspace] notificationCenter]
71 selector:@selector(applicationTerminated:)
72 name:NSWorkspaceDidTerminateApplicationNotification
75 [[[NSWorkspace sharedWorkspace] notificationCenter]
77 selector:@selector(applicationLaunched:)
78 name:NSWorkspaceDidLaunchApplicationNotification
81 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
82 [[PreferencesController sharedPrefs] registerDefaults];
85 [StatusItemHack install];
86 statusItem = [[ITStatusItem alloc]
87 initWithStatusBar:[NSStatusBar systemStatusBar]
88 withLength:NSSquareStatusItemLength];
90 bling = [[MTBlingController alloc] init];
92 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
94 selector:@selector(blingTime)
99 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
100 [self applicationLaunched:nil];
102 if ([df boolForKey:@"LaunchPlayerWithMT"])
105 [self applicationTerminated:nil];
108 [self networkError:localException];
111 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
112 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
114 [networkController startRemoteServerSearch];
118 - (ITMTRemote *)loadRemote
120 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
121 ITDebugLog(@"Gathering remotes.");
123 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
124 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
125 NSString *bundlePath;
127 while ( (bundlePath = [enumerator nextObject]) ) {
128 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
131 Class remoteClass = [remoteBundle principalClass];
133 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
134 [remoteClass isKindOfClass:[NSObject class]]) {
135 id remote = [remoteClass remote];
136 ITDebugLog(@"Adding remote at path %@", bundlePath);
137 [remoteArray addObject:remote];
142 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
143 // if ( [remoteArray count] > 1 ) {
144 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
146 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
149 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
150 return [remoteArray objectAtIndex:0];
153 /*************************************************************************/
155 #pragma mark INSTANCE METHODS
156 /*************************************************************************/
158 /*- (void)startTimerInNewThread
160 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
161 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
162 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
164 selector:@selector(timerUpdate)
166 repeats:YES] retain];
168 ITDebugLog(@"Timer started.");
172 - (void)setBlingTime:(NSDate*)date
174 NSMutableDictionary *globalPrefs;
176 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
178 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
180 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
182 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
184 [globalPrefs release];
187 - (NSDate*)getBlingTime
190 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
195 NSDate *now = [NSDate date];
196 if (![self blingBling]) {
197 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
198 [self setBlingTime:now];
200 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
202 [statusItem setEnabled:NO];
204 if ([refreshTimer isValid]) {
205 [refreshTimer invalidate];
207 [statusWindowController showRegistrationQueryWindow];
211 [statusItem setEnabled:YES];
213 if (![refreshTimer isValid]) {
214 [refreshTimer release];
215 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
217 selector:@selector(timerUpdate)
219 repeats:YES] retain];
223 [self setBlingTime:nil];
234 if ( ! ([bling checkDone] == 2475) ) {
241 - (BOOL)songIsPlaying
243 NSString *identifier;
245 identifier = [[self currentRemote] playerStateUniqueIdentifier];
247 [self networkError:localException];
249 return ( ! ([identifier isEqualToString:@"0-0"]) );
252 - (BOOL)radioIsPlaying
254 ITMTRemotePlayerPlaylistClass class;
256 class = [[self currentRemote] currentPlaylistClass];
258 [self networkError:localException];
260 return (class == ITMTRemotePlayerRadioPlaylist );
265 NSString *identifier;
267 identifier = [[self currentRemote] playerStateUniqueIdentifier];
269 [self networkError:localException];
271 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
274 - (NSString *)latestSongIdentifier
276 return _latestSongIdentifier;
279 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
281 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
282 [_latestSongIdentifier autorelease];
283 _latestSongIdentifier = [newIdentifier copy];
288 if ([networkController isConnectedToServer]) {
289 [statusItem setMenu:[menuController menu]];
292 if ( [self songChanged] && (timerUpdating != YES) ) {
293 ITDebugLog(@"The song changed.");
297 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
298 [menuController rebuildSubmenus];
300 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
301 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
304 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
306 [self networkError:localException];
315 ITDebugLog(@"Menu clicked.");
316 if ([networkController isConnectedToServer]) {
317 //Used the cached version
322 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
323 [statusItem setMenu:[menuController menu]];
325 [statusItem setMenu:[menuController menuForNoPlayer]];
328 [self networkError:localException];
341 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
342 ITDebugLog(@"Play/Pause toggled");
343 if (state == ITMTRemotePlayerPlaying) {
344 [[self currentRemote] pause];
345 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
346 [[self currentRemote] pause];
347 [[self currentRemote] play];
349 [[self currentRemote] play];
352 [self networkError:localException];
360 ITDebugLog(@"Going to next song.");
362 [[self currentRemote] goToNextSong];
364 [self networkError:localException];
371 ITDebugLog(@"Going to previous song.");
373 [[self currentRemote] goToPreviousSong];
375 [self networkError:localException];
382 ITDebugLog(@"Fast forwarding.");
384 [[self currentRemote] forward];
386 [self networkError:localException];
393 ITDebugLog(@"Rewinding.");
395 [[self currentRemote] rewind];
397 [self networkError:localException];
402 - (void)selectPlaylistAtIndex:(int)index
404 ITDebugLog(@"Selecting playlist %i", index);
406 [[self currentRemote] switchToPlaylistAtIndex:index];
408 [self networkError:localException];
413 - (void)selectSongAtIndex:(int)index
415 ITDebugLog(@"Selecting song %i", index);
417 [[self currentRemote] switchToSongAtIndex:index];
419 [self networkError:localException];
424 - (void)selectSongRating:(int)rating
426 ITDebugLog(@"Selecting song rating %i", rating);
428 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
430 [self networkError:localException];
435 - (void)selectEQPresetAtIndex:(int)index
437 ITDebugLog(@"Selecting EQ preset %i", index);
439 [[self currentRemote] switchToEQAtIndex:index];
441 [self networkError:localException];
448 ITDebugLog(@"Beginning show player.");
449 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
450 ITDebugLog(@"Showing player interface.");
452 [[self currentRemote] showPrimaryInterface];
454 [self networkError:localException];
457 ITDebugLog(@"Launching player.");
459 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
460 ITDebugLog(@"Error Launching Player");
463 [self networkError:localException];
466 ITDebugLog(@"Finished show player.");
469 - (void)showPreferences
471 ITDebugLog(@"Show preferences.");
472 [[PreferencesController sharedPrefs] setController:self];
473 [[PreferencesController sharedPrefs] showPrefsWindow:self];
476 - (void)quitMenuTunes
478 ITDebugLog(@"Quitting MenuTunes.");
479 [NSApp terminate:self];
485 - (void)closePreferences
487 ITDebugLog(@"Preferences closed.");
488 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
493 - (ITMTRemote *)currentRemote
495 return currentRemote;
506 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
507 ITHotKey *nextHotKey;
508 ITDebugLog(@"Clearing hot keys.");
509 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
510 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
512 ITDebugLog(@"Done clearing hot keys.");
518 ITDebugLog(@"Setting up hot keys.");
520 if (playerRunningState == ITMTRemotePlayerNotRunning) {
524 if ([df objectForKey:@"PlayPause"] != nil) {
525 ITDebugLog(@"Setting up play pause hot key.");
526 hotKey = [[ITHotKey alloc] init];
527 [hotKey setName:@"PlayPause"];
528 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
529 [hotKey setTarget:self];
530 [hotKey setAction:@selector(playPause)];
531 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
534 if ([df objectForKey:@"NextTrack"] != nil) {
535 ITDebugLog(@"Setting up next track hot key.");
536 hotKey = [[ITHotKey alloc] init];
537 [hotKey setName:@"NextTrack"];
538 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
539 [hotKey setTarget:self];
540 [hotKey setAction:@selector(nextSong)];
541 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
544 if ([df objectForKey:@"PrevTrack"] != nil) {
545 ITDebugLog(@"Setting up previous track hot key.");
546 hotKey = [[ITHotKey alloc] init];
547 [hotKey setName:@"PrevTrack"];
548 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
549 [hotKey setTarget:self];
550 [hotKey setAction:@selector(prevSong)];
551 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
554 if ([df objectForKey:@"ShowPlayer"] != nil) {
555 ITDebugLog(@"Setting up show player hot key.");
556 hotKey = [[ITHotKey alloc] init];
557 [hotKey setName:@"ShowPlayer"];
558 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
559 [hotKey setTarget:self];
560 [hotKey setAction:@selector(showPlayer)];
561 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
564 if ([df objectForKey:@"TrackInfo"] != nil) {
565 ITDebugLog(@"Setting up track info hot key.");
566 hotKey = [[ITHotKey alloc] init];
567 [hotKey setName:@"TrackInfo"];
568 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
569 [hotKey setTarget:self];
570 [hotKey setAction:@selector(showCurrentTrackInfo)];
571 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
574 if ([df objectForKey:@"UpcomingSongs"] != nil) {
575 ITDebugLog(@"Setting up upcoming songs hot key.");
576 hotKey = [[ITHotKey alloc] init];
577 [hotKey setName:@"UpcomingSongs"];
578 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
579 [hotKey setTarget:self];
580 [hotKey setAction:@selector(showUpcomingSongs)];
581 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
584 if ([df objectForKey:@"ToggleLoop"] != nil) {
585 ITDebugLog(@"Setting up toggle loop hot key.");
586 hotKey = [[ITHotKey alloc] init];
587 [hotKey setName:@"ToggleLoop"];
588 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
589 [hotKey setTarget:self];
590 [hotKey setAction:@selector(toggleLoop)];
591 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
594 if ([df objectForKey:@"ToggleShuffle"] != nil) {
595 ITDebugLog(@"Setting up toggle shuffle hot key.");
596 hotKey = [[ITHotKey alloc] init];
597 [hotKey setName:@"ToggleShuffle"];
598 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
599 [hotKey setTarget:self];
600 [hotKey setAction:@selector(toggleShuffle)];
601 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
604 if ([df objectForKey:@"IncrementVolume"] != nil) {
605 ITDebugLog(@"Setting up increment volume hot key.");
606 hotKey = [[ITHotKey alloc] init];
607 [hotKey setName:@"IncrementVolume"];
608 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
609 [hotKey setTarget:self];
610 [hotKey setAction:@selector(incrementVolume)];
611 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
614 if ([df objectForKey:@"DecrementVolume"] != nil) {
615 ITDebugLog(@"Setting up decrement volume hot key.");
616 hotKey = [[ITHotKey alloc] init];
617 [hotKey setName:@"DecrementVolume"];
618 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
619 [hotKey setTarget:self];
620 [hotKey setAction:@selector(decrementVolume)];
621 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
624 if ([df objectForKey:@"IncrementRating"] != nil) {
625 ITDebugLog(@"Setting up increment rating hot key.");
626 hotKey = [[ITHotKey alloc] init];
627 [hotKey setName:@"IncrementRating"];
628 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
629 [hotKey setTarget:self];
630 [hotKey setAction:@selector(incrementRating)];
631 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
634 if ([df objectForKey:@"DecrementRating"] != nil) {
635 ITDebugLog(@"Setting up decrement rating hot key.");
636 hotKey = [[ITHotKey alloc] init];
637 [hotKey setName:@"DecrementRating"];
638 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
639 [hotKey setTarget:self];
640 [hotKey setAction:@selector(decrementRating)];
641 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
643 ITDebugLog(@"Finished setting up hot keys.");
646 - (void)showCurrentTrackInfo
648 ITMTRemotePlayerSource source;
650 NSString *album = nil;
651 NSString *artist = nil;
652 NSString *time = nil;
653 NSString *track = nil;
657 source = [[self currentRemote] currentSource];
658 title = [[self currentRemote] currentSongTitle];
660 [self networkError:localException];
663 ITDebugLog(@"Showing track info status window.");
667 if ( [df boolForKey:@"showAlbum"] ) {
669 album = [[self currentRemote] currentSongAlbum];
671 [self networkError:localException];
675 if ( [df boolForKey:@"showArtist"] ) {
677 artist = [[self currentRemote] currentSongArtist];
679 [self networkError:localException];
683 if ( [df boolForKey:@"showTime"] ) {
685 time = [NSString stringWithFormat:@"%@: %@ / %@",
687 [[self currentRemote] currentSongElapsed],
688 [[self currentRemote] currentSongLength]];
690 [self networkError:localException];
694 if ( [df boolForKey:@"showTrackNumber"] ) {
699 trackNo = [[self currentRemote] currentSongTrack];
700 trackCount = [[self currentRemote] currentAlbumTrackCount];
702 [self networkError:localException];
705 if ( (trackNo > 0) || (trackCount > 0) ) {
706 track = [NSString stringWithFormat:@"%@: %i %@ %i",
707 @"Track", trackNo, @"of", trackCount];
711 if ( [df boolForKey:@"showTrackRating"] ) {
715 currentRating = [[self currentRemote] currentSongRating];
717 [self networkError:localException];
720 if (currentRating >= 0.0) {
721 rating = ( currentRating * 5 );
726 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
729 [statusWindowController showSongInfoWindowWithSource:source
738 - (void)showUpcomingSongs
743 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
745 [self networkError:localException];
748 ITDebugLog(@"Showing upcoming songs status window.");
751 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
752 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
753 int curTrack = [[self currentRemote] currentSongIndex];
756 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
758 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
762 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
764 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
767 [self networkError:localException];
771 - (void)incrementVolume
774 float volume = [[self currentRemote] volume];
775 float dispVol = volume;
776 ITDebugLog(@"Incrementing volume.");
785 ITDebugLog(@"Setting volume to %f", volume);
786 [[self currentRemote] setVolume:volume];
788 // Show volume status window
789 [statusWindowController showVolumeWindowWithLevel:dispVol];
791 [self networkError:localException];
795 - (void)decrementVolume
798 float volume = [[self currentRemote] volume];
799 float dispVol = volume;
800 ITDebugLog(@"Decrementing volume.");
809 ITDebugLog(@"Setting volume to %f", volume);
810 [[self currentRemote] setVolume:volume];
812 //Show volume status window
813 [statusWindowController showVolumeWindowWithLevel:dispVol];
815 [self networkError:localException];
819 - (void)incrementRating
822 float rating = [[self currentRemote] currentSongRating];
823 ITDebugLog(@"Incrementing rating.");
825 if ([[self currentRemote] currentPlaylistIndex] == 0) {
826 ITDebugLog(@"No song playing, rating change aborted.");
834 ITDebugLog(@"Setting rating to %f", rating);
835 [[self currentRemote] setCurrentSongRating:rating];
837 //Show rating status window
838 [statusWindowController showRatingWindowWithRating:rating];
840 [self networkError:localException];
844 - (void)decrementRating
847 float rating = [[self currentRemote] currentSongRating];
848 ITDebugLog(@"Decrementing rating.");
850 if ([[self currentRemote] currentPlaylistIndex] == 0) {
851 ITDebugLog(@"No song playing, rating change aborted.");
859 ITDebugLog(@"Setting rating to %f", rating);
860 [[self currentRemote] setCurrentSongRating:rating];
862 //Show rating status window
863 [statusWindowController showRatingWindowWithRating:rating];
865 [self networkError:localException];
872 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
873 ITDebugLog(@"Toggling repeat mode.");
874 switch (repeatMode) {
875 case ITMTRemotePlayerRepeatOff:
876 repeatMode = ITMTRemotePlayerRepeatAll;
878 case ITMTRemotePlayerRepeatAll:
879 repeatMode = ITMTRemotePlayerRepeatOne;
881 case ITMTRemotePlayerRepeatOne:
882 repeatMode = ITMTRemotePlayerRepeatOff;
885 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
886 [[self currentRemote] setRepeatMode:repeatMode];
888 //Show loop status window
889 [statusWindowController showRepeatWindowWithMode:repeatMode];
891 [self networkError:localException];
895 - (void)toggleShuffle
898 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
899 ITDebugLog(@"Toggling shuffle mode.");
900 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
901 //Show shuffle status window
902 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
903 [statusWindowController showShuffleWindow:newShuffleEnabled];
905 [self networkError:localException];
909 - (void)registerNowOK
911 [[StatusWindow sharedWindow] setLocked:NO];
912 [[StatusWindow sharedWindow] vanish:self];
913 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
918 - (void)registerNowCancel
920 [[StatusWindow sharedWindow] setLocked:NO];
921 [[StatusWindow sharedWindow] vanish:self];
922 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
924 [NSApp terminate:self];
927 /*************************************************************************/
929 #pragma mark NETWORK HANDLERS
930 /*************************************************************************/
932 - (void)setServerStatus:(BOOL)newStatus
936 [networkController setServerStatus:YES];
939 [networkController setServerStatus:NO];
943 - (BOOL)connectToServer
946 if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
947 currentRemote = [networkController sharedRemote];
948 [refreshTimer invalidate];
951 currentRemote = [remoteArray objectAtIndex:0];
956 - (BOOL)disconnectFromServer
959 currentRemote = [remoteArray objectAtIndex:0];
960 [networkController disconnect];
965 - (void)networkError:(NSException *)exception
967 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
968 NSRunAlertPanel(@"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);
969 if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
971 ITDebugLog(@"CRITICAL ERROR DISCONNECTING!");
975 /*************************************************************************/
977 #pragma mark WORKSPACE NOTIFICATION HANDLERS
978 /*************************************************************************/
980 - (void)applicationLaunched:(NSNotification *)note
983 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
984 ITDebugLog(@"Remote application launched.");
985 playerRunningState = ITMTRemotePlayerRunning;
986 [[self currentRemote] begin];
987 [self setLatestSongIdentifier:@""];
989 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
991 selector:@selector(timerUpdate)
993 repeats:YES] retain];
994 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
998 [self networkError:localException];
1002 - (void)applicationTerminated:(NSNotification *)note
1005 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1006 ITDebugLog(@"Remote application terminated.");
1007 [[self currentRemote] halt];
1008 [refreshTimer invalidate];
1009 [refreshTimer release];
1011 [self clearHotKeys];
1012 playerRunningState = ITMTRemotePlayerNotRunning;
1015 [self networkError:localException];
1020 /*************************************************************************/
1022 #pragma mark NSApplication DELEGATE METHODS
1023 /*************************************************************************/
1025 - (void)applicationWillTerminate:(NSNotification *)note
1027 [self clearHotKeys];
1028 [networkController stopRemoteServerSearch];
1029 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1033 /*************************************************************************/
1035 #pragma mark DEALLOCATION METHOD
1036 /*************************************************************************/
1040 [self applicationTerminated:nil];
1042 [statusItem release];
1043 [statusWindowController release];
1044 [menuController release];
1045 [networkController release];