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.");
309 if ([df boolForKey:@"runScripts"] && [[self currentRemote] currentSongTitle]) {
310 NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
311 NSEnumerator *scriptsEnum = [scripts objectEnumerator];
312 NSString *nextScript;
313 while ( (nextScript = [scriptsEnum nextObject]) ) {
315 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
316 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
317 ITDebugLog(@"Error running script %@.", nextScript);
319 [currentScript release];
326 latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
327 [menuController rebuildSubmenus];
329 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
330 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
333 [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
335 NSString *artist = [[self currentRemote] currentSongArtist];
336 NSString *title = [[self currentRemote] currentSongTitle];
339 toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
343 toolTip = @"No Song Playing";
345 [statusItem setToolTip:toolTip];
347 [self networkError:localException];
356 ITDebugLog(@"Menu clicked.");
357 if ([networkController isConnectedToServer]) {
358 //Used the cached version
363 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
364 [statusItem setMenu:[menuController menu]];
366 [statusItem setMenu:[menuController menuForNoPlayer]];
369 [self networkError:localException];
382 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
383 ITDebugLog(@"Play/Pause toggled");
384 if (state == ITMTRemotePlayerPlaying) {
385 [[self currentRemote] pause];
386 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
387 [[self currentRemote] pause];
388 [[self currentRemote] play];
390 [[self currentRemote] play];
393 [self networkError:localException];
401 ITDebugLog(@"Going to next song.");
403 [[self currentRemote] goToNextSong];
405 [self networkError:localException];
412 ITDebugLog(@"Going to previous song.");
414 [[self currentRemote] goToPreviousSong];
416 [self networkError:localException];
423 ITDebugLog(@"Fast forwarding.");
425 [[self currentRemote] forward];
427 [self networkError:localException];
434 ITDebugLog(@"Rewinding.");
436 [[self currentRemote] rewind];
438 [self networkError:localException];
443 - (void)selectPlaylistAtIndex:(int)index
445 ITDebugLog(@"Selecting playlist %i", index);
447 [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
448 //[[self currentRemote] switchToPlaylistAtIndex:index];
450 [self networkError:localException];
455 - (void)selectSongAtIndex:(int)index
457 ITDebugLog(@"Selecting song %i", index);
459 [[self currentRemote] switchToSongAtIndex:index];
461 [self networkError:localException];
466 - (void)selectSongRating:(int)rating
468 ITDebugLog(@"Selecting song rating %i", rating);
470 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
472 [self networkError:localException];
477 - (void)selectEQPresetAtIndex:(int)index
479 ITDebugLog(@"Selecting EQ preset %i", index);
481 [[self currentRemote] switchToEQAtIndex:index];
483 [self networkError:localException];
490 ITDebugLog(@"Beginning show player.");
491 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
492 ITDebugLog(@"Showing player interface.");
494 [[self currentRemote] showPrimaryInterface];
496 [self networkError:localException];
499 ITDebugLog(@"Launching player.");
501 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
502 ITDebugLog(@"Error Launching Player");
505 [self networkError:localException];
508 ITDebugLog(@"Finished show player.");
511 - (void)showPreferences
513 ITDebugLog(@"Show preferences.");
514 [[PreferencesController sharedPrefs] showPrefsWindow:self];
517 - (void)showPreferencesAndClose
519 ITDebugLog(@"Show preferences.");
520 [[PreferencesController sharedPrefs] showPrefsWindow:self];
521 [[StatusWindow sharedWindow] setLocked:NO];
522 [[StatusWindow sharedWindow] vanish:self];
523 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
526 - (void)showTestWindow
528 [self showCurrentTrackInfo];
531 - (void)quitMenuTunes
533 ITDebugLog(@"Quitting MenuTunes.");
534 [NSApp terminate:self];
540 - (void)closePreferences
542 ITDebugLog(@"Preferences closed.");
543 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
548 - (ITMTRemote *)currentRemote
550 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
551 [self networkError:nil];
554 return currentRemote;
565 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
566 ITHotKey *nextHotKey;
567 ITDebugLog(@"Clearing hot keys.");
568 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
569 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
571 ITDebugLog(@"Done clearing hot keys.");
577 ITDebugLog(@"Setting up hot keys.");
579 if (playerRunningState == ITMTRemotePlayerNotRunning) {
583 if ([df objectForKey:@"PlayPause"] != nil) {
584 ITDebugLog(@"Setting up play pause hot key.");
585 hotKey = [[ITHotKey alloc] init];
586 [hotKey setName:@"PlayPause"];
587 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
588 [hotKey setTarget:self];
589 [hotKey setAction:@selector(playPause)];
590 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
593 if ([df objectForKey:@"NextTrack"] != nil) {
594 ITDebugLog(@"Setting up next track hot key.");
595 hotKey = [[ITHotKey alloc] init];
596 [hotKey setName:@"NextTrack"];
597 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
598 [hotKey setTarget:self];
599 [hotKey setAction:@selector(nextSong)];
600 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
603 if ([df objectForKey:@"PrevTrack"] != nil) {
604 ITDebugLog(@"Setting up previous track hot key.");
605 hotKey = [[ITHotKey alloc] init];
606 [hotKey setName:@"PrevTrack"];
607 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
608 [hotKey setTarget:self];
609 [hotKey setAction:@selector(prevSong)];
610 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
613 if ([df objectForKey:@"ShowPlayer"] != nil) {
614 ITDebugLog(@"Setting up show player hot key.");
615 hotKey = [[ITHotKey alloc] init];
616 [hotKey setName:@"ShowPlayer"];
617 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
618 [hotKey setTarget:self];
619 [hotKey setAction:@selector(showPlayer)];
620 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
623 if ([df objectForKey:@"TrackInfo"] != nil) {
624 ITDebugLog(@"Setting up track info hot key.");
625 hotKey = [[ITHotKey alloc] init];
626 [hotKey setName:@"TrackInfo"];
627 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
628 [hotKey setTarget:self];
629 [hotKey setAction:@selector(showCurrentTrackInfo)];
630 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
633 if ([df objectForKey:@"UpcomingSongs"] != nil) {
634 ITDebugLog(@"Setting up upcoming songs hot key.");
635 hotKey = [[ITHotKey alloc] init];
636 [hotKey setName:@"UpcomingSongs"];
637 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
638 [hotKey setTarget:self];
639 [hotKey setAction:@selector(showUpcomingSongs)];
640 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
643 if ([df objectForKey:@"ToggleLoop"] != nil) {
644 ITDebugLog(@"Setting up toggle loop hot key.");
645 hotKey = [[ITHotKey alloc] init];
646 [hotKey setName:@"ToggleLoop"];
647 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
648 [hotKey setTarget:self];
649 [hotKey setAction:@selector(toggleLoop)];
650 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
653 if ([df objectForKey:@"ToggleShuffle"] != nil) {
654 ITDebugLog(@"Setting up toggle shuffle hot key.");
655 hotKey = [[ITHotKey alloc] init];
656 [hotKey setName:@"ToggleShuffle"];
657 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
658 [hotKey setTarget:self];
659 [hotKey setAction:@selector(toggleShuffle)];
660 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
663 if ([df objectForKey:@"IncrementVolume"] != nil) {
664 ITDebugLog(@"Setting up increment volume hot key.");
665 hotKey = [[ITHotKey alloc] init];
666 [hotKey setName:@"IncrementVolume"];
667 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
668 [hotKey setTarget:self];
669 [hotKey setAction:@selector(incrementVolume)];
670 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
673 if ([df objectForKey:@"DecrementVolume"] != nil) {
674 ITDebugLog(@"Setting up decrement volume hot key.");
675 hotKey = [[ITHotKey alloc] init];
676 [hotKey setName:@"DecrementVolume"];
677 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
678 [hotKey setTarget:self];
679 [hotKey setAction:@selector(decrementVolume)];
680 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
683 if ([df objectForKey:@"IncrementRating"] != nil) {
684 ITDebugLog(@"Setting up increment rating hot key.");
685 hotKey = [[ITHotKey alloc] init];
686 [hotKey setName:@"IncrementRating"];
687 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
688 [hotKey setTarget:self];
689 [hotKey setAction:@selector(incrementRating)];
690 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
693 if ([df objectForKey:@"DecrementRating"] != nil) {
694 ITDebugLog(@"Setting up decrement rating hot key.");
695 hotKey = [[ITHotKey alloc] init];
696 [hotKey setName:@"DecrementRating"];
697 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
698 [hotKey setTarget:self];
699 [hotKey setAction:@selector(decrementRating)];
700 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
702 ITDebugLog(@"Finished setting up hot keys.");
705 - (void)showCurrentTrackInfo
707 ITMTRemotePlayerSource source = 0;
708 NSString *title = nil;
709 NSString *album = nil;
710 NSString *artist = nil;
711 NSString *time = nil;
712 NSString *track = nil;
717 source = [[self currentRemote] currentSource];
718 title = [[self currentRemote] currentSongTitle];
720 [self networkError:localException];
723 ITDebugLog(@"Showing track info status window.");
727 if ( [df boolForKey:@"showAlbum"] ) {
729 album = [[self currentRemote] currentSongAlbum];
731 [self networkError:localException];
735 if ( [df boolForKey:@"showArtist"] ) {
737 artist = [[self currentRemote] currentSongArtist];
739 [self networkError:localException];
743 if ( [df boolForKey:@"showTime"] ) {
745 time = [NSString stringWithFormat:@"%@: %@ / %@",
747 [[self currentRemote] currentSongElapsed],
748 [[self currentRemote] currentSongLength]];
750 [self networkError:localException];
754 if ( [df boolForKey:@"showTrackNumber"] ) {
759 trackNo = [[self currentRemote] currentSongTrack];
760 trackCount = [[self currentRemote] currentAlbumTrackCount];
762 [self networkError:localException];
765 if ( (trackNo > 0) || (trackCount > 0) ) {
766 track = [NSString stringWithFormat:@"%@: %i %@ %i",
767 @"Track", trackNo, @"of", trackCount];
771 if ( [df boolForKey:@"showTrackRating"] ) {
772 float currentRating = 0;
775 currentRating = [[self currentRemote] currentSongRating];
777 [self networkError:localException];
780 if (currentRating >= 0.0) {
781 rating = ( currentRating * 5 );
785 if ( [df boolForKey:@"showAlbumArtwork"] ) {
787 art = [[self currentRemote] currentSongAlbumArt];
789 [self networkError:localException];
794 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
797 [statusWindowController showSongInfoWindowWithSource:source
807 - (void)showUpcomingSongs
811 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
813 [self networkError:localException];
816 ITDebugLog(@"Showing upcoming songs status window.");
819 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
820 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
821 int curTrack = [[self currentRemote] currentSongIndex];
824 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
826 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
830 if ([songList count] == 0) {
831 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
834 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
836 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
839 [self networkError:localException];
843 - (void)incrementVolume
846 float volume = [[self currentRemote] volume];
847 float dispVol = volume;
848 ITDebugLog(@"Incrementing volume.");
857 ITDebugLog(@"Setting volume to %f", volume);
858 [[self currentRemote] setVolume:volume];
860 // Show volume status window
861 [statusWindowController showVolumeWindowWithLevel:dispVol];
863 [self networkError:localException];
867 - (void)decrementVolume
870 float volume = [[self currentRemote] volume];
871 float dispVol = volume;
872 ITDebugLog(@"Decrementing volume.");
881 ITDebugLog(@"Setting volume to %f", volume);
882 [[self currentRemote] setVolume:volume];
884 //Show volume status window
885 [statusWindowController showVolumeWindowWithLevel:dispVol];
887 [self networkError:localException];
891 - (void)incrementRating
894 float rating = [[self currentRemote] currentSongRating];
895 ITDebugLog(@"Incrementing rating.");
897 if ([[self currentRemote] currentPlaylistIndex] == 0) {
898 ITDebugLog(@"No song playing, rating change aborted.");
906 ITDebugLog(@"Setting rating to %f", rating);
907 [[self currentRemote] setCurrentSongRating:rating];
909 //Show rating status window
910 [statusWindowController showRatingWindowWithRating:rating];
912 [self networkError:localException];
916 - (void)decrementRating
919 float rating = [[self currentRemote] currentSongRating];
920 ITDebugLog(@"Decrementing rating.");
922 if ([[self currentRemote] currentPlaylistIndex] == 0) {
923 ITDebugLog(@"No song playing, rating change aborted.");
931 ITDebugLog(@"Setting rating to %f", rating);
932 [[self currentRemote] setCurrentSongRating:rating];
934 //Show rating status window
935 [statusWindowController showRatingWindowWithRating:rating];
937 [self networkError:localException];
944 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
945 ITDebugLog(@"Toggling repeat mode.");
946 switch (repeatMode) {
947 case ITMTRemotePlayerRepeatOff:
948 repeatMode = ITMTRemotePlayerRepeatAll;
950 case ITMTRemotePlayerRepeatAll:
951 repeatMode = ITMTRemotePlayerRepeatOne;
953 case ITMTRemotePlayerRepeatOne:
954 repeatMode = ITMTRemotePlayerRepeatOff;
957 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
958 [[self currentRemote] setRepeatMode:repeatMode];
960 //Show loop status window
961 [statusWindowController showRepeatWindowWithMode:repeatMode];
963 [self networkError:localException];
967 - (void)toggleShuffle
970 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
971 ITDebugLog(@"Toggling shuffle mode.");
972 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
973 //Show shuffle status window
974 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
975 [statusWindowController showShuffleWindow:newShuffleEnabled];
977 [self networkError:localException];
981 - (void)registerNowOK
983 [[StatusWindow sharedWindow] setLocked:NO];
984 [[StatusWindow sharedWindow] vanish:self];
985 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
990 - (void)registerNowCancel
992 [[StatusWindow sharedWindow] setLocked:NO];
993 [[StatusWindow sharedWindow] vanish:self];
994 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
996 [NSApp terminate:self];
999 /*************************************************************************/
1001 #pragma mark NETWORK HANDLERS
1002 /*************************************************************************/
1004 - (void)setServerStatus:(BOOL)newStatus
1008 [networkController setServerStatus:YES];
1011 [networkController setServerStatus:NO];
1015 - (int)connectToServer
1018 ITDebugLog(@"Attempting to connect to shared remote.");
1019 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1022 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1023 currentRemote = [[[networkController networkObject] remote] retain];
1024 [refreshTimer invalidate];
1025 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1027 selector:@selector(timerUpdate)
1029 repeats:YES] retain];
1031 ITDebugLog(@"Connection successful.");
1033 } else if (result == 0) {
1034 ITDebugLog(@"Connection failed.");
1035 currentRemote = [remoteArray objectAtIndex:0];
1038 //Do something about the password being invalid
1039 ITDebugLog(@"Connection failed.");
1040 currentRemote = [remoteArray objectAtIndex:0];
1045 - (BOOL)disconnectFromServer
1047 ITDebugLog(@"Disconnecting from shared remote.");
1049 [currentRemote release];
1050 currentRemote = [remoteArray objectAtIndex:0];
1051 [networkController disconnect];
1056 - (void)checkForRemoteServer:(NSTimer *)timer
1058 ITDebugLog(@"Checking for remote server.");
1059 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1060 ITDebugLog(@"Remote server found.");
1062 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1063 [[StatusWindowController sharedController] showReconnectQueryWindow];
1066 ITDebugLog(@"Remote server not found.");
1070 - (void)networkError:(NSException *)exception
1072 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1073 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1074 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);
1075 if ([self disconnectFromServer]) {
1076 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1077 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1079 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1086 if ([self connectToServer] == 0) {
1087 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1089 [[StatusWindow sharedWindow] setLocked:NO];
1090 [[StatusWindow sharedWindow] vanish:self];
1091 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1094 - (void)cancelReconnect
1096 [[StatusWindow sharedWindow] setLocked:NO];
1097 [[StatusWindow sharedWindow] vanish:self];
1098 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1101 /*************************************************************************/
1103 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1104 /*************************************************************************/
1106 - (void)applicationLaunched:(NSNotification *)note
1109 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1110 ITDebugLog(@"Remote application launched.");
1111 playerRunningState = ITMTRemotePlayerRunning;
1112 [[self currentRemote] begin];
1113 [self setLatestSongIdentifier:@""];
1115 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1117 selector:@selector(timerUpdate)
1119 repeats:YES] retain];
1120 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1121 [self setupHotKeys];
1124 [self networkError:localException];
1128 - (void)applicationTerminated:(NSNotification *)note
1131 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1132 ITDebugLog(@"Remote application terminated.");
1133 playerRunningState = ITMTRemotePlayerNotRunning;
1134 [[self currentRemote] halt];
1135 [refreshTimer invalidate];
1136 [refreshTimer release];
1138 [self clearHotKeys];
1140 if ([df objectForKey:@"ShowPlayer"] != nil) {
1142 ITDebugLog(@"Setting up show player hot key.");
1143 hotKey = [[ITHotKey alloc] init];
1144 [hotKey setName:@"ShowPlayer"];
1145 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1146 [hotKey setTarget:self];
1147 [hotKey setAction:@selector(showPlayer)];
1148 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1152 [self networkError:localException];
1157 /*************************************************************************/
1159 #pragma mark NSApplication DELEGATE METHODS
1160 /*************************************************************************/
1162 - (void)applicationWillTerminate:(NSNotification *)note
1164 [networkController stopRemoteServerSearch];
1165 [self clearHotKeys];
1166 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1170 /*************************************************************************/
1172 #pragma mark DEALLOCATION METHOD
1173 /*************************************************************************/
1177 [self applicationTerminated:nil];
1179 [statusItem release];
1180 [statusWindowController release];
1181 [menuController release];
1182 [networkController release];