1 #import "MainController.h"
2 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
4 #import "StatusWindow.h"
6 @interface MainController(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 MainController
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 playerRunningState] == 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 unichar fullstar = 0x2605;
284 unichar emptystar = 0x2606;
285 NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1];
286 NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1];
289 NSString *curTitle = [NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar];
291 songRatingMenuItem = [menu addItemWithTitle:@"Song Rating"
295 ratingMenu = [[NSMenu alloc] initWithTitle:@""];
297 item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar]
298 action:@selector(setSongRating:)
300 [item setTarget:self];
303 for (i = 1; i < 6; i++) {
304 curTitle = [curTitle substringToIndex:4];
305 curTitle = [fullStarChar stringByAppendingString:curTitle];
306 item = [ratingMenu addItemWithTitle:curTitle
307 action:@selector(setSongRating:)
309 [item setTarget:self];
310 [item setTag:(i * 20)];
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];
398 if (songRatingMenuItem) {
399 int rating = (int)[currentRemote currentSongRating] * 10;
401 for (i = 0; i < 5; i++) {
402 [[ratingMenu itemAtIndex:i] setState:NSOffState];
403 [[ratingMenu itemAtIndex:i] setTarget:self];
405 [[ratingMenu itemAtIndex:rating / 2] setState:NSOnState];
409 if ([defaults boolForKey:@"showName"]) {
410 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
413 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
418 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
419 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
420 [menu insertItem:menuItem atIndex:trackInfoIndex];
423 [songRatingMenuItem setSubmenu:ratingMenu];
424 [songRatingMenuItem setEnabled:YES];
426 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
427 [menu removeItemAtIndex:trackInfoIndex];
429 if ([defaults boolForKey:@"showName"] == YES) {
430 [menu removeItemAtIndex:trackInfoIndex];
433 if ([defaults boolForKey:@"showTime"] == YES) {
434 [menu removeItemAtIndex:trackInfoIndex];
437 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
438 [menu removeItemAtIndex:trackInfoIndex];
441 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
442 [menu removeItemAtIndex:trackInfoIndex];
445 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
446 [menu insertItem:menuItem atIndex:trackInfoIndex];
450 if ([defaults boolForKey:@"showArtist"]) {
451 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
454 if ([defaults boolForKey:@"showAlbum"]) {
455 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
461 //Rebuild the upcoming songs submenu. Can be improved a lot.
462 - (void)rebuildUpcomingSongsMenu
464 int curIndex = [currentRemote currentPlaylistIndex];
465 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
466 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
468 if (!isPlayingRadio) {
470 int curTrack = [currentRemote currentSongIndex];
473 [upcomingSongsMenu release];
474 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
475 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
476 [upcomingSongsItem setEnabled:YES];
478 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
480 NSString *curSong = [currentRemote songTitleAtIndex:i];
481 NSMenuItem *songItem;
482 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
483 [songItem setTarget:self];
484 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
485 [upcomingSongsMenu addItem:songItem];
493 [upcomingSongsItem setSubmenu:nil];
494 [upcomingSongsItem setEnabled:NO];
498 - (void)rebuildPlaylistMenu
500 NSArray *playlists = [currentRemote playlists];
501 int i, curPlaylist = [currentRemote currentPlaylistIndex];
503 if (isPlayingRadio) {
506 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
509 [playlistMenu release];
510 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
512 for (i = 0; i < [playlists count]; i++) {
513 NSString *playlistName = [playlists objectAtIndex:i];
514 NSMenuItem *tempItem;
515 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
516 [tempItem setTarget:self];
517 [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
518 [playlistMenu addItem:tempItem];
521 [playlistItem setSubmenu:playlistMenu];
522 [playlistItem setEnabled:YES];
525 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
529 //Build a menu with the list of all available EQ presets
530 - (void)rebuildEQPresetsMenu
532 NSArray *eqPresets = [currentRemote eqPresets];
533 NSMenuItem *enabledItem;
536 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
540 eqMenu = [[NSMenu alloc] initWithTitle:@""];
542 enabledItem = [eqMenu addItemWithTitle:@"Disabled"
543 action:@selector(toggleEqualizer)
546 if ([currentRemote equalizerEnabled] == NO) {
547 [enabledItem setState:NSOnState];
550 [eqMenu addItem:[NSMenuItem separatorItem]];
552 for (i = 0; i < [eqPresets count]; i++) {
553 NSString *setName = [eqPresets objectAtIndex:i];
554 NSMenuItem *tempItem;
556 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
557 [tempItem setTarget:self];
558 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
559 [eqMenu addItem:tempItem];
563 [eqItem setSubmenu:eqMenu];
565 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
570 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
571 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
572 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
573 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
574 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
579 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
581 if ([defaults objectForKey:@"PlayPause"] != nil) {
582 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
583 combo:[defaults keyComboForKey:@"PlayPause"]
584 target:self action:@selector(playPause:)];
587 if ([defaults objectForKey:@"NextTrack"] != nil) {
588 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
589 combo:[defaults keyComboForKey:@"NextTrack"]
590 target:self action:@selector(nextSong:)];
593 if ([defaults objectForKey:@"PrevTrack"] != nil) {
594 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
595 combo:[defaults keyComboForKey:@"PrevTrack"]
596 target:self action:@selector(prevSong:)];
599 if ([defaults objectForKey:@"TrackInfo"] != nil) {
600 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
601 combo:[defaults keyComboForKey:@"TrackInfo"]
602 target:self action:@selector(showCurrentTrackInfo)];
605 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
606 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
607 combo:[defaults keyComboForKey:@"UpcomingSongs"]
608 target:self action:@selector(showUpcomingSongs)];
612 //Called when the timer fires.
615 int playlist = [currentRemote currentPlaylistIndex];
616 ITMTRemotePlayerPlayingState playerPlayingState = [currentRemote playerPlayingState];
618 if ((playlist > 0) || playerPlayingState != ITMTRemotePlayerStopped) {
619 int trackPlayingIndex = [currentRemote currentSongIndex];
621 if (trackPlayingIndex != lastSongIndex) {
622 BOOL wasPlayingRadio = isPlayingRadio;
623 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
625 if (isPlayingRadio && !wasPlayingRadio) {
627 for (i = 0; i < [playlistMenu numberOfItems]; i++)
629 [[playlistMenu itemAtIndex:i] setState:NSOffState];
632 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
635 if (wasPlayingRadio) {
636 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
637 [menu insertItem:temp atIndex:trackInfoIndex + 1];
642 lastSongIndex = trackPlayingIndex;
644 if (playlist != lastPlaylistIndex) {
645 BOOL wasPlayingRadio = isPlayingRadio;
646 isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
648 if (isPlayingRadio && !wasPlayingRadio) {
650 for (i = 0; i < [playlistMenu numberOfItems]; i++) {
651 [[playlistMenu itemAtIndex:i] setState:NSOffState];
655 if (wasPlayingRadio) {
656 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
657 [menu insertItem:temp atIndex:trackInfoIndex + 1];
661 if (!isPlayingRadio) {
663 for (i = 0; i < [playlistMenu numberOfItems]; i++)
665 [[playlistMenu itemAtIndex:i] setState:NSOffState];
667 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
671 lastSongIndex = trackPlayingIndex;
672 lastPlaylistIndex = playlist;
675 //Update Play/Pause menu item
676 if (playPauseMenuItem){
677 if (playerPlayingState == ITMTRemotePlayerPlaying) {
678 [playPauseMenuItem setTitle:@"Pause"];
680 [playPauseMenuItem setTitle:@"Play"];
683 } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
684 NSMenuItem *menuItem;
685 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
686 //Remote the now playing item and add no song item
687 [menu removeItemAtIndex:trackInfoIndex];
689 if ([defaults boolForKey:@"showName"] == YES) {
690 [menu removeItemAtIndex:trackInfoIndex];
693 if ([defaults boolForKey:@"showTime"] == YES) {
694 [menu removeItemAtIndex:trackInfoIndex];
697 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
698 [menu removeItemAtIndex:trackInfoIndex];
701 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
702 [menu removeItemAtIndex:trackInfoIndex];
705 [playPauseMenuItem setTitle:@"Play"];
707 didHaveArtistName = NO;
708 didHaveAlbumName = NO;
709 lastPlaylistIndex = -1;
712 [upcomingSongsItem setSubmenu:nil];
713 [upcomingSongsItem setEnabled:NO];
715 [songRatingMenuItem setSubmenu:nil];
716 [songRatingMenuItem setEnabled:NO];
718 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
719 [menu insertItem:menuItem atIndex:trackInfoIndex];
724 - (void)remotePlayerLaunched:(NSNotification *)note
726 isAppRunning = ITMTRemotePlayerRunning;
729 [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
731 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
733 [self rebuildPlaylistMenu];
736 [self rebuildEQPresetsMenu];
738 [statusItem setMenu:menu]; //Set the menu back to the main one
741 - (void)runTimerInNewThread
743 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
744 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
745 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain];
750 - (void)remotePlayerTerminated:(NSNotification *)note
752 isAppRunning = ITMTRemotePlayerNotRunning;
755 menu = [[NSMenu alloc] initWithTitle:@""];
756 [[menu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self];
757 [menu addItem:[NSMenuItem separatorItem]];
758 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
759 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
760 [statusItem setMenu:menu];
762 [refreshTimer invalidate];
763 [refreshTimer release];
770 // Selectors - called from status item menu
774 // Plugin dependent selectors
776 - (void)playTrack:(id)sender
778 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
781 - (void)selectPlaylist:(id)sender
783 int playlist = [[sender representedObject] intValue];
784 if (!isPlayingRadio) {
785 int curPlaylist = [currentRemote currentPlaylistIndex];
786 if (curPlaylist > 0) {
787 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
790 [currentRemote switchToPlaylistAtIndex:playlist];
791 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
795 - (void)selectEQPreset:(id)sender
797 int curSet = [currentRemote currentEQPresetIndex];
798 int item = [[sender representedObject] intValue];
799 [currentRemote switchToEQAtIndex:item];
800 [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState];
801 [[eqMenu itemAtIndex:item + 2] setState:NSOnState];
804 - (void)playPause:(id)sender
806 ITMTRemotePlayerPlayingState state = [currentRemote playerPlayingState];
808 if (state == ITMTRemotePlayerPlaying) {
809 [currentRemote pause];
810 [playPauseMenuItem setTitle:@"Play"];
811 } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
812 [currentRemote pause];
813 [currentRemote play];
815 [currentRemote play];
816 [playPauseMenuItem setTitle:@"Pause"];
820 - (void)nextSong:(id)sender
822 [currentRemote goToNextSong];
825 - (void)prevSong:(id)sender
827 [currentRemote goToPreviousSong];
830 - (void)fastForward:(id)sender
832 [currentRemote forward];
833 [playPauseMenuItem setTitle:@"Play"];
836 - (void)rewind:(id)sender
838 [currentRemote rewind];
839 [playPauseMenuItem setTitle:@"Play"];
842 - (void)toggleEqualizer
844 [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
847 - (void)setSongRating:(id)sender
849 NSLog(@"%f", [currentRemote currentSongRating]);
850 NSLog(@"%f", (float)[sender tag] / 100.0);
851 [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
856 // Plugin independent selectors
859 - (void)quitMenuTunes:(id)sender
861 [NSApp terminate:self];
864 - (void)showPreferences:(id)sender
866 if (!prefsController) {
867 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
872 - (void)closePreferences
874 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
877 [prefsController release];
878 prefsController = nil;
881 - (void)showPlayer:(id)sender
883 if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
884 [currentRemote showPrimaryInterface];
886 if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) {
887 NSLog(@"Error Launching Player");
895 // Show Current Track Info And Show Upcoming Songs Floaters
899 - (void)showCurrentTrackInfo
901 NSString *trackName = [currentRemote currentSongTitle];
902 if (!statusWindow && [trackName length]) {
903 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
904 NSString *stringToShow = @"";
906 if ([defaults boolForKey:@"showName"]) {
907 if ([defaults boolForKey:@"showArtist"]) {
908 NSString *trackArtist = [currentRemote currentSongArtist];
909 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
911 stringToShow = [stringToShow stringByAppendingString:trackName];
912 stringToShow = [stringToShow stringByAppendingString:@"\n"];
915 if ([defaults boolForKey:@"showAlbum"]) {
916 NSString *trackAlbum = [currentRemote currentSongAlbum];
917 if ([trackAlbum length]) {
918 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
919 stringToShow = [stringToShow stringByAppendingString:@"\n"];
923 if ([defaults boolForKey:@"showTime"]) {
924 NSString *trackTime = [currentRemote currentSongLength];
925 if ([trackTime length]) {
926 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
931 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
932 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
934 stringToShow = [stringToShow stringByAppendingString:
935 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
937 stringToShow = [stringToShow stringByAppendingString:
938 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
942 [statusWindow setText:stringToShow];
943 [NSTimer scheduledTimerWithTimeInterval:3.0
945 selector:@selector(fadeAndCloseStatusWindow)
951 - (void)showUpcomingSongs
953 int curPlaylist = [currentRemote currentPlaylistIndex];
955 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
958 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
959 int curTrack = [currentRemote currentSongIndex];
961 NSString *songs = @"";
963 statusWindow = [ITTransientStatusWindow sharedWindow];
964 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
966 NSString *curSong = [currentRemote songTitleAtIndex:i];
967 songs = [songs stringByAppendingString:curSong];
968 songs = [songs stringByAppendingString:@"\n"];
971 [statusWindow setText:songs];
972 [NSTimer scheduledTimerWithTimeInterval:3.0
974 selector:@selector(fadeAndCloseStatusWindow)
981 - (void)fadeAndCloseStatusWindow
983 [statusWindow orderOut:self];
986 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
987 onItem:(NSMenuItem *)item
989 unichar charcode = 'a';
991 long cocoaModifiers = 0;
992 static long carbonToCocoa[6][2] =
994 { cmdKey, NSCommandKeyMask },
995 { optionKey, NSAlternateKeyMask },
996 { controlKey, NSControlKeyMask },
997 { shiftKey, NSShiftKeyMask },
1000 for (i = 0; i < 6; i++) {
1001 if (modifiers & carbonToCocoa[i][0]) {
1002 cocoaModifiers += carbonToCocoa[i][1];
1005 [item setKeyEquivalentModifierMask:cocoaModifiers];
1007 //Missing key combos for some keys. Must find them later.
1021 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
1022 NSLog(@"%@", menuRef);
1023 SetMenuItemCommandKey(menuRef, 0, NO, 49);
1024 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
1025 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
1031 charcode = NSDeleteFunctionKey;
1047 charcode = NSF5FunctionKey;
1051 charcode = NSF6FunctionKey;
1055 charcode = NSF7FunctionKey;
1059 charcode = NSF3FunctionKey;
1063 charcode = NSF8FunctionKey;
1067 charcode = NSF9FunctionKey;
1071 charcode = NSF11FunctionKey;
1075 charcode = NSF3FunctionKey;
1079 charcode = NSF14FunctionKey;
1083 charcode = NSF10FunctionKey;
1087 charcode = NSF12FunctionKey;
1091 charcode = NSF13FunctionKey;
1095 charcode = NSInsertFunctionKey;
1099 charcode = NSHomeFunctionKey;
1103 charcode = NSPageUpFunctionKey;
1107 charcode = NSDeleteFunctionKey;
1111 charcode = NSF4FunctionKey;
1115 charcode = NSEndFunctionKey;
1119 charcode = NSF2FunctionKey;
1123 charcode = NSPageDownFunctionKey;
1127 charcode = NSF1FunctionKey;
1131 charcode = NSLeftArrowFunctionKey;
1135 charcode = NSRightArrowFunctionKey;
1139 charcode = NSDownArrowFunctionKey;
1143 charcode = NSUpArrowFunctionKey;
1147 if (charcode == 'a') {
1148 unsigned long state;
1153 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1154 keyTrans = KeyTranslate(kchr, code, &state);
1155 charCode = keyTrans;
1156 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1157 } else if (charcode != 'b') {
1158 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1162 /*************************************************************************/
1164 #pragma mark NSApplication DELEGATE METHODS
1165 /*************************************************************************/
1167 - (void)applicationWillTerminate:(NSNotification *)note
1169 [self clearHotKeys];
1170 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1174 /*************************************************************************/
1176 #pragma mark DEALLOCATION METHODS
1177 /*************************************************************************/
1182 [refreshTimer invalidate];
1183 [refreshTimer release];
1186 [currentRemote halt];
1187 [statusItem release];