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) ) {
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];
411 [self networkError:localException];
416 - (void)selectSongAtIndex:(int)index
418 ITDebugLog(@"Selecting song %i", index);
420 [[self currentRemote] switchToSongAtIndex:index];
422 [self networkError:localException];
427 - (void)selectSongRating:(int)rating
429 ITDebugLog(@"Selecting song rating %i", rating);
431 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
433 [self networkError:localException];
438 - (void)selectEQPresetAtIndex:(int)index
440 ITDebugLog(@"Selecting EQ preset %i", index);
442 [[self currentRemote] switchToEQAtIndex:index];
444 [self networkError:localException];
451 ITDebugLog(@"Beginning show player.");
452 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
453 ITDebugLog(@"Showing player interface.");
455 [[self currentRemote] showPrimaryInterface];
457 [self networkError:localException];
460 ITDebugLog(@"Launching player.");
462 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
463 ITDebugLog(@"Error Launching Player");
466 [self networkError:localException];
469 ITDebugLog(@"Finished show player.");
472 - (void)showPreferences
474 ITDebugLog(@"Show preferences.");
475 [[PreferencesController sharedPrefs] showPrefsWindow:self];
478 - (void)showTestWindow
480 [self showCurrentTrackInfo];
483 - (void)quitMenuTunes
485 ITDebugLog(@"Quitting MenuTunes.");
486 [NSApp terminate:self];
492 - (void)closePreferences
494 ITDebugLog(@"Preferences closed.");
495 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
500 - (ITMTRemote *)currentRemote
502 return currentRemote;
513 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
514 ITHotKey *nextHotKey;
515 ITDebugLog(@"Clearing hot keys.");
516 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
517 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
519 ITDebugLog(@"Done clearing hot keys.");
525 ITDebugLog(@"Setting up hot keys.");
527 if (playerRunningState == ITMTRemotePlayerNotRunning) {
531 if ([df objectForKey:@"PlayPause"] != nil) {
532 ITDebugLog(@"Setting up play pause hot key.");
533 hotKey = [[ITHotKey alloc] init];
534 [hotKey setName:@"PlayPause"];
535 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
536 [hotKey setTarget:self];
537 [hotKey setAction:@selector(playPause)];
538 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
541 if ([df objectForKey:@"NextTrack"] != nil) {
542 ITDebugLog(@"Setting up next track hot key.");
543 hotKey = [[ITHotKey alloc] init];
544 [hotKey setName:@"NextTrack"];
545 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
546 [hotKey setTarget:self];
547 [hotKey setAction:@selector(nextSong)];
548 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
551 if ([df objectForKey:@"PrevTrack"] != nil) {
552 ITDebugLog(@"Setting up previous track hot key.");
553 hotKey = [[ITHotKey alloc] init];
554 [hotKey setName:@"PrevTrack"];
555 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
556 [hotKey setTarget:self];
557 [hotKey setAction:@selector(prevSong)];
558 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
561 if ([df objectForKey:@"ShowPlayer"] != nil) {
562 ITDebugLog(@"Setting up show player hot key.");
563 hotKey = [[ITHotKey alloc] init];
564 [hotKey setName:@"ShowPlayer"];
565 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
566 [hotKey setTarget:self];
567 [hotKey setAction:@selector(showPlayer)];
568 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
571 if ([df objectForKey:@"TrackInfo"] != nil) {
572 ITDebugLog(@"Setting up track info hot key.");
573 hotKey = [[ITHotKey alloc] init];
574 [hotKey setName:@"TrackInfo"];
575 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
576 [hotKey setTarget:self];
577 [hotKey setAction:@selector(showCurrentTrackInfo)];
578 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
581 if ([df objectForKey:@"UpcomingSongs"] != nil) {
582 ITDebugLog(@"Setting up upcoming songs hot key.");
583 hotKey = [[ITHotKey alloc] init];
584 [hotKey setName:@"UpcomingSongs"];
585 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
586 [hotKey setTarget:self];
587 [hotKey setAction:@selector(showUpcomingSongs)];
588 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
591 if ([df objectForKey:@"ToggleLoop"] != nil) {
592 ITDebugLog(@"Setting up toggle loop hot key.");
593 hotKey = [[ITHotKey alloc] init];
594 [hotKey setName:@"ToggleLoop"];
595 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
596 [hotKey setTarget:self];
597 [hotKey setAction:@selector(toggleLoop)];
598 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
601 if ([df objectForKey:@"ToggleShuffle"] != nil) {
602 ITDebugLog(@"Setting up toggle shuffle hot key.");
603 hotKey = [[ITHotKey alloc] init];
604 [hotKey setName:@"ToggleShuffle"];
605 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
606 [hotKey setTarget:self];
607 [hotKey setAction:@selector(toggleShuffle)];
608 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
611 if ([df objectForKey:@"IncrementVolume"] != nil) {
612 ITDebugLog(@"Setting up increment volume hot key.");
613 hotKey = [[ITHotKey alloc] init];
614 [hotKey setName:@"IncrementVolume"];
615 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
616 [hotKey setTarget:self];
617 [hotKey setAction:@selector(incrementVolume)];
618 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
621 if ([df objectForKey:@"DecrementVolume"] != nil) {
622 ITDebugLog(@"Setting up decrement volume hot key.");
623 hotKey = [[ITHotKey alloc] init];
624 [hotKey setName:@"DecrementVolume"];
625 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
626 [hotKey setTarget:self];
627 [hotKey setAction:@selector(decrementVolume)];
628 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
631 if ([df objectForKey:@"IncrementRating"] != nil) {
632 ITDebugLog(@"Setting up increment rating hot key.");
633 hotKey = [[ITHotKey alloc] init];
634 [hotKey setName:@"IncrementRating"];
635 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
636 [hotKey setTarget:self];
637 [hotKey setAction:@selector(incrementRating)];
638 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
641 if ([df objectForKey:@"DecrementRating"] != nil) {
642 ITDebugLog(@"Setting up decrement rating hot key.");
643 hotKey = [[ITHotKey alloc] init];
644 [hotKey setName:@"DecrementRating"];
645 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
646 [hotKey setTarget:self];
647 [hotKey setAction:@selector(decrementRating)];
648 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
650 ITDebugLog(@"Finished setting up hot keys.");
653 - (void)showCurrentTrackInfo
655 ITMTRemotePlayerSource source = 0;
656 NSString *title = nil;
657 NSString *album = nil;
658 NSString *artist = nil;
659 NSString *time = nil;
660 NSString *track = nil;
664 source = [[self currentRemote] currentSource];
665 title = [[self currentRemote] currentSongTitle];
667 [self networkError:localException];
670 ITDebugLog(@"Showing track info status window.");
674 if ( [df boolForKey:@"showAlbum"] ) {
676 album = [[self currentRemote] currentSongAlbum];
678 [self networkError:localException];
682 if ( [df boolForKey:@"showArtist"] ) {
684 artist = [[self currentRemote] currentSongArtist];
686 [self networkError:localException];
690 if ( [df boolForKey:@"showTime"] ) {
692 time = [NSString stringWithFormat:@"%@: %@ / %@",
694 [[self currentRemote] currentSongElapsed],
695 [[self currentRemote] currentSongLength]];
697 [self networkError:localException];
701 if ( [df boolForKey:@"showTrackNumber"] ) {
706 trackNo = [[self currentRemote] currentSongTrack];
707 trackCount = [[self currentRemote] currentAlbumTrackCount];
709 [self networkError:localException];
712 if ( (trackNo > 0) || (trackCount > 0) ) {
713 track = [NSString stringWithFormat:@"%@: %i %@ %i",
714 @"Track", trackNo, @"of", trackCount];
718 if ( [df boolForKey:@"showTrackRating"] ) {
719 float currentRating = 0;
722 currentRating = [[self currentRemote] currentSongRating];
724 [self networkError:localException];
727 if (currentRating >= 0.0) {
728 rating = ( currentRating * 5 );
733 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
736 [statusWindowController showSongInfoWindowWithSource:source
745 - (void)showUpcomingSongs
749 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
751 [self networkError:localException];
754 ITDebugLog(@"Showing upcoming songs status window.");
757 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
758 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
759 int curTrack = [[self currentRemote] currentSongIndex];
762 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
764 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
768 if ([songList count] == 0) {
769 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
772 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
774 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
777 [self networkError:localException];
781 - (void)incrementVolume
784 float volume = [[self currentRemote] volume];
785 float dispVol = volume;
786 ITDebugLog(@"Incrementing volume.");
795 ITDebugLog(@"Setting volume to %f", volume);
796 [[self currentRemote] setVolume:volume];
798 // Show volume status window
799 [statusWindowController showVolumeWindowWithLevel:dispVol];
801 [self networkError:localException];
805 - (void)decrementVolume
808 float volume = [[self currentRemote] volume];
809 float dispVol = volume;
810 ITDebugLog(@"Decrementing volume.");
819 ITDebugLog(@"Setting volume to %f", volume);
820 [[self currentRemote] setVolume:volume];
822 //Show volume status window
823 [statusWindowController showVolumeWindowWithLevel:dispVol];
825 [self networkError:localException];
829 - (void)incrementRating
832 float rating = [[self currentRemote] currentSongRating];
833 ITDebugLog(@"Incrementing rating.");
835 if ([[self currentRemote] currentPlaylistIndex] == 0) {
836 ITDebugLog(@"No song playing, rating change aborted.");
844 ITDebugLog(@"Setting rating to %f", rating);
845 [[self currentRemote] setCurrentSongRating:rating];
847 //Show rating status window
848 [statusWindowController showRatingWindowWithRating:rating];
850 [self networkError:localException];
854 - (void)decrementRating
857 float rating = [[self currentRemote] currentSongRating];
858 ITDebugLog(@"Decrementing rating.");
860 if ([[self currentRemote] currentPlaylistIndex] == 0) {
861 ITDebugLog(@"No song playing, rating change aborted.");
869 ITDebugLog(@"Setting rating to %f", rating);
870 [[self currentRemote] setCurrentSongRating:rating];
872 //Show rating status window
873 [statusWindowController showRatingWindowWithRating:rating];
875 [self networkError:localException];
882 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
883 ITDebugLog(@"Toggling repeat mode.");
884 switch (repeatMode) {
885 case ITMTRemotePlayerRepeatOff:
886 repeatMode = ITMTRemotePlayerRepeatAll;
888 case ITMTRemotePlayerRepeatAll:
889 repeatMode = ITMTRemotePlayerRepeatOne;
891 case ITMTRemotePlayerRepeatOne:
892 repeatMode = ITMTRemotePlayerRepeatOff;
895 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
896 [[self currentRemote] setRepeatMode:repeatMode];
898 //Show loop status window
899 [statusWindowController showRepeatWindowWithMode:repeatMode];
901 [self networkError:localException];
905 - (void)toggleShuffle
908 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
909 ITDebugLog(@"Toggling shuffle mode.");
910 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
911 //Show shuffle status window
912 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
913 [statusWindowController showShuffleWindow:newShuffleEnabled];
915 [self networkError:localException];
919 - (void)registerNowOK
921 [[StatusWindow sharedWindow] setLocked:NO];
922 [[StatusWindow sharedWindow] vanish:self];
923 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
928 - (void)registerNowCancel
930 [[StatusWindow sharedWindow] setLocked:NO];
931 [[StatusWindow sharedWindow] vanish:self];
932 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
934 [NSApp terminate:self];
937 /*************************************************************************/
939 #pragma mark NETWORK HANDLERS
940 /*************************************************************************/
942 - (void)setServerStatus:(BOOL)newStatus
946 [networkController setServerStatus:YES];
949 [networkController setServerStatus:NO];
953 - (int)connectToServer
956 ITDebugLog(@"Attempting to connect to shared remote.");
957 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
960 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
961 currentRemote = [[[networkController networkObject] remote] retain];
962 [refreshTimer invalidate];
963 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
965 selector:@selector(timerUpdate)
967 repeats:YES] retain];
969 ITDebugLog(@"Connection successful.");
971 } else if (result == 0) {
972 ITDebugLog(@"Connection failed.");
973 currentRemote = [remoteArray objectAtIndex:0];
976 //Do something about the password being invalid
977 ITDebugLog(@"Connection failed.");
978 currentRemote = [remoteArray objectAtIndex:0];
983 - (BOOL)disconnectFromServer
985 ITDebugLog(@"Disconnecting from shared remote.");
987 [currentRemote release];
988 currentRemote = [remoteArray objectAtIndex:0];
989 [networkController disconnect];
994 - (void)checkForRemoteServer:(NSTimer *)timer
996 ITDebugLog(@"Checking for remote server.");
997 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
998 ITDebugLog(@"Remote server found.");
1000 if (![networkController isConnectedToServer]) {
1001 [[StatusWindowController sharedController] showReconnectQueryWindow];
1004 ITDebugLog(@"Remote server not found.");
1008 - (void)networkError:(NSException *)exception
1010 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1011 NSLog(@"%@", [exception reason]);
1012 if ([[exception name] isEqualToString:NSPortTimeoutException] && [networkController isConnectedToServer]) {
1013 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);
1014 if ([self disconnectFromServer]) {
1015 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1016 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1018 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1025 if ([self connectToServer] == 0) {
1026 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1028 [[StatusWindow sharedWindow] setLocked:NO];
1029 [[StatusWindow sharedWindow] vanish:self];
1030 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1033 - (void)cancelReconnect
1035 [[StatusWindow sharedWindow] setLocked:NO];
1036 [[StatusWindow sharedWindow] vanish:self];
1037 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1040 /*************************************************************************/
1042 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1043 /*************************************************************************/
1045 - (void)applicationLaunched:(NSNotification *)note
1048 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1049 ITDebugLog(@"Remote application launched.");
1050 playerRunningState = ITMTRemotePlayerRunning;
1051 [[self currentRemote] begin];
1052 [self setLatestSongIdentifier:@""];
1054 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1056 selector:@selector(timerUpdate)
1058 repeats:YES] retain];
1059 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1060 [self setupHotKeys];
1063 [self networkError:localException];
1067 - (void)applicationTerminated:(NSNotification *)note
1070 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1071 ITDebugLog(@"Remote application terminated.");
1072 [[self currentRemote] halt];
1073 [refreshTimer invalidate];
1074 [refreshTimer release];
1076 [self clearHotKeys];
1077 playerRunningState = ITMTRemotePlayerNotRunning;
1079 if ([df objectForKey:@"ShowPlayer"] != nil) {
1081 ITDebugLog(@"Setting up show player hot key.");
1082 hotKey = [[ITHotKey alloc] init];
1083 [hotKey setName:@"ShowPlayer"];
1084 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1085 [hotKey setTarget:self];
1086 [hotKey setAction:@selector(showPlayer)];
1087 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1091 [self networkError:localException];
1096 /*************************************************************************/
1098 #pragma mark NSApplication DELEGATE METHODS
1099 /*************************************************************************/
1101 - (void)applicationWillTerminate:(NSNotification *)note
1103 [networkController stopRemoteServerSearch];
1104 [self clearHotKeys];
1105 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1109 /*************************************************************************/
1111 #pragma mark DEALLOCATION METHOD
1112 /*************************************************************************/
1116 [self applicationTerminated:nil];
1118 [statusItem release];
1119 [statusWindowController release];
1120 [menuController release];
1121 [networkController release];