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;
686 source = [[self currentRemote] currentSource];
687 title = [[self currentRemote] currentSongTitle];
689 [self networkError:localException];
692 ITDebugLog(@"Showing track info status window.");
696 if ( [df boolForKey:@"showAlbum"] ) {
698 album = [[self currentRemote] currentSongAlbum];
700 [self networkError:localException];
704 if ( [df boolForKey:@"showArtist"] ) {
706 artist = [[self currentRemote] currentSongArtist];
708 [self networkError:localException];
712 if ( [df boolForKey:@"showTime"] ) {
714 time = [NSString stringWithFormat:@"%@: %@ / %@",
716 [[self currentRemote] currentSongElapsed],
717 [[self currentRemote] currentSongLength]];
719 [self networkError:localException];
723 if ( [df boolForKey:@"showTrackNumber"] ) {
728 trackNo = [[self currentRemote] currentSongTrack];
729 trackCount = [[self currentRemote] currentAlbumTrackCount];
731 [self networkError:localException];
734 if ( (trackNo > 0) || (trackCount > 0) ) {
735 track = [NSString stringWithFormat:@"%@: %i %@ %i",
736 @"Track", trackNo, @"of", trackCount];
740 if ( [df boolForKey:@"showTrackRating"] ) {
741 float currentRating = 0;
744 currentRating = [[self currentRemote] currentSongRating];
746 [self networkError:localException];
749 if (currentRating >= 0.0) {
750 rating = ( currentRating * 5 );
754 if ( [df boolForKey:@"showAlbumArtwork"] ) {
756 art = [[self currentRemote] currentSongAlbumArt];
758 [self networkError:localException];
763 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
766 [statusWindowController showSongInfoWindowWithSource:source
776 - (void)showUpcomingSongs
780 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
782 [self networkError:localException];
785 ITDebugLog(@"Showing upcoming songs status window.");
788 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
789 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
790 int curTrack = [[self currentRemote] currentSongIndex];
793 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
795 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
799 if ([songList count] == 0) {
800 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
803 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
805 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
808 [self networkError:localException];
812 - (void)incrementVolume
815 float volume = [[self currentRemote] volume];
816 float dispVol = volume;
817 ITDebugLog(@"Incrementing volume.");
826 ITDebugLog(@"Setting volume to %f", volume);
827 [[self currentRemote] setVolume:volume];
829 // Show volume status window
830 [statusWindowController showVolumeWindowWithLevel:dispVol];
832 [self networkError:localException];
836 - (void)decrementVolume
839 float volume = [[self currentRemote] volume];
840 float dispVol = volume;
841 ITDebugLog(@"Decrementing volume.");
850 ITDebugLog(@"Setting volume to %f", volume);
851 [[self currentRemote] setVolume:volume];
853 //Show volume status window
854 [statusWindowController showVolumeWindowWithLevel:dispVol];
856 [self networkError:localException];
860 - (void)incrementRating
863 float rating = [[self currentRemote] currentSongRating];
864 ITDebugLog(@"Incrementing rating.");
866 if ([[self currentRemote] currentPlaylistIndex] == 0) {
867 ITDebugLog(@"No song playing, rating change aborted.");
875 ITDebugLog(@"Setting rating to %f", rating);
876 [[self currentRemote] setCurrentSongRating:rating];
878 //Show rating status window
879 [statusWindowController showRatingWindowWithRating:rating];
881 [self networkError:localException];
885 - (void)decrementRating
888 float rating = [[self currentRemote] currentSongRating];
889 ITDebugLog(@"Decrementing rating.");
891 if ([[self currentRemote] currentPlaylistIndex] == 0) {
892 ITDebugLog(@"No song playing, rating change aborted.");
900 ITDebugLog(@"Setting rating to %f", rating);
901 [[self currentRemote] setCurrentSongRating:rating];
903 //Show rating status window
904 [statusWindowController showRatingWindowWithRating:rating];
906 [self networkError:localException];
913 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
914 ITDebugLog(@"Toggling repeat mode.");
915 switch (repeatMode) {
916 case ITMTRemotePlayerRepeatOff:
917 repeatMode = ITMTRemotePlayerRepeatAll;
919 case ITMTRemotePlayerRepeatAll:
920 repeatMode = ITMTRemotePlayerRepeatOne;
922 case ITMTRemotePlayerRepeatOne:
923 repeatMode = ITMTRemotePlayerRepeatOff;
926 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
927 [[self currentRemote] setRepeatMode:repeatMode];
929 //Show loop status window
930 [statusWindowController showRepeatWindowWithMode:repeatMode];
932 [self networkError:localException];
936 - (void)toggleShuffle
939 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
940 ITDebugLog(@"Toggling shuffle mode.");
941 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
942 //Show shuffle status window
943 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
944 [statusWindowController showShuffleWindow:newShuffleEnabled];
946 [self networkError:localException];
950 - (void)registerNowOK
952 [[StatusWindow sharedWindow] setLocked:NO];
953 [[StatusWindow sharedWindow] vanish:self];
954 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
959 - (void)registerNowCancel
961 [[StatusWindow sharedWindow] setLocked:NO];
962 [[StatusWindow sharedWindow] vanish:self];
963 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
965 [NSApp terminate:self];
968 /*************************************************************************/
970 #pragma mark NETWORK HANDLERS
971 /*************************************************************************/
973 - (void)setServerStatus:(BOOL)newStatus
977 [networkController setServerStatus:YES];
980 [networkController setServerStatus:NO];
984 - (int)connectToServer
987 ITDebugLog(@"Attempting to connect to shared remote.");
988 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
991 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
992 currentRemote = [[[networkController networkObject] remote] retain];
993 [refreshTimer invalidate];
994 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
996 selector:@selector(timerUpdate)
998 repeats:YES] retain];
1000 ITDebugLog(@"Connection successful.");
1002 } else if (result == 0) {
1003 ITDebugLog(@"Connection failed.");
1004 currentRemote = [remoteArray objectAtIndex:0];
1007 //Do something about the password being invalid
1008 ITDebugLog(@"Connection failed.");
1009 currentRemote = [remoteArray objectAtIndex:0];
1014 - (BOOL)disconnectFromServer
1016 ITDebugLog(@"Disconnecting from shared remote.");
1018 [currentRemote release];
1019 currentRemote = [remoteArray objectAtIndex:0];
1020 [networkController disconnect];
1025 - (void)checkForRemoteServer:(NSTimer *)timer
1027 ITDebugLog(@"Checking for remote server.");
1028 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1029 ITDebugLog(@"Remote server found.");
1031 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1032 [[StatusWindowController sharedController] showReconnectQueryWindow];
1035 ITDebugLog(@"Remote server not found.");
1039 - (void)networkError:(NSException *)exception
1041 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1042 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1043 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);
1044 if ([self disconnectFromServer]) {
1045 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1046 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1048 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1055 if ([self connectToServer] == 0) {
1056 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1058 [[StatusWindow sharedWindow] setLocked:NO];
1059 [[StatusWindow sharedWindow] vanish:self];
1060 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1063 - (void)cancelReconnect
1065 [[StatusWindow sharedWindow] setLocked:NO];
1066 [[StatusWindow sharedWindow] vanish:self];
1067 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1070 /*************************************************************************/
1072 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1073 /*************************************************************************/
1075 - (void)applicationLaunched:(NSNotification *)note
1078 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1079 ITDebugLog(@"Remote application launched.");
1080 playerRunningState = ITMTRemotePlayerRunning;
1081 [[self currentRemote] begin];
1082 [self setLatestSongIdentifier:@""];
1084 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1086 selector:@selector(timerUpdate)
1088 repeats:YES] retain];
1089 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1090 [self setupHotKeys];
1093 [self networkError:localException];
1097 - (void)applicationTerminated:(NSNotification *)note
1100 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1101 ITDebugLog(@"Remote application terminated.");
1102 playerRunningState = ITMTRemotePlayerNotRunning;
1103 [[self currentRemote] halt];
1104 [refreshTimer invalidate];
1105 [refreshTimer release];
1107 [self clearHotKeys];
1109 if ([df objectForKey:@"ShowPlayer"] != nil) {
1111 ITDebugLog(@"Setting up show player hot key.");
1112 hotKey = [[ITHotKey alloc] init];
1113 [hotKey setName:@"ShowPlayer"];
1114 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1115 [hotKey setTarget:self];
1116 [hotKey setAction:@selector(showPlayer)];
1117 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1121 [self networkError:localException];
1126 /*************************************************************************/
1128 #pragma mark NSApplication DELEGATE METHODS
1129 /*************************************************************************/
1131 - (void)applicationWillTerminate:(NSNotification *)note
1133 [networkController stopRemoteServerSearch];
1134 [self clearHotKeys];
1135 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1139 /*************************************************************************/
1141 #pragma mark DEALLOCATION METHOD
1142 /*************************************************************************/
1146 [self applicationTerminated:nil];
1148 [statusItem release];
1149 [statusWindowController release];
1150 [menuController release];
1151 [networkController release];