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];
45 [[PreferencesController sharedPrefs] setController:self];
52 - (void)applicationDidFinishLaunching:(NSNotification *)note
54 //Turn on debug mode if needed
55 if ([df boolForKey:@"ITDebugMode"]) {
59 currentRemote = [self loadRemote];
60 [[self currentRemote] begin];
62 //Turn on network stuff if needed
63 networkController = [[NetworkController alloc] init];
64 if ([df boolForKey:@"enableSharing"]) {
65 [self setServerStatus:YES];
66 } else if ([df boolForKey:@"useSharedPlayer"]) {
67 if (![self connectToServer]) {
68 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
72 //Setup for notification of the remote player launching or quitting
73 [[[NSWorkspace sharedWorkspace] notificationCenter]
75 selector:@selector(applicationTerminated:)
76 name:NSWorkspaceDidTerminateApplicationNotification
79 [[[NSWorkspace sharedWorkspace] notificationCenter]
81 selector:@selector(applicationLaunched:)
82 name:NSWorkspaceDidLaunchApplicationNotification
85 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
86 [[PreferencesController sharedPrefs] registerDefaults];
89 [StatusItemHack install];
90 statusItem = [[ITStatusItem alloc]
91 initWithStatusBar:[NSStatusBar systemStatusBar]
92 withLength:NSSquareStatusItemLength];
94 bling = [[MTBlingController alloc] init];
96 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
98 selector:@selector(blingTime)
100 repeats:YES] retain];
103 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
104 [self applicationLaunched:nil];
106 if ([df boolForKey:@"LaunchPlayerWithMT"])
109 [self applicationTerminated:nil];
112 [self networkError:localException];
115 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
116 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
118 [networkController startRemoteServerSearch];
122 - (ITMTRemote *)loadRemote
124 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
125 ITDebugLog(@"Gathering remotes.");
127 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
128 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
129 NSString *bundlePath;
131 while ( (bundlePath = [enumerator nextObject]) ) {
132 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
135 Class remoteClass = [remoteBundle principalClass];
137 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
138 [remoteClass isKindOfClass:[NSObject class]]) {
139 id remote = [remoteClass remote];
140 ITDebugLog(@"Adding remote at path %@", bundlePath);
141 [remoteArray addObject:remote];
146 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
147 // if ( [remoteArray count] > 1 ) {
148 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
150 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
153 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
154 return [remoteArray objectAtIndex:0];
157 /*************************************************************************/
159 #pragma mark INSTANCE METHODS
160 /*************************************************************************/
162 /*- (void)startTimerInNewThread
164 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
165 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
166 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
168 selector:@selector(timerUpdate)
170 repeats:YES] retain];
172 ITDebugLog(@"Timer started.");
176 - (void)setBlingTime:(NSDate*)date
178 NSMutableDictionary *globalPrefs;
180 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
182 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
184 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
186 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
188 [globalPrefs release];
191 - (NSDate*)getBlingTime
194 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
199 NSDate *now = [NSDate date];
200 if (![self blingBling]) {
201 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
202 [self setBlingTime:now];
204 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
206 [statusItem setEnabled:NO];
208 if ([refreshTimer isValid]) {
209 [refreshTimer invalidate];
211 [statusWindowController showRegistrationQueryWindow];
215 [statusItem setEnabled:YES];
217 if (![refreshTimer isValid]) {
218 [refreshTimer release];
219 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
221 selector:@selector(timerUpdate)
223 repeats:YES] retain];
227 [self setBlingTime:nil];
238 if ( ! ([bling checkDone] == 2475) ) {
245 - (BOOL)songIsPlaying
247 NSString *identifier;
249 identifier = [[self currentRemote] playerStateUniqueIdentifier];
251 [self networkError:localException];
253 return ( ! ([identifier isEqualToString:@"0-0"]) );
256 - (BOOL)radioIsPlaying
258 ITMTRemotePlayerPlaylistClass class;
260 class = [[self currentRemote] currentPlaylistClass];
262 [self networkError:localException];
264 return (class == ITMTRemotePlayerRadioPlaylist );
269 NSString *identifier;
271 identifier = [[self currentRemote] playerStateUniqueIdentifier];
273 [self networkError:localException];
275 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
278 - (NSString *)latestSongIdentifier
280 return _latestSongIdentifier;
283 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
285 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
286 [_latestSongIdentifier autorelease];
287 _latestSongIdentifier = [newIdentifier copy];
292 if ([networkController isConnectedToServer]) {
293 [statusItem setMenu:[menuController menu]];
296 if ( [self songChanged] && (timerUpdating != YES) ) {
297 ITDebugLog(@"The song changed.");
301 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
302 [menuController rebuildSubmenus];
304 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
305 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
308 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
310 [self networkError:localException];
319 ITDebugLog(@"Menu clicked.");
320 if ([networkController isConnectedToServer]) {
321 //Used the cached version
326 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
327 [statusItem setMenu:[menuController menu]];
329 [statusItem setMenu:[menuController menuForNoPlayer]];
332 [self networkError:localException];
345 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
346 ITDebugLog(@"Play/Pause toggled");
347 if (state == ITMTRemotePlayerPlaying) {
348 [[self currentRemote] pause];
349 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
350 [[self currentRemote] pause];
351 [[self currentRemote] play];
353 [[self currentRemote] play];
356 [self networkError:localException];
364 ITDebugLog(@"Going to next song.");
366 [[self currentRemote] goToNextSong];
368 [self networkError:localException];
375 ITDebugLog(@"Going to previous song.");
377 [[self currentRemote] goToPreviousSong];
379 [self networkError:localException];
386 ITDebugLog(@"Fast forwarding.");
388 [[self currentRemote] forward];
390 [self networkError:localException];
397 ITDebugLog(@"Rewinding.");
399 [[self currentRemote] rewind];
401 [self networkError:localException];
406 - (void)selectPlaylistAtIndex:(int)index
408 ITDebugLog(@"Selecting playlist %i", index);
410 [[self currentRemote] switchToPlaylistAtIndex:index];
412 [self networkError:localException];
417 - (void)selectSongAtIndex:(int)index
419 ITDebugLog(@"Selecting song %i", index);
421 [[self currentRemote] switchToSongAtIndex:index];
423 [self networkError:localException];
428 - (void)selectSongRating:(int)rating
430 ITDebugLog(@"Selecting song rating %i", rating);
432 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
434 [self networkError:localException];
439 - (void)selectEQPresetAtIndex:(int)index
441 ITDebugLog(@"Selecting EQ preset %i", index);
443 [[self currentRemote] switchToEQAtIndex:index];
445 [self networkError:localException];
452 ITDebugLog(@"Beginning show player.");
453 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
454 ITDebugLog(@"Showing player interface.");
456 [[self currentRemote] showPrimaryInterface];
458 [self networkError:localException];
461 ITDebugLog(@"Launching player.");
463 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
464 ITDebugLog(@"Error Launching Player");
467 [self networkError:localException];
470 ITDebugLog(@"Finished show player.");
473 - (void)showPreferences
475 ITDebugLog(@"Show preferences.");
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
949 ITDebugLog(@"Attempting to connect to shared remote.");
950 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
953 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
954 currentRemote = [[[networkController networkObject] remote] retain];
956 ITDebugLog(@"Connection successful.");
958 } else if (result == 0) {
959 ITDebugLog(@"Connection failed.");
960 currentRemote = [remoteArray objectAtIndex:0];
963 //Do something about the password being invalid
964 ITDebugLog(@"Connection failed.");
965 currentRemote = [remoteArray objectAtIndex:0];
970 - (BOOL)disconnectFromServer
972 ITDebugLog(@"Disconnecting from shared remote.");
974 [currentRemote release];
975 currentRemote = [remoteArray objectAtIndex:0];
976 [networkController disconnect];
981 - (void)checkForRemoteServer:(NSTimer *)timer
983 ITDebugLog(@"Checking for remote server.");
984 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
985 ITDebugLog(@"Remote server found.");
987 if (![networkController isConnectedToServer]) {
988 [[StatusWindowController sharedController] showReconnectQueryWindow];
991 ITDebugLog(@"Remote server not found.");
995 - (void)networkError:(NSException *)exception
997 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
998 NSLog(@"%@", [exception reason]);
999 if ([[exception name] isEqualToString:NSPortTimeoutException] && [networkController isConnectedToServer]) {
1000 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);
1001 if ([self disconnectFromServer]) {
1002 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1003 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1005 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1012 if (![self connectToServer]) {
1013 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1015 [[StatusWindow sharedWindow] setLocked:NO];
1016 [[StatusWindow sharedWindow] vanish:self];
1017 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1020 - (void)cancelReconnect
1022 [[StatusWindow sharedWindow] setLocked:NO];
1023 [[StatusWindow sharedWindow] vanish:self];
1024 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1027 /*************************************************************************/
1029 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1030 /*************************************************************************/
1032 - (void)applicationLaunched:(NSNotification *)note
1035 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1036 ITDebugLog(@"Remote application launched.");
1037 playerRunningState = ITMTRemotePlayerRunning;
1038 [[self currentRemote] begin];
1039 [self setLatestSongIdentifier:@""];
1041 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1043 selector:@selector(timerUpdate)
1045 repeats:YES] retain];
1046 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1047 [self setupHotKeys];
1050 [self networkError:localException];
1054 - (void)applicationTerminated:(NSNotification *)note
1057 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1058 ITDebugLog(@"Remote application terminated.");
1059 [[self currentRemote] halt];
1060 [refreshTimer invalidate];
1061 [refreshTimer release];
1063 [self clearHotKeys];
1064 playerRunningState = ITMTRemotePlayerNotRunning;
1067 [self networkError:localException];
1072 /*************************************************************************/
1074 #pragma mark NSApplication DELEGATE METHODS
1075 /*************************************************************************/
1077 - (void)applicationWillTerminate:(NSNotification *)note
1079 [networkController stopRemoteServerSearch];
1080 [self clearHotKeys];
1081 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1085 /*************************************************************************/
1087 #pragma mark DEALLOCATION METHOD
1088 /*************************************************************************/
1092 [self applicationTerminated:nil];
1094 [statusItem release];
1095 [statusWindowController release];
1096 [menuController release];
1097 [networkController release];