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 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
718 [currentRemote switchToPlaylistAtIndex:playlist];
719 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
723 - (void)selectEQPreset:(id)sender
725 int curSet = [currentRemote currentEQPresetIndex];
726 int item = [[sender representedObject] intValue];
727 [currentRemote switchToEQAtIndex:item];
728 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
729 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
732 - (void)playPause:(id)sender
734 ITMTRemotePlayerState state = [currentRemote playerState];
736 if (state == playing) {
737 [currentRemote pause];
738 [playPauseMenuItem setTitle:@"Play"];
739 } else if ((state == forwarding) || (state == rewinding)) {
740 [currentRemote pause];
741 [currentRemote play];
743 [currentRemote play];
744 [playPauseMenuItem setTitle:@"Pause"];
748 - (void)nextSong:(id)sender
750 [currentRemote goToNextSong];
753 - (void)prevSong:(id)sender
755 [currentRemote goToPreviousSong];
758 - (void)fastForward:(id)sender
760 [currentRemote fastForward];
761 [playPauseMenuItem setTitle:@"Play"];
764 - (void)rewind:(id)sender
766 [currentRemote rewind];
767 [playPauseMenuItem setTitle:@"Play"];
770 - (void)setSongRating:(id)sender
772 //[currentRemote setCurrentSongRating:[sender tag]];
777 // Plugin independent selectors
780 - (void)quitMenuTunes:(id)sender
782 [NSApp terminate:self];
785 - (void)showPreferences:(id)sender
787 if (!prefsController) {
788 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
793 - (void)closePreferences
798 [prefsController release];
799 prefsController = nil;
804 // Show Current Track Info And Show Upcoming Songs Floaters
808 - (void)showCurrentTrackInfo
810 NSString *trackName = [currentRemote currentSongTitle];
811 if (!statusWindow && [trackName length]) {
812 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
813 NSString *stringToShow = @"";
815 if ([defaults boolForKey:@"showName"]) {
816 if ([defaults boolForKey:@"showArtist"]) {
817 NSString *trackArtist = [currentRemote currentSongArtist];
818 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
820 stringToShow = [stringToShow stringByAppendingString:trackName];
821 stringToShow = [stringToShow stringByAppendingString:@"\n"];
824 if ([defaults boolForKey:@"showAlbum"]) {
825 NSString *trackAlbum = [currentRemote currentSongAlbum];
826 if ([trackAlbum length]) {
827 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
828 stringToShow = [stringToShow stringByAppendingString:@"\n"];
832 if ([defaults boolForKey:@"showTime"]) {
833 NSString *trackTime = [currentRemote currentSongLength];
834 if ([trackTime length]) {
835 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
840 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
841 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
843 stringToShow = [stringToShow stringByAppendingString:
844 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
846 stringToShow = [stringToShow stringByAppendingString:
847 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
851 [statusWindow setText:stringToShow];
852 [NSTimer scheduledTimerWithTimeInterval:3.0
854 selector:@selector(fadeAndCloseStatusWindow)
860 - (void)showUpcomingSongs
862 int curPlaylist = [currentRemote currentPlaylistIndex];
864 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
867 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
868 int curTrack = [currentRemote currentSongIndex];
870 NSString *songs = @"";
872 statusWindow = [ITTransientStatusWindow sharedWindow];
873 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
875 NSString *curSong = [currentRemote songTitleAtIndex:i];
876 songs = [songs stringByAppendingString:curSong];
877 songs = [songs stringByAppendingString:@"\n"];
880 [statusWindow setText:songs];
881 [NSTimer scheduledTimerWithTimeInterval:3.0
883 selector:@selector(fadeAndCloseStatusWindow)
890 - (void)fadeAndCloseStatusWindow
892 [statusWindow orderOut:self];
895 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
896 onItem:(NSMenuItem *)item
898 unichar charcode = 'a';
900 long cocoaModifiers = 0;
901 static long carbonToCocoa[6][2] =
903 { cmdKey, NSCommandKeyMask },
904 { optionKey, NSAlternateKeyMask },
905 { controlKey, NSControlKeyMask },
906 { shiftKey, NSShiftKeyMask },
909 for (i = 0; i < 6; i++) {
910 if (modifiers & carbonToCocoa[i][0]) {
911 cocoaModifiers += carbonToCocoa[i][1];
914 [item setKeyEquivalentModifierMask:cocoaModifiers];
916 //Missing key combos for some keys. Must find them later.
930 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
931 NSLog(@"%@", menuRef);
932 SetMenuItemCommandKey(menuRef, 0, NO, 49);
933 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
934 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
940 charcode = NSDeleteFunctionKey;
956 charcode = NSF5FunctionKey;
960 charcode = NSF6FunctionKey;
964 charcode = NSF7FunctionKey;
968 charcode = NSF3FunctionKey;
972 charcode = NSF8FunctionKey;
976 charcode = NSF9FunctionKey;
980 charcode = NSF11FunctionKey;
984 charcode = NSF3FunctionKey;
988 charcode = NSF14FunctionKey;
992 charcode = NSF10FunctionKey;
996 charcode = NSF12FunctionKey;
1000 charcode = NSF13FunctionKey;
1004 charcode = NSInsertFunctionKey;
1008 charcode = NSHomeFunctionKey;
1012 charcode = NSPageUpFunctionKey;
1016 charcode = NSDeleteFunctionKey;
1020 charcode = NSF4FunctionKey;
1024 charcode = NSEndFunctionKey;
1028 charcode = NSF2FunctionKey;
1032 charcode = NSPageDownFunctionKey;
1036 charcode = NSF1FunctionKey;
1040 charcode = NSLeftArrowFunctionKey;
1044 charcode = NSRightArrowFunctionKey;
1048 charcode = NSDownArrowFunctionKey;
1052 charcode = NSUpArrowFunctionKey;
1056 if (charcode == 'a') {
1057 unsigned long state;
1062 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1063 keyTrans = KeyTranslate(kchr, code, &state);
1064 charCode = keyTrans;
1065 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1066 } else if (charcode != 'b') {
1067 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1071 /*************************************************************************/
1073 #pragma mark NSApplication DELEGATE METHODS
1074 /*************************************************************************/
1076 - (void)applicationWillTerminate:(NSNotification *)note
1078 [self clearHotKeys];
1079 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1083 /*************************************************************************/
1085 #pragma mark DEALLOCATION METHODS
1086 /*************************************************************************/
1091 [refreshTimer invalidate];
1094 [currentRemote halt];
1095 [statusItem release];