2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
6 @interface MenuTunes(Private)
7 - (ITMTRemote *)loadRemote;
9 - (void)rebuildUpcomingSongsMenu;
10 - (void)rebuildPlaylistMenu;
11 - (void)rebuildEQPresetsMenu;
14 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
15 onItem:(NSMenuItem *)item;
19 @implementation MenuTunes
21 /*************************************************************************/
23 #pragma mark INITIALIZATION METHODS
24 /*************************************************************************/
28 if ( ( self = [super init] ) ) {
29 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
30 statusWindow = [StatusWindow sharedWindow];
35 - (void)applicationDidFinishLaunching:(NSNotification *)note
37 currentRemote = [self loadRemote];
38 [currentRemote begin];
40 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
41 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
43 [self registerDefaultsIfNeeded];
45 menu = [[NSMenu alloc] initWithTitle:@""];
47 if ( ( [currentRemote playerRunningStatus] == ITMTRemotePlayerRunning ) ) {
48 [self remotePlayerLaunched:nil];
50 [self remotePlayerTerminated:nil];
53 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar] withLength:NSSquareStatusItemLength];
55 [statusItem setImage:[NSImage imageNamed:@"menu"]];
56 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
57 [statusItem setMenu:menu];
58 // Below line of code is for creating builds for Beta Testers
59 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
62 - (ITMTRemote *)loadRemote
64 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
67 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
68 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
71 while ( (bundlePath = [enumerator nextObject]) ) {
72 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
75 Class remoteClass = [remoteBundle principalClass];
77 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
78 [remoteClass isKindOfClass:[NSObject class]]) {
80 id remote = [remoteClass remote];
81 [remoteArray addObject:remote];
86 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
87 // if ( [remoteArray count] > 1 ) {
88 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
90 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
93 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
94 return [remoteArray objectAtIndex:0];
98 /*************************************************************************/
100 #pragma mark INSTANCE METHODS
101 /*************************************************************************/
103 - (void)registerDefaultsIfNeeded
105 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
106 if (![defaults objectForKey:@"menu"]) {
108 NSMutableDictionary *loginwindow;
109 NSMutableArray *loginarray;
113 [NSArray arrayWithObjects:
127 @"Current Track Info",
128 nil] forKey:@"menu"];
130 [defaults synchronize];
131 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
132 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
134 for (i = 0; i < [loginarray count]; i++) {
135 NSDictionary *tempDict = [loginarray objectAtIndex:i];
136 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
143 //We must fix it so it is no longer suxy
145 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
146 AEDesc scriptDesc, resultDesc;
147 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]];
148 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
150 AECreateDesc(typeChar, [script cString], [script cStringLength],
153 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
155 AEDisposeDesc(&scriptDesc);
156 AEDisposeDesc(&resultDesc);
158 CloseComponent(asComponent);
163 if (![defaults integerForKey:@"SongsInAdvance"])
165 [defaults setInteger:5 forKey:@"SongsInAdvance"];
168 if (![defaults objectForKey:@"showName"]) {
169 [defaults setBool:YES forKey:@"showName"];
172 if (![defaults objectForKey:@"showArtist"]) {
173 [defaults setBool:YES forKey:@"showArtist"];
176 if (![defaults objectForKey:@"showAlbum"]) {
177 [defaults setBool:NO forKey:@"showAlbum"];
180 if (![defaults objectForKey:@"showTime"]) {
181 [defaults setBool:NO forKey:@"showTime"];
185 //Recreate the status item menu
188 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
193 lastPlaylistIndex = -1;
194 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
195 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
197 while ([menu numberOfItems] > 0) {
198 [menu removeItemAtIndex:0];
201 playPauseMenuItem = nil;
202 upcomingSongsItem = nil;
203 songRatingMenuItem = nil;
205 [playlistMenu release];
211 for (i = 0; i < [myMenu count]; i++) {
212 NSString *item = [myMenu objectAtIndex:i];
213 if ([item isEqualToString:@"Play/Pause"]) {
214 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
215 playPauseMenuItem = [menu addItemWithTitle:@"Play"
216 action:@selector(playPause:)
218 [playPauseMenuItem setTarget:self];
221 [self setKeyEquivalentForCode:[tempCombo keyCode]
222 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
225 } else if ([item isEqualToString:@"Next Track"]) {
226 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
227 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
228 action:@selector(nextSong:)
231 [nextTrack setTarget:self];
233 [self setKeyEquivalentForCode:[tempCombo keyCode]
234 andModifiers:[tempCombo modifiers] onItem:nextTrack];
237 } else if ([item isEqualToString:@"Previous Track"]) {
238 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
239 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
240 action:@selector(prevSong:)
243 [prevTrack setTarget:self];
245 [self setKeyEquivalentForCode:[tempCombo keyCode]
246 andModifiers:[tempCombo modifiers] onItem:prevTrack];
249 } else if ([item isEqualToString:@"Fast Forward"]) {
250 [[menu addItemWithTitle:@"Fast Forward"
251 action:@selector(fastForward:)
252 keyEquivalent:@""] setTarget:self];
253 } else if ([item isEqualToString:@"Rewind"]) {
254 [[menu addItemWithTitle:@"Rewind"
255 action:@selector(rewind:)
256 keyEquivalent:@""] setTarget:self];
257 } else if ([item isEqualToString:@"Upcoming Songs"]) {
258 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
261 } else if ([item isEqualToString:@"Playlists"]) {
262 playlistItem = [menu addItemWithTitle:@"Playlists"
265 } else if ([item isEqualToString:@"EQ Presets"]) {
266 eqItem = [menu addItemWithTitle:@"EQ Presets"
269 } else if ([item isEqualToString:@"PreferencesÉ"]) {
270 [[menu addItemWithTitle:@"PreferencesÉ"
271 action:@selector(showPreferences:)
272 keyEquivalent:@""] setTarget:self];
273 } else if ([item isEqualToString:@"Quit"]) {
274 [[menu addItemWithTitle:@"Quit"
275 action:@selector(quitMenuTunes:)
276 keyEquivalent:@""] setTarget:self];
277 } else if ([item isEqualToString:@"Current Track Info"]) {
278 trackInfoIndex = [menu numberOfItems];
279 [menu addItemWithTitle:@"No Song"
282 } else if ([item isEqualToString:@"Song Rating"]) {
283 songRatingMenuItem = [menu addItemWithTitle:@"Song Rating"
286 } else if ([item isEqualToString:@"<separator>"]) {
287 [menu addItem:[NSMenuItem separatorItem]];
291 [self timerUpdate]; //Updates dynamic info in the menu
297 //Updates the menu with current player state, song, and upcoming songs
300 NSMenuItem *menuItem;
301 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
303 if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
307 if (upcomingSongsItem) {
308 [self rebuildUpcomingSongsMenu];
312 [self rebuildPlaylistMenu];
316 [self rebuildEQPresetsMenu];
318 if (trackInfoIndex > -1) {
319 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
320 curSongName = [currentRemote currentSongTitle];
322 if ([defaults boolForKey:@"showAlbum"]) {
323 curAlbumName = [currentRemote currentSongAlbum];
326 if ([defaults boolForKey:@"showArtist"]) {
327 curArtistName = [currentRemote currentSongArtist];
330 if ([curSongName length] > 0) {
331 int index = [menu indexOfItemWithTitle:@"Now Playing"];
333 if ([defaults boolForKey:@"showName"]) {
334 [menu removeItemAtIndex:index + 1];
336 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
337 [menu removeItemAtIndex:index + 1];
339 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
340 [menu removeItemAtIndex:index + 1];
342 if ([defaults boolForKey:@"showTime"]) {
343 [menu removeItemAtIndex:index + 1];
347 if (!isPlayingRadio) {
348 if ([defaults boolForKey:@"showTime"]) {
349 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
352 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
356 if ([curArtistName length] > 0) {
357 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
360 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
364 if ([curAlbumName length] > 0) {
365 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
368 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
372 if (songRatingMenuItem) {
373 int rating = (int)[currentRemote currentSongRating] * 10;
376 for (i = 0; i < 5; i++) {
377 [[ratingMenu itemAtIndex:i] setState:NSOffState];
382 [[ratingMenu itemAtIndex:5] setState:NSOnState];
385 [[ratingMenu itemAtIndex:4] setState:NSOnState];
388 [[ratingMenu itemAtIndex:3] setState:NSOnState];
391 [[ratingMenu itemAtIndex:2] setState:NSOnState];
394 [[ratingMenu itemAtIndex:1] setState:NSOnState];
397 [[ratingMenu itemAtIndex:0] setState:NSOnState];
403 if ([defaults boolForKey:@"showName"]) {
404 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
407 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
412 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
413 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
414 [menu insertItem:menuItem atIndex:trackInfoIndex];
417 [songRatingMenuItem setSubmenu:ratingMenu];
418 [songRatingMenuItem setEnabled:YES];
420 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
421 [menu removeItemAtIndex:trackInfoIndex];
423 if ([defaults boolForKey:@"showName"] == YES) {
424 [menu removeItemAtIndex:trackInfoIndex];
427 if ([defaults boolForKey:@"showTime"] == YES) {
428 [menu removeItemAtIndex:trackInfoIndex];
431 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
432 [menu removeItemAtIndex:trackInfoIndex];
435 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
436 [menu removeItemAtIndex:trackInfoIndex];
439 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
440 [menu insertItem:menuItem atIndex:trackInfoIndex];
444 if ([defaults boolForKey:@"showArtist"]) {
445 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
448 if ([defaults boolForKey:@"showAlbum"]) {
449 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
455 //Rebuild the upcoming songs submenu. Can be improved a lot.
456 - (void)rebuildUpcomingSongsMenu
458 int curIndex = [currentRemote currentPlaylistIndex];
459 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
460 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
462 if (!isPlayingRadio) {
464 int curTrack = [currentRemote currentSongIndex];
467 [upcomingSongsMenu release];
468 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
469 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
470 [upcomingSongsItem setEnabled:YES];
472 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
474 NSString *curSong = [currentRemote songTitleAtIndex:i];
475 NSMenuItem *songItem;
476 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
477 [songItem setTarget:self];
478 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
479 [upcomingSongsMenu addItem:songItem];
487 [upcomingSongsItem setSubmenu:nil];
488 [upcomingSongsItem setEnabled:NO];
492 - (void)rebuildPlaylistMenu
494 NSArray *playlists = [currentRemote playlists];
495 int i, curPlaylist = [currentRemote currentPlaylistIndex];
497 if (isPlayingRadio) {
500 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
503 [playlistMenu release];
504 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
506 for (i = 0; i < [playlists count]; i++) {
507 NSString *playlistName = [playlists objectAtIndex:i];
508 NSMenuItem *tempItem;
509 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
510 [tempItem setTarget:self];
511 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
512 [playlistMenu addItem:tempItem];
515 [playlistItem setSubmenu:playlistMenu];
516 [playlistItem setEnabled:YES];
519 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
523 //Build a menu with the list of all available EQ presets
524 - (void)rebuildEQPresetsMenu
526 NSArray *eqPresets = [currentRemote eqPresets];
527 NSMenuItem *enabledItem;
530 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
534 eqMenu = [[NSMenu alloc] initWithTitle:@""];
536 enabledItem = [eqMenu addItemWithTitle:@"Disabled"
537 action:@selector(toggleEqualizer)
540 if ([currentRemote equalizerEnabled] == NO) {
541 [enabledItem setState:NSOnState];
544 [eqMenu addItem:[NSMenuItem separatorItem]];
546 for (i = 0; i < [eqPresets count]; i++) {
547 NSString *setName = [eqPresets objectAtIndex:i];
548 NSMenuItem *tempItem;
550 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
551 [tempItem setTarget:self];
552 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
553 [eqMenu addItem:tempItem];
557 [eqItem setSubmenu:eqMenu];
559 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
564 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
565 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
566 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
567 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
568 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
573 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
575 if ([defaults objectForKey:@"PlayPause"] != nil) {
576 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
577 combo:[defaults keyComboForKey:@"PlayPause"]
578 target:self action:@selector(playPause:)];
581 if ([defaults objectForKey:@"NextTrack"] != nil) {
582 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
583 combo:[defaults keyComboForKey:@"NextTrack"]
584 target:self action:@selector(nextSong:)];
587 if ([defaults objectForKey:@"PrevTrack"] != nil) {
588 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
589 combo:[defaults keyComboForKey:@"PrevTrack"]
590 target:self action:@selector(prevSong:)];
593 if ([defaults objectForKey:@"TrackInfo"] != nil) {
594 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
595 combo:[defaults keyComboForKey:@"TrackInfo"]
596 target:self action:@selector(showCurrentTrackInfo)];
599 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
600 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
601 combo:[defaults keyComboForKey:@"UpcomingSongs"]
602 target:self action:@selector(showUpcomingSongs)];
606 //Called when the timer fires.
609 int playlist = [currentRemote currentPlaylistIndex];
610 ITMTRemotePlayerState playerState = [currentRemote playerState];
612 if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
613 int trackPlayingIndex = [currentRemote currentSongIndex];
615 if (trackPlayingIndex != lastSongIndex) {
616 BOOL wasPlayingRadio = isPlayingRadio;
617 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
619 if (isPlayingRadio && !wasPlayingRadio) {
621 for (i = 0; i < [playlistMenu numberOfItems]; i++)
623 [[playlistMenu itemAtIndex:i] setState:NSOffState];
626 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
629 if (wasPlayingRadio) {
630 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
631 [menu insertItem:temp atIndex:trackInfoIndex + 1];
636 lastSongIndex = trackPlayingIndex;
638 if (playlist != lastPlaylistIndex) {
639 BOOL wasPlayingRadio = isPlayingRadio;
640 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
642 if (isPlayingRadio && !wasPlayingRadio) {
644 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
645 [[playlistMenu itemAtIndex:i] setState:NSOffState];
649 if (wasPlayingRadio) {
650 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
651 [menu insertItem:temp atIndex:trackInfoIndex + 1];
655 if (!isPlayingRadio) {
657 for (i = 0; i < [playlistMenu numberOfItems]; i++)
659 [[playlistMenu itemAtIndex:i] setState:NSOffState];
661 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
665 lastSongIndex = trackPlayingIndex;
666 lastPlaylistIndex = playlist;
669 //Update Play/Pause menu item
670 if (playPauseMenuItem){
671 if (playerState == ITMTRemotePlayerPlaying) {
672 [playPauseMenuItem setTitle:@"Pause"];
674 [playPauseMenuItem setTitle:@"Play"];
677 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
678 NSMenuItem *menuItem;
679 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
680 //Remote the now playing item and add no song item
681 [menu removeItemAtIndex:trackInfoIndex];
683 if ([defaults boolForKey:@"showName"] == YES) {
684 [menu removeItemAtIndex:trackInfoIndex];
687 if ([defaults boolForKey:@"showTime"] == YES) {
688 [menu removeItemAtIndex:trackInfoIndex];
691 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
692 [menu removeItemAtIndex:trackInfoIndex];
695 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
696 [menu removeItemAtIndex:trackInfoIndex];
699 [playPauseMenuItem setTitle:@"Play"];
701 didHaveArtistName = NO;
702 didHaveAlbumName = NO;
703 lastPlaylistIndex = -1;
706 [upcomingSongsItem setSubmenu:nil];
707 [upcomingSongsItem setEnabled:NO];
709 [songRatingMenuItem setSubmenu:nil];
710 [songRatingMenuItem setEnabled:NO];
712 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
713 [menu insertItem:menuItem atIndex:trackInfoIndex];
718 - (void)remotePlayerLaunched:(NSNotification *)note
720 isAppRunning = ITMTRemotePlayerRunning;
723 [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
725 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
727 [self rebuildPlaylistMenu];
730 [self rebuildEQPresetsMenu];
732 [statusItem setMenu:menu]; //Set the menu back to the main one
735 - (void)runTimerInNewThread
737 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
738 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
739 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain];
744 - (void)remotePlayerTerminated:(NSNotification *)note
746 isAppRunning = ITMTRemotePlayerNotRunning;
749 menu = [[NSMenu alloc] initWithTitle:@""];
750 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
751 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
752 [menu addItem:[NSMenuItem separatorItem]];
753 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
754 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
755 [statusItem setMenu:menu];
757 [refreshTimer invalidate];
758 [refreshTimer release];
765 // Selectors - called from status item menu
769 // Plugin dependent selectors
771 - (void)playTrack:(id)sender
773 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
777 - (void)selectPlaylist:(id)sender
779 int playlist = [[sender representedObject] intValue];
780 if (!isPlayingRadio) {
781 int curPlaylist = [currentRemote currentPlaylistIndex];
782 if (curPlaylist > 0) {
783 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
786 [currentRemote switchToPlaylistAtIndex:playlist];
787 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
791 - (void)selectEQPreset:(id)sender
793 int curSet = [currentRemote currentEQPresetIndex];
794 int item = [[sender representedObject] intValue];
795 [currentRemote switchToEQAtIndex:item];
796 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
797 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
800 - (void)playPause:(id)sender
802 ITMTRemotePlayerState state = [currentRemote playerState];
804 if (state == ITMTRemotePlayerPlaying) {
805 [currentRemote pause];
806 [playPauseMenuItem setTitle:@"Play"];
807 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
808 [currentRemote pause];
809 [currentRemote play];
811 [currentRemote play];
812 [playPauseMenuItem setTitle:@"Pause"];
816 - (void)nextSong:(id)sender
818 [currentRemote goToNextSong];
821 - (void)prevSong:(id)sender
823 [currentRemote goToPreviousSong];
826 - (void)fastForward:(id)sender
828 [currentRemote forward];
829 [playPauseMenuItem setTitle:@"Play"];
832 - (void)rewind:(id)sender
834 [currentRemote rewind];
835 [playPauseMenuItem setTitle:@"Play"];
838 - (void)toggleEqualizer
840 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
843 - (IBAction)setSongRating:(id)sender
845 NSLog(@"%f", [currentRemote currentSongRating]);
846 NSLog(@"%f", (float)[sender tag] / 100.0);
847 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
852 // Plugin independent selectors
855 - (void)quitMenuTunes:(id)sender
857 [NSApp terminate:self];
860 - (void)showPreferences:(id)sender
862 if (!prefsController) {
863 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
868 - (void)closePreferences
870 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
873 [prefsController release];
874 prefsController = nil;
879 // Show Current Track Info And Show Upcoming Songs Floaters
883 - (void)showCurrentTrackInfo
885 NSString *trackName = [currentRemote currentSongTitle];
886 if (!statusWindow && [trackName length]) {
887 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
888 NSString *stringToShow = @"";
890 if ([defaults boolForKey:@"showName"]) {
891 if ([defaults boolForKey:@"showArtist"]) {
892 NSString *trackArtist = [currentRemote currentSongArtist];
893 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
895 stringToShow = [stringToShow stringByAppendingString:trackName];
896 stringToShow = [stringToShow stringByAppendingString:@"\n"];
899 if ([defaults boolForKey:@"showAlbum"]) {
900 NSString *trackAlbum = [currentRemote currentSongAlbum];
901 if ([trackAlbum length]) {
902 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
903 stringToShow = [stringToShow stringByAppendingString:@"\n"];
907 if ([defaults boolForKey:@"showTime"]) {
908 NSString *trackTime = [currentRemote currentSongLength];
909 if ([trackTime length]) {
910 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
915 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
916 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
918 stringToShow = [stringToShow stringByAppendingString:
919 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
921 stringToShow = [stringToShow stringByAppendingString:
922 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
926 [statusWindow setText:stringToShow];
927 [NSTimer scheduledTimerWithTimeInterval:3.0
929 selector:@selector(fadeAndCloseStatusWindow)
935 - (void)showUpcomingSongs
937 int curPlaylist = [currentRemote currentPlaylistIndex];
939 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
942 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
943 int curTrack = [currentRemote currentSongIndex];
945 NSString *songs = @"";
947 statusWindow = [ITTransientStatusWindow sharedWindow];
948 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
950 NSString *curSong = [currentRemote songTitleAtIndex:i];
951 songs = [songs stringByAppendingString:curSong];
952 songs = [songs stringByAppendingString:@"\n"];
955 [statusWindow setText:songs];
956 [NSTimer scheduledTimerWithTimeInterval:3.0
958 selector:@selector(fadeAndCloseStatusWindow)
965 - (void)fadeAndCloseStatusWindow
967 [statusWindow orderOut:self];
970 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
971 onItem:(NSMenuItem *)item
973 unichar charcode = 'a';
975 long cocoaModifiers = 0;
976 static long carbonToCocoa[6][2] =
978 { cmdKey, NSCommandKeyMask },
979 { optionKey, NSAlternateKeyMask },
980 { controlKey, NSControlKeyMask },
981 { shiftKey, NSShiftKeyMask },
984 for (i = 0; i < 6; i++) {
985 if (modifiers & carbonToCocoa[i][0]) {
986 cocoaModifiers += carbonToCocoa[i][1];
989 [item setKeyEquivalentModifierMask:cocoaModifiers];
991 //Missing key combos for some keys. Must find them later.
1005 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1006 NSLog(@"%@", menuRef);
1007 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1008 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1009 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1015 charcode = NSDeleteFunctionKey;
1031 charcode = NSF5FunctionKey;
1035 charcode = NSF6FunctionKey;
1039 charcode = NSF7FunctionKey;
1043 charcode = NSF3FunctionKey;
1047 charcode = NSF8FunctionKey;
1051 charcode = NSF9FunctionKey;
1055 charcode = NSF11FunctionKey;
1059 charcode = NSF3FunctionKey;
1063 charcode = NSF14FunctionKey;
1067 charcode = NSF10FunctionKey;
1071 charcode = NSF12FunctionKey;
1075 charcode = NSF13FunctionKey;
1079 charcode = NSInsertFunctionKey;
1083 charcode = NSHomeFunctionKey;
1087 charcode = NSPageUpFunctionKey;
1091 charcode = NSDeleteFunctionKey;
1095 charcode = NSF4FunctionKey;
1099 charcode = NSEndFunctionKey;
1103 charcode = NSF2FunctionKey;
1107 charcode = NSPageDownFunctionKey;
1111 charcode = NSF1FunctionKey;
1115 charcode = NSLeftArrowFunctionKey;
1119 charcode = NSRightArrowFunctionKey;
1123 charcode = NSDownArrowFunctionKey;
1127 charcode = NSUpArrowFunctionKey;
1131 if (charcode == 'a') {
1132 unsigned long state;
1137 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1138 keyTrans = KeyTranslate(kchr, code, &state);
1139 charCode = keyTrans;
1140 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1141 } else if (charcode != 'b') {
1142 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1146 /*************************************************************************/
1148 #pragma mark NSApplication DELEGATE METHODS
1149 /*************************************************************************/
1151 - (void)applicationWillTerminate:(NSNotification *)note
1153 [self clearHotKeys];
1154 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1158 /*************************************************************************/
1160 #pragma mark DEALLOCATION METHODS
1161 /*************************************************************************/
1166 [refreshTimer invalidate];
1167 [refreshTimer release];
1170 [currentRemote halt];
1171 [statusItem release];