Back to the old version with fixes from the new MainController. Let's how
[MenuTunes.git] / OldMainController.m
index 23a4be4..0d22d9a 100755 (executable)
     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];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
     
-    [self registerDefaultsIfNeeded];
+    [self registerDefaults];
     
     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];
     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:[menu autorelease]];
+        
+        [refreshTimer invalidate];
+        [refreshTimer release];
+        refreshTimer = nil;
+        [self clearHotKeys];
+        isAppRunning = NO;
+        return;
+    }
+}
+
+- (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];
+}
 
 /*************************************************************************/
 #pragma mark -
 #pragma mark INSTANCE METHODS
 /*************************************************************************/
 
-- (void)registerDefaultsIfNeeded
+- (void)registerDefaults
 {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     if (![defaults objectForKey:@"menu"]) {
             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
             
             ratingMenu = [[NSMenu alloc] initWithTitle:@""];
             
-            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar]
-                            action:@selector(setSongRating:)
-                            keyEquivalent:@""];
+            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
             [item setTarget:self];
             [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 setTarget:self];
+            [item setTag:20];
+            
+            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, emptyStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
+            [item setTarget:self];
+            [item setTag:40];
+            
+            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, emptyStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
+            [item setTarget:self];
+            [item setTag:60];
+            
+            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, emptyStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
+            [item setTarget:self];
+            [item setTag:80];
+            
+            item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
+            [item setTarget:self];
+            [item setTag:100];
         } else if ([item isEqualToString:@"<separator>"]) {
             [menu addItem:[NSMenuItem separatorItem]];
         }
     }
 }
 
-- (void)remotePlayerLaunched:(NSNotification *)note
-{
-    isAppRunning = ITMTRemotePlayerRunning;
-    
-    //Restart the timer
-    [NSThread detachNewThreadSelector:@selector(runTimerInNewThread) toTarget:self withObject:nil];
-    
-    [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];
-}
-
 //
 //
 // Selectors - called from status item menu
     [currentRemote setEqualizerEnabled:![currentRemote equalizerEnabled]];
 }
 
-- (void)setSongRating:(id)sender
+- (void)selectSongRating:(id)sender
 {
-    NSLog(@"%f", [currentRemote currentSongRating]);
-    NSLog(@"%f", (float)[sender tag] / 100.0);
+    [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState];
     [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
+    [sender setState:NSOnState];
 }
 
 //