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;
14 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
15 onItem:(NSMenuItem *)item;
19 @implementation MainController
21 /*************************************************************************/
23 #pragma mark INITIALIZATION/DEALLOCATION METHODS
24 /*************************************************************************/
28 if ( ( self = [super init] ) ) {
29 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
30 statusWindow = [StatusWindow sharedWindow];
38 [refreshTimer invalidate];
39 [refreshTimer release];
48 - (void)applicationDidFinishLaunching:(NSNotification *)note
50 currentRemote = [self loadRemote];
51 [currentRemote begin];
53 //Setup for notification of the remote player launching or quitting
54 [[[NSWorkspace sharedWorkspace] notificationCenter]
56 selector:@selector(applicationTerminated:)
57 name:NSWorkspaceDidTerminateApplicationNotification
60 [[[NSWorkspace sharedWorkspace] notificationCenter]
62 selector:@selector(applicationLaunched:)
63 name:NSWorkspaceDidLaunchApplicationNotification
66 [self registerDefaults];
68 statusItem = [[ITStatusItem alloc]
69 initWithStatusBar:[NSStatusBar systemStatusBar]
70 withLength:NSSquareStatusItemLength];
72 menu = [[NSMenu alloc] initWithTitle:@""];
73 if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) {
74 [self applicationLaunched:nil];
76 [self applicationTerminated:nil];
79 [statusItem setImage:[NSImage imageNamed:@"menu"]];
80 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
81 // Below line of code is for creating builds for Beta Testers
82 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
85 - (void)applicationWillTerminate:(NSNotification *)note
88 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
91 - (ITMTRemote *)loadRemote
93 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
96 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
97 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
100 while ( (bundlePath = [enumerator nextObject]) ) {
101 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
104 Class remoteClass = [remoteBundle principalClass];
106 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
107 [remoteClass isKindOfClass:[NSObject class]]) {
109 id remote = [remoteClass remote];
110 [remoteArray addObject:remote];
115 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
116 // if ( [remoteArray count] > 1 ) {
117 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
119 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
122 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
123 return [remoteArray objectAtIndex:0];
129 - (void)applicationLaunched:(NSNotification *)note
131 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
132 [NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
135 [statusItem setMenu:menu];
137 isAppRunning = ITMTRemotePlayerRunning;
141 isAppRunning = ITMTRemotePlayerRunning;
144 - (void)applicationTerminated:(NSNotification *)note
146 if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {
147 NSMenu *notRunningMenu = [[NSMenu alloc] initWithTitle:@""];
148 [[notRunningMenu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self];
149 [notRunningMenu addItem:[NSMenuItem separatorItem]];
150 [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
151 [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
152 [statusItem setMenu:[notRunningMenu autorelease]];
154 [refreshTimer invalidate];
155 [refreshTimer release];
163 /*************************************************************************/
165 #pragma mark INSTANCE METHODS
166 /*************************************************************************/
168 - (void)registerDefaults
170 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
171 if (![defaults objectForKey:@"menu"]) {
173 NSMutableDictionary *loginwindow;
174 NSMutableArray *loginarray;
178 [NSArray arrayWithObjects:
192 @"Current Track Info",
193 nil] forKey:@"menu"];
195 [defaults synchronize];
196 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
197 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
199 for (i = 0; i < [loginarray count]; i++) {
200 NSDictionary *tempDict = [loginarray objectAtIndex:i];
201 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:
202 [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
209 //We must fix it so it is no longer suxy
211 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
212 AEDesc scriptDesc, resultDesc;
213 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]];
214 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
216 AECreateDesc(typeChar, [script cString], [script cStringLength],
219 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
221 AEDisposeDesc(&scriptDesc);
222 AEDisposeDesc(&resultDesc);
224 CloseComponent(asComponent);
229 if (![defaults integerForKey:@"SongsInAdvance"])
231 [defaults setInteger:5 forKey:@"SongsInAdvance"];
234 if (![defaults objectForKey:@"showName"]) {
235 [defaults setBool:YES forKey:@"showName"];
238 if (![defaults objectForKey:@"showArtist"]) {
239 [defaults setBool:YES forKey:@"showArtist"];
242 if (![defaults objectForKey:@"showAlbum"]) {
243 [defaults setBool:NO forKey:@"showAlbum"];
246 if (![defaults objectForKey:@"showTime"]) {
247 [defaults setBool:NO forKey:@"showTime"];
251 - (void)startTimerInNewThread
253 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
254 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
255 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
257 selector:@selector(timerUpdate)
259 repeats:YES] retain];
264 //Recreate the status item menu
267 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
271 lastPlaylistIndex = -1;
272 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
273 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
276 menu = [[NSMenu alloc] initWithTitle:@""];
278 /*while ([menu numberOfItems] > 0) {
279 [menu removeItemAtIndex:0];
283 lastSongIdentifier = @"0-0";
285 upcomingSongsItem = nil;
286 [upcomingSongsMenu release];
287 upcomingSongsMenu = nil;
290 [ratingMenu release];
294 [playlistMenu release];
301 for (i = 0; i < [myMenu count]; i++) {
302 NSString *item = [myMenu objectAtIndex:i];
303 if ([item isEqualToString:@"Play/Pause"]) {
304 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
305 playPauseItem = [menu addItemWithTitle:@"Play"
306 action:@selector(playPause:)
310 [self setKeyEquivalentForCode:[tempCombo keyCode]
311 andModifiers:[tempCombo modifiers] onItem:playPauseItem];
314 } else if ([item isEqualToString:@"Next Track"]) {
315 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
316 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
317 action:@selector(nextSong:)
321 [self setKeyEquivalentForCode:[tempCombo keyCode]
322 andModifiers:[tempCombo modifiers] onItem:nextTrack];
325 } else if ([item isEqualToString:@"Previous Track"]) {
326 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
327 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
328 action:@selector(prevSong:)
332 [self setKeyEquivalentForCode:[tempCombo keyCode]
333 andModifiers:[tempCombo modifiers] onItem:prevTrack];
336 } else if ([item isEqualToString:@"Fast Forward"]) {
337 [menu addItemWithTitle:@"Fast Forward"
338 action:@selector(fastForward:)
340 } else if ([item isEqualToString:@"Rewind"]) {
341 [menu addItemWithTitle:@"Rewind"
342 action:@selector(rewind:)
344 } else if ([item isEqualToString:@"Upcoming Songs"]) {
345 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
348 } else if ([item isEqualToString:@"Playlists"]) {
349 playlistItem = [menu addItemWithTitle:@"Playlists"
352 } else if ([item isEqualToString:@"EQ Presets"]) {
353 eqItem = [menu addItemWithTitle:@"EQ Presets"
356 } else if ([item isEqualToString:@"PreferencesÉ"]) {
357 [menu addItemWithTitle:@"PreferencesÉ"
358 action:@selector(showPreferences:)
360 } else if ([item isEqualToString:@"Quit"]) {
361 [menu addItemWithTitle:@"Quit"
362 action:@selector(quitMenuTunes:)
364 } else if ([item isEqualToString:@"Current Track Info"]) {
365 trackInfoIndex = [menu numberOfItems];
366 [menu addItemWithTitle:@"No Song"
369 } else if ([item isEqualToString:@"Song Rating"]) {
370 unichar fullstar = 0x2605;
371 unichar emptystar = 0x2606;
372 NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1];
373 NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1];
376 ratingItem = [menu addItemWithTitle:@"Song Rating"
380 ratingMenu = [[NSMenu alloc] initWithTitle:@""];
382 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
385 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
388 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
391 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
394 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
397 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
399 } else if ([item isEqualToString:@"<separator>"]) {
400 [menu addItem:[NSMenuItem separatorItem]];
404 [statusItem setMenu:menu];
412 //Rebuild the upcoming songs submenu. Can be improved a lot.
413 - (void)rebuildUpcomingSongsMenu
415 int curIndex = [currentRemote currentPlaylistIndex];
416 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
417 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
419 if (!isPlayingRadio) {
421 int curTrack = [currentRemote currentSongIndex];
424 [upcomingSongsMenu release];
425 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
426 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
427 [upcomingSongsItem setEnabled:YES];
429 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
431 NSString *curSong = [currentRemote songTitleAtIndex:i];
432 NSMenuItem *songItem;
433 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(selectSong:) keyEquivalent:@""];
434 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
435 [upcomingSongsMenu addItem:songItem];
443 [upcomingSongsItem setSubmenu:nil];
444 [upcomingSongsItem setEnabled:NO];
448 - (void)rebuildPlaylistMenu
450 NSArray *playlists = [currentRemote playlists];
451 int i, curPlaylist = [currentRemote currentPlaylistIndex];
453 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
456 [playlistMenu release];
457 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
459 for (i = 0; i < [playlists count]; i++) {
460 NSString *playlistName = [playlists objectAtIndex:i];
461 NSMenuItem *tempItem;
462 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
463 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
464 [playlistMenu addItem:tempItem];
467 [playlistItem setSubmenu:playlistMenu];
468 [playlistItem setEnabled:YES];
470 if (!isPlayingRadio) {
471 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
475 //Build a menu with the list of all available EQ presets
476 - (void)rebuildEQPresetsMenu
478 NSArray *eqPresets = [currentRemote eqPresets];
479 NSMenuItem *enabledItem;
482 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
486 eqMenu = [[NSMenu alloc] initWithTitle:@""];
488 enabledItem = [eqMenu addItemWithTitle:@"Enabled"
489 action:@selector(selectEQPreset:)
491 [enabledItem setTag:-1];
493 if ([currentRemote equalizerEnabled]) {
494 [enabledItem setState:NSOnState];
497 [eqMenu addItem:[NSMenuItem separatorItem]];
499 for (i = 0; i < [eqPresets count]; i++) {
500 NSString *setName = [eqPresets objectAtIndex:i];
501 NSMenuItem *tempItem;
503 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
505 [eqMenu addItem:tempItem];
509 [eqItem setSubmenu:eqMenu];
511 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
516 NSString *currentIdentifier = [currentRemote currentSongUniqueIdentifier];
517 if (![lastSongIdentifier isEqualToString:currentIdentifier]) {
519 } else if (!isPlayingRadio && ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist)) {
523 //Update Play/Pause menu item
525 if ([currentRemote playerPlayingState] == ITMTRemotePlayerPlaying) {
526 [playPauseItem setTitle:@"Pause"];
528 [playPauseItem setTitle:@"Play"];
535 NSUserDefaults *defaults;
536 int playlist = [currentRemote currentPlaylistIndex];
537 BOOL wasPlayingRadio = isPlayingRadio;
539 if ( (isAppRunning == ITMTRemotePlayerNotRunning) ) {
543 defaults = [NSUserDefaults standardUserDefaults];
545 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
549 //rebuild submenus, make them properly enabled for once...
553 if ( (trackInfoIndex > -1) && (playlist) ) {
554 NSString *title, *album, *artist;
557 lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
559 if ( (temp = [menu indexOfItemWithTitle:@"No Song"]) && (temp > -1) ) {
560 [menu removeItemAtIndex:temp];
561 [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:temp];
563 if ([defaults boolForKey:@"showName"]) {
564 [menu removeItemAtIndex:trackInfoIndex + 1];
568 title = [currentRemote currentSongTitle];
570 if (!wasPlayingRadio && (temp == -1)) {
571 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
572 [menu removeItemAtIndex:trackInfoIndex + 1];
574 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
575 [menu removeItemAtIndex:trackInfoIndex + 1];
577 if ([defaults boolForKey:@"showTime"]) {
578 [menu removeItemAtIndex:trackInfoIndex + 1];
582 if (!isPlayingRadio) {
583 ([defaults boolForKey:@"showAlbum"]) ? (album = [currentRemote currentSongAlbum]) :
585 ([defaults boolForKey:@"showArtist"]) ? (artist = [currentRemote currentSongArtist]) :
587 if ([defaults boolForKey:@"showTime"]) {
588 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
591 if ([artist length] > 0) {
592 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
595 if ([album length] > 0) {
596 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
599 if ([defaults boolForKey:@"showArtist"]) {
600 didHaveArtistName = (([artist length] > 0) ? YES : NO);
603 if ([defaults boolForKey:@"showAlbum"]) {
604 didHaveAlbumName = (([album length] > 0) ? YES : NO);
608 if ([title length] > 0) {
609 [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", title] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
621 - (void)selectSong:(id)sender
623 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
626 - (void)selectPlaylist:(id)sender
628 int playlist = [[sender representedObject] intValue];
629 if (!isPlayingRadio) {
630 int curPlaylist = [currentRemote currentPlaylistIndex];
631 if (curPlaylist > 0) {
632 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
635 [currentRemote switchToPlaylistAtIndex:playlist];
636 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
640 - (void)selectEQPreset:(id)sender
642 int curSet = [currentRemote currentEQPresetIndex];
643 int item = [sender tag];
646 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
648 [currentRemote setEqualizerEnabled:YES];
649 [currentRemote switchToEQAtIndex:item];
650 [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
651 [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
655 - (void)selectSongRating:(id)sender
657 [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState];
658 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
659 [sender setState:NSOnState];
662 - (void)playPause:(id)sender
664 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
666 if (state == ITMTRemotePlayerPlaying) {
667 [currentRemote pause];
668 [playPauseItem setTitle:@"Play"];
669 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
670 [currentRemote pause];
671 [currentRemote play];
673 [currentRemote play];
674 [playPauseItem setTitle:@"Pause"];
678 - (void)nextSong:(id)sender
680 [currentRemote goToNextSong];
683 - (void)prevSong:(id)sender
685 [currentRemote goToPreviousSong];
688 - (void)fastForward:(id)sender
690 [currentRemote forward];
691 [playPauseItem setTitle:@"Play"];
694 - (void)rewind:(id)sender
696 [currentRemote rewind];
697 [playPauseItem setTitle:@"Play"];
702 - (void)quitMenuTunes:(id)sender
704 [NSApp terminate:self];
707 - (void)showPlayer:(id)sender
709 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
710 [currentRemote showPrimaryInterface];
712 if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
713 NSLog(@"Error Launching Player");
718 - (void)showPreferences:(id)sender
720 if (!prefsController) {
721 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
726 - (void)closePreferences
728 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
731 [prefsController release];
732 prefsController = nil;
744 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
745 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
746 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
747 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
748 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
753 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
755 if ([defaults objectForKey:@"PlayPause"] != nil) {
756 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
757 combo:[defaults keyComboForKey:@"PlayPause"]
758 target:self action:@selector(playPause:)];
761 if ([defaults objectForKey:@"NextTrack"] != nil) {
762 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
763 combo:[defaults keyComboForKey:@"NextTrack"]
764 target:self action:@selector(nextSong:)];
767 if ([defaults objectForKey:@"PrevTrack"] != nil) {
768 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
769 combo:[defaults keyComboForKey:@"PrevTrack"]
770 target:self action:@selector(prevSong:)];
773 if ([defaults objectForKey:@"TrackInfo"] != nil) {
774 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
775 combo:[defaults keyComboForKey:@"TrackInfo"]
776 target:self action:@selector(showCurrentTrackInfo)];
779 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
780 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
781 combo:[defaults keyComboForKey:@"UpcomingSongs"]
782 target:self action:@selector(showUpcomingSongs)];
788 // Show Current Track Info And Show Upcoming Songs Floaters
792 - (void)showCurrentTrackInfo
794 NSString *trackName = [currentRemote currentSongTitle];
795 if (!statusWindow && [trackName length]) {
796 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
797 NSString *stringToShow = @"";
799 if ([defaults boolForKey:@"showName"]) {
800 if ([defaults boolForKey:@"showArtist"]) {
801 NSString *trackArtist = [currentRemote currentSongArtist];
802 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
804 stringToShow = [stringToShow stringByAppendingString:trackName];
805 stringToShow = [stringToShow stringByAppendingString:@"\n"];
808 if ([defaults boolForKey:@"showAlbum"]) {
809 NSString *trackAlbum = [currentRemote currentSongAlbum];
810 if ([trackAlbum length]) {
811 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
812 stringToShow = [stringToShow stringByAppendingString:@"\n"];
816 if ([defaults boolForKey:@"showTime"]) {
817 NSString *trackTime = [currentRemote currentSongLength];
818 if ([trackTime length]) {
819 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
824 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
825 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
827 stringToShow = [stringToShow stringByAppendingString:
828 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
830 stringToShow = [stringToShow stringByAppendingString:
831 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
835 [statusWindow setText:stringToShow];
836 [NSTimer scheduledTimerWithTimeInterval:3.0
838 selector:@selector(fadeAndCloseStatusWindow)
844 - (void)showUpcomingSongs
846 int curPlaylist = [currentRemote currentPlaylistIndex];
848 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
851 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
852 int curTrack = [currentRemote currentSongIndex];
854 NSString *songs = @"";
856 statusWindow = [ITTransientStatusWindow sharedWindow];
857 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
859 NSString *curSong = [currentRemote songTitleAtIndex:i];
860 songs = [songs stringByAppendingString:curSong];
861 songs = [songs stringByAppendingString:@"\n"];
864 [statusWindow setText:songs];
865 [NSTimer scheduledTimerWithTimeInterval:3.0
867 selector:@selector(fadeAndCloseStatusWindow)
874 - (void)fadeAndCloseStatusWindow
876 [statusWindow orderOut:self];
879 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
880 onItem:(NSMenuItem *)item
882 unichar charcode = 'a';
884 long cocoaModifiers = 0;
885 static long carbonToCocoa[6][2] =
887 { cmdKey, NSCommandKeyMask },
888 { optionKey, NSAlternateKeyMask },
889 { controlKey, NSControlKeyMask },
890 { shiftKey, NSShiftKeyMask },
893 for (i = 0; i < 6; i++) {
894 if (modifiers & carbonToCocoa[i][0]) {
895 cocoaModifiers += carbonToCocoa[i][1];
898 [item setKeyEquivalentModifierMask:cocoaModifiers];
900 //Missing key combos for some keys. Must find them later.
914 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
915 NSLog(@"%@", menuRef);
916 SetMenuItemCommandKey(menuRef, 0, NO, 49);
917 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
918 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
924 charcode = NSDeleteFunctionKey;
940 charcode = NSF5FunctionKey;
944 charcode = NSF6FunctionKey;
948 charcode = NSF7FunctionKey;
952 charcode = NSF3FunctionKey;
956 charcode = NSF8FunctionKey;
960 charcode = NSF9FunctionKey;
964 charcode = NSF11FunctionKey;
968 charcode = NSF3FunctionKey;
972 charcode = NSF14FunctionKey;
976 charcode = NSF10FunctionKey;
980 charcode = NSF12FunctionKey;
984 charcode = NSF13FunctionKey;
988 charcode = NSInsertFunctionKey;
992 charcode = NSHomeFunctionKey;
996 charcode = NSPageUpFunctionKey;
1000 charcode = NSDeleteFunctionKey;
1004 charcode = NSF4FunctionKey;
1008 charcode = NSEndFunctionKey;
1012 charcode = NSF2FunctionKey;
1016 charcode = NSPageDownFunctionKey;
1020 charcode = NSF1FunctionKey;
1024 charcode = NSLeftArrowFunctionKey;
1028 charcode = NSRightArrowFunctionKey;
1032 charcode = NSDownArrowFunctionKey;
1036 charcode = NSUpArrowFunctionKey;
1040 if (charcode == 'a') {
1041 unsigned long state;
1046 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1047 keyTrans = KeyTranslate(kchr, code, &state);
1048 charCode = keyTrans;
1049 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1050 } else if (charcode != 'b') {
1051 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];