The stars in the rating menu show up now.
[MenuTunes.git] / MenuTunes.m
index 145f702..4b8ac85 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"
@@ -53,7 +44,7 @@ Things to do:
     
     menu = [[NSMenu alloc] initWithTitle:@""];
     
-    if ([currentRemote isAppRunning]) {
+    if ( ( [currentRemote playerRunningStatus] == ITMTRemotePlayerRunning ) ) {
         [self remotePlayerLaunched:nil];
     } else {
         [self remotePlayerTerminated:nil];
@@ -289,18 +280,22 @@ Things to do:
                     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
+            /*NSMenu *ratingSubmenu = [[NSMenu alloc] initWithTitle:@""];
+            unichar whiteStarChar = 2606;
+            unichar blackStarChar = 2605;
+            NSString *whiteStar = [NSString stringWithCharacters:&whiteStarChar
                                             length:1];
-            NSString *blackStarString = [NSString stringWithCharacters:&blackStar
+            NSString *blackStar = [NSString stringWithCharacters:&blackStarChar
                                             length:1];
+            NSData *whiteStarData = [whiteStar dataUsingEncoding:NSUTF8StringEncoding];
+            NSData *blackStarData = [blackStar dataUsingEncoding:NSUTF8StringEncoding];
             NSString *string = @"";
             int i;
             
+            whiteStar = [[NSString alloc] initWithData:whiteStarData encoding:NSUTF8StringEncoding];
+            
             for (i = 0; i < 5; i++) {
-                string = [string stringByAppendingString:whiteStarString];
+                string = [string stringByAppendingString:whiteStar];
             }
             for (i = 0; i < 6; i++) {
                 NSMenuItem *ratingItem;
@@ -308,12 +303,12 @@ Things to do:
                 [ratingItem setTarget:self];
                 [ratingItem setTag:i * 20];
                 string = [string substringToIndex:4];
-                string = [blackStarString stringByAppendingString:string];
+                string = [blackStar stringByAppendingString:string];
             }
+            [ratingSubmenu autorelease];*/
             [[menu addItemWithTitle:@"Song Rating"
                     action:nil
-                    keyEquivalent:@""] setSubmenu:ratingSubmenu];
-            [ratingSubmenu autorelease];
+                    keyEquivalent:@""] setSubmenu:ratingMenu];
         } else if ([item isEqualToString:@"<separator>"]) {
             [menu addItem:[NSMenuItem separatorItem]];
         }
@@ -331,7 +326,7 @@ Things to do:
     NSMenuItem *menuItem;
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     
-    if (!isAppRunning) {
+    if ( ( isAppRunning == ITMTRemotePlayerNotRunning ) ) {
         return;
     }
     
@@ -346,7 +341,6 @@ Things to do:
     if (eqItem) {
         [self rebuildEQPresetsMenu];
     }
-    
     if (trackInfoIndex > -1) {
         NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
         curSongName = [currentRemote currentSongTitle];
@@ -549,7 +543,7 @@ Things to do:
     }
     [eqItem setSubmenu:eqMenu];
     
-    [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
+    [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
 }
 
 - (void)clearHotKeys
@@ -602,7 +596,7 @@ Things to do:
     int playlist = [currentRemote currentPlaylistIndex];
     ITMTRemotePlayerState playerState = [currentRemote playerState];
     
-    if ((playlist > 0) || playerState != stopped) {
+    if ((playlist > 0) || playerState != ITMTRemotePlayerStopped) {
         int trackPlayingIndex = [currentRemote currentSongIndex];
         
         if (trackPlayingIndex != lastSongIndex) {
@@ -661,7 +655,7 @@ Things to do:
         }
         //Update Play/Pause menu item
         if (playPauseMenuItem){
-            if (playerState == playing) {
+            if (playerState == ITMTRemotePlayerPlaying) {
                 [playPauseMenuItem setTitle:@"Pause"];
             } else {
                 [playPauseMenuItem setTitle:@"Play"];
@@ -707,19 +701,33 @@ Things to do:
 
 - (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]; 
+    [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
     
     [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)runTimerInNewThread
+{
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
+    refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES] retain];
+    [runLoop run];
+    [pool release];
+}
+
 - (void)remotePlayerTerminated:(NSNotification *)note
 {
-    isAppRunning = NO;
+    isAppRunning = ITMTRemotePlayerNotRunning;
     
     [menu release];
     menu = [[NSMenu alloc] initWithTitle:@""];
@@ -731,6 +739,7 @@ Things to do:
     [statusItem setMenu:menu];
     
     [refreshTimer invalidate];
+    [refreshTimer release];
     refreshTimer = nil;
     [self clearHotKeys];
 }
@@ -776,10 +785,10 @@ Things to do:
 {
     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 {
@@ -800,7 +809,7 @@ Things to do:
 
 - (void)fastForward:(id)sender
 {
-    [currentRemote fastForward];
+    [currentRemote forward];
     [playPauseMenuItem setTitle:@"Play"];
 }
 
@@ -810,7 +819,7 @@ Things to do:
     [playPauseMenuItem setTitle:@"Play"];
 }
 
-- (void)setSongRating:(id)sender
+- (IBAction)setSongRating:(id)sender
 {
     NSLog(@"%f", (float)[sender tag] / 100.0);
     [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
@@ -836,7 +845,7 @@ Things to do:
 
 - (void)closePreferences
 {
-    if (isAppRunning) {
+    if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
         [self setupHotKeys];
     }
     [prefsController release];
@@ -1133,6 +1142,7 @@ Things to do:
 {
     if (refreshTimer) {
         [refreshTimer invalidate];
+        [refreshTimer release];
         refreshTimer = nil;
     }
     [currentRemote halt];