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(iTunesTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
50 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
52 [self registerDefaultsIfNeeded];
54 menu = [[NSMenu alloc] initWithTitle:@""];
56 if ([currentRemote isAppRunning]) {
57 [self iTunesLaunched:nil];
59 [self iTunesTerminated: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__]];
70 [statusWindow orderFront:self]; //DEBUG
73 - (ITMTRemote *)loadRemote
75 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
78 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
79 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
82 while ( (bundlePath = [enumerator nextObject]) ) {
83 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
86 Class remoteClass = [remoteBundle principalClass];
88 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
89 [remoteClass isKindOfClass:[NSObject class]]) {
91 id remote = [remoteClass remote];
92 [remoteArray addObject:remote];
97 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
98 // if ( [remoteArray count] > 1 ) {
99 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
101 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
104 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
105 return [remoteArray objectAtIndex:0];
109 /*************************************************************************/
111 #pragma mark INSTANCE METHODS
112 /*************************************************************************/
114 - (void)registerDefaultsIfNeeded
116 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
117 if (![defaults objectForKey:@"menu"]) {
119 NSMutableDictionary *loginwindow;
120 NSMutableArray *loginarray;
124 [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:@"<separator>"]) {
292 [menu addItem:[NSMenuItem separatorItem]];
296 [self timerUpdate]; //Updates dynamic info in the menu
302 //Updates the menu with current player state, song, and upcoming songs
305 NSMenuItem *menuItem;
306 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
312 if (upcomingSongsItem) {
313 [self rebuildUpcomingSongsMenu];
317 [self rebuildPlaylistMenu];
321 [self rebuildEQPresetsMenu];
324 if (trackInfoIndex > -1) {
325 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
326 curSongName = [currentRemote currentSongTitle];
328 if ([defaults boolForKey:@"showAlbum"]) {
329 curAlbumName = [currentRemote currentSongAlbum];
332 if ([defaults boolForKey:@"showArtist"]) {
333 curArtistName = [currentRemote currentSongArtist];
336 if ([curSongName length] > 0) {
337 int index = [menu indexOfItemWithTitle:@"Now Playing"];
339 if ([defaults boolForKey:@"showName"]) {
340 [menu removeItemAtIndex:index + 1];
342 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
343 [menu removeItemAtIndex:index + 1];
345 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
346 [menu removeItemAtIndex:index + 1];
348 if ([defaults boolForKey:@"showTime"]) {
349 [menu removeItemAtIndex:index + 1];
353 if (!isPlayingRadio) {
354 if ([defaults boolForKey:@"showTime"]) {
355 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
358 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
362 if ([curArtistName length] > 0) {
363 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
366 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
370 if ([curAlbumName length] > 0) {
371 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
374 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
379 if ([defaults boolForKey:@"showName"]) {
380 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
383 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
388 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
389 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
390 [menu insertItem:menuItem atIndex:trackInfoIndex];
393 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
394 [menu removeItemAtIndex:trackInfoIndex];
396 if ([defaults boolForKey:@"showName"] == YES) {
397 [menu removeItemAtIndex:trackInfoIndex];
400 if ([defaults boolForKey:@"showTime"] == YES) {
401 [menu removeItemAtIndex:trackInfoIndex];
404 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
405 [menu removeItemAtIndex:trackInfoIndex];
408 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
409 [menu removeItemAtIndex:trackInfoIndex];
412 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
413 [menu insertItem:menuItem atIndex:trackInfoIndex];
417 if ([defaults boolForKey:@"showArtist"]) {
418 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
421 if ([defaults boolForKey:@"showAlbum"]) {
422 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
427 //Rebuild the upcoming songs submenu. Can be improved a lot.
428 - (void)rebuildUpcomingSongsMenu
430 int curIndex = [currentRemote currentPlaylistIndex];
431 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
432 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
434 if (!isPlayingRadio) {
436 int curTrack = [currentRemote currentSongIndex];
439 [upcomingSongsMenu release];
440 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
441 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
442 [upcomingSongsItem setEnabled:YES];
444 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
446 NSString *curSong = [currentRemote songTitleAtIndex:i];
447 NSMenuItem *songItem;
448 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
449 [songItem setTarget:self];
450 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
451 [upcomingSongsMenu addItem:songItem];
459 [upcomingSongsItem setSubmenu:nil];
460 [upcomingSongsItem setEnabled:NO];
464 - (void)rebuildPlaylistMenu
466 NSArray *playlists = [currentRemote playlists];
467 int i, curPlaylist = [currentRemote currentPlaylistIndex];
469 if (isPlayingRadio) {
472 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
475 [playlistMenu release];
476 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
478 for (i = 0; i < [playlists count]; i++) {
479 NSString *playlistName = [playlists objectAtIndex:i];
480 NSMenuItem *tempItem;
481 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
482 [tempItem setTarget:self];
483 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
484 [playlistMenu addItem:tempItem];
487 [playlistItem setSubmenu:playlistMenu];
488 [playlistItem setEnabled:YES];
491 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
495 //Build a menu with the list of all available EQ presets
496 - (void)rebuildEQPresetsMenu
498 NSArray *eqPresets = [currentRemote eqPresets];
501 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
505 eqMenu = [[NSMenu alloc] initWithTitle:@""];
507 for (i = 0; i < [eqPresets count]; i++) {
508 NSString *setName = [eqPresets objectAtIndex:i];
509 NSMenuItem *tempItem;
511 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
512 [tempItem setTarget:self];
513 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
514 [eqMenu addItem:tempItem];
518 [eqItem setSubmenu:eqMenu];
520 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
525 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
526 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
527 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
528 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
529 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
534 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
536 if ([defaults objectForKey:@"PlayPause"] != nil) {
537 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
538 combo:[defaults keyComboForKey:@"PlayPause"]
539 target:self action:@selector(playPause:)];
542 if ([defaults objectForKey:@"NextTrack"] != nil) {
543 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
544 combo:[defaults keyComboForKey:@"NextTrack"]
545 target:self action:@selector(nextSong:)];
548 if ([defaults objectForKey:@"PrevTrack"] != nil) {
549 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
550 combo:[defaults keyComboForKey:@"PrevTrack"]
551 target:self action:@selector(prevSong:)];
554 if ([defaults objectForKey:@"TrackInfo"] != nil) {
555 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
556 combo:[defaults keyComboForKey:@"TrackInfo"]
557 target:self action:@selector(showCurrentTrackInfo)];
560 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
561 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
562 combo:[defaults keyComboForKey:@"UpcomingSongs"]
563 target:self action:@selector(showUpcomingSongs)];
567 //Called when the timer fires.
570 int playlist = [currentRemote currentPlaylistIndex];
571 PlayerState playerState = [currentRemote playerState];
573 if ((playlist > 0) || playerState != stopped) {
574 int trackPlayingIndex = [currentRemote currentSongIndex];
576 if (trackPlayingIndex != lastSongIndex) {
577 BOOL wasPlayingRadio = isPlayingRadio;
578 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
580 if (isPlayingRadio && !wasPlayingRadio) {
582 for (i = 0; i < [playlistMenu numberOfItems]; i++)
584 [[playlistMenu itemAtIndex:i] setState:NSOffState];
587 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
590 if (wasPlayingRadio) {
591 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
592 [menu insertItem:temp atIndex:trackInfoIndex + 1];
597 lastSongIndex = trackPlayingIndex;
599 if (playlist != lastPlaylistIndex) {
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++) {
606 [[playlistMenu itemAtIndex:i] setState:NSOffState];
610 if (wasPlayingRadio) {
611 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
612 [menu insertItem:temp atIndex:trackInfoIndex + 1];
616 if (!isPlayingRadio) {
618 for (i = 0; i < [playlistMenu numberOfItems]; i++)
620 [[playlistMenu itemAtIndex:i] setState:NSOffState];
622 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
626 lastSongIndex = trackPlayingIndex;
627 lastPlaylistIndex = playlist;
630 //Update Play/Pause menu item
631 if (playPauseMenuItem){
632 if (playerState == playing) {
633 [playPauseMenuItem setTitle:@"Pause"];
635 [playPauseMenuItem setTitle:@"Play"];
641 - (void)iTunesLaunched:(NSNotification *)note
646 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
648 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
649 [self rebuildPlaylistMenu];
650 [statusItem setMenu:menu]; //Set the menu back to the main one
653 - (void)iTunesTerminated:(NSNotification *)note
658 menu = [[NSMenu alloc] initWithTitle:@""];
659 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
660 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
661 [menu addItem:[NSMenuItem separatorItem]];
662 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
663 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
664 [statusItem setMenu:menu];
666 [refreshTimer invalidate];
673 // Selectors - called from status item menu
677 // Plugin dependent selectors
679 - (void)playTrack:(id)sender
681 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
685 - (void)selectPlaylist:(id)sender
687 int playlist = [[sender representedObject] intValue];
688 if (!isPlayingRadio) {
689 int curPlaylist = [currentRemote currentPlaylistIndex];
690 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
692 [currentRemote switchToPlaylistAtIndex:playlist];
693 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
697 - (void)selectEQPreset:(id)sender
699 int curSet = [currentRemote currentEQPresetIndex];
700 int item = [[sender representedObject] intValue];
701 [currentRemote switchToEQAtIndex:item];
702 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
703 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
706 - (void)playPause:(id)sender
708 PlayerState state = [currentRemote playerState];
710 if (state == playing) {
711 [currentRemote pause];
712 [playPauseMenuItem setTitle:@"Play"];
713 } else if ((state == forwarding) || (state == rewinding)) {
714 [currentRemote pause];
715 [currentRemote play];
717 [currentRemote play];
718 [playPauseMenuItem setTitle:@"Pause"];
722 - (void)nextSong:(id)sender
724 [currentRemote goToNextSong];
727 - (void)prevSong:(id)sender
729 [currentRemote goToPreviousSong];
732 - (void)fastForward:(id)sender
734 [currentRemote fastForward];
735 [playPauseMenuItem setTitle:@"Play"];
738 - (void)rewind:(id)sender
740 [currentRemote rewind];
741 [playPauseMenuItem setTitle:@"Play"];
746 // Plugin independent selectors
749 - (void)quitMenuTunes:(id)sender
751 [NSApp terminate:self];
754 - (void)showPreferences:(id)sender
756 if (!prefsController) {
757 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
762 - (void)closePreferences
767 [prefsController release];
768 prefsController = nil;
773 // Show Current Track Info And Show Upcoming Songs Floaters
777 - (void)showCurrentTrackInfo
779 NSString *trackName = [currentRemote currentSongTitle];
780 if (!statusWindow && [trackName length]) {
781 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
782 NSString *stringToShow = @"";
784 if ([defaults boolForKey:@"showName"]) {
785 if ([defaults boolForKey:@"showArtist"]) {
786 NSString *trackArtist = [currentRemote currentSongArtist];
787 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
789 stringToShow = [stringToShow stringByAppendingString:trackName];
790 stringToShow = [stringToShow stringByAppendingString:@"\n"];
793 if ([defaults boolForKey:@"showAlbum"]) {
794 NSString *trackAlbum = [currentRemote currentSongAlbum];
795 if ([trackAlbum length]) {
796 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
797 stringToShow = [stringToShow stringByAppendingString:@"\n"];
801 if ([defaults boolForKey:@"showTime"]) {
802 NSString *trackTime = [currentRemote currentSongLength];
803 if ([trackTime length]) {
804 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
809 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
810 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
812 stringToShow = [stringToShow stringByAppendingString:
813 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
815 stringToShow = [stringToShow stringByAppendingString:
816 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
820 [statusWindow setText:stringToShow];
821 [NSTimer scheduledTimerWithTimeInterval:3.0
823 selector:@selector(fadeAndCloseStatusWindow)
829 - (void)showUpcomingSongs
831 int curPlaylist = [currentRemote currentPlaylistIndex];
833 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
836 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
837 int curTrack = [currentRemote currentSongIndex];
839 NSString *songs = @"";
841 statusWindow = [ITTransientStatusWindow sharedWindow];
842 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
844 NSString *curSong = [currentRemote songTitleAtIndex:i];
845 songs = [songs stringByAppendingString:curSong];
846 songs = [songs stringByAppendingString:@"\n"];
849 [statusWindow setText:songs];
850 [NSTimer scheduledTimerWithTimeInterval:3.0
852 selector:@selector(fadeAndCloseStatusWindow)
859 - (void)fadeAndCloseStatusWindow
861 [statusWindow orderOut:self];
864 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
865 onItem:(NSMenuItem *)item
867 unichar charcode = 'a';
869 long cocoaModifiers = 0;
870 static long carbonToCocoa[6][2] =
872 { cmdKey, NSCommandKeyMask },
873 { optionKey, NSAlternateKeyMask },
874 { controlKey, NSControlKeyMask },
875 { shiftKey, NSShiftKeyMask },
878 for (i = 0; i < 6; i++) {
879 if (modifiers & carbonToCocoa[i][0]) {
880 cocoaModifiers += carbonToCocoa[i][1];
883 [item setKeyEquivalentModifierMask:cocoaModifiers];
885 //Missing key combos for some keys. Must find them later.
899 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
900 NSLog(@"%@", menuRef);
901 SetMenuItemCommandKey(menuRef, 0, NO, 49);
902 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
903 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
909 charcode = NSDeleteFunctionKey;
925 charcode = NSF5FunctionKey;
929 charcode = NSF6FunctionKey;
933 charcode = NSF7FunctionKey;
937 charcode = NSF3FunctionKey;
941 charcode = NSF8FunctionKey;
945 charcode = NSF9FunctionKey;
949 charcode = NSF11FunctionKey;
953 charcode = NSF3FunctionKey;
957 charcode = NSF14FunctionKey;
961 charcode = NSF10FunctionKey;
965 charcode = NSF12FunctionKey;
969 charcode = NSF13FunctionKey;
973 charcode = NSInsertFunctionKey;
977 charcode = NSHomeFunctionKey;
981 charcode = NSPageUpFunctionKey;
985 charcode = NSDeleteFunctionKey;
989 charcode = NSF4FunctionKey;
993 charcode = NSEndFunctionKey;
997 charcode = NSF2FunctionKey;
1001 charcode = NSPageDownFunctionKey;
1005 charcode = NSF1FunctionKey;
1009 charcode = NSLeftArrowFunctionKey;
1013 charcode = NSRightArrowFunctionKey;
1017 charcode = NSDownArrowFunctionKey;
1021 charcode = NSUpArrowFunctionKey;
1025 if (charcode == 'a') {
1026 unsigned long state;
1031 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1032 keyTrans = KeyTranslate(kchr, code, &state);
1033 charCode = keyTrans;
1034 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1035 } else if (charcode != 'b') {
1036 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1040 /*************************************************************************/
1042 #pragma mark NSApplication DELEGATE METHODS
1043 /*************************************************************************/
1045 - (void)applicationWillTerminate:(NSNotification *)note
1047 [self clearHotKeys];
1048 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1052 /*************************************************************************/
1054 #pragma mark DEALLOCATION METHODS
1055 /*************************************************************************/
1060 [refreshTimer invalidate];
1063 [currentRemote halt];
1064 [statusItem release];