A couple more modifications, as well as some comments to ease others'
[MenuTunes.git] / iTunesRemote.m
index 8886102..5781211 100755 (executable)
+/* Copyright (c) 2002 - 2003 by iThink Software. All Rights Reserved. */
+
 #import "iTunesRemote.h"
 
+
 @implementation iTunesRemote
 
-+ (id)remote
-{
++ (id)remote {
     return [[[iTunesRemote alloc] init] autorelease];
 }
 
-- (NSString *)title
-{
-    return @"iTunes Plug-in";
-}
-
-- (NSString *)information;
-{
-    return @"Default MenuTunes plugin to control iTunes.";
-}
-
-- (NSImage *)icon
-{
-    return nil;
-}
-
-- (BOOL)begin
-{
-    iTunesPSN = [self iTunesPSN];
-    
-    //Register for application termination in NSWorkspace
-    
-    NSLog(@"iTunes Plugin loaded");
-    return YES;
-}
-
-- (BOOL)halt
-{
-    iTunesPSN.highLongOfPSN = kNoProcess;
-    
-    //Unregister for application termination in NSWorkspace
-    return YES;
-}
-
-- (NSArray *)sources
-{
-    return nil;
-}
-
-- (int)currentSourceIndex
-{
-    return nil;
-}
-
-- (NSArray *)playlistsForCurrentSource
-{
-    return nil;
-}
-
-- (int)currentPlaylistIndex
-{
-    NSString *result;
-    result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pidx"
-                fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-    NSLog(@"result: %@", result);
-    return nil;
-}
-
-- (NSString *)songTitleAtIndex
-{
-    return nil; 
-}
-
-- (int)currentSongIndex
-{
-    NSString *result;
-    result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pidx"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-    NSLog(@"result: %@", result);
-    return nil;
-}
-
-- (NSString *)currentSongTitle
-{
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongArtist
-{
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongAlbum
-{
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongGenre
-{
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongLength
-{
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur"
-                fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongRemaining
-{
-    NSString* duration = [[ITAppleEventCenter sharedCenter]
-                        sendTwoTierAEWithRequestedKey:@"pDur"
-                        fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
-                        appPSN:[self iTunesPSN]];
-    NSString* current = [[ITAppleEventCenter sharedCenter]
-                        sendAEWithRequestedKey:@"pPos"
-                        eventClass:@"core" eventID:@"getd"
-                        appPSN:[self iTunesPSN]];
-    NSLog(@"%@ %@", duration, current);
-    //return [[NSNumber numberWithInt:duration - current] stringValue];
+- (id)valueOfProperty:(ITMTRemoteProperty)property {
+    // Get from Info.plist
     return nil;
 }
 
-- (NSArray *)eqPresets;
-{
+- (NSDictionary *)propertiesAndValues {
+    // Get from Info.plist
     return nil;
 }
 
-- (BOOL)play
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
-            appPSN:[self iTunesPSN]];
-    return YES;
-}
-
-- (BOOL)pause
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus"
-            appPSN:[self iTunesPSN]];
-    return YES;
-}
-
-- (BOOL)goToNextSong
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
-            appPSN:[self iTunesPSN]];
-    return YES;
+- (ITMTPlayerStyle)playerStyle {
+    return ITMTSinglePlayerStyle;
 }
 
-- (BOOL)goToPreviousSong
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
-            appPSN:[self iTunesPSN]];
-    return YES;
+- (BOOL)activate {
+    if ( !_activated ) {
+        if ( [self iTunesIsRunning] ) {
+            _currentPSN = [self iTunesPSN];
+        } else {
+            if ( [self launchiTunes] ) {
+                _currentPSN = [self iTunesPSN];
+            } else {
+                return NO;
+            }
+        }
+        if ( ( _player = [iTunesPlayer sharedPlayerForRemote:self] ) ) {
+            _activated = YES;
+            return YES;
+        }
+    } else {
+        return NO;
+    }
 }
 
-- (BOOL)goToNextPlaylist
-{
-    return NO;
+- (BOOL)deactivate {
+    if ( _activated ) {
+        _currentPSN = kNoProcess;
+        _player = nil;
+        _activated = NO;
+        return YES;
+    } else {
+        return NO;
+    }
 }
 
-- (BOOL)goToPreviousPlaylist
-{
-    return NO;
+- (ITMTPlayer *)currentPlayer {
+    if (_activated) {
+        return _player;
+    } else {
+        return nil;
+    }
 }
 
-- (BOOL)switchToSourceAtIndex:(int)index
-{
-    return NO;
+- (NSArray *)players {
+    if (_activated) {
+        return [NSArray arrayWithObject:_player];
+    } else {
+        return nil;
+    }
 }
 
-- (BOOL)switchToPlaylistAtIndex:(int)index
-{
-    return NO;
-}
+#pragma mark -
+#pragma mark INTERNAL METHODS
+#pragma mark -
 
-- (BOOL)switchToSongAtIndex:(int)index
-{
+- (BOOL)launchiTunes {
     return NO;
 }
 
-- (BOOL)switchToEQAtIndex:(int)index
-{
+- (BOOL)iTunesIsRunning {
     return NO;
 }
 
 - (ProcessSerialNumber)iTunesPSN
 {
-    NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
     ProcessSerialNumber number;
-    int i;
-    
     number.highLongOfPSN = kNoProcess;
+    number.lowLongOfPSN = 0;
     
-    for (i = 0; i < [apps count]; i++)
+    while ( (GetNextProcess(&number) == noErr) ) 
     {
-        NSDictionary *curApp = [apps objectAtIndex:i];
-        
-        if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
+        CFStringRef name;
+        if ( (CopyProcessName(&number, &name) == noErr) )
         {
-            number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
-            number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
+            if ([(NSString *)name isEqualToString:@"iTunes"])
+            {
+                return number;
+            }
+            [(NSString *)name release];
         }
     }
     return number;