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 playerRunningStatus] == ITMTRemotePlayerRunning ) ) {
48 [self remotePlayerLaunched:nil];
50 [self remotePlayerTerminated:nil];
53 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
54 withLength:NSSquareStatusItemLength];
56 [statusItem setImage:[NSImage imageNamed:@"menu"]];
57 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
58 [statusItem setMenu:menu];
59 // Below line of code is for creating builds for Beta Testers
60 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
63 - (ITMTRemote *)loadRemote
65 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
68 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
69 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
72 while ( (bundlePath = [enumerator nextObject]) ) {
73 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
76 Class remoteClass = [remoteBundle principalClass];
78 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
79 [remoteClass isKindOfClass:[NSObject class]]) {
81 id remote = [remoteClass remote];
82 [remoteArray addObject:remote];
87 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
88 // if ( [remoteArray count] > 1 ) {
89 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
91 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
94 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
95 return [remoteArray objectAtIndex:0];
99 /*************************************************************************/
101 #pragma mark INSTANCE METHODS
102 /*************************************************************************/
104 - (void)registerDefaultsIfNeeded
106 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
107 if (![defaults objectForKey:@"menu"]) {
109 NSMutableDictionary *loginwindow;
110 NSMutableArray *loginarray;
114 [NSArray arrayWithObjects:
128 @"Current Track Info",
129 nil] forKey:@"menu"];
131 [defaults synchronize];
132 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
133 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
135 for (i = 0; i < [loginarray count]; i++) {
136 NSDictionary *tempDict = [loginarray objectAtIndex:i];
137 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
144 //We must fix it so it is no longer suxy
146 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
147 AEDesc scriptDesc, resultDesc;
148 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]];
149 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
151 AECreateDesc(typeChar, [script cString], [script cStringLength],
154 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
156 AEDisposeDesc(&scriptDesc);
157 AEDisposeDesc(&resultDesc);
159 CloseComponent(asComponent);
164 if (![defaults integerForKey:@"SongsInAdvance"])
166 [defaults setInteger:5 forKey:@"SongsInAdvance"];
169 if (![defaults objectForKey:@"showName"]) {
170 [defaults setBool:YES forKey:@"showName"];
173 if (![defaults objectForKey:@"showArtist"]) {
174 [defaults setBool:YES forKey:@"showArtist"];
177 if (![defaults objectForKey:@"showAlbum"]) {
178 [defaults setBool:NO forKey:@"showAlbum"];
181 if (![defaults objectForKey:@"showTime"]) {
182 [defaults setBool:NO forKey:@"showTime"];
186 //Recreate the status item menu
189 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
194 lastPlaylistIndex = -1;
195 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
196 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
198 while ([menu numberOfItems] > 0) {
199 [menu removeItemAtIndex:0];
202 playPauseMenuItem = nil;
203 upcomingSongsItem = 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"]) {
283 NSMenu *ratingSubmenu = [[NSMenu alloc] initWithTitle:@""];
284 unichar whiteStar = 'o';//2606;
285 unichar blackStar = 'x';//2605;
286 NSString *whiteStarString = [NSString stringWithCharacters:&whiteStar
288 NSString *blackStarString = [NSString stringWithCharacters:&blackStar
290 NSString *string = @"";
293 for (i = 0; i < 5; i++) {
294 string = [string stringByAppendingString:whiteStarString];
296 for (i = 0; i < 6; i++) {
297 NSMenuItem *ratingItem;
298 ratingItem = [ratingSubmenu addItemWithTitle:string action:@selector(setSongRating:) keyEquivalent:@""];
299 [ratingItem setTarget:self];
300 [ratingItem setTag:i * 20];
301 string = [string substringToIndex:4];
302 string = [blackStarString stringByAppendingString:string];
304 [[menu addItemWithTitle:@"Song Rating"
306 keyEquivalent:@""] setSubmenu:ratingSubmenu];
307 [ratingSubmenu autorelease];
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];
395 if ([defaults boolForKey:@"showName"]) {
396 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
399 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
404 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
405 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
406 [menu insertItem:menuItem atIndex:trackInfoIndex];
409 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
410 [menu removeItemAtIndex:trackInfoIndex];
412 if ([defaults boolForKey:@"showName"] == YES) {
413 [menu removeItemAtIndex:trackInfoIndex];
416 if ([defaults boolForKey:@"showTime"] == YES) {
417 [menu removeItemAtIndex:trackInfoIndex];
420 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
421 [menu removeItemAtIndex:trackInfoIndex];
424 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
425 [menu removeItemAtIndex:trackInfoIndex];
428 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
429 [menu insertItem:menuItem atIndex:trackInfoIndex];
433 if ([defaults boolForKey:@"showArtist"]) {
434 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
437 if ([defaults boolForKey:@"showAlbum"]) {
438 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
443 //Rebuild the upcoming songs submenu. Can be improved a lot.
444 - (void)rebuildUpcomingSongsMenu
446 int curIndex = [currentRemote currentPlaylistIndex];
447 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
448 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
450 if (!isPlayingRadio) {
452 int curTrack = [currentRemote currentSongIndex];
455 [upcomingSongsMenu release];
456 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
457 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
458 [upcomingSongsItem setEnabled:YES];
460 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
462 NSString *curSong = [currentRemote songTitleAtIndex:i];
463 NSMenuItem *songItem;
464 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
465 [songItem setTarget:self];
466 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
467 [upcomingSongsMenu addItem:songItem];
475 [upcomingSongsItem setSubmenu:nil];
476 [upcomingSongsItem setEnabled:NO];
480 - (void)rebuildPlaylistMenu
482 NSArray *playlists = [currentRemote playlists];
483 int i, curPlaylist = [currentRemote currentPlaylistIndex];
485 if (isPlayingRadio) {
488 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
491 [playlistMenu release];
492 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
494 for (i = 0; i < [playlists count]; i++) {
495 NSString *playlistName = [playlists objectAtIndex:i];
496 NSMenuItem *tempItem;
497 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
498 [tempItem setTarget:self];
499 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
500 [playlistMenu addItem:tempItem];
503 [playlistItem setSubmenu:playlistMenu];
504 [playlistItem setEnabled:YES];
507 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
511 //Build a menu with the list of all available EQ presets
512 - (void)rebuildEQPresetsMenu
514 NSArray *eqPresets = [currentRemote eqPresets];
515 NSMenuItem *enabledItem;
518 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
522 eqMenu = [[NSMenu alloc] initWithTitle:@""];
524 enabledItem = [eqMenu addItemWithTitle:@"EQ Enabled"
527 [eqMenu addItem:[NSMenuItem separatorItem]];
529 for (i = 0; i < [eqPresets count]; i++) {
530 NSString *setName = [eqPresets objectAtIndex:i];
531 NSMenuItem *tempItem;
533 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
534 [tempItem setTarget:self];
535 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
536 [eqMenu addItem:tempItem];
540 [eqItem setSubmenu:eqMenu];
542 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
547 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
548 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
549 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
550 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
551 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
556 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
558 if ([defaults objectForKey:@"PlayPause"] != nil) {
559 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
560 combo:[defaults keyComboForKey:@"PlayPause"]
561 target:self action:@selector(playPause:)];
564 if ([defaults objectForKey:@"NextTrack"] != nil) {
565 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
566 combo:[defaults keyComboForKey:@"NextTrack"]
567 target:self action:@selector(nextSong:)];
570 if ([defaults objectForKey:@"PrevTrack"] != nil) {
571 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
572 combo:[defaults keyComboForKey:@"PrevTrack"]
573 target:self action:@selector(prevSong:)];
576 if ([defaults objectForKey:@"TrackInfo"] != nil) {
577 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
578 combo:[defaults keyComboForKey:@"TrackInfo"]
579 target:self action:@selector(showCurrentTrackInfo)];
582 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
583 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
584 combo:[defaults keyComboForKey:@"UpcomingSongs"]
585 target:self action:@selector(showUpcomingSongs)];
589 //Called when the timer fires.
592 int playlist = [currentRemote currentPlaylistIndex];
593 ITMTRemotePlayerState playerState = [currentRemote playerState];
595 if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
596 int trackPlayingIndex = [currentRemote currentSongIndex];
598 if (trackPlayingIndex != lastSongIndex) {
599 BOOL wasPlayingRadio = isPlayingRadio;
600 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
602 if (isPlayingRadio && !wasPlayingRadio) {
604 for (i = 0; i < [playlistMenu numberOfItems]; i++)
606 [[playlistMenu itemAtIndex:i] setState:NSOffState];
609 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
612 if (wasPlayingRadio) {
613 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
614 [menu insertItem:temp atIndex:trackInfoIndex + 1];
619 lastSongIndex = trackPlayingIndex;
621 if (playlist != lastPlaylistIndex) {
622 BOOL wasPlayingRadio = isPlayingRadio;
623 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
625 if (isPlayingRadio && !wasPlayingRadio) {
627 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
628 [[playlistMenu itemAtIndex:i] setState:NSOffState];
632 if (wasPlayingRadio) {
633 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
634 [menu insertItem:temp atIndex:trackInfoIndex + 1];
638 if (!isPlayingRadio) {
640 for (i = 0; i < [playlistMenu numberOfItems]; i++)
642 [[playlistMenu itemAtIndex:i] setState:NSOffState];
644 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
648 lastSongIndex = trackPlayingIndex;
649 lastPlaylistIndex = playlist;
652 //Update Play/Pause menu item
653 if (playPauseMenuItem){
654 if (playerState == ITMTRemotePlayerPlaying) {
655 [playPauseMenuItem setTitle:@"Pause"];
657 [playPauseMenuItem setTitle:@"Play"];
660 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
661 NSMenuItem *menuItem;
662 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
663 //Remote the now playing item and add no song item
664 [menu removeItemAtIndex:trackInfoIndex];
666 if ([defaults boolForKey:@"showName"] == YES) {
667 [menu removeItemAtIndex:trackInfoIndex];
670 if ([defaults boolForKey:@"showTime"] == YES) {
671 [menu removeItemAtIndex:trackInfoIndex];
674 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
675 [menu removeItemAtIndex:trackInfoIndex];
678 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
679 [menu removeItemAtIndex:trackInfoIndex];
682 [playPauseMenuItem setTitle:@"Play"];
684 didHaveArtistName = NO;
685 didHaveAlbumName = NO;
686 lastPlaylistIndex = -1;
689 [upcomingSongsItem setSubmenu:nil];
690 [upcomingSongsItem setEnabled:NO];
692 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
693 [menu insertItem:menuItem atIndex:trackInfoIndex];
698 - (void)remotePlayerLaunched:(NSNotification *)note
700 isAppRunning = ITMTRemotePlayerRunning;
703 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
705 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
707 [self rebuildPlaylistMenu];
710 [self rebuildEQPresetsMenu];
712 [statusItem setMenu:menu]; //Set the menu back to the main one
715 - (void)remotePlayerTerminated:(NSNotification *)note
717 isAppRunning = ITMTRemotePlayerNotRunning;
720 menu = [[NSMenu alloc] initWithTitle:@""];
721 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
722 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
723 [menu addItem:[NSMenuItem separatorItem]];
724 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
725 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
726 [statusItem setMenu:menu];
728 [refreshTimer invalidate];
735 // Selectors - called from status item menu
739 // Plugin dependent selectors
741 - (void)playTrack:(id)sender
743 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
747 - (void)selectPlaylist:(id)sender
749 int playlist = [[sender representedObject] intValue];
750 if (!isPlayingRadio) {
751 int curPlaylist = [currentRemote currentPlaylistIndex];
752 if (curPlaylist > 0) {
753 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
756 [currentRemote switchToPlaylistAtIndex:playlist];
757 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
761 - (void)selectEQPreset:(id)sender
763 int curSet = [currentRemote currentEQPresetIndex];
764 int item = [[sender representedObject] intValue];
765 [currentRemote switchToEQAtIndex:item];
766 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
767 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
770 - (void)playPause:(id)sender
772 ITMTRemotePlayerState state = [currentRemote playerState];
774 if (state == ITMTRemotePlayerPlaying) {
775 [currentRemote pause];
776 [playPauseMenuItem setTitle:@"Play"];
777 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
778 [currentRemote pause];
779 [currentRemote play];
781 [currentRemote play];
782 [playPauseMenuItem setTitle:@"Pause"];
786 - (void)nextSong:(id)sender
788 [currentRemote goToNextSong];
791 - (void)prevSong:(id)sender
793 [currentRemote goToPreviousSong];
796 - (void)fastForward:(id)sender
798 [currentRemote forward];
799 [playPauseMenuItem setTitle:@"Play"];
802 - (void)rewind:(id)sender
804 [currentRemote rewind];
805 [playPauseMenuItem setTitle:@"Play"];
808 - (void)setSongRating:(id)sender
810 NSLog(@"%f", (float)[sender tag] / 100.0);
811 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
816 // Plugin independent selectors
819 - (void)quitMenuTunes:(id)sender
821 [NSApp terminate:self];
824 - (void)showPreferences:(id)sender
826 if (!prefsController) {
827 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
832 - (void)closePreferences
834 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
837 [prefsController release];
838 prefsController = nil;
843 // Show Current Track Info And Show Upcoming Songs Floaters
847 - (void)showCurrentTrackInfo
849 NSString *trackName = [currentRemote currentSongTitle];
850 if (!statusWindow && [trackName length]) {
851 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
852 NSString *stringToShow = @"";
854 if ([defaults boolForKey:@"showName"]) {
855 if ([defaults boolForKey:@"showArtist"]) {
856 NSString *trackArtist = [currentRemote currentSongArtist];
857 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
859 stringToShow = [stringToShow stringByAppendingString:trackName];
860 stringToShow = [stringToShow stringByAppendingString:@"\n"];
863 if ([defaults boolForKey:@"showAlbum"]) {
864 NSString *trackAlbum = [currentRemote currentSongAlbum];
865 if ([trackAlbum length]) {
866 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
867 stringToShow = [stringToShow stringByAppendingString:@"\n"];
871 if ([defaults boolForKey:@"showTime"]) {
872 NSString *trackTime = [currentRemote currentSongLength];
873 if ([trackTime length]) {
874 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
879 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
880 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
882 stringToShow = [stringToShow stringByAppendingString:
883 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
885 stringToShow = [stringToShow stringByAppendingString:
886 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
890 [statusWindow setText:stringToShow];
891 [NSTimer scheduledTimerWithTimeInterval:3.0
893 selector:@selector(fadeAndCloseStatusWindow)
899 - (void)showUpcomingSongs
901 int curPlaylist = [currentRemote currentPlaylistIndex];
903 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
906 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
907 int curTrack = [currentRemote currentSongIndex];
909 NSString *songs = @"";
911 statusWindow = [ITTransientStatusWindow sharedWindow];
912 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
914 NSString *curSong = [currentRemote songTitleAtIndex:i];
915 songs = [songs stringByAppendingString:curSong];
916 songs = [songs stringByAppendingString:@"\n"];
919 [statusWindow setText:songs];
920 [NSTimer scheduledTimerWithTimeInterval:3.0
922 selector:@selector(fadeAndCloseStatusWindow)
929 - (void)fadeAndCloseStatusWindow
931 [statusWindow orderOut:self];
934 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
935 onItem:(NSMenuItem *)item
937 unichar charcode = 'a';
939 long cocoaModifiers = 0;
940 static long carbonToCocoa[6][2] =
942 { cmdKey, NSCommandKeyMask },
943 { optionKey, NSAlternateKeyMask },
944 { controlKey, NSControlKeyMask },
945 { shiftKey, NSShiftKeyMask },
948 for (i = 0; i < 6; i++) {
949 if (modifiers & carbonToCocoa[i][0]) {
950 cocoaModifiers += carbonToCocoa[i][1];
953 [item setKeyEquivalentModifierMask:cocoaModifiers];
955 //Missing key combos for some keys. Must find them later.
969 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
970 NSLog(@"%@", menuRef);
971 SetMenuItemCommandKey(menuRef, 0, NO, 49);
972 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
973 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
979 charcode = NSDeleteFunctionKey;
995 charcode = NSF5FunctionKey;
999 charcode = NSF6FunctionKey;
1003 charcode = NSF7FunctionKey;
1007 charcode = NSF3FunctionKey;
1011 charcode = NSF8FunctionKey;
1015 charcode = NSF9FunctionKey;
1019 charcode = NSF11FunctionKey;
1023 charcode = NSF3FunctionKey;
1027 charcode = NSF14FunctionKey;
1031 charcode = NSF10FunctionKey;
1035 charcode = NSF12FunctionKey;
1039 charcode = NSF13FunctionKey;
1043 charcode = NSInsertFunctionKey;
1047 charcode = NSHomeFunctionKey;
1051 charcode = NSPageUpFunctionKey;
1055 charcode = NSDeleteFunctionKey;
1059 charcode = NSF4FunctionKey;
1063 charcode = NSEndFunctionKey;
1067 charcode = NSF2FunctionKey;
1071 charcode = NSPageDownFunctionKey;
1075 charcode = NSF1FunctionKey;
1079 charcode = NSLeftArrowFunctionKey;
1083 charcode = NSRightArrowFunctionKey;
1087 charcode = NSDownArrowFunctionKey;
1091 charcode = NSUpArrowFunctionKey;
1095 if (charcode == 'a') {
1096 unsigned long state;
1101 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1102 keyTrans = KeyTranslate(kchr, code, &state);
1103 charCode = keyTrans;
1104 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1105 } else if (charcode != 'b') {
1106 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1110 /*************************************************************************/
1112 #pragma mark NSApplication DELEGATE METHODS
1113 /*************************************************************************/
1115 - (void)applicationWillTerminate:(NSNotification *)note
1117 [self clearHotKeys];
1118 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1122 /*************************************************************************/
1124 #pragma mark DEALLOCATION METHODS
1125 /*************************************************************************/
1130 [refreshTimer invalidate];
1133 [currentRemote halt];
1134 [statusItem release];