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 "StatusWindow.h"
10 #import "StatusWindowController.h"
11 #import "StatusItemHack.h"
13 @interface MainController(Private)
14 - (ITMTRemote *)loadRemote;
16 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
17 - (void)showCurrentTrackInfo;
18 - (void)applicationLaunched:(NSNotification *)note;
19 - (void)applicationTerminated:(NSNotification *)note;
22 static MainController *sharedController;
24 @implementation MainController
26 + (MainController *)sharedController
28 return sharedController;
31 /*************************************************************************/
33 #pragma mark INITIALIZATION/DEALLOCATION METHODS
34 /*************************************************************************/
38 if ( ( self = [super init] ) ) {
39 sharedController = self;
41 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
42 statusWindowController = [StatusWindowController sharedController];
43 menuController = [[MenuController alloc] init];
44 df = [[NSUserDefaults standardUserDefaults] retain];
51 - (void)applicationDidFinishLaunching:(NSNotification *)note
53 //Turn on debug mode if needed
54 if ([df boolForKey:@"ITDebugMode"]) {
58 currentRemote = [self loadRemote];
59 [[self currentRemote] begin];
61 //Turn on network stuff if needed
62 networkController = [[NetworkController alloc] init];
63 if ([df boolForKey:@"enableSharing"]) {
64 [self setServerStatus:YES];
65 } else if ([df boolForKey:@"useSharedPlayer"]) {
66 if (![self connectToServer]) {
67 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
71 //Setup for notification of the remote player launching or quitting
72 [[[NSWorkspace sharedWorkspace] notificationCenter]
74 selector:@selector(applicationTerminated:)
75 name:NSWorkspaceDidTerminateApplicationNotification
78 [[[NSWorkspace sharedWorkspace] notificationCenter]
80 selector:@selector(applicationLaunched:)
81 name:NSWorkspaceDidLaunchApplicationNotification
84 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
85 [[PreferencesController sharedPrefs] registerDefaults];
88 [StatusItemHack install];
89 statusItem = [[ITStatusItem alloc]
90 initWithStatusBar:[NSStatusBar systemStatusBar]
91 withLength:NSSquareStatusItemLength];
93 bling = [[MTBlingController alloc] init];
95 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
97 selector:@selector(blingTime)
102 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
103 [self applicationLaunched:nil];
105 if ([df boolForKey:@"LaunchPlayerWithMT"])
108 [self applicationTerminated:nil];
111 [self networkError:localException];
114 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
115 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
117 [networkController startRemoteServerSearch];
121 - (ITMTRemote *)loadRemote
123 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
124 ITDebugLog(@"Gathering remotes.");
126 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
127 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
128 NSString *bundlePath;
130 while ( (bundlePath = [enumerator nextObject]) ) {
131 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
134 Class remoteClass = [remoteBundle principalClass];
136 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
137 [remoteClass isKindOfClass:[NSObject class]]) {
138 id remote = [remoteClass remote];
139 ITDebugLog(@"Adding remote at path %@", bundlePath);
140 [remoteArray addObject:remote];
145 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
146 // if ( [remoteArray count] > 1 ) {
147 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
149 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
152 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
153 return [remoteArray objectAtIndex:0];
156 /*************************************************************************/
158 #pragma mark INSTANCE METHODS
159 /*************************************************************************/
161 /*- (void)startTimerInNewThread
163 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
164 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
165 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
167 selector:@selector(timerUpdate)
169 repeats:YES] retain];
171 ITDebugLog(@"Timer started.");
175 - (void)setBlingTime:(NSDate*)date
177 NSMutableDictionary *globalPrefs;
179 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
181 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
183 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
185 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
187 [globalPrefs release];
190 - (NSDate*)getBlingTime
193 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
198 NSDate *now = [NSDate date];
199 if (![self blingBling]) {
200 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
201 [self setBlingTime:now];
203 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
205 [statusItem setEnabled:NO];
207 if ([refreshTimer isValid]) {
208 [refreshTimer invalidate];
210 [statusWindowController showRegistrationQueryWindow];
214 [statusItem setEnabled:YES];
216 if (![refreshTimer isValid]) {
217 [refreshTimer release];
218 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
220 selector:@selector(timerUpdate)
222 repeats:YES] retain];
226 [self setBlingTime:nil];
237 if ( ! ([bling checkDone] == 2475) ) {
244 - (BOOL)songIsPlaying
246 NSString *identifier;
248 identifier = [[self currentRemote] playerStateUniqueIdentifier];
250 [self networkError:localException];
252 return ( ! ([identifier isEqualToString:@"0-0"]) );
255 - (BOOL)radioIsPlaying
257 ITMTRemotePlayerPlaylistClass class;
259 class = [[self currentRemote] currentPlaylistClass];
261 [self networkError:localException];
263 return (class == ITMTRemotePlayerRadioPlaylist );
268 NSString *identifier;
270 identifier = [[self currentRemote] playerStateUniqueIdentifier];
272 [self networkError:localException];
274 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
277 - (NSString *)latestSongIdentifier
279 return _latestSongIdentifier;
282 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
284 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
285 [_latestSongIdentifier autorelease];
286 _latestSongIdentifier = [newIdentifier copy];
291 if ([networkController isConnectedToServer]) {
292 [statusItem setMenu:[menuController menu]];
295 if ( [self songChanged] && (timerUpdating != YES) ) {
296 ITDebugLog(@"The song changed.");
300 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
301 [menuController rebuildSubmenus];
303 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
304 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
307 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
309 [self networkError:localException];
318 ITDebugLog(@"Menu clicked.");
319 if ([networkController isConnectedToServer]) {
320 //Used the cached version
325 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
326 [statusItem setMenu:[menuController menu]];
328 [statusItem setMenu:[menuController menuForNoPlayer]];
331 [self networkError:localException];
344 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
345 ITDebugLog(@"Play/Pause toggled");
346 if (state == ITMTRemotePlayerPlaying) {
347 [[self currentRemote] pause];
348 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
349 [[self currentRemote] pause];
350 [[self currentRemote] play];
352 [[self currentRemote] play];
355 [self networkError:localException];
363 ITDebugLog(@"Going to next song.");
365 [[self currentRemote] goToNextSong];
367 [self networkError:localException];
374 ITDebugLog(@"Going to previous song.");
376 [[self currentRemote] goToPreviousSong];
378 [self networkError:localException];
385 ITDebugLog(@"Fast forwarding.");
387 [[self currentRemote] forward];
389 [self networkError:localException];
396 ITDebugLog(@"Rewinding.");
398 [[self currentRemote] rewind];
400 [self networkError:localException];
405 - (void)selectPlaylistAtIndex:(int)index
407 ITDebugLog(@"Selecting playlist %i", index);
409 [[self currentRemote] switchToPlaylistAtIndex:index];
411 [self networkError:localException];
416 - (void)selectSongAtIndex:(int)index
418 ITDebugLog(@"Selecting song %i", index);
420 [[self currentRemote] switchToSongAtIndex:index];
422 [self networkError:localException];
427 - (void)selectSongRating:(int)rating
429 ITDebugLog(@"Selecting song rating %i", rating);
431 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
433 [self networkError:localException];
438 - (void)selectEQPresetAtIndex:(int)index
440 ITDebugLog(@"Selecting EQ preset %i", index);
442 [[self currentRemote] switchToEQAtIndex:index];
444 [self networkError:localException];
451 ITDebugLog(@"Beginning show player.");
452 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
453 ITDebugLog(@"Showing player interface.");
455 [[self currentRemote] showPrimaryInterface];
457 [self networkError:localException];
460 ITDebugLog(@"Launching player.");
462 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
463 ITDebugLog(@"Error Launching Player");
466 [self networkError:localException];
469 ITDebugLog(@"Finished show player.");
472 - (void)showPreferences
474 ITDebugLog(@"Show preferences.");
475 [[PreferencesController sharedPrefs] setController:self];
476 [[PreferencesController sharedPrefs] showPrefsWindow:self];
479 - (void)quitMenuTunes
481 ITDebugLog(@"Quitting MenuTunes.");
482 [NSApp terminate:self];
488 - (void)closePreferences
490 ITDebugLog(@"Preferences closed.");
491 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
496 - (ITMTRemote *)currentRemote
498 return currentRemote;
509 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
510 ITHotKey *nextHotKey;
511 ITDebugLog(@"Clearing hot keys.");
512 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
513 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
515 ITDebugLog(@"Done clearing hot keys.");
521 ITDebugLog(@"Setting up hot keys.");
523 if (playerRunningState == ITMTRemotePlayerNotRunning) {
527 if ([df objectForKey:@"PlayPause"] != nil) {
528 ITDebugLog(@"Setting up play pause hot key.");
529 hotKey = [[ITHotKey alloc] init];
530 [hotKey setName:@"PlayPause"];
531 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
532 [hotKey setTarget:self];
533 [hotKey setAction:@selector(playPause)];
534 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
537 if ([df objectForKey:@"NextTrack"] != nil) {
538 ITDebugLog(@"Setting up next track hot key.");
539 hotKey = [[ITHotKey alloc] init];
540 [hotKey setName:@"NextTrack"];
541 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
542 [hotKey setTarget:self];
543 [hotKey setAction:@selector(nextSong)];
544 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
547 if ([df objectForKey:@"PrevTrack"] != nil) {
548 ITDebugLog(@"Setting up previous track hot key.");
549 hotKey = [[ITHotKey alloc] init];
550 [hotKey setName:@"PrevTrack"];
551 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
552 [hotKey setTarget:self];
553 [hotKey setAction:@selector(prevSong)];
554 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
557 if ([df objectForKey:@"ShowPlayer"] != nil) {
558 ITDebugLog(@"Setting up show player hot key.");
559 hotKey = [[ITHotKey alloc] init];
560 [hotKey setName:@"ShowPlayer"];
561 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
562 [hotKey setTarget:self];
563 [hotKey setAction:@selector(showPlayer)];
564 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
567 if ([df objectForKey:@"TrackInfo"] != nil) {
568 ITDebugLog(@"Setting up track info hot key.");
569 hotKey = [[ITHotKey alloc] init];
570 [hotKey setName:@"TrackInfo"];
571 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
572 [hotKey setTarget:self];
573 [hotKey setAction:@selector(showCurrentTrackInfo)];
574 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
577 if ([df objectForKey:@"UpcomingSongs"] != nil) {
578 ITDebugLog(@"Setting up upcoming songs hot key.");
579 hotKey = [[ITHotKey alloc] init];
580 [hotKey setName:@"UpcomingSongs"];
581 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
582 [hotKey setTarget:self];
583 [hotKey setAction:@selector(showUpcomingSongs)];
584 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
587 if ([df objectForKey:@"ToggleLoop"] != nil) {
588 ITDebugLog(@"Setting up toggle loop hot key.");
589 hotKey = [[ITHotKey alloc] init];
590 [hotKey setName:@"ToggleLoop"];
591 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
592 [hotKey setTarget:self];
593 [hotKey setAction:@selector(toggleLoop)];
594 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
597 if ([df objectForKey:@"ToggleShuffle"] != nil) {
598 ITDebugLog(@"Setting up toggle shuffle hot key.");
599 hotKey = [[ITHotKey alloc] init];
600 [hotKey setName:@"ToggleShuffle"];
601 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
602 [hotKey setTarget:self];
603 [hotKey setAction:@selector(toggleShuffle)];
604 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
607 if ([df objectForKey:@"IncrementVolume"] != nil) {
608 ITDebugLog(@"Setting up increment volume hot key.");
609 hotKey = [[ITHotKey alloc] init];
610 [hotKey setName:@"IncrementVolume"];
611 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
612 [hotKey setTarget:self];
613 [hotKey setAction:@selector(incrementVolume)];
614 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
617 if ([df objectForKey:@"DecrementVolume"] != nil) {
618 ITDebugLog(@"Setting up decrement volume hot key.");
619 hotKey = [[ITHotKey alloc] init];
620 [hotKey setName:@"DecrementVolume"];
621 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
622 [hotKey setTarget:self];
623 [hotKey setAction:@selector(decrementVolume)];
624 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
627 if ([df objectForKey:@"IncrementRating"] != nil) {
628 ITDebugLog(@"Setting up increment rating hot key.");
629 hotKey = [[ITHotKey alloc] init];
630 [hotKey setName:@"IncrementRating"];
631 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
632 [hotKey setTarget:self];
633 [hotKey setAction:@selector(incrementRating)];
634 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
637 if ([df objectForKey:@"DecrementRating"] != nil) {
638 ITDebugLog(@"Setting up decrement rating hot key.");
639 hotKey = [[ITHotKey alloc] init];
640 [hotKey setName:@"DecrementRating"];
641 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
642 [hotKey setTarget:self];
643 [hotKey setAction:@selector(decrementRating)];
644 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
646 ITDebugLog(@"Finished setting up hot keys.");
649 - (void)showCurrentTrackInfo
651 ITMTRemotePlayerSource source;
653 NSString *album = nil;
654 NSString *artist = nil;
655 NSString *time = nil;
656 NSString *track = nil;
660 source = [[self currentRemote] currentSource];
661 title = [[self currentRemote] currentSongTitle];
663 [self networkError:localException];
666 ITDebugLog(@"Showing track info status window.");
670 if ( [df boolForKey:@"showAlbum"] ) {
672 album = [[self currentRemote] currentSongAlbum];
674 [self networkError:localException];
678 if ( [df boolForKey:@"showArtist"] ) {
680 artist = [[self currentRemote] currentSongArtist];
682 [self networkError:localException];
686 if ( [df boolForKey:@"showTime"] ) {
688 time = [NSString stringWithFormat:@"%@: %@ / %@",
690 [[self currentRemote] currentSongElapsed],
691 [[self currentRemote] currentSongLength]];
693 [self networkError:localException];
697 if ( [df boolForKey:@"showTrackNumber"] ) {
702 trackNo = [[self currentRemote] currentSongTrack];
703 trackCount = [[self currentRemote] currentAlbumTrackCount];
705 [self networkError:localException];
708 if ( (trackNo > 0) || (trackCount > 0) ) {
709 track = [NSString stringWithFormat:@"%@: %i %@ %i",
710 @"Track", trackNo, @"of", trackCount];
714 if ( [df boolForKey:@"showTrackRating"] ) {
718 currentRating = [[self currentRemote] currentSongRating];
720 [self networkError:localException];
723 if (currentRating >= 0.0) {
724 rating = ( currentRating * 5 );
729 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
732 [statusWindowController showSongInfoWindowWithSource:source
741 - (void)showUpcomingSongs
746 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
748 [self networkError:localException];
751 ITDebugLog(@"Showing upcoming songs status window.");
754 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
755 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
756 int curTrack = [[self currentRemote] currentSongIndex];
759 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
761 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
765 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
767 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
770 [self networkError:localException];
774 - (void)incrementVolume
777 float volume = [[self currentRemote] volume];
778 float dispVol = volume;
779 ITDebugLog(@"Incrementing volume.");
788 ITDebugLog(@"Setting volume to %f", volume);
789 [[self currentRemote] setVolume:volume];
791 // Show volume status window
792 [statusWindowController showVolumeWindowWithLevel:dispVol];
794 [self networkError:localException];
798 - (void)decrementVolume
801 float volume = [[self currentRemote] volume];
802 float dispVol = volume;
803 ITDebugLog(@"Decrementing volume.");
812 ITDebugLog(@"Setting volume to %f", volume);
813 [[self currentRemote] setVolume:volume];
815 //Show volume status window
816 [statusWindowController showVolumeWindowWithLevel:dispVol];
818 [self networkError:localException];
822 - (void)incrementRating
825 float rating = [[self currentRemote] currentSongRating];
826 ITDebugLog(@"Incrementing rating.");
828 if ([[self currentRemote] currentPlaylistIndex] == 0) {
829 ITDebugLog(@"No song playing, rating change aborted.");
837 ITDebugLog(@"Setting rating to %f", rating);
838 [[self currentRemote] setCurrentSongRating:rating];
840 //Show rating status window
841 [statusWindowController showRatingWindowWithRating:rating];
843 [self networkError:localException];
847 - (void)decrementRating
850 float rating = [[self currentRemote] currentSongRating];
851 ITDebugLog(@"Decrementing rating.");
853 if ([[self currentRemote] currentPlaylistIndex] == 0) {
854 ITDebugLog(@"No song playing, rating change aborted.");
862 ITDebugLog(@"Setting rating to %f", rating);
863 [[self currentRemote] setCurrentSongRating:rating];
865 //Show rating status window
866 [statusWindowController showRatingWindowWithRating:rating];
868 [self networkError:localException];
875 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
876 ITDebugLog(@"Toggling repeat mode.");
877 switch (repeatMode) {
878 case ITMTRemotePlayerRepeatOff:
879 repeatMode = ITMTRemotePlayerRepeatAll;
881 case ITMTRemotePlayerRepeatAll:
882 repeatMode = ITMTRemotePlayerRepeatOne;
884 case ITMTRemotePlayerRepeatOne:
885 repeatMode = ITMTRemotePlayerRepeatOff;
888 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
889 [[self currentRemote] setRepeatMode:repeatMode];
891 //Show loop status window
892 [statusWindowController showRepeatWindowWithMode:repeatMode];
894 [self networkError:localException];
898 - (void)toggleShuffle
901 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
902 ITDebugLog(@"Toggling shuffle mode.");
903 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
904 //Show shuffle status window
905 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
906 [statusWindowController showShuffleWindow:newShuffleEnabled];
908 [self networkError:localException];
912 - (void)registerNowOK
914 [[StatusWindow sharedWindow] setLocked:NO];
915 [[StatusWindow sharedWindow] vanish:self];
916 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
921 - (void)registerNowCancel
923 [[StatusWindow sharedWindow] setLocked:NO];
924 [[StatusWindow sharedWindow] vanish:self];
925 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
927 [NSApp terminate:self];
930 /*************************************************************************/
932 #pragma mark NETWORK HANDLERS
933 /*************************************************************************/
935 - (void)setServerStatus:(BOOL)newStatus
939 [networkController setServerStatus:YES];
942 [networkController setServerStatus:NO];
946 - (BOOL)connectToServer
948 ITDebugLog(@"Attempting to connect to shared remote.");
950 if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
951 currentRemote = [[[networkController networkObject] remote] retain];
953 //[refreshTimer invalidate];
954 ITDebugLog(@"Connection successful.");
957 ITDebugLog(@"Connection failed.");
958 currentRemote = [remoteArray objectAtIndex:0];
963 - (BOOL)disconnectFromServer
965 ITDebugLog(@"Disconnecting from shared remote.");
967 [currentRemote release];
968 currentRemote = [remoteArray objectAtIndex:0];
969 [networkController disconnect];
974 - (void)checkForRemoteServer:(NSTimer *)timer
976 ITDebugLog(@"Checking for remote server.");
977 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
978 ITDebugLog(@"Remote server found.");
980 [[StatusWindowController sharedController] showReconnectQueryWindow];
982 ITDebugLog(@"Remote server not found.");
986 - (void)networkError:(NSException *)exception
988 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
989 if ([[exception name] isEqualToString:NSPortTimeoutException]) {
990 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);
991 if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
992 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
994 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1001 if (![self connectToServer]) {
1002 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1004 [[StatusWindow sharedWindow] setLocked:NO];
1005 [[StatusWindow sharedWindow] vanish:self];
1006 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1009 - (void)cancelReconnect
1011 [[StatusWindow sharedWindow] setLocked:NO];
1012 [[StatusWindow sharedWindow] vanish:self];
1013 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1016 /*************************************************************************/
1018 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1019 /*************************************************************************/
1021 - (void)applicationLaunched:(NSNotification *)note
1024 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1025 ITDebugLog(@"Remote application launched.");
1026 playerRunningState = ITMTRemotePlayerRunning;
1027 [[self currentRemote] begin];
1028 [self setLatestSongIdentifier:@""];
1030 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1032 selector:@selector(timerUpdate)
1034 repeats:YES] retain];
1035 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1036 [self setupHotKeys];
1039 [self networkError:localException];
1043 - (void)applicationTerminated:(NSNotification *)note
1046 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1047 ITDebugLog(@"Remote application terminated.");
1048 [[self currentRemote] halt];
1049 [refreshTimer invalidate];
1050 [refreshTimer release];
1052 [self clearHotKeys];
1053 playerRunningState = ITMTRemotePlayerNotRunning;
1056 [self networkError:localException];
1061 /*************************************************************************/
1063 #pragma mark NSApplication DELEGATE METHODS
1064 /*************************************************************************/
1066 - (void)applicationWillTerminate:(NSNotification *)note
1068 [self clearHotKeys];
1069 [networkController stopRemoteServerSearch];
1070 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1074 /*************************************************************************/
1076 #pragma mark DEALLOCATION METHOD
1077 /*************************************************************************/
1081 [self applicationTerminated:nil];
1083 [statusItem release];
1084 [statusWindowController release];
1085 [menuController release];
1086 [networkController release];