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 [self networkError:localException];
344 ITDebugLog(@"Menu clicked.");
345 if ([networkController isConnectedToServer]) {
346 //Used the cached version
351 if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
352 [statusItem setMenu:[menuController menu]];
354 [statusItem setMenu:[menuController menuForNoPlayer]];
357 [self networkError:localException];
370 ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
371 ITDebugLog(@"Play/Pause toggled");
372 if (state == ITMTRemotePlayerPlaying) {
373 [[self currentRemote] pause];
374 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
375 [[self currentRemote] pause];
376 [[self currentRemote] play];
378 [[self currentRemote] play];
381 [self networkError:localException];
389 ITDebugLog(@"Going to next song.");
391 [[self currentRemote] goToNextSong];
393 [self networkError:localException];
400 ITDebugLog(@"Going to previous song.");
402 [[self currentRemote] goToPreviousSong];
404 [self networkError:localException];
411 ITDebugLog(@"Fast forwarding.");
413 [[self currentRemote] forward];
415 [self networkError:localException];
422 ITDebugLog(@"Rewinding.");
424 [[self currentRemote] rewind];
426 [self networkError:localException];
431 - (void)selectPlaylistAtIndex:(int)index
433 ITDebugLog(@"Selecting playlist %i", index);
435 //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
436 [[self currentRemote] switchToPlaylistAtIndex:index];
438 [self networkError:localException];
443 - (void)selectSongAtIndex:(int)index
445 ITDebugLog(@"Selecting song %i", index);
447 [[self currentRemote] switchToSongAtIndex:index];
449 [self networkError:localException];
454 - (void)selectSongRating:(int)rating
456 ITDebugLog(@"Selecting song rating %i", rating);
458 [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
460 [self networkError:localException];
465 - (void)selectEQPresetAtIndex:(int)index
467 ITDebugLog(@"Selecting EQ preset %i", index);
469 [[self currentRemote] switchToEQAtIndex:index];
471 [self networkError:localException];
478 ITDebugLog(@"Beginning show player.");
479 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
480 ITDebugLog(@"Showing player interface.");
482 [[self currentRemote] showPrimaryInterface];
484 [self networkError:localException];
487 ITDebugLog(@"Launching player.");
489 if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
490 ITDebugLog(@"Error Launching Player");
493 [self networkError:localException];
496 ITDebugLog(@"Finished show player.");
499 - (void)showPreferences
501 ITDebugLog(@"Show preferences.");
502 [[PreferencesController sharedPrefs] showPrefsWindow:self];
505 - (void)showPreferencesAndClose
507 ITDebugLog(@"Show preferences.");
508 [[PreferencesController sharedPrefs] showPrefsWindow:self];
509 [[StatusWindow sharedWindow] setLocked:NO];
510 [[StatusWindow sharedWindow] vanish:self];
511 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
514 - (void)showTestWindow
516 [self showCurrentTrackInfo];
519 - (void)quitMenuTunes
521 ITDebugLog(@"Quitting MenuTunes.");
522 [NSApp terminate:self];
528 - (void)closePreferences
530 ITDebugLog(@"Preferences closed.");
531 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
536 - (ITMTRemote *)currentRemote
538 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
539 [self networkError:nil];
542 return currentRemote;
553 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
554 ITHotKey *nextHotKey;
555 ITDebugLog(@"Clearing hot keys.");
556 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
557 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
559 ITDebugLog(@"Done clearing hot keys.");
565 ITDebugLog(@"Setting up hot keys.");
567 if (playerRunningState == ITMTRemotePlayerNotRunning) {
571 if ([df objectForKey:@"PlayPause"] != nil) {
572 ITDebugLog(@"Setting up play pause hot key.");
573 hotKey = [[ITHotKey alloc] init];
574 [hotKey setName:@"PlayPause"];
575 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
576 [hotKey setTarget:self];
577 [hotKey setAction:@selector(playPause)];
578 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
581 if ([df objectForKey:@"NextTrack"] != nil) {
582 ITDebugLog(@"Setting up next track hot key.");
583 hotKey = [[ITHotKey alloc] init];
584 [hotKey setName:@"NextTrack"];
585 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
586 [hotKey setTarget:self];
587 [hotKey setAction:@selector(nextSong)];
588 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
591 if ([df objectForKey:@"PrevTrack"] != nil) {
592 ITDebugLog(@"Setting up previous track hot key.");
593 hotKey = [[ITHotKey alloc] init];
594 [hotKey setName:@"PrevTrack"];
595 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
596 [hotKey setTarget:self];
597 [hotKey setAction:@selector(prevSong)];
598 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
601 if ([df objectForKey:@"ShowPlayer"] != nil) {
602 ITDebugLog(@"Setting up show player hot key.");
603 hotKey = [[ITHotKey alloc] init];
604 [hotKey setName:@"ShowPlayer"];
605 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
606 [hotKey setTarget:self];
607 [hotKey setAction:@selector(showPlayer)];
608 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
611 if ([df objectForKey:@"TrackInfo"] != nil) {
612 ITDebugLog(@"Setting up track info hot key.");
613 hotKey = [[ITHotKey alloc] init];
614 [hotKey setName:@"TrackInfo"];
615 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
616 [hotKey setTarget:self];
617 [hotKey setAction:@selector(showCurrentTrackInfo)];
618 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
621 if ([df objectForKey:@"UpcomingSongs"] != nil) {
622 ITDebugLog(@"Setting up upcoming songs hot key.");
623 hotKey = [[ITHotKey alloc] init];
624 [hotKey setName:@"UpcomingSongs"];
625 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
626 [hotKey setTarget:self];
627 [hotKey setAction:@selector(showUpcomingSongs)];
628 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
631 if ([df objectForKey:@"ToggleLoop"] != nil) {
632 ITDebugLog(@"Setting up toggle loop hot key.");
633 hotKey = [[ITHotKey alloc] init];
634 [hotKey setName:@"ToggleLoop"];
635 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
636 [hotKey setTarget:self];
637 [hotKey setAction:@selector(toggleLoop)];
638 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
641 if ([df objectForKey:@"ToggleShuffle"] != nil) {
642 ITDebugLog(@"Setting up toggle shuffle hot key.");
643 hotKey = [[ITHotKey alloc] init];
644 [hotKey setName:@"ToggleShuffle"];
645 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
646 [hotKey setTarget:self];
647 [hotKey setAction:@selector(toggleShuffle)];
648 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
651 if ([df objectForKey:@"IncrementVolume"] != nil) {
652 ITDebugLog(@"Setting up increment volume hot key.");
653 hotKey = [[ITHotKey alloc] init];
654 [hotKey setName:@"IncrementVolume"];
655 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
656 [hotKey setTarget:self];
657 [hotKey setAction:@selector(incrementVolume)];
658 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
661 if ([df objectForKey:@"DecrementVolume"] != nil) {
662 ITDebugLog(@"Setting up decrement volume hot key.");
663 hotKey = [[ITHotKey alloc] init];
664 [hotKey setName:@"DecrementVolume"];
665 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
666 [hotKey setTarget:self];
667 [hotKey setAction:@selector(decrementVolume)];
668 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
671 if ([df objectForKey:@"IncrementRating"] != nil) {
672 ITDebugLog(@"Setting up increment rating hot key.");
673 hotKey = [[ITHotKey alloc] init];
674 [hotKey setName:@"IncrementRating"];
675 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
676 [hotKey setTarget:self];
677 [hotKey setAction:@selector(incrementRating)];
678 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
681 if ([df objectForKey:@"DecrementRating"] != nil) {
682 ITDebugLog(@"Setting up decrement rating hot key.");
683 hotKey = [[ITHotKey alloc] init];
684 [hotKey setName:@"DecrementRating"];
685 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
686 [hotKey setTarget:self];
687 [hotKey setAction:@selector(decrementRating)];
688 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
690 ITDebugLog(@"Finished setting up hot keys.");
693 - (void)showCurrentTrackInfo
695 ITMTRemotePlayerSource source = 0;
696 NSString *title = nil;
697 NSString *album = nil;
698 NSString *artist = nil;
699 NSString *time = nil;
700 NSString *track = nil;
705 source = [[self currentRemote] currentSource];
706 title = [[self currentRemote] currentSongTitle];
708 [self networkError:localException];
711 ITDebugLog(@"Showing track info status window.");
715 if ( [df boolForKey:@"showAlbum"] ) {
717 album = [[self currentRemote] currentSongAlbum];
719 [self networkError:localException];
723 if ( [df boolForKey:@"showArtist"] ) {
725 artist = [[self currentRemote] currentSongArtist];
727 [self networkError:localException];
731 if ( [df boolForKey:@"showTime"] ) {
733 time = [NSString stringWithFormat:@"%@: %@ / %@",
735 [[self currentRemote] currentSongElapsed],
736 [[self currentRemote] currentSongLength]];
738 [self networkError:localException];
742 if ( [df boolForKey:@"showTrackNumber"] ) {
747 trackNo = [[self currentRemote] currentSongTrack];
748 trackCount = [[self currentRemote] currentAlbumTrackCount];
750 [self networkError:localException];
753 if ( (trackNo > 0) || (trackCount > 0) ) {
754 track = [NSString stringWithFormat:@"%@: %i %@ %i",
755 @"Track", trackNo, @"of", trackCount];
759 if ( [df boolForKey:@"showTrackRating"] ) {
760 float currentRating = 0;
763 currentRating = [[self currentRemote] currentSongRating];
765 [self networkError:localException];
768 if (currentRating >= 0.0) {
769 rating = ( currentRating * 5 );
773 if ( [df boolForKey:@"showAlbumArtwork"] ) {
775 art = [[self currentRemote] currentSongAlbumArt];
777 [self networkError:localException];
782 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
785 [statusWindowController showSongInfoWindowWithSource:source
795 - (void)showUpcomingSongs
799 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
801 [self networkError:localException];
804 ITDebugLog(@"Showing upcoming songs status window.");
807 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
808 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
809 int curTrack = [[self currentRemote] currentSongIndex];
812 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
814 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
818 if ([songList count] == 0) {
819 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
822 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
824 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
827 [self networkError:localException];
831 - (void)incrementVolume
834 float volume = [[self currentRemote] volume];
835 float dispVol = volume;
836 ITDebugLog(@"Incrementing volume.");
845 ITDebugLog(@"Setting volume to %f", volume);
846 [[self currentRemote] setVolume:volume];
848 // Show volume status window
849 [statusWindowController showVolumeWindowWithLevel:dispVol];
851 [self networkError:localException];
855 - (void)decrementVolume
858 float volume = [[self currentRemote] volume];
859 float dispVol = volume;
860 ITDebugLog(@"Decrementing volume.");
869 ITDebugLog(@"Setting volume to %f", volume);
870 [[self currentRemote] setVolume:volume];
872 //Show volume status window
873 [statusWindowController showVolumeWindowWithLevel:dispVol];
875 [self networkError:localException];
879 - (void)incrementRating
882 float rating = [[self currentRemote] currentSongRating];
883 ITDebugLog(@"Incrementing rating.");
885 if ([[self currentRemote] currentPlaylistIndex] == 0) {
886 ITDebugLog(@"No song playing, rating change aborted.");
894 ITDebugLog(@"Setting rating to %f", rating);
895 [[self currentRemote] setCurrentSongRating:rating];
897 //Show rating status window
898 [statusWindowController showRatingWindowWithRating:rating];
900 [self networkError:localException];
904 - (void)decrementRating
907 float rating = [[self currentRemote] currentSongRating];
908 ITDebugLog(@"Decrementing rating.");
910 if ([[self currentRemote] currentPlaylistIndex] == 0) {
911 ITDebugLog(@"No song playing, rating change aborted.");
919 ITDebugLog(@"Setting rating to %f", rating);
920 [[self currentRemote] setCurrentSongRating:rating];
922 //Show rating status window
923 [statusWindowController showRatingWindowWithRating:rating];
925 [self networkError:localException];
932 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
933 ITDebugLog(@"Toggling repeat mode.");
934 switch (repeatMode) {
935 case ITMTRemotePlayerRepeatOff:
936 repeatMode = ITMTRemotePlayerRepeatAll;
938 case ITMTRemotePlayerRepeatAll:
939 repeatMode = ITMTRemotePlayerRepeatOne;
941 case ITMTRemotePlayerRepeatOne:
942 repeatMode = ITMTRemotePlayerRepeatOff;
945 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
946 [[self currentRemote] setRepeatMode:repeatMode];
948 //Show loop status window
949 [statusWindowController showRepeatWindowWithMode:repeatMode];
951 [self networkError:localException];
955 - (void)toggleShuffle
958 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
959 ITDebugLog(@"Toggling shuffle mode.");
960 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
961 //Show shuffle status window
962 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
963 [statusWindowController showShuffleWindow:newShuffleEnabled];
965 [self networkError:localException];
969 - (void)registerNowOK
971 [[StatusWindow sharedWindow] setLocked:NO];
972 [[StatusWindow sharedWindow] vanish:self];
973 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
978 - (void)registerNowCancel
980 [[StatusWindow sharedWindow] setLocked:NO];
981 [[StatusWindow sharedWindow] vanish:self];
982 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
984 [NSApp terminate:self];
987 /*************************************************************************/
989 #pragma mark NETWORK HANDLERS
990 /*************************************************************************/
992 - (void)setServerStatus:(BOOL)newStatus
996 [networkController setServerStatus:YES];
999 [networkController setServerStatus:NO];
1003 - (int)connectToServer
1006 ITDebugLog(@"Attempting to connect to shared remote.");
1007 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1010 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1011 currentRemote = [[[networkController networkObject] remote] retain];
1012 [refreshTimer invalidate];
1013 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1015 selector:@selector(timerUpdate)
1017 repeats:YES] retain];
1019 ITDebugLog(@"Connection successful.");
1021 } else if (result == 0) {
1022 ITDebugLog(@"Connection failed.");
1023 currentRemote = [remoteArray objectAtIndex:0];
1026 //Do something about the password being invalid
1027 ITDebugLog(@"Connection failed.");
1028 currentRemote = [remoteArray objectAtIndex:0];
1033 - (BOOL)disconnectFromServer
1035 ITDebugLog(@"Disconnecting from shared remote.");
1037 [currentRemote release];
1038 currentRemote = [remoteArray objectAtIndex:0];
1039 [networkController disconnect];
1044 - (void)checkForRemoteServer:(NSTimer *)timer
1046 ITDebugLog(@"Checking for remote server.");
1047 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1048 ITDebugLog(@"Remote server found.");
1050 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1051 [[StatusWindowController sharedController] showReconnectQueryWindow];
1054 ITDebugLog(@"Remote server not found.");
1058 - (void)networkError:(NSException *)exception
1060 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1061 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1062 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);
1063 if ([self disconnectFromServer]) {
1064 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1065 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1067 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1074 if ([self connectToServer] == 0) {
1075 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1077 [[StatusWindow sharedWindow] setLocked:NO];
1078 [[StatusWindow sharedWindow] vanish:self];
1079 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1082 - (void)cancelReconnect
1084 [[StatusWindow sharedWindow] setLocked:NO];
1085 [[StatusWindow sharedWindow] vanish:self];
1086 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1089 /*************************************************************************/
1091 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1092 /*************************************************************************/
1094 - (void)applicationLaunched:(NSNotification *)note
1097 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1098 ITDebugLog(@"Remote application launched.");
1099 playerRunningState = ITMTRemotePlayerRunning;
1100 [[self currentRemote] begin];
1101 [self setLatestSongIdentifier:@""];
1103 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1105 selector:@selector(timerUpdate)
1107 repeats:YES] retain];
1108 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1109 [self setupHotKeys];
1112 [self networkError:localException];
1116 - (void)applicationTerminated:(NSNotification *)note
1119 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1120 ITDebugLog(@"Remote application terminated.");
1121 playerRunningState = ITMTRemotePlayerNotRunning;
1122 [[self currentRemote] halt];
1123 [refreshTimer invalidate];
1124 [refreshTimer release];
1126 [self clearHotKeys];
1128 if ([df objectForKey:@"ShowPlayer"] != nil) {
1130 ITDebugLog(@"Setting up show player hot key.");
1131 hotKey = [[ITHotKey alloc] init];
1132 [hotKey setName:@"ShowPlayer"];
1133 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1134 [hotKey setTarget:self];
1135 [hotKey setAction:@selector(showPlayer)];
1136 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1140 [self networkError:localException];
1145 /*************************************************************************/
1147 #pragma mark NSApplication DELEGATE METHODS
1148 /*************************************************************************/
1150 - (void)applicationWillTerminate:(NSNotification *)note
1152 [networkController stopRemoteServerSearch];
1153 [self clearHotKeys];
1154 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1158 /*************************************************************************/
1160 #pragma mark DEALLOCATION METHOD
1161 /*************************************************************************/
1165 [self applicationTerminated:nil];
1167 [statusItem release];
1168 [statusWindowController release];
1169 [menuController release];
1170 [networkController release];