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"]) {
65 if (![self connectToServer]) {
66 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
70 //Setup for notification of the remote player launching or quitting
71 [[[NSWorkspace sharedWorkspace] notificationCenter]
73 selector:@selector(applicationTerminated:)
74 name:NSWorkspaceDidTerminateApplicationNotification
77 [[[NSWorkspace sharedWorkspace] notificationCenter]
79 selector:@selector(applicationLaunched:)
80 name:NSWorkspaceDidLaunchApplicationNotification
83 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
84 [[PreferencesController sharedPrefs] registerDefaults];
87 [StatusItemHack install];
88 statusItem = [[ITStatusItem alloc]
89 initWithStatusBar:[NSStatusBar systemStatusBar]
90 withLength:NSSquareStatusItemLength];
92 bling = [[MTBlingController alloc] init];
94 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
96 selector:@selector(blingTime)
101 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
102 [self applicationLaunched:nil];
104 if ([df boolForKey:@"LaunchPlayerWithMT"])
107 [self applicationTerminated:nil];
110 [self networkError:localException];
113 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
114 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
116 [networkController startRemoteServerSearch];
120 - (ITMTRemote *)loadRemote
122 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
123 ITDebugLog(@"Gathering remotes.");
125 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
126 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
127 NSString *bundlePath;
129 while ( (bundlePath = [enumerator nextObject]) ) {
130 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
133 Class remoteClass = [remoteBundle principalClass];
135 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
136 [remoteClass isKindOfClass:[NSObject class]]) {
137 id remote = [remoteClass remote];
138 ITDebugLog(@"Adding remote at path %@", bundlePath);
139 [remoteArray addObject:remote];
144 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
145 // if ( [remoteArray count] > 1 ) {
146 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
148 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
151 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
152 return [remoteArray objectAtIndex:0];
155 /*************************************************************************/
157 #pragma mark INSTANCE METHODS
158 /*************************************************************************/
160 /*- (void)startTimerInNewThread
162 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
163 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
164 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
166 selector:@selector(timerUpdate)
168 repeats:YES] retain];
170 ITDebugLog(@"Timer started.");
174 - (void)setBlingTime:(NSDate*)date
176 NSMutableDictionary *globalPrefs;
178 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
180 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
182 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
184 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
186 [globalPrefs release];
189 - (NSDate*)getBlingTime
192 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
197 NSDate *now = [NSDate date];
198 if (![self blingBling]) {
199 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
200 [self setBlingTime:now];
202 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
204 [statusItem setEnabled:NO];
206 if ([refreshTimer isValid]) {
207 [refreshTimer invalidate];
209 [statusWindowController showRegistrationQueryWindow];
213 [statusItem setEnabled:YES];
215 if (![refreshTimer isValid]) {
216 [refreshTimer release];
217 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
219 selector:@selector(timerUpdate)
221 repeats:YES] retain];
225 [self setBlingTime:nil];
236 if ( ! ([bling checkDone] == 2475) ) {
243 - (BOOL)songIsPlaying
245 NSString *identifier;
247 identifier = [[self currentRemote] playerStateUniqueIdentifier];
249 [self networkError:localException];
251 return ( ! ([identifier isEqualToString:@"0-0"]) );
254 - (BOOL)radioIsPlaying
256 ITMTRemotePlayerPlaylistClass class;
258 class = [[self currentRemote] currentPlaylistClass];
260 [self networkError:localException];
262 return (class == ITMTRemotePlayerRadioPlaylist );
267 NSString *identifier;
269 identifier = [[self currentRemote] playerStateUniqueIdentifier];
271 [self networkError:localException];
273 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
276 - (NSString *)latestSongIdentifier
278 return _latestSongIdentifier;
281 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
283 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
284 [_latestSongIdentifier autorelease];
285 _latestSongIdentifier = [newIdentifier copy];
290 if ([networkController isConnectedToServer]) {
291 [statusItem setMenu:[menuController menu]];
294 if ( [self songChanged] && (timerUpdating != YES) ) {
295 ITDebugLog(@"The song changed.");
299 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
300 [menuController rebuildSubmenus];
302 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
303 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
306 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
308 [self networkError:localException];
317 ITDebugLog(@"Menu clicked.");
318 if ([networkController isConnectedToServer]) {
319 //Used the cached version
324 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
325 [statusItem setMenu:[menuController menu]];
327 [statusItem setMenu:[menuController menuForNoPlayer]];
330 [self networkError:localException];
343 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
344 ITDebugLog(@"Play/Pause toggled");
345 if (state == ITMTRemotePlayerPlaying) {
346 [[self currentRemote] pause];
347 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
348 [[self currentRemote] pause];
349 [[self currentRemote] play];
351 [[self currentRemote] play];
354 [self networkError:localException];
362 ITDebugLog(@"Going to next song.");
364 [[self currentRemote] goToNextSong];
366 [self networkError:localException];
373 ITDebugLog(@"Going to previous song.");
375 [[self currentRemote] goToPreviousSong];
377 [self networkError:localException];
384 ITDebugLog(@"Fast forwarding.");
386 [[self currentRemote] forward];
388 [self networkError:localException];
395 ITDebugLog(@"Rewinding.");
397 [[self currentRemote] rewind];
399 [self networkError:localException];
404 - (void)selectPlaylistAtIndex:(int)index
406 ITDebugLog(@"Selecting playlist %i", index);
408 [[self currentRemote] switchToPlaylistAtIndex:index];
410 [self networkError:localException];
415 - (void)selectSongAtIndex:(int)index
417 ITDebugLog(@"Selecting song %i", index);
419 [[self currentRemote] switchToSongAtIndex:index];
421 [self networkError:localException];
426 - (void)selectSongRating:(int)rating
428 ITDebugLog(@"Selecting song rating %i", rating);
430 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
432 [self networkError:localException];
437 - (void)selectEQPresetAtIndex:(int)index
439 ITDebugLog(@"Selecting EQ preset %i", index);
441 [[self currentRemote] switchToEQAtIndex:index];
443 [self networkError:localException];
450 ITDebugLog(@"Beginning show player.");
451 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
452 ITDebugLog(@"Showing player interface.");
454 [[self currentRemote] showPrimaryInterface];
456 [self networkError:localException];
459 ITDebugLog(@"Launching player.");
461 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
462 ITDebugLog(@"Error Launching Player");
465 [self networkError:localException];
468 ITDebugLog(@"Finished show player.");
471 - (void)showPreferences
473 ITDebugLog(@"Show preferences.");
474 [[PreferencesController sharedPrefs] setController:self];
475 [[PreferencesController sharedPrefs] showPrefsWindow:self];
478 - (void)quitMenuTunes
480 ITDebugLog(@"Quitting MenuTunes.");
481 [NSApp terminate:self];
487 - (void)closePreferences
489 ITDebugLog(@"Preferences closed.");
490 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
495 - (ITMTRemote *)currentRemote
497 return currentRemote;
508 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
509 ITHotKey *nextHotKey;
510 ITDebugLog(@"Clearing hot keys.");
511 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
512 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
514 ITDebugLog(@"Done clearing hot keys.");
520 ITDebugLog(@"Setting up hot keys.");
522 if (playerRunningState == ITMTRemotePlayerNotRunning) {
526 if ([df objectForKey:@"PlayPause"] != nil) {
527 ITDebugLog(@"Setting up play pause hot key.");
528 hotKey = [[ITHotKey alloc] init];
529 [hotKey setName:@"PlayPause"];
530 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
531 [hotKey setTarget:self];
532 [hotKey setAction:@selector(playPause)];
533 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
536 if ([df objectForKey:@"NextTrack"] != nil) {
537 ITDebugLog(@"Setting up next track hot key.");
538 hotKey = [[ITHotKey alloc] init];
539 [hotKey setName:@"NextTrack"];
540 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
541 [hotKey setTarget:self];
542 [hotKey setAction:@selector(nextSong)];
543 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
546 if ([df objectForKey:@"PrevTrack"] != nil) {
547 ITDebugLog(@"Setting up previous track hot key.");
548 hotKey = [[ITHotKey alloc] init];
549 [hotKey setName:@"PrevTrack"];
550 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
551 [hotKey setTarget:self];
552 [hotKey setAction:@selector(prevSong)];
553 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
556 if ([df objectForKey:@"ShowPlayer"] != nil) {
557 ITDebugLog(@"Setting up show player hot key.");
558 hotKey = [[ITHotKey alloc] init];
559 [hotKey setName:@"ShowPlayer"];
560 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
561 [hotKey setTarget:self];
562 [hotKey setAction:@selector(showPlayer)];
563 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
566 if ([df objectForKey:@"TrackInfo"] != nil) {
567 ITDebugLog(@"Setting up track info hot key.");
568 hotKey = [[ITHotKey alloc] init];
569 [hotKey setName:@"TrackInfo"];
570 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
571 [hotKey setTarget:self];
572 [hotKey setAction:@selector(showCurrentTrackInfo)];
573 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
576 if ([df objectForKey:@"UpcomingSongs"] != nil) {
577 ITDebugLog(@"Setting up upcoming songs hot key.");
578 hotKey = [[ITHotKey alloc] init];
579 [hotKey setName:@"UpcomingSongs"];
580 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
581 [hotKey setTarget:self];
582 [hotKey setAction:@selector(showUpcomingSongs)];
583 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
586 if ([df objectForKey:@"ToggleLoop"] != nil) {
587 ITDebugLog(@"Setting up toggle loop hot key.");
588 hotKey = [[ITHotKey alloc] init];
589 [hotKey setName:@"ToggleLoop"];
590 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
591 [hotKey setTarget:self];
592 [hotKey setAction:@selector(toggleLoop)];
593 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
596 if ([df objectForKey:@"ToggleShuffle"] != nil) {
597 ITDebugLog(@"Setting up toggle shuffle hot key.");
598 hotKey = [[ITHotKey alloc] init];
599 [hotKey setName:@"ToggleShuffle"];
600 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
601 [hotKey setTarget:self];
602 [hotKey setAction:@selector(toggleShuffle)];
603 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
606 if ([df objectForKey:@"IncrementVolume"] != nil) {
607 ITDebugLog(@"Setting up increment volume hot key.");
608 hotKey = [[ITHotKey alloc] init];
609 [hotKey setName:@"IncrementVolume"];
610 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
611 [hotKey setTarget:self];
612 [hotKey setAction:@selector(incrementVolume)];
613 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
616 if ([df objectForKey:@"DecrementVolume"] != nil) {
617 ITDebugLog(@"Setting up decrement volume hot key.");
618 hotKey = [[ITHotKey alloc] init];
619 [hotKey setName:@"DecrementVolume"];
620 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
621 [hotKey setTarget:self];
622 [hotKey setAction:@selector(decrementVolume)];
623 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
626 if ([df objectForKey:@"IncrementRating"] != nil) {
627 ITDebugLog(@"Setting up increment rating hot key.");
628 hotKey = [[ITHotKey alloc] init];
629 [hotKey setName:@"IncrementRating"];
630 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
631 [hotKey setTarget:self];
632 [hotKey setAction:@selector(incrementRating)];
633 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
636 if ([df objectForKey:@"DecrementRating"] != nil) {
637 ITDebugLog(@"Setting up decrement rating hot key.");
638 hotKey = [[ITHotKey alloc] init];
639 [hotKey setName:@"DecrementRating"];
640 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
641 [hotKey setTarget:self];
642 [hotKey setAction:@selector(decrementRating)];
643 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
645 ITDebugLog(@"Finished setting up hot keys.");
648 - (void)showCurrentTrackInfo
650 ITMTRemotePlayerSource source;
652 NSString *album = nil;
653 NSString *artist = nil;
654 NSString *time = nil;
655 NSString *track = nil;
659 source = [[self currentRemote] currentSource];
660 title = [[self currentRemote] currentSongTitle];
662 [self networkError:localException];
665 ITDebugLog(@"Showing track info status window.");
669 if ( [df boolForKey:@"showAlbum"] ) {
671 album = [[self currentRemote] currentSongAlbum];
673 [self networkError:localException];
677 if ( [df boolForKey:@"showArtist"] ) {
679 artist = [[self currentRemote] currentSongArtist];
681 [self networkError:localException];
685 if ( [df boolForKey:@"showTime"] ) {
687 time = [NSString stringWithFormat:@"%@: %@ / %@",
689 [[self currentRemote] currentSongElapsed],
690 [[self currentRemote] currentSongLength]];
692 [self networkError:localException];
696 if ( [df boolForKey:@"showTrackNumber"] ) {
701 trackNo = [[self currentRemote] currentSongTrack];
702 trackCount = [[self currentRemote] currentAlbumTrackCount];
704 [self networkError:localException];
707 if ( (trackNo > 0) || (trackCount > 0) ) {
708 track = [NSString stringWithFormat:@"%@: %i %@ %i",
709 @"Track", trackNo, @"of", trackCount];
713 if ( [df boolForKey:@"showTrackRating"] ) {
717 currentRating = [[self currentRemote] currentSongRating];
719 [self networkError:localException];
722 if (currentRating >= 0.0) {
723 rating = ( currentRating * 5 );
728 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
731 [statusWindowController showSongInfoWindowWithSource:source
740 - (void)showUpcomingSongs
745 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
747 [self networkError:localException];
750 ITDebugLog(@"Showing upcoming songs status window.");
753 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
754 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
755 int curTrack = [[self currentRemote] currentSongIndex];
758 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
760 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
764 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
766 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
769 [self networkError:localException];
773 - (void)incrementVolume
776 float volume = [[self currentRemote] volume];
777 float dispVol = volume;
778 ITDebugLog(@"Incrementing volume.");
787 ITDebugLog(@"Setting volume to %f", volume);
788 [[self currentRemote] setVolume:volume];
790 // Show volume status window
791 [statusWindowController showVolumeWindowWithLevel:dispVol];
793 [self networkError:localException];
797 - (void)decrementVolume
800 float volume = [[self currentRemote] volume];
801 float dispVol = volume;
802 ITDebugLog(@"Decrementing volume.");
811 ITDebugLog(@"Setting volume to %f", volume);
812 [[self currentRemote] setVolume:volume];
814 //Show volume status window
815 [statusWindowController showVolumeWindowWithLevel:dispVol];
817 [self networkError:localException];
821 - (void)incrementRating
824 float rating = [[self currentRemote] currentSongRating];
825 ITDebugLog(@"Incrementing rating.");
827 if ([[self currentRemote] currentPlaylistIndex] == 0) {
828 ITDebugLog(@"No song playing, rating change aborted.");
836 ITDebugLog(@"Setting rating to %f", rating);
837 [[self currentRemote] setCurrentSongRating:rating];
839 //Show rating status window
840 [statusWindowController showRatingWindowWithRating:rating];
842 [self networkError:localException];
846 - (void)decrementRating
849 float rating = [[self currentRemote] currentSongRating];
850 ITDebugLog(@"Decrementing rating.");
852 if ([[self currentRemote] currentPlaylistIndex] == 0) {
853 ITDebugLog(@"No song playing, rating change aborted.");
861 ITDebugLog(@"Setting rating to %f", rating);
862 [[self currentRemote] setCurrentSongRating:rating];
864 //Show rating status window
865 [statusWindowController showRatingWindowWithRating:rating];
867 [self networkError:localException];
874 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
875 ITDebugLog(@"Toggling repeat mode.");
876 switch (repeatMode) {
877 case ITMTRemotePlayerRepeatOff:
878 repeatMode = ITMTRemotePlayerRepeatAll;
880 case ITMTRemotePlayerRepeatAll:
881 repeatMode = ITMTRemotePlayerRepeatOne;
883 case ITMTRemotePlayerRepeatOne:
884 repeatMode = ITMTRemotePlayerRepeatOff;
887 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
888 [[self currentRemote] setRepeatMode:repeatMode];
890 //Show loop status window
891 [statusWindowController showRepeatWindowWithMode:repeatMode];
893 [self networkError:localException];
897 - (void)toggleShuffle
900 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
901 ITDebugLog(@"Toggling shuffle mode.");
902 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
903 //Show shuffle status window
904 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
905 [statusWindowController showShuffleWindow:newShuffleEnabled];
907 [self networkError:localException];
911 - (void)registerNowOK
913 [[StatusWindow sharedWindow] setLocked:NO];
914 [[StatusWindow sharedWindow] vanish:self];
915 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
920 - (void)registerNowCancel
922 [[StatusWindow sharedWindow] setLocked:NO];
923 [[StatusWindow sharedWindow] vanish:self];
924 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
926 [NSApp terminate:self];
929 /*************************************************************************/
931 #pragma mark NETWORK HANDLERS
932 /*************************************************************************/
934 - (void)setServerStatus:(BOOL)newStatus
938 [networkController setServerStatus:YES];
941 [networkController setServerStatus:NO];
945 - (BOOL)connectToServer
947 ITDebugLog(@"Attempting to connect to shared remote.");
949 if ([networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]]) {
950 currentRemote = [[networkController networkObject] remote];
951 [refreshTimer invalidate];
952 ITDebugLog(@"Connection successful.");
955 ITDebugLog(@"Connection failed.");
956 currentRemote = [remoteArray objectAtIndex:0];
961 - (BOOL)disconnectFromServer
963 ITDebugLog(@"Disconnecting from shared remote.");
965 currentRemote = [remoteArray objectAtIndex:0];
966 [networkController disconnect];
971 - (void)checkForRemoteServer:(NSTimer *)timer
973 ITDebugLog(@"Checking for remote server.");
974 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
975 ITDebugLog(@"Remote server found.");
977 [[StatusWindowController sharedController] showReconnectQueryWindow];
979 ITDebugLog(@"Remote server not found.");
983 - (void)networkError:(NSException *)exception
985 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
986 NSLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
987 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);
988 if ([networkController isConnectedToServer] && [self disconnectFromServer]) {
989 if ([[exception name] isEqualToString:NSPortTimeoutException]) {
990 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
993 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
999 if (![self connectToServer]) {
1000 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1002 [[StatusWindow sharedWindow] setLocked:NO];
1003 [[StatusWindow sharedWindow] vanish:self];
1004 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1007 - (void)cancelReconnect
1009 [[StatusWindow sharedWindow] setLocked:NO];
1010 [[StatusWindow sharedWindow] vanish:self];
1011 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1014 /*************************************************************************/
1016 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1017 /*************************************************************************/
1019 - (void)applicationLaunched:(NSNotification *)note
1022 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1023 ITDebugLog(@"Remote application launched.");
1024 playerRunningState = ITMTRemotePlayerRunning;
1025 [[self currentRemote] begin];
1026 [self setLatestSongIdentifier:@""];
1028 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1030 selector:@selector(timerUpdate)
1032 repeats:YES] retain];
1033 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1034 [self setupHotKeys];
1037 [self networkError:localException];
1041 - (void)applicationTerminated:(NSNotification *)note
1044 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1045 ITDebugLog(@"Remote application terminated.");
1046 [[self currentRemote] halt];
1047 [refreshTimer invalidate];
1048 [refreshTimer release];
1050 [self clearHotKeys];
1051 playerRunningState = ITMTRemotePlayerNotRunning;
1054 [self networkError:localException];
1059 /*************************************************************************/
1061 #pragma mark NSApplication DELEGATE METHODS
1062 /*************************************************************************/
1064 - (void)applicationWillTerminate:(NSNotification *)note
1066 [self clearHotKeys];
1067 [networkController stopRemoteServerSearch];
1068 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1072 /*************************************************************************/
1074 #pragma mark DEALLOCATION METHOD
1075 /*************************************************************************/
1079 [self applicationTerminated:nil];
1081 [statusItem release];
1082 [statusWindowController release];
1083 [menuController release];
1084 [networkController release];