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 if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) {
59 [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
61 [[PreferencesController sharedPrefs] registerDefaults];
62 [[StatusWindowController sharedController] showPreferencesUpdateWindow];
65 currentRemote = [self loadRemote];
66 [[self currentRemote] begin];
68 //Turn on network stuff if needed
69 networkController = [[NetworkController alloc] init];
70 if ([df boolForKey:@"enableSharing"]) {
71 [self setServerStatus:YES];
72 } else if ([df boolForKey:@"useSharedPlayer"]) {
73 if ([self connectToServer] == 0) {
74 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
78 //Setup for notification of the remote player launching or quitting
79 [[[NSWorkspace sharedWorkspace] notificationCenter]
81 selector:@selector(applicationTerminated:)
82 name:NSWorkspaceDidTerminateApplicationNotification
85 [[[NSWorkspace sharedWorkspace] notificationCenter]
87 selector:@selector(applicationLaunched:)
88 name:NSWorkspaceDidLaunchApplicationNotification
91 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
92 [[PreferencesController sharedPrefs] registerDefaults];
95 [StatusItemHack install];
96 statusItem = [[ITStatusItem alloc]
97 initWithStatusBar:[NSStatusBar systemStatusBar]
98 withLength:NSSquareStatusItemLength];
100 bling = [[MTBlingController alloc] init];
102 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
104 selector:@selector(blingTime)
106 repeats:YES] retain];
109 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
110 [self applicationLaunched:nil];
112 if ([df boolForKey:@"LaunchPlayerWithMT"])
115 [self applicationTerminated:nil];
118 [self networkError:localException];
121 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
122 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
124 [networkController startRemoteServerSearch];
128 - (ITMTRemote *)loadRemote
130 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
131 ITDebugLog(@"Gathering remotes.");
133 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
134 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
135 NSString *bundlePath;
137 while ( (bundlePath = [enumerator nextObject]) ) {
138 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
141 Class remoteClass = [remoteBundle principalClass];
143 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
144 [remoteClass isKindOfClass:[NSObject class]]) {
145 id remote = [remoteClass remote];
146 ITDebugLog(@"Adding remote at path %@", bundlePath);
147 [remoteArray addObject:remote];
152 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
153 // if ( [remoteArray count] > 1 ) {
154 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
156 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
159 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
160 return [remoteArray objectAtIndex:0];
163 /*************************************************************************/
165 #pragma mark INSTANCE METHODS
166 /*************************************************************************/
168 /*- (void)startTimerInNewThread
170 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
171 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
172 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
174 selector:@selector(timerUpdate)
176 repeats:YES] retain];
178 ITDebugLog(@"Timer started.");
182 - (void)setBlingTime:(NSDate*)date
184 NSMutableDictionary *globalPrefs;
186 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
188 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
190 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
192 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
194 [globalPrefs release];
197 - (NSDate*)getBlingTime
200 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
205 NSDate *now = [NSDate date];
206 if (![self blingBling]) {
207 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
208 [self setBlingTime:now];
210 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
212 [statusItem setEnabled:NO];
214 if ([refreshTimer isValid]) {
215 [refreshTimer invalidate];
217 [statusWindowController showRegistrationQueryWindow];
221 [statusItem setEnabled:YES];
223 if (![refreshTimer isValid]) {
224 [refreshTimer release];
225 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
227 selector:@selector(timerUpdate)
229 repeats:YES] retain];
233 [self setBlingTime:nil];
244 if ( ! ([bling checkDone] == 2475) ) {
251 - (BOOL)songIsPlaying
253 NSString *identifier = nil;
255 identifier = [[self currentRemote] playerStateUniqueIdentifier];
257 [self networkError:localException];
259 return ( ! ([identifier isEqualToString:@"0-0"]) );
262 - (BOOL)radioIsPlaying
264 ITMTRemotePlayerPlaylistClass class = nil;
266 class = [[self currentRemote] currentPlaylistClass];
268 [self networkError:localException];
270 return (class == ITMTRemotePlayerRadioPlaylist );
275 NSString *identifier = nil;
277 identifier = [[self currentRemote] playerStateUniqueIdentifier];
279 [self networkError:localException];
281 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
284 - (NSString *)latestSongIdentifier
286 return _latestSongIdentifier;
289 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
291 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
292 [_latestSongIdentifier autorelease];
293 _latestSongIdentifier = [newIdentifier copy];
298 if ([networkController isConnectedToServer]) {
299 [statusItem setMenu:[menuController menu]];
302 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
303 ITDebugLog(@"The song changed.");
307 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
308 [menuController rebuildSubmenus];
310 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
311 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
314 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
316 [self networkError:localException];
325 ITDebugLog(@"Menu clicked.");
326 if ([networkController isConnectedToServer]) {
327 //Used the cached version
332 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
333 [statusItem setMenu:[menuController menu]];
335 [statusItem setMenu:[menuController menuForNoPlayer]];
338 [self networkError:localException];
351 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
352 ITDebugLog(@"Play/Pause toggled");
353 if (state == ITMTRemotePlayerPlaying) {
354 [[self currentRemote] pause];
355 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
356 [[self currentRemote] pause];
357 [[self currentRemote] play];
359 [[self currentRemote] play];
362 [self networkError:localException];
370 ITDebugLog(@"Going to next song.");
372 [[self currentRemote] goToNextSong];
374 [self networkError:localException];
381 ITDebugLog(@"Going to previous song.");
383 [[self currentRemote] goToPreviousSong];
385 [self networkError:localException];
392 ITDebugLog(@"Fast forwarding.");
394 [[self currentRemote] forward];
396 [self networkError:localException];
403 ITDebugLog(@"Rewinding.");
405 [[self currentRemote] rewind];
407 [self networkError:localException];
412 - (void)selectPlaylistAtIndex:(int)index
414 ITDebugLog(@"Selecting playlist %i", index);
416 //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
417 [[self currentRemote] switchToPlaylistAtIndex:index];
419 [self networkError:localException];
424 - (void)selectSongAtIndex:(int)index
426 ITDebugLog(@"Selecting song %i", index);
428 [[self currentRemote] switchToSongAtIndex:index];
430 [self networkError:localException];
435 - (void)selectSongRating:(int)rating
437 ITDebugLog(@"Selecting song rating %i", rating);
439 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
441 [self networkError:localException];
446 - (void)selectEQPresetAtIndex:(int)index
448 ITDebugLog(@"Selecting EQ preset %i", index);
450 [[self currentRemote] switchToEQAtIndex:index];
452 [self networkError:localException];
459 ITDebugLog(@"Beginning show player.");
460 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
461 ITDebugLog(@"Showing player interface.");
463 [[self currentRemote] showPrimaryInterface];
465 [self networkError:localException];
468 ITDebugLog(@"Launching player.");
470 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
471 ITDebugLog(@"Error Launching Player");
474 [self networkError:localException];
477 ITDebugLog(@"Finished show player.");
480 - (void)showPreferences
482 ITDebugLog(@"Show preferences.");
483 [[PreferencesController sharedPrefs] showPrefsWindow:self];
486 - (void)showPreferencesAndClose
488 ITDebugLog(@"Show preferences.");
489 [[PreferencesController sharedPrefs] showPrefsWindow:self];
490 [[StatusWindow sharedWindow] setLocked:NO];
491 [[StatusWindow sharedWindow] vanish:self];
492 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
495 - (void)showTestWindow
497 [self showCurrentTrackInfo];
500 - (void)quitMenuTunes
502 ITDebugLog(@"Quitting MenuTunes.");
503 [NSApp terminate:self];
509 - (void)closePreferences
511 ITDebugLog(@"Preferences closed.");
512 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
517 - (ITMTRemote *)currentRemote
519 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
520 [self networkError:nil];
523 return currentRemote;
534 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
535 ITHotKey *nextHotKey;
536 ITDebugLog(@"Clearing hot keys.");
537 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
538 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
540 ITDebugLog(@"Done clearing hot keys.");
546 ITDebugLog(@"Setting up hot keys.");
548 if (playerRunningState == ITMTRemotePlayerNotRunning) {
552 if ([df objectForKey:@"PlayPause"] != nil) {
553 ITDebugLog(@"Setting up play pause hot key.");
554 hotKey = [[ITHotKey alloc] init];
555 [hotKey setName:@"PlayPause"];
556 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
557 [hotKey setTarget:self];
558 [hotKey setAction:@selector(playPause)];
559 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
562 if ([df objectForKey:@"NextTrack"] != nil) {
563 ITDebugLog(@"Setting up next track hot key.");
564 hotKey = [[ITHotKey alloc] init];
565 [hotKey setName:@"NextTrack"];
566 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
567 [hotKey setTarget:self];
568 [hotKey setAction:@selector(nextSong)];
569 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
572 if ([df objectForKey:@"PrevTrack"] != nil) {
573 ITDebugLog(@"Setting up previous track hot key.");
574 hotKey = [[ITHotKey alloc] init];
575 [hotKey setName:@"PrevTrack"];
576 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
577 [hotKey setTarget:self];
578 [hotKey setAction:@selector(prevSong)];
579 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
582 if ([df objectForKey:@"ShowPlayer"] != nil) {
583 ITDebugLog(@"Setting up show player hot key.");
584 hotKey = [[ITHotKey alloc] init];
585 [hotKey setName:@"ShowPlayer"];
586 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
587 [hotKey setTarget:self];
588 [hotKey setAction:@selector(showPlayer)];
589 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
592 if ([df objectForKey:@"TrackInfo"] != nil) {
593 ITDebugLog(@"Setting up track info hot key.");
594 hotKey = [[ITHotKey alloc] init];
595 [hotKey setName:@"TrackInfo"];
596 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
597 [hotKey setTarget:self];
598 [hotKey setAction:@selector(showCurrentTrackInfo)];
599 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
602 if ([df objectForKey:@"UpcomingSongs"] != nil) {
603 ITDebugLog(@"Setting up upcoming songs hot key.");
604 hotKey = [[ITHotKey alloc] init];
605 [hotKey setName:@"UpcomingSongs"];
606 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
607 [hotKey setTarget:self];
608 [hotKey setAction:@selector(showUpcomingSongs)];
609 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
612 if ([df objectForKey:@"ToggleLoop"] != nil) {
613 ITDebugLog(@"Setting up toggle loop hot key.");
614 hotKey = [[ITHotKey alloc] init];
615 [hotKey setName:@"ToggleLoop"];
616 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
617 [hotKey setTarget:self];
618 [hotKey setAction:@selector(toggleLoop)];
619 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
622 if ([df objectForKey:@"ToggleShuffle"] != nil) {
623 ITDebugLog(@"Setting up toggle shuffle hot key.");
624 hotKey = [[ITHotKey alloc] init];
625 [hotKey setName:@"ToggleShuffle"];
626 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
627 [hotKey setTarget:self];
628 [hotKey setAction:@selector(toggleShuffle)];
629 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
632 if ([df objectForKey:@"IncrementVolume"] != nil) {
633 ITDebugLog(@"Setting up increment volume hot key.");
634 hotKey = [[ITHotKey alloc] init];
635 [hotKey setName:@"IncrementVolume"];
636 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
637 [hotKey setTarget:self];
638 [hotKey setAction:@selector(incrementVolume)];
639 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
642 if ([df objectForKey:@"DecrementVolume"] != nil) {
643 ITDebugLog(@"Setting up decrement volume hot key.");
644 hotKey = [[ITHotKey alloc] init];
645 [hotKey setName:@"DecrementVolume"];
646 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
647 [hotKey setTarget:self];
648 [hotKey setAction:@selector(decrementVolume)];
649 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
652 if ([df objectForKey:@"IncrementRating"] != nil) {
653 ITDebugLog(@"Setting up increment rating hot key.");
654 hotKey = [[ITHotKey alloc] init];
655 [hotKey setName:@"IncrementRating"];
656 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
657 [hotKey setTarget:self];
658 [hotKey setAction:@selector(incrementRating)];
659 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
662 if ([df objectForKey:@"DecrementRating"] != nil) {
663 ITDebugLog(@"Setting up decrement rating hot key.");
664 hotKey = [[ITHotKey alloc] init];
665 [hotKey setName:@"DecrementRating"];
666 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
667 [hotKey setTarget:self];
668 [hotKey setAction:@selector(decrementRating)];
669 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
671 ITDebugLog(@"Finished setting up hot keys.");
674 - (void)showCurrentTrackInfo
676 ITMTRemotePlayerSource source = 0;
677 NSString *title = nil;
678 NSString *album = nil;
679 NSString *artist = nil;
680 NSString *time = nil;
681 NSString *track = nil;
685 source = [[self currentRemote] currentSource];
686 title = [[self currentRemote] currentSongTitle];
688 [self networkError:localException];
691 ITDebugLog(@"Showing track info status window.");
695 if ( [df boolForKey:@"showAlbum"] ) {
697 album = [[self currentRemote] currentSongAlbum];
699 [self networkError:localException];
703 if ( [df boolForKey:@"showArtist"] ) {
705 artist = [[self currentRemote] currentSongArtist];
707 [self networkError:localException];
711 if ( [df boolForKey:@"showTime"] ) {
713 time = [NSString stringWithFormat:@"%@: %@ / %@",
715 [[self currentRemote] currentSongElapsed],
716 [[self currentRemote] currentSongLength]];
718 [self networkError:localException];
722 if ( [df boolForKey:@"showTrackNumber"] ) {
727 trackNo = [[self currentRemote] currentSongTrack];
728 trackCount = [[self currentRemote] currentAlbumTrackCount];
730 [self networkError:localException];
733 if ( (trackNo > 0) || (trackCount > 0) ) {
734 track = [NSString stringWithFormat:@"%@: %i %@ %i",
735 @"Track", trackNo, @"of", trackCount];
739 if ( [df boolForKey:@"showTrackRating"] ) {
740 float currentRating = 0;
743 currentRating = [[self currentRemote] currentSongRating];
745 [self networkError:localException];
748 if (currentRating >= 0.0) {
749 rating = ( currentRating * 5 );
754 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
757 [statusWindowController showSongInfoWindowWithSource:source
766 - (void)showUpcomingSongs
770 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
772 [self networkError:localException];
775 ITDebugLog(@"Showing upcoming songs status window.");
778 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
779 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
780 int curTrack = [[self currentRemote] currentSongIndex];
783 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
785 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
789 if ([songList count] == 0) {
790 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
793 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
795 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
798 [self networkError:localException];
802 - (void)incrementVolume
805 float volume = [[self currentRemote] volume];
806 float dispVol = volume;
807 ITDebugLog(@"Incrementing volume.");
816 ITDebugLog(@"Setting volume to %f", volume);
817 [[self currentRemote] setVolume:volume];
819 // Show volume status window
820 [statusWindowController showVolumeWindowWithLevel:dispVol];
822 [self networkError:localException];
826 - (void)decrementVolume
829 float volume = [[self currentRemote] volume];
830 float dispVol = volume;
831 ITDebugLog(@"Decrementing volume.");
840 ITDebugLog(@"Setting volume to %f", volume);
841 [[self currentRemote] setVolume:volume];
843 //Show volume status window
844 [statusWindowController showVolumeWindowWithLevel:dispVol];
846 [self networkError:localException];
850 - (void)incrementRating
853 float rating = [[self currentRemote] currentSongRating];
854 ITDebugLog(@"Incrementing rating.");
856 if ([[self currentRemote] currentPlaylistIndex] == 0) {
857 ITDebugLog(@"No song playing, rating change aborted.");
865 ITDebugLog(@"Setting rating to %f", rating);
866 [[self currentRemote] setCurrentSongRating:rating];
868 //Show rating status window
869 [statusWindowController showRatingWindowWithRating:rating];
871 [self networkError:localException];
875 - (void)decrementRating
878 float rating = [[self currentRemote] currentSongRating];
879 ITDebugLog(@"Decrementing rating.");
881 if ([[self currentRemote] currentPlaylistIndex] == 0) {
882 ITDebugLog(@"No song playing, rating change aborted.");
890 ITDebugLog(@"Setting rating to %f", rating);
891 [[self currentRemote] setCurrentSongRating:rating];
893 //Show rating status window
894 [statusWindowController showRatingWindowWithRating:rating];
896 [self networkError:localException];
903 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
904 ITDebugLog(@"Toggling repeat mode.");
905 switch (repeatMode) {
906 case ITMTRemotePlayerRepeatOff:
907 repeatMode = ITMTRemotePlayerRepeatAll;
909 case ITMTRemotePlayerRepeatAll:
910 repeatMode = ITMTRemotePlayerRepeatOne;
912 case ITMTRemotePlayerRepeatOne:
913 repeatMode = ITMTRemotePlayerRepeatOff;
916 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
917 [[self currentRemote] setRepeatMode:repeatMode];
919 //Show loop status window
920 [statusWindowController showRepeatWindowWithMode:repeatMode];
922 [self networkError:localException];
926 - (void)toggleShuffle
929 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
930 ITDebugLog(@"Toggling shuffle mode.");
931 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
932 //Show shuffle status window
933 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
934 [statusWindowController showShuffleWindow:newShuffleEnabled];
936 [self networkError:localException];
940 - (void)registerNowOK
942 [[StatusWindow sharedWindow] setLocked:NO];
943 [[StatusWindow sharedWindow] vanish:self];
944 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
949 - (void)registerNowCancel
951 [[StatusWindow sharedWindow] setLocked:NO];
952 [[StatusWindow sharedWindow] vanish:self];
953 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
955 [NSApp terminate:self];
958 /*************************************************************************/
960 #pragma mark NETWORK HANDLERS
961 /*************************************************************************/
963 - (void)setServerStatus:(BOOL)newStatus
967 [networkController setServerStatus:YES];
970 [networkController setServerStatus:NO];
974 - (int)connectToServer
977 ITDebugLog(@"Attempting to connect to shared remote.");
978 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
981 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
982 currentRemote = [[[networkController networkObject] remote] retain];
983 [refreshTimer invalidate];
984 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
986 selector:@selector(timerUpdate)
988 repeats:YES] retain];
990 ITDebugLog(@"Connection successful.");
992 } else if (result == 0) {
993 ITDebugLog(@"Connection failed.");
994 currentRemote = [remoteArray objectAtIndex:0];
997 //Do something about the password being invalid
998 ITDebugLog(@"Connection failed.");
999 currentRemote = [remoteArray objectAtIndex:0];
1004 - (BOOL)disconnectFromServer
1006 ITDebugLog(@"Disconnecting from shared remote.");
1008 [currentRemote release];
1009 currentRemote = [remoteArray objectAtIndex:0];
1010 [networkController disconnect];
1015 - (void)checkForRemoteServer:(NSTimer *)timer
1017 ITDebugLog(@"Checking for remote server.");
1018 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1019 ITDebugLog(@"Remote server found.");
1021 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1022 [[StatusWindowController sharedController] showReconnectQueryWindow];
1025 ITDebugLog(@"Remote server not found.");
1029 - (void)networkError:(NSException *)exception
1031 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1032 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1033 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);
1034 if ([self disconnectFromServer]) {
1035 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1036 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1038 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1045 if ([self connectToServer] == 0) {
1046 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1048 [[StatusWindow sharedWindow] setLocked:NO];
1049 [[StatusWindow sharedWindow] vanish:self];
1050 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1053 - (void)cancelReconnect
1055 [[StatusWindow sharedWindow] setLocked:NO];
1056 [[StatusWindow sharedWindow] vanish:self];
1057 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1060 /*************************************************************************/
1062 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1063 /*************************************************************************/
1065 - (void)applicationLaunched:(NSNotification *)note
1068 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1069 ITDebugLog(@"Remote application launched.");
1070 playerRunningState = ITMTRemotePlayerRunning;
1071 [[self currentRemote] begin];
1072 [self setLatestSongIdentifier:@""];
1074 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1076 selector:@selector(timerUpdate)
1078 repeats:YES] retain];
1079 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1080 [self setupHotKeys];
1083 [self networkError:localException];
1087 - (void)applicationTerminated:(NSNotification *)note
1090 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1091 ITDebugLog(@"Remote application terminated.");
1092 playerRunningState = ITMTRemotePlayerNotRunning;
1093 [[self currentRemote] halt];
1094 [refreshTimer invalidate];
1095 [refreshTimer release];
1097 [self clearHotKeys];
1099 if ([df objectForKey:@"ShowPlayer"] != nil) {
1101 ITDebugLog(@"Setting up show player hot key.");
1102 hotKey = [[ITHotKey alloc] init];
1103 [hotKey setName:@"ShowPlayer"];
1104 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1105 [hotKey setTarget:self];
1106 [hotKey setAction:@selector(showPlayer)];
1107 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1111 [self networkError:localException];
1116 /*************************************************************************/
1118 #pragma mark NSApplication DELEGATE METHODS
1119 /*************************************************************************/
1121 - (void)applicationWillTerminate:(NSNotification *)note
1123 [networkController stopRemoteServerSearch];
1124 [self clearHotKeys];
1125 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1129 /*************************************************************************/
1131 #pragma mark DEALLOCATION METHOD
1132 /*************************************************************************/
1136 [self applicationTerminated:nil];
1138 [statusItem release];
1139 [statusWindowController release];
1140 [menuController release];
1141 [networkController release];