1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import <ITKit/ITHotKeyCenter.h>
5 #import <ITKit/ITHotKey.h>
6 #import <ITKit/ITKeyCombo.h>
7 #import "StatusWindowController.h"
8 #import "StatusItemHack.h"
10 @interface MainController(Private)
11 - (ITMTRemote *)loadRemote;
13 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
14 - (void)showCurrentTrackInfo;
15 - (void)applicationLaunched:(NSNotification *)note;
16 - (void)applicationTerminated:(NSNotification *)note;
19 static MainController *sharedController;
21 @implementation MainController
23 + (MainController *)sharedController
25 return sharedController;
28 /*************************************************************************/
30 #pragma mark INITIALIZATION/DEALLOCATION METHODS
31 /*************************************************************************/
35 if ( ( self = [super init] ) ) {
36 sharedController = self;
38 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
39 statusWindowController = [StatusWindowController sharedController];
40 menuController = [[MenuController alloc] init];
41 df = [[NSUserDefaults standardUserDefaults] retain];
47 - (void)applicationDidFinishLaunching:(NSNotification *)note
49 //Turn on debug mode if needed
50 if ([df boolForKey:@"ITDebugMode"]) {
54 bling = [[MTBlingController alloc] init];
57 currentRemote = [self loadRemote];
58 [currentRemote begin];
60 //Setup for notification of the remote player launching or quitting
61 [[[NSWorkspace sharedWorkspace] notificationCenter]
63 selector:@selector(applicationTerminated:)
64 name:NSWorkspaceDidTerminateApplicationNotification
67 [[[NSWorkspace sharedWorkspace] notificationCenter]
69 selector:@selector(applicationLaunched:)
70 name:NSWorkspaceDidLaunchApplicationNotification
73 if ( ! [df objectForKey:@"menu"] ) { // If this is nil, defaults have never been registered.
74 [[PreferencesController sharedPrefs] registerDefaults];
77 [StatusItemHack install];
78 statusItem = [[ITStatusItem alloc]
79 initWithStatusBar:[NSStatusBar systemStatusBar]
80 withLength:NSSquareStatusItemLength];
82 if ([currentRemote playerRunningState] == ITMTRemotePlayerRunning) {
83 [self applicationLaunched:nil];
85 if ([df boolForKey:@"LaunchPlayerWithMT"])
88 [self applicationTerminated:nil];
91 [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
92 [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
97 - (ITMTRemote *)loadRemote
99 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
100 ITDebugLog(@"Gathering remotes.");
102 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
103 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
104 NSString *bundlePath;
106 while ( (bundlePath = [enumerator nextObject]) ) {
107 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
110 Class remoteClass = [remoteBundle principalClass];
112 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
113 [remoteClass isKindOfClass:[NSObject class]]) {
114 id remote = [remoteClass remote];
115 ITDebugLog(@"Adding remote at path %@", bundlePath);
116 [remoteArray addObject:remote];
121 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
122 // if ( [remoteArray count] > 1 ) {
123 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
125 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
128 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
129 return [remoteArray objectAtIndex:0];
132 /*************************************************************************/
134 #pragma mark INSTANCE METHODS
135 /*************************************************************************/
137 /*- (void)startTimerInNewThread
139 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
140 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
141 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
143 selector:@selector(timerUpdate)
145 repeats:YES] retain];
147 ITDebugLog(@"Timer started.");
153 NSDate *now = [NSDate date];
154 if ( (! blingDate) || ([now timeIntervalSinceDate:blingDate] >= 86400) ) {
155 [bling showPanelIfNeeded];
156 [blingDate autorelease];
157 blingDate = [now retain];
164 [blingDate autorelease];
165 blingDate = [[NSDate date] retain];
170 if ( ! ([bling checkDone] == 2475) ) {
177 - (BOOL)songIsPlaying
179 return ( ! ([[currentRemote playerStateUniqueIdentifier] isEqualToString:@"0-0"]) );
182 - (BOOL)radioIsPlaying
184 return ( [currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist );
189 return ( ! [[currentRemote playerStateUniqueIdentifier] isEqualToString:_latestSongIdentifier] );
192 - (NSString *)latestSongIdentifier
194 return _latestSongIdentifier;
197 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
199 ITDebugLog(@"Setting latest song identifier to %@", newIdentifier);
200 [_latestSongIdentifier autorelease];
201 _latestSongIdentifier = [newIdentifier copy];
206 if ( [self songChanged] && (timerUpdating != YES) ) {
207 ITDebugLog(@"The song changed.");
209 latestPlaylistClass = [currentRemote currentPlaylistClass];
210 [menuController rebuildSubmenus];
212 if ( [df boolForKey:@"showSongInfoOnChange"] ) {
213 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
216 [self setLatestSongIdentifier:[currentRemote playerStateUniqueIdentifier]];
224 ITDebugLog(@"Menu clicked.");
225 if ([currentRemote playerRunningState] == ITMTRemotePlayerRunning) {
226 [statusItem setMenu:[menuController menu]];
228 [statusItem setMenu:[menuController menuForNoPlayer]];
240 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
241 ITDebugLog(@"Play/Pause toggled");
242 if (state == ITMTRemotePlayerPlaying) {
243 [currentRemote pause];
244 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
245 [currentRemote pause];
246 [currentRemote play];
248 [currentRemote play];
255 ITDebugLog(@"Going to next song.");
256 [currentRemote goToNextSong];
262 ITDebugLog(@"Going to previous song.");
263 [currentRemote goToPreviousSong];
269 ITDebugLog(@"Fast forwarding.");
270 [currentRemote forward];
276 ITDebugLog(@"Rewinding.");
277 [currentRemote rewind];
281 - (void)selectPlaylistAtIndex:(int)index
283 ITDebugLog(@"Selecting playlist %i", index);
284 [currentRemote switchToPlaylistAtIndex:index];
288 - (void)selectSongAtIndex:(int)index
290 ITDebugLog(@"Selecting song %i", index);
291 [currentRemote switchToSongAtIndex:index];
295 - (void)selectSongRating:(int)rating
297 ITDebugLog(@"Selecting song rating %i", rating);
298 [currentRemote setCurrentSongRating:(float)rating / 100.0];
302 - (void)selectEQPresetAtIndex:(int)index
304 ITDebugLog(@"Selecting EQ preset %i", index);
305 [currentRemote switchToEQAtIndex:index];
311 ITDebugLog(@"Beginning show player.");
312 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
313 ITDebugLog(@"Showing player interface.");
314 [currentRemote showPrimaryInterface];
316 ITDebugLog(@"Launching player.");
317 if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
318 ITDebugLog(@"Error Launching Player");
321 ITDebugLog(@"Finished show player.");
324 - (void)showPreferences
326 ITDebugLog(@"Show preferences.");
327 [[PreferencesController sharedPrefs] setController:self];
328 [[PreferencesController sharedPrefs] showPrefsWindow:self];
331 - (void)quitMenuTunes
333 ITDebugLog(@"Quitting MenuTunes.");
334 [NSApp terminate:self];
340 - (void)closePreferences
342 ITDebugLog(@"Preferences closed.");
343 if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
348 - (ITMTRemote *)currentRemote
350 return currentRemote;
361 NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
362 ITHotKey *nextHotKey;
363 ITDebugLog(@"Clearing hot keys.");
364 while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
365 [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
367 ITDebugLog(@"Done clearing hot keys.");
373 ITDebugLog(@"Setting up hot keys.");
375 if (playerRunningState == ITMTRemotePlayerNotRunning) {
379 if ([df objectForKey:@"PlayPause"] != nil) {
380 ITDebugLog(@"Setting up play pause hot key.");
381 hotKey = [[ITHotKey alloc] init];
382 [hotKey setName:@"PlayPause"];
383 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
384 [hotKey setTarget:self];
385 [hotKey setAction:@selector(playPause)];
386 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
389 if ([df objectForKey:@"NextTrack"] != nil) {
390 ITDebugLog(@"Setting up next track hot key.");
391 hotKey = [[ITHotKey alloc] init];
392 [hotKey setName:@"NextTrack"];
393 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
394 [hotKey setTarget:self];
395 [hotKey setAction:@selector(nextSong)];
396 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
399 if ([df objectForKey:@"PrevTrack"] != nil) {
400 ITDebugLog(@"Setting up previous track hot key.");
401 hotKey = [[ITHotKey alloc] init];
402 [hotKey setName:@"PrevTrack"];
403 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
404 [hotKey setTarget:self];
405 [hotKey setAction:@selector(prevSong)];
406 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
409 if ([df objectForKey:@"ShowPlayer"] != nil) {
410 ITDebugLog(@"Setting up show player hot key.");
411 hotKey = [[ITHotKey alloc] init];
412 [hotKey setName:@"ShowPlayer"];
413 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
414 [hotKey setTarget:self];
415 [hotKey setAction:@selector(showPlayer)];
416 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
419 if ([df objectForKey:@"TrackInfo"] != nil) {
420 ITDebugLog(@"Setting up track info hot key.");
421 hotKey = [[ITHotKey alloc] init];
422 [hotKey setName:@"TrackInfo"];
423 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
424 [hotKey setTarget:self];
425 [hotKey setAction:@selector(showCurrentTrackInfo)];
426 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
429 if ([df objectForKey:@"UpcomingSongs"] != nil) {
430 ITDebugLog(@"Setting up upcoming songs hot key.");
431 hotKey = [[ITHotKey alloc] init];
432 [hotKey setName:@"UpcomingSongs"];
433 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
434 [hotKey setTarget:self];
435 [hotKey setAction:@selector(showUpcomingSongs)];
436 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
439 if ([df objectForKey:@"ToggleLoop"] != nil) {
440 ITDebugLog(@"Setting up toggle loop hot key.");
441 hotKey = [[ITHotKey alloc] init];
442 [hotKey setName:@"ToggleLoop"];
443 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
444 [hotKey setTarget:self];
445 [hotKey setAction:@selector(toggleLoop)];
446 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
449 if ([df objectForKey:@"ToggleShuffle"] != nil) {
450 ITDebugLog(@"Setting up toggle shuffle hot key.");
451 hotKey = [[ITHotKey alloc] init];
452 [hotKey setName:@"ToggleShuffle"];
453 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
454 [hotKey setTarget:self];
455 [hotKey setAction:@selector(toggleShuffle)];
456 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
459 if ([df objectForKey:@"IncrementVolume"] != nil) {
460 ITDebugLog(@"Setting up increment volume hot key.");
461 hotKey = [[ITHotKey alloc] init];
462 [hotKey setName:@"IncrementVolume"];
463 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
464 [hotKey setTarget:self];
465 [hotKey setAction:@selector(incrementVolume)];
466 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
469 if ([df objectForKey:@"DecrementVolume"] != nil) {
470 ITDebugLog(@"Setting up decrement volume hot key.");
471 hotKey = [[ITHotKey alloc] init];
472 [hotKey setName:@"DecrementVolume"];
473 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
474 [hotKey setTarget:self];
475 [hotKey setAction:@selector(decrementVolume)];
476 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
479 if ([df objectForKey:@"IncrementRating"] != nil) {
480 ITDebugLog(@"Setting up increment rating hot key.");
481 hotKey = [[ITHotKey alloc] init];
482 [hotKey setName:@"IncrementRating"];
483 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
484 [hotKey setTarget:self];
485 [hotKey setAction:@selector(incrementRating)];
486 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
489 if ([df objectForKey:@"DecrementRating"] != nil) {
490 ITDebugLog(@"Setting up decrement rating hot key.");
491 hotKey = [[ITHotKey alloc] init];
492 [hotKey setName:@"DecrementRating"];
493 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
494 [hotKey setTarget:self];
495 [hotKey setAction:@selector(decrementRating)];
496 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
498 ITDebugLog(@"Finished setting up hot keys.");
501 - (void)showCurrentTrackInfo
503 ITMTRemotePlayerSource source = [currentRemote currentSource];
504 NSString *title = [currentRemote currentSongTitle];
505 NSString *album = nil;
506 NSString *artist = nil;
507 NSString *time = nil;
508 NSString *track = nil;
511 ITDebugLog(@"Showing track info status window.");
515 if ( [df boolForKey:@"showAlbum"] ) {
516 album = [currentRemote currentSongAlbum];
519 if ( [df boolForKey:@"showArtist"] ) {
520 artist = [currentRemote currentSongArtist];
523 if ( [df boolForKey:@"showTime"] ) {
524 time = [NSString stringWithFormat:@"%@: %@ / %@",
526 [currentRemote currentSongElapsed],
527 [currentRemote currentSongLength]];
530 if ( [df boolForKey:@"showTrackNumber"] ) {
531 int trackNo = [currentRemote currentSongTrack];
532 int trackCount = [currentRemote currentAlbumTrackCount];
534 if ( (trackNo > 0) || (trackCount > 0) ) {
535 track = [NSString stringWithFormat:@"%@: %i %@ %i",
536 @"Track", trackNo, @"of", trackCount];
540 if ( [df boolForKey:@"showTrackRating"] ) {
541 float currentRating = [currentRemote currentSongRating];
542 if (currentRating >= 0.0) {
543 rating = ( currentRating * 5 );
548 title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
551 [statusWindowController showSongInfoWindowWithSource:source
560 - (void)showUpcomingSongs
562 int curPlaylist = [currentRemote currentPlaylistIndex];
563 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
564 ITDebugLog(@"Showing upcoming songs status window.");
566 NSMutableArray *songList = [NSMutableArray arrayWithCapacity:5];
567 int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
568 int curTrack = [currentRemote currentSongIndex];
571 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
573 [songList addObject:[currentRemote songTitleAtIndex:i]];
577 [statusWindowController showUpcomingSongsWindowWithTitles:songList];
580 [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
584 - (void)incrementVolume
586 float volume = [currentRemote volume];
587 float dispVol = volume;
588 ITDebugLog(@"Incrementing volume.");
597 ITDebugLog(@"Setting volume to %f", volume);
598 [currentRemote setVolume:volume];
600 // Show volume status window
601 [statusWindowController showVolumeWindowWithLevel:dispVol];
604 - (void)decrementVolume
606 float volume = [currentRemote volume];
607 float dispVol = volume;
608 ITDebugLog(@"Decrementing volume.");
617 ITDebugLog(@"Setting volume to %f", volume);
618 [currentRemote setVolume:volume];
620 //Show volume status window
621 [statusWindowController showVolumeWindowWithLevel:dispVol];
624 - (void)incrementRating
626 float rating = [currentRemote currentSongRating];
627 ITDebugLog(@"Incrementing rating.");
629 if ([currentRemote currentPlaylistIndex] == 0) {
630 ITDebugLog(@"No song playing, rating change aborted.");
638 ITDebugLog(@"Setting rating to %f", rating);
639 [currentRemote setCurrentSongRating:rating];
641 //Show rating status window
642 [statusWindowController showRatingWindowWithRating:rating];
645 - (void)decrementRating
647 float rating = [currentRemote currentSongRating];
648 ITDebugLog(@"Decrementing rating.");
650 if ([currentRemote currentPlaylistIndex] == 0) {
651 ITDebugLog(@"No song playing, rating change aborted.");
659 ITDebugLog(@"Setting rating to %f", rating);
660 [currentRemote setCurrentSongRating:rating];
662 //Show rating status window
663 [statusWindowController showRatingWindowWithRating:rating];
668 ITMTRemotePlayerRepeatMode repeatMode = [currentRemote repeatMode];
669 ITDebugLog(@"Toggling repeat mode.");
670 switch (repeatMode) {
671 case ITMTRemotePlayerRepeatOff:
672 repeatMode = ITMTRemotePlayerRepeatAll;
674 case ITMTRemotePlayerRepeatAll:
675 repeatMode = ITMTRemotePlayerRepeatOne;
677 case ITMTRemotePlayerRepeatOne:
678 repeatMode = ITMTRemotePlayerRepeatOff;
681 ITDebugLog(@"Setting repeat mode to %i", repeatMode);
682 [currentRemote setRepeatMode:repeatMode];
684 //Show loop status window
685 [statusWindowController showRepeatWindowWithMode:repeatMode];
688 - (void)toggleShuffle
690 BOOL newShuffleEnabled = ( ! [currentRemote shuffleEnabled] );
691 ITDebugLog(@"Toggling shuffle mode.");
692 [currentRemote setShuffleEnabled:newShuffleEnabled];
693 //Show shuffle status window
694 ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
695 [statusWindowController showShuffleWindow:newShuffleEnabled];
698 - (void)registerNowOK
700 [[StatusWindow sharedWindow] setLocked:NO];
701 [[StatusWindow sharedWindow] vanish:self];
702 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
707 - (void)registerNowCancel
709 [[StatusWindow sharedWindow] setLocked:NO];
710 [[StatusWindow sharedWindow] vanish:self];
711 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
717 /*************************************************************************/
719 #pragma mark WORKSPACE NOTIFICATION HANDLERS
720 /*************************************************************************/
722 - (void)applicationLaunched:(NSNotification *)note
724 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
725 ITDebugLog(@"Remote application launched.");
726 [currentRemote begin];
727 [self setLatestSongIdentifier:@""];
729 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
731 selector:@selector(timerUpdate)
733 repeats:YES] retain];
734 //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
736 playerRunningState = ITMTRemotePlayerRunning;
740 - (void)applicationTerminated:(NSNotification *)note
742 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
743 ITDebugLog(@"Remote application terminated.");
744 [currentRemote halt];
745 [refreshTimer invalidate];
746 [refreshTimer release];
749 playerRunningState = ITMTRemotePlayerNotRunning;
754 /*************************************************************************/
756 #pragma mark NSApplication DELEGATE METHODS
757 /*************************************************************************/
759 - (void)applicationWillTerminate:(NSNotification *)note
762 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
766 /*************************************************************************/
768 #pragma mark DEALLOCATION METHOD
769 /*************************************************************************/
773 [self applicationTerminated:nil];
775 [statusItem release];
776 [statusWindowController release];
777 [menuController release];