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)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 [[PreferencesController sharedPrefs] setController:self];
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 if (![df stringForKey:@"appVersion"]) {
59 [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
60 [df setObject:@"1.2" forKey:@"appVersion"];
61 [[StatusWindowController sharedController] showPreferencesUpdateWindow];
64 currentRemote = [self loadRemote];
65 [[self currentRemote] begin];
67 //Turn on network stuff if needed
68 networkController = [[NetworkController alloc] init];
69 if ([df boolForKey:@"enableSharing"]) {
70 [self setServerStatus:YES];
71 } else if ([df boolForKey:@"useSharedPlayer"]) {
72 if ([self connectToServer] == 0) {
73 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
77 //Setup for notification of the remote player launching or quitting
78 [[[NSWorkspace sharedWorkspace] notificationCenter]
80 selector:@selector(applicationTerminated:)
81 name:NSWorkspaceDidTerminateApplicationNotification
84 [[[NSWorkspace sharedWorkspace] notificationCenter]
86 selector:@selector(applicationLaunched:)
87 name:NSWorkspaceDidLaunchApplicationNotification
90 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
91 [[PreferencesController sharedPrefs] registerDefaults];
94 [StatusItemHack install];
95 statusItem = [[ITStatusItem alloc]
96 initWithStatusBar:[NSStatusBar systemStatusBar]
97 withLength:NSSquareStatusItemLength];
99 bling = [[MTBlingController alloc] init];
101 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
103 selector:@selector(blingTime)
105 repeats:YES] retain];
108 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
109 [self applicationLaunched:nil];
111 if ([df boolForKey:@"LaunchPlayerWithMT"])
114 [self applicationTerminated:nil];
117 [self networkError:localException];
120 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
121 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
123 [networkController startRemoteServerSearch];
127 - (ITMTRemote *)loadRemote
129 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
130 ITDebugLog(@"Gathering remotes.");
132 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
133 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
134 NSString *bundlePath;
136 while ( (bundlePath = [enumerator nextObject]) ) {
137 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
140 Class remoteClass = [remoteBundle principalClass];
142 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
143 [remoteClass isKindOfClass:[NSObject class]]) {
144 id remote = [remoteClass remote];
145 ITDebugLog(@"Adding remote at path %@", bundlePath);
146 [remoteArray addObject:remote];
151 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
152 // if ( [remoteArray count] > 1 ) {
153 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
155 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
158 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
159 return [remoteArray objectAtIndex:0];
162 /*************************************************************************/
164 #pragma mark INSTANCE METHODS
165 /*************************************************************************/
167 /*- (void)startTimerInNewThread
169 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
170 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
171 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
173 selector:@selector(timerUpdate)
175 repeats:YES] retain];
177 ITDebugLog(@"Timer started.");
181 - (void)setBlingTime:(NSDate*)date
183 NSMutableDictionary *globalPrefs;
185 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
187 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
189 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
191 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
193 [globalPrefs release];
196 - (NSDate*)getBlingTime
199 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
204 NSDate *now = [NSDate date];
205 if (![self blingBling]) {
206 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
207 [self setBlingTime:now];
209 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
211 [statusItem setEnabled:NO];
213 if ([refreshTimer isValid]) {
214 [refreshTimer invalidate];
216 [statusWindowController showRegistrationQueryWindow];
220 [statusItem setEnabled:YES];
222 if (![refreshTimer isValid]) {
223 [refreshTimer release];
224 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
226 selector:@selector(timerUpdate)
228 repeats:YES] retain];
232 [self setBlingTime:nil];
243 if ( ! ([bling checkDone] == 2475) ) {
250 - (BOOL)songIsPlaying
252 NSString *identifier = nil;
254 identifier = [[self currentRemote] playerStateUniqueIdentifier];
256 [self networkError:localException];
258 return ( ! ([identifier isEqualToString:@"0-0"]) );
261 - (BOOL)radioIsPlaying
263 ITMTRemotePlayerPlaylistClass class = nil;
265 class = [[self currentRemote] currentPlaylistClass];
267 [self networkError:localException];
269 return (class == ITMTRemotePlayerRadioPlaylist );
274 NSString *identifier = nil;
276 identifier = [[self currentRemote] playerStateUniqueIdentifier];
278 [self networkError:localException];
280 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
283 - (NSString *)latestSongIdentifier
285 return _latestSongIdentifier;
288 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
290 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
291 [_latestSongIdentifier autorelease];
292 _latestSongIdentifier = [newIdentifier copy];
297 if ([networkController isConnectedToServer]) {
298 [statusItem setMenu:[menuController menu]];
301 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
302 ITDebugLog(@"The song changed.");
306 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
307 [menuController rebuildSubmenus];
309 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
310 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
313 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
315 [self networkError:localException];
324 ITDebugLog(@"Menu clicked.");
325 if ([networkController isConnectedToServer]) {
326 //Used the cached version
331 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
332 [statusItem setMenu:[menuController menu]];
334 [statusItem setMenu:[menuController menuForNoPlayer]];
337 [self networkError:localException];
350 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
351 ITDebugLog(@"Play/Pause toggled");
352 if (state == ITMTRemotePlayerPlaying) {
353 [[self currentRemote] pause];
354 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
355 [[self currentRemote] pause];
356 [[self currentRemote] play];
358 [[self currentRemote] play];
361 [self networkError:localException];
369 ITDebugLog(@"Going to next song.");
371 [[self currentRemote] goToNextSong];
373 [self networkError:localException];
380 ITDebugLog(@"Going to previous song.");
382 [[self currentRemote] goToPreviousSong];
384 [self networkError:localException];
391 ITDebugLog(@"Fast forwarding.");
393 [[self currentRemote] forward];
395 [self networkError:localException];
402 ITDebugLog(@"Rewinding.");
404 [[self currentRemote] rewind];
406 [self networkError:localException];
411 - (void)selectPlaylistAtIndex:(int)index
413 ITDebugLog(@"Selecting playlist %i", index);
415 //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
416 [[self currentRemote] switchToPlaylistAtIndex:index];
418 [self networkError:localException];
423 - (void)selectSongAtIndex:(int)index
425 ITDebugLog(@"Selecting song %i", index);
427 [[self currentRemote] switchToSongAtIndex:index];
429 [self networkError:localException];
434 - (void)selectSongRating:(int)rating
436 ITDebugLog(@"Selecting song rating %i", rating);
438 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
440 [self networkError:localException];
445 - (void)selectEQPresetAtIndex:(int)index
447 ITDebugLog(@"Selecting EQ preset %i", index);
449 [[self currentRemote] switchToEQAtIndex:index];
451 [self networkError:localException];
458 ITDebugLog(@"Beginning show player.");
459 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
460 ITDebugLog(@"Showing player interface.");
462 [[self currentRemote] showPrimaryInterface];
464 [self networkError:localException];
467 ITDebugLog(@"Launching player.");
469 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
470 ITDebugLog(@"Error Launching Player");
473 [self networkError:localException];
476 ITDebugLog(@"Finished show player.");
479 - (void)showPreferences
481 ITDebugLog(@"Show preferences.");
482 [[PreferencesController sharedPrefs] showPrefsWindow:self];
485 - (void)showPreferencesAndClose
487 ITDebugLog(@"Show preferences.");
488 [[PreferencesController sharedPrefs] showPrefsWindow:self];
489 [[StatusWindow sharedWindow] setLocked:NO];
490 [[StatusWindow sharedWindow] vanish:self];
491 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
494 - (void)showTestWindow
496 [self showCurrentTrackInfo];
499 - (void)quitMenuTunes
501 ITDebugLog(@"Quitting MenuTunes.");
502 [NSApp terminate:self];
508 - (void)closePreferences
510 ITDebugLog(@"Preferences closed.");
511 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
516 - (ITMTRemote *)currentRemote
518 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
519 [self networkError:nil];
522 return currentRemote;
533 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
534 ITHotKey *nextHotKey;
535 ITDebugLog(@"Clearing hot keys.");
536 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
537 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
539 ITDebugLog(@"Done clearing hot keys.");
545 ITDebugLog(@"Setting up hot keys.");
547 if (playerRunningState == ITMTRemotePlayerNotRunning) {
551 if ([df objectForKey:@"PlayPause"] != nil) {
552 ITDebugLog(@"Setting up play pause hot key.");
553 hotKey = [[ITHotKey alloc] init];
554 [hotKey setName:@"PlayPause"];
555 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
556 [hotKey setTarget:self];
557 [hotKey setAction:@selector(playPause)];
558 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
561 if ([df objectForKey:@"NextTrack"] != nil) {
562 ITDebugLog(@"Setting up next track hot key.");
563 hotKey = [[ITHotKey alloc] init];
564 [hotKey setName:@"NextTrack"];
565 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
566 [hotKey setTarget:self];
567 [hotKey setAction:@selector(nextSong)];
568 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
571 if ([df objectForKey:@"PrevTrack"] != nil) {
572 ITDebugLog(@"Setting up previous track hot key.");
573 hotKey = [[ITHotKey alloc] init];
574 [hotKey setName:@"PrevTrack"];
575 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
576 [hotKey setTarget:self];
577 [hotKey setAction:@selector(prevSong)];
578 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
581 if ([df objectForKey:@"ShowPlayer"] != nil) {
582 ITDebugLog(@"Setting up show player hot key.");
583 hotKey = [[ITHotKey alloc] init];
584 [hotKey setName:@"ShowPlayer"];
585 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
586 [hotKey setTarget:self];
587 [hotKey setAction:@selector(showPlayer)];
588 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
591 if ([df objectForKey:@"TrackInfo"] != nil) {
592 ITDebugLog(@"Setting up track info hot key.");
593 hotKey = [[ITHotKey alloc] init];
594 [hotKey setName:@"TrackInfo"];
595 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
596 [hotKey setTarget:self];
597 [hotKey setAction:@selector(showCurrentTrackInfo)];
598 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
601 if ([df objectForKey:@"UpcomingSongs"] != nil) {
602 ITDebugLog(@"Setting up upcoming songs hot key.");
603 hotKey = [[ITHotKey alloc] init];
604 [hotKey setName:@"UpcomingSongs"];
605 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
606 [hotKey setTarget:self];
607 [hotKey setAction:@selector(showUpcomingSongs)];
608 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
611 if ([df objectForKey:@"ToggleLoop"] != nil) {
612 ITDebugLog(@"Setting up toggle loop hot key.");
613 hotKey = [[ITHotKey alloc] init];
614 [hotKey setName:@"ToggleLoop"];
615 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
616 [hotKey setTarget:self];
617 [hotKey setAction:@selector(toggleLoop)];
618 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
621 if ([df objectForKey:@"ToggleShuffle"] != nil) {
622 ITDebugLog(@"Setting up toggle shuffle hot key.");
623 hotKey = [[ITHotKey alloc] init];
624 [hotKey setName:@"ToggleShuffle"];
625 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
626 [hotKey setTarget:self];
627 [hotKey setAction:@selector(toggleShuffle)];
628 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
631 if ([df objectForKey:@"IncrementVolume"] != nil) {
632 ITDebugLog(@"Setting up increment volume hot key.");
633 hotKey = [[ITHotKey alloc] init];
634 [hotKey setName:@"IncrementVolume"];
635 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
636 [hotKey setTarget:self];
637 [hotKey setAction:@selector(incrementVolume)];
638 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
641 if ([df objectForKey:@"DecrementVolume"] != nil) {
642 ITDebugLog(@"Setting up decrement volume hot key.");
643 hotKey = [[ITHotKey alloc] init];
644 [hotKey setName:@"DecrementVolume"];
645 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
646 [hotKey setTarget:self];
647 [hotKey setAction:@selector(decrementVolume)];
648 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
651 if ([df objectForKey:@"IncrementRating"] != nil) {
652 ITDebugLog(@"Setting up increment rating hot key.");
653 hotKey = [[ITHotKey alloc] init];
654 [hotKey setName:@"IncrementRating"];
655 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
656 [hotKey setTarget:self];
657 [hotKey setAction:@selector(incrementRating)];
658 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
661 if ([df objectForKey:@"DecrementRating"] != nil) {
662 ITDebugLog(@"Setting up decrement rating hot key.");
663 hotKey = [[ITHotKey alloc] init];
664 [hotKey setName:@"DecrementRating"];
665 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
666 [hotKey setTarget:self];
667 [hotKey setAction:@selector(decrementRating)];
668 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
670 ITDebugLog(@"Finished setting up hot keys.");
673 - (void)showCurrentTrackInfo
675 ITMTRemotePlayerSource source = 0;
676 NSString *title = nil;
677 NSString *album = nil;
678 NSString *artist = nil;
679 NSString *time = nil;
680 NSString *track = nil;
684 source = [[self currentRemote] currentSource];
685 title = [[self currentRemote] currentSongTitle];
687 [self networkError:localException];
690 ITDebugLog(@"Showing track info status window.");
694 if ( [df boolForKey:@"showAlbum"] ) {
696 album = [[self currentRemote] currentSongAlbum];
698 [self networkError:localException];
702 if ( [df boolForKey:@"showArtist"] ) {
704 artist = [[self currentRemote] currentSongArtist];
706 [self networkError:localException];
710 if ( [df boolForKey:@"showTime"] ) {
712 time = [NSString stringWithFormat:@"%@: %@ / %@",
714 [[self currentRemote] currentSongElapsed],
715 [[self currentRemote] currentSongLength]];
717 [self networkError:localException];
721 if ( [df boolForKey:@"showTrackNumber"] ) {
726 trackNo = [[self currentRemote] currentSongTrack];
727 trackCount = [[self currentRemote] currentAlbumTrackCount];
729 [self networkError:localException];
732 if ( (trackNo > 0) || (trackCount > 0) ) {
733 track = [NSString stringWithFormat:@"%@: %i %@ %i",
734 @"Track", trackNo, @"of", trackCount];
738 if ( [df boolForKey:@"showTrackRating"] ) {
739 float currentRating = 0;
742 currentRating = [[self currentRemote] currentSongRating];
744 [self networkError:localException];
747 if (currentRating >= 0.0) {
748 rating = ( currentRating * 5 );
753 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
756 [statusWindowController showSongInfoWindowWithSource:source
765 - (void)showUpcomingSongs
769 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
771 [self networkError:localException];
774 ITDebugLog(@"Showing upcoming songs status window.");
777 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
778 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
779 int curTrack = [[self currentRemote] currentSongIndex];
782 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
784 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
788 if ([songList count] == 0) {
789 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
792 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
794 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
797 [self networkError:localException];
801 - (void)incrementVolume
804 float volume = [[self currentRemote] volume];
805 float dispVol = volume;
806 ITDebugLog(@"Incrementing 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)decrementVolume
828 float volume = [[self currentRemote] volume];
829 float dispVol = volume;
830 ITDebugLog(@"Decrementing volume.");
839 ITDebugLog(@"Setting volume to %f", volume);
840 [[self currentRemote] setVolume:volume];
842 //Show volume status window
843 [statusWindowController showVolumeWindowWithLevel:dispVol];
845 [self networkError:localException];
849 - (void)incrementRating
852 float rating = [[self currentRemote] currentSongRating];
853 ITDebugLog(@"Incrementing rating.");
855 if ([[self currentRemote] currentPlaylistIndex] == 0) {
856 ITDebugLog(@"No song playing, rating change aborted.");
864 ITDebugLog(@"Setting rating to %f", rating);
865 [[self currentRemote] setCurrentSongRating:rating];
867 //Show rating status window
868 [statusWindowController showRatingWindowWithRating:rating];
870 [self networkError:localException];
874 - (void)decrementRating
877 float rating = [[self currentRemote] currentSongRating];
878 ITDebugLog(@"Decrementing rating.");
880 if ([[self currentRemote] currentPlaylistIndex] == 0) {
881 ITDebugLog(@"No song playing, rating change aborted.");
889 ITDebugLog(@"Setting rating to %f", rating);
890 [[self currentRemote] setCurrentSongRating:rating];
892 //Show rating status window
893 [statusWindowController showRatingWindowWithRating:rating];
895 [self networkError:localException];
902 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
903 ITDebugLog(@"Toggling repeat mode.");
904 switch (repeatMode) {
905 case ITMTRemotePlayerRepeatOff:
906 repeatMode = ITMTRemotePlayerRepeatAll;
908 case ITMTRemotePlayerRepeatAll:
909 repeatMode = ITMTRemotePlayerRepeatOne;
911 case ITMTRemotePlayerRepeatOne:
912 repeatMode = ITMTRemotePlayerRepeatOff;
915 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
916 [[self currentRemote] setRepeatMode:repeatMode];
918 //Show loop status window
919 [statusWindowController showRepeatWindowWithMode:repeatMode];
921 [self networkError:localException];
925 - (void)toggleShuffle
928 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
929 ITDebugLog(@"Toggling shuffle mode.");
930 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
931 //Show shuffle status window
932 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
933 [statusWindowController showShuffleWindow:newShuffleEnabled];
935 [self networkError:localException];
939 - (void)registerNowOK
941 [[StatusWindow sharedWindow] setLocked:NO];
942 [[StatusWindow sharedWindow] vanish:self];
943 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
948 - (void)registerNowCancel
950 [[StatusWindow sharedWindow] setLocked:NO];
951 [[StatusWindow sharedWindow] vanish:self];
952 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
954 [NSApp terminate:self];
957 /*************************************************************************/
959 #pragma mark NETWORK HANDLERS
960 /*************************************************************************/
962 - (void)setServerStatus:(BOOL)newStatus
966 [networkController setServerStatus:YES];
969 [networkController setServerStatus:NO];
973 - (int)connectToServer
976 ITDebugLog(@"Attempting to connect to shared remote.");
977 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
980 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
981 currentRemote = [[[networkController networkObject] remote] retain];
982 [refreshTimer invalidate];
983 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
985 selector:@selector(timerUpdate)
987 repeats:YES] retain];
989 ITDebugLog(@"Connection successful.");
991 } else if (result == 0) {
992 ITDebugLog(@"Connection failed.");
993 currentRemote = [remoteArray objectAtIndex:0];
996 //Do something about the password being invalid
997 ITDebugLog(@"Connection failed.");
998 currentRemote = [remoteArray objectAtIndex:0];
1003 - (BOOL)disconnectFromServer
1005 ITDebugLog(@"Disconnecting from shared remote.");
1007 [currentRemote release];
1008 currentRemote = [remoteArray objectAtIndex:0];
1009 [networkController disconnect];
1014 - (void)checkForRemoteServer:(NSTimer *)timer
1016 ITDebugLog(@"Checking for remote server.");
1017 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1018 ITDebugLog(@"Remote server found.");
1020 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1021 [[StatusWindowController sharedController] showReconnectQueryWindow];
1024 ITDebugLog(@"Remote server not found.");
1028 - (void)networkError:(NSException *)exception
1030 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1031 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1032 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);
1033 if ([self disconnectFromServer]) {
1034 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1035 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1037 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1044 if ([self connectToServer] == 0) {
1045 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1047 [[StatusWindow sharedWindow] setLocked:NO];
1048 [[StatusWindow sharedWindow] vanish:self];
1049 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1052 - (void)cancelReconnect
1054 [[StatusWindow sharedWindow] setLocked:NO];
1055 [[StatusWindow sharedWindow] vanish:self];
1056 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1059 /*************************************************************************/
1061 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1062 /*************************************************************************/
1064 - (void)applicationLaunched:(NSNotification *)note
1067 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1068 ITDebugLog(@"Remote application launched.");
1069 playerRunningState = ITMTRemotePlayerRunning;
1070 [[self currentRemote] begin];
1071 [self setLatestSongIdentifier:@""];
1073 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1075 selector:@selector(timerUpdate)
1077 repeats:YES] retain];
1078 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1079 [self setupHotKeys];
1082 [self networkError:localException];
1086 - (void)applicationTerminated:(NSNotification *)note
1089 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1090 ITDebugLog(@"Remote application terminated.");
1091 playerRunningState = ITMTRemotePlayerNotRunning;
1092 [[self currentRemote] halt];
1093 [refreshTimer invalidate];
1094 [refreshTimer release];
1096 [self clearHotKeys];
1098 if ([df objectForKey:@"ShowPlayer"] != nil) {
1100 ITDebugLog(@"Setting up show player hot key.");
1101 hotKey = [[ITHotKey alloc] init];
1102 [hotKey setName:@"ShowPlayer"];
1103 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1104 [hotKey setTarget:self];
1105 [hotKey setAction:@selector(showPlayer)];
1106 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1110 [self networkError:localException];
1115 /*************************************************************************/
1117 #pragma mark NSApplication DELEGATE METHODS
1118 /*************************************************************************/
1120 - (void)applicationWillTerminate:(NSNotification *)note
1122 [networkController stopRemoteServerSearch];
1123 [self clearHotKeys];
1124 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1128 /*************************************************************************/
1130 #pragma mark DEALLOCATION METHOD
1131 /*************************************************************************/
1135 [self applicationTerminated:nil];
1137 [statusItem release];
1138 [statusWindowController release];
1139 [menuController release];
1140 [networkController release];