Changed the player state names to the new ones. Increased refresh rate fire.
[MenuTunes.git] / MenuTunes.m
index a1121d7..77c59ba 100755 (executable)
@@ -1,12 +1,3 @@
-/*
-Things to do:
-¥ Make preferences window pretty
-¥ Optimize
-¥ Apple Events! Apple Events! Apple Events!
-¥ Manual and webpage
-¥ Finish up registration frontend
-*/
-
 #import "MenuTunes.h"
 #import "PreferencesController.h"
 #import "HotKeyCenter.h"
@@ -46,17 +37,17 @@ Things to do:
     currentRemote = [self loadRemote];
     [currentRemote begin];
     
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(remotePlayerLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
     
     [self registerDefaultsIfNeeded];
     
     menu = [[NSMenu alloc] initWithTitle:@""];
     
-    if ([currentRemote isAppRunning]) {
-        [self iTunesLaunched:nil];
+    if ( ( [currentRemote playerRunningStatus] == ITMTRemotePlayerRunning ) ) {
+        [self remotePlayerLaunched:nil];
     } else {
-        [self iTunesTerminated:nil];
+        [self remotePlayerTerminated:nil];
     }
     
     statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
@@ -93,14 +84,14 @@ Things to do:
             }
         }
 
-//      if ( [remoteArray count] > 0 ) {
+//      if ( [remoteArray count] > 0 ) {  // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
 //          if ( [remoteArray count] > 1 ) {
 //              [remoteArray sortUsingSelector:@selector(sortAlpha:)];
 //          }
 //          [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
 //      }
     }
-    NSLog(@"%@", [remoteArray objectAtIndex:0]);
+//  NSLog(@"%@", [remoteArray objectAtIndex:0]);  //DEBUG
     return [remoteArray objectAtIndex:0];
 }
 
@@ -129,6 +120,7 @@ Things to do:
                 @"<separator>",
                 @"Upcoming Songs",
                 @"Playlists",
+                @"Song Rating",
                 @"<separator>",
                 @"PreferencesÉ",
                 @"Quit",
@@ -287,6 +279,32 @@ Things to do:
             [menu addItemWithTitle:@"No Song"
                     action:nil
                     keyEquivalent:@""];
+        } else if ([item isEqualToString:@"Song Rating"]) {
+            NSMenu *ratingSubmenu = [[NSMenu alloc] initWithTitle:@""];
+            unichar whiteStar = 'o';//2606;
+            unichar blackStar = 'x';//2605;
+            NSString *whiteStarString = [NSString stringWithCharacters:&whiteStar
+                                            length:1];
+            NSString *blackStarString = [NSString stringWithCharacters:&blackStar
+                                            length:1];
+            NSString *string = @"";
+            int i;
+            
+            for (i = 0; i < 5; i++) {
+                string = [string stringByAppendingString:whiteStarString];
+            }
+            for (i = 0; i < 6; i++) {
+                NSMenuItem *ratingItem;
+                ratingItem = [ratingSubmenu addItemWithTitle:string action:@selector(setSongRating:) keyEquivalent:@""];
+                [ratingItem setTarget:self];
+                [ratingItem setTag:i * 20];
+                string = [string substringToIndex:4];
+                string = [blackStarString stringByAppendingString:string];
+            }
+            [[menu addItemWithTitle:@"Song Rating"
+                    action:nil
+                    keyEquivalent:@""] setSubmenu:ratingSubmenu];
+            [ratingSubmenu autorelease];
         } else if ([item isEqualToString:@"<separator>"]) {
             [menu addItem:[NSMenuItem separatorItem]];
         }
@@ -304,7 +322,7 @@ Things to do:
     NSMenuItem *menuItem;
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     
-    if (!isAppRunning) {
+    if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
         return;
     }
     
@@ -319,7 +337,6 @@ Things to do:
     if (eqItem) {
         [self rebuildEQPresetsMenu];
     }
-    
     if (trackInfoIndex > -1) {
         NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
         curSongName = [currentRemote currentSongTitle];
@@ -495,6 +512,7 @@ Things to do:
 - (void)rebuildEQPresetsMenu
 {
     NSArray *eqPresets = [currentRemote eqPresets];
+    NSMenuItem *enabledItem;
     int i;
     
     if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
@@ -503,6 +521,11 @@ Things to do:
     [eqMenu release];
     eqMenu = [[NSMenu alloc] initWithTitle:@""];
     
+    enabledItem = [eqMenu addItemWithTitle:@"EQ Enabled"
+                          action:NULL
+                          keyEquivalent:@""];
+    [eqMenu addItem:[NSMenuItem separatorItem]];
+    
     for (i = 0; i < [eqPresets count]; i++) {
         NSString *setName = [eqPresets objectAtIndex:i];
         NSMenuItem *tempItem;
@@ -516,7 +539,7 @@ Things to do:
     }
     [eqItem setSubmenu:eqMenu];
     
-    [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
+    [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
 }
 
 - (void)clearHotKeys
@@ -567,9 +590,9 @@ Things to do:
 - (void)timerUpdate
 {
     int playlist = [currentRemote currentPlaylistIndex];
-    PlayerState playerState = [currentRemote playerState];
+    ITMTRemotePlayerState playerState = [currentRemote playerState];
     
-    if ((playlist > 0) || playerState != stopped) {
+    if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
         int trackPlayingIndex = [currentRemote currentSongIndex];
         
         if (trackPlayingIndex != lastSongIndex) {
@@ -628,30 +651,70 @@ Things to do:
         }
         //Update Play/Pause menu item
         if (playPauseMenuItem){
-            if (playerState == playing) {
+            if (playerState == ITMTRemotePlayerPlaying) {
                 [playPauseMenuItem setTitle:@"Pause"];
             } else {
                 [playPauseMenuItem setTitle:@"Play"];
             }
         }
+    } else if ((lastPlaylistIndex > 0) && (playlist == 0)) {
+        NSMenuItem *menuItem;
+        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];
+        }
+        
+        if ([defaults boolForKey:@"showTime"] == YES) {
+            [menu removeItemAtIndex:trackInfoIndex];
+        }
+        
+        if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
+            [menu removeItemAtIndex:trackInfoIndex];
+        }
+        
+        if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
+            [menu removeItemAtIndex:trackInfoIndex];
+        }
+        
+        [playPauseMenuItem setTitle:@"Play"];
+        
+        didHaveArtistName = NO;
+        didHaveAlbumName = NO;
+        lastPlaylistIndex = -1;
+        lastSongIndex = -1;
+        
+        [upcomingSongsItem setSubmenu:nil];
+        [upcomingSongsItem setEnabled:NO];
+        
+        menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
+        [menu insertItem:menuItem atIndex:trackInfoIndex];
+        [menuItem release];
     }
 }
 
-- (void)iTunesLaunched:(NSNotification *)note
+- (void)remotePlayerLaunched:(NSNotification *)note
 {
-    isAppRunning = YES;
+    isAppRunning = ITMTRemotePlayerRunning;
     
     //Restart the timer
-    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 
+    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 
     
     [self rebuildMenu]; //Rebuild the menu since no songs will be playing
-    [self rebuildPlaylistMenu];
+    if (playlistItem) {
+        [self rebuildPlaylistMenu];
+    }
+    if (eqItem) {
+        [self rebuildEQPresetsMenu];
+    }
     [statusItem setMenu:menu]; //Set the menu back to the main one
 }
 
-- (void)iTunesTerminated:(NSNotification *)note
+- (void)remotePlayerTerminated:(NSNotification *)note
 {
-    isAppRunning = NO;
+    isAppRunning = ITMTRemotePlayerNotRunning;
     
     [menu release];
     menu = [[NSMenu alloc] initWithTitle:@""];
@@ -686,7 +749,9 @@ Things to do:
     int playlist = [[sender representedObject] intValue];
     if (!isPlayingRadio) {
         int curPlaylist = [currentRemote currentPlaylistIndex];
-        [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
+        if (curPlaylist > 0) {
+            [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
+        }
     }
     [currentRemote switchToPlaylistAtIndex:playlist];
     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
@@ -704,12 +769,12 @@ Things to do:
 
 - (void)playPause:(id)sender
 {
-    PlayerState state = [currentRemote playerState];
+    ITMTRemotePlayerState state = [currentRemote playerState];
     
-    if (state == playing) {
+    if (state == ITMTRemotePlayerPlaying) {
         [currentRemote pause];
         [playPauseMenuItem setTitle:@"Play"];
-    } else if ((state == forwarding) || (state == rewinding)) {
+    } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
         [currentRemote pause];
         [currentRemote play];
     } else {
@@ -730,7 +795,7 @@ Things to do:
 
 - (void)fastForward:(id)sender
 {
-    [currentRemote fastForward];
+    [currentRemote forward];
     [playPauseMenuItem setTitle:@"Play"];
 }
 
@@ -740,6 +805,12 @@ Things to do:
     [playPauseMenuItem setTitle:@"Play"];
 }
 
+- (void)setSongRating:(id)sender
+{
+    NSLog(@"%f", (float)[sender tag] / 100.0);
+    [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
+}
+
 //
 //
 // Plugin independent selectors
@@ -760,7 +831,7 @@ Things to do:
 
 - (void)closePreferences
 {
-    if (isAppRunning) {
+    if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
         [self setupHotKeys];
     }
     [prefsController release];