1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import "NetworkController.h"
5 #import "NetworkObject.h"
6 #import <ITKit/ITHotKeyCenter.h>
7 #import <ITKit/ITHotKey.h>
8 #import <ITKit/ITKeyCombo.h>
9 #import "StatusWindow.h"
10 #import "StatusWindowController.h"
11 #import "StatusItemHack.h"
13 @interface MainController(Private)
14 - (ITMTRemote *)loadRemote;
16 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
17 - (void)showCurrentTrackInfo;
18 - (void)applicationLaunched:(NSNotification *)note;
19 - (void)applicationTerminated:(NSNotification *)note;
22 static MainController *sharedController;
24 @implementation MainController
26 + (MainController *)sharedController
28 return sharedController;
31 /*************************************************************************/
33 #pragma mark INITIALIZATION/DEALLOCATION METHODS
34 /*************************************************************************/
38 if ( ( self = [super init] ) ) {
39 sharedController = self;
41 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
42 statusWindowController = [StatusWindowController sharedController];
43 menuController = [[MenuController alloc] init];
44 df = [[NSUserDefaults standardUserDefaults] retain];
45 [[PreferencesController sharedPrefs] setController:self];
52 - (void)applicationDidFinishLaunching:(NSNotification *)note
54 //Turn on debug mode if needed
55 if ([df boolForKey:@"ITDebugMode"]) {
59 currentRemote = [self loadRemote];
60 [[self currentRemote] begin];
62 //Turn on network stuff if needed
63 networkController = [[NetworkController alloc] init];
64 if ([df boolForKey:@"enableSharing"]) {
65 [self setServerStatus:YES];
66 } else if ([df boolForKey:@"useSharedPlayer"]) {
67 if ([self connectToServer] == 0) {
68 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
72 //Setup for notification of the remote player launching or quitting
73 [[[NSWorkspace sharedWorkspace] notificationCenter]
75 selector:@selector(applicationTerminated:)
76 name:NSWorkspaceDidTerminateApplicationNotification
79 [[[NSWorkspace sharedWorkspace] notificationCenter]
81 selector:@selector(applicationLaunched:)
82 name:NSWorkspaceDidLaunchApplicationNotification
85 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
86 [[PreferencesController sharedPrefs] registerDefaults];
89 [StatusItemHack install];
90 statusItem = [[ITStatusItem alloc]
91 initWithStatusBar:[NSStatusBar systemStatusBar]
92 withLength:NSSquareStatusItemLength];
94 bling = [[MTBlingController alloc] init];
96 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
98 selector:@selector(blingTime)
100 repeats:YES] retain];
103 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
104 [self applicationLaunched:nil];
106 if ([df boolForKey:@"LaunchPlayerWithMT"])
109 [self applicationTerminated:nil];
112 [self networkError:localException];
115 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
116 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
118 [networkController startRemoteServerSearch];
122 - (ITMTRemote *)loadRemote
124 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
125 ITDebugLog(@"Gathering remotes.");
127 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
128 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
129 NSString *bundlePath;
131 while ( (bundlePath = [enumerator nextObject]) ) {
132 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
135 Class remoteClass = [remoteBundle principalClass];
137 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
138 [remoteClass isKindOfClass:[NSObject class]]) {
139 id remote = [remoteClass remote];
140 ITDebugLog(@"Adding remote at path %@", bundlePath);
141 [remoteArray addObject:remote];
146 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
147 // if ( [remoteArray count] > 1 ) {
148 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
150 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
153 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
154 return [remoteArray objectAtIndex:0];
157 /*************************************************************************/
159 #pragma mark INSTANCE METHODS
160 /*************************************************************************/
162 /*- (void)startTimerInNewThread
164 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
165 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
166 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
168 selector:@selector(timerUpdate)
170 repeats:YES] retain];
172 ITDebugLog(@"Timer started.");
176 - (void)setBlingTime:(NSDate*)date
178 NSMutableDictionary *globalPrefs;
180 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
182 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
184 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
186 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
188 [globalPrefs release];
191 - (NSDate*)getBlingTime
194 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
199 NSDate *now = [NSDate date];
200 if (![self blingBling]) {
201 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
202 [self setBlingTime:now];
204 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
206 [statusItem setEnabled:NO];
208 if ([refreshTimer isValid]) {
209 [refreshTimer invalidate];
211 [statusWindowController showRegistrationQueryWindow];
215 [statusItem setEnabled:YES];
217 if (![refreshTimer isValid]) {
218 [refreshTimer release];
219 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
221 selector:@selector(timerUpdate)
223 repeats:YES] retain];
227 [self setBlingTime:nil];
238 if ( ! ([bling checkDone] == 2475) ) {
245 - (BOOL)songIsPlaying
247 NSString *identifier = nil;
249 identifier = [[self currentRemote] playerStateUniqueIdentifier];
251 [self networkError:localException];
253 return ( ! ([identifier isEqualToString:@"0-0"]) );
256 - (BOOL)radioIsPlaying
258 ITMTRemotePlayerPlaylistClass class = nil;
260 class = [[self currentRemote] currentPlaylistClass];
262 [self networkError:localException];
264 return (class == ITMTRemotePlayerRadioPlaylist );
269 NSString *identifier = nil;
271 identifier = [[self currentRemote] playerStateUniqueIdentifier];
273 [self networkError:localException];
275 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
278 - (NSString *)latestSongIdentifier
280 return _latestSongIdentifier;
283 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
285 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
286 [_latestSongIdentifier autorelease];
287 _latestSongIdentifier = [newIdentifier copy];
292 if ([networkController isConnectedToServer]) {
293 [statusItem setMenu:[menuController menu]];
296 if ( [self songChanged] && (timerUpdating != YES) ) {
297 ITDebugLog(@"The song changed.");
301 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
302 [menuController rebuildSubmenus];
304 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
305 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
308 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
310 [self networkError:localException];
319 ITDebugLog(@"Menu clicked.");
320 if ([networkController isConnectedToServer]) {
321 //Used the cached version
326 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
327 [statusItem setMenu:[menuController menu]];
329 [statusItem setMenu:[menuController menuForNoPlayer]];
332 [self networkError:localException];
345 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
346 ITDebugLog(@"Play/Pause toggled");
347 if (state == ITMTRemotePlayerPlaying) {
348 [[self currentRemote] pause];
349 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
350 [[self currentRemote] pause];
351 [[self currentRemote] play];
353 [[self currentRemote] play];
356 [self networkError:localException];
364 ITDebugLog(@"Going to next song.");
366 [[self currentRemote] goToNextSong];
368 [self networkError:localException];
375 ITDebugLog(@"Going to previous song.");
377 [[self currentRemote] goToPreviousSong];
379 [self networkError:localException];
386 ITDebugLog(@"Fast forwarding.");
388 [[self currentRemote] forward];
390 [self networkError:localException];
397 ITDebugLog(@"Rewinding.");
399 [[self currentRemote] rewind];
401 [self networkError:localException];
406 - (void)selectPlaylistAtIndex:(int)index
408 ITDebugLog(@"Selecting playlist %i", index);
410 [[self currentRemote] switchToPlaylistAtIndex:index];
412 [self networkError:localException];
417 - (void)selectSongAtIndex:(int)index
419 ITDebugLog(@"Selecting song %i", index);
421 [[self currentRemote] switchToSongAtIndex:index];
423 [self networkError:localException];
428 - (void)selectSongRating:(int)rating
430 ITDebugLog(@"Selecting song rating %i", rating);
432 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
434 [self networkError:localException];
439 - (void)selectEQPresetAtIndex:(int)index
441 ITDebugLog(@"Selecting EQ preset %i", index);
443 [[self currentRemote] switchToEQAtIndex:index];
445 [self networkError:localException];
452 ITDebugLog(@"Beginning show player.");
453 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
454 ITDebugLog(@"Showing player interface.");
456 [[self currentRemote] showPrimaryInterface];
458 [self networkError:localException];
461 ITDebugLog(@"Launching player.");
463 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
464 ITDebugLog(@"Error Launching Player");
467 [self networkError:localException];
470 ITDebugLog(@"Finished show player.");
473 - (void)showPreferences
475 ITDebugLog(@"Show preferences.");
476 [[PreferencesController sharedPrefs] showPrefsWindow:self];
479 - (void)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 return currentRemote;
514 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
515 ITHotKey *nextHotKey;
516 ITDebugLog(@"Clearing hot keys.");
517 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
518 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
520 ITDebugLog(@"Done clearing hot keys.");
526 ITDebugLog(@"Setting up hot keys.");
528 if (playerRunningState == ITMTRemotePlayerNotRunning) {
532 if ([df objectForKey:@"PlayPause"] != nil) {
533 ITDebugLog(@"Setting up play pause hot key.");
534 hotKey = [[ITHotKey alloc] init];
535 [hotKey setName:@"PlayPause"];
536 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
537 [hotKey setTarget:self];
538 [hotKey setAction:@selector(playPause)];
539 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
542 if ([df objectForKey:@"NextTrack"] != nil) {
543 ITDebugLog(@"Setting up next track hot key.");
544 hotKey = [[ITHotKey alloc] init];
545 [hotKey setName:@"NextTrack"];
546 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
547 [hotKey setTarget:self];
548 [hotKey setAction:@selector(nextSong)];
549 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
552 if ([df objectForKey:@"PrevTrack"] != nil) {
553 ITDebugLog(@"Setting up previous track hot key.");
554 hotKey = [[ITHotKey alloc] init];
555 [hotKey setName:@"PrevTrack"];
556 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
557 [hotKey setTarget:self];
558 [hotKey setAction:@selector(prevSong)];
559 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
562 if ([df objectForKey:@"ShowPlayer"] != nil) {
563 ITDebugLog(@"Setting up show player hot key.");
564 hotKey = [[ITHotKey alloc] init];
565 [hotKey setName:@"ShowPlayer"];
566 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
567 [hotKey setTarget:self];
568 [hotKey setAction:@selector(showPlayer)];
569 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
572 if ([df objectForKey:@"TrackInfo"] != nil) {
573 ITDebugLog(@"Setting up track info hot key.");
574 hotKey = [[ITHotKey alloc] init];
575 [hotKey setName:@"TrackInfo"];
576 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
577 [hotKey setTarget:self];
578 [hotKey setAction:@selector(showCurrentTrackInfo)];
579 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
582 if ([df objectForKey:@"UpcomingSongs"] != nil) {
583 ITDebugLog(@"Setting up upcoming songs hot key.");
584 hotKey = [[ITHotKey alloc] init];
585 [hotKey setName:@"UpcomingSongs"];
586 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
587 [hotKey setTarget:self];
588 [hotKey setAction:@selector(showUpcomingSongs)];
589 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
592 if ([df objectForKey:@"ToggleLoop"] != nil) {
593 ITDebugLog(@"Setting up toggle loop hot key.");
594 hotKey = [[ITHotKey alloc] init];
595 [hotKey setName:@"ToggleLoop"];
596 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
597 [hotKey setTarget:self];
598 [hotKey setAction:@selector(toggleLoop)];
599 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
602 if ([df objectForKey:@"ToggleShuffle"] != nil) {
603 ITDebugLog(@"Setting up toggle shuffle hot key.");
604 hotKey = [[ITHotKey alloc] init];
605 [hotKey setName:@"ToggleShuffle"];
606 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
607 [hotKey setTarget:self];
608 [hotKey setAction:@selector(toggleShuffle)];
609 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
612 if ([df objectForKey:@"IncrementVolume"] != nil) {
613 ITDebugLog(@"Setting up increment volume hot key.");
614 hotKey = [[ITHotKey alloc] init];
615 [hotKey setName:@"IncrementVolume"];
616 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
617 [hotKey setTarget:self];
618 [hotKey setAction:@selector(incrementVolume)];
619 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
622 if ([df objectForKey:@"DecrementVolume"] != nil) {
623 ITDebugLog(@"Setting up decrement volume hot key.");
624 hotKey = [[ITHotKey alloc] init];
625 [hotKey setName:@"DecrementVolume"];
626 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
627 [hotKey setTarget:self];
628 [hotKey setAction:@selector(decrementVolume)];
629 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
632 if ([df objectForKey:@"IncrementRating"] != nil) {
633 ITDebugLog(@"Setting up increment rating hot key.");
634 hotKey = [[ITHotKey alloc] init];
635 [hotKey setName:@"IncrementRating"];
636 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
637 [hotKey setTarget:self];
638 [hotKey setAction:@selector(incrementRating)];
639 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
642 if ([df objectForKey:@"DecrementRating"] != nil) {
643 ITDebugLog(@"Setting up decrement rating hot key.");
644 hotKey = [[ITHotKey alloc] init];
645 [hotKey setName:@"DecrementRating"];
646 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
647 [hotKey setTarget:self];
648 [hotKey setAction:@selector(decrementRating)];
649 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
651 ITDebugLog(@"Finished setting up hot keys.");
654 - (void)showCurrentTrackInfo
656 ITMTRemotePlayerSource source = 0;
657 NSString *title = nil;
658 NSString *album = nil;
659 NSString *artist = nil;
660 NSString *time = nil;
661 NSString *track = nil;
665 source = [[self currentRemote] currentSource];
666 title = [[self currentRemote] currentSongTitle];
668 [self networkError:localException];
671 ITDebugLog(@"Showing track info status window.");
675 if ( [df boolForKey:@"showAlbum"] ) {
677 album = [[self currentRemote] currentSongAlbum];
679 [self networkError:localException];
683 if ( [df boolForKey:@"showArtist"] ) {
685 artist = [[self currentRemote] currentSongArtist];
687 [self networkError:localException];
691 if ( [df boolForKey:@"showTime"] ) {
693 time = [NSString stringWithFormat:@"%@: %@ / %@",
695 [[self currentRemote] currentSongElapsed],
696 [[self currentRemote] currentSongLength]];
698 [self networkError:localException];
702 if ( [df boolForKey:@"showTrackNumber"] ) {
707 trackNo = [[self currentRemote] currentSongTrack];
708 trackCount = [[self currentRemote] currentAlbumTrackCount];
710 [self networkError:localException];
713 if ( (trackNo > 0) || (trackCount > 0) ) {
714 track = [NSString stringWithFormat:@"%@: %i %@ %i",
715 @"Track", trackNo, @"of", trackCount];
719 if ( [df boolForKey:@"showTrackRating"] ) {
720 float currentRating = 0;
723 currentRating = [[self currentRemote] currentSongRating];
725 [self networkError:localException];
728 if (currentRating >= 0.0) {
729 rating = ( currentRating * 5 );
734 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
737 [statusWindowController showSongInfoWindowWithSource:source
746 - (void)showUpcomingSongs
750 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
752 [self networkError:localException];
755 ITDebugLog(@"Showing upcoming songs status window.");
758 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
759 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
760 int curTrack = [[self currentRemote] currentSongIndex];
763 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
765 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
769 if ([songList count] == 0) {
770 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
773 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
775 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
778 [self networkError:localException];
782 - (void)incrementVolume
785 float volume = [[self currentRemote] volume];
786 float dispVol = volume;
787 ITDebugLog(@"Incrementing volume.");
796 ITDebugLog(@"Setting volume to %f", volume);
797 [[self currentRemote] setVolume:volume];
799 // Show volume status window
800 [statusWindowController showVolumeWindowWithLevel:dispVol];
802 [self networkError:localException];
806 - (void)decrementVolume
809 float volume = [[self currentRemote] volume];
810 float dispVol = volume;
811 ITDebugLog(@"Decrementing volume.");
820 ITDebugLog(@"Setting volume to %f", volume);
821 [[self currentRemote] setVolume:volume];
823 //Show volume status window
824 [statusWindowController showVolumeWindowWithLevel:dispVol];
826 [self networkError:localException];
830 - (void)incrementRating
833 float rating = [[self currentRemote] currentSongRating];
834 ITDebugLog(@"Incrementing rating.");
836 if ([[self currentRemote] currentPlaylistIndex] == 0) {
837 ITDebugLog(@"No song playing, rating change aborted.");
845 ITDebugLog(@"Setting rating to %f", rating);
846 [[self currentRemote] setCurrentSongRating:rating];
848 //Show rating status window
849 [statusWindowController showRatingWindowWithRating:rating];
851 [self networkError:localException];
855 - (void)decrementRating
858 float rating = [[self currentRemote] currentSongRating];
859 ITDebugLog(@"Decrementing rating.");
861 if ([[self currentRemote] currentPlaylistIndex] == 0) {
862 ITDebugLog(@"No song playing, rating change aborted.");
870 ITDebugLog(@"Setting rating to %f", rating);
871 [[self currentRemote] setCurrentSongRating:rating];
873 //Show rating status window
874 [statusWindowController showRatingWindowWithRating:rating];
876 [self networkError:localException];
883 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
884 ITDebugLog(@"Toggling repeat mode.");
885 switch (repeatMode) {
886 case ITMTRemotePlayerRepeatOff:
887 repeatMode = ITMTRemotePlayerRepeatAll;
889 case ITMTRemotePlayerRepeatAll:
890 repeatMode = ITMTRemotePlayerRepeatOne;
892 case ITMTRemotePlayerRepeatOne:
893 repeatMode = ITMTRemotePlayerRepeatOff;
896 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
897 [[self currentRemote] setRepeatMode:repeatMode];
899 //Show loop status window
900 [statusWindowController showRepeatWindowWithMode:repeatMode];
902 [self networkError:localException];
906 - (void)toggleShuffle
909 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
910 ITDebugLog(@"Toggling shuffle mode.");
911 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
912 //Show shuffle status window
913 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
914 [statusWindowController showShuffleWindow:newShuffleEnabled];
916 [self networkError:localException];
920 - (void)registerNowOK
922 [[StatusWindow sharedWindow] setLocked:NO];
923 [[StatusWindow sharedWindow] vanish:self];
924 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
929 - (void)registerNowCancel
931 [[StatusWindow sharedWindow] setLocked:NO];
932 [[StatusWindow sharedWindow] vanish:self];
933 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
935 [NSApp terminate:self];
938 /*************************************************************************/
940 #pragma mark NETWORK HANDLERS
941 /*************************************************************************/
943 - (void)setServerStatus:(BOOL)newStatus
947 [networkController setServerStatus:YES];
950 [networkController setServerStatus:NO];
954 - (int)connectToServer
957 ITDebugLog(@"Attempting to connect to shared remote.");
958 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
961 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
962 currentRemote = [[[networkController networkObject] remote] retain];
964 ITDebugLog(@"Connection successful.");
966 } else if (result == 0) {
967 ITDebugLog(@"Connection failed.");
968 currentRemote = [remoteArray objectAtIndex:0];
971 //Do something about the password being invalid
972 ITDebugLog(@"Connection failed.");
973 currentRemote = [remoteArray objectAtIndex:0];
978 - (BOOL)disconnectFromServer
980 ITDebugLog(@"Disconnecting from shared remote.");
982 [currentRemote release];
983 currentRemote = [remoteArray objectAtIndex:0];
984 [networkController disconnect];
989 - (void)checkForRemoteServer:(NSTimer *)timer
991 ITDebugLog(@"Checking for remote server.");
992 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
993 ITDebugLog(@"Remote server found.");
995 if (![networkController isConnectedToServer]) {
996 [[StatusWindowController sharedController] showReconnectQueryWindow];
999 ITDebugLog(@"Remote server not found.");
1003 - (void)networkError:(NSException *)exception
1005 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1006 NSLog(@"%@", [exception reason]);
1007 if ([[exception name] isEqualToString:NSPortTimeoutException] && [networkController isConnectedToServer]) {
1008 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);
1009 if ([self disconnectFromServer]) {
1010 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1011 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1013 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1020 if ([self connectToServer] == 0) {
1021 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1023 [[StatusWindow sharedWindow] setLocked:NO];
1024 [[StatusWindow sharedWindow] vanish:self];
1025 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1028 - (void)cancelReconnect
1030 [[StatusWindow sharedWindow] setLocked:NO];
1031 [[StatusWindow sharedWindow] vanish:self];
1032 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1035 /*************************************************************************/
1037 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1038 /*************************************************************************/
1040 - (void)applicationLaunched:(NSNotification *)note
1043 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1044 ITDebugLog(@"Remote application launched.");
1045 playerRunningState = ITMTRemotePlayerRunning;
1046 [[self currentRemote] begin];
1047 [self setLatestSongIdentifier:@""];
1049 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1051 selector:@selector(timerUpdate)
1053 repeats:YES] retain];
1054 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1055 [self setupHotKeys];
1058 [self networkError:localException];
1062 - (void)applicationTerminated:(NSNotification *)note
1065 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1066 ITDebugLog(@"Remote application terminated.");
1067 [[self currentRemote] halt];
1068 [refreshTimer invalidate];
1069 [refreshTimer release];
1071 [self clearHotKeys];
1072 playerRunningState = ITMTRemotePlayerNotRunning;
1075 [self networkError:localException];
1080 /*************************************************************************/
1082 #pragma mark NSApplication DELEGATE METHODS
1083 /*************************************************************************/
1085 - (void)applicationWillTerminate:(NSNotification *)note
1087 [networkController stopRemoteServerSearch];
1088 [self clearHotKeys];
1089 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1093 /*************************************************************************/
1095 #pragma mark DEALLOCATION METHOD
1096 /*************************************************************************/
1100 [self applicationTerminated:nil];
1102 [statusItem release];
1103 [statusWindowController release];
1104 [menuController release];
1105 [networkController release];