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]];
293 if ([[networkController networkObject] remote] == nil) {
294 [self networkError:nil];
298 if ( [self songChanged] && (timerUpdating != YES) ) {
299 ITDebugLog(@"The song changed.");
303 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
304 [menuController rebuildSubmenus];
306 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
307 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
310 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
312 [self networkError:localException];
321 ITDebugLog(@"Menu clicked.");
322 if ([networkController isConnectedToServer]) {
323 //Used the cached version
328 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
329 [statusItem setMenu:[menuController menu]];
331 [statusItem setMenu:[menuController menuForNoPlayer]];
334 [self networkError:localException];
347 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
348 ITDebugLog(@"Play/Pause toggled");
349 if (state == ITMTRemotePlayerPlaying) {
350 [[self currentRemote] pause];
351 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
352 [[self currentRemote] pause];
353 [[self currentRemote] play];
355 [[self currentRemote] play];
358 [self networkError:localException];
366 ITDebugLog(@"Going to next song.");
368 [[self currentRemote] goToNextSong];
370 [self networkError:localException];
377 ITDebugLog(@"Going to previous song.");
379 [[self currentRemote] goToPreviousSong];
381 [self networkError:localException];
388 ITDebugLog(@"Fast forwarding.");
390 [[self currentRemote] forward];
392 [self networkError:localException];
399 ITDebugLog(@"Rewinding.");
401 [[self currentRemote] rewind];
403 [self networkError:localException];
408 - (void)selectPlaylistAtIndex:(int)index
410 ITDebugLog(@"Selecting playlist %i", index);
412 [[self currentRemote] switchToPlaylistAtIndex:index];
414 [self networkError:localException];
419 - (void)selectSongAtIndex:(int)index
421 ITDebugLog(@"Selecting song %i", index);
423 [[self currentRemote] switchToSongAtIndex:index];
425 [self networkError:localException];
430 - (void)selectSongRating:(int)rating
432 ITDebugLog(@"Selecting song rating %i", rating);
434 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
436 [self networkError:localException];
441 - (void)selectEQPresetAtIndex:(int)index
443 ITDebugLog(@"Selecting EQ preset %i", index);
445 [[self currentRemote] switchToEQAtIndex:index];
447 [self networkError:localException];
454 ITDebugLog(@"Beginning show player.");
455 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
456 ITDebugLog(@"Showing player interface.");
458 [[self currentRemote] showPrimaryInterface];
460 [self networkError:localException];
463 ITDebugLog(@"Launching player.");
465 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
466 ITDebugLog(@"Error Launching Player");
469 [self networkError:localException];
472 ITDebugLog(@"Finished show player.");
475 - (void)showPreferences
477 ITDebugLog(@"Show preferences.");
478 [[PreferencesController sharedPrefs] showPrefsWindow:self];
481 - (void)showTestWindow
483 [self showCurrentTrackInfo];
486 - (void)quitMenuTunes
488 ITDebugLog(@"Quitting MenuTunes.");
489 [NSApp terminate:self];
495 - (void)closePreferences
497 ITDebugLog(@"Preferences closed.");
498 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
503 - (ITMTRemote *)currentRemote
505 return currentRemote;
516 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
517 ITHotKey *nextHotKey;
518 ITDebugLog(@"Clearing hot keys.");
519 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
520 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
522 ITDebugLog(@"Done clearing hot keys.");
528 ITDebugLog(@"Setting up hot keys.");
530 if (playerRunningState == ITMTRemotePlayerNotRunning) {
534 if ([df objectForKey:@"PlayPause"] != nil) {
535 ITDebugLog(@"Setting up play pause hot key.");
536 hotKey = [[ITHotKey alloc] init];
537 [hotKey setName:@"PlayPause"];
538 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
539 [hotKey setTarget:self];
540 [hotKey setAction:@selector(playPause)];
541 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
544 if ([df objectForKey:@"NextTrack"] != nil) {
545 ITDebugLog(@"Setting up next track hot key.");
546 hotKey = [[ITHotKey alloc] init];
547 [hotKey setName:@"NextTrack"];
548 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
549 [hotKey setTarget:self];
550 [hotKey setAction:@selector(nextSong)];
551 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
554 if ([df objectForKey:@"PrevTrack"] != nil) {
555 ITDebugLog(@"Setting up previous track hot key.");
556 hotKey = [[ITHotKey alloc] init];
557 [hotKey setName:@"PrevTrack"];
558 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
559 [hotKey setTarget:self];
560 [hotKey setAction:@selector(prevSong)];
561 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
564 if ([df objectForKey:@"ShowPlayer"] != nil) {
565 ITDebugLog(@"Setting up show player hot key.");
566 hotKey = [[ITHotKey alloc] init];
567 [hotKey setName:@"ShowPlayer"];
568 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
569 [hotKey setTarget:self];
570 [hotKey setAction:@selector(showPlayer)];
571 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
574 if ([df objectForKey:@"TrackInfo"] != nil) {
575 ITDebugLog(@"Setting up track info hot key.");
576 hotKey = [[ITHotKey alloc] init];
577 [hotKey setName:@"TrackInfo"];
578 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
579 [hotKey setTarget:self];
580 [hotKey setAction:@selector(showCurrentTrackInfo)];
581 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
584 if ([df objectForKey:@"UpcomingSongs"] != nil) {
585 ITDebugLog(@"Setting up upcoming songs hot key.");
586 hotKey = [[ITHotKey alloc] init];
587 [hotKey setName:@"UpcomingSongs"];
588 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
589 [hotKey setTarget:self];
590 [hotKey setAction:@selector(showUpcomingSongs)];
591 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
594 if ([df objectForKey:@"ToggleLoop"] != nil) {
595 ITDebugLog(@"Setting up toggle loop hot key.");
596 hotKey = [[ITHotKey alloc] init];
597 [hotKey setName:@"ToggleLoop"];
598 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
599 [hotKey setTarget:self];
600 [hotKey setAction:@selector(toggleLoop)];
601 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
604 if ([df objectForKey:@"ToggleShuffle"] != nil) {
605 ITDebugLog(@"Setting up toggle shuffle hot key.");
606 hotKey = [[ITHotKey alloc] init];
607 [hotKey setName:@"ToggleShuffle"];
608 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
609 [hotKey setTarget:self];
610 [hotKey setAction:@selector(toggleShuffle)];
611 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
614 if ([df objectForKey:@"IncrementVolume"] != nil) {
615 ITDebugLog(@"Setting up increment volume hot key.");
616 hotKey = [[ITHotKey alloc] init];
617 [hotKey setName:@"IncrementVolume"];
618 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
619 [hotKey setTarget:self];
620 [hotKey setAction:@selector(incrementVolume)];
621 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
624 if ([df objectForKey:@"DecrementVolume"] != nil) {
625 ITDebugLog(@"Setting up decrement volume hot key.");
626 hotKey = [[ITHotKey alloc] init];
627 [hotKey setName:@"DecrementVolume"];
628 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
629 [hotKey setTarget:self];
630 [hotKey setAction:@selector(decrementVolume)];
631 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
634 if ([df objectForKey:@"IncrementRating"] != nil) {
635 ITDebugLog(@"Setting up increment rating hot key.");
636 hotKey = [[ITHotKey alloc] init];
637 [hotKey setName:@"IncrementRating"];
638 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
639 [hotKey setTarget:self];
640 [hotKey setAction:@selector(incrementRating)];
641 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
644 if ([df objectForKey:@"DecrementRating"] != nil) {
645 ITDebugLog(@"Setting up decrement rating hot key.");
646 hotKey = [[ITHotKey alloc] init];
647 [hotKey setName:@"DecrementRating"];
648 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
649 [hotKey setTarget:self];
650 [hotKey setAction:@selector(decrementRating)];
651 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
653 ITDebugLog(@"Finished setting up hot keys.");
656 - (void)showCurrentTrackInfo
658 ITMTRemotePlayerSource source = 0;
659 NSString *title = nil;
660 NSString *album = nil;
661 NSString *artist = nil;
662 NSString *time = nil;
663 NSString *track = nil;
667 source = [[self currentRemote] currentSource];
668 title = [[self currentRemote] currentSongTitle];
670 [self networkError:localException];
673 ITDebugLog(@"Showing track info status window.");
677 if ( [df boolForKey:@"showAlbum"] ) {
679 album = [[self currentRemote] currentSongAlbum];
681 [self networkError:localException];
685 if ( [df boolForKey:@"showArtist"] ) {
687 artist = [[self currentRemote] currentSongArtist];
689 [self networkError:localException];
693 if ( [df boolForKey:@"showTime"] ) {
695 time = [NSString stringWithFormat:@"%@: %@ / %@",
697 [[self currentRemote] currentSongElapsed],
698 [[self currentRemote] currentSongLength]];
700 [self networkError:localException];
704 if ( [df boolForKey:@"showTrackNumber"] ) {
709 trackNo = [[self currentRemote] currentSongTrack];
710 trackCount = [[self currentRemote] currentAlbumTrackCount];
712 [self networkError:localException];
715 if ( (trackNo > 0) || (trackCount > 0) ) {
716 track = [NSString stringWithFormat:@"%@: %i %@ %i",
717 @"Track", trackNo, @"of", trackCount];
721 if ( [df boolForKey:@"showTrackRating"] ) {
722 float currentRating = 0;
725 currentRating = [[self currentRemote] currentSongRating];
727 [self networkError:localException];
730 if (currentRating >= 0.0) {
731 rating = ( currentRating * 5 );
736 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
739 [statusWindowController showSongInfoWindowWithSource:source
748 - (void)showUpcomingSongs
752 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
754 [self networkError:localException];
757 ITDebugLog(@"Showing upcoming songs status window.");
760 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
761 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
762 int curTrack = [[self currentRemote] currentSongIndex];
765 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
767 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
771 if ([songList count] == 0) {
772 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
775 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
777 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
780 [self networkError:localException];
784 - (void)incrementVolume
787 float volume = [[self currentRemote] volume];
788 float dispVol = volume;
789 ITDebugLog(@"Incrementing volume.");
798 ITDebugLog(@"Setting volume to %f", volume);
799 [[self currentRemote] setVolume:volume];
801 // Show volume status window
802 [statusWindowController showVolumeWindowWithLevel:dispVol];
804 [self networkError:localException];
808 - (void)decrementVolume
811 float volume = [[self currentRemote] volume];
812 float dispVol = volume;
813 ITDebugLog(@"Decrementing volume.");
822 ITDebugLog(@"Setting volume to %f", volume);
823 [[self currentRemote] setVolume:volume];
825 //Show volume status window
826 [statusWindowController showVolumeWindowWithLevel:dispVol];
828 [self networkError:localException];
832 - (void)incrementRating
835 float rating = [[self currentRemote] currentSongRating];
836 ITDebugLog(@"Incrementing rating.");
838 if ([[self currentRemote] currentPlaylistIndex] == 0) {
839 ITDebugLog(@"No song playing, rating change aborted.");
847 ITDebugLog(@"Setting rating to %f", rating);
848 [[self currentRemote] setCurrentSongRating:rating];
850 //Show rating status window
851 [statusWindowController showRatingWindowWithRating:rating];
853 [self networkError:localException];
857 - (void)decrementRating
860 float rating = [[self currentRemote] currentSongRating];
861 ITDebugLog(@"Decrementing rating.");
863 if ([[self currentRemote] currentPlaylistIndex] == 0) {
864 ITDebugLog(@"No song playing, rating change aborted.");
872 ITDebugLog(@"Setting rating to %f", rating);
873 [[self currentRemote] setCurrentSongRating:rating];
875 //Show rating status window
876 [statusWindowController showRatingWindowWithRating:rating];
878 [self networkError:localException];
885 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
886 ITDebugLog(@"Toggling repeat mode.");
887 switch (repeatMode) {
888 case ITMTRemotePlayerRepeatOff:
889 repeatMode = ITMTRemotePlayerRepeatAll;
891 case ITMTRemotePlayerRepeatAll:
892 repeatMode = ITMTRemotePlayerRepeatOne;
894 case ITMTRemotePlayerRepeatOne:
895 repeatMode = ITMTRemotePlayerRepeatOff;
898 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
899 [[self currentRemote] setRepeatMode:repeatMode];
901 //Show loop status window
902 [statusWindowController showRepeatWindowWithMode:repeatMode];
904 [self networkError:localException];
908 - (void)toggleShuffle
911 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
912 ITDebugLog(@"Toggling shuffle mode.");
913 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
914 //Show shuffle status window
915 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
916 [statusWindowController showShuffleWindow:newShuffleEnabled];
918 [self networkError:localException];
922 - (void)registerNowOK
924 [[StatusWindow sharedWindow] setLocked:NO];
925 [[StatusWindow sharedWindow] vanish:self];
926 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
931 - (void)registerNowCancel
933 [[StatusWindow sharedWindow] setLocked:NO];
934 [[StatusWindow sharedWindow] vanish:self];
935 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
937 [NSApp terminate:self];
940 /*************************************************************************/
942 #pragma mark NETWORK HANDLERS
943 /*************************************************************************/
945 - (void)setServerStatus:(BOOL)newStatus
949 [networkController setServerStatus:YES];
952 [networkController setServerStatus:NO];
956 - (int)connectToServer
959 ITDebugLog(@"Attempting to connect to shared remote.");
960 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
963 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
964 currentRemote = [[[networkController networkObject] remote] retain];
965 [refreshTimer invalidate];
966 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
968 selector:@selector(timerUpdate)
970 repeats:YES] retain];
972 ITDebugLog(@"Connection successful.");
974 } else if (result == 0) {
975 ITDebugLog(@"Connection failed.");
976 currentRemote = [remoteArray objectAtIndex:0];
979 //Do something about the password being invalid
980 ITDebugLog(@"Connection failed.");
981 currentRemote = [remoteArray objectAtIndex:0];
986 - (BOOL)disconnectFromServer
988 ITDebugLog(@"Disconnecting from shared remote.");
990 [currentRemote release];
991 currentRemote = [remoteArray objectAtIndex:0];
992 [networkController disconnect];
997 - (void)checkForRemoteServer:(NSTimer *)timer
999 ITDebugLog(@"Checking for remote server.");
1000 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1001 ITDebugLog(@"Remote server found.");
1003 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1004 [[StatusWindowController sharedController] showReconnectQueryWindow];
1007 ITDebugLog(@"Remote server not found.");
1011 - (void)networkError:(NSException *)exception
1013 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1014 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1015 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);
1016 if ([self disconnectFromServer]) {
1017 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1018 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1020 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1027 if ([self connectToServer] == 0) {
1028 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1030 [[StatusWindow sharedWindow] setLocked:NO];
1031 [[StatusWindow sharedWindow] vanish:self];
1032 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1035 - (void)cancelReconnect
1037 [[StatusWindow sharedWindow] setLocked:NO];
1038 [[StatusWindow sharedWindow] vanish:self];
1039 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1042 /*************************************************************************/
1044 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1045 /*************************************************************************/
1047 - (void)applicationLaunched:(NSNotification *)note
1050 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1051 ITDebugLog(@"Remote application launched.");
1052 playerRunningState = ITMTRemotePlayerRunning;
1053 [[self currentRemote] begin];
1054 [self setLatestSongIdentifier:@""];
1056 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1058 selector:@selector(timerUpdate)
1060 repeats:YES] retain];
1061 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1062 [self setupHotKeys];
1065 [self networkError:localException];
1069 - (void)applicationTerminated:(NSNotification *)note
1072 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1073 ITDebugLog(@"Remote application terminated.");
1074 [[self currentRemote] halt];
1075 [refreshTimer invalidate];
1076 [refreshTimer release];
1078 [self clearHotKeys];
1079 playerRunningState = ITMTRemotePlayerNotRunning;
1081 if ([df objectForKey:@"ShowPlayer"] != nil) {
1083 ITDebugLog(@"Setting up show player hot key.");
1084 hotKey = [[ITHotKey alloc] init];
1085 [hotKey setName:@"ShowPlayer"];
1086 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1087 [hotKey setTarget:self];
1088 [hotKey setAction:@selector(showPlayer)];
1089 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1093 [self networkError:localException];
1098 /*************************************************************************/
1100 #pragma mark NSApplication DELEGATE METHODS
1101 /*************************************************************************/
1103 - (void)applicationWillTerminate:(NSNotification *)note
1105 [networkController stopRemoteServerSearch];
1106 [self clearHotKeys];
1107 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1111 /*************************************************************************/
1113 #pragma mark DEALLOCATION METHOD
1114 /*************************************************************************/
1118 [self applicationTerminated:nil];
1120 [statusItem release];
1121 [statusWindowController release];
1122 [menuController release];
1123 [networkController release];