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 currentRemote = [self loadRemote];
59 [[self currentRemote] begin];
61 //Turn on network stuff if needed
62 networkController = [[NetworkController alloc] init];
63 if ([df boolForKey:@"enableSharing"]) {
64 [self setServerStatus:YES];
65 } else if ([df boolForKey:@"useSharedPlayer"]) {
66 if ([self connectToServer] == 0) {
67 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
71 //Setup for notification of the remote player launching or quitting
72 [[[NSWorkspace sharedWorkspace] notificationCenter]
74 selector:@selector(applicationTerminated:)
75 name:NSWorkspaceDidTerminateApplicationNotification
78 [[[NSWorkspace sharedWorkspace] notificationCenter]
80 selector:@selector(applicationLaunched:)
81 name:NSWorkspaceDidLaunchApplicationNotification
84 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
85 [[PreferencesController sharedPrefs] registerDefaults];
88 [StatusItemHack install];
89 statusItem = [[ITStatusItem alloc]
90 initWithStatusBar:[NSStatusBar systemStatusBar]
91 withLength:NSSquareStatusItemLength];
93 bling = [[MTBlingController alloc] init];
95 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
97 selector:@selector(blingTime)
102 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
103 [self applicationLaunched:nil];
105 if ([df boolForKey:@"LaunchPlayerWithMT"])
108 [self applicationTerminated:nil];
111 [self networkError:localException];
114 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
115 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
117 [networkController startRemoteServerSearch];
121 - (ITMTRemote *)loadRemote
123 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
124 ITDebugLog(@"Gathering remotes.");
126 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
127 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
128 NSString *bundlePath;
130 while ( (bundlePath = [enumerator nextObject]) ) {
131 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
134 Class remoteClass = [remoteBundle principalClass];
136 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
137 [remoteClass isKindOfClass:[NSObject class]]) {
138 id remote = [remoteClass remote];
139 ITDebugLog(@"Adding remote at path %@", bundlePath);
140 [remoteArray addObject:remote];
145 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
146 // if ( [remoteArray count] > 1 ) {
147 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
149 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
152 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
153 return [remoteArray objectAtIndex:0];
156 /*************************************************************************/
158 #pragma mark INSTANCE METHODS
159 /*************************************************************************/
161 /*- (void)startTimerInNewThread
163 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
164 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
165 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
167 selector:@selector(timerUpdate)
169 repeats:YES] retain];
171 ITDebugLog(@"Timer started.");
175 - (void)setBlingTime:(NSDate*)date
177 NSMutableDictionary *globalPrefs;
179 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
181 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
183 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
185 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
187 [globalPrefs release];
190 - (NSDate*)getBlingTime
193 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
198 NSDate *now = [NSDate date];
199 if (![self blingBling]) {
200 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
201 [self setBlingTime:now];
203 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
205 [statusItem setEnabled:NO];
207 if ([refreshTimer isValid]) {
208 [refreshTimer invalidate];
210 [statusWindowController showRegistrationQueryWindow];
214 [statusItem setEnabled:YES];
216 if (![refreshTimer isValid]) {
217 [refreshTimer release];
218 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
220 selector:@selector(timerUpdate)
222 repeats:YES] retain];
226 [self setBlingTime:nil];
237 if ( ! ([bling checkDone] == 2475) ) {
244 - (BOOL)songIsPlaying
246 NSString *identifier = nil;
248 identifier = [[self currentRemote] playerStateUniqueIdentifier];
250 [self networkError:localException];
252 return ( ! ([identifier isEqualToString:@"0-0"]) );
255 - (BOOL)radioIsPlaying
257 ITMTRemotePlayerPlaylistClass class = nil;
259 class = [[self currentRemote] currentPlaylistClass];
261 [self networkError:localException];
263 return (class == ITMTRemotePlayerRadioPlaylist );
268 NSString *identifier = nil;
270 identifier = [[self currentRemote] playerStateUniqueIdentifier];
272 [self networkError:localException];
274 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
277 - (NSString *)latestSongIdentifier
279 return _latestSongIdentifier;
282 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
284 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
285 [_latestSongIdentifier autorelease];
286 _latestSongIdentifier = [newIdentifier copy];
291 if ([networkController isConnectedToServer]) {
292 [statusItem setMenu:[menuController menu]];
295 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
296 ITDebugLog(@"The song changed.");
300 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
301 [menuController rebuildSubmenus];
303 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
304 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
307 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
309 [self networkError:localException];
318 ITDebugLog(@"Menu clicked.");
319 if ([networkController isConnectedToServer]) {
320 //Used the cached version
325 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
326 [statusItem setMenu:[menuController menu]];
328 [statusItem setMenu:[menuController menuForNoPlayer]];
331 [self networkError:localException];
344 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
345 ITDebugLog(@"Play/Pause toggled");
346 if (state == ITMTRemotePlayerPlaying) {
347 [[self currentRemote] pause];
348 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
349 [[self currentRemote] pause];
350 [[self currentRemote] play];
352 [[self currentRemote] play];
355 [self networkError:localException];
363 ITDebugLog(@"Going to next song.");
365 [[self currentRemote] goToNextSong];
367 [self networkError:localException];
374 ITDebugLog(@"Going to previous song.");
376 [[self currentRemote] goToPreviousSong];
378 [self networkError:localException];
385 ITDebugLog(@"Fast forwarding.");
387 [[self currentRemote] forward];
389 [self networkError:localException];
396 ITDebugLog(@"Rewinding.");
398 [[self currentRemote] rewind];
400 [self networkError:localException];
405 - (void)selectPlaylistAtIndex:(int)index
407 ITDebugLog(@"Selecting playlist %i", index);
409 //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
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)showTestWindow
481 [self showCurrentTrackInfo];
484 - (void)quitMenuTunes
486 ITDebugLog(@"Quitting MenuTunes.");
487 [NSApp terminate:self];
493 - (void)closePreferences
495 ITDebugLog(@"Preferences closed.");
496 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
501 - (ITMTRemote *)currentRemote
503 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
504 [self networkError:nil];
507 return currentRemote;
518 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
519 ITHotKey *nextHotKey;
520 ITDebugLog(@"Clearing hot keys.");
521 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
522 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
524 ITDebugLog(@"Done clearing hot keys.");
530 ITDebugLog(@"Setting up hot keys.");
532 if (playerRunningState == ITMTRemotePlayerNotRunning) {
536 if ([df objectForKey:@"PlayPause"] != nil) {
537 ITDebugLog(@"Setting up play pause hot key.");
538 hotKey = [[ITHotKey alloc] init];
539 [hotKey setName:@"PlayPause"];
540 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
541 [hotKey setTarget:self];
542 [hotKey setAction:@selector(playPause)];
543 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
546 if ([df objectForKey:@"NextTrack"] != nil) {
547 ITDebugLog(@"Setting up next track hot key.");
548 hotKey = [[ITHotKey alloc] init];
549 [hotKey setName:@"NextTrack"];
550 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
551 [hotKey setTarget:self];
552 [hotKey setAction:@selector(nextSong)];
553 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
556 if ([df objectForKey:@"PrevTrack"] != nil) {
557 ITDebugLog(@"Setting up previous track hot key.");
558 hotKey = [[ITHotKey alloc] init];
559 [hotKey setName:@"PrevTrack"];
560 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
561 [hotKey setTarget:self];
562 [hotKey setAction:@selector(prevSong)];
563 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
566 if ([df objectForKey:@"ShowPlayer"] != nil) {
567 ITDebugLog(@"Setting up show player hot key.");
568 hotKey = [[ITHotKey alloc] init];
569 [hotKey setName:@"ShowPlayer"];
570 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
571 [hotKey setTarget:self];
572 [hotKey setAction:@selector(showPlayer)];
573 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
576 if ([df objectForKey:@"TrackInfo"] != nil) {
577 ITDebugLog(@"Setting up track info hot key.");
578 hotKey = [[ITHotKey alloc] init];
579 [hotKey setName:@"TrackInfo"];
580 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
581 [hotKey setTarget:self];
582 [hotKey setAction:@selector(showCurrentTrackInfo)];
583 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
586 if ([df objectForKey:@"UpcomingSongs"] != nil) {
587 ITDebugLog(@"Setting up upcoming songs hot key.");
588 hotKey = [[ITHotKey alloc] init];
589 [hotKey setName:@"UpcomingSongs"];
590 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
591 [hotKey setTarget:self];
592 [hotKey setAction:@selector(showUpcomingSongs)];
593 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
596 if ([df objectForKey:@"ToggleLoop"] != nil) {
597 ITDebugLog(@"Setting up toggle loop hot key.");
598 hotKey = [[ITHotKey alloc] init];
599 [hotKey setName:@"ToggleLoop"];
600 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
601 [hotKey setTarget:self];
602 [hotKey setAction:@selector(toggleLoop)];
603 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
606 if ([df objectForKey:@"ToggleShuffle"] != nil) {
607 ITDebugLog(@"Setting up toggle shuffle hot key.");
608 hotKey = [[ITHotKey alloc] init];
609 [hotKey setName:@"ToggleShuffle"];
610 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
611 [hotKey setTarget:self];
612 [hotKey setAction:@selector(toggleShuffle)];
613 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
616 if ([df objectForKey:@"IncrementVolume"] != nil) {
617 ITDebugLog(@"Setting up increment volume hot key.");
618 hotKey = [[ITHotKey alloc] init];
619 [hotKey setName:@"IncrementVolume"];
620 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
621 [hotKey setTarget:self];
622 [hotKey setAction:@selector(incrementVolume)];
623 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
626 if ([df objectForKey:@"DecrementVolume"] != nil) {
627 ITDebugLog(@"Setting up decrement volume hot key.");
628 hotKey = [[ITHotKey alloc] init];
629 [hotKey setName:@"DecrementVolume"];
630 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
631 [hotKey setTarget:self];
632 [hotKey setAction:@selector(decrementVolume)];
633 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
636 if ([df objectForKey:@"IncrementRating"] != nil) {
637 ITDebugLog(@"Setting up increment rating hot key.");
638 hotKey = [[ITHotKey alloc] init];
639 [hotKey setName:@"IncrementRating"];
640 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
641 [hotKey setTarget:self];
642 [hotKey setAction:@selector(incrementRating)];
643 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
646 if ([df objectForKey:@"DecrementRating"] != nil) {
647 ITDebugLog(@"Setting up decrement rating hot key.");
648 hotKey = [[ITHotKey alloc] init];
649 [hotKey setName:@"DecrementRating"];
650 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
651 [hotKey setTarget:self];
652 [hotKey setAction:@selector(decrementRating)];
653 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
655 ITDebugLog(@"Finished setting up hot keys.");
658 - (void)showCurrentTrackInfo
660 ITMTRemotePlayerSource source = 0;
661 NSString *title = nil;
662 NSString *album = nil;
663 NSString *artist = nil;
664 NSString *time = nil;
665 NSString *track = nil;
669 source = [[self currentRemote] currentSource];
670 title = [[self currentRemote] currentSongTitle];
672 [self networkError:localException];
675 ITDebugLog(@"Showing track info status window.");
679 if ( [df boolForKey:@"showAlbum"] ) {
681 album = [[self currentRemote] currentSongAlbum];
683 [self networkError:localException];
687 if ( [df boolForKey:@"showArtist"] ) {
689 artist = [[self currentRemote] currentSongArtist];
691 [self networkError:localException];
695 if ( [df boolForKey:@"showTime"] ) {
697 time = [NSString stringWithFormat:@"%@: %@ / %@",
699 [[self currentRemote] currentSongElapsed],
700 [[self currentRemote] currentSongLength]];
702 [self networkError:localException];
706 if ( [df boolForKey:@"showTrackNumber"] ) {
711 trackNo = [[self currentRemote] currentSongTrack];
712 trackCount = [[self currentRemote] currentAlbumTrackCount];
714 [self networkError:localException];
717 if ( (trackNo > 0) || (trackCount > 0) ) {
718 track = [NSString stringWithFormat:@"%@: %i %@ %i",
719 @"Track", trackNo, @"of", trackCount];
723 if ( [df boolForKey:@"showTrackRating"] ) {
724 float currentRating = 0;
727 currentRating = [[self currentRemote] currentSongRating];
729 [self networkError:localException];
732 if (currentRating >= 0.0) {
733 rating = ( currentRating * 5 );
738 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
741 [statusWindowController showSongInfoWindowWithSource:source
750 - (void)showUpcomingSongs
754 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
756 [self networkError:localException];
759 ITDebugLog(@"Showing upcoming songs status window.");
762 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
763 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
764 int curTrack = [[self currentRemote] currentSongIndex];
767 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
769 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
773 if ([songList count] == 0) {
774 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
777 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
779 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
782 [self networkError:localException];
786 - (void)incrementVolume
789 float volume = [[self currentRemote] volume];
790 float dispVol = volume;
791 ITDebugLog(@"Incrementing volume.");
800 ITDebugLog(@"Setting volume to %f", volume);
801 [[self currentRemote] setVolume:volume];
803 // Show volume status window
804 [statusWindowController showVolumeWindowWithLevel:dispVol];
806 [self networkError:localException];
810 - (void)decrementVolume
813 float volume = [[self currentRemote] volume];
814 float dispVol = volume;
815 ITDebugLog(@"Decrementing volume.");
824 ITDebugLog(@"Setting volume to %f", volume);
825 [[self currentRemote] setVolume:volume];
827 //Show volume status window
828 [statusWindowController showVolumeWindowWithLevel:dispVol];
830 [self networkError:localException];
834 - (void)incrementRating
837 float rating = [[self currentRemote] currentSongRating];
838 ITDebugLog(@"Incrementing rating.");
840 if ([[self currentRemote] currentPlaylistIndex] == 0) {
841 ITDebugLog(@"No song playing, rating change aborted.");
849 ITDebugLog(@"Setting rating to %f", rating);
850 [[self currentRemote] setCurrentSongRating:rating];
852 //Show rating status window
853 [statusWindowController showRatingWindowWithRating:rating];
855 [self networkError:localException];
859 - (void)decrementRating
862 float rating = [[self currentRemote] currentSongRating];
863 ITDebugLog(@"Decrementing rating.");
865 if ([[self currentRemote] currentPlaylistIndex] == 0) {
866 ITDebugLog(@"No song playing, rating change aborted.");
874 ITDebugLog(@"Setting rating to %f", rating);
875 [[self currentRemote] setCurrentSongRating:rating];
877 //Show rating status window
878 [statusWindowController showRatingWindowWithRating:rating];
880 [self networkError:localException];
887 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
888 ITDebugLog(@"Toggling repeat mode.");
889 switch (repeatMode) {
890 case ITMTRemotePlayerRepeatOff:
891 repeatMode = ITMTRemotePlayerRepeatAll;
893 case ITMTRemotePlayerRepeatAll:
894 repeatMode = ITMTRemotePlayerRepeatOne;
896 case ITMTRemotePlayerRepeatOne:
897 repeatMode = ITMTRemotePlayerRepeatOff;
900 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
901 [[self currentRemote] setRepeatMode:repeatMode];
903 //Show loop status window
904 [statusWindowController showRepeatWindowWithMode:repeatMode];
906 [self networkError:localException];
910 - (void)toggleShuffle
913 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
914 ITDebugLog(@"Toggling shuffle mode.");
915 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
916 //Show shuffle status window
917 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
918 [statusWindowController showShuffleWindow:newShuffleEnabled];
920 [self networkError:localException];
924 - (void)registerNowOK
926 [[StatusWindow sharedWindow] setLocked:NO];
927 [[StatusWindow sharedWindow] vanish:self];
928 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
933 - (void)registerNowCancel
935 [[StatusWindow sharedWindow] setLocked:NO];
936 [[StatusWindow sharedWindow] vanish:self];
937 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
939 [NSApp terminate:self];
942 /*************************************************************************/
944 #pragma mark NETWORK HANDLERS
945 /*************************************************************************/
947 - (void)setServerStatus:(BOOL)newStatus
951 [networkController setServerStatus:YES];
954 [networkController setServerStatus:NO];
958 - (int)connectToServer
961 ITDebugLog(@"Attempting to connect to shared remote.");
962 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
965 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
966 currentRemote = [[[networkController networkObject] remote] retain];
967 [refreshTimer invalidate];
968 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
970 selector:@selector(timerUpdate)
972 repeats:YES] retain];
974 ITDebugLog(@"Connection successful.");
976 } else if (result == 0) {
977 ITDebugLog(@"Connection failed.");
978 currentRemote = [remoteArray objectAtIndex:0];
981 //Do something about the password being invalid
982 ITDebugLog(@"Connection failed.");
983 currentRemote = [remoteArray objectAtIndex:0];
988 - (BOOL)disconnectFromServer
990 ITDebugLog(@"Disconnecting from shared remote.");
992 [currentRemote release];
993 currentRemote = [remoteArray objectAtIndex:0];
994 [networkController disconnect];
999 - (void)checkForRemoteServer:(NSTimer *)timer
1001 ITDebugLog(@"Checking for remote server.");
1002 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1003 ITDebugLog(@"Remote server found.");
1005 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1006 [[StatusWindowController sharedController] showReconnectQueryWindow];
1009 ITDebugLog(@"Remote server not found.");
1013 - (void)networkError:(NSException *)exception
1015 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1016 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1017 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);
1018 if ([self disconnectFromServer]) {
1019 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1020 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1022 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1029 if ([self connectToServer] == 0) {
1030 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1032 [[StatusWindow sharedWindow] setLocked:NO];
1033 [[StatusWindow sharedWindow] vanish:self];
1034 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1037 - (void)cancelReconnect
1039 [[StatusWindow sharedWindow] setLocked:NO];
1040 [[StatusWindow sharedWindow] vanish:self];
1041 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1044 /*************************************************************************/
1046 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1047 /*************************************************************************/
1049 - (void)applicationLaunched:(NSNotification *)note
1052 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1053 ITDebugLog(@"Remote application launched.");
1054 playerRunningState = ITMTRemotePlayerRunning;
1055 [[self currentRemote] begin];
1056 [self setLatestSongIdentifier:@""];
1058 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1060 selector:@selector(timerUpdate)
1062 repeats:YES] retain];
1063 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1064 [self setupHotKeys];
1067 [self networkError:localException];
1071 - (void)applicationTerminated:(NSNotification *)note
1074 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1075 ITDebugLog(@"Remote application terminated.");
1076 playerRunningState = ITMTRemotePlayerNotRunning;
1077 [[self currentRemote] halt];
1078 [refreshTimer invalidate];
1079 [refreshTimer release];
1081 [self clearHotKeys];
1083 if ([df objectForKey:@"ShowPlayer"] != nil) {
1085 ITDebugLog(@"Setting up show player hot key.");
1086 hotKey = [[ITHotKey alloc] init];
1087 [hotKey setName:@"ShowPlayer"];
1088 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1089 [hotKey setTarget:self];
1090 [hotKey setAction:@selector(showPlayer)];
1091 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1095 [self networkError:localException];
1100 /*************************************************************************/
1102 #pragma mark NSApplication DELEGATE METHODS
1103 /*************************************************************************/
1105 - (void)applicationWillTerminate:(NSNotification *)note
1107 [networkController stopRemoteServerSearch];
1108 [self clearHotKeys];
1109 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1113 /*************************************************************************/
1115 #pragma mark DEALLOCATION METHOD
1116 /*************************************************************************/
1120 [self applicationTerminated:nil];
1122 [statusItem release];
1123 [statusWindowController release];
1124 [menuController release];
1125 [networkController release];