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] == 0) {
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
745 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
747 [self networkError:localException];
750 ITDebugLog(@"Showing upcoming songs status window.");
753 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
754 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
755 int curTrack = [[self currentRemote] currentSongIndex];
758 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
760 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
764 if ([songList count] == 0) {
765 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
768 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
770 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
773 [self networkError:localException];
777 - (void)incrementVolume
780 float volume = [[self currentRemote] volume];
781 float dispVol = volume;
782 ITDebugLog(@"Incrementing volume.");
791 ITDebugLog(@"Setting volume to %f", volume);
792 [[self currentRemote] setVolume:volume];
794 // Show volume status window
795 [statusWindowController showVolumeWindowWithLevel:dispVol];
797 [self networkError:localException];
801 - (void)decrementVolume
804 float volume = [[self currentRemote] volume];
805 float dispVol = volume;
806 ITDebugLog(@"Decrementing volume.");
815 ITDebugLog(@"Setting volume to %f", volume);
816 [[self currentRemote] setVolume:volume];
818 //Show volume status window
819 [statusWindowController showVolumeWindowWithLevel:dispVol];
821 [self networkError:localException];
825 - (void)incrementRating
828 float rating = [[self currentRemote] currentSongRating];
829 ITDebugLog(@"Incrementing rating.");
831 if ([[self currentRemote] currentPlaylistIndex] == 0) {
832 ITDebugLog(@"No song playing, rating change aborted.");
840 ITDebugLog(@"Setting rating to %f", rating);
841 [[self currentRemote] setCurrentSongRating:rating];
843 //Show rating status window
844 [statusWindowController showRatingWindowWithRating:rating];
846 [self networkError:localException];
850 - (void)decrementRating
853 float rating = [[self currentRemote] currentSongRating];
854 ITDebugLog(@"Decrementing rating.");
856 if ([[self currentRemote] currentPlaylistIndex] == 0) {
857 ITDebugLog(@"No song playing, rating change aborted.");
865 ITDebugLog(@"Setting rating to %f", rating);
866 [[self currentRemote] setCurrentSongRating:rating];
868 //Show rating status window
869 [statusWindowController showRatingWindowWithRating:rating];
871 [self networkError:localException];
878 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
879 ITDebugLog(@"Toggling repeat mode.");
880 switch (repeatMode) {
881 case ITMTRemotePlayerRepeatOff:
882 repeatMode = ITMTRemotePlayerRepeatAll;
884 case ITMTRemotePlayerRepeatAll:
885 repeatMode = ITMTRemotePlayerRepeatOne;
887 case ITMTRemotePlayerRepeatOne:
888 repeatMode = ITMTRemotePlayerRepeatOff;
891 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
892 [[self currentRemote] setRepeatMode:repeatMode];
894 //Show loop status window
895 [statusWindowController showRepeatWindowWithMode:repeatMode];
897 [self networkError:localException];
901 - (void)toggleShuffle
904 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
905 ITDebugLog(@"Toggling shuffle mode.");
906 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
907 //Show shuffle status window
908 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
909 [statusWindowController showShuffleWindow:newShuffleEnabled];
911 [self networkError:localException];
915 - (void)registerNowOK
917 [[StatusWindow sharedWindow] setLocked:NO];
918 [[StatusWindow sharedWindow] vanish:self];
919 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
924 - (void)registerNowCancel
926 [[StatusWindow sharedWindow] setLocked:NO];
927 [[StatusWindow sharedWindow] vanish:self];
928 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
930 [NSApp terminate:self];
933 /*************************************************************************/
935 #pragma mark NETWORK HANDLERS
936 /*************************************************************************/
938 - (void)setServerStatus:(BOOL)newStatus
942 [networkController setServerStatus:YES];
945 [networkController setServerStatus:NO];
949 - (int)connectToServer
952 ITDebugLog(@"Attempting to connect to shared remote.");
953 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
956 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
957 currentRemote = [[[networkController networkObject] remote] retain];
959 ITDebugLog(@"Connection successful.");
961 } else if (result == 0) {
962 ITDebugLog(@"Connection failed.");
963 currentRemote = [remoteArray objectAtIndex:0];
966 //Do something about the password being invalid
967 ITDebugLog(@"Connection failed.");
968 currentRemote = [remoteArray objectAtIndex:0];
973 - (BOOL)disconnectFromServer
975 ITDebugLog(@"Disconnecting from shared remote.");
977 [currentRemote release];
978 currentRemote = [remoteArray objectAtIndex:0];
979 [networkController disconnect];
984 - (void)checkForRemoteServer:(NSTimer *)timer
986 ITDebugLog(@"Checking for remote server.");
987 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
988 ITDebugLog(@"Remote server found.");
990 if (![networkController isConnectedToServer]) {
991 [[StatusWindowController sharedController] showReconnectQueryWindow];
994 ITDebugLog(@"Remote server not found.");
998 - (void)networkError:(NSException *)exception
1000 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1001 NSLog(@"%@", [exception reason]);
1002 if ([[exception name] isEqualToString:NSPortTimeoutException] && [networkController isConnectedToServer]) {
1003 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);
1004 if ([self disconnectFromServer]) {
1005 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1006 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1008 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1015 if ([self connectToServer] == 0) {
1016 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1018 [[StatusWindow sharedWindow] setLocked:NO];
1019 [[StatusWindow sharedWindow] vanish:self];
1020 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1023 - (void)cancelReconnect
1025 [[StatusWindow sharedWindow] setLocked:NO];
1026 [[StatusWindow sharedWindow] vanish:self];
1027 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1030 /*************************************************************************/
1032 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1033 /*************************************************************************/
1035 - (void)applicationLaunched:(NSNotification *)note
1038 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1039 ITDebugLog(@"Remote application launched.");
1040 playerRunningState = ITMTRemotePlayerRunning;
1041 [[self currentRemote] begin];
1042 [self setLatestSongIdentifier:@""];
1044 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1046 selector:@selector(timerUpdate)
1048 repeats:YES] retain];
1049 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1050 [self setupHotKeys];
1053 [self networkError:localException];
1057 - (void)applicationTerminated:(NSNotification *)note
1060 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1061 ITDebugLog(@"Remote application terminated.");
1062 [[self currentRemote] halt];
1063 [refreshTimer invalidate];
1064 [refreshTimer release];
1066 [self clearHotKeys];
1067 playerRunningState = ITMTRemotePlayerNotRunning;
1070 [self networkError:localException];
1075 /*************************************************************************/
1077 #pragma mark NSApplication DELEGATE METHODS
1078 /*************************************************************************/
1080 - (void)applicationWillTerminate:(NSNotification *)note
1082 [networkController stopRemoteServerSearch];
1083 [self clearHotKeys];
1084 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1088 /*************************************************************************/
1090 #pragma mark DEALLOCATION METHOD
1091 /*************************************************************************/
1095 [self applicationTerminated:nil];
1097 [statusItem release];
1098 [statusWindowController release];
1099 [menuController release];
1100 [networkController release];