Added the current track info to the menu. Yay.
[MenuTunes.git] / MenuTunes.m
index b3f858a..f4a66cd 100755 (executable)
@@ -1,18 +1,11 @@
 /*
 Things to do:
-¥ Radio mode makes things act oddly
 ¥ Make preferences window pretty
-¥ Hot Keys
-    - hot keys can't be set when NSBGOnly is on. The window is not key,
-      so the KeyBroadcaster does not pick up key combos
-    - going to need a different way of defining key combos
 ¥ Optimize
 ¥ Apple Events! Apple Events! Apple Events!
-¥ Upcoming songs menu items are disabled after launching iTunes and playing
 */
 
 #import "MenuTunes.h"
-// #import "MenuTunesView.h"
 #import "PreferencesController.h"
 #import "HotKeyCenter.h"
 #import "StatusWindowController.h"
@@ -71,9 +64,9 @@ Things to do:
     [[statusItem _button] setAlternateImage:[NSImage imageNamed:@"selected_image"]];
     [statusItem setHighlightMode:YES];
     [statusItem setMenu:menu];
+    // Below line of code is for creating builds for Beta Testers
+    // [statusItem setToolTip:@"This Nontransferable Beta (Built on __DATE__) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
     [statusItem retain];
-//  view = [[MenuTunesView alloc] initWithFrame:[[statusItem view] frame]];
-//  [statusItem setView:view];
 }
 
 
@@ -84,8 +77,9 @@ Things to do:
 
 - (void)registerDefaultsIfNeeded
 {
-    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"menu"]) {
-        [[NSUserDefaults standardUserDefaults] setObject:
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    if (![defaults objectForKey:@"menu"]) {
+        [defaults setObject:
             [NSArray arrayWithObjects:
                 @"Play/Pause",
                 @"Next Track",
@@ -102,6 +96,26 @@ Things to do:
                 @"Current Track Info",
                 nil] forKey:@"menu"];
     }
+    
+    if (![defaults objectForKey:@"showName"])
+    {
+        [defaults setBool:YES forKey:@"showName"];
+    }
+    
+    if (![defaults objectForKey:@"showArtist"])
+    {
+        [defaults setBool:YES forKey:@"showArtist"];
+    }
+    
+    if (![defaults objectForKey:@"showAlbum"])
+    {
+        [defaults setBool:NO forKey:@"showAlbum"];
+    }
+    
+    if (![defaults objectForKey:@"showTime"])
+    {
+        [defaults setBool:NO forKey:@"showTime"];
+    }
 }
 
 //Recreate the status item menu
@@ -112,7 +126,9 @@ Things to do:
     
     trackInfoIndex = -1;
     didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
-
+    didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
+    
+    
     while ([menu numberOfItems] > 0) {
         [menu removeItemAtIndex:0];
     }
@@ -189,8 +205,9 @@ Things to do:
 //Updates the menu with current player state, song, and upcoming songs
 - (void)updateMenu
 {
-    NSString *curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
     NSMenuItem *menuItem;
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    
     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
         return;
     }
@@ -207,21 +224,51 @@ Things to do:
     
     if (trackInfoIndex > -1)
     {
-        NSString *curSongName;
+        NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+            curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
+        }
+        
+        if ([defaults boolForKey:@"showAlbum"]) {
+            curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
+        }
+        
         if ([curSongName length] > 0) {
             int index = [menu indexOfItemWithTitle:@"Now Playing"];
             
             if (index > -1) {
-                [menu removeItemAtIndex:index + 1];
-                
-                if (!isPlayingRadio) {
-                    if (didHaveAlbumName) {
-                        [menu removeItemAtIndex:index + 1];
-                    }
+                if ([defaults boolForKey:@"showName"]) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if (didHaveAlbumName) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if (didHaveArtistName) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if ([defaults boolForKey:@"showTime"]) {
+                    [menu removeItemAtIndex:index + 1];
                 }
             }
             if (!isPlayingRadio) {
+                if ([defaults boolForKey:@"showTime"]) {
+                    menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", [self runScriptAndReturnResult:@"return time of current track"]]
+                                                        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
@@ -245,9 +292,20 @@ Things to do:
             
         } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
             [menu removeItemAtIndex:trackInfoIndex];
-            [menu removeItemAtIndex:trackInfoIndex];
             
-            if (didHaveAlbumName) {
+            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];
             }
             
@@ -255,8 +313,15 @@ Things to do:
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+                didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
+            }
+            
+            if ([defaults boolForKey:@"showAlbum"]) {
+                didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
+            }
     }
-    didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
 }
 
 //Rebuild the upcoming songs submenu. Can be improved a lot.
@@ -271,6 +336,8 @@ Things to do:
             
             [upcomingSongsMenu release];
             upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
+            [upcomingSongsItem setSubmenu:upcomingSongsMenu];
+            [upcomingSongsItem setEnabled:YES];
             
             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
                 if (i <= numSongs) {
@@ -282,12 +349,9 @@ Things to do:
                     [upcomingSongsMenu addItem:songItem];
                     [songItem release];
                 } else {
-                    [upcomingSongsMenu addItemWithTitle:@"End of playlist." action:nil keyEquivalent:@""];
                     break;
                 }
             }
-            [upcomingSongsItem setSubmenu:upcomingSongsMenu];
-            [upcomingSongsItem setEnabled:YES];
         }
     } else {
         [upcomingSongsItem setSubmenu:nil];
@@ -433,16 +497,50 @@ Things to do:
 - (void)timerUpdate
 {
     int pid;
-    
     if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
         int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
         
         if (trackPlayingIndex != curTrackIndex) {
+            bool wasPlayingRadio = isPlayingRadio;
             isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
+            if (isPlayingRadio && !wasPlayingRadio) {
+                int i;
+                for (i = 0; i < [playlistMenu numberOfItems]; i++)
+                {
+                    [[playlistMenu itemAtIndex:i] setState:NSOffState];
+                }
+            }
+            if (wasPlayingRadio) {
+                NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
+                [menu insertItem:temp atIndex:trackInfoIndex + 1];
+                [temp release];
+            }
             [self updateMenu];
             curTrackIndex = trackPlayingIndex;
         }
-               
+        else
+        {
+            int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
+            if (playlist != curPlaylistIndex) {
+                bool wasPlayingRadio = isPlayingRadio;
+                isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
+                if (isPlayingRadio && !wasPlayingRadio) {
+                    int i;
+                    for (i = 0; i < [playlistMenu numberOfItems]; i++)
+                    {
+                        [[playlistMenu itemAtIndex:i] setState:NSOffState];
+                    }
+                }
+                if (wasPlayingRadio) {
+                    NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
+                    [menu insertItem:temp atIndex:trackInfoIndex + 1];
+                    [temp release];
+                }
+                [self updateMenu];
+                curTrackIndex = trackPlayingIndex;
+                curPlaylistIndex = playlist;
+            }
+        }
         //Update Play/Pause menu item
         if (playPauseMenuItem){
             if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
@@ -527,11 +625,12 @@ andEventID:(AEEventID)eventID
 - (void)selectPlaylist:(id)sender
 {
     int playlist = [[sender representedObject] intValue];
-    int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
-    [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
+    if (!isPlayingRadio) {
+        int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
+        [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
+    }
     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
-    [self updateMenu];
 }
 
 - (void)selectEQPreset:(id)sender