Added a browse Artist menu. The playlist creation and everything is
[MenuTunes.git] / MenuController.m
index 54f9809..88a94ba 100755 (executable)
@@ -21,6 +21,7 @@
 - (NSMenu *)upcomingSongsMenu;
 - (NSMenu *)playlistsMenu;
 - (NSMenu *)eqMenu;
+- (NSMenu *)artistsMenu;
 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
         onItem:(id <NSMenuItem>)item;
 - (BOOL)iPodWithNameAutomaticallyUpdates:(NSString *)name;
         [tempItem setSubmenu:nil];
     }
     
+    if ( (tempItem = [_currentMenu itemWithTag:5]) ) {
+        ITDebugLog(@"Removing \"Artists\" submenu.");
+        [tempItem setSubmenu:nil];
+    }
+    
     ITDebugLog(@"Begin building menu.");
     
     //create our menu
                 [[MainController sharedController] networkError:localException];
             NS_ENDHANDLER
         } else if ([nextObject isEqualToString:@"songRating"] && currentSongRating) {
-                ITDebugLog(@"Add \"Song Rating\" submenu.");
-                tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
-                        action:nil
-                        keyEquivalent:@""];
-                [tempItem setSubmenu:_ratingMenu];
-                [tempItem setTag:1];
-                if (_playingRadio || !_currentPlaylist) {
-                    [tempItem setEnabled:NO];
-                }
-                
-                itemEnum = [[_ratingMenu itemArray] objectEnumerator];
-                while ( (tempItem = [itemEnum nextObject]) ) {
-                    [tempItem setState:NSOffState];
-                }
-                
-                NS_DURING
-                    [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
-                NS_HANDLER
-                    [[MainController sharedController] networkError:localException];
-                NS_ENDHANDLER
-            } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
-                ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
-                tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
-                        action:nil
-                        keyEquivalent:@""];
-                [tempItem setSubmenu:_upcomingSongsMenu];
-                [tempItem setTag:2];
-                if (_playingRadio || !_currentPlaylist) {
-                    [tempItem setEnabled:NO];
-                }
+            ITDebugLog(@"Add \"Song Rating\" submenu.");
+            tempItem = [menu addItemWithTitle:NSLocalizedString(@"songRating", @"Song Rating")
+                    action:nil
+                    keyEquivalent:@""];
+            [tempItem setSubmenu:_ratingMenu];
+            [tempItem setTag:1];
+            if (_playingRadio || !_currentPlaylist) {
+                [tempItem setEnabled:NO];
+            }
+            
+            itemEnum = [[_ratingMenu itemArray] objectEnumerator];
+            while ( (tempItem = [itemEnum nextObject]) ) {
+                [tempItem setState:NSOffState];
+            }
+            
+            NS_DURING
+                [[_ratingMenu itemAtIndex:([mtr currentSongRating] * 5)] setState:NSOnState];
+            NS_HANDLER
+                [[MainController sharedController] networkError:localException];
+            NS_ENDHANDLER
+        } else if ([nextObject isEqualToString:@"upcomingSongs"]) {
+            ITDebugLog(@"Add \"Upcoming Songs\" submenu.");
+            tempItem = [menu addItemWithTitle:NSLocalizedString(@"upcomingSongs", @"Upcoming Songs")
+                    action:nil
+                    keyEquivalent:@""];
+            [tempItem setSubmenu:_upcomingSongsMenu];
+            [tempItem setTag:2];
+            if (_playingRadio || !_currentPlaylist) {
+                [tempItem setEnabled:NO];
+            }
+        } else if ([nextObject isEqualToString:@"artists"]) {
+            ITDebugLog(@"Add \"Artists\" submenu.");
+            tempItem = [menu addItemWithTitle:NSLocalizedString(@"artists", @"Artists")
+                    action:nil
+                    keyEquivalent:@""];
+            [tempItem setSubmenu:_artistsMenu];
+            [tempItem setTag:5];
+            
+            itemEnum = [[_eqMenu itemArray] objectEnumerator];
+            while ( (tempItem = [itemEnum nextObject]) ) {
+                [tempItem setState:NSOffState];
             }
         }
+    }
     ITDebugLog(@"Finished building menu.");
     [_currentMenu release];
     _currentMenu = menu;
 
 - (void)rebuildSubmenus
 {
+    NSArray *menu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
     ITDebugLog(@"Rebuilding all of the submenus.");
     NS_DURING
         _currentPlaylist = [[[MainController sharedController] currentRemote] currentPlaylistIndex];
     [_playlistsMenu release];
     ITDebugLog(@" - EQ menu");
     [_eqMenu release];
+    
     ITDebugLog(@"Beginning Rebuild of \"Song Rating\" submenu.");
     _ratingMenu = [self ratingMenu];
     ITDebugLog(@"Beginning Rebuild of \"Upcoming Songs\" submenu.");
     _playlistsMenu = [self playlistsMenu];
     ITDebugLog(@"Beginning Rebuild of \"EQ Presets\" submenu.");
     _eqMenu = [self eqMenu];
+    
+    if ([menu containsObject:@"artists"]) {
+        ITDebugLog(@"Releasing artists menu");
+        [_artistsMenu release];
+        ITDebugLog(@"Beginning Rebuild of \"Artists\" submenu.");
+        _artistsMenu = [self artistsMenu];
+    }
     ITDebugLog(@"Done rebuilding all of the submenus.");
 }
 
         [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState];
     }
     [indices release];
-    [playlistsMenu addItem:[NSMenuItem separatorItem]];
-    [[playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""] setTarget:self];
+    tempItem = [playlistsMenu addItemWithTitle:NSLocalizedString(@"refresh", @"Refresh") action:@selector(rebuildSubmenus) keyEquivalent:@""];
+    [tempItem setTarget:self];
+    [tempItem setImage:[NSImage imageNamed:@"ChasingArrow"]];
     ITDebugLog(@"Done Building \"Playlists\" menu");
     return playlistsMenu;
 }
     return eqMenu;
 }
 
+- (NSMenu *)artistsMenu
+{
+    NSMenu *artistsMenu = [[NSMenu alloc] initWithTitle:@""];
+    NSEnumerator *artistsEnumerator;
+    NSString *nextArtist;
+    id <NSMenuItem> tempItem;
+    ITDebugLog(@"Building \"Artists\" menu.");
+    NS_DURING
+        artistsEnumerator = [[[[MainController sharedController] currentRemote] artists] objectEnumerator];
+        while ( (nextArtist = [artistsEnumerator nextObject]) ) {
+            tempItem = [artistsMenu addItemWithTitle:nextArtist action:@selector(performArtistsMenuAction:) keyEquivalent:@""];
+            [tempItem setRepresentedObject:nextArtist];
+            [tempItem setTarget:self];
+        }
+    NS_HANDLER
+        [[MainController sharedController] networkError:localException];
+    NS_ENDHANDLER
+    ITDebugLog(@"Done Building \"Artists\" menu");
+    return artistsMenu;
+}
+
 - (void)performMainMenuAction:(id)sender
 {
     switch ( [sender tag] )
     [[MainController sharedController] selectSongAtIndex:[sender tag]];
 }
 
+- (void)performArtistsMenuAction:(id)sender
+{
+    ITDebugLog(@"Artist action selected on item with object %i", [sender representedObject]);
+    /*
+    ** 1 - Artist
+    ** 2 - Album
+    ** 3 - Genre?
+    */
+    [[MainController sharedController] makePlaylistWithTerm:[sender representedObject] ofType:1];
+}
+
 - (void)updateMenu
 {
     ITDebugLog(@"Update Menu");