Added a commented-out line, template for when making builds for beta testers.
[MenuTunes.git] / MenuTunes.m
index 18565b9..1bdd910 100755 (executable)
@@ -1,6 +1,5 @@
 /*
 Things to do:
 /*
 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,
 ¥ Make preferences window pretty
 ¥ Hot Keys
     - hot keys can't be set when NSBGOnly is on. The window is not key,
@@ -8,11 +7,10 @@ Things to do:
     - going to need a different way of defining key combos
 ¥ Optimize
 ¥ Apple Events! Apple Events! Apple Events!
     - 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 "MenuTunes.h"
-#import "MenuTunesView.h"
+// #import "MenuTunesView.h"
 #import "PreferencesController.h"
 #import "HotKeyCenter.h"
 #import "StatusWindowController.h"
 #import "PreferencesController.h"
 #import "HotKeyCenter.h"
 #import "StatusWindowController.h"
@@ -66,12 +64,16 @@ Things to do:
     }
     
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
     }
     
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
-    [statusItem setImage:[NSImage imageNamed:@"menu.tiff"]];
+    [[[statusItem _button] cell] setType:NSNullCellType];
+    [statusItem setImage:[NSImage imageNamed:@"menu"]];
+    [[statusItem _button] setAlternateImage:[NSImage imageNamed:@"selected_image"]];
     [statusItem setHighlightMode:YES];
     [statusItem setMenu:menu];
     [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];
     [statusItem retain];
-    view = [[MenuTunesView alloc] initWithFrame:[[statusItem view] frame]];
-    //[statusItem setView:view];
+//  view = [[MenuTunesView alloc] initWithFrame:[[statusItem view] frame]];
+//  [statusItem setView:view];
 }
 
 
 }
 
 
@@ -187,17 +189,12 @@ Things to do:
 //Updates the menu with current player state, song, and upcoming songs
 - (void)updateMenu
 {
 //Updates the menu with current player state, song, and upcoming songs
 - (void)updateMenu
 {
-    NSString *curSongName, *curAlbumName;
+    NSString *curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
     NSMenuItem *menuItem;
     NSMenuItem *menuItem;
-    
     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
         return;
     }
     
     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
         return;
     }
     
-    //Get the current track name and album.
-    curSongName = [self runScriptAndReturnResult:@"return name of current track"];
-    curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
-    
     if (upcomingSongsItem) {
         [self rebuildUpcomingSongsMenu];
     }
     if (upcomingSongsItem) {
         [self rebuildUpcomingSongsMenu];
     }
@@ -208,51 +205,54 @@ Things to do:
         [self rebuildEQPresetsMenu];
     }
     
         [self rebuildEQPresetsMenu];
     }
     
-    if ([curSongName length] > 0) {
-        int index = [menu indexOfItemWithTitle:@"Now Playing"];
-        
-        if (index > -1) {
-            [menu removeItemAtIndex:index + 1];
+    if (trackInfoIndex > -1)
+    {
+        NSString *curSongName;
+        curSongName = [self runScriptAndReturnResult:@"return name of current track"];
+        if ([curSongName length] > 0) {
+            int index = [menu indexOfItemWithTitle:@"Now Playing"];
             
             
-            if (didHaveAlbumName) {
+            if (index > -1) {
                 [menu removeItemAtIndex:index + 1];
                 [menu removeItemAtIndex:index + 1];
+                if (didHaveAlbumName) {
+                    [menu removeItemAtIndex:index + 1];
+                }
             }
             }
-        }
-        
-        if ([curAlbumName length] > 0) {
-            menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
-                                                  action:nil
-                                                  keyEquivalent:@""];
+            if (!isPlayingRadio) {
+                if ([curAlbumName length] > 0) {
+                    menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
+                                                        action:nil
+                                                        keyEquivalent:@""];
+                    [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
+                    [menuItem release];
+                }
+            }
+            menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curSongName]
+                                                action:nil
+                                                keyEquivalent:@""];
             [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
             [menuItem release];
             [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
             [menuItem release];
-        }
-        
-        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"]];
+            
+            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];
+            }
+            
+        } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
+            [menu removeItemAtIndex:trackInfoIndex];
+            [menu removeItemAtIndex:trackInfoIndex];
+            
+            if (didHaveAlbumName) {
+                [menu removeItemAtIndex:trackInfoIndex];
+            }
+            
+            menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
-        
-    } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
-        [menu removeItemAtIndex:trackInfoIndex];
-        [menu removeItemAtIndex:trackInfoIndex];
-        
-        if (didHaveAlbumName) {
-            [menu removeItemAtIndex:trackInfoIndex];
-        }
-        
-        menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
-        [menu insertItem:menuItem atIndex:trackInfoIndex];
-        [menuItem release];
     }
     }
-    
     didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
 }
 
     didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
 }
 
@@ -261,30 +261,33 @@ Things to do:
 {
     int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
 {
     int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
-    
-    if (numSongs > 0) {
-        int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
-        int i;
-        
-        [upcomingSongsMenu release];
-        upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
-        
-        for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
-            if (i <= numSongs) {
-                NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
-                NSMenuItem *songItem;
-                songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
-                [songItem setTarget:self];
-                [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
-                [upcomingSongsMenu addItem:songItem];
-                [songItem release];
-            } else {
-                [upcomingSongsMenu addItemWithTitle:@"End of playlist." action:nil keyEquivalent:@""];
-                break;
+    if (!isPlayingRadio) {
+        if (numSongs > 0) {
+            int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
+            int i;
+            
+            [upcomingSongsMenu release];
+            upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
+            [upcomingSongsItem setSubmenu:upcomingSongsMenu];
+            [upcomingSongsItem setEnabled:YES];
+            
+            for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
+                if (i <= numSongs) {
+                    NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
+                    NSMenuItem *songItem;
+                    songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
+                    [songItem setTarget:self];
+                    [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
+                    [upcomingSongsMenu addItem:songItem];
+                    [songItem release];
+                } else {
+                    break;
+                }
             }
         }
             }
         }
-        [upcomingSongsItem setSubmenu:upcomingSongsMenu];
-        [upcomingSongsItem setEnabled:YES];
+    } else {
+        [upcomingSongsItem setSubmenu:nil];
+        [upcomingSongsItem setEnabled:NO];
     }
 }
 
     }
 }
 
@@ -293,6 +296,11 @@ Things to do:
     int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
     int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
     
     int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
     int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
     
+    if (isPlayingRadio)
+    {
+        curPlaylist = 0;
+    }
+    
     if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
         return;
     
     if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
         return;
     
@@ -421,15 +429,50 @@ Things to do:
 - (void)timerUpdate
 {
     int pid;
 - (void)timerUpdate
 {
     int pid;
-    
     if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
         int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
         
         if (trackPlayingIndex != curTrackIndex) {
     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;
         }
             [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"]) {
         //Update Play/Pause menu item
         if (playPauseMenuItem){
             if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
@@ -514,9 +557,12 @@ andEventID:(AEEventID)eventID
 - (void)selectPlaylist:(id)sender
 {
     int playlist = [[sender representedObject] intValue];
 - (void)selectPlaylist:(id)sender
 {
     int playlist = [[sender representedObject] intValue];
+    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 runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
-    [self updateMenu];
 }
 
 - (void)selectEQPreset:(id)sender
 }
 
 - (void)selectEQPreset:(id)sender
@@ -722,7 +768,7 @@ isEqualToString:@"rewinding"]) {
     CloseComponent(asComponent);
     [statusItem release];
     [menu release];
     CloseComponent(asComponent);
     [statusItem release];
     [menu release];
-    [view release];
+//  [view release];
     [super dealloc];
 }
 
     [super dealloc];
 }