3 ¥ Make preferences window pretty
5 ¥ Apple Events! Apple Events! Apple Events!
7 ¥ Finish up registration frontend
11 #import "PreferencesController.h"
12 #import "HotKeyCenter.h"
13 #import "StatusWindow.h"
15 @interface MenuTunes(Private)
16 - (ITMTRemote *)loadRemote;
18 - (void)rebuildUpcomingSongsMenu;
19 - (void)rebuildPlaylistMenu;
20 - (void)rebuildEQPresetsMenu;
23 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
24 onItem:(NSMenuItem *)item;
28 @implementation MenuTunes
30 /*************************************************************************/
32 #pragma mark INITIALIZATION METHODS
33 /*************************************************************************/
37 if ( ( self = [super init] ) ) {
38 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
39 statusWindow = [StatusWindow sharedWindow];
44 - (void)applicationDidFinishLaunching:(NSNotification *)note
46 currentRemote = [self loadRemote];
47 [currentRemote begin];
49 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
50 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
52 [self registerDefaultsIfNeeded];
54 menu = [[NSMenu alloc] initWithTitle:@""];
56 if ([currentRemote isAppRunning]) {
57 [self remotePlayerLaunched:nil];
59 [self remotePlayerTerminated:nil];
62 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
63 withLength:NSSquareStatusItemLength];
65 [statusItem setImage:[NSImage imageNamed:@"menu"]];
66 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
67 [statusItem setMenu:menu];
68 // Below line of code is for creating builds for Beta Testers
69 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
72 - (ITMTRemote *)loadRemote
74 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
77 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
78 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
81 while ( (bundlePath = [enumerator nextObject]) ) {
82 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
85 Class remoteClass = [remoteBundle principalClass];
87 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
88 [remoteClass isKindOfClass:[NSObject class]]) {
90 id remote = [remoteClass remote];
91 [remoteArray addObject:remote];
96 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
97 // if ( [remoteArray count] > 1 ) {
98 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
100 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
103 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
104 return [remoteArray objectAtIndex:0];
108 /*************************************************************************/
110 #pragma mark INSTANCE METHODS
111 /*************************************************************************/
113 - (void)registerDefaultsIfNeeded
115 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
116 if (![defaults objectForKey:@"menu"]) {
118 NSMutableDictionary *loginwindow;
119 NSMutableArray *loginarray;
123 [NSArray arrayWithObjects:
137 @"Current Track Info",
138 nil] forKey:@"menu"];
140 [defaults synchronize];
141 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
142 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
144 for (i = 0; i < [loginarray count]; i++) {
145 NSDictionary *tempDict = [loginarray objectAtIndex:i];
146 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
153 //We must fix it so it is no longer suxy
155 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
156 AEDesc scriptDesc, resultDesc;
157 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]];
158 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
160 AECreateDesc(typeChar, [script cString], [script cStringLength],
163 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
165 AEDisposeDesc(&scriptDesc);
166 AEDisposeDesc(&resultDesc);
168 CloseComponent(asComponent);
173 if (![defaults integerForKey:@"SongsInAdvance"])
175 [defaults setInteger:5 forKey:@"SongsInAdvance"];
178 if (![defaults objectForKey:@"showName"]) {
179 [defaults setBool:YES forKey:@"showName"];
182 if (![defaults objectForKey:@"showArtist"]) {
183 [defaults setBool:YES forKey:@"showArtist"];
186 if (![defaults objectForKey:@"showAlbum"]) {
187 [defaults setBool:NO forKey:@"showAlbum"];
190 if (![defaults objectForKey:@"showTime"]) {
191 [defaults setBool:NO forKey:@"showTime"];
195 //Recreate the status item menu
198 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
203 lastPlaylistIndex = -1;
204 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
205 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
207 while ([menu numberOfItems] > 0) {
208 [menu removeItemAtIndex:0];
211 playPauseMenuItem = nil;
212 upcomingSongsItem = nil;
214 [playlistMenu release];
220 for (i = 0; i < [myMenu count]; i++) {
221 NSString *item = [myMenu objectAtIndex:i];
222 if ([item isEqualToString:@"Play/Pause"]) {
223 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
224 playPauseMenuItem = [menu addItemWithTitle:@"Play"
225 action:@selector(playPause:)
227 [playPauseMenuItem setTarget:self];
230 [self setKeyEquivalentForCode:[tempCombo keyCode]
231 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
234 } else if ([item isEqualToString:@"Next Track"]) {
235 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
236 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
237 action:@selector(nextSong:)
240 [nextTrack setTarget:self];
242 [self setKeyEquivalentForCode:[tempCombo keyCode]
243 andModifiers:[tempCombo modifiers] onItem:nextTrack];
246 } else if ([item isEqualToString:@"Previous Track"]) {
247 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
248 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
249 action:@selector(prevSong:)
252 [prevTrack setTarget:self];
254 [self setKeyEquivalentForCode:[tempCombo keyCode]
255 andModifiers:[tempCombo modifiers] onItem:prevTrack];
258 } else if ([item isEqualToString:@"Fast Forward"]) {
259 [[menu addItemWithTitle:@"Fast Forward"
260 action:@selector(fastForward:)
261 keyEquivalent:@""] setTarget:self];
262 } else if ([item isEqualToString:@"Rewind"]) {
263 [[menu addItemWithTitle:@"Rewind"
264 action:@selector(rewind:)
265 keyEquivalent:@""] setTarget:self];
266 } else if ([item isEqualToString:@"Upcoming Songs"]) {
267 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
270 } else if ([item isEqualToString:@"Playlists"]) {
271 playlistItem = [menu addItemWithTitle:@"Playlists"
274 } else if ([item isEqualToString:@"EQ Presets"]) {
275 eqItem = [menu addItemWithTitle:@"EQ Presets"
278 } else if ([item isEqualToString:@"PreferencesÉ"]) {
279 [[menu addItemWithTitle:@"PreferencesÉ"
280 action:@selector(showPreferences:)
281 keyEquivalent:@""] setTarget:self];
282 } else if ([item isEqualToString:@"Quit"]) {
283 [[menu addItemWithTitle:@"Quit"
284 action:@selector(quitMenuTunes:)
285 keyEquivalent:@""] setTarget:self];
286 } else if ([item isEqualToString:@"Current Track Info"]) {
287 trackInfoIndex = [menu numberOfItems];
288 [menu addItemWithTitle:@"No Song"
291 } else if ([item isEqualToString:@"Song Rating"]) {
292 NSMenu *ratingSubmenu = [[NSMenu alloc] initWithTitle:@""];
293 unichar whiteStar = 'o';//2606;
294 unichar blackStar = 'x';//2605;
295 NSString *whiteStarString = [NSString stringWithCharacters:&whiteStar
297 NSString *blackStarString = [NSString stringWithCharacters:&blackStar
299 NSString *string = @"";
302 for (i = 0; i < 5; i++) {
303 string = [string stringByAppendingString:whiteStarString];
305 for (i = 0; i < 6; i++) {
306 NSMenuItem *ratingItem;
307 ratingItem = [ratingSubmenu addItemWithTitle:string action:@selector(setSongRating:) keyEquivalent:@""];
308 [ratingItem setTarget:self];
309 [ratingItem setTag:i * 20];
310 string = [string substringToIndex:4];
311 string = [blackStarString stringByAppendingString:string];
313 [[menu addItemWithTitle:@"Song Rating"
315 keyEquivalent:@""] setSubmenu:ratingSubmenu];
316 [ratingSubmenu autorelease];
317 } else if ([item isEqualToString:@"<separator>"]) {
318 [menu addItem:[NSMenuItem separatorItem]];
322 [self timerUpdate]; //Updates dynamic info in the menu
328 //Updates the menu with current player state, song, and upcoming songs
331 NSMenuItem *menuItem;
332 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
338 if (upcomingSongsItem) {
339 [self rebuildUpcomingSongsMenu];
343 [self rebuildPlaylistMenu];
347 [self rebuildEQPresetsMenu];
350 if (trackInfoIndex > -1) {
351 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
352 curSongName = [currentRemote currentSongTitle];
354 if ([defaults boolForKey:@"showAlbum"]) {
355 curAlbumName = [currentRemote currentSongAlbum];
358 if ([defaults boolForKey:@"showArtist"]) {
359 curArtistName = [currentRemote currentSongArtist];
362 if ([curSongName length] > 0) {
363 int index = [menu indexOfItemWithTitle:@"Now Playing"];
365 if ([defaults boolForKey:@"showName"]) {
366 [menu removeItemAtIndex:index + 1];
368 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
369 [menu removeItemAtIndex:index + 1];
371 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
372 [menu removeItemAtIndex:index + 1];
374 if ([defaults boolForKey:@"showTime"]) {
375 [menu removeItemAtIndex:index + 1];
379 if (!isPlayingRadio) {
380 if ([defaults boolForKey:@"showTime"]) {
381 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
384 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
388 if ([curArtistName length] > 0) {
389 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
392 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
396 if ([curAlbumName length] > 0) {
397 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
400 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
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 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
420 [menu removeItemAtIndex:trackInfoIndex];
422 if ([defaults boolForKey:@"showName"] == YES) {
423 [menu removeItemAtIndex:trackInfoIndex];
426 if ([defaults boolForKey:@"showTime"] == YES) {
427 [menu removeItemAtIndex:trackInfoIndex];
430 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
431 [menu removeItemAtIndex:trackInfoIndex];
434 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
435 [menu removeItemAtIndex:trackInfoIndex];
438 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
439 [menu insertItem:menuItem atIndex:trackInfoIndex];
443 if ([defaults boolForKey:@"showArtist"]) {
444 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
447 if ([defaults boolForKey:@"showAlbum"]) {
448 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
453 //Rebuild the upcoming songs submenu. Can be improved a lot.
454 - (void)rebuildUpcomingSongsMenu
456 int curIndex = [currentRemote currentPlaylistIndex];
457 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
458 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
460 if (!isPlayingRadio) {
462 int curTrack = [currentRemote currentSongIndex];
465 [upcomingSongsMenu release];
466 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
467 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
468 [upcomingSongsItem setEnabled:YES];
470 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
472 NSString *curSong = [currentRemote songTitleAtIndex:i];
473 NSMenuItem *songItem;
474 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
475 [songItem setTarget:self];
476 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
477 [upcomingSongsMenu addItem:songItem];
485 [upcomingSongsItem setSubmenu:nil];
486 [upcomingSongsItem setEnabled:NO];
490 - (void)rebuildPlaylistMenu
492 NSArray *playlists = [currentRemote playlists];
493 int i, curPlaylist = [currentRemote currentPlaylistIndex];
495 if (isPlayingRadio) {
498 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
501 [playlistMenu release];
502 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
504 for (i = 0; i < [playlists count]; i++) {
505 NSString *playlistName = [playlists objectAtIndex:i];
506 NSMenuItem *tempItem;
507 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
508 [tempItem setTarget:self];
509 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
510 [playlistMenu addItem:tempItem];
513 [playlistItem setSubmenu:playlistMenu];
514 [playlistItem setEnabled:YES];
517 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
521 //Build a menu with the list of all available EQ presets
522 - (void)rebuildEQPresetsMenu
524 NSArray *eqPresets = [currentRemote eqPresets];
527 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
531 eqMenu = [[NSMenu alloc] initWithTitle:@""];
533 for (i = 0; i < [eqPresets count]; i++) {
534 NSString *setName = [eqPresets objectAtIndex:i];
535 NSMenuItem *tempItem;
537 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
538 [tempItem setTarget:self];
539 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
540 [eqMenu addItem:tempItem];
544 [eqItem setSubmenu:eqMenu];
546 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
551 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
552 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
553 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
554 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
555 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
560 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
562 if ([defaults objectForKey:@"PlayPause"] != nil) {
563 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
564 combo:[defaults keyComboForKey:@"PlayPause"]
565 target:self action:@selector(playPause:)];
568 if ([defaults objectForKey:@"NextTrack"] != nil) {
569 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
570 combo:[defaults keyComboForKey:@"NextTrack"]
571 target:self action:@selector(nextSong:)];
574 if ([defaults objectForKey:@"PrevTrack"] != nil) {
575 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
576 combo:[defaults keyComboForKey:@"PrevTrack"]
577 target:self action:@selector(prevSong:)];
580 if ([defaults objectForKey:@"TrackInfo"] != nil) {
581 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
582 combo:[defaults keyComboForKey:@"TrackInfo"]
583 target:self action:@selector(showCurrentTrackInfo)];
586 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
587 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
588 combo:[defaults keyComboForKey:@"UpcomingSongs"]
589 target:self action:@selector(showUpcomingSongs)];
593 //Called when the timer fires.
596 int playlist = [currentRemote currentPlaylistIndex];
597 ITMTRemotePlayerState playerState = [currentRemote playerState];
599 if ((playlist > 0) || playerState != stopped) {
600 int trackPlayingIndex = [currentRemote currentSongIndex];
602 if (trackPlayingIndex != lastSongIndex) {
603 BOOL wasPlayingRadio = isPlayingRadio;
604 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
606 if (isPlayingRadio && !wasPlayingRadio) {
608 for (i = 0; i < [playlistMenu numberOfItems]; i++)
610 [[playlistMenu itemAtIndex:i] setState:NSOffState];
613 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
616 if (wasPlayingRadio) {
617 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
618 [menu insertItem:temp atIndex:trackInfoIndex + 1];
623 lastSongIndex = trackPlayingIndex;
625 if (playlist != lastPlaylistIndex) {
626 BOOL wasPlayingRadio = isPlayingRadio;
627 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
629 if (isPlayingRadio && !wasPlayingRadio) {
631 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
632 [[playlistMenu itemAtIndex:i] setState:NSOffState];
636 if (wasPlayingRadio) {
637 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
638 [menu insertItem:temp atIndex:trackInfoIndex + 1];
642 if (!isPlayingRadio) {
644 for (i = 0; i < [playlistMenu numberOfItems]; i++)
646 [[playlistMenu itemAtIndex:i] setState:NSOffState];
648 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
652 lastSongIndex = trackPlayingIndex;
653 lastPlaylistIndex = playlist;
656 //Update Play/Pause menu item
657 if (playPauseMenuItem){
658 if (playerState == playing) {
659 [playPauseMenuItem setTitle:@"Pause"];
661 [playPauseMenuItem setTitle:@"Play"];
667 - (void)remotePlayerLaunched:(NSNotification *)note
672 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
674 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
675 [self rebuildPlaylistMenu];
676 [statusItem setMenu:menu]; //Set the menu back to the main one
679 - (void)remotePlayerTerminated:(NSNotification *)note
684 menu = [[NSMenu alloc] initWithTitle:@""];
685 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
686 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
687 [menu addItem:[NSMenuItem separatorItem]];
688 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
689 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
690 [statusItem setMenu:menu];
692 [refreshTimer invalidate];
699 // Selectors - called from status item menu
703 // Plugin dependent selectors
705 - (void)playTrack:(id)sender
707 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
711 - (void)selectPlaylist:(id)sender
713 int playlist = [[sender representedObject] intValue];
714 if (!isPlayingRadio) {
715 int curPlaylist = [currentRemote currentPlaylistIndex];
716 if (curPlaylist > 0) {
717 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
720 [currentRemote switchToPlaylistAtIndex:playlist];
721 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
725 - (void)selectEQPreset:(id)sender
727 int curSet = [currentRemote currentEQPresetIndex];
728 int item = [[sender representedObject] intValue];
729 [currentRemote switchToEQAtIndex:item];
730 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
731 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
734 - (void)playPause:(id)sender
736 ITMTRemotePlayerState state = [currentRemote playerState];
738 if (state == playing) {
739 [currentRemote pause];
740 [playPauseMenuItem setTitle:@"Play"];
741 } else if ((state == forwarding) || (state == rewinding)) {
742 [currentRemote pause];
743 [currentRemote play];
745 [currentRemote play];
746 [playPauseMenuItem setTitle:@"Pause"];
750 - (void)nextSong:(id)sender
752 [currentRemote goToNextSong];
755 - (void)prevSong:(id)sender
757 [currentRemote goToPreviousSong];
760 - (void)fastForward:(id)sender
762 [currentRemote fastForward];
763 [playPauseMenuItem setTitle:@"Play"];
766 - (void)rewind:(id)sender
768 [currentRemote rewind];
769 [playPauseMenuItem setTitle:@"Play"];
772 - (void)setSongRating:(id)sender
774 //[currentRemote setCurrentSongRating:[sender tag]];
779 // Plugin independent selectors
782 - (void)quitMenuTunes:(id)sender
784 [NSApp terminate:self];
787 - (void)showPreferences:(id)sender
789 if (!prefsController) {
790 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
795 - (void)closePreferences
800 [prefsController release];
801 prefsController = nil;
806 // Show Current Track Info And Show Upcoming Songs Floaters
810 - (void)showCurrentTrackInfo
812 NSString *trackName = [currentRemote currentSongTitle];
813 if (!statusWindow && [trackName length]) {
814 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
815 NSString *stringToShow = @"";
817 if ([defaults boolForKey:@"showName"]) {
818 if ([defaults boolForKey:@"showArtist"]) {
819 NSString *trackArtist = [currentRemote currentSongArtist];
820 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
822 stringToShow = [stringToShow stringByAppendingString:trackName];
823 stringToShow = [stringToShow stringByAppendingString:@"\n"];
826 if ([defaults boolForKey:@"showAlbum"]) {
827 NSString *trackAlbum = [currentRemote currentSongAlbum];
828 if ([trackAlbum length]) {
829 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
830 stringToShow = [stringToShow stringByAppendingString:@"\n"];
834 if ([defaults boolForKey:@"showTime"]) {
835 NSString *trackTime = [currentRemote currentSongLength];
836 if ([trackTime length]) {
837 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
842 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
843 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
845 stringToShow = [stringToShow stringByAppendingString:
846 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
848 stringToShow = [stringToShow stringByAppendingString:
849 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
853 [statusWindow setText:stringToShow];
854 [NSTimer scheduledTimerWithTimeInterval:3.0
856 selector:@selector(fadeAndCloseStatusWindow)
862 - (void)showUpcomingSongs
864 int curPlaylist = [currentRemote currentPlaylistIndex];
866 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
869 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
870 int curTrack = [currentRemote currentSongIndex];
872 NSString *songs = @"";
874 statusWindow = [ITTransientStatusWindow sharedWindow];
875 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
877 NSString *curSong = [currentRemote songTitleAtIndex:i];
878 songs = [songs stringByAppendingString:curSong];
879 songs = [songs stringByAppendingString:@"\n"];
882 [statusWindow setText:songs];
883 [NSTimer scheduledTimerWithTimeInterval:3.0
885 selector:@selector(fadeAndCloseStatusWindow)
892 - (void)fadeAndCloseStatusWindow
894 [statusWindow orderOut:self];
897 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
898 onItem:(NSMenuItem *)item
900 unichar charcode = 'a';
902 long cocoaModifiers = 0;
903 static long carbonToCocoa[6][2] =
905 { cmdKey, NSCommandKeyMask },
906 { optionKey, NSAlternateKeyMask },
907 { controlKey, NSControlKeyMask },
908 { shiftKey, NSShiftKeyMask },
911 for (i = 0; i < 6; i++) {
912 if (modifiers & carbonToCocoa[i][0]) {
913 cocoaModifiers += carbonToCocoa[i][1];
916 [item setKeyEquivalentModifierMask:cocoaModifiers];
918 //Missing key combos for some keys. Must find them later.
932 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
933 NSLog(@"%@", menuRef);
934 SetMenuItemCommandKey(menuRef, 0, NO, 49);
935 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
936 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
942 charcode = NSDeleteFunctionKey;
958 charcode = NSF5FunctionKey;
962 charcode = NSF6FunctionKey;
966 charcode = NSF7FunctionKey;
970 charcode = NSF3FunctionKey;
974 charcode = NSF8FunctionKey;
978 charcode = NSF9FunctionKey;
982 charcode = NSF11FunctionKey;
986 charcode = NSF3FunctionKey;
990 charcode = NSF14FunctionKey;
994 charcode = NSF10FunctionKey;
998 charcode = NSF12FunctionKey;
1002 charcode = NSF13FunctionKey;
1006 charcode = NSInsertFunctionKey;
1010 charcode = NSHomeFunctionKey;
1014 charcode = NSPageUpFunctionKey;
1018 charcode = NSDeleteFunctionKey;
1022 charcode = NSF4FunctionKey;
1026 charcode = NSEndFunctionKey;
1030 charcode = NSF2FunctionKey;
1034 charcode = NSPageDownFunctionKey;
1038 charcode = NSF1FunctionKey;
1042 charcode = NSLeftArrowFunctionKey;
1046 charcode = NSRightArrowFunctionKey;
1050 charcode = NSDownArrowFunctionKey;
1054 charcode = NSUpArrowFunctionKey;
1058 if (charcode == 'a') {
1059 unsigned long state;
1064 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1065 keyTrans = KeyTranslate(kchr, code, &state);
1066 charCode = keyTrans;
1067 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1068 } else if (charcode != 'b') {
1069 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1073 /*************************************************************************/
1075 #pragma mark NSApplication DELEGATE METHODS
1076 /*************************************************************************/
1078 - (void)applicationWillTerminate:(NSNotification *)note
1080 [self clearHotKeys];
1081 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1085 /*************************************************************************/
1087 #pragma mark DEALLOCATION METHODS
1088 /*************************************************************************/
1093 [refreshTimer invalidate];
1096 [currentRemote halt];
1097 [statusItem release];