Added a number formatter to the Preferences.
[MenuTunes.git] / OldMainController.m
index 82aedce..aada73b 100755 (executable)
@@ -8,9 +8,9 @@
 - (void)rebuildUpcomingSongsMenu;
 - (void)rebuildPlaylistMenu;
 - (void)rebuildEQPresetsMenu;
+- (void)updateRatingMenu;
 - (void)setupHotKeys;
 - (void)timerUpdate;
-- (void)updateMenu;
 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
         onItem:(NSMenuItem *)item;
 
@@ -47,6 +47,7 @@
 
 - (void)applicationDidFinishLaunching:(NSNotification *)note
 {
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     currentRemote = [self loadRemote];
     [currentRemote begin];
     
             selector:@selector(applicationLaunched:)
             name:NSWorkspaceDidLaunchApplicationNotification
             object:nil];
-    
-    [self registerDefaults];
+
+    if ( ! [defaults objectForKey:@"menu"] ) {  // If this is nil, defaults have never been registered.
+        [[PreferencesController sharedPrefs] registerDefaults];
+    }
     
     statusItem = [[ITStatusItem alloc]
             initWithStatusBar:[NSStatusBar systemStatusBar]
 {
     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;
 
 - (void)applicationTerminated:(NSNotification *)note
 {
-    if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[currentRemote playerFullName]]) {        
+    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 addItemWithTitle:[NSString stringWithFormat:@"Open %@", [currentRemote playerSimpleName]] action:@selector(showPlayer:) keyEquivalent:@""];
         [notRunningMenu addItem:[NSMenuItem separatorItem]];
-        [[notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
-        [[notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
-        [statusItem setMenu:[notRunningMenu autorelease]];
+        [notRunningMenu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""];
+        [notRunningMenu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""];
         
         [refreshTimer invalidate];
         [refreshTimer release];
         refreshTimer = nil;
         [self clearHotKeys];
-        isAppRunning = NO;
-        return;
+        isAppRunning = ITMTRemotePlayerNotRunning;
+        
+        [statusItem setMenu:[notRunningMenu autorelease]];
     }
 }
 
 #pragma mark INSTANCE METHODS
 /*************************************************************************/
 
-- (void)registerDefaults
-{
-    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
-    if (![defaults objectForKey:@"menu"]) {
-        BOOL found = NO;
-        NSMutableDictionary *loginwindow;
-        NSMutableArray *loginarray;
-        int i;
-        
-        [defaults setObject:
-            [NSArray arrayWithObjects:
-                @"Play/Pause",
-                @"Next Track",
-                @"Previous Track",
-                @"Fast Forward",
-                @"Rewind",
-                @"<separator>",
-                @"Upcoming Songs",
-                @"Playlists",
-                @"Song Rating",
-                @"<separator>",
-                @"PreferencesÉ",
-                @"Quit",
-                @"<separator>",
-                @"Current Track Info",
-                nil] forKey:@"menu"];
-        
-        [defaults synchronize];
-        loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
-        loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
-        
-        for (i = 0; i < [loginarray count]; i++) {
-            NSDictionary *tempDict = [loginarray objectAtIndex:i];
-            if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:
-                [[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
-                found = YES;
-            }
-        }
-        
-        //
-        //This is teh sux
-        //We must fix it so it is no longer suxy
-        if (!found) {
-            if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
-                AEDesc scriptDesc, resultDesc;
-                NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
-                ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
-                
-                AECreateDesc(typeChar, [script cString], [script cStringLength], 
-            &scriptDesc);
-                
-                OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
-                
-                AEDisposeDesc(&scriptDesc);
-                AEDisposeDesc(&resultDesc);
-                
-                CloseComponent(asComponent);
-            }
-        }
-    }
-    
-    if (![defaults integerForKey:@"SongsInAdvance"])
-    {
-        [defaults setInteger:5 forKey:@"SongsInAdvance"];
-    }
-    
-    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"];
-    }
-}
-
 - (void)startTimerInNewThread
 {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 //Recreate the status item menu
 - (void)rebuildMenu
 {
-    NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    NSArray *myMenu = [defaults arrayForKey:@"menu"];
+    int playlist = [currentRemote currentPlaylistIndex];
     int i;
     
+    if ([currentRemote playerRunningState] == ITMTRemotePlayerNotRunning) {
+        return;
+    }
+    
     trackInfoIndex = -1;
     lastPlaylistIndex = -1;
     didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
     didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
     
-    [menu autorelease];
+    [menu release];
     menu = [[NSMenu alloc] initWithTitle:@""];
     
-    /*while ([menu numberOfItems] > 0) {
-        [menu removeItemAtIndex:0];
-    }*/
-    
     playPauseItem = nil;
-    lastSongIdentifier = @"0-0";
     
     upcomingSongsItem = nil;
     [upcomingSongsMenu release];
             [menu addItemWithTitle:@"Rewind"
                     action:@selector(rewind:)
                     keyEquivalent:@""];
+        } else if ([item isEqualToString:@"Show Player"]) {
+            [menu addItemWithTitle:[NSString stringWithFormat:@"Show %@", [currentRemote playerSimpleName]]
+                    action:@selector(showPlayer:)
+                    keyEquivalent:@""];
         } else if ([item isEqualToString:@"Upcoming Songs"]) {
             upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
                     action:nil
                     keyEquivalent:@""];
+            upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
+            [upcomingSongsItem setSubmenu:upcomingSongsMenu];
+            [upcomingSongsItem setEnabled:NO];
         } else if ([item isEqualToString:@"Playlists"]) {
             playlistItem = [menu addItemWithTitle:@"Playlists"
                     action:nil
             
             item = [ratingMenu addItemWithTitle:[NSString stringWithFormat:@"%@%@%@%@%@", fullStarChar, fullStarChar, fullStarChar, fullStarChar, fullStarChar] action:@selector(selectSongRating:) keyEquivalent:@""];
             [item setTag:100];
+            
+            [ratingItem setSubmenu:ratingMenu];
         } else if ([item isEqualToString:@"<separator>"]) {
             [menu addItem:[NSMenuItem separatorItem]];
         }
     }
     
-    [statusItem setMenu:menu];
+    if (playlistItem) {
+        [self rebuildPlaylistMenu];
+    }
+    
+    if (eqItem) {
+        [self rebuildEQPresetsMenu];
+    }
+    
+    isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
+    
+    if (upcomingSongsItem) {
+        [self rebuildUpcomingSongsMenu];
+    }
+    
+    if (ratingItem) {
+        if (isPlayingRadio || !playlist) {
+            [ratingItem setEnabled:NO];
+        } else {
+            int currentSongRating = ([currentRemote currentSongRating] * 5);
+            [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
+            lastSongRating = currentSongRating;
+            [[ratingMenu itemAtIndex:lastSongRating] setState:NSOnState];
+            [ratingItem setEnabled:YES];
+        }
+    }
     
-    [self updateMenu];
+    //Set the new unique song identifier
+    lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
+    
+    //If we're in a playlist or radio mode
+    if ( (trackInfoIndex > -1) && (playlist || isPlayingRadio) ) {
+        NSString *title;
+        
+        if ( (i = [menu indexOfItemWithTitle:@"No Song"]) && (i > -1) ) {
+            [menu removeItemAtIndex:i];
+            [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:i];
+        }
+        
+        title = [currentRemote currentSongTitle];
+        
+        if (!isPlayingRadio) {
+            if ([defaults boolForKey:@"showTime"]) {
+                NSString *length = [currentRemote currentSongLength];
+                char character = [length characterAtIndex:0];
+                if ( (character > '0') && (character < '9') ) {
+                    [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
+                }
+            }
+            
+            if ([defaults boolForKey:@"showArtist"]) {
+                NSString *artist = [currentRemote currentSongArtist];
+                
+                if ([artist length] > 0) {
+                    [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
+                }
+                
+                didHaveArtistName = (([artist length] > 0) ? YES : NO);
+            }
+            
+            if ([defaults boolForKey:@"showAlbum"]) {
+                NSString *album = [currentRemote currentSongAlbum];
+                
+                if ([album length] > 0) {
+                    [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
+                    didHaveAlbumName = (([album length]) ? YES : NO);
+                }
+            }
+        }
+        
+        if ([title length] > 0) {
+            [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", title] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
+        }
+    }
+    
+    [statusItem setMenu:menu];
     
     [self clearHotKeys];
     [self setupHotKeys];
 - (void)rebuildPlaylistMenu
 {
     NSArray *playlists = [currentRemote playlists];
-    int i, curPlaylist = [currentRemote currentPlaylistIndex];
-    
-    if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
-        return;
+    int i, currentPlaylist = [currentRemote currentPlaylistIndex];
     
     [playlistMenu release];
     playlistMenu = [[NSMenu alloc] initWithTitle:@""];
         NSString *playlistName = [playlists objectAtIndex:i];
         NSMenuItem *tempItem;
         tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
-        [tempItem setRepresentedObject:[NSNumber numberWithInt:i + 1]];
+        [tempItem setTag:i + 1];
         [playlistMenu addItem:tempItem];
         [tempItem release];
     }
     [playlistItem setSubmenu:playlistMenu];
     [playlistItem setEnabled:YES];
     
-    if (!isPlayingRadio) {
-        [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
+    if (!isPlayingRadio && currentPlaylist) {
+        [[playlistMenu itemAtIndex:currentPlaylist - 1] setState:NSOnState];
     }
 }
 
     NSMenuItem *enabledItem;
     int i;
     
-    if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
-        return;
-    
     [eqMenu release];
     eqMenu = [[NSMenu alloc] initWithTitle:@""];
     
     [eqMenu addItem:[NSMenuItem separatorItem]];
     
     for (i = 0; i < [eqPresets count]; i++) {
-        NSString *setName = [eqPresets objectAtIndex:i];
+        NSString *name = [eqPresets objectAtIndex:i];
         NSMenuItem *tempItem;
-       if (setName) {
-        tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
-        [tempItem setTag:i];
-        [eqMenu addItem:tempItem];
-        [tempItem release];
+       if (name) {
+            tempItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(selectEQPreset:) keyEquivalent:@""];
+            [tempItem setTag:i];
+            [eqMenu addItem:tempItem];
+            [tempItem release];
        }
     }
     [eqItem setSubmenu:eqMenu];
+    [eqItem setEnabled:YES];
     
     [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] + 1] setState:NSOnState];
 }
 
+- (void)updateRatingMenu
+{
+    int currentSongRating = ([currentRemote currentSongRating] * 5);
+    if ([currentRemote currentPlaylistIndex] && (currentSongRating != lastSongRating)) {
+        if ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist) {
+            return;
+        }
+        [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
+        lastSongRating = currentSongRating;
+        [[ratingMenu itemAtIndex:lastSongRating] setState:NSOnState];
+    }
+}
+
 - (void)timerUpdate
 {
     NSString *currentIdentifier = [currentRemote currentSongUniqueIdentifier];
-    if (![lastSongIdentifier isEqualToString:currentIdentifier]) {
-        [self updateMenu];
-    } else if (!isPlayingRadio && ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist)) {
+    if (![lastSongIdentifier isEqualToString:currentIdentifier] ||
+       (!isPlayingRadio && ([currentRemote classOfPlaylistAtIndex:[currentRemote currentPlaylistIndex]] == ITMTRemotePlayerRadioPlaylist))) {
         [self rebuildMenu];
     }
     
+    [self updateRatingMenu];
+    
     //Update Play/Pause menu item
     if (playPauseItem){
         if ([currentRemote playerPlayingState] == ITMTRemotePlayerPlaying) {
     }
 }
 
-- (void)updateMenu
-{
-    NSUserDefaults *defaults;
-    int playlist = [currentRemote currentPlaylistIndex];
-    BOOL wasPlayingRadio = isPlayingRadio;
-    
-    if ( (isAppRunning == ITMTRemotePlayerNotRunning) ) {
-        return;
-    }
-    
-    defaults = [NSUserDefaults standardUserDefaults];
-    
-    isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
-    
-    //
-    //
-    //rebuild submenus, make them properly enabled for once...
-    //
-    //
-    
-    if ( (trackInfoIndex > -1) && (playlist) ) {
-        NSString *title, *album, *artist;
-        int temp;
-        
-        lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
-        
-        if ( (temp = [menu indexOfItemWithTitle:@"No Song"]) && (temp > -1) ) {
-            [menu removeItemAtIndex:temp];
-            [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:temp];
-        } else {
-            if ([defaults boolForKey:@"showName"]) {
-                [menu removeItemAtIndex:trackInfoIndex + 1];
-            }
-        }
-        
-        title = [currentRemote currentSongTitle];
-        
-        if (!wasPlayingRadio && (temp == -1)) {
-            if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
-                [menu removeItemAtIndex:trackInfoIndex + 1];
-            }
-            if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
-                [menu removeItemAtIndex:trackInfoIndex + 1];
-            }
-            if ([defaults boolForKey:@"showTime"]) {
-                [menu removeItemAtIndex:trackInfoIndex + 1];
-            }
-        }
-        
-        if (!isPlayingRadio) {
-            ([defaults boolForKey:@"showAlbum"]) ? (album = [currentRemote currentSongAlbum]) :
-                                                   (album = @"");
-            ([defaults boolForKey:@"showArtist"]) ? (artist = [currentRemote currentSongArtist]) :
-                                                    (artist = @"");
-            if ([defaults boolForKey:@"showTime"]) {
-                [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
-            }
-            
-            if ([artist length] > 0) {
-                [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
-            }
-            
-            if ([album length] > 0) {
-                [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
-            }
-            
-            if ([defaults boolForKey:@"showArtist"]) {
-                didHaveArtistName = (([artist length] > 0) ? YES : NO);
-            }
-            
-            if ([defaults boolForKey:@"showAlbum"]) {
-                didHaveAlbumName = (([album length] > 0) ? YES : NO);
-            }
-        }
-        
-        if ([title length] > 0) {
-            [menu insertItemWithTitle:[NSString stringWithFormat:@"  %@", title] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
-        }
-    }
-}
-
-
 //
 //
 // Menu Selectors
 
 - (void)selectPlaylist:(id)sender
 {
-    int playlist = [[sender representedObject] intValue];
-    if (!isPlayingRadio) {
-        int curPlaylist = [currentRemote currentPlaylistIndex];
-        if (curPlaylist > 0) {
-            [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
-        }
-    }
+    int playlist = [sender tag];
     [currentRemote switchToPlaylistAtIndex:playlist];
-    [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
-    [self updateMenu];
 }
 
 - (void)selectEQPreset:(id)sender
 
 - (void)selectSongRating:(id)sender
 {
-    [[ratingMenu itemAtIndex:([currentRemote currentSongRating] / 20)] setState:NSOffState];
-    [currentRemote setCurrentSongRating:(float)[sender tag] / 100.0];
+    int newRating = [sender tag];
+    [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
     [sender setState:NSOnState];
+    [currentRemote setCurrentSongRating:(float)newRating / 100.0];
+    lastSongRating = newRating / 20;
 }
 
 - (void)playPause:(id)sender
 
 - (void)showPreferences:(id)sender
 {
-    if (!prefsController) {
-        prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
-        [self clearHotKeys];
-    }
+    [[PreferencesController sharedPrefs] setController:self];
+    [[PreferencesController sharedPrefs] showPrefsWindow:self];
 }
 
 - (void)closePreferences
     if ( ( isAppRunning == ITMTRemotePlayerRunning) ) {
         [self setupHotKeys];
     }
-    [prefsController release];
-    prefsController = nil;
 }
 
+- (ITMTRemote *)currentRemote
+{
+    return currentRemote;
+}
 
 //
 //
             }
         }
         
-        [statusWindow setText:stringToShow];
+        //
+        //SHOW THE STATUS WINDOW HERE WITH STRING stringToShow
+        //
+        
+        /*[statusWindow setText:stringToShow];
         [NSTimer scheduledTimerWithTimeInterval:3.0
                     target:self
                     selector:@selector(fadeAndCloseStatusWindow)
                     userInfo:nil
-                    repeats:NO];
+                    repeats:NO];*/
     }
 }
 
             int i;
             NSString *songs = @"";
             
-            statusWindow = [ITTransientStatusWindow sharedWindow];
             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
                 if (i <= numSongs) {
                     NSString *curSong = [currentRemote songTitleAtIndex:i];
                     songs = [songs stringByAppendingString:@"\n"];
                 }
             }
-            [statusWindow setText:songs];
+            
+            //
+            //SHOW STATUS WINDOW HERE WITH STRING songs
+            //
+            
+            /*[statusWindow setText:songs];
             [NSTimer scheduledTimerWithTimeInterval:3.0
                         target:self
                         selector:@selector(fadeAndCloseStatusWindow)
                         userInfo:nil
-                        repeats:NO];
+                        repeats:NO];*/
         }
     }
 }