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 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
503 [self networkError:nil];
506 return currentRemote;
517 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
518 ITHotKey *nextHotKey;
519 ITDebugLog(@"Clearing hot keys.");
520 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
521 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
523 ITDebugLog(@"Done clearing hot keys.");
529 ITDebugLog(@"Setting up hot keys.");
531 if (playerRunningState == ITMTRemotePlayerNotRunning) {
535 if ([df objectForKey:@"PlayPause"] != nil) {
536 ITDebugLog(@"Setting up play pause hot key.");
537 hotKey = [[ITHotKey alloc] init];
538 [hotKey setName:@"PlayPause"];
539 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
540 [hotKey setTarget:self];
541 [hotKey setAction:@selector(playPause)];
542 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
545 if ([df objectForKey:@"NextTrack"] != nil) {
546 ITDebugLog(@"Setting up next track hot key.");
547 hotKey = [[ITHotKey alloc] init];
548 [hotKey setName:@"NextTrack"];
549 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
550 [hotKey setTarget:self];
551 [hotKey setAction:@selector(nextSong)];
552 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
555 if ([df objectForKey:@"PrevTrack"] != nil) {
556 ITDebugLog(@"Setting up previous track hot key.");
557 hotKey = [[ITHotKey alloc] init];
558 [hotKey setName:@"PrevTrack"];
559 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
560 [hotKey setTarget:self];
561 [hotKey setAction:@selector(prevSong)];
562 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
565 if ([df objectForKey:@"ShowPlayer"] != nil) {
566 ITDebugLog(@"Setting up show player hot key.");
567 hotKey = [[ITHotKey alloc] init];
568 [hotKey setName:@"ShowPlayer"];
569 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
570 [hotKey setTarget:self];
571 [hotKey setAction:@selector(showPlayer)];
572 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
575 if ([df objectForKey:@"TrackInfo"] != nil) {
576 ITDebugLog(@"Setting up track info hot key.");
577 hotKey = [[ITHotKey alloc] init];
578 [hotKey setName:@"TrackInfo"];
579 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
580 [hotKey setTarget:self];
581 [hotKey setAction:@selector(showCurrentTrackInfo)];
582 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
585 if ([df objectForKey:@"UpcomingSongs"] != nil) {
586 ITDebugLog(@"Setting up upcoming songs hot key.");
587 hotKey = [[ITHotKey alloc] init];
588 [hotKey setName:@"UpcomingSongs"];
589 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
590 [hotKey setTarget:self];
591 [hotKey setAction:@selector(showUpcomingSongs)];
592 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
595 if ([df objectForKey:@"ToggleLoop"] != nil) {
596 ITDebugLog(@"Setting up toggle loop hot key.");
597 hotKey = [[ITHotKey alloc] init];
598 [hotKey setName:@"ToggleLoop"];
599 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
600 [hotKey setTarget:self];
601 [hotKey setAction:@selector(toggleLoop)];
602 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
605 if ([df objectForKey:@"ToggleShuffle"] != nil) {
606 ITDebugLog(@"Setting up toggle shuffle hot key.");
607 hotKey = [[ITHotKey alloc] init];
608 [hotKey setName:@"ToggleShuffle"];
609 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
610 [hotKey setTarget:self];
611 [hotKey setAction:@selector(toggleShuffle)];
612 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
615 if ([df objectForKey:@"IncrementVolume"] != nil) {
616 ITDebugLog(@"Setting up increment volume hot key.");
617 hotKey = [[ITHotKey alloc] init];
618 [hotKey setName:@"IncrementVolume"];
619 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
620 [hotKey setTarget:self];
621 [hotKey setAction:@selector(incrementVolume)];
622 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
625 if ([df objectForKey:@"DecrementVolume"] != nil) {
626 ITDebugLog(@"Setting up decrement volume hot key.");
627 hotKey = [[ITHotKey alloc] init];
628 [hotKey setName:@"DecrementVolume"];
629 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
630 [hotKey setTarget:self];
631 [hotKey setAction:@selector(decrementVolume)];
632 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
635 if ([df objectForKey:@"IncrementRating"] != nil) {
636 ITDebugLog(@"Setting up increment rating hot key.");
637 hotKey = [[ITHotKey alloc] init];
638 [hotKey setName:@"IncrementRating"];
639 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
640 [hotKey setTarget:self];
641 [hotKey setAction:@selector(incrementRating)];
642 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
645 if ([df objectForKey:@"DecrementRating"] != nil) {
646 ITDebugLog(@"Setting up decrement rating hot key.");
647 hotKey = [[ITHotKey alloc] init];
648 [hotKey setName:@"DecrementRating"];
649 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
650 [hotKey setTarget:self];
651 [hotKey setAction:@selector(decrementRating)];
652 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
654 ITDebugLog(@"Finished setting up hot keys.");
657 - (void)showCurrentTrackInfo
659 ITMTRemotePlayerSource source = 0;
660 NSString *title = nil;
661 NSString *album = nil;
662 NSString *artist = nil;
663 NSString *time = nil;
664 NSString *track = nil;
668 source = [[self currentRemote] currentSource];
669 title = [[self currentRemote] currentSongTitle];
671 [self networkError:localException];
674 ITDebugLog(@"Showing track info status window.");
678 if ( [df boolForKey:@"showAlbum"] ) {
680 album = [[self currentRemote] currentSongAlbum];
682 [self networkError:localException];
686 if ( [df boolForKey:@"showArtist"] ) {
688 artist = [[self currentRemote] currentSongArtist];
690 [self networkError:localException];
694 if ( [df boolForKey:@"showTime"] ) {
696 time = [NSString stringWithFormat:@"%@: %@ / %@",
698 [[self currentRemote] currentSongElapsed],
699 [[self currentRemote] currentSongLength]];
701 [self networkError:localException];
705 if ( [df boolForKey:@"showTrackNumber"] ) {
710 trackNo = [[self currentRemote] currentSongTrack];
711 trackCount = [[self currentRemote] currentAlbumTrackCount];
713 [self networkError:localException];
716 if ( (trackNo > 0) || (trackCount > 0) ) {
717 track = [NSString stringWithFormat:@"%@: %i %@ %i",
718 @"Track", trackNo, @"of", trackCount];
722 if ( [df boolForKey:@"showTrackRating"] ) {
723 float currentRating = 0;
726 currentRating = [[self currentRemote] currentSongRating];
728 [self networkError:localException];
731 if (currentRating >= 0.0) {
732 rating = ( currentRating * 5 );
737 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
740 [statusWindowController showSongInfoWindowWithSource:source
749 - (void)showUpcomingSongs
753 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
755 [self networkError:localException];
758 ITDebugLog(@"Showing upcoming songs status window.");
761 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
762 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
763 int curTrack = [[self currentRemote] currentSongIndex];
766 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
768 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
772 if ([songList count] == 0) {
773 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
776 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
778 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
781 [self networkError:localException];
785 - (void)incrementVolume
788 float volume = [[self currentRemote] volume];
789 float dispVol = volume;
790 ITDebugLog(@"Incrementing volume.");
799 ITDebugLog(@"Setting volume to %f", volume);
800 [[self currentRemote] setVolume:volume];
802 // Show volume status window
803 [statusWindowController showVolumeWindowWithLevel:dispVol];
805 [self networkError:localException];
809 - (void)decrementVolume
812 float volume = [[self currentRemote] volume];
813 float dispVol = volume;
814 ITDebugLog(@"Decrementing volume.");
823 ITDebugLog(@"Setting volume to %f", volume);
824 [[self currentRemote] setVolume:volume];
826 //Show volume status window
827 [statusWindowController showVolumeWindowWithLevel:dispVol];
829 [self networkError:localException];
833 - (void)incrementRating
836 float rating = [[self currentRemote] currentSongRating];
837 ITDebugLog(@"Incrementing rating.");
839 if ([[self currentRemote] currentPlaylistIndex] == 0) {
840 ITDebugLog(@"No song playing, rating change aborted.");
848 ITDebugLog(@"Setting rating to %f", rating);
849 [[self currentRemote] setCurrentSongRating:rating];
851 //Show rating status window
852 [statusWindowController showRatingWindowWithRating:rating];
854 [self networkError:localException];
858 - (void)decrementRating
861 float rating = [[self currentRemote] currentSongRating];
862 ITDebugLog(@"Decrementing rating.");
864 if ([[self currentRemote] currentPlaylistIndex] == 0) {
865 ITDebugLog(@"No song playing, rating change aborted.");
873 ITDebugLog(@"Setting rating to %f", rating);
874 [[self currentRemote] setCurrentSongRating:rating];
876 //Show rating status window
877 [statusWindowController showRatingWindowWithRating:rating];
879 [self networkError:localException];
886 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
887 ITDebugLog(@"Toggling repeat mode.");
888 switch (repeatMode) {
889 case ITMTRemotePlayerRepeatOff:
890 repeatMode = ITMTRemotePlayerRepeatAll;
892 case ITMTRemotePlayerRepeatAll:
893 repeatMode = ITMTRemotePlayerRepeatOne;
895 case ITMTRemotePlayerRepeatOne:
896 repeatMode = ITMTRemotePlayerRepeatOff;
899 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
900 [[self currentRemote] setRepeatMode:repeatMode];
902 //Show loop status window
903 [statusWindowController showRepeatWindowWithMode:repeatMode];
905 [self networkError:localException];
909 - (void)toggleShuffle
912 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
913 ITDebugLog(@"Toggling shuffle mode.");
914 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
915 //Show shuffle status window
916 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
917 [statusWindowController showShuffleWindow:newShuffleEnabled];
919 [self networkError:localException];
923 - (void)registerNowOK
925 [[StatusWindow sharedWindow] setLocked:NO];
926 [[StatusWindow sharedWindow] vanish:self];
927 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
932 - (void)registerNowCancel
934 [[StatusWindow sharedWindow] setLocked:NO];
935 [[StatusWindow sharedWindow] vanish:self];
936 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
938 [NSApp terminate:self];
941 /*************************************************************************/
943 #pragma mark NETWORK HANDLERS
944 /*************************************************************************/
946 - (void)setServerStatus:(BOOL)newStatus
950 [networkController setServerStatus:YES];
953 [networkController setServerStatus:NO];
957 - (int)connectToServer
960 ITDebugLog(@"Attempting to connect to shared remote.");
961 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
964 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
965 currentRemote = [[[networkController networkObject] remote] retain];
966 [refreshTimer invalidate];
967 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
969 selector:@selector(timerUpdate)
971 repeats:YES] retain];
973 ITDebugLog(@"Connection successful.");
975 } else if (result == 0) {
976 ITDebugLog(@"Connection failed.");
977 currentRemote = [remoteArray objectAtIndex:0];
980 //Do something about the password being invalid
981 ITDebugLog(@"Connection failed.");
982 currentRemote = [remoteArray objectAtIndex:0];
987 - (BOOL)disconnectFromServer
989 ITDebugLog(@"Disconnecting from shared remote.");
991 [currentRemote release];
992 currentRemote = [remoteArray objectAtIndex:0];
993 [networkController disconnect];
998 - (void)checkForRemoteServer:(NSTimer *)timer
1000 ITDebugLog(@"Checking for remote server.");
1001 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1002 ITDebugLog(@"Remote server found.");
1004 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1005 [[StatusWindowController sharedController] showReconnectQueryWindow];
1008 ITDebugLog(@"Remote server not found.");
1012 - (void)networkError:(NSException *)exception
1014 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1015 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1016 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);
1017 if ([self disconnectFromServer]) {
1018 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1019 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1021 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1028 if ([self connectToServer] == 0) {
1029 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1031 [[StatusWindow sharedWindow] setLocked:NO];
1032 [[StatusWindow sharedWindow] vanish:self];
1033 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1036 - (void)cancelReconnect
1038 [[StatusWindow sharedWindow] setLocked:NO];
1039 [[StatusWindow sharedWindow] vanish:self];
1040 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1043 /*************************************************************************/
1045 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1046 /*************************************************************************/
1048 - (void)applicationLaunched:(NSNotification *)note
1051 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1052 ITDebugLog(@"Remote application launched.");
1053 playerRunningState = ITMTRemotePlayerRunning;
1054 [[self currentRemote] begin];
1055 [self setLatestSongIdentifier:@""];
1057 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1059 selector:@selector(timerUpdate)
1061 repeats:YES] retain];
1062 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1063 [self setupHotKeys];
1066 [self networkError:localException];
1070 - (void)applicationTerminated:(NSNotification *)note
1073 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1074 ITDebugLog(@"Remote application terminated.");
1075 [[self currentRemote] halt];
1076 [refreshTimer invalidate];
1077 [refreshTimer release];
1079 [self clearHotKeys];
1080 playerRunningState = ITMTRemotePlayerNotRunning;
1082 if ([df objectForKey:@"ShowPlayer"] != nil) {
1084 ITDebugLog(@"Setting up show player hot key.");
1085 hotKey = [[ITHotKey alloc] init];
1086 [hotKey setName:@"ShowPlayer"];
1087 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1088 [hotKey setTarget:self];
1089 [hotKey setAction:@selector(showPlayer)];
1090 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1094 [self networkError:localException];
1099 /*************************************************************************/
1101 #pragma mark NSApplication DELEGATE METHODS
1102 /*************************************************************************/
1104 - (void)applicationWillTerminate:(NSNotification *)note
1106 [networkController stopRemoteServerSearch];
1107 [self clearHotKeys];
1108 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1112 /*************************************************************************/
1114 #pragma mark DEALLOCATION METHOD
1115 /*************************************************************************/
1119 [self applicationTerminated:nil];
1121 [statusItem release];
1122 [statusWindowController release];
1123 [menuController release];
1124 [networkController release];