1 #import "MainController.h"
2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
6 @interface MainController(Private)
7 - (ITMTRemote *)loadRemote;
9 - (void)rebuildUpcomingSongsMenu;
10 - (void)rebuildPlaylistMenu;
11 - (void)rebuildEQPresetsMenu;
14 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
15 onItem:(NSMenuItem *)item;
19 @implementation MainController
21 /*************************************************************************/
23 #pragma mark INITIALIZATION/DEALLOCATION METHODS
24 /*************************************************************************/
28 if ( ( self = [super init] ) ) {
29 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
30 statusWindow = [StatusWindow sharedWindow];
38 [refreshTimer invalidate];
39 [refreshTimer release];
48 - (void)applicationDidFinishLaunching:(NSNotification *)note
50 currentRemote = [self loadRemote];
51 [currentRemote begin];
53 //Setup for notification of the remote player launching or quitting
54 [[[NSWorkspace sharedWorkspace] notificationCenter]
56 selector:@selector(applicationTerminated:)
57 name:NSWorkspaceDidTerminateApplicationNotification
60 [[[NSWorkspace sharedWorkspace] notificationCenter]
62 selector:@selector(applicationLaunched:)
63 name:NSWorkspaceDidLaunchApplicationNotification
66 [self registerDefaults];
68 statusItem = [[ITStatusItem alloc]
69 initWithStatusBar:[NSStatusBar systemStatusBar]
70 withLength:NSSquareStatusItemLength];
72 menu = [[NSMenu alloc] initWithTitle:@""];
73 if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) {
74 [self applicationLaunched:nil];
76 [self applicationTerminated:nil];
79 [statusItem setImage:[NSImage imageNamed:@"menu"]];
80 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
81 // Below line of code is for creating builds for Beta Testers
82 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
85 - (void)applicationWillTerminate:(NSNotification *)note
88 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
91 - (ITMTRemote *)loadRemote
93 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
96 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
97 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
100 while ( (bundlePath = [enumerator nextObject]) ) {
101 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
104 Class remoteClass = [remoteBundle principalClass];
106 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
107 [remoteClass isKindOfClass:[NSObject class]]) {
109 id remote = [remoteClass remote];
110 [remoteArray addObject:remote];
115 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
116 // if ( [remoteArray count] > 1 ) {
117 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
119 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
122 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
123 return [remoteArray objectAtIndex:0];
129 - (void)applicationLaunched:(NSNotification *)note
131 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
132 [NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
135 [statusItem setMenu:menu];
137 isAppRunning = ITMTRemotePlayerRunning;
141 isAppRunning = ITMTRemotePlayerRunning;
144 - (void)applicationTerminated:(NSNotification *)note
146 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
147 NSMenu *notRunningMenu = [[NSMenu alloc] initWithTitle:@""];
148 [[notRunningMenu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self];
149 [notRunningMenu addItem:[NSMenuItem separatorItem]];
150 [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
151 [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
152 [statusItem setMenu:[notRunningMenu autorelease]];
154 [refreshTimer invalidate];
155 [refreshTimer release];
163 /*************************************************************************/
165 #pragma mark INSTANCE METHODS
166 /*************************************************************************/
168 - (void)registerDefaults
170 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
171 if (![defaults objectForKey:@"menu"]) {
173 NSMutableDictionary *loginwindow;
174 NSMutableArray *loginarray;
178 [NSArray arrayWithObjects:
192 @"Current Track Info",
193 nil] forKey:@"menu"];
195 [defaults synchronize];
196 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
197 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
199 for (i = 0; i < [loginarray count]; i++) {
200 NSDictionary *tempDict = [loginarray objectAtIndex:i];
201 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:
202 [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
209 //We must fix it so it is no longer suxy
211 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
212 AEDesc scriptDesc, resultDesc;
213 NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
214 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
216 AECreateDesc(typeChar, [script cString], [script cStringLength],
219 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
221 AEDisposeDesc(&scriptDesc);
222 AEDisposeDesc(&resultDesc);
224 CloseComponent(asComponent);
229 if (![defaults integerForKey:@"SongsInAdvance"])
231 [defaults setInteger:5 forKey:@"SongsInAdvance"];
234 if (![defaults objectForKey:@"showName"]) {
235 [defaults setBool:YES forKey:@"showName"];
238 if (![defaults objectForKey:@"showArtist"]) {
239 [defaults setBool:YES forKey:@"showArtist"];
242 if (![defaults objectForKey:@"showAlbum"]) {
243 [defaults setBool:NO forKey:@"showAlbum"];
246 if (![defaults objectForKey:@"showTime"]) {
247 [defaults setBool:NO forKey:@"showTime"];
251 - (void)startTimerInNewThread
253 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
254 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
255 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
257 selector:@selector(timerUpdate)
259 repeats:YES] retain];
264 //Recreate the status item menu
267 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
272 lastPlaylistIndex = -1;
273 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
274 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
276 while ([menu numberOfItems] > 0) {
277 [menu removeItemAtIndex:0];
280 playPauseMenuItem = nil;
281 upcomingSongsItem = nil;
282 songRatingMenuItem = nil;
284 [playlistMenu release];
290 for (i = 0; i < [myMenu count]; i++) {
291 NSString *item = [myMenu objectAtIndex:i];
292 if ([item isEqualToString:@"Play/Pause"]) {
293 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
294 playPauseMenuItem = [menu addItemWithTitle:@"Play"
295 action:@selector(playPause:)
299 [self setKeyEquivalentForCode:[tempCombo keyCode]
300 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
303 } else if ([item isEqualToString:@"Next Track"]) {
304 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
305 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
306 action:@selector(nextSong:)
310 [self setKeyEquivalentForCode:[tempCombo keyCode]
311 andModifiers:[tempCombo modifiers] onItem:nextTrack];
314 } else if ([item isEqualToString:@"Previous Track"]) {
315 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
316 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
317 action:@selector(prevSong:)
321 [self setKeyEquivalentForCode:[tempCombo keyCode]
322 andModifiers:[tempCombo modifiers] onItem:prevTrack];
325 } else if ([item isEqualToString:@"Fast Forward"]) {
326 [menu addItemWithTitle:@"Fast Forward"
327 action:@selector(fastForward:)
329 } else if ([item isEqualToString:@"Rewind"]) {
330 [menu addItemWithTitle:@"Rewind"
331 action:@selector(rewind:)
333 } else if ([item isEqualToString:@"Upcoming Songs"]) {
334 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
337 } else if ([item isEqualToString:@"Playlists"]) {
338 playlistItem = [menu addItemWithTitle:@"Playlists"
341 } else if ([item isEqualToString:@"EQ Presets"]) {
342 eqItem = [menu addItemWithTitle:@"EQ Presets"
345 } else if ([item isEqualToString:@"PreferencesÉ"]) {
346 [menu addItemWithTitle:@"PreferencesÉ"
347 action:@selector(showPreferences:)
349 } else if ([item isEqualToString:@"Quit"]) {
350 [menu addItemWithTitle:@"Quit"
351 action:@selector(quitMenuTunes:)
353 } else if ([item isEqualToString:@"Current Track Info"]) {
354 trackInfoIndex = [menu numberOfItems];
355 [menu addItemWithTitle:@"No Song"
358 } else if ([item isEqualToString:@"Song Rating"]) {
359 unichar fullstar = 0x2605;
360 unichar emptystar = 0x2606;
361 NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1];
362 NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1];
365 songRatingMenuItem = [menu addItemWithTitle:@"Song Rating"
369 ratingMenu = [[NSMenu alloc] initWithTitle:@""];
371 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
374 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
377 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
380 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
383 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
386 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
388 } else if ([item isEqualToString:@"<separator>"]) {
389 [menu addItem:[NSMenuItem separatorItem]];
394 //[self timerUpdate]; //Updates dynamic info in the menu
400 //Rebuild the upcoming songs submenu. Can be improved a lot.
401 - (void)rebuildUpcomingSongsMenu
403 int curIndex = [currentRemote currentPlaylistIndex];
404 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
405 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
407 if (!isPlayingRadio) {
409 int curTrack = [currentRemote currentSongIndex];
412 [upcomingSongsMenu release];
413 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
414 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
415 [upcomingSongsItem setEnabled:YES];
417 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
419 NSString *curSong = [currentRemote songTitleAtIndex:i];
420 NSMenuItem *songItem;
421 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(selectSong:) keyEquivalent:@""];
422 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
423 [upcomingSongsMenu addItem:songItem];
431 [upcomingSongsItem setSubmenu:nil];
432 [upcomingSongsItem setEnabled:NO];
436 - (void)rebuildPlaylistMenu
438 NSArray *playlists = [currentRemote playlists];
439 int i, curPlaylist = [currentRemote currentPlaylistIndex];
441 if (isPlayingRadio) {
444 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
447 [playlistMenu release];
448 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
450 for (i = 0; i < [playlists count]; i++) {
451 NSString *playlistName = [playlists objectAtIndex:i];
452 NSMenuItem *tempItem;
453 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
454 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
455 [playlistMenu addItem:tempItem];
458 [playlistItem setSubmenu:playlistMenu];
459 [playlistItem setEnabled:YES];
462 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
466 //Build a menu with the list of all available EQ presets
467 - (void)rebuildEQPresetsMenu
469 NSArray *eqPresets = [currentRemote eqPresets];
470 NSMenuItem *enabledItem;
473 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
477 eqMenu = [[NSMenu alloc] initWithTitle:@""];
479 enabledItem = [eqMenu addItemWithTitle:@"Enabled"
480 action:@selector(selectEQPreset:)
482 [enabledItem setTag:-1];
484 if ([currentRemote equalizerEnabled]) {
485 [enabledItem setState:NSOnState];
488 [eqMenu addItem:[NSMenuItem separatorItem]];
490 for (i = 0; i < [eqPresets count]; i++) {
491 NSString *setName = [eqPresets objectAtIndex:i];
492 NSMenuItem *tempItem;
494 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
496 [eqMenu addItem:tempItem];
500 [eqItem setSubmenu:eqMenu];
502 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
505 //Called when the timer fires.
508 int playlist = [currentRemote currentPlaylistIndex];
509 ITMTRemotePlayerPlayingState playerState = [currentRemote playerPlayingState];
511 if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
512 int trackPlayingIndex = [currentRemote currentSongIndex];
514 if (trackPlayingIndex != lastSongIndex) {
515 BOOL wasPlayingRadio = isPlayingRadio;
516 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
518 if (isPlayingRadio && !wasPlayingRadio) {
520 for (i = 0; i < [playlistMenu numberOfItems]; i++)
522 [[playlistMenu itemAtIndex:i] setState:NSOffState];
525 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
528 if (wasPlayingRadio) {
529 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
530 [menu insertItem:temp atIndex:trackInfoIndex + 1];
534 lastSongIndex = trackPlayingIndex;
536 if (playlist != lastPlaylistIndex) {
537 BOOL wasPlayingRadio = isPlayingRadio;
538 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
540 if (isPlayingRadio && !wasPlayingRadio) {
542 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
543 [[playlistMenu itemAtIndex:i] setState:NSOffState];
547 if (wasPlayingRadio) {
548 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
549 [menu insertItem:temp atIndex:trackInfoIndex + 1];
553 if (!isPlayingRadio) {
555 for (i = 0; i < [playlistMenu numberOfItems]; i++)
557 [[playlistMenu itemAtIndex:i] setState:NSOffState];
559 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
563 lastSongIndex = trackPlayingIndex;
564 lastPlaylistIndex = playlist;
567 //Update Play/Pause menu item
568 if (playPauseMenuItem){
569 if (playerState == ITMTRemotePlayerPlaying) {
570 [playPauseMenuItem setTitle:@"Pause"];
572 [playPauseMenuItem setTitle:@"Play"];
575 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
576 NSMenuItem *menuItem;
577 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
578 //Remote the now playing item and add no song item
579 [menu removeItemAtIndex:trackInfoIndex];
580 if ([defaults boolForKey:@"showName"] == YES) {
581 [menu removeItemAtIndex:trackInfoIndex];
584 if ([defaults boolForKey:@"showTime"] == YES) {
585 [menu removeItemAtIndex:trackInfoIndex];
588 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
589 [menu removeItemAtIndex:trackInfoIndex];
592 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
593 [menu removeItemAtIndex:trackInfoIndex];
596 [playPauseMenuItem setTitle:@"Play"];
598 didHaveArtistName = NO;
599 didHaveAlbumName = NO;
600 lastPlaylistIndex = -1;
603 [upcomingSongsItem setSubmenu:nil];
604 [upcomingSongsItem setEnabled:NO];
606 [songRatingMenuItem setSubmenu:nil];
607 [songRatingMenuItem setEnabled:NO];
609 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
610 [menu insertItem:menuItem atIndex:trackInfoIndex];
615 //Updates the menu with current player state, song, and upcoming songs
618 NSMenuItem *menuItem;
619 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
621 if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
625 if (upcomingSongsItem) {
626 [self rebuildUpcomingSongsMenu];
630 [self rebuildPlaylistMenu];
634 [self rebuildEQPresetsMenu];
636 if (trackInfoIndex > -1) {
637 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
638 curSongName = [currentRemote currentSongTitle];
640 if ([defaults boolForKey:@"showAlbum"]) {
641 curAlbumName = [currentRemote currentSongAlbum];
644 if ([defaults boolForKey:@"showArtist"]) {
645 curArtistName = [currentRemote currentSongArtist];
648 if ([curSongName length] > 0) {
649 int index = [menu indexOfItemWithTitle:@"Now Playing"];
651 if ([defaults boolForKey:@"showName"]) {
652 [menu removeItemAtIndex:index + 1];
654 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
655 [menu removeItemAtIndex:index + 1];
657 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
658 [menu removeItemAtIndex:index + 1];
660 if ([defaults boolForKey:@"showTime"]) {
661 [menu removeItemAtIndex:index + 1];
665 if (!isPlayingRadio) {
666 if ([defaults boolForKey:@"showTime"]) {
667 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
670 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
674 if ([curArtistName length] > 0) {
675 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
678 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
682 if ([curAlbumName length] > 0) {
683 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
686 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
690 if (songRatingMenuItem) {
691 int rating = (int)[currentRemote currentSongRating] * 10;
693 for (i = 0; i < 5; i++) {
694 [[ratingMenu itemAtIndex:i] setState:NSOffState];
695 [[ratingMenu itemAtIndex:i] setTarget:self];
697 [[ratingMenu itemAtIndex:rating / 2] setState:NSOnState];
701 if ([defaults boolForKey:@"showName"]) {
702 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
705 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
710 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
711 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
712 [menu insertItem:menuItem atIndex:trackInfoIndex];
715 [songRatingMenuItem setSubmenu:ratingMenu];
716 [songRatingMenuItem setEnabled:YES];
718 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
719 [menu removeItemAtIndex:trackInfoIndex];
721 if ([defaults boolForKey:@"showName"] == YES) {
722 [menu removeItemAtIndex:trackInfoIndex];
725 if ([defaults boolForKey:@"showTime"] == YES) {
726 [menu removeItemAtIndex:trackInfoIndex];
729 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
730 [menu removeItemAtIndex:trackInfoIndex];
733 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
734 [menu removeItemAtIndex:trackInfoIndex];
737 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
738 [menu insertItem:menuItem atIndex:trackInfoIndex];
742 if ([defaults boolForKey:@"showArtist"]) {
743 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
746 if ([defaults boolForKey:@"showAlbum"]) {
747 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
759 - (void)selectSong:(id)sender
761 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
764 - (void)selectPlaylist:(id)sender
766 int playlist = [[sender representedObject] intValue];
767 if (!isPlayingRadio) {
768 int curPlaylist = [currentRemote currentPlaylistIndex];
769 if (curPlaylist > 0) {
770 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
773 [currentRemote switchToPlaylistAtIndex:playlist];
774 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
778 - (void)selectEQPreset:(id)sender
780 int curSet = [currentRemote currentEQPresetIndex];
781 int item = [sender tag];
784 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
786 [currentRemote setEqualizerEnabled:YES];
787 [currentRemote switchToEQAtIndex:item];
788 [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
789 [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
793 - (void)selectSongRating:(id)sender
795 [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState];
796 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
797 [sender setState:NSOnState];
800 - (void)playPause:(id)sender
802 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
804 if (state == ITMTRemotePlayerPlaying) {
805 [currentRemote pause];
806 [playPauseMenuItem setTitle:@"Play"];
807 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
808 [currentRemote pause];
809 [currentRemote play];
811 [currentRemote play];
812 [playPauseMenuItem setTitle:@"Pause"];
816 - (void)nextSong:(id)sender
818 [currentRemote goToNextSong];
821 - (void)prevSong:(id)sender
823 [currentRemote goToPreviousSong];
826 - (void)fastForward:(id)sender
828 [currentRemote forward];
829 [playPauseMenuItem setTitle:@"Play"];
832 - (void)rewind:(id)sender
834 [currentRemote rewind];
835 [playPauseMenuItem setTitle:@"Play"];
840 - (void)quitMenuTunes:(id)sender
842 [NSApp terminate:self];
845 - (void)showPlayer:(id)sender
847 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
848 [currentRemote showPrimaryInterface];
850 if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
851 NSLog(@"Error Launching Player");
856 - (void)showPreferences:(id)sender
858 if (!prefsController) {
859 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
864 - (void)closePreferences
866 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
869 [prefsController release];
870 prefsController = nil;
882 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
883 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
884 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
885 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
886 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
891 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
893 if ([defaults objectForKey:@"PlayPause"] != nil) {
894 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
895 combo:[defaults keyComboForKey:@"PlayPause"]
896 target:self action:@selector(playPause:)];
899 if ([defaults objectForKey:@"NextTrack"] != nil) {
900 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
901 combo:[defaults keyComboForKey:@"NextTrack"]
902 target:self action:@selector(nextSong:)];
905 if ([defaults objectForKey:@"PrevTrack"] != nil) {
906 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
907 combo:[defaults keyComboForKey:@"PrevTrack"]
908 target:self action:@selector(prevSong:)];
911 if ([defaults objectForKey:@"TrackInfo"] != nil) {
912 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
913 combo:[defaults keyComboForKey:@"TrackInfo"]
914 target:self action:@selector(showCurrentTrackInfo)];
917 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
918 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
919 combo:[defaults keyComboForKey:@"UpcomingSongs"]
920 target:self action:@selector(showUpcomingSongs)];
926 // Show Current Track Info And Show Upcoming Songs Floaters
930 - (void)showCurrentTrackInfo
932 NSString *trackName = [currentRemote currentSongTitle];
933 if (!statusWindow && [trackName length]) {
934 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
935 NSString *stringToShow = @"";
937 if ([defaults boolForKey:@"showName"]) {
938 if ([defaults boolForKey:@"showArtist"]) {
939 NSString *trackArtist = [currentRemote currentSongArtist];
940 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
942 stringToShow = [stringToShow stringByAppendingString:trackName];
943 stringToShow = [stringToShow stringByAppendingString:@"\n"];
946 if ([defaults boolForKey:@"showAlbum"]) {
947 NSString *trackAlbum = [currentRemote currentSongAlbum];
948 if ([trackAlbum length]) {
949 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
950 stringToShow = [stringToShow stringByAppendingString:@"\n"];
954 if ([defaults boolForKey:@"showTime"]) {
955 NSString *trackTime = [currentRemote currentSongLength];
956 if ([trackTime length]) {
957 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
962 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
963 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
965 stringToShow = [stringToShow stringByAppendingString:
966 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
968 stringToShow = [stringToShow stringByAppendingString:
969 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
973 [statusWindow setText:stringToShow];
974 [NSTimer scheduledTimerWithTimeInterval:3.0
976 selector:@selector(fadeAndCloseStatusWindow)
982 - (void)showUpcomingSongs
984 int curPlaylist = [currentRemote currentPlaylistIndex];
986 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
989 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
990 int curTrack = [currentRemote currentSongIndex];
992 NSString *songs = @"";
994 statusWindow = [ITTransientStatusWindow sharedWindow];
995 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
997 NSString *curSong = [currentRemote songTitleAtIndex:i];
998 songs = [songs stringByAppendingString:curSong];
999 songs = [songs stringByAppendingString:@"\n"];
1002 [statusWindow setText:songs];
1003 [NSTimer scheduledTimerWithTimeInterval:3.0
1005 selector:@selector(fadeAndCloseStatusWindow)
1012 - (void)fadeAndCloseStatusWindow
1014 [statusWindow orderOut:self];
1017 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
1018 onItem:(NSMenuItem *)item
1020 unichar charcode = 'a';
1022 long cocoaModifiers = 0;
1023 static long carbonToCocoa[6][2] =
1025 { cmdKey, NSCommandKeyMask },
1026 { optionKey, NSAlternateKeyMask },
1027 { controlKey, NSControlKeyMask },
1028 { shiftKey, NSShiftKeyMask },
1031 for (i = 0; i < 6; i++) {
1032 if (modifiers & carbonToCocoa[i][0]) {
1033 cocoaModifiers += carbonToCocoa[i][1];
1036 [item setKeyEquivalentModifierMask:cocoaModifiers];
1038 //Missing key combos for some keys. Must find them later.
1052 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1053 NSLog(@"%@", menuRef);
1054 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1055 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1056 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1062 charcode = NSDeleteFunctionKey;
1078 charcode = NSF5FunctionKey;
1082 charcode = NSF6FunctionKey;
1086 charcode = NSF7FunctionKey;
1090 charcode = NSF3FunctionKey;
1094 charcode = NSF8FunctionKey;
1098 charcode = NSF9FunctionKey;
1102 charcode = NSF11FunctionKey;
1106 charcode = NSF3FunctionKey;
1110 charcode = NSF14FunctionKey;
1114 charcode = NSF10FunctionKey;
1118 charcode = NSF12FunctionKey;
1122 charcode = NSF13FunctionKey;
1126 charcode = NSInsertFunctionKey;
1130 charcode = NSHomeFunctionKey;
1134 charcode = NSPageUpFunctionKey;
1138 charcode = NSDeleteFunctionKey;
1142 charcode = NSF4FunctionKey;
1146 charcode = NSEndFunctionKey;
1150 charcode = NSF2FunctionKey;
1154 charcode = NSPageDownFunctionKey;
1158 charcode = NSF1FunctionKey;
1162 charcode = NSLeftArrowFunctionKey;
1166 charcode = NSRightArrowFunctionKey;
1170 charcode = NSDownArrowFunctionKey;
1174 charcode = NSUpArrowFunctionKey;
1178 if (charcode == 'a') {
1179 unsigned long state;
1184 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1185 keyTrans = KeyTranslate(kchr, code, &state);
1186 charCode = keyTrans;
1187 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1188 } else if (charcode != 'b') {
1189 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];