2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
6 @interface MenuTunes(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 MenuTunes
21 /*************************************************************************/
23 #pragma mark INITIALIZATION METHODS
24 /*************************************************************************/
28 if ( ( self = [super init] ) ) {
29 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
30 statusWindow = [StatusWindow sharedWindow];
35 - (void)applicationDidFinishLaunching:(NSNotification *)note
37 currentRemote = [self loadRemote];
38 [currentRemote begin];
40 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
41 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
43 [self registerDefaultsIfNeeded];
45 menu = [[NSMenu alloc] initWithTitle:@""];
47 if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) {
48 [self remotePlayerLaunched:nil];
50 [self remotePlayerTerminated:nil];
53 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar] withLength:NSSquareStatusItemLength];
55 [statusItem setImage:[NSImage imageNamed:@"menu"]];
56 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
57 [statusItem setMenu:menu];
58 // Below line of code is for creating builds for Beta Testers
59 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
62 - (ITMTRemote *)loadRemote
64 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
67 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
68 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
71 while ( (bundlePath = [enumerator nextObject]) ) {
72 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
75 Class remoteClass = [remoteBundle principalClass];
77 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
78 [remoteClass isKindOfClass:[NSObject class]]) {
80 id remote = [remoteClass remote];
81 [remoteArray addObject:remote];
86 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
87 // if ( [remoteArray count] > 1 ) {
88 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
90 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
93 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
94 return [remoteArray objectAtIndex:0];
98 /*************************************************************************/
100 #pragma mark INSTANCE METHODS
101 /*************************************************************************/
103 - (void)registerDefaultsIfNeeded
105 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
106 if (![defaults objectForKey:@"menu"]) {
108 NSMutableDictionary *loginwindow;
109 NSMutableArray *loginarray;
113 [NSArray arrayWithObjects:
127 @"Current Track Info",
128 nil] forKey:@"menu"];
130 [defaults synchronize];
131 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
132 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
134 for (i = 0; i < [loginarray count]; i++) {
135 NSDictionary *tempDict = [loginarray objectAtIndex:i];
136 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
143 //We must fix it so it is no longer suxy
145 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
146 AEDesc scriptDesc, resultDesc;
147 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]];
148 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
150 AECreateDesc(typeChar, [script cString], [script cStringLength],
153 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
155 AEDisposeDesc(&scriptDesc);
156 AEDisposeDesc(&resultDesc);
158 CloseComponent(asComponent);
163 if (![defaults integerForKey:@"SongsInAdvance"])
165 [defaults setInteger:5 forKey:@"SongsInAdvance"];
168 if (![defaults objectForKey:@"showName"]) {
169 [defaults setBool:YES forKey:@"showName"];
172 if (![defaults objectForKey:@"showArtist"]) {
173 [defaults setBool:YES forKey:@"showArtist"];
176 if (![defaults objectForKey:@"showAlbum"]) {
177 [defaults setBool:NO forKey:@"showAlbum"];
180 if (![defaults objectForKey:@"showTime"]) {
181 [defaults setBool:NO forKey:@"showTime"];
185 //Recreate the status item menu
188 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
193 lastPlaylistIndex = -1;
194 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
195 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
197 while ([menu numberOfItems] > 0) {
198 [menu removeItemAtIndex:0];
201 playPauseMenuItem = nil;
202 upcomingSongsItem = nil;
203 songRatingMenuItem = nil;
205 [playlistMenu release];
211 for (i = 0; i < [myMenu count]; i++) {
212 NSString *item = [myMenu objectAtIndex:i];
213 if ([item isEqualToString:@"Play/Pause"]) {
214 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
215 playPauseMenuItem = [menu addItemWithTitle:@"Play"
216 action:@selector(playPause:)
218 [playPauseMenuItem setTarget:self];
221 [self setKeyEquivalentForCode:[tempCombo keyCode]
222 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
225 } else if ([item isEqualToString:@"Next Track"]) {
226 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
227 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
228 action:@selector(nextSong:)
231 [nextTrack setTarget:self];
233 [self setKeyEquivalentForCode:[tempCombo keyCode]
234 andModifiers:[tempCombo modifiers] onItem:nextTrack];
237 } else if ([item isEqualToString:@"Previous Track"]) {
238 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
239 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
240 action:@selector(prevSong:)
243 [prevTrack setTarget:self];
245 [self setKeyEquivalentForCode:[tempCombo keyCode]
246 andModifiers:[tempCombo modifiers] onItem:prevTrack];
249 } else if ([item isEqualToString:@"Fast Forward"]) {
250 [[menu addItemWithTitle:@"Fast Forward"
251 action:@selector(fastForward:)
252 keyEquivalent:@""] setTarget:self];
253 } else if ([item isEqualToString:@"Rewind"]) {
254 [[menu addItemWithTitle:@"Rewind"
255 action:@selector(rewind:)
256 keyEquivalent:@""] setTarget:self];
257 } else if ([item isEqualToString:@"Upcoming Songs"]) {
258 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
261 } else if ([item isEqualToString:@"Playlists"]) {
262 playlistItem = [menu addItemWithTitle:@"Playlists"
265 } else if ([item isEqualToString:@"EQ Presets"]) {
266 eqItem = [menu addItemWithTitle:@"EQ Presets"
269 } else if ([item isEqualToString:@"PreferencesÉ"]) {
270 [[menu addItemWithTitle:@"PreferencesÉ"
271 action:@selector(showPreferences:)
272 keyEquivalent:@""] setTarget:self];
273 } else if ([item isEqualToString:@"Quit"]) {
274 [[menu addItemWithTitle:@"Quit"
275 action:@selector(quitMenuTunes:)
276 keyEquivalent:@""] setTarget:self];
277 } else if ([item isEqualToString:@"Current Track Info"]) {
278 trackInfoIndex = [menu numberOfItems];
279 [menu addItemWithTitle:@"No Song"
282 } else if ([item isEqualToString:@"Song Rating"]) {
285 NSString *curTitle = @".....";
287 songRatingMenuItem = [menu addItemWithTitle:@"Song Rating"
291 ratingMenu = [[NSMenu alloc] initWithTitle:@""];
293 item = [ratingMenu addItemWithTitle:@"....."
294 action:@selector(setSongRating:)
296 [item setTarget:self];
299 for (i = 1; i < 6; i++) {
300 curTitle = [curTitle substringToIndex:4];
301 curTitle = [@"x" stringByAppendingString:curTitle];
302 item = [ratingMenu addItemWithTitle:curTitle
303 action:@selector(setSongRating:)
305 [item setTarget:self];
306 [item setTag:(i * 20)];
308 } else if ([item isEqualToString:@"<separator>"]) {
309 [menu addItem:[NSMenuItem separatorItem]];
313 [self timerUpdate]; //Updates dynamic info in the menu
319 //Updates the menu with current player state, song, and upcoming songs
322 NSMenuItem *menuItem;
323 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
325 if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
329 if (upcomingSongsItem) {
330 [self rebuildUpcomingSongsMenu];
334 [self rebuildPlaylistMenu];
338 [self rebuildEQPresetsMenu];
340 if (trackInfoIndex > -1) {
341 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
342 curSongName = [currentRemote currentSongTitle];
344 if ([defaults boolForKey:@"showAlbum"]) {
345 curAlbumName = [currentRemote currentSongAlbum];
348 if ([defaults boolForKey:@"showArtist"]) {
349 curArtistName = [currentRemote currentSongArtist];
352 if ([curSongName length] > 0) {
353 int index = [menu indexOfItemWithTitle:@"Now Playing"];
355 if ([defaults boolForKey:@"showName"]) {
356 [menu removeItemAtIndex:index + 1];
358 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
359 [menu removeItemAtIndex:index + 1];
361 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
362 [menu removeItemAtIndex:index + 1];
364 if ([defaults boolForKey:@"showTime"]) {
365 [menu removeItemAtIndex:index + 1];
369 if (!isPlayingRadio) {
370 if ([defaults boolForKey:@"showTime"]) {
371 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
374 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
378 if ([curArtistName length] > 0) {
379 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
382 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
386 if ([curAlbumName length] > 0) {
387 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
390 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
394 if (songRatingMenuItem) {
395 int rating = (int)[currentRemote currentSongRating] * 10;
397 for (i = 0; i < 5; i++) {
398 [[ratingMenu itemAtIndex:i] setState:NSOffState];
399 [[ratingMenu itemAtIndex:i] setTarget:self];
401 [[ratingMenu itemAtIndex:rating / 2] setState:NSOnState];
405 if ([defaults boolForKey:@"showName"]) {
406 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
409 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
414 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
415 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
416 [menu insertItem:menuItem atIndex:trackInfoIndex];
419 [songRatingMenuItem setSubmenu:ratingMenu];
420 [songRatingMenuItem setEnabled:YES];
422 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
423 [menu removeItemAtIndex:trackInfoIndex];
425 if ([defaults boolForKey:@"showName"] == YES) {
426 [menu removeItemAtIndex:trackInfoIndex];
429 if ([defaults boolForKey:@"showTime"] == YES) {
430 [menu removeItemAtIndex:trackInfoIndex];
433 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
434 [menu removeItemAtIndex:trackInfoIndex];
437 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
438 [menu removeItemAtIndex:trackInfoIndex];
441 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
442 [menu insertItem:menuItem atIndex:trackInfoIndex];
446 if ([defaults boolForKey:@"showArtist"]) {
447 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
450 if ([defaults boolForKey:@"showAlbum"]) {
451 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
457 //Rebuild the upcoming songs submenu. Can be improved a lot.
458 - (void)rebuildUpcomingSongsMenu
460 int curIndex = [currentRemote currentPlaylistIndex];
461 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
462 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
464 if (!isPlayingRadio) {
466 int curTrack = [currentRemote currentSongIndex];
469 [upcomingSongsMenu release];
470 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
471 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
472 [upcomingSongsItem setEnabled:YES];
474 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
476 NSString *curSong = [currentRemote songTitleAtIndex:i];
477 NSMenuItem *songItem;
478 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
479 [songItem setTarget:self];
480 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
481 [upcomingSongsMenu addItem:songItem];
489 [upcomingSongsItem setSubmenu:nil];
490 [upcomingSongsItem setEnabled:NO];
494 - (void)rebuildPlaylistMenu
496 NSArray *playlists = [currentRemote playlists];
497 int i, curPlaylist = [currentRemote currentPlaylistIndex];
499 if (isPlayingRadio) {
502 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
505 [playlistMenu release];
506 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
508 for (i = 0; i < [playlists count]; i++) {
509 NSString *playlistName = [playlists objectAtIndex:i];
510 NSMenuItem *tempItem;
511 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
512 [tempItem setTarget:self];
513 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
514 [playlistMenu addItem:tempItem];
517 [playlistItem setSubmenu:playlistMenu];
518 [playlistItem setEnabled:YES];
521 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
525 //Build a menu with the list of all available EQ presets
526 - (void)rebuildEQPresetsMenu
528 NSArray *eqPresets = [currentRemote eqPresets];
529 NSMenuItem *enabledItem;
532 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
536 eqMenu = [[NSMenu alloc] initWithTitle:@""];
538 enabledItem = [eqMenu addItemWithTitle:@"Disabled"
539 action:@selector(toggleEqualizer)
542 if ([currentRemote equalizerEnabled] == NO) {
543 [enabledItem setState:NSOnState];
546 [eqMenu addItem:[NSMenuItem separatorItem]];
548 for (i = 0; i < [eqPresets count]; i++) {
549 NSString *setName = [eqPresets objectAtIndex:i];
550 NSMenuItem *tempItem;
552 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
553 [tempItem setTarget:self];
554 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
555 [eqMenu addItem:tempItem];
559 [eqItem setSubmenu:eqMenu];
561 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
566 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
567 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
568 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
569 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
570 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
575 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
577 if ([defaults objectForKey:@"PlayPause"] != nil) {
578 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
579 combo:[defaults keyComboForKey:@"PlayPause"]
580 target:self action:@selector(playPause:)];
583 if ([defaults objectForKey:@"NextTrack"] != nil) {
584 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
585 combo:[defaults keyComboForKey:@"NextTrack"]
586 target:self action:@selector(nextSong:)];
589 if ([defaults objectForKey:@"PrevTrack"] != nil) {
590 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
591 combo:[defaults keyComboForKey:@"PrevTrack"]
592 target:self action:@selector(prevSong:)];
595 if ([defaults objectForKey:@"TrackInfo"] != nil) {
596 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
597 combo:[defaults keyComboForKey:@"TrackInfo"]
598 target:self action:@selector(showCurrentTrackInfo)];
601 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
602 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
603 combo:[defaults keyComboForKey:@"UpcomingSongs"]
604 target:self action:@selector(showUpcomingSongs)];
608 //Called when the timer fires.
611 int playlist = [currentRemote currentPlaylistIndex];
612 ITMTRemotePlayerPlayingState playerPlayingState = [currentRemote playerPlayingState];
614 if ((playlist > 0) || playerPlayingState != ITMTRemotePlayerStopped) {
615 int trackPlayingIndex = [currentRemote currentSongIndex];
617 if (trackPlayingIndex != lastSongIndex) {
618 BOOL wasPlayingRadio = isPlayingRadio;
619 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
621 if (isPlayingRadio && !wasPlayingRadio) {
623 for (i = 0; i < [playlistMenu numberOfItems]; i++)
625 [[playlistMenu itemAtIndex:i] setState:NSOffState];
628 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
631 if (wasPlayingRadio) {
632 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
633 [menu insertItem:temp atIndex:trackInfoIndex + 1];
638 lastSongIndex = trackPlayingIndex;
640 if (playlist != lastPlaylistIndex) {
641 BOOL wasPlayingRadio = isPlayingRadio;
642 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
644 if (isPlayingRadio && !wasPlayingRadio) {
646 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
647 [[playlistMenu itemAtIndex:i] setState:NSOffState];
651 if (wasPlayingRadio) {
652 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
653 [menu insertItem:temp atIndex:trackInfoIndex + 1];
657 if (!isPlayingRadio) {
659 for (i = 0; i < [playlistMenu numberOfItems]; i++)
661 [[playlistMenu itemAtIndex:i] setState:NSOffState];
663 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
667 lastSongIndex = trackPlayingIndex;
668 lastPlaylistIndex = playlist;
671 //Update Play/Pause menu item
672 if (playPauseMenuItem){
673 if (playerPlayingState == ITMTRemotePlayerPlaying) {
674 [playPauseMenuItem setTitle:@"Pause"];
676 [playPauseMenuItem setTitle:@"Play"];
679 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
680 NSMenuItem *menuItem;
681 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
682 //Remote the now playing item and add no song item
683 [menu removeItemAtIndex:trackInfoIndex];
685 if ([defaults boolForKey:@"showName"] == YES) {
686 [menu removeItemAtIndex:trackInfoIndex];
689 if ([defaults boolForKey:@"showTime"] == YES) {
690 [menu removeItemAtIndex:trackInfoIndex];
693 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
694 [menu removeItemAtIndex:trackInfoIndex];
697 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
698 [menu removeItemAtIndex:trackInfoIndex];
701 [playPauseMenuItem setTitle:@"Play"];
703 didHaveArtistName = NO;
704 didHaveAlbumName = NO;
705 lastPlaylistIndex = -1;
708 [upcomingSongsItem setSubmenu:nil];
709 [upcomingSongsItem setEnabled:NO];
711 [songRatingMenuItem setSubmenu:nil];
712 [songRatingMenuItem setEnabled:NO];
714 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
715 [menu insertItem:menuItem atIndex:trackInfoIndex];
720 - (void)remotePlayerLaunched:(NSNotification *)note
722 isAppRunning = ITMTRemotePlayerRunning;
725 [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
727 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
729 [self rebuildPlaylistMenu];
732 [self rebuildEQPresetsMenu];
734 [statusItem setMenu:menu]; //Set the menu back to the main one
737 - (void)runTimerInNewThread
739 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
740 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
741 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain];
746 - (void)remotePlayerTerminated:(NSNotification *)note
748 isAppRunning = ITMTRemotePlayerNotRunning;
751 menu = [[NSMenu alloc] initWithTitle:@""];
752 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
753 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
754 [menu addItem:[NSMenuItem separatorItem]];
755 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
756 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
757 [statusItem setMenu:menu];
759 [refreshTimer invalidate];
760 [refreshTimer release];
767 // Selectors - called from status item menu
771 // Plugin dependent selectors
773 - (void)playTrack:(id)sender
775 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
778 - (void)selectPlaylist:(id)sender
780 int playlist = [[sender representedObject] intValue];
781 if (!isPlayingRadio) {
782 int curPlaylist = [currentRemote currentPlaylistIndex];
783 if (curPlaylist > 0) {
784 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
787 [currentRemote switchToPlaylistAtIndex:playlist];
788 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
792 - (void)selectEQPreset:(id)sender
794 int curSet = [currentRemote currentEQPresetIndex];
795 int item = [[sender representedObject] intValue];
796 [currentRemote switchToEQAtIndex:item];
797 [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
798 [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
801 - (void)playPause:(id)sender
803 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
805 if (state == ITMTRemotePlayerPlaying) {
806 [currentRemote pause];
807 [playPauseMenuItem setTitle:@"Play"];
808 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
809 [currentRemote pause];
810 [currentRemote play];
812 [currentRemote play];
813 [playPauseMenuItem setTitle:@"Pause"];
817 - (void)nextSong:(id)sender
819 [currentRemote goToNextSong];
822 - (void)prevSong:(id)sender
824 [currentRemote goToPreviousSong];
827 - (void)fastForward:(id)sender
829 [currentRemote forward];
830 [playPauseMenuItem setTitle:@"Play"];
833 - (void)rewind:(id)sender
835 [currentRemote rewind];
836 [playPauseMenuItem setTitle:@"Play"];
839 - (void)toggleEqualizer
841 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
844 - (void)setSongRating:(id)sender
846 NSLog(@"%f", [currentRemote currentSongRating]);
847 NSLog(@"%f", (float)[sender tag] / 100.0);
848 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
853 // Plugin independent selectors
856 - (void)quitMenuTunes:(id)sender
858 [NSApp terminate:self];
861 - (void)showPreferences:(id)sender
863 if (!prefsController) {
864 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
869 - (void)closePreferences
871 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
874 [prefsController release];
875 prefsController = nil;
880 // Show Current Track Info And Show Upcoming Songs Floaters
884 - (void)showCurrentTrackInfo
886 NSString *trackName = [currentRemote currentSongTitle];
887 if (!statusWindow && [trackName length]) {
888 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
889 NSString *stringToShow = @"";
891 if ([defaults boolForKey:@"showName"]) {
892 if ([defaults boolForKey:@"showArtist"]) {
893 NSString *trackArtist = [currentRemote currentSongArtist];
894 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
896 stringToShow = [stringToShow stringByAppendingString:trackName];
897 stringToShow = [stringToShow stringByAppendingString:@"\n"];
900 if ([defaults boolForKey:@"showAlbum"]) {
901 NSString *trackAlbum = [currentRemote currentSongAlbum];
902 if ([trackAlbum length]) {
903 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
904 stringToShow = [stringToShow stringByAppendingString:@"\n"];
908 if ([defaults boolForKey:@"showTime"]) {
909 NSString *trackTime = [currentRemote currentSongLength];
910 if ([trackTime length]) {
911 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
916 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
917 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
919 stringToShow = [stringToShow stringByAppendingString:
920 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
922 stringToShow = [stringToShow stringByAppendingString:
923 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
927 [statusWindow setText:stringToShow];
928 [NSTimer scheduledTimerWithTimeInterval:3.0
930 selector:@selector(fadeAndCloseStatusWindow)
936 - (void)showUpcomingSongs
938 int curPlaylist = [currentRemote currentPlaylistIndex];
940 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
943 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
944 int curTrack = [currentRemote currentSongIndex];
946 NSString *songs = @"";
948 statusWindow = [ITTransientStatusWindow sharedWindow];
949 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
951 NSString *curSong = [currentRemote songTitleAtIndex:i];
952 songs = [songs stringByAppendingString:curSong];
953 songs = [songs stringByAppendingString:@"\n"];
956 [statusWindow setText:songs];
957 [NSTimer scheduledTimerWithTimeInterval:3.0
959 selector:@selector(fadeAndCloseStatusWindow)
966 - (void)fadeAndCloseStatusWindow
968 [statusWindow orderOut:self];
971 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
972 onItem:(NSMenuItem *)item
974 unichar charcode = 'a';
976 long cocoaModifiers = 0;
977 static long carbonToCocoa[6][2] =
979 { cmdKey, NSCommandKeyMask },
980 { optionKey, NSAlternateKeyMask },
981 { controlKey, NSControlKeyMask },
982 { shiftKey, NSShiftKeyMask },
985 for (i = 0; i < 6; i++) {
986 if (modifiers & carbonToCocoa[i][0]) {
987 cocoaModifiers += carbonToCocoa[i][1];
990 [item setKeyEquivalentModifierMask:cocoaModifiers];
992 //Missing key combos for some keys. Must find them later.
1006 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1007 NSLog(@"%@", menuRef);
1008 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1009 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1010 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1016 charcode = NSDeleteFunctionKey;
1032 charcode = NSF5FunctionKey;
1036 charcode = NSF6FunctionKey;
1040 charcode = NSF7FunctionKey;
1044 charcode = NSF3FunctionKey;
1048 charcode = NSF8FunctionKey;
1052 charcode = NSF9FunctionKey;
1056 charcode = NSF11FunctionKey;
1060 charcode = NSF3FunctionKey;
1064 charcode = NSF14FunctionKey;
1068 charcode = NSF10FunctionKey;
1072 charcode = NSF12FunctionKey;
1076 charcode = NSF13FunctionKey;
1080 charcode = NSInsertFunctionKey;
1084 charcode = NSHomeFunctionKey;
1088 charcode = NSPageUpFunctionKey;
1092 charcode = NSDeleteFunctionKey;
1096 charcode = NSF4FunctionKey;
1100 charcode = NSEndFunctionKey;
1104 charcode = NSF2FunctionKey;
1108 charcode = NSPageDownFunctionKey;
1112 charcode = NSF1FunctionKey;
1116 charcode = NSLeftArrowFunctionKey;
1120 charcode = NSRightArrowFunctionKey;
1124 charcode = NSDownArrowFunctionKey;
1128 charcode = NSUpArrowFunctionKey;
1132 if (charcode == 'a') {
1133 unsigned long state;
1138 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1139 keyTrans = KeyTranslate(kchr, code, &state);
1140 charCode = keyTrans;
1141 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1142 } else if (charcode != 'b') {
1143 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1147 /*************************************************************************/
1149 #pragma mark NSApplication DELEGATE METHODS
1150 /*************************************************************************/
1152 - (void)applicationWillTerminate:(NSNotification *)note
1154 [self clearHotKeys];
1155 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1159 /*************************************************************************/
1161 #pragma mark DEALLOCATION METHODS
1162 /*************************************************************************/
1167 [refreshTimer invalidate];
1168 [refreshTimer release];
1171 [currentRemote halt];
1172 [statusItem release];