1 #import "MainController.h"
2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
6 @interface MainController(Private)
7 - (ITMTRemote *)loadRemote;
8 - (void)rebuildUpcomingSongsMenu;
9 - (void)rebuildPlaylistMenu;
10 - (void)rebuildEQPresetsMenu;
11 - (void)updateRatingMenu;
15 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
16 onItem:(NSMenuItem *)item;
20 @implementation MainController
22 /*************************************************************************/
24 #pragma mark INITIALIZATION/DEALLOCATION METHODS
25 /*************************************************************************/
29 if ( ( self = [super init] ) ) {
30 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
31 statusWindow = [StatusWindow sharedWindow];
39 [refreshTimer invalidate];
40 [refreshTimer release];
49 - (void)applicationDidFinishLaunching:(NSNotification *)note
51 currentRemote = [self loadRemote];
52 [currentRemote begin];
54 //Setup for notification of the remote player launching or quitting
55 [[[NSWorkspace sharedWorkspace] notificationCenter]
57 selector:@selector(applicationTerminated:)
58 name:NSWorkspaceDidTerminateApplicationNotification
61 [[[NSWorkspace sharedWorkspace] notificationCenter]
63 selector:@selector(applicationLaunched:)
64 name:NSWorkspaceDidLaunchApplicationNotification
67 [self registerDefaults];
69 statusItem = [[ITStatusItem alloc]
70 initWithStatusBar:[NSStatusBar systemStatusBar]
71 withLength:NSSquareStatusItemLength];
73 menu = [[NSMenu alloc] initWithTitle:@""];
74 if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) {
75 [self applicationLaunched:nil];
77 [self applicationTerminated:nil];
80 [statusItem setImage:[NSImage imageNamed:@"menu"]];
81 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
82 // Below line of code is for creating builds for Beta Testers
83 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
86 - (void)applicationWillTerminate:(NSNotification *)note
89 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
92 - (ITMTRemote *)loadRemote
94 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
97 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
98 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
101 while ( (bundlePath = [enumerator nextObject]) ) {
102 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
105 Class remoteClass = [remoteBundle principalClass];
107 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
108 [remoteClass isKindOfClass:[NSObject class]]) {
110 id remote = [remoteClass remote];
111 [remoteArray addObject:remote];
116 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
117 // if ( [remoteArray count] > 1 ) {
118 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
120 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
123 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
124 return [remoteArray objectAtIndex:0];
130 - (void)applicationLaunched:(NSNotification *)note
132 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
133 [NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
136 [statusItem setMenu:menu];
138 isAppRunning = ITMTRemotePlayerRunning;
142 isAppRunning = ITMTRemotePlayerRunning;
145 - (void)applicationTerminated:(NSNotification *)note
147 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
148 NSMenu *notRunningMenu = [[NSMenu alloc] initWithTitle:@""];
149 [[notRunningMenu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self];
150 [notRunningMenu addItem:[NSMenuItem separatorItem]];
151 [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
152 [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
153 [statusItem setMenu:[notRunningMenu autorelease]];
155 [refreshTimer invalidate];
156 [refreshTimer release];
164 /*************************************************************************/
166 #pragma mark INSTANCE METHODS
167 /*************************************************************************/
169 - (void)registerDefaults
171 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
172 if (![defaults objectForKey:@"menu"]) {
174 NSMutableDictionary *loginwindow;
175 NSMutableArray *loginarray;
179 [NSArray arrayWithObjects:
193 @"Current Track Info",
194 nil] forKey:@"menu"];
196 [defaults synchronize];
197 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
198 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
200 for (i = 0; i < [loginarray count]; i++) {
201 NSDictionary *tempDict = [loginarray objectAtIndex:i];
202 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:
203 [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
210 //We must fix it so it is no longer suxy
212 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
213 AEDesc scriptDesc, resultDesc;
214 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]];
215 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
217 AECreateDesc(typeChar, [script cString], [script cStringLength],
220 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
222 AEDisposeDesc(&scriptDesc);
223 AEDisposeDesc(&resultDesc);
225 CloseComponent(asComponent);
230 if (![defaults integerForKey:@"SongsInAdvance"])
232 [defaults setInteger:5 forKey:@"SongsInAdvance"];
235 if (![defaults objectForKey:@"showName"]) {
236 [defaults setBool:YES forKey:@"showName"];
239 if (![defaults objectForKey:@"showArtist"]) {
240 [defaults setBool:YES forKey:@"showArtist"];
243 if (![defaults objectForKey:@"showAlbum"]) {
244 [defaults setBool:NO forKey:@"showAlbum"];
247 if (![defaults objectForKey:@"showTime"]) {
248 [defaults setBool:NO forKey:@"showTime"];
252 - (void)startTimerInNewThread
254 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
255 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
256 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
258 selector:@selector(timerUpdate)
260 repeats:YES] retain];
265 //Recreate the status item menu
268 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
272 lastPlaylistIndex = -1;
273 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
274 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
277 menu = [[NSMenu alloc] initWithTitle:@""];
279 /*while ([menu numberOfItems] > 0) {
280 [menu removeItemAtIndex:0];
284 lastSongIdentifier = @"0-0";
286 upcomingSongsItem = nil;
287 [upcomingSongsMenu release];
288 upcomingSongsMenu = nil;
291 [ratingMenu release];
295 [playlistMenu release];
302 for (i = 0; i < [myMenu count]; i++) {
303 NSString *item = [myMenu objectAtIndex:i];
304 if ([item isEqualToString:@"Play/Pause"]) {
305 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
306 playPauseItem = [menu addItemWithTitle:@"Play"
307 action:@selector(playPause:)
311 [self setKeyEquivalentForCode:[tempCombo keyCode]
312 andModifiers:[tempCombo modifiers] onItem:playPauseItem];
315 } else if ([item isEqualToString:@"Next Track"]) {
316 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
317 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
318 action:@selector(nextSong:)
322 [self setKeyEquivalentForCode:[tempCombo keyCode]
323 andModifiers:[tempCombo modifiers] onItem:nextTrack];
326 } else if ([item isEqualToString:@"Previous Track"]) {
327 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
328 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
329 action:@selector(prevSong:)
333 [self setKeyEquivalentForCode:[tempCombo keyCode]
334 andModifiers:[tempCombo modifiers] onItem:prevTrack];
337 } else if ([item isEqualToString:@"Fast Forward"]) {
338 [menu addItemWithTitle:@"Fast Forward"
339 action:@selector(fastForward:)
341 } else if ([item isEqualToString:@"Rewind"]) {
342 [menu addItemWithTitle:@"Rewind"
343 action:@selector(rewind:)
345 } else if ([item isEqualToString:@"Upcoming Songs"]) {
346 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
349 } else if ([item isEqualToString:@"Playlists"]) {
350 playlistItem = [menu addItemWithTitle:@"Playlists"
353 } else if ([item isEqualToString:@"EQ Presets"]) {
354 eqItem = [menu addItemWithTitle:@"EQ Presets"
357 } else if ([item isEqualToString:@"PreferencesÉ"]) {
358 [menu addItemWithTitle:@"PreferencesÉ"
359 action:@selector(showPreferences:)
361 } else if ([item isEqualToString:@"Quit"]) {
362 [menu addItemWithTitle:@"Quit"
363 action:@selector(quitMenuTunes:)
365 } else if ([item isEqualToString:@"Current Track Info"]) {
366 trackInfoIndex = [menu numberOfItems];
367 [menu addItemWithTitle:@"No Song"
370 } else if ([item isEqualToString:@"Song Rating"]) {
371 unichar fullstar = 0x2605;
372 unichar emptystar = 0x2606;
373 NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1];
374 NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1];
377 ratingItem = [menu addItemWithTitle:@"Song Rating"
381 ratingMenu = [[NSMenu alloc] initWithTitle:@""];
383 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
386 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
389 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
392 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
395 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
398 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
400 } else if ([item isEqualToString:@"<separator>"]) {
401 [menu addItem:[NSMenuItem separatorItem]];
405 [statusItem setMenu:menu];
413 //Rebuild the upcoming songs submenu. Can be improved a lot.
414 - (void)rebuildUpcomingSongsMenu
416 int curIndex = [currentRemote currentPlaylistIndex];
417 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
418 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
420 if (!isPlayingRadio) {
422 int curTrack = [currentRemote currentSongIndex];
425 [upcomingSongsMenu release];
426 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
427 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
428 [upcomingSongsItem setEnabled:YES];
430 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
432 NSString *curSong = [currentRemote songTitleAtIndex:i];
433 NSMenuItem *songItem;
434 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(selectSong:) keyEquivalent:@""];
435 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
436 [upcomingSongsMenu addItem:songItem];
444 [upcomingSongsItem setSubmenu:nil];
445 [upcomingSongsItem setEnabled:NO];
449 - (void)rebuildPlaylistMenu
451 NSArray *playlists = [currentRemote playlists];
452 int i, currentPlaylist = [currentRemote currentPlaylistIndex];
454 [playlistMenu release];
455 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
457 for (i = 0; i < [playlists count]; i++) {
458 NSString *playlistName = [playlists objectAtIndex:i];
459 NSMenuItem *tempItem;
460 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
461 [tempItem setTag:i + 1];
462 [playlistMenu addItem:tempItem];
465 [playlistItem setSubmenu:playlistMenu];
466 [playlistItem setEnabled:YES];
468 if (!isPlayingRadio && currentPlaylist) {
469 [[playlistMenu itemAtIndex:currentPlaylist - 1] setState:NSOnState];
473 //Build a menu with the list of all available EQ presets
474 - (void)rebuildEQPresetsMenu
476 NSArray *eqPresets = [currentRemote eqPresets];
477 NSMenuItem *enabledItem;
481 eqMenu = [[NSMenu alloc] initWithTitle:@""];
483 enabledItem = [eqMenu addItemWithTitle:@"Enabled"
484 action:@selector(selectEQPreset:)
486 [enabledItem setTag:-1];
488 if ([currentRemote equalizerEnabled]) {
489 [enabledItem setState:NSOnState];
492 [eqMenu addItem:[NSMenuItem separatorItem]];
494 for (i = 0; i < [eqPresets count]; i++) {
495 NSString *name = [eqPresets objectAtIndex:i];
496 NSMenuItem *tempItem;
498 tempItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(selectEQPreset:) keyEquivalent:@""];
500 [eqMenu addItem:tempItem];
504 [eqItem setSubmenu:eqMenu];
505 [eqItem setEnabled:YES];
507 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
510 - (void)updateRatingMenu
512 int currentSongRating = ([currentRemote currentSongRating] * 5);
513 if ([currentRemote currentPlaylistIndex] && (currentSongRating != lastSongRating)) {
514 if ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist) {
517 [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
518 lastSongRating = currentSongRating;
519 [[ratingMenu itemAtIndex:lastSongRating] setState:NSOnState];
525 NSString *currentIdentifier = [currentRemote currentSongUniqueIdentifier];
526 if (![lastSongIdentifier isEqualToString:currentIdentifier] ||
527 (!isPlayingRadio && ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist))) {
531 [self updateRatingMenu];
533 //Update Play/Pause menu item
535 if ([currentRemote playerPlayingState] == ITMTRemotePlayerPlaying) {
536 [playPauseItem setTitle:@"Pause"];
538 [playPauseItem setTitle:@"Play"];
545 NSUserDefaults *defaults;
546 int playlist = [currentRemote currentPlaylistIndex];
549 if ( (isAppRunning == ITMTRemotePlayerNotRunning) ) {
553 defaults = [NSUserDefaults standardUserDefaults];
554 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
556 if (upcomingSongsItem) {
557 [self rebuildUpcomingSongsMenu];
561 [self rebuildPlaylistMenu];
565 [self rebuildEQPresetsMenu];
569 if (isPlayingRadio || !playlist) {
570 [ratingItem setEnabled:NO];
571 if ([ratingItem submenu]) {
572 [ratingItem setSubmenu:nil];
575 int currentSongRating = ([currentRemote currentSongRating] * 5);
576 [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
577 lastSongRating = currentSongRating;
578 [[ratingMenu itemAtIndex:lastSongRating] setState:NSOnState];
579 [ratingItem setEnabled:YES];
580 [ratingItem setSubmenu:ratingMenu];
584 //Set the new unique song identifier
585 lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
587 //If we're in a playlist or radio mode
588 if ( (trackInfoIndex > -1) && (playlist || isPlayingRadio) ) {
589 NSString *title, *album, *artist;
591 if ( (temp = [menu indexOfItemWithTitle:@"No Song"]) && (temp > -1) ) {
592 [menu removeItemAtIndex:temp];
593 [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:temp];
596 title = [currentRemote currentSongTitle];
598 if (!isPlayingRadio) {
599 ([defaults boolForKey:@"showAlbum"]) ? (album = [currentRemote currentSongAlbum]) :
601 ([defaults boolForKey:@"showArtist"]) ? (artist = [currentRemote currentSongArtist]) :
603 if ([defaults boolForKey:@"showTime"]) {
604 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
607 if ([artist length] > 0) {
608 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
611 if ([album length] > 0) {
612 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
615 if ([defaults boolForKey:@"showArtist"]) {
616 didHaveArtistName = (([artist length] > 0) ? YES : NO);
619 if ([defaults boolForKey:@"showAlbum"]) {
620 didHaveAlbumName = (([album length] > 0) ? YES : NO);
624 if ([title length] > 0) {
625 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", title] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
638 - (void)selectSong:(id)sender
640 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
643 - (void)selectPlaylist:(id)sender
645 int playlist = [sender tag];
646 [currentRemote switchToPlaylistAtIndex:playlist];
649 - (void)selectEQPreset:(id)sender
651 int curSet = [currentRemote currentEQPresetIndex];
652 int item = [sender tag];
655 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
657 [currentRemote setEqualizerEnabled:YES];
658 [currentRemote switchToEQAtIndex:item];
659 [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
660 [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
664 - (void)selectSongRating:(id)sender
666 int newRating = [sender tag];
667 [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
668 [sender setState:NSOnState];
669 [currentRemote setCurrentSongRating:(float)newRating / 100.0];
670 lastSongRating = newRating / 20;
673 - (void)playPause:(id)sender
675 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
677 if (state == ITMTRemotePlayerPlaying) {
678 [currentRemote pause];
679 [playPauseItem setTitle:@"Play"];
680 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
681 [currentRemote pause];
682 [currentRemote play];
684 [currentRemote play];
685 [playPauseItem setTitle:@"Pause"];
689 - (void)nextSong:(id)sender
691 [currentRemote goToNextSong];
694 - (void)prevSong:(id)sender
696 [currentRemote goToPreviousSong];
699 - (void)fastForward:(id)sender
701 [currentRemote forward];
702 [playPauseItem setTitle:@"Play"];
705 - (void)rewind:(id)sender
707 [currentRemote rewind];
708 [playPauseItem setTitle:@"Play"];
713 - (void)quitMenuTunes:(id)sender
715 [NSApp terminate:self];
718 - (void)showPlayer:(id)sender
720 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
721 [currentRemote showPrimaryInterface];
723 if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
724 NSLog(@"Error Launching Player");
729 - (void)showPreferences:(id)sender
731 [[PreferencesController sharedPrefs] setController:self];
732 [[PreferencesController sharedPrefs] showPrefsWindow:self];
735 - (void)closePreferences
737 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
751 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
752 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
753 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
754 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
755 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
760 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
762 if ([defaults objectForKey:@"PlayPause"] != nil) {
763 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
764 combo:[defaults keyComboForKey:@"PlayPause"]
765 target:self action:@selector(playPause:)];
768 if ([defaults objectForKey:@"NextTrack"] != nil) {
769 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
770 combo:[defaults keyComboForKey:@"NextTrack"]
771 target:self action:@selector(nextSong:)];
774 if ([defaults objectForKey:@"PrevTrack"] != nil) {
775 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
776 combo:[defaults keyComboForKey:@"PrevTrack"]
777 target:self action:@selector(prevSong:)];
780 if ([defaults objectForKey:@"TrackInfo"] != nil) {
781 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
782 combo:[defaults keyComboForKey:@"TrackInfo"]
783 target:self action:@selector(showCurrentTrackInfo)];
786 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
787 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
788 combo:[defaults keyComboForKey:@"UpcomingSongs"]
789 target:self action:@selector(showUpcomingSongs)];
795 // Show Current Track Info And Show Upcoming Songs Floaters
799 - (void)showCurrentTrackInfo
801 NSString *trackName = [currentRemote currentSongTitle];
802 if (!statusWindow && [trackName length]) {
803 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
804 NSString *stringToShow = @"";
806 if ([defaults boolForKey:@"showName"]) {
807 if ([defaults boolForKey:@"showArtist"]) {
808 NSString *trackArtist = [currentRemote currentSongArtist];
809 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
811 stringToShow = [stringToShow stringByAppendingString:trackName];
812 stringToShow = [stringToShow stringByAppendingString:@"\n"];
815 if ([defaults boolForKey:@"showAlbum"]) {
816 NSString *trackAlbum = [currentRemote currentSongAlbum];
817 if ([trackAlbum length]) {
818 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
819 stringToShow = [stringToShow stringByAppendingString:@"\n"];
823 if ([defaults boolForKey:@"showTime"]) {
824 NSString *trackTime = [currentRemote currentSongLength];
825 if ([trackTime length]) {
826 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
831 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
832 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
834 stringToShow = [stringToShow stringByAppendingString:
835 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
837 stringToShow = [stringToShow stringByAppendingString:
838 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
842 [statusWindow setText:stringToShow];
843 [NSTimer scheduledTimerWithTimeInterval:3.0
845 selector:@selector(fadeAndCloseStatusWindow)
851 - (void)showUpcomingSongs
853 int curPlaylist = [currentRemote currentPlaylistIndex];
855 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
858 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
859 int curTrack = [currentRemote currentSongIndex];
861 NSString *songs = @"";
863 statusWindow = [ITTransientStatusWindow sharedWindow];
864 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
866 NSString *curSong = [currentRemote songTitleAtIndex:i];
867 songs = [songs stringByAppendingString:curSong];
868 songs = [songs stringByAppendingString:@"\n"];
871 [statusWindow setText:songs];
872 [NSTimer scheduledTimerWithTimeInterval:3.0
874 selector:@selector(fadeAndCloseStatusWindow)
881 - (void)fadeAndCloseStatusWindow
883 [statusWindow orderOut:self];
886 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
887 onItem:(NSMenuItem *)item
889 unichar charcode = 'a';
891 long cocoaModifiers = 0;
892 static long carbonToCocoa[6][2] =
894 { cmdKey, NSCommandKeyMask },
895 { optionKey, NSAlternateKeyMask },
896 { controlKey, NSControlKeyMask },
897 { shiftKey, NSShiftKeyMask },
900 for (i = 0; i < 6; i++) {
901 if (modifiers & carbonToCocoa[i][0]) {
902 cocoaModifiers += carbonToCocoa[i][1];
905 [item setKeyEquivalentModifierMask:cocoaModifiers];
907 //Missing key combos for some keys. Must find them later.
921 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
922 NSLog(@"%@", menuRef);
923 SetMenuItemCommandKey(menuRef, 0, NO, 49);
924 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
925 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
931 charcode = NSDeleteFunctionKey;
947 charcode = NSF5FunctionKey;
951 charcode = NSF6FunctionKey;
955 charcode = NSF7FunctionKey;
959 charcode = NSF3FunctionKey;
963 charcode = NSF8FunctionKey;
967 charcode = NSF9FunctionKey;
971 charcode = NSF11FunctionKey;
975 charcode = NSF3FunctionKey;
979 charcode = NSF14FunctionKey;
983 charcode = NSF10FunctionKey;
987 charcode = NSF12FunctionKey;
991 charcode = NSF13FunctionKey;
995 charcode = NSInsertFunctionKey;
999 charcode = NSHomeFunctionKey;
1003 charcode = NSPageUpFunctionKey;
1007 charcode = NSDeleteFunctionKey;
1011 charcode = NSF4FunctionKey;
1015 charcode = NSEndFunctionKey;
1019 charcode = NSF2FunctionKey;
1023 charcode = NSPageDownFunctionKey;
1027 charcode = NSF1FunctionKey;
1031 charcode = NSLeftArrowFunctionKey;
1035 charcode = NSRightArrowFunctionKey;
1039 charcode = NSDownArrowFunctionKey;
1043 charcode = NSUpArrowFunctionKey;
1047 if (charcode == 'a') {
1048 unsigned long state;
1053 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1054 keyTrans = KeyTranslate(kchr, code, &state);
1055 charCode = keyTrans;
1056 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1057 } else if (charcode != 'b') {
1058 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];