FINALLY got CVS working. Fixed another elusive menu bug that hated me.
[MenuTunes.git] / MenuTunes.m
index d6a25cf..98748eb 100755 (executable)
@@ -1,22 +1,18 @@
 /*
 Things to do:
 ¥ 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!
+¥ Manual and webpage
+¥ Finish up registration frontend
 */
 
 #import "MenuTunes.h"
-// #import "MenuTunesView.h"
 #import "PreferencesController.h"
 #import "HotKeyCenter.h"
 #import "StatusWindowController.h"
 
 @interface MenuTunes(Private)
-- (void)registerDefaultsIfNeeded;
 - (void)updateMenu;
 - (void)rebuildUpcomingSongsMenu;
 - (void)rebuildPlaylistMenu;
@@ -48,10 +44,10 @@ Things to do:
     {
         [self rebuildMenu];
         refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
-                                                        target:self
-                                                      selector:@selector(timerUpdate)
-                                                      userInfo:nil
-                                                       repeats:YES];
+                                                    target:self
+                                                    selector:@selector(timerUpdate)
+                                                    userInfo:nil
+                                                    repeats:YES];
     }
     else
     {
@@ -62,16 +58,15 @@ Things to do:
         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
         refreshTimer = nil;
     }
+
+    statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
+                                              withLength:NSSquareStatusItemLength];
     
-    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
-    [[[statusItem _button] cell] setType:NSNullCellType];
     [statusItem setImage:[NSImage imageNamed:@"menu"]];
-    [[statusItem _button] setAlternateImage:[NSImage imageNamed:@"selected_image"]];
-    [statusItem setHighlightMode:YES];
+    [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
     [statusItem setMenu:menu];
-    [statusItem retain];
-//  view = [[MenuTunesView alloc] initWithFrame:[[statusItem view] frame]];
-//  [statusItem setView:view];
+    // Below line of code is for creating builds for Beta Testers
+    // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
 }
 
 
@@ -82,8 +77,14 @@ Things to do:
 
 - (void)registerDefaultsIfNeeded
 {
-    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"menu"]) {
-        [[NSUserDefaults standardUserDefaults] setObject:
+    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",
@@ -99,6 +100,53 @@ Things to do:
                 @"<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;
+            }
+        }
+        
+        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]];
+                
+                AECreateDesc(typeChar, [script cString], [script cStringLength], 
+            &scriptDesc);
+                
+                OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
+                
+                AEDisposeDesc(&scriptDesc);
+                AEDisposeDesc(&resultDesc);
+            }
+        }
+    }
+    
+    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"];
     }
 }
 
@@ -110,7 +158,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];
     }
@@ -187,8 +237,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;
     }
@@ -196,27 +247,62 @@ Things to do:
     if (upcomingSongsItem) {
         [self rebuildUpcomingSongsMenu];
     }
+    
     if (playlistItem) {
         [self rebuildPlaylistMenu];
     }
+    
     if (eqItem) {
         [self rebuildEQPresetsMenu];
     }
     
     if (trackInfoIndex > -1)
     {
-        NSString *curSongName;
+        NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
+        
+        if ([defaults boolForKey:@"showAlbum"]) {
+            curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
+        }
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+            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 (didHaveAlbumName) {
+                if ([defaults boolForKey:@"showName"]) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
+                    [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
@@ -225,11 +311,14 @@ Things to do:
                     [menuItem release];
                 }
             }
-            menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curSongName]
-                                                action:nil
-                                                keyEquivalent:@""];
-            [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
-            [menuItem release];
+            
+            if ([defaults boolForKey:@"showName"]) {
+                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:@""];
@@ -237,12 +326,22 @@ Things to do:
                 [menu insertItem:menuItem atIndex:trackInfoIndex];
                 [menuItem release];
             }
-            
         } 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];
             }
             
@@ -250,8 +349,15 @@ Things to do:
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+            didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
+        }
+            
+        if ([defaults boolForKey:@"showAlbum"]) {
+            didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
+        }
     }
-    didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
 }
 
 //Rebuild the upcoming songs submenu. Can be improved a lot.
@@ -522,7 +628,7 @@ Things to do:
             if ([(NSString *)procName isEqualToString:@"iTunes"]) {
                 return procNum;
             }
-            [(NSString *)procName release];
+            CFRelease(procName);
         }
     }
     return procNum;
@@ -649,7 +755,6 @@ isEqualToString:@"rewinding"]) {
     if (!statusController && [trackName length]) {
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         NSString *stringToShow = @"";
-        int lines = 1;
         
         if ([defaults boolForKey:@"showName"]) {
             if ([defaults boolForKey:@"showArtist"]) {
@@ -658,10 +763,6 @@ isEqualToString:@"rewinding"]) {
             }
             stringToShow = [stringToShow stringByAppendingString:trackName];
             stringToShow = [stringToShow stringByAppendingString:@"\n"];
-            if ([trackName length] > 38) {
-                lines++;
-            }
-            lines++;
         }
         
         if ([defaults boolForKey:@"showAlbum"]) {
@@ -669,7 +770,6 @@ isEqualToString:@"rewinding"]) {
             if ([trackAlbum length]) {
                 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
                 stringToShow = [stringToShow stringByAppendingString:@"\n"];
-                lines++;
             }
         }
         
@@ -677,7 +777,6 @@ isEqualToString:@"rewinding"]) {
             NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
             if ([trackTime length]) {
                 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
-                lines++;
             }
         }
         
@@ -694,12 +793,12 @@ isEqualToString:@"rewinding"]) {
         }
         
         statusController = [[StatusWindowController alloc] init];
-        [statusController setTrackInfo:stringToShow lines:lines];
+        [statusController setTrackInfo:stringToShow];
         [NSTimer scheduledTimerWithTimeInterval:3.0
-                                         target:self
-                                       selector:@selector(fadeAndCloseStatusWindow)
-                                       userInfo:nil
-                                        repeats:NO];
+                                    target:self
+                                    selector:@selector(fadeAndCloseStatusWindow)
+                                    userInfo:nil
+                                    repeats:NO];
     }
 }
 
@@ -723,7 +822,7 @@ isEqualToString:@"rewinding"]) {
                     songs = [songs stringByAppendingString:@"\n"];
                 }
             }
-            [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
+            [statusController setUpcomingSongs:songs];
             [NSTimer scheduledTimerWithTimeInterval:3.0
                                              target:self
                                            selector:@selector(fadeAndCloseStatusWindow)