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 isAppRunning]) {
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];
329 if (upcomingSongsItem) {
330 [self rebuildUpcomingSongsMenu];
334 [self rebuildPlaylistMenu];
338 [self rebuildEQPresetsMenu];
341 if (trackInfoIndex > -1) {
342 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
343 curSongName = [currentRemote currentSongTitle];
345 if ([defaults boolForKey:@"showAlbum"]) {
346 curAlbumName = [currentRemote currentSongAlbum];
349 if ([defaults boolForKey:@"showArtist"]) {
350 curArtistName = [currentRemote currentSongArtist];
353 if ([curSongName length] > 0) {
354 int index = [menu indexOfItemWithTitle:@"Now Playing"];
356 if ([defaults boolForKey:@"showName"]) {
357 [menu removeItemAtIndex:index + 1];
359 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
360 [menu removeItemAtIndex:index + 1];
362 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
363 [menu removeItemAtIndex:index + 1];
365 if ([defaults boolForKey:@"showTime"]) {
366 [menu removeItemAtIndex:index + 1];
370 if (!isPlayingRadio) {
371 if ([defaults boolForKey:@"showTime"]) {
372 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
375 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
379 if ([curArtistName length] > 0) {
380 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
383 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
387 if ([curAlbumName length] > 0) {
388 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
391 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
396 if ([defaults boolForKey:@"showName"]) {
397 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
400 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
405 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
406 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
407 [menu insertItem:menuItem atIndex:trackInfoIndex];
410 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
411 [menu removeItemAtIndex:trackInfoIndex];
413 if ([defaults boolForKey:@"showName"] == YES) {
414 [menu removeItemAtIndex:trackInfoIndex];
417 if ([defaults boolForKey:@"showTime"] == YES) {
418 [menu removeItemAtIndex:trackInfoIndex];
421 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
422 [menu removeItemAtIndex:trackInfoIndex];
425 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
426 [menu removeItemAtIndex:trackInfoIndex];
429 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
430 [menu insertItem:menuItem atIndex:trackInfoIndex];
434 if ([defaults boolForKey:@"showArtist"]) {
435 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
438 if ([defaults boolForKey:@"showAlbum"]) {
439 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
444 //Rebuild the upcoming songs submenu. Can be improved a lot.
445 - (void)rebuildUpcomingSongsMenu
447 int curIndex = [currentRemote currentPlaylistIndex];
448 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
449 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
451 if (!isPlayingRadio) {
453 int curTrack = [currentRemote currentSongIndex];
456 [upcomingSongsMenu release];
457 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
458 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
459 [upcomingSongsItem setEnabled:YES];
461 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
463 NSString *curSong = [currentRemote songTitleAtIndex:i];
464 NSMenuItem *songItem;
465 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
466 [songItem setTarget:self];
467 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
468 [upcomingSongsMenu addItem:songItem];
476 [upcomingSongsItem setSubmenu:nil];
477 [upcomingSongsItem setEnabled:NO];
481 - (void)rebuildPlaylistMenu
483 NSArray *playlists = [currentRemote playlists];
484 int i, curPlaylist = [currentRemote currentPlaylistIndex];
486 if (isPlayingRadio) {
489 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
492 [playlistMenu release];
493 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
495 for (i = 0; i < [playlists count]; i++) {
496 NSString *playlistName = [playlists objectAtIndex:i];
497 NSMenuItem *tempItem;
498 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
499 [tempItem setTarget:self];
500 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
501 [playlistMenu addItem:tempItem];
504 [playlistItem setSubmenu:playlistMenu];
505 [playlistItem setEnabled:YES];
508 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
512 //Build a menu with the list of all available EQ presets
513 - (void)rebuildEQPresetsMenu
515 NSArray *eqPresets = [currentRemote eqPresets];
516 NSMenuItem *enabledItem;
519 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
523 eqMenu = [[NSMenu alloc] initWithTitle:@""];
525 enabledItem = [eqMenu addItemWithTitle:@"EQ Enabled"
528 [eqMenu addItem:[NSMenuItem separatorItem]];
530 for (i = 0; i < [eqPresets count]; i++) {
531 NSString *setName = [eqPresets objectAtIndex:i];
532 NSMenuItem *tempItem;
534 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
535 [tempItem setTarget:self];
536 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
537 [eqMenu addItem:tempItem];
541 [eqItem setSubmenu:eqMenu];
543 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
548 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
549 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
550 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
551 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
552 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
557 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
559 if ([defaults objectForKey:@"PlayPause"] != nil) {
560 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
561 combo:[defaults keyComboForKey:@"PlayPause"]
562 target:self action:@selector(playPause:)];
565 if ([defaults objectForKey:@"NextTrack"] != nil) {
566 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
567 combo:[defaults keyComboForKey:@"NextTrack"]
568 target:self action:@selector(nextSong:)];
571 if ([defaults objectForKey:@"PrevTrack"] != nil) {
572 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
573 combo:[defaults keyComboForKey:@"PrevTrack"]
574 target:self action:@selector(prevSong:)];
577 if ([defaults objectForKey:@"TrackInfo"] != nil) {
578 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
579 combo:[defaults keyComboForKey:@"TrackInfo"]
580 target:self action:@selector(showCurrentTrackInfo)];
583 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
584 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
585 combo:[defaults keyComboForKey:@"UpcomingSongs"]
586 target:self action:@selector(showUpcomingSongs)];
590 //Called when the timer fires.
593 int playlist = [currentRemote currentPlaylistIndex];
594 ITMTRemotePlayerState playerState = [currentRemote playerState];
596 if ((playlist > 0) || playerState != stopped) {
597 int trackPlayingIndex = [currentRemote currentSongIndex];
599 if (trackPlayingIndex != lastSongIndex) {
600 BOOL wasPlayingRadio = isPlayingRadio;
601 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
603 if (isPlayingRadio && !wasPlayingRadio) {
605 for (i = 0; i < [playlistMenu numberOfItems]; i++)
607 [[playlistMenu itemAtIndex:i] setState:NSOffState];
610 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
613 if (wasPlayingRadio) {
614 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
615 [menu insertItem:temp atIndex:trackInfoIndex + 1];
620 lastSongIndex = trackPlayingIndex;
622 if (playlist != lastPlaylistIndex) {
623 BOOL wasPlayingRadio = isPlayingRadio;
624 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
626 if (isPlayingRadio && !wasPlayingRadio) {
628 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
629 [[playlistMenu itemAtIndex:i] setState:NSOffState];
633 if (wasPlayingRadio) {
634 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
635 [menu insertItem:temp atIndex:trackInfoIndex + 1];
639 if (!isPlayingRadio) {
641 for (i = 0; i < [playlistMenu numberOfItems]; i++)
643 [[playlistMenu itemAtIndex:i] setState:NSOffState];
645 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
649 lastSongIndex = trackPlayingIndex;
650 lastPlaylistIndex = playlist;
653 //Update Play/Pause menu item
654 if (playPauseMenuItem){
655 if (playerState == playing) {
656 [playPauseMenuItem setTitle:@"Pause"];
658 [playPauseMenuItem setTitle:@"Play"];
661 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
662 NSMenuItem *menuItem;
663 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
664 //Remote the now playing item and add no song item
665 [menu removeItemAtIndex:trackInfoIndex];
667 if ([defaults boolForKey:@"showName"] == YES) {
668 [menu removeItemAtIndex:trackInfoIndex];
671 if ([defaults boolForKey:@"showTime"] == YES) {
672 [menu removeItemAtIndex:trackInfoIndex];
675 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
676 [menu removeItemAtIndex:trackInfoIndex];
679 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
680 [menu removeItemAtIndex:trackInfoIndex];
683 [playPauseMenuItem setTitle:@"Play"];
685 didHaveArtistName = NO;
686 didHaveAlbumName = NO;
687 lastPlaylistIndex = -1;
690 [upcomingSongsItem setSubmenu:nil];
691 [upcomingSongsItem setEnabled:NO];
693 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
694 [menu insertItem:menuItem atIndex:trackInfoIndex];
699 - (void)remotePlayerLaunched:(NSNotification *)note
704 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
706 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
708 [self rebuildPlaylistMenu];
711 [self rebuildEQPresetsMenu];
713 [statusItem setMenu:menu]; //Set the menu back to the main one
716 - (void)remotePlayerTerminated:(NSNotification *)note
721 menu = [[NSMenu alloc] initWithTitle:@""];
722 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
723 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
724 [menu addItem:[NSMenuItem separatorItem]];
725 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
726 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
727 [statusItem setMenu:menu];
729 [refreshTimer invalidate];
736 // Selectors - called from status item menu
740 // Plugin dependent selectors
742 - (void)playTrack:(id)sender
744 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
748 - (void)selectPlaylist:(id)sender
750 int playlist = [[sender representedObject] intValue];
751 if (!isPlayingRadio) {
752 int curPlaylist = [currentRemote currentPlaylistIndex];
753 if (curPlaylist > 0) {
754 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
757 [currentRemote switchToPlaylistAtIndex:playlist];
758 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
762 - (void)selectEQPreset:(id)sender
764 int curSet = [currentRemote currentEQPresetIndex];
765 int item = [[sender representedObject] intValue];
766 [currentRemote switchToEQAtIndex:item];
767 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
768 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
771 - (void)playPause:(id)sender
773 ITMTRemotePlayerState state = [currentRemote playerState];
775 if (state == playing) {
776 [currentRemote pause];
777 [playPauseMenuItem setTitle:@"Play"];
778 } else if ((state == forwarding) || (state == rewinding)) {
779 [currentRemote pause];
780 [currentRemote play];
782 [currentRemote play];
783 [playPauseMenuItem setTitle:@"Pause"];
787 - (void)nextSong:(id)sender
789 [currentRemote goToNextSong];
792 - (void)prevSong:(id)sender
794 [currentRemote goToPreviousSong];
797 - (void)fastForward:(id)sender
799 [currentRemote fastForward];
800 [playPauseMenuItem setTitle:@"Play"];
803 - (void)rewind:(id)sender
805 [currentRemote rewind];
806 [playPauseMenuItem setTitle:@"Play"];
809 - (void)setSongRating:(id)sender
811 NSLog(@"%f", (float)[sender tag] / 100.0);
812 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
817 // Plugin independent selectors
820 - (void)quitMenuTunes:(id)sender
822 [NSApp terminate:self];
825 - (void)showPreferences:(id)sender
827 if (!prefsController) {
828 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
833 - (void)closePreferences
838 [prefsController release];
839 prefsController = nil;
844 // Show Current Track Info And Show Upcoming Songs Floaters
848 - (void)showCurrentTrackInfo
850 NSString *trackName = [currentRemote currentSongTitle];
851 if (!statusWindow && [trackName length]) {
852 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
853 NSString *stringToShow = @"";
855 if ([defaults boolForKey:@"showName"]) {
856 if ([defaults boolForKey:@"showArtist"]) {
857 NSString *trackArtist = [currentRemote currentSongArtist];
858 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
860 stringToShow = [stringToShow stringByAppendingString:trackName];
861 stringToShow = [stringToShow stringByAppendingString:@"\n"];
864 if ([defaults boolForKey:@"showAlbum"]) {
865 NSString *trackAlbum = [currentRemote currentSongAlbum];
866 if ([trackAlbum length]) {
867 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
868 stringToShow = [stringToShow stringByAppendingString:@"\n"];
872 if ([defaults boolForKey:@"showTime"]) {
873 NSString *trackTime = [currentRemote currentSongLength];
874 if ([trackTime length]) {
875 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
880 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
881 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
883 stringToShow = [stringToShow stringByAppendingString:
884 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
886 stringToShow = [stringToShow stringByAppendingString:
887 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
891 [statusWindow setText:stringToShow];
892 [NSTimer scheduledTimerWithTimeInterval:3.0
894 selector:@selector(fadeAndCloseStatusWindow)
900 - (void)showUpcomingSongs
902 int curPlaylist = [currentRemote currentPlaylistIndex];
904 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
907 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
908 int curTrack = [currentRemote currentSongIndex];
910 NSString *songs = @"";
912 statusWindow = [ITTransientStatusWindow sharedWindow];
913 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
915 NSString *curSong = [currentRemote songTitleAtIndex:i];
916 songs = [songs stringByAppendingString:curSong];
917 songs = [songs stringByAppendingString:@"\n"];
920 [statusWindow setText:songs];
921 [NSTimer scheduledTimerWithTimeInterval:3.0
923 selector:@selector(fadeAndCloseStatusWindow)
930 - (void)fadeAndCloseStatusWindow
932 [statusWindow orderOut:self];
935 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
936 onItem:(NSMenuItem *)item
938 unichar charcode = 'a';
940 long cocoaModifiers = 0;
941 static long carbonToCocoa[6][2] =
943 { cmdKey, NSCommandKeyMask },
944 { optionKey, NSAlternateKeyMask },
945 { controlKey, NSControlKeyMask },
946 { shiftKey, NSShiftKeyMask },
949 for (i = 0; i < 6; i++) {
950 if (modifiers & carbonToCocoa[i][0]) {
951 cocoaModifiers += carbonToCocoa[i][1];
954 [item setKeyEquivalentModifierMask:cocoaModifiers];
956 //Missing key combos for some keys. Must find them later.
970 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
971 NSLog(@"%@", menuRef);
972 SetMenuItemCommandKey(menuRef, 0, NO, 49);
973 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
974 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
980 charcode = NSDeleteFunctionKey;
996 charcode = NSF5FunctionKey;
1000 charcode = NSF6FunctionKey;
1004 charcode = NSF7FunctionKey;
1008 charcode = NSF3FunctionKey;
1012 charcode = NSF8FunctionKey;
1016 charcode = NSF9FunctionKey;
1020 charcode = NSF11FunctionKey;
1024 charcode = NSF3FunctionKey;
1028 charcode = NSF14FunctionKey;
1032 charcode = NSF10FunctionKey;
1036 charcode = NSF12FunctionKey;
1040 charcode = NSF13FunctionKey;
1044 charcode = NSInsertFunctionKey;
1048 charcode = NSHomeFunctionKey;
1052 charcode = NSPageUpFunctionKey;
1056 charcode = NSDeleteFunctionKey;
1060 charcode = NSF4FunctionKey;
1064 charcode = NSEndFunctionKey;
1068 charcode = NSF2FunctionKey;
1072 charcode = NSPageDownFunctionKey;
1076 charcode = NSF1FunctionKey;
1080 charcode = NSLeftArrowFunctionKey;
1084 charcode = NSRightArrowFunctionKey;
1088 charcode = NSDownArrowFunctionKey;
1092 charcode = NSUpArrowFunctionKey;
1096 if (charcode == 'a') {
1097 unsigned long state;
1102 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1103 keyTrans = KeyTranslate(kchr, code, &state);
1104 charCode = keyTrans;
1105 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1106 } else if (charcode != 'b') {
1107 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1111 /*************************************************************************/
1113 #pragma mark NSApplication DELEGATE METHODS
1114 /*************************************************************************/
1116 - (void)applicationWillTerminate:(NSNotification *)note
1118 [self clearHotKeys];
1119 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1123 /*************************************************************************/
1125 #pragma mark DEALLOCATION METHODS
1126 /*************************************************************************/
1131 [refreshTimer invalidate];
1134 [currentRemote halt];
1135 [statusItem release];