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 if ([df boolForKey:@"ITMTNoStatusItem"]) {
98 [StatusItemHack install];
99 statusItem = [[ITStatusItem alloc]
100 initWithStatusBar:[NSStatusBar systemStatusBar]
101 withLength:NSSquareStatusItemLength];
104 bling = [[MTBlingController alloc] init];
106 registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
108 selector:@selector(blingTime)
110 repeats:YES] retain];
113 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
114 [self applicationLaunched:nil];
116 if ([df boolForKey:@"LaunchPlayerWithMT"])
119 [self applicationTerminated:nil];
122 [self networkError:localException];
125 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
126 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
128 [networkController startRemoteServerSearch];
132 - (ITMTRemote *)loadRemote
134 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
135 ITDebugLog(@"Gathering remotes.");
137 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
138 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
139 NSString *bundlePath;
141 while ( (bundlePath = [enumerator nextObject]) ) {
142 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
145 Class remoteClass = [remoteBundle principalClass];
147 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
148 [remoteClass isKindOfClass:[NSObject class]]) {
149 id remote = [remoteClass remote];
150 ITDebugLog(@"Adding remote at path %@", bundlePath);
151 [remoteArray addObject:remote];
156 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
157 // if ( [remoteArray count] > 1 ) {
158 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
160 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
163 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
164 return [remoteArray objectAtIndex:0];
167 /*************************************************************************/
169 #pragma mark INSTANCE METHODS
170 /*************************************************************************/
172 /*- (void)startTimerInNewThread
174 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
175 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
176 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
178 selector:@selector(timerUpdate)
180 repeats:YES] retain];
182 ITDebugLog(@"Timer started.");
186 - (void)setBlingTime:(NSDate*)date
188 NSMutableDictionary *globalPrefs;
190 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
192 [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
194 [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
196 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
198 [globalPrefs release];
201 - (NSDate*)getBlingTime
204 return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
209 NSDate *now = [NSDate date];
210 if (![self blingBling]) {
211 if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
212 [self setBlingTime:now];
214 if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
216 [statusItem setEnabled:NO];
218 if ([refreshTimer isValid]) {
219 [refreshTimer invalidate];
221 [statusWindowController showRegistrationQueryWindow];
225 [statusItem setEnabled:YES];
227 if (![refreshTimer isValid]) {
228 [refreshTimer release];
229 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
231 selector:@selector(timerUpdate)
233 repeats:YES] retain];
237 [self setBlingTime:nil];
248 if ( ! ([bling checkDone] == 2475) ) {
255 - (BOOL)songIsPlaying
257 NSString *identifier = nil;
259 identifier = [[self currentRemote] playerStateUniqueIdentifier];
261 [self networkError:localException];
263 return ( ! ([identifier isEqualToString:@"0-0"]) );
266 - (BOOL)radioIsPlaying
268 ITMTRemotePlayerPlaylistClass class = nil;
270 class = [[self currentRemote] currentPlaylistClass];
272 [self networkError:localException];
274 return (class == ITMTRemotePlayerRadioPlaylist );
279 NSString *identifier = nil;
281 identifier = [[self currentRemote] playerStateUniqueIdentifier];
283 [self networkError:localException];
285 return ( ! [identifier isEqualToString:_latestSongIdentifier] );
288 - (NSString *)latestSongIdentifier
290 return _latestSongIdentifier;
293 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
295 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
296 [_latestSongIdentifier autorelease];
297 _latestSongIdentifier = [newIdentifier copy];
302 if ([networkController isConnectedToServer]) {
303 [statusItem setMenu:[menuController menu]];
306 if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
307 ITDebugLog(@"The song changed.");
311 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
312 [menuController rebuildSubmenus];
314 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
315 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
318 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
320 [self networkError:localException];
329 ITDebugLog(@"Menu clicked.");
330 if ([networkController isConnectedToServer]) {
331 //Used the cached version
336 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
337 [statusItem setMenu:[menuController menu]];
339 [statusItem setMenu:[menuController menuForNoPlayer]];
342 [self networkError:localException];
355 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
356 ITDebugLog(@"Play/Pause toggled");
357 if (state == ITMTRemotePlayerPlaying) {
358 [[self currentRemote] pause];
359 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
360 [[self currentRemote] pause];
361 [[self currentRemote] play];
363 [[self currentRemote] play];
366 [self networkError:localException];
374 ITDebugLog(@"Going to next song.");
376 [[self currentRemote] goToNextSong];
378 [self networkError:localException];
385 ITDebugLog(@"Going to previous song.");
387 [[self currentRemote] goToPreviousSong];
389 [self networkError:localException];
396 ITDebugLog(@"Fast forwarding.");
398 [[self currentRemote] forward];
400 [self networkError:localException];
407 ITDebugLog(@"Rewinding.");
409 [[self currentRemote] rewind];
411 [self networkError:localException];
416 - (void)selectPlaylistAtIndex:(int)index
418 ITDebugLog(@"Selecting playlist %i", index);
420 //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
421 [[self currentRemote] switchToPlaylistAtIndex:index];
423 [self networkError:localException];
428 - (void)selectSongAtIndex:(int)index
430 ITDebugLog(@"Selecting song %i", index);
432 [[self currentRemote] switchToSongAtIndex:index];
434 [self networkError:localException];
439 - (void)selectSongRating:(int)rating
441 ITDebugLog(@"Selecting song rating %i", rating);
443 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
445 [self networkError:localException];
450 - (void)selectEQPresetAtIndex:(int)index
452 ITDebugLog(@"Selecting EQ preset %i", index);
454 [[self currentRemote] switchToEQAtIndex:index];
456 [self networkError:localException];
463 ITDebugLog(@"Beginning show player.");
464 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
465 ITDebugLog(@"Showing player interface.");
467 [[self currentRemote] showPrimaryInterface];
469 [self networkError:localException];
472 ITDebugLog(@"Launching player.");
474 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
475 ITDebugLog(@"Error Launching Player");
478 [self networkError:localException];
481 ITDebugLog(@"Finished show player.");
484 - (void)showPreferences
486 ITDebugLog(@"Show preferences.");
487 [[PreferencesController sharedPrefs] showPrefsWindow:self];
490 - (void)showPreferencesAndClose
492 ITDebugLog(@"Show preferences.");
493 [[PreferencesController sharedPrefs] showPrefsWindow:self];
494 [[StatusWindow sharedWindow] setLocked:NO];
495 [[StatusWindow sharedWindow] vanish:self];
496 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
499 - (void)showTestWindow
501 [self showCurrentTrackInfo];
504 - (void)quitMenuTunes
506 ITDebugLog(@"Quitting MenuTunes.");
507 [NSApp terminate:self];
513 - (void)closePreferences
515 ITDebugLog(@"Preferences closed.");
516 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
521 - (ITMTRemote *)currentRemote
523 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
524 [self networkError:nil];
527 return currentRemote;
538 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
539 ITHotKey *nextHotKey;
540 ITDebugLog(@"Clearing hot keys.");
541 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
542 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
544 ITDebugLog(@"Done clearing hot keys.");
550 ITDebugLog(@"Setting up hot keys.");
552 if (playerRunningState == ITMTRemotePlayerNotRunning) {
556 if ([df objectForKey:@"PlayPause"] != nil) {
557 ITDebugLog(@"Setting up play pause hot key.");
558 hotKey = [[ITHotKey alloc] init];
559 [hotKey setName:@"PlayPause"];
560 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
561 [hotKey setTarget:self];
562 [hotKey setAction:@selector(playPause)];
563 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
566 if ([df objectForKey:@"NextTrack"] != nil) {
567 ITDebugLog(@"Setting up next track hot key.");
568 hotKey = [[ITHotKey alloc] init];
569 [hotKey setName:@"NextTrack"];
570 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
571 [hotKey setTarget:self];
572 [hotKey setAction:@selector(nextSong)];
573 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
576 if ([df objectForKey:@"PrevTrack"] != nil) {
577 ITDebugLog(@"Setting up previous track hot key.");
578 hotKey = [[ITHotKey alloc] init];
579 [hotKey setName:@"PrevTrack"];
580 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
581 [hotKey setTarget:self];
582 [hotKey setAction:@selector(prevSong)];
583 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
586 if ([df objectForKey:@"ShowPlayer"] != nil) {
587 ITDebugLog(@"Setting up show player hot key.");
588 hotKey = [[ITHotKey alloc] init];
589 [hotKey setName:@"ShowPlayer"];
590 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
591 [hotKey setTarget:self];
592 [hotKey setAction:@selector(showPlayer)];
593 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
596 if ([df objectForKey:@"TrackInfo"] != nil) {
597 ITDebugLog(@"Setting up track info hot key.");
598 hotKey = [[ITHotKey alloc] init];
599 [hotKey setName:@"TrackInfo"];
600 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
601 [hotKey setTarget:self];
602 [hotKey setAction:@selector(showCurrentTrackInfo)];
603 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
606 if ([df objectForKey:@"UpcomingSongs"] != nil) {
607 ITDebugLog(@"Setting up upcoming songs hot key.");
608 hotKey = [[ITHotKey alloc] init];
609 [hotKey setName:@"UpcomingSongs"];
610 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
611 [hotKey setTarget:self];
612 [hotKey setAction:@selector(showUpcomingSongs)];
613 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
616 if ([df objectForKey:@"ToggleLoop"] != nil) {
617 ITDebugLog(@"Setting up toggle loop hot key.");
618 hotKey = [[ITHotKey alloc] init];
619 [hotKey setName:@"ToggleLoop"];
620 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
621 [hotKey setTarget:self];
622 [hotKey setAction:@selector(toggleLoop)];
623 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
626 if ([df objectForKey:@"ToggleShuffle"] != nil) {
627 ITDebugLog(@"Setting up toggle shuffle hot key.");
628 hotKey = [[ITHotKey alloc] init];
629 [hotKey setName:@"ToggleShuffle"];
630 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
631 [hotKey setTarget:self];
632 [hotKey setAction:@selector(toggleShuffle)];
633 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
636 if ([df objectForKey:@"IncrementVolume"] != nil) {
637 ITDebugLog(@"Setting up increment volume hot key.");
638 hotKey = [[ITHotKey alloc] init];
639 [hotKey setName:@"IncrementVolume"];
640 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
641 [hotKey setTarget:self];
642 [hotKey setAction:@selector(incrementVolume)];
643 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
646 if ([df objectForKey:@"DecrementVolume"] != nil) {
647 ITDebugLog(@"Setting up decrement volume hot key.");
648 hotKey = [[ITHotKey alloc] init];
649 [hotKey setName:@"DecrementVolume"];
650 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
651 [hotKey setTarget:self];
652 [hotKey setAction:@selector(decrementVolume)];
653 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
656 if ([df objectForKey:@"IncrementRating"] != nil) {
657 ITDebugLog(@"Setting up increment rating hot key.");
658 hotKey = [[ITHotKey alloc] init];
659 [hotKey setName:@"IncrementRating"];
660 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
661 [hotKey setTarget:self];
662 [hotKey setAction:@selector(incrementRating)];
663 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
666 if ([df objectForKey:@"DecrementRating"] != nil) {
667 ITDebugLog(@"Setting up decrement rating hot key.");
668 hotKey = [[ITHotKey alloc] init];
669 [hotKey setName:@"DecrementRating"];
670 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
671 [hotKey setTarget:self];
672 [hotKey setAction:@selector(decrementRating)];
673 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
675 ITDebugLog(@"Finished setting up hot keys.");
678 - (void)showCurrentTrackInfo
680 ITMTRemotePlayerSource source = 0;
681 NSString *title = nil;
682 NSString *album = nil;
683 NSString *artist = nil;
684 NSString *time = nil;
685 NSString *track = nil;
690 source = [[self currentRemote] currentSource];
691 title = [[self currentRemote] currentSongTitle];
693 [self networkError:localException];
696 ITDebugLog(@"Showing track info status window.");
700 if ( [df boolForKey:@"showAlbum"] ) {
702 album = [[self currentRemote] currentSongAlbum];
704 [self networkError:localException];
708 if ( [df boolForKey:@"showArtist"] ) {
710 artist = [[self currentRemote] currentSongArtist];
712 [self networkError:localException];
716 if ( [df boolForKey:@"showTime"] ) {
718 time = [NSString stringWithFormat:@"%@: %@ / %@",
720 [[self currentRemote] currentSongElapsed],
721 [[self currentRemote] currentSongLength]];
723 [self networkError:localException];
727 if ( [df boolForKey:@"showTrackNumber"] ) {
732 trackNo = [[self currentRemote] currentSongTrack];
733 trackCount = [[self currentRemote] currentAlbumTrackCount];
735 [self networkError:localException];
738 if ( (trackNo > 0) || (trackCount > 0) ) {
739 track = [NSString stringWithFormat:@"%@: %i %@ %i",
740 @"Track", trackNo, @"of", trackCount];
744 if ( [df boolForKey:@"showTrackRating"] ) {
745 float currentRating = 0;
748 currentRating = [[self currentRemote] currentSongRating];
750 [self networkError:localException];
753 if (currentRating >= 0.0) {
754 rating = ( currentRating * 5 );
758 if ( [df boolForKey:@"showAlbumArtwork"] ) {
760 art = [[self currentRemote] currentSongAlbumArt];
762 [self networkError:localException];
767 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
770 [statusWindowController showSongInfoWindowWithSource:source
780 - (void)showUpcomingSongs
784 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
786 [self networkError:localException];
789 ITDebugLog(@"Showing upcoming songs status window.");
792 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
793 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
794 int curTrack = [[self currentRemote] currentSongIndex];
797 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
799 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
803 if ([songList count] == 0) {
804 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
807 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
809 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
812 [self networkError:localException];
816 - (void)incrementVolume
819 float volume = [[self currentRemote] volume];
820 float dispVol = volume;
821 ITDebugLog(@"Incrementing volume.");
830 ITDebugLog(@"Setting volume to %f", volume);
831 [[self currentRemote] setVolume:volume];
833 // Show volume status window
834 [statusWindowController showVolumeWindowWithLevel:dispVol];
836 [self networkError:localException];
840 - (void)decrementVolume
843 float volume = [[self currentRemote] volume];
844 float dispVol = volume;
845 ITDebugLog(@"Decrementing volume.");
854 ITDebugLog(@"Setting volume to %f", volume);
855 [[self currentRemote] setVolume:volume];
857 //Show volume status window
858 [statusWindowController showVolumeWindowWithLevel:dispVol];
860 [self networkError:localException];
864 - (void)incrementRating
867 float rating = [[self currentRemote] currentSongRating];
868 ITDebugLog(@"Incrementing rating.");
870 if ([[self currentRemote] currentPlaylistIndex] == 0) {
871 ITDebugLog(@"No song playing, rating change aborted.");
879 ITDebugLog(@"Setting rating to %f", rating);
880 [[self currentRemote] setCurrentSongRating:rating];
882 //Show rating status window
883 [statusWindowController showRatingWindowWithRating:rating];
885 [self networkError:localException];
889 - (void)decrementRating
892 float rating = [[self currentRemote] currentSongRating];
893 ITDebugLog(@"Decrementing rating.");
895 if ([[self currentRemote] currentPlaylistIndex] == 0) {
896 ITDebugLog(@"No song playing, rating change aborted.");
904 ITDebugLog(@"Setting rating to %f", rating);
905 [[self currentRemote] setCurrentSongRating:rating];
907 //Show rating status window
908 [statusWindowController showRatingWindowWithRating:rating];
910 [self networkError:localException];
917 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
918 ITDebugLog(@"Toggling repeat mode.");
919 switch (repeatMode) {
920 case ITMTRemotePlayerRepeatOff:
921 repeatMode = ITMTRemotePlayerRepeatAll;
923 case ITMTRemotePlayerRepeatAll:
924 repeatMode = ITMTRemotePlayerRepeatOne;
926 case ITMTRemotePlayerRepeatOne:
927 repeatMode = ITMTRemotePlayerRepeatOff;
930 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
931 [[self currentRemote] setRepeatMode:repeatMode];
933 //Show loop status window
934 [statusWindowController showRepeatWindowWithMode:repeatMode];
936 [self networkError:localException];
940 - (void)toggleShuffle
943 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
944 ITDebugLog(@"Toggling shuffle mode.");
945 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
946 //Show shuffle status window
947 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
948 [statusWindowController showShuffleWindow:newShuffleEnabled];
950 [self networkError:localException];
954 - (void)registerNowOK
956 [[StatusWindow sharedWindow] setLocked:NO];
957 [[StatusWindow sharedWindow] vanish:self];
958 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
963 - (void)registerNowCancel
965 [[StatusWindow sharedWindow] setLocked:NO];
966 [[StatusWindow sharedWindow] vanish:self];
967 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
969 [NSApp terminate:self];
972 /*************************************************************************/
974 #pragma mark NETWORK HANDLERS
975 /*************************************************************************/
977 - (void)setServerStatus:(BOOL)newStatus
981 [networkController setServerStatus:YES];
984 [networkController setServerStatus:NO];
988 - (int)connectToServer
991 ITDebugLog(@"Attempting to connect to shared remote.");
992 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
995 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
996 currentRemote = [[[networkController networkObject] remote] retain];
997 [refreshTimer invalidate];
998 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1000 selector:@selector(timerUpdate)
1002 repeats:YES] retain];
1004 ITDebugLog(@"Connection successful.");
1006 } else if (result == 0) {
1007 ITDebugLog(@"Connection failed.");
1008 currentRemote = [remoteArray objectAtIndex:0];
1011 //Do something about the password being invalid
1012 ITDebugLog(@"Connection failed.");
1013 currentRemote = [remoteArray objectAtIndex:0];
1018 - (BOOL)disconnectFromServer
1020 ITDebugLog(@"Disconnecting from shared remote.");
1022 [currentRemote release];
1023 currentRemote = [remoteArray objectAtIndex:0];
1024 [networkController disconnect];
1029 - (void)checkForRemoteServer:(NSTimer *)timer
1031 ITDebugLog(@"Checking for remote server.");
1032 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1033 ITDebugLog(@"Remote server found.");
1035 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1036 [[StatusWindowController sharedController] showReconnectQueryWindow];
1039 ITDebugLog(@"Remote server not found.");
1043 - (void)networkError:(NSException *)exception
1045 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1046 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1047 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);
1048 if ([self disconnectFromServer]) {
1049 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1050 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1052 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1059 if ([self connectToServer] == 0) {
1060 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1062 [[StatusWindow sharedWindow] setLocked:NO];
1063 [[StatusWindow sharedWindow] vanish:self];
1064 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1067 - (void)cancelReconnect
1069 [[StatusWindow sharedWindow] setLocked:NO];
1070 [[StatusWindow sharedWindow] vanish:self];
1071 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1074 /*************************************************************************/
1076 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1077 /*************************************************************************/
1079 - (void)applicationLaunched:(NSNotification *)note
1082 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1083 ITDebugLog(@"Remote application launched.");
1084 playerRunningState = ITMTRemotePlayerRunning;
1085 [[self currentRemote] begin];
1086 [self setLatestSongIdentifier:@""];
1088 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1090 selector:@selector(timerUpdate)
1092 repeats:YES] retain];
1093 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1094 [self setupHotKeys];
1097 [self networkError:localException];
1101 - (void)applicationTerminated:(NSNotification *)note
1104 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1105 ITDebugLog(@"Remote application terminated.");
1106 playerRunningState = ITMTRemotePlayerNotRunning;
1107 [[self currentRemote] halt];
1108 [refreshTimer invalidate];
1109 [refreshTimer release];
1111 [self clearHotKeys];
1113 if ([df objectForKey:@"ShowPlayer"] != nil) {
1115 ITDebugLog(@"Setting up show player hot key.");
1116 hotKey = [[ITHotKey alloc] init];
1117 [hotKey setName:@"ShowPlayer"];
1118 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1119 [hotKey setTarget:self];
1120 [hotKey setAction:@selector(showPlayer)];
1121 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1125 [self networkError:localException];
1130 /*************************************************************************/
1132 #pragma mark NSApplication DELEGATE METHODS
1133 /*************************************************************************/
1135 - (void)applicationWillTerminate:(NSNotification *)note
1137 [networkController stopRemoteServerSearch];
1138 [self clearHotKeys];
1139 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1143 /*************************************************************************/
1145 #pragma mark DEALLOCATION METHOD
1146 /*************************************************************************/
1150 [self applicationTerminated:nil];
1152 [statusItem release];
1153 [statusWindowController release];
1154 [menuController release];
1155 [networkController release];