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.");
502 if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
504 path = [[self currentRemote] playerFullName];
506 if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
507 ITDebugLog(@"Error Launching Player");
510 [self networkError:localException];
513 ITDebugLog(@"Finished show player.");
516 - (void)showPreferences
518 ITDebugLog(@"Show preferences.");
519 [[PreferencesController sharedPrefs] showPrefsWindow:self];
522 - (void)showPreferencesAndClose
524 ITDebugLog(@"Show preferences.");
525 [[PreferencesController sharedPrefs] showPrefsWindow:self];
526 [[StatusWindow sharedWindow] setLocked:NO];
527 [[StatusWindow sharedWindow] vanish:self];
528 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
531 - (void)showTestWindow
533 [self showCurrentTrackInfo];
536 - (void)quitMenuTunes
538 ITDebugLog(@"Quitting MenuTunes.");
539 [NSApp terminate:self];
545 - (void)closePreferences
547 ITDebugLog(@"Preferences closed.");
548 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
553 - (ITMTRemote *)currentRemote
555 if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
556 [self networkError:nil];
559 return currentRemote;
570 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
571 ITHotKey *nextHotKey;
572 ITDebugLog(@"Clearing hot keys.");
573 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
574 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
576 ITDebugLog(@"Done clearing hot keys.");
582 ITDebugLog(@"Setting up hot keys.");
584 if (playerRunningState == ITMTRemotePlayerNotRunning) {
588 if ([df objectForKey:@"PlayPause"] != nil) {
589 ITDebugLog(@"Setting up play pause hot key.");
590 hotKey = [[ITHotKey alloc] init];
591 [hotKey setName:@"PlayPause"];
592 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
593 [hotKey setTarget:self];
594 [hotKey setAction:@selector(playPause)];
595 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
598 if ([df objectForKey:@"NextTrack"] != nil) {
599 ITDebugLog(@"Setting up next track hot key.");
600 hotKey = [[ITHotKey alloc] init];
601 [hotKey setName:@"NextTrack"];
602 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
603 [hotKey setTarget:self];
604 [hotKey setAction:@selector(nextSong)];
605 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
608 if ([df objectForKey:@"PrevTrack"] != nil) {
609 ITDebugLog(@"Setting up previous track hot key.");
610 hotKey = [[ITHotKey alloc] init];
611 [hotKey setName:@"PrevTrack"];
612 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
613 [hotKey setTarget:self];
614 [hotKey setAction:@selector(prevSong)];
615 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
618 if ([df objectForKey:@"ShowPlayer"] != nil) {
619 ITDebugLog(@"Setting up show player hot key.");
620 hotKey = [[ITHotKey alloc] init];
621 [hotKey setName:@"ShowPlayer"];
622 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
623 [hotKey setTarget:self];
624 [hotKey setAction:@selector(showPlayer)];
625 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
628 if ([df objectForKey:@"TrackInfo"] != nil) {
629 ITDebugLog(@"Setting up track info hot key.");
630 hotKey = [[ITHotKey alloc] init];
631 [hotKey setName:@"TrackInfo"];
632 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
633 [hotKey setTarget:self];
634 [hotKey setAction:@selector(showCurrentTrackInfo)];
635 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
638 if ([df objectForKey:@"UpcomingSongs"] != nil) {
639 ITDebugLog(@"Setting up upcoming songs hot key.");
640 hotKey = [[ITHotKey alloc] init];
641 [hotKey setName:@"UpcomingSongs"];
642 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
643 [hotKey setTarget:self];
644 [hotKey setAction:@selector(showUpcomingSongs)];
645 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
648 if ([df objectForKey:@"ToggleLoop"] != nil) {
649 ITDebugLog(@"Setting up toggle loop hot key.");
650 hotKey = [[ITHotKey alloc] init];
651 [hotKey setName:@"ToggleLoop"];
652 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
653 [hotKey setTarget:self];
654 [hotKey setAction:@selector(toggleLoop)];
655 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
658 if ([df objectForKey:@"ToggleShuffle"] != nil) {
659 ITDebugLog(@"Setting up toggle shuffle hot key.");
660 hotKey = [[ITHotKey alloc] init];
661 [hotKey setName:@"ToggleShuffle"];
662 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
663 [hotKey setTarget:self];
664 [hotKey setAction:@selector(toggleShuffle)];
665 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
668 if ([df objectForKey:@"IncrementVolume"] != nil) {
669 ITDebugLog(@"Setting up increment volume hot key.");
670 hotKey = [[ITHotKey alloc] init];
671 [hotKey setName:@"IncrementVolume"];
672 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
673 [hotKey setTarget:self];
674 [hotKey setAction:@selector(incrementVolume)];
675 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
678 if ([df objectForKey:@"DecrementVolume"] != nil) {
679 ITDebugLog(@"Setting up decrement volume hot key.");
680 hotKey = [[ITHotKey alloc] init];
681 [hotKey setName:@"DecrementVolume"];
682 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
683 [hotKey setTarget:self];
684 [hotKey setAction:@selector(decrementVolume)];
685 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
688 if ([df objectForKey:@"IncrementRating"] != nil) {
689 ITDebugLog(@"Setting up increment rating hot key.");
690 hotKey = [[ITHotKey alloc] init];
691 [hotKey setName:@"IncrementRating"];
692 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
693 [hotKey setTarget:self];
694 [hotKey setAction:@selector(incrementRating)];
695 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
698 if ([df objectForKey:@"DecrementRating"] != nil) {
699 ITDebugLog(@"Setting up decrement rating hot key.");
700 hotKey = [[ITHotKey alloc] init];
701 [hotKey setName:@"DecrementRating"];
702 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
703 [hotKey setTarget:self];
704 [hotKey setAction:@selector(decrementRating)];
705 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
707 ITDebugLog(@"Finished setting up hot keys.");
710 - (void)showCurrentTrackInfo
712 ITMTRemotePlayerSource source = 0;
713 NSString *title = nil;
714 NSString *album = nil;
715 NSString *artist = nil;
716 NSString *time = nil;
717 NSString *track = nil;
722 source = [[self currentRemote] currentSource];
723 title = [[self currentRemote] currentSongTitle];
725 [self networkError:localException];
728 ITDebugLog(@"Showing track info status window.");
732 if ( [df boolForKey:@"showAlbum"] ) {
734 album = [[self currentRemote] currentSongAlbum];
736 [self networkError:localException];
740 if ( [df boolForKey:@"showArtist"] ) {
742 artist = [[self currentRemote] currentSongArtist];
744 [self networkError:localException];
748 if ( [df boolForKey:@"showTime"] ) {
750 time = [NSString stringWithFormat:@"%@: %@ / %@",
752 [[self currentRemote] currentSongElapsed],
753 [[self currentRemote] currentSongLength]];
755 [self networkError:localException];
759 if ( [df boolForKey:@"showTrackNumber"] ) {
764 trackNo = [[self currentRemote] currentSongTrack];
765 trackCount = [[self currentRemote] currentAlbumTrackCount];
767 [self networkError:localException];
770 if ( (trackNo > 0) || (trackCount > 0) ) {
771 track = [NSString stringWithFormat:@"%@: %i %@ %i",
772 @"Track", trackNo, @"of", trackCount];
776 if ( [df boolForKey:@"showTrackRating"] ) {
777 float currentRating = 0;
780 currentRating = [[self currentRemote] currentSongRating];
782 [self networkError:localException];
785 if (currentRating >= 0.0) {
786 rating = ( currentRating * 5 );
790 if ( [df boolForKey:@"showAlbumArtwork"] ) {
792 art = [[self currentRemote] currentSongAlbumArt];
794 [self networkError:localException];
799 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
802 [statusWindowController showSongInfoWindowWithSource:source
812 - (void)showUpcomingSongs
816 numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
818 [self networkError:localException];
821 ITDebugLog(@"Showing upcoming songs status window.");
824 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
825 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
826 int curTrack = [[self currentRemote] currentSongIndex];
829 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
831 [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
835 if ([songList count] == 0) {
836 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
839 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
841 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
844 [self networkError:localException];
848 - (void)incrementVolume
851 float volume = [[self currentRemote] volume];
852 float dispVol = volume;
853 ITDebugLog(@"Incrementing volume.");
862 ITDebugLog(@"Setting volume to %f", volume);
863 [[self currentRemote] setVolume:volume];
865 // Show volume status window
866 [statusWindowController showVolumeWindowWithLevel:dispVol];
868 [self networkError:localException];
872 - (void)decrementVolume
875 float volume = [[self currentRemote] volume];
876 float dispVol = volume;
877 ITDebugLog(@"Decrementing volume.");
886 ITDebugLog(@"Setting volume to %f", volume);
887 [[self currentRemote] setVolume:volume];
889 //Show volume status window
890 [statusWindowController showVolumeWindowWithLevel:dispVol];
892 [self networkError:localException];
896 - (void)incrementRating
899 float rating = [[self currentRemote] currentSongRating];
900 ITDebugLog(@"Incrementing rating.");
902 if ([[self currentRemote] currentPlaylistIndex] == 0) {
903 ITDebugLog(@"No song playing, rating change aborted.");
911 ITDebugLog(@"Setting rating to %f", rating);
912 [[self currentRemote] setCurrentSongRating:rating];
914 //Show rating status window
915 [statusWindowController showRatingWindowWithRating:rating];
917 [self networkError:localException];
921 - (void)decrementRating
924 float rating = [[self currentRemote] currentSongRating];
925 ITDebugLog(@"Decrementing rating.");
927 if ([[self currentRemote] currentPlaylistIndex] == 0) {
928 ITDebugLog(@"No song playing, rating change aborted.");
936 ITDebugLog(@"Setting rating to %f", rating);
937 [[self currentRemote] setCurrentSongRating:rating];
939 //Show rating status window
940 [statusWindowController showRatingWindowWithRating:rating];
942 [self networkError:localException];
949 ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
950 ITDebugLog(@"Toggling repeat mode.");
951 switch (repeatMode) {
952 case ITMTRemotePlayerRepeatOff:
953 repeatMode = ITMTRemotePlayerRepeatAll;
955 case ITMTRemotePlayerRepeatAll:
956 repeatMode = ITMTRemotePlayerRepeatOne;
958 case ITMTRemotePlayerRepeatOne:
959 repeatMode = ITMTRemotePlayerRepeatOff;
962 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
963 [[self currentRemote] setRepeatMode:repeatMode];
965 //Show loop status window
966 [statusWindowController showRepeatWindowWithMode:repeatMode];
968 [self networkError:localException];
972 - (void)toggleShuffle
975 BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
976 ITDebugLog(@"Toggling shuffle mode.");
977 [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
978 //Show shuffle status window
979 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
980 [statusWindowController showShuffleWindow:newShuffleEnabled];
982 [self networkError:localException];
986 - (void)registerNowOK
988 [[StatusWindow sharedWindow] setLocked:NO];
989 [[StatusWindow sharedWindow] vanish:self];
990 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
995 - (void)registerNowCancel
997 [[StatusWindow sharedWindow] setLocked:NO];
998 [[StatusWindow sharedWindow] vanish:self];
999 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1001 [NSApp terminate:self];
1004 /*************************************************************************/
1006 #pragma mark NETWORK HANDLERS
1007 /*************************************************************************/
1009 - (void)setServerStatus:(BOOL)newStatus
1013 [networkController setServerStatus:YES];
1016 [networkController setServerStatus:NO];
1020 - (int)connectToServer
1023 ITDebugLog(@"Attempting to connect to shared remote.");
1024 result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1027 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1028 currentRemote = [[[networkController networkObject] remote] retain];
1029 [refreshTimer invalidate];
1030 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1032 selector:@selector(timerUpdate)
1034 repeats:YES] retain];
1036 ITDebugLog(@"Connection successful.");
1038 } else if (result == 0) {
1039 ITDebugLog(@"Connection failed.");
1040 currentRemote = [remoteArray objectAtIndex:0];
1043 //Do something about the password being invalid
1044 ITDebugLog(@"Connection failed.");
1045 currentRemote = [remoteArray objectAtIndex:0];
1050 - (BOOL)disconnectFromServer
1052 ITDebugLog(@"Disconnecting from shared remote.");
1054 [currentRemote release];
1055 currentRemote = [remoteArray objectAtIndex:0];
1056 [networkController disconnect];
1061 - (void)checkForRemoteServer:(NSTimer *)timer
1063 ITDebugLog(@"Checking for remote server.");
1064 if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1065 ITDebugLog(@"Remote server found.");
1067 if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1068 [[StatusWindowController sharedController] showReconnectQueryWindow];
1071 ITDebugLog(@"Remote server not found.");
1075 - (void)networkError:(NSException *)exception
1077 ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1078 if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1079 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);
1080 if ([self disconnectFromServer]) {
1081 [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1082 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1084 ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1091 if ([self connectToServer] == 0) {
1092 [NSTimer scheduledTimerWithTimeInterval:45 target:self selector:@selector(checkForRemoteServer:) userInfo:nil repeats:YES];
1094 [[StatusWindow sharedWindow] setLocked:NO];
1095 [[StatusWindow sharedWindow] vanish:self];
1096 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1099 - (void)cancelReconnect
1101 [[StatusWindow sharedWindow] setLocked:NO];
1102 [[StatusWindow sharedWindow] vanish:self];
1103 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1106 /*************************************************************************/
1108 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1109 /*************************************************************************/
1111 - (void)applicationLaunched:(NSNotification *)note
1114 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1115 ITDebugLog(@"Remote application launched.");
1116 playerRunningState = ITMTRemotePlayerRunning;
1117 [[self currentRemote] begin];
1118 [self setLatestSongIdentifier:@""];
1120 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1122 selector:@selector(timerUpdate)
1124 repeats:YES] retain];
1125 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1126 [self setupHotKeys];
1129 [self networkError:localException];
1133 - (void)applicationTerminated:(NSNotification *)note
1136 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]]) {
1137 ITDebugLog(@"Remote application terminated.");
1138 playerRunningState = ITMTRemotePlayerNotRunning;
1139 [[self currentRemote] halt];
1140 [refreshTimer invalidate];
1141 [refreshTimer release];
1143 [self clearHotKeys];
1145 if ([df objectForKey:@"ShowPlayer"] != nil) {
1147 ITDebugLog(@"Setting up show player hot key.");
1148 hotKey = [[ITHotKey alloc] init];
1149 [hotKey setName:@"ShowPlayer"];
1150 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1151 [hotKey setTarget:self];
1152 [hotKey setAction:@selector(showPlayer)];
1153 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1157 [self networkError:localException];
1162 /*************************************************************************/
1164 #pragma mark NSApplication DELEGATE METHODS
1165 /*************************************************************************/
1167 - (void)applicationWillTerminate:(NSNotification *)note
1169 [networkController stopRemoteServerSearch];
1170 [self clearHotKeys];
1171 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1175 /*************************************************************************/
1177 #pragma mark DEALLOCATION METHOD
1178 /*************************************************************************/
1182 [self applicationTerminated:nil];
1184 [statusItem release];
1185 [statusWindowController release];
1186 [menuController release];
1187 [networkController release];