Scripts now run when the player switches to no song playing.
[MenuTunes.git] / MainController.m
index 307800f..bf3bda3 100755 (executable)
 #import "StatusWindowController.h"
 #import "StatusItemHack.h"
 
+@interface NSImage (WeeAdditions)
+- (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize;
+@end
+
+@implementation NSImage (WeeAdditions)
+
+- (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
+{
+    NSImage *newImage;
+    NSImageRep *rep = [self bestRepresentationForDevice:nil];
+    
+    newImage = [[NSImage alloc] initWithSize:scaledSize];
+    [newImage lockFocus];
+    {
+        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
+        [[NSGraphicsContext currentContext] setShouldAntialias:YES];
+        [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
+    }
+    [newImage unlockFocus];
+    return [newImage autorelease];
+}
+
+@end
+
 @interface MainController(Private)
 - (ITMTRemote *)loadRemote;
 - (void)timerUpdate;
@@ -305,6 +329,21 @@ static MainController *sharedController;
     
     if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
         ITDebugLog(@"The song changed.");
+        
+        if ([df boolForKey:@"runScripts"]) {
+            NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
+            NSEnumerator *scriptsEnum = [scripts objectEnumerator];
+            NSString *nextScript;
+            while ( (nextScript = [scriptsEnum nextObject]) ) {
+                NSDictionary *error;
+                NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
+                if (!currentScript || ![currentScript executeAndReturnError:nil]) {
+                    ITDebugLog(@"Error running script %@.", nextScript);
+                }
+                [currentScript release];
+            }
+        }
+        
         timerUpdating = YES;
         
         NS_DURING
@@ -316,6 +355,18 @@ static MainController *sharedController;
             }
             
             [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
+            
+            NSString *artist = [[self currentRemote] currentSongArtist];
+            NSString *title = [[self currentRemote] currentSongTitle];
+            NSString *toolTip;
+            if (artist) {
+                toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
+            } else if (title) {
+                toolTip = title;
+            } else {
+                toolTip = @"No Song Playing";
+            }
+            [statusItem setToolTip:toolTip];
         NS_HANDLER
             [self networkError:localException];
         NS_ENDHANDLER
@@ -417,8 +468,8 @@ static MainController *sharedController;
 {
     ITDebugLog(@"Selecting playlist %i", index);
     NS_DURING
-        //[[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
-        [[self currentRemote] switchToPlaylistAtIndex:index];
+        [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
+        //[[self currentRemote] switchToPlaylistAtIndex:index];
     NS_HANDLER
         [self networkError:localException];
     NS_ENDHANDLER
@@ -471,7 +522,12 @@ static MainController *sharedController;
     } else {
         ITDebugLog(@"Launching player.");
         NS_DURING
-            if (![[NSWorkspace sharedWorkspace] launchApplication:[[self currentRemote] playerFullName]]) {
+            NSString *path;
+            if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
+            } else {
+                path = [[self currentRemote] playerFullName];
+            }
+            if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
                 ITDebugLog(@"Error Launching Player");
             }
         NS_HANDLER
@@ -756,8 +812,13 @@ static MainController *sharedController;
         }
         
         if ( [df boolForKey:@"showAlbumArtwork"] ) {
+           NSSize oldSize, newSize;
              NS_DURING
-                art = [[self currentRemote] currentSongAlbumArt];
+                art = [[self currentRemote] currentSongAlbumArt];
+                oldSize = [art size];
+                if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
+                else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
+                art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
             NS_HANDLER
                 [self networkError:localException];
             NS_ENDHANDLER
@@ -766,7 +827,6 @@ static MainController *sharedController;
     } else {
         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
     }
-
     [statusWindowController showSongInfoWindowWithSource:source
                                                    title:title
                                                    album:album