X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/0f0fcccca7516fef317716f61356ebb6bb07c084..8fe29f5b163e9acbda7b317b406a5835bac8a95d:/OldMainController.m?ds=inline diff --git a/OldMainController.m b/OldMainController.m index 23a4be4..ff98908 100755 --- a/OldMainController.m +++ b/OldMainController.m @@ -20,7 +20,7 @@ /*************************************************************************/ #pragma mark - -#pragma mark INITIALIZATION METHODS +#pragma mark INITIALIZATION/DEALLOCATION METHODS /*************************************************************************/ - (id)init @@ -32,33 +32,62 @@ return self; } +- (void)dealloc +{ + if (refreshTimer) { + [refreshTimer invalidate]; + [refreshTimer release]; + refreshTimer = nil; + } + [currentRemote halt]; + [statusItem release]; + [menu release]; + [super dealloc]; +} + - (void)applicationDidFinishLaunching:(NSNotification *)note { currentRemote = [self loadRemote]; [currentRemote begin]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil]; + //Setup for notification of the remote player launching or quitting + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(applicationTerminated:) + name:NSWorkspaceDidTerminateApplicationNotification + object:nil]; - [self registerDefaultsIfNeeded]; + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(applicationLaunched:) + name:NSWorkspaceDidLaunchApplicationNotification + object:nil]; - menu = [[NSMenu alloc] initWithTitle:@""]; + [self registerDefaults]; + statusItem = [[ITStatusItem alloc] + initWithStatusBar:[NSStatusBar systemStatusBar] + withLength:NSSquareStatusItemLength]; + + menu = [[NSMenu alloc] initWithTitle:@""]; if ( ( [currentRemote playerRunningState] == ITMTRemotePlayerRunning ) ) { - [self remotePlayerLaunched:nil]; + [self applicationLaunched:nil]; } else { - [self remotePlayerTerminated:nil]; + [self applicationTerminated:nil]; } - statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar] withLength:NSSquareStatusItemLength]; - [statusItem setImage:[NSImage imageNamed:@"menu"]]; [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]]; - [statusItem setMenu:menu]; // Below line of code is for creating builds for Beta Testers // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]]; } +- (void)applicationWillTerminate:(NSNotification *)note +{ + [self clearHotKeys]; + [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; +} + - (ITMTRemote *)loadRemote { NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath]; @@ -94,13 +123,49 @@ return [remoteArray objectAtIndex:0]; } +// +// + +- (void)applicationLaunched:(NSNotification *)note +{ + if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) { + [NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil]; + + [self rebuildMenu]; + [statusItem setMenu:menu]; + [self setupHotKeys]; + isAppRunning = ITMTRemotePlayerRunning; + return; + } + + isAppRunning = ITMTRemotePlayerRunning; +} + +- (void)applicationTerminated:(NSNotification *)note +{ + if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) { + NSMenu *notRunningMenu = [[NSMenu alloc] initWithTitle:@""]; + [[notRunningMenu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self]; + [notRunningMenu addItem:[NSMenuItem separatorItem]]; + [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self]; + [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self]; + [statusItem setMenu:[notRunningMenu autorelease]]; + + [refreshTimer invalidate]; + [refreshTimer release]; + refreshTimer = nil; + [self clearHotKeys]; + isAppRunning = NO; + return; + } +} /*************************************************************************/ #pragma mark - #pragma mark INSTANCE METHODS /*************************************************************************/ -- (void)registerDefaultsIfNeeded +- (void)registerDefaults { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (![defaults objectForKey:@"menu"]) { @@ -133,7 +198,8 @@ for (i = 0; i < [loginarray count]; i++) { NSDictionary *tempDict = [loginarray objectAtIndex:i]; - if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) { + if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString: + [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) { found = YES; } } @@ -182,6 +248,19 @@ } } +- (void)startTimerInNewThread +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; + refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5 + target:self + selector:@selector(timerUpdate) + userInfo:nil + repeats:YES] retain]; + [runLoop run]; + [pool release]; +} + //Recreate the status item menu - (void)rebuildMenu { @@ -215,7 +294,6 @@ playPauseMenuItem = [menu addItemWithTitle:@"Play" action:@selector(playPause:) keyEquivalent:@""]; - [playPauseMenuItem setTarget:self]; if (tempCombo) { [self setKeyEquivalentForCode:[tempCombo keyCode] @@ -228,7 +306,6 @@ action:@selector(nextSong:) keyEquivalent:@""]; - [nextTrack setTarget:self]; if (tempCombo) { [self setKeyEquivalentForCode:[tempCombo keyCode] andModifiers:[tempCombo modifiers] onItem:nextTrack]; @@ -240,20 +317,19 @@ action:@selector(prevSong:) keyEquivalent:@""]; - [prevTrack setTarget:self]; if (tempCombo) { [self setKeyEquivalentForCode:[tempCombo keyCode] andModifiers:[tempCombo modifiers] onItem:prevTrack]; [tempCombo release]; } } else if ([item isEqualToString:@"Fast Forward"]) { - [[menu addItemWithTitle:@"Fast Forward" + [menu addItemWithTitle:@"Fast Forward" action:@selector(fastForward:) - keyEquivalent:@""] setTarget:self]; + keyEquivalent:@""]; } else if ([item isEqualToString:@"Rewind"]) { - [[menu addItemWithTitle:@"Rewind" + [menu addItemWithTitle:@"Rewind" action:@selector(rewind:) - keyEquivalent:@""] setTarget:self]; + keyEquivalent:@""]; } else if ([item isEqualToString:@"Upcoming Songs"]) { upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs" action:nil @@ -267,13 +343,13 @@ action:nil keyEquivalent:@""]; } else if ([item isEqualToString:@"PreferencesÉ"]) { - [[menu addItemWithTitle:@"PreferencesÉ" + [menu addItemWithTitle:@"PreferencesÉ" action:@selector(showPreferences:) - keyEquivalent:@""] setTarget:self]; + keyEquivalent:@""]; } else if ([item isEqualToString:@"Quit"]) { - [[menu addItemWithTitle:@"Quit" + [menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) - keyEquivalent:@""] setTarget:self]; + keyEquivalent:@""]; } else if ([item isEqualToString:@"Current Track Info"]) { trackInfoIndex = [menu numberOfItems]; [menu addItemWithTitle:@"No Song" @@ -285,8 +361,6 @@ NSString *fullStarChar = [NSString stringWithCharacters:&fullstar length:1]; NSString *emptyStarChar = [NSString stringWithCharacters:&emptystar length:1]; NSMenuItem *item; - int i; - NSString *curTitle = [NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar]; songRatingMenuItem = [menu addItemWithTitle:@"Song Rating" action:nil @@ -294,170 +368,35 @@ ratingMenu = [[NSMenu alloc] initWithTitle:@""]; - item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] - action:@selector(setSongRating:) - keyEquivalent:@""]; - [item setTarget:self]; + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; [item setTag:0]; - for (i = 1; i < 6; i++) { - curTitle = [curTitle substringToIndex:4]; - curTitle = [fullStarChar stringByAppendingString:curTitle]; - item = [ratingMenu addItemWithTitle:curTitle - action:@selector(setSongRating:) - keyEquivalent:@""]; - [item setTarget:self]; - [item setTag:(i * 20)]; - } + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; + [item setTag:20]; + + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; + [item setTag:40]; + + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; + [item setTag:60]; + + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; + [item setTag:80]; + + item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""]; + [item setTag:100]; } else if ([item isEqualToString:@""]) { [menu addItem:[NSMenuItem separatorItem]]; } } - [self timerUpdate]; //Updates dynamic info in the menu + [self updateMenu]; + //[self timerUpdate]; //Updates dynamic info in the menu [self clearHotKeys]; [self setupHotKeys]; } -//Updates the menu with current player state, song, and upcoming songs -- (void)updateMenu -{ - NSMenuItem *menuItem; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - - if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) { - return; - } - - if (upcomingSongsItem) { - [self rebuildUpcomingSongsMenu]; - } - - if (playlistItem) { - [self rebuildPlaylistMenu]; - } - - if (eqItem) { - [self rebuildEQPresetsMenu]; - } - if (trackInfoIndex > -1) { - NSString *curSongName, *curAlbumName = @"", *curArtistName = @""; - curSongName = [currentRemote currentSongTitle]; - - if ([defaults boolForKey:@"showAlbum"]) { - curAlbumName = [currentRemote currentSongAlbum]; - } - - if ([defaults boolForKey:@"showArtist"]) { - curArtistName = [currentRemote currentSongArtist]; - } - - if ([curSongName length] > 0) { - int index = [menu indexOfItemWithTitle:@"Now Playing"]; - if (index > -1) { - if ([defaults boolForKey:@"showName"]) { - [menu removeItemAtIndex:index + 1]; - } - if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) { - [menu removeItemAtIndex:index + 1]; - } - if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) { - [menu removeItemAtIndex:index + 1]; - } - if ([defaults boolForKey:@"showTime"]) { - [menu removeItemAtIndex:index + 1]; - } - } - - if (!isPlayingRadio) { - if ([defaults boolForKey:@"showTime"]) { - menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]] - action:nil - keyEquivalent:@""]; - [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; - [menuItem release]; - } - - if ([curArtistName length] > 0) { - menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName] - action:nil - keyEquivalent:@""]; - [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; - [menuItem release]; - } - - if ([curAlbumName length] > 0) { - menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName] - action:nil - keyEquivalent:@""]; - [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; - [menuItem release]; - } - - if (songRatingMenuItem) { - int rating = (int)[currentRemote currentSongRating] * 10; - int i; - for (i = 0; i < 5; i++) { - [[ratingMenu itemAtIndex:i] setState:NSOffState]; - [[ratingMenu itemAtIndex:i] setTarget:self]; - } - [[ratingMenu itemAtIndex:rating / 2] setState:NSOnState]; - } - } - - if ([defaults boolForKey:@"showName"]) { - menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName] - action:nil - keyEquivalent:@""]; - [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; - [menuItem release]; - } - - if (index == -1) { - menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""]; - [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]]; - [menu insertItem:menuItem atIndex:trackInfoIndex]; - [menuItem release]; - - [songRatingMenuItem setSubmenu:ratingMenu]; - [songRatingMenuItem setEnabled:YES]; - } - } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) { - [menu removeItemAtIndex:trackInfoIndex]; - - if ([defaults boolForKey:@"showName"] == YES) { - [menu removeItemAtIndex:trackInfoIndex]; - } - - if ([defaults boolForKey:@"showTime"] == YES) { - [menu removeItemAtIndex:trackInfoIndex]; - } - - if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) { - [menu removeItemAtIndex:trackInfoIndex]; - } - - if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) { - [menu removeItemAtIndex:trackInfoIndex]; - } - - menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""]; - [menu insertItem:menuItem atIndex:trackInfoIndex]; - [menuItem release]; - } - - if ([defaults boolForKey:@"showArtist"]) { - didHaveArtistName = (([curArtistName length] > 0) ? YES : NO); - } - - if ([defaults boolForKey:@"showAlbum"]) { - didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO); - } - } - [menu update]; -} - //Rebuild the upcoming songs submenu. Can be improved a lot. - (void)rebuildUpcomingSongsMenu { @@ -479,8 +418,7 @@ if (i <= numSongs) { NSString *curSong = [currentRemote songTitleAtIndex:i]; NSMenuItem *songItem; - songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""]; - [songItem setTarget:self]; + songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(selectSong:) keyEquivalent:@""]; [songItem setRepresentedObject:[NSNumber numberWithInt:i]]; [upcomingSongsMenu addItem:songItem]; [songItem release]; @@ -513,7 +451,6 @@ NSString *playlistName = [playlists objectAtIndex:i]; NSMenuItem *tempItem; tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""]; - [tempItem setTarget:self]; [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]]; [playlistMenu addItem:tempItem]; [tempItem release]; @@ -539,11 +476,12 @@ [eqMenu release]; eqMenu = [[NSMenu alloc] initWithTitle:@""]; - enabledItem = [eqMenu addItemWithTitle:@"Disabled" - action:@selector(toggleEqualizer) + enabledItem = [eqMenu addItemWithTitle:@"Enabled" + action:@selector(selectEQPreset:) keyEquivalent:@""]; + [enabledItem setTag:-1]; - if ([currentRemote equalizerEnabled] == NO) { + if ([currentRemote equalizerEnabled]) { [enabledItem setState:NSOnState]; } @@ -554,8 +492,7 @@ NSMenuItem *tempItem; if (setName) { tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""]; - [tempItem setTarget:self]; - [tempItem setRepresentedObject:[NSNumber numberWithInt:i]]; + [tempItem setTag:i]; [eqMenu addItem:tempItem]; [tempItem release]; } @@ -565,57 +502,13 @@ [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState]; } -- (void)clearHotKeys -{ - [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"]; - [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"]; - [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"]; - [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"]; - [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"]; -} - -- (void)setupHotKeys -{ - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - - if ([defaults objectForKey:@"PlayPause"] != nil) { - [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause" - combo:[defaults keyComboForKey:@"PlayPause"] - target:self action:@selector(playPause:)]; - } - - if ([defaults objectForKey:@"NextTrack"] != nil) { - [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack" - combo:[defaults keyComboForKey:@"NextTrack"] - target:self action:@selector(nextSong:)]; - } - - if ([defaults objectForKey:@"PrevTrack"] != nil) { - [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack" - combo:[defaults keyComboForKey:@"PrevTrack"] - target:self action:@selector(prevSong:)]; - } - - if ([defaults objectForKey:@"TrackInfo"] != nil) { - [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo" - combo:[defaults keyComboForKey:@"TrackInfo"] - target:self action:@selector(showCurrentTrackInfo)]; - } - - if ([defaults objectForKey:@"UpcomingSongs"] != nil) { - [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs" - combo:[defaults keyComboForKey:@"UpcomingSongs"] - target:self action:@selector(showUpcomingSongs)]; - } -} - //Called when the timer fires. - (void)timerUpdate { int playlist = [currentRemote currentPlaylistIndex]; - ITMTRemotePlayerPlayingState playerPlayingState = [currentRemote playerPlayingState]; + ITMTRemotePlayerPlayingState playerState = [currentRemote playerPlayingState]; - if ((playlist > 0) || playerPlayingState != ITMTRemotePlayerStopped) { + if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) { int trackPlayingIndex = [currentRemote currentSongIndex]; if (trackPlayingIndex != lastSongIndex) { @@ -637,7 +530,6 @@ [menu insertItem:temp atIndex:trackInfoIndex + 1]; [temp release]; } - [self updateMenu]; lastSongIndex = trackPlayingIndex; } else { @@ -674,7 +566,7 @@ } //Update Play/Pause menu item if (playPauseMenuItem){ - if (playerPlayingState == ITMTRemotePlayerPlaying) { + if (playerState == ITMTRemotePlayerPlaying) { [playPauseMenuItem setTitle:@"Pause"]; } else { [playPauseMenuItem setTitle:@"Play"]; @@ -685,7 +577,6 @@ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //Remote the now playing item and add no song item [menu removeItemAtIndex:trackInfoIndex]; - if ([defaults boolForKey:@"showName"] == YES) { [menu removeItemAtIndex:trackInfoIndex]; } @@ -721,59 +612,151 @@ } } -- (void)remotePlayerLaunched:(NSNotification *)note +//Updates the menu with current player state, song, and upcoming songs +- (void)updateMenu { - isAppRunning = ITMTRemotePlayerRunning; + NSMenuItem *menuItem; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) { + return; + } - //Restart the timer - [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil]; + if (upcomingSongsItem) { + [self rebuildUpcomingSongsMenu]; + } - [self rebuildMenu]; //Rebuild the menu since no songs will be playing if (playlistItem) { [self rebuildPlaylistMenu]; } + if (eqItem) { [self rebuildEQPresetsMenu]; } - [statusItem setMenu:menu]; //Set the menu back to the main one -} - -- (void)runTimerInNewThread -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; - refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain]; - [runLoop run]; - [pool release]; -} - -- (void)remotePlayerTerminated:(NSNotification *)note -{ - isAppRunning = ITMTRemotePlayerNotRunning; - - [menu release]; - menu = [[NSMenu alloc] initWithTitle:@""]; - [[menu addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""] setTarget:self]; - [menu addItem:[NSMenuItem separatorItem]]; - [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self]; - [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self]; - [statusItem setMenu:menu]; - - [refreshTimer invalidate]; - [refreshTimer release]; - refreshTimer = nil; - [self clearHotKeys]; + if (trackInfoIndex > -1) { + NSString *curSongName, *curAlbumName = @"", *curArtistName = @""; + curSongName = [currentRemote currentSongTitle]; + + if ([defaults boolForKey:@"showAlbum"]) { + curAlbumName = [currentRemote currentSongAlbum]; + } + + if ([defaults boolForKey:@"showArtist"]) { + curArtistName = [currentRemote currentSongArtist]; + } + + if ([curSongName length] > 0) { + int index = [menu indexOfItemWithTitle:@"Now Playing"]; + if (index > -1) { + if ([defaults boolForKey:@"showName"]) { + [menu removeItemAtIndex:index + 1]; + } + if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) { + [menu removeItemAtIndex:index + 1]; + } + if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) { + [menu removeItemAtIndex:index + 1]; + } + if ([defaults boolForKey:@"showTime"]) { + [menu removeItemAtIndex:index + 1]; + } + } + + if (!isPlayingRadio) { + if ([defaults boolForKey:@"showTime"]) { + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]] + action:nil + keyEquivalent:@""]; + [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; + [menuItem release]; + } + + if ([curArtistName length] > 0) { + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName] + action:nil + keyEquivalent:@""]; + [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; + [menuItem release]; + } + + if ([curAlbumName length] > 0) { + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName] + action:nil + keyEquivalent:@""]; + [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; + [menuItem release]; + } + + if (songRatingMenuItem) { + int rating = (int)[currentRemote currentSongRating] * 10; + int i; + for (i = 0; i < 5; i++) { + [[ratingMenu itemAtIndex:i] setState:NSOffState]; + [[ratingMenu itemAtIndex:i] setTarget:self]; + } + [[ratingMenu itemAtIndex:rating / 2] setState:NSOnState]; + } + } + + if ([defaults boolForKey:@"showName"]) { + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName] + action:nil + keyEquivalent:@""]; + [menu insertItem:menuItem atIndex:trackInfoIndex + 1]; + [menuItem release]; + } + + if (index == -1) { + menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""]; + [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]]; + [menu insertItem:menuItem atIndex:trackInfoIndex]; + [menuItem release]; + + [songRatingMenuItem setSubmenu:ratingMenu]; + [songRatingMenuItem setEnabled:YES]; + } + } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) { + [menu removeItemAtIndex:trackInfoIndex]; + + if ([defaults boolForKey:@"showName"] == YES) { + [menu removeItemAtIndex:trackInfoIndex]; + } + + if ([defaults boolForKey:@"showTime"] == YES) { + [menu removeItemAtIndex:trackInfoIndex]; + } + + if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) { + [menu removeItemAtIndex:trackInfoIndex]; + } + + if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) { + [menu removeItemAtIndex:trackInfoIndex]; + } + + menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""]; + [menu insertItem:menuItem atIndex:trackInfoIndex]; + [menuItem release]; + } + + if ([defaults boolForKey:@"showArtist"]) { + didHaveArtistName = (([curArtistName length] > 0) ? YES : NO); + } + + if ([defaults boolForKey:@"showAlbum"]) { + didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO); + } + } + [menu update]; } // // -// Selectors - called from status item menu +// Menu Selectors // // -// Plugin dependent selectors - -- (void)playTrack:(id)sender +- (void)selectSong:(id)sender { [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]]; } @@ -795,10 +778,23 @@ - (void)selectEQPreset:(id)sender { int curSet = [currentRemote currentEQPresetIndex]; - int item = [[sender representedObject] intValue]; - [currentRemote switchToEQAtIndex:item]; - [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState]; - [[eqMenu itemAtIndex:item + 2] setState:NSOnState]; + int item = [sender tag]; + + if (item == -1) { + [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]]; + } else { + [currentRemote setEqualizerEnabled:YES]; + [currentRemote switchToEQAtIndex:item]; + [[eqMenu itemAtIndex:curSet + 1] setState:NSOffState]; + [[eqMenu itemAtIndex:item + 2] setState:NSOnState]; + } +} + +- (void)selectSongRating:(id)sender +{ + [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState]; + [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0]; + [sender setState:NSOnState]; } - (void)playPause:(id)sender @@ -839,21 +835,6 @@ [playPauseMenuItem setTitle:@"Play"]; } -- (void)toggleEqualizer -{ - [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]]; -} - -- (void)setSongRating:(id)sender -{ - NSLog(@"%f", [currentRemote currentSongRating]); - NSLog(@"%f", (float)[sender tag] / 100.0); - [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0]; -} - -// -// -// Plugin independent selectors // // - (void)quitMenuTunes:(id)sender @@ -861,6 +842,17 @@ [NSApp terminate:self]; } +- (void)showPlayer:(id)sender +{ + if ( ( isAppRunning == ITMTRemotePlayerRunning) ) { + [currentRemote showPrimaryInterface]; + } else { + if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) { + NSLog(@"Error Launching Player"); + } + } +} + - (void)showPreferences:(id)sender { if (!prefsController) { @@ -878,17 +870,56 @@ prefsController = nil; } -- (void)showPlayer:(id)sender + +// +// +// Hot key setup +// +// + +- (void)clearHotKeys { - if ( ( isAppRunning == ITMTRemotePlayerRunning) ) { - [currentRemote showPrimaryInterface]; - } else { - if (![[NSWorkspace sharedWorkspace] launchApplication:[currentRemote playerFullName]]) { - NSLog(@"Error Launching Player"); - } + [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"]; + [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"]; + [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"]; + [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"]; + [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"]; +} + +- (void)setupHotKeys +{ + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + if ([defaults objectForKey:@"PlayPause"] != nil) { + [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause" + combo:[defaults keyComboForKey:@"PlayPause"] + target:self action:@selector(playPause:)]; + } + + if ([defaults objectForKey:@"NextTrack"] != nil) { + [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack" + combo:[defaults keyComboForKey:@"NextTrack"] + target:self action:@selector(nextSong:)]; + } + + if ([defaults objectForKey:@"PrevTrack"] != nil) { + [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack" + combo:[defaults keyComboForKey:@"PrevTrack"] + target:self action:@selector(prevSong:)]; + } + + if ([defaults objectForKey:@"TrackInfo"] != nil) { + [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo" + combo:[defaults keyComboForKey:@"TrackInfo"] + target:self action:@selector(showCurrentTrackInfo)]; + } + + if ([defaults objectForKey:@"UpcomingSongs"] != nil) { + [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs" + combo:[defaults keyComboForKey:@"UpcomingSongs"] + target:self action:@selector(showUpcomingSongs)]; } } - // // @@ -1159,35 +1190,4 @@ } } -/*************************************************************************/ -#pragma mark - -#pragma mark NSApplication DELEGATE METHODS -/*************************************************************************/ - -- (void)applicationWillTerminate:(NSNotification *)note -{ - [self clearHotKeys]; - [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; -} - - -/*************************************************************************/ -#pragma mark - -#pragma mark DEALLOCATION METHODS -/*************************************************************************/ - -- (void)dealloc -{ - if (refreshTimer) { - [refreshTimer invalidate]; - [refreshTimer release]; - refreshTimer = nil; - } - [currentRemote halt]; - [statusItem release]; - [menu release]; -// [view release]; - [super dealloc]; -} - @end