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]
54 withLength:NSSquareStatusItemLength];
56 [statusItem setImage:[NSImage imageNamed:@"menu"]];
57 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
58 [statusItem setMenu:menu];
59 // Below line of code is for creating builds for Beta Testers
60 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
63 - (ITMTRemote *)loadRemote
65 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
68 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
69 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
72 while ( (bundlePath = [enumerator nextObject]) ) {
73 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
76 Class remoteClass = [remoteBundle principalClass];
78 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
79 [remoteClass isKindOfClass:[NSObject class]]) {
81 id remote = [remoteClass remote];
82 [remoteArray addObject:remote];
87 // if ( [remoteArray count] > 0 ) { // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
88 // if ( [remoteArray count] > 1 ) {
89 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
91 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
94 // NSLog(@"%@", [remoteArray objectAtIndex:0]); //DEBUG
95 return [remoteArray objectAtIndex:0];
99 /*************************************************************************/
101 #pragma mark INSTANCE METHODS
102 /*************************************************************************/
104 - (void)registerDefaultsIfNeeded
106 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
107 if (![defaults objectForKey:@"menu"]) {
109 NSMutableDictionary *loginwindow;
110 NSMutableArray *loginarray;
114 [NSArray arrayWithObjects:
128 @"Current Track Info",
129 nil] forKey:@"menu"];
131 [defaults synchronize];
132 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
133 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
135 for (i = 0; i < [loginarray count]; i++) {
136 NSDictionary *tempDict = [loginarray objectAtIndex:i];
137 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
144 //We must fix it so it is no longer suxy
146 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
147 AEDesc scriptDesc, resultDesc;
148 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]];
149 ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
151 AECreateDesc(typeChar, [script cString], [script cStringLength],
154 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
156 AEDisposeDesc(&scriptDesc);
157 AEDisposeDesc(&resultDesc);
159 CloseComponent(asComponent);
164 if (![defaults integerForKey:@"SongsInAdvance"])
166 [defaults setInteger:5 forKey:@"SongsInAdvance"];
169 if (![defaults objectForKey:@"showName"]) {
170 [defaults setBool:YES forKey:@"showName"];
173 if (![defaults objectForKey:@"showArtist"]) {
174 [defaults setBool:YES forKey:@"showArtist"];
177 if (![defaults objectForKey:@"showAlbum"]) {
178 [defaults setBool:NO forKey:@"showAlbum"];
181 if (![defaults objectForKey:@"showTime"]) {
182 [defaults setBool:NO forKey:@"showTime"];
186 //Recreate the status item menu
189 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
194 lastPlaylistIndex = -1;
195 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
196 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
198 while ([menu numberOfItems] > 0) {
199 [menu removeItemAtIndex:0];
202 playPauseMenuItem = nil;
203 upcomingSongsItem = 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 /*NSMenu *ratingSubmenu = [[NSMenu alloc] initWithTitle:@""];
284 unichar whiteStarChar = 2606;
285 unichar blackStarChar = 2605;
286 NSString *whiteStar = [NSString stringWithCharacters:&whiteStarChar
288 NSString *blackStar = [NSString stringWithCharacters:&blackStarChar
290 NSData *whiteStarData = [whiteStar dataUsingEncoding:NSUTF8StringEncoding];
291 NSData *blackStarData = [blackStar dataUsingEncoding:NSUTF8StringEncoding];
292 NSString *string = @"";
295 whiteStar = [[NSString alloc] initWithData:whiteStarData encoding:NSUTF8StringEncoding];
297 for (i = 0; i < 5; i++) {
298 string = [string stringByAppendingString:whiteStar];
300 for (i = 0; i < 6; i++) {
301 NSMenuItem *ratingItem;
302 ratingItem = [ratingSubmenu addItemWithTitle:string action:@selector(setSongRating:) keyEquivalent:@""];
303 [ratingItem setTarget:self];
304 [ratingItem setTag:i * 20];
305 string = [string substringToIndex:4];
306 string = [blackStar stringByAppendingString:string];
308 [ratingSubmenu autorelease];*/
309 [[menu addItemWithTitle:@"Song Rating"
311 keyEquivalent:@""] setSubmenu:ratingMenu];
312 } else if ([item isEqualToString:@"<separator>"]) {
313 [menu addItem:[NSMenuItem separatorItem]];
317 [self timerUpdate]; //Updates dynamic info in the menu
323 //Updates the menu with current player state, song, and upcoming songs
326 NSMenuItem *menuItem;
327 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
329 if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
333 if (upcomingSongsItem) {
334 [self rebuildUpcomingSongsMenu];
338 [self rebuildPlaylistMenu];
342 [self rebuildEQPresetsMenu];
344 if (trackInfoIndex > -1) {
345 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
346 curSongName = [currentRemote currentSongTitle];
348 if ([defaults boolForKey:@"showAlbum"]) {
349 curAlbumName = [currentRemote currentSongAlbum];
352 if ([defaults boolForKey:@"showArtist"]) {
353 curArtistName = [currentRemote currentSongArtist];
356 if ([curSongName length] > 0) {
357 int index = [menu indexOfItemWithTitle:@"Now Playing"];
359 if ([defaults boolForKey:@"showName"]) {
360 [menu removeItemAtIndex:index + 1];
362 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
363 [menu removeItemAtIndex:index + 1];
365 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
366 [menu removeItemAtIndex:index + 1];
368 if ([defaults boolForKey:@"showTime"]) {
369 [menu removeItemAtIndex:index + 1];
373 if (!isPlayingRadio) {
374 if ([defaults boolForKey:@"showTime"]) {
375 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
378 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
382 if ([curArtistName length] > 0) {
383 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
386 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
390 if ([curAlbumName length] > 0) {
391 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
394 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
399 if ([defaults boolForKey:@"showName"]) {
400 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
403 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
408 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
409 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
410 [menu insertItem:menuItem atIndex:trackInfoIndex];
413 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
414 [menu removeItemAtIndex:trackInfoIndex];
416 if ([defaults boolForKey:@"showName"] == YES) {
417 [menu removeItemAtIndex:trackInfoIndex];
420 if ([defaults boolForKey:@"showTime"] == YES) {
421 [menu removeItemAtIndex:trackInfoIndex];
424 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
425 [menu removeItemAtIndex:trackInfoIndex];
428 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
429 [menu removeItemAtIndex:trackInfoIndex];
432 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
433 [menu insertItem:menuItem atIndex:trackInfoIndex];
437 if ([defaults boolForKey:@"showArtist"]) {
438 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
441 if ([defaults boolForKey:@"showAlbum"]) {
442 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
447 //Rebuild the upcoming songs submenu. Can be improved a lot.
448 - (void)rebuildUpcomingSongsMenu
450 int curIndex = [currentRemote currentPlaylistIndex];
451 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
452 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
454 if (!isPlayingRadio) {
456 int curTrack = [currentRemote currentSongIndex];
459 [upcomingSongsMenu release];
460 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
461 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
462 [upcomingSongsItem setEnabled:YES];
464 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
466 NSString *curSong = [currentRemote songTitleAtIndex:i];
467 NSMenuItem *songItem;
468 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
469 [songItem setTarget:self];
470 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
471 [upcomingSongsMenu addItem:songItem];
479 [upcomingSongsItem setSubmenu:nil];
480 [upcomingSongsItem setEnabled:NO];
484 - (void)rebuildPlaylistMenu
486 NSArray *playlists = [currentRemote playlists];
487 int i, curPlaylist = [currentRemote currentPlaylistIndex];
489 if (isPlayingRadio) {
492 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
495 [playlistMenu release];
496 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
498 for (i = 0; i < [playlists count]; i++) {
499 NSString *playlistName = [playlists objectAtIndex:i];
500 NSMenuItem *tempItem;
501 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
502 [tempItem setTarget:self];
503 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
504 [playlistMenu addItem:tempItem];
507 [playlistItem setSubmenu:playlistMenu];
508 [playlistItem setEnabled:YES];
511 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
515 //Build a menu with the list of all available EQ presets
516 - (void)rebuildEQPresetsMenu
518 NSArray *eqPresets = [currentRemote eqPresets];
519 NSMenuItem *enabledItem;
522 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
526 eqMenu = [[NSMenu alloc] initWithTitle:@""];
528 enabledItem = [eqMenu addItemWithTitle:@"EQ Enabled"
531 [eqMenu addItem:[NSMenuItem separatorItem]];
533 for (i = 0; i < [eqPresets count]; i++) {
534 NSString *setName = [eqPresets objectAtIndex:i];
535 NSMenuItem *tempItem;
537 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
538 [tempItem setTarget:self];
539 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
540 [eqMenu addItem:tempItem];
544 [eqItem setSubmenu:eqMenu];
546 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
551 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
552 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
553 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
554 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
555 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
560 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
562 if ([defaults objectForKey:@"PlayPause"] != nil) {
563 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
564 combo:[defaults keyComboForKey:@"PlayPause"]
565 target:self action:@selector(playPause:)];
568 if ([defaults objectForKey:@"NextTrack"] != nil) {
569 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
570 combo:[defaults keyComboForKey:@"NextTrack"]
571 target:self action:@selector(nextSong:)];
574 if ([defaults objectForKey:@"PrevTrack"] != nil) {
575 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
576 combo:[defaults keyComboForKey:@"PrevTrack"]
577 target:self action:@selector(prevSong:)];
580 if ([defaults objectForKey:@"TrackInfo"] != nil) {
581 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
582 combo:[defaults keyComboForKey:@"TrackInfo"]
583 target:self action:@selector(showCurrentTrackInfo)];
586 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
587 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
588 combo:[defaults keyComboForKey:@"UpcomingSongs"]
589 target:self action:@selector(showUpcomingSongs)];
593 //Called when the timer fires.
596 int playlist = [currentRemote currentPlaylistIndex];
597 ITMTRemotePlayerState playerState = [currentRemote playerState];
599 if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
600 int trackPlayingIndex = [currentRemote currentSongIndex];
602 if (trackPlayingIndex != lastSongIndex) {
603 BOOL wasPlayingRadio = isPlayingRadio;
604 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
606 if (isPlayingRadio && !wasPlayingRadio) {
608 for (i = 0; i < [playlistMenu numberOfItems]; i++)
610 [[playlistMenu itemAtIndex:i] setState:NSOffState];
613 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
616 if (wasPlayingRadio) {
617 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
618 [menu insertItem:temp atIndex:trackInfoIndex + 1];
623 lastSongIndex = trackPlayingIndex;
625 if (playlist != lastPlaylistIndex) {
626 BOOL wasPlayingRadio = isPlayingRadio;
627 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
629 if (isPlayingRadio && !wasPlayingRadio) {
631 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
632 [[playlistMenu itemAtIndex:i] setState:NSOffState];
636 if (wasPlayingRadio) {
637 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
638 [menu insertItem:temp atIndex:trackInfoIndex + 1];
642 if (!isPlayingRadio) {
644 for (i = 0; i < [playlistMenu numberOfItems]; i++)
646 [[playlistMenu itemAtIndex:i] setState:NSOffState];
648 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
652 lastSongIndex = trackPlayingIndex;
653 lastPlaylistIndex = playlist;
656 //Update Play/Pause menu item
657 if (playPauseMenuItem){
658 if (playerState == ITMTRemotePlayerPlaying) {
659 [playPauseMenuItem setTitle:@"Pause"];
661 [playPauseMenuItem setTitle:@"Play"];
664 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
665 NSMenuItem *menuItem;
666 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
667 //Remote the now playing item and add no song item
668 [menu removeItemAtIndex:trackInfoIndex];
670 if ([defaults boolForKey:@"showName"] == YES) {
671 [menu removeItemAtIndex:trackInfoIndex];
674 if ([defaults boolForKey:@"showTime"] == YES) {
675 [menu removeItemAtIndex:trackInfoIndex];
678 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
679 [menu removeItemAtIndex:trackInfoIndex];
682 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
683 [menu removeItemAtIndex:trackInfoIndex];
686 [playPauseMenuItem setTitle:@"Play"];
688 didHaveArtistName = NO;
689 didHaveAlbumName = NO;
690 lastPlaylistIndex = -1;
693 [upcomingSongsItem setSubmenu:nil];
694 [upcomingSongsItem setEnabled:NO];
696 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
697 [menu insertItem:menuItem atIndex:trackInfoIndex];
702 - (void)remotePlayerLaunched:(NSNotification *)note
704 isAppRunning = ITMTRemotePlayerRunning;
707 [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
709 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
711 [self rebuildPlaylistMenu];
714 [self rebuildEQPresetsMenu];
716 [statusItem setMenu:menu]; //Set the menu back to the main one
719 - (void)runTimerInNewThread
721 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
722 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
723 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain];
728 - (void)remotePlayerTerminated:(NSNotification *)note
730 isAppRunning = ITMTRemotePlayerNotRunning;
733 menu = [[NSMenu alloc] initWithTitle:@""];
734 [menu addItemWithTitle:@"Audio Player" action:NULL keyEquivalent:@""];
735 [menu addItemWithTitle:@"Not Running" action:NULL keyEquivalent:@""];
736 [menu addItem:[NSMenuItem separatorItem]];
737 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
738 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
739 [statusItem setMenu:menu];
741 [refreshTimer invalidate];
742 [refreshTimer release];
749 // Selectors - called from status item menu
753 // Plugin dependent selectors
755 - (void)playTrack:(id)sender
757 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
761 - (void)selectPlaylist:(id)sender
763 int playlist = [[sender representedObject] intValue];
764 if (!isPlayingRadio) {
765 int curPlaylist = [currentRemote currentPlaylistIndex];
766 if (curPlaylist > 0) {
767 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
770 [currentRemote switchToPlaylistAtIndex:playlist];
771 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
775 - (void)selectEQPreset:(id)sender
777 int curSet = [currentRemote currentEQPresetIndex];
778 int item = [[sender representedObject] intValue];
779 [currentRemote switchToEQAtIndex:item];
780 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
781 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
784 - (void)playPause:(id)sender
786 ITMTRemotePlayerState state = [currentRemote playerState];
788 if (state == ITMTRemotePlayerPlaying) {
789 [currentRemote pause];
790 [playPauseMenuItem setTitle:@"Play"];
791 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
792 [currentRemote pause];
793 [currentRemote play];
795 [currentRemote play];
796 [playPauseMenuItem setTitle:@"Pause"];
800 - (void)nextSong:(id)sender
802 [currentRemote goToNextSong];
805 - (void)prevSong:(id)sender
807 [currentRemote goToPreviousSong];
810 - (void)fastForward:(id)sender
812 [currentRemote forward];
813 [playPauseMenuItem setTitle:@"Play"];
816 - (void)rewind:(id)sender
818 [currentRemote rewind];
819 [playPauseMenuItem setTitle:@"Play"];
822 - (IBAction)setSongRating:(id)sender
824 NSLog(@"%f", (float)[sender tag] / 100.0);
825 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
830 // Plugin independent selectors
833 - (void)quitMenuTunes:(id)sender
835 [NSApp terminate:self];
838 - (void)showPreferences:(id)sender
840 if (!prefsController) {
841 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
846 - (void)closePreferences
848 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
851 [prefsController release];
852 prefsController = nil;
857 // Show Current Track Info And Show Upcoming Songs Floaters
861 - (void)showCurrentTrackInfo
863 NSString *trackName = [currentRemote currentSongTitle];
864 if (!statusWindow && [trackName length]) {
865 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
866 NSString *stringToShow = @"";
868 if ([defaults boolForKey:@"showName"]) {
869 if ([defaults boolForKey:@"showArtist"]) {
870 NSString *trackArtist = [currentRemote currentSongArtist];
871 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
873 stringToShow = [stringToShow stringByAppendingString:trackName];
874 stringToShow = [stringToShow stringByAppendingString:@"\n"];
877 if ([defaults boolForKey:@"showAlbum"]) {
878 NSString *trackAlbum = [currentRemote currentSongAlbum];
879 if ([trackAlbum length]) {
880 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
881 stringToShow = [stringToShow stringByAppendingString:@"\n"];
885 if ([defaults boolForKey:@"showTime"]) {
886 NSString *trackTime = [currentRemote currentSongLength];
887 if ([trackTime length]) {
888 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
893 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
894 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
896 stringToShow = [stringToShow stringByAppendingString:
897 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
899 stringToShow = [stringToShow stringByAppendingString:
900 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
904 [statusWindow setText:stringToShow];
905 [NSTimer scheduledTimerWithTimeInterval:3.0
907 selector:@selector(fadeAndCloseStatusWindow)
913 - (void)showUpcomingSongs
915 int curPlaylist = [currentRemote currentPlaylistIndex];
917 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
920 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
921 int curTrack = [currentRemote currentSongIndex];
923 NSString *songs = @"";
925 statusWindow = [ITTransientStatusWindow sharedWindow];
926 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
928 NSString *curSong = [currentRemote songTitleAtIndex:i];
929 songs = [songs stringByAppendingString:curSong];
930 songs = [songs stringByAppendingString:@"\n"];
933 [statusWindow setText:songs];
934 [NSTimer scheduledTimerWithTimeInterval:3.0
936 selector:@selector(fadeAndCloseStatusWindow)
943 - (void)fadeAndCloseStatusWindow
945 [statusWindow orderOut:self];
948 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
949 onItem:(NSMenuItem *)item
951 unichar charcode = 'a';
953 long cocoaModifiers = 0;
954 static long carbonToCocoa[6][2] =
956 { cmdKey, NSCommandKeyMask },
957 { optionKey, NSAlternateKeyMask },
958 { controlKey, NSControlKeyMask },
959 { shiftKey, NSShiftKeyMask },
962 for (i = 0; i < 6; i++) {
963 if (modifiers & carbonToCocoa[i][0]) {
964 cocoaModifiers += carbonToCocoa[i][1];
967 [item setKeyEquivalentModifierMask:cocoaModifiers];
969 //Missing key combos for some keys. Must find them later.
983 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
984 NSLog(@"%@", menuRef);
985 SetMenuItemCommandKey(menuRef, 0, NO, 49);
986 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
987 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
993 charcode = NSDeleteFunctionKey;
1009 charcode = NSF5FunctionKey;
1013 charcode = NSF6FunctionKey;
1017 charcode = NSF7FunctionKey;
1021 charcode = NSF3FunctionKey;
1025 charcode = NSF8FunctionKey;
1029 charcode = NSF9FunctionKey;
1033 charcode = NSF11FunctionKey;
1037 charcode = NSF3FunctionKey;
1041 charcode = NSF14FunctionKey;
1045 charcode = NSF10FunctionKey;
1049 charcode = NSF12FunctionKey;
1053 charcode = NSF13FunctionKey;
1057 charcode = NSInsertFunctionKey;
1061 charcode = NSHomeFunctionKey;
1065 charcode = NSPageUpFunctionKey;
1069 charcode = NSDeleteFunctionKey;
1073 charcode = NSF4FunctionKey;
1077 charcode = NSEndFunctionKey;
1081 charcode = NSF2FunctionKey;
1085 charcode = NSPageDownFunctionKey;
1089 charcode = NSF1FunctionKey;
1093 charcode = NSLeftArrowFunctionKey;
1097 charcode = NSRightArrowFunctionKey;
1101 charcode = NSDownArrowFunctionKey;
1105 charcode = NSUpArrowFunctionKey;
1109 if (charcode == 'a') {
1110 unsigned long state;
1115 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1116 keyTrans = KeyTranslate(kchr, code, &state);
1117 charCode = keyTrans;
1118 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1119 } else if (charcode != 'b') {
1120 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1124 /*************************************************************************/
1126 #pragma mark NSApplication DELEGATE METHODS
1127 /*************************************************************************/
1129 - (void)applicationWillTerminate:(NSNotification *)note
1131 [self clearHotKeys];
1132 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1136 /*************************************************************************/
1138 #pragma mark DEALLOCATION METHODS
1139 /*************************************************************************/
1144 [refreshTimer invalidate];
1145 [refreshTimer release];
1148 [currentRemote halt];
1149 [statusItem release];