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 isAppRunning = [currentRemote isAppRunning];
59 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
61 selector:@selector(timerUpdate)
65 menu = [[NSMenu alloc] initWithTitle:@""];
66 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
67 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
68 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
72 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
73 withLength:NSSquareStatusItemLength];
75 [statusItem setImage:[NSImage imageNamed:@"menu"]];
76 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
77 [statusItem setMenu:menu];
78 // Below line of code is for creating builds for Beta Testers
79 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
82 - (ITMTRemote *)loadRemote
84 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
87 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
88 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
91 while ( (bundlePath = [enumerator nextObject]) ) {
92 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
95 Class remoteClass = [remoteBundle principalClass];
97 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
98 [remoteClass isKindOfClass:[NSObject class]]) {
100 id remote = [remoteClass remote];
101 [remoteArray addObject:remote];
106 // if ( [remoteArray count] > 0 ) {
107 // if ( [remoteArray count] > 1 ) {
108 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
110 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
113 NSLog(@"%@", [remoteArray objectAtIndex:0]);
114 return [remoteArray objectAtIndex:0];
118 /*************************************************************************/
120 #pragma mark INSTANCE METHODS
121 /*************************************************************************/
123 - (void)registerDefaultsIfNeeded
125 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
126 if (![defaults objectForKey:@"menu"]) {
128 NSMutableDictionary *loginwindow;
129 NSMutableArray *loginarray;
133 [NSArray arrayWithObjects:
146 @"Current Track Info",
147 nil] forKey:@"menu"];
149 [defaults synchronize];
150 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
151 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
153 for (i = 0; i < [loginarray count]; i++) {
154 NSDictionary *tempDict = [loginarray objectAtIndex:i];
155 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
162 //We must fix it so it is no longer suxy
164 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
165 AEDesc scriptDesc, resultDesc;
166 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]];
167 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
169 AECreateDesc(typeChar, [script cString], [script cStringLength],
172 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
174 AEDisposeDesc(&scriptDesc);
175 AEDisposeDesc(&resultDesc);
177 CloseComponent(asComponent);
182 if (![defaults integerForKey:@"SongsInAdvance"])
184 [defaults setInteger:5 forKey:@"SongsInAdvance"];
187 if (![defaults objectForKey:@"showName"]) {
188 [defaults setBool:YES forKey:@"showName"];
191 if (![defaults objectForKey:@"showArtist"]) {
192 [defaults setBool:YES forKey:@"showArtist"];
195 if (![defaults objectForKey:@"showAlbum"]) {
196 [defaults setBool:NO forKey:@"showAlbum"];
199 if (![defaults objectForKey:@"showTime"]) {
200 [defaults setBool:NO forKey:@"showTime"];
204 //Recreate the status item menu
207 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
212 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
213 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
215 while ([menu numberOfItems] > 0) {
216 [menu removeItemAtIndex:0];
219 playPauseMenuItem = nil;
220 upcomingSongsItem = nil;
222 [playlistMenu release];
228 for (i = 0; i < [myMenu count]; i++) {
229 NSString *item = [myMenu objectAtIndex:i];
230 if ([item isEqualToString:@"Play/Pause"]) {
231 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
232 playPauseMenuItem = [menu addItemWithTitle:@"Play"
233 action:@selector(playPause:)
235 [playPauseMenuItem setTarget:self];
238 [self setKeyEquivalentForCode:[tempCombo keyCode]
239 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
242 } else if ([item isEqualToString:@"Next Track"]) {
243 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
244 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
245 action:@selector(nextSong:)
248 [nextTrack setTarget:self];
250 [self setKeyEquivalentForCode:[tempCombo keyCode]
251 andModifiers:[tempCombo modifiers] onItem:nextTrack];
254 } else if ([item isEqualToString:@"Previous Track"]) {
255 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
256 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
257 action:@selector(prevSong:)
260 [prevTrack setTarget:self];
262 [self setKeyEquivalentForCode:[tempCombo keyCode]
263 andModifiers:[tempCombo modifiers] onItem:prevTrack];
266 } else if ([item isEqualToString:@"Fast Forward"]) {
267 [[menu addItemWithTitle:@"Fast Forward"
268 action:@selector(fastForward:)
269 keyEquivalent:@""] setTarget:self];
270 } else if ([item isEqualToString:@"Rewind"]) {
271 [[menu addItemWithTitle:@"Rewind"
272 action:@selector(rewind:)
273 keyEquivalent:@""] setTarget:self];
274 } else if ([item isEqualToString:@"Upcoming Songs"]) {
275 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
278 } else if ([item isEqualToString:@"Playlists"]) {
279 playlistItem = [menu addItemWithTitle:@"Playlists"
282 } else if ([item isEqualToString:@"EQ Presets"]) {
283 eqItem = [menu addItemWithTitle:@"EQ Presets"
286 } else if ([item isEqualToString:@"PreferencesÉ"]) {
287 [[menu addItemWithTitle:@"PreferencesÉ"
288 action:@selector(showPreferences:)
289 keyEquivalent:@""] setTarget:self];
290 } else if ([item isEqualToString:@"Quit"]) {
291 [[menu addItemWithTitle:@"Quit"
292 action:@selector(quitMenuTunes:)
293 keyEquivalent:@""] setTarget:self];
294 } else if ([item isEqualToString:@"Current Track Info"]) {
295 trackInfoIndex = [menu numberOfItems];
296 [menu addItemWithTitle:@"No Song"
299 } else if ([item isEqualToString:@"<separator>"]) {
300 [menu addItem:[NSMenuItem separatorItem]];
304 [self timerUpdate]; //Updates dynamic info in the menu
310 //Updates the menu with current player state, song, and upcoming songs
313 NSMenuItem *menuItem;
314 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
320 if (upcomingSongsItem) {
321 [self rebuildUpcomingSongsMenu];
325 [self rebuildPlaylistMenu];
329 [self rebuildEQPresetsMenu];
332 if (trackInfoIndex > -1) {
333 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
334 curSongName = [currentRemote currentSongTitle];
336 if ([defaults boolForKey:@"showAlbum"]) {
337 curAlbumName = [currentRemote currentSongAlbum];
340 if ([defaults boolForKey:@"showArtist"]) {
341 curArtistName = [currentRemote currentSongArtist];
344 if ([curSongName length] > 0) {
345 int index = [menu indexOfItemWithTitle:@"Now Playing"];
347 if ([defaults boolForKey:@"showName"]) {
348 [menu removeItemAtIndex:index + 1];
350 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
351 [menu removeItemAtIndex:index + 1];
353 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
354 [menu removeItemAtIndex:index + 1];
356 if ([defaults boolForKey:@"showTime"]) {
357 [menu removeItemAtIndex:index + 1];
361 if (!isPlayingRadio) {
362 if ([defaults boolForKey:@"showTime"]) {
363 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
366 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
370 if ([curArtistName length] > 0) {
371 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
374 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
378 if ([curAlbumName length] > 0) {
379 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
382 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
387 if ([defaults boolForKey:@"showName"]) {
388 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
391 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
396 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
397 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
398 [menu insertItem:menuItem atIndex:trackInfoIndex];
401 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
402 [menu removeItemAtIndex:trackInfoIndex];
404 if ([defaults boolForKey:@"showName"] == YES) {
405 [menu removeItemAtIndex:trackInfoIndex];
408 if ([defaults boolForKey:@"showTime"] == YES) {
409 [menu removeItemAtIndex:trackInfoIndex];
412 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
413 [menu removeItemAtIndex:trackInfoIndex];
416 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
417 [menu removeItemAtIndex:trackInfoIndex];
420 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
421 [menu insertItem:menuItem atIndex:trackInfoIndex];
425 if ([defaults boolForKey:@"showArtist"]) {
426 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
429 if ([defaults boolForKey:@"showAlbum"]) {
430 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
435 //Rebuild the upcoming songs submenu. Can be improved a lot.
436 - (void)rebuildUpcomingSongsMenu
438 int curIndex = [currentRemote currentPlaylistIndex];
439 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
440 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
442 if (!isPlayingRadio) {
444 int curTrack = [currentRemote currentSongIndex];
447 [upcomingSongsMenu release];
448 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
449 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
450 [upcomingSongsItem setEnabled:YES];
452 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
454 NSString *curSong = [currentRemote songTitleAtIndex:i];
455 NSMenuItem *songItem;
456 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
457 [songItem setTarget:self];
458 [songItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
459 [upcomingSongsMenu addItem:songItem];
467 [upcomingSongsItem setSubmenu:nil];
468 [upcomingSongsItem setEnabled:NO];
472 - (void)rebuildPlaylistMenu
474 NSArray *playlists = [currentRemote playlists];
475 int i, curPlaylist = [currentRemote currentPlaylistIndex];
477 if (isPlayingRadio) {
480 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
483 [playlistMenu release];
484 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
486 for (i = 0; i < [playlists count]; i++) {
487 NSString *playlistName = [playlists objectAtIndex:i];
488 NSMenuItem *tempItem;
489 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
490 [tempItem setTarget:self];
491 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
492 [playlistMenu addItem:tempItem];
495 [playlistItem setSubmenu:playlistMenu];
498 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
502 //Build a menu with the list of all available EQ presets
503 - (void)rebuildEQPresetsMenu
505 NSArray *eqPresets = [currentRemote eqPresets];
508 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
512 eqMenu = [[NSMenu alloc] initWithTitle:@""];
514 for (i = 0; i < [eqPresets count]; i++) {
515 NSString *setName = [eqPresets objectAtIndex:i];
516 NSMenuItem *tempItem;
518 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
519 [tempItem setTarget:self];
520 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
521 [eqMenu addItem:tempItem];
525 [eqItem setSubmenu:eqMenu];
527 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
532 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
533 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
534 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
535 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
536 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
541 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
543 if ([defaults objectForKey:@"PlayPause"] != nil) {
544 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
545 combo:[defaults keyComboForKey:@"PlayPause"]
546 target:self action:@selector(playPause:)];
549 if ([defaults objectForKey:@"NextTrack"] != nil) {
550 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
551 combo:[defaults keyComboForKey:@"NextTrack"]
552 target:self action:@selector(nextSong:)];
555 if ([defaults objectForKey:@"PrevTrack"] != nil) {
556 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
557 combo:[defaults keyComboForKey:@"PrevTrack"]
558 target:self action:@selector(prevSong:)];
561 if ([defaults objectForKey:@"TrackInfo"] != nil) {
562 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
563 combo:[defaults keyComboForKey:@"TrackInfo"]
564 target:self action:@selector(showCurrentTrackInfo)];
567 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
568 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
569 combo:[defaults keyComboForKey:@"UpcomingSongs"]
570 target:self action:@selector(showUpcomingSongs)];
574 //Called when the timer fires.
577 NSLog(@"%i", [currentRemote playerState]);
578 if ([currentRemote playerState] != stopped) {
579 int trackPlayingIndex = [currentRemote currentSongIndex];
580 int playlist = [currentRemote currentPlaylistIndex];
582 if (trackPlayingIndex != lastSongIndex) {
583 BOOL wasPlayingRadio = isPlayingRadio;
584 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
585 if (isPlayingRadio && !wasPlayingRadio) {
587 for (i = 0; i < [playlistMenu numberOfItems]; i++)
589 [[playlistMenu itemAtIndex:i] setState:NSOffState];
592 if (wasPlayingRadio) {
593 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
594 [menu insertItem:temp atIndex:trackInfoIndex + 1];
599 lastSongIndex = trackPlayingIndex;
601 if (playlist != [currentRemote currentPlaylistIndex]) {
602 BOOL wasPlayingRadio = isPlayingRadio;
603 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
604 if (isPlayingRadio && !wasPlayingRadio) {
606 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
607 [[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 lastSongIndex = trackPlayingIndex;
619 //Update Play/Pause menu item
620 if (playPauseMenuItem){
621 if ([currentRemote playerState] == playing) {
622 [playPauseMenuItem setTitle:@"Pause"];
624 [playPauseMenuItem setTitle:@"Play"];
630 - (void)iTunesLaunched:(NSNotification *)note
635 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
637 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
638 [statusItem setMenu:menu]; //Set the menu back to the main one
641 - (void)iTunesTerminated:(NSNotification *)note
646 menu = [[NSMenu alloc] initWithTitle:@""];
647 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
648 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
649 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
650 [statusItem setMenu:menu];
652 [refreshTimer invalidate];
659 // Selectors - called from status item menu
663 // Plugin dependent selectors
665 - (void)playTrack:(id)sender
667 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
671 - (void)selectPlaylist:(id)sender
673 int playlist = [[sender representedObject] intValue];
674 if (!isPlayingRadio) {
675 int curPlaylist = [currentRemote currentPlaylistIndex];
676 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
678 [currentRemote switchToPlaylistAtIndex:playlist];
679 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
683 - (void)selectEQPreset:(id)sender
685 int curSet = [currentRemote currentEQPresetIndex];
686 int item = [[sender representedObject] intValue];
687 [currentRemote switchToEQAtIndex:item];
688 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
689 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
692 - (void)playPause:(id)sender
694 PlayerState state = [currentRemote playerState];
695 if (state == playing) {
696 [currentRemote pause];
697 [playPauseMenuItem setTitle:@"Play"];
698 } else if ((state == forwarding) || (state == rewinding)) {
699 [currentRemote pause];
700 [currentRemote play];
702 [currentRemote play];
703 [playPauseMenuItem setTitle:@"Pause"];
707 - (void)nextSong:(id)sender
709 [currentRemote goToNextSong];
712 - (void)prevSong:(id)sender
714 [currentRemote goToPreviousSong];
717 - (void)fastForward:(id)sender
719 [currentRemote fastForward];
720 [playPauseMenuItem setTitle:@"Play"];
723 - (void)rewind:(id)sender
725 [currentRemote rewind];
726 [playPauseMenuItem setTitle:@"Play"];
731 // Plugin independent selectors
734 - (void)quitMenuTunes:(id)sender
736 [NSApp terminate:self];
739 //How is this going to work, now that we're pluginized?
740 - (void)openiTunes:(id)sender
742 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
745 - (void)showPreferences:(id)sender
747 if (!prefsController) {
748 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
754 - (void)closePreferences
759 [prefsController release];
760 prefsController = nil;
765 // Show Current Track Info And Show Upcoming Songs Floaters
769 - (void)showCurrentTrackInfo
771 NSString *trackName = [currentRemote currentSongTitle];
772 if (!statusWindow && [trackName length]) {
773 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
774 NSString *stringToShow = @"";
776 if ([defaults boolForKey:@"showName"]) {
777 if ([defaults boolForKey:@"showArtist"]) {
778 NSString *trackArtist = [currentRemote currentSongArtist];
779 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
781 stringToShow = [stringToShow stringByAppendingString:trackName];
782 stringToShow = [stringToShow stringByAppendingString:@"\n"];
785 if ([defaults boolForKey:@"showAlbum"]) {
786 NSString *trackAlbum = [currentRemote currentSongAlbum];
787 if ([trackAlbum length]) {
788 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
789 stringToShow = [stringToShow stringByAppendingString:@"\n"];
793 if ([defaults boolForKey:@"showTime"]) {
794 NSString *trackTime = [currentRemote currentSongLength];
795 if ([trackTime length]) {
796 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
801 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
802 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
804 stringToShow = [stringToShow stringByAppendingString:
805 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
807 stringToShow = [stringToShow stringByAppendingString:
808 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
812 [statusWindow setText:stringToShow];
813 [NSTimer scheduledTimerWithTimeInterval:3.0
815 selector:@selector(fadeAndCloseStatusWindow)
821 - (void)showUpcomingSongs
823 int curPlaylist = [currentRemote currentPlaylistIndex];
825 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
828 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
829 int curTrack = [currentRemote currentSongIndex];
831 NSString *songs = @"";
833 statusWindow = [ITTransientStatusWindow sharedWindow];
834 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
836 NSString *curSong = [currentRemote songTitleAtIndex:i];
837 songs = [songs stringByAppendingString:curSong];
838 songs = [songs stringByAppendingString:@"\n"];
841 [statusWindow setText:songs];
842 [NSTimer scheduledTimerWithTimeInterval:3.0
844 selector:@selector(fadeAndCloseStatusWindow)
851 - (void)fadeAndCloseStatusWindow
853 [statusWindow orderOut:self];
856 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
857 onItem:(NSMenuItem *)item
859 unichar charcode = 'a';
861 long cocoaModifiers = 0;
862 static long carbonToCocoa[6][2] =
864 { cmdKey, NSCommandKeyMask },
865 { optionKey, NSAlternateKeyMask },
866 { controlKey, NSControlKeyMask },
867 { shiftKey, NSShiftKeyMask },
870 for (i = 0; i < 6; i++) {
871 if (modifiers & carbonToCocoa[i][0]) {
872 cocoaModifiers += carbonToCocoa[i][1];
875 [item setKeyEquivalentModifierMask:cocoaModifiers];
877 //Missing key combos for some keys. Must find them later.
891 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
892 NSLog(@"%@", menuRef);
893 SetMenuItemCommandKey(menuRef, 0, NO, 49);
894 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
895 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
901 charcode = NSDeleteFunctionKey;
917 charcode = NSF5FunctionKey;
921 charcode = NSF6FunctionKey;
925 charcode = NSF7FunctionKey;
929 charcode = NSF3FunctionKey;
933 charcode = NSF8FunctionKey;
937 charcode = NSF9FunctionKey;
941 charcode = NSF11FunctionKey;
945 charcode = NSF3FunctionKey;
949 charcode = NSF14FunctionKey;
953 charcode = NSF10FunctionKey;
957 charcode = NSF12FunctionKey;
961 charcode = NSF13FunctionKey;
965 charcode = NSInsertFunctionKey;
969 charcode = NSHomeFunctionKey;
973 charcode = NSPageUpFunctionKey;
977 charcode = NSDeleteFunctionKey;
981 charcode = NSF4FunctionKey;
985 charcode = NSEndFunctionKey;
989 charcode = NSF2FunctionKey;
993 charcode = NSPageDownFunctionKey;
997 charcode = NSF1FunctionKey;
1001 charcode = NSLeftArrowFunctionKey;
1005 charcode = NSRightArrowFunctionKey;
1009 charcode = NSDownArrowFunctionKey;
1013 charcode = NSUpArrowFunctionKey;
1017 if (charcode == 'a') {
1018 unsigned long state;
1023 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1024 keyTrans = KeyTranslate(kchr, code, &state);
1025 charCode = keyTrans;
1026 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1027 } else if (charcode != 'b') {
1028 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1032 /*************************************************************************/
1034 #pragma mark NSApplication DELEGATE METHODS
1035 /*************************************************************************/
1037 - (void)applicationWillTerminate:(NSNotification *)note
1039 [self clearHotKeys];
1040 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1044 /*************************************************************************/
1046 #pragma mark DEALLOCATION METHODS
1047 /*************************************************************************/
1052 [refreshTimer invalidate];
1055 [currentRemote halt];
1056 [statusItem release];