A couple more modifications, as well as some comments to ease others'
[MenuTunes.git] / iTunesRemote.m
index 073b549..5781211 100755 (executable)
+/* Copyright (c) 2002 - 2003 by iThink Software. All Rights Reserved. */
+
 #import "iTunesRemote.h"
 
 #import "iTunesRemote.h"
 
+
 @implementation iTunesRemote
 
 @implementation iTunesRemote
 
-+ (id)remote
-{
++ (id)remote {
     return [[[iTunesRemote alloc] init] autorelease];
 }
 
     return [[[iTunesRemote alloc] init] autorelease];
 }
 
-- (NSString *)title
-{
-    return @"iTunes Plug-in";
-}
-
-- (NSString *)information;
-{
-    return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
-}
-
-- (NSImage *)icon
-{
+- (id)valueOfProperty:(ITMTRemoteProperty)property {
+    // Get from Info.plist
     return nil;
 }
 
     return nil;
 }
 
-- (BOOL)begin
-{
-    iTunesPSN = [self iTunesPSN];
-
-    //Register for application termination in NSWorkspace
-    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
-    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
-
-    return YES;
+- (NSDictionary *)propertiesAndValues {
+    // Get from Info.plist
+    return nil;
 }
 
 }
 
-- (BOOL)halt
-{
-    iTunesPSN.highLongOfPSN = kNoProcess;
-
-    //Unregister for application termination in NSWorkspace
-    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
-
-    return YES;
+- (ITMTPlayerStyle)playerStyle {
+    return ITMTSinglePlayerStyle;
 }
 
 }
 
-- (BOOL)isAppRunning
-{
-    NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
-    int i,count = [apps count];
-
-    for (i = 0; i < count; i++) {
-        if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
-                isEqualToString:@"iTunes"]) {
+- (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;
         }
             return YES;
         }
+    } else {
+        return NO;
     }
     }
-    return NO;
 }
 
 }
 
-- (PlayerState)playerState
-{
-    long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-    
-    switch (result)
-    {
-        default:
-        case 'kPSS':
-            return stopped;
-        case 'kPSP':
-            return paused;
-        case 'kPSp':
-            return playing;
-        case 'kPSR':
-            return rewinding;
-        case 'kPSF':
-            return forwarding;
+- (BOOL)deactivate {
+    if ( _activated ) {
+        _currentPSN = kNoProcess;
+        _player = nil;
+        _activated = NO;
+        return YES;
+    } else {
+        return NO;
     }
     }
-    
-    return stopped;
-}
-
-- (NSArray *)playlists
-{
-    long i = 0;
-    const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]];
-    NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
-    NSLog(@"entered playlist");
-
-          for (i = 1; i <= numPlaylists; i++) {
-                 const long j = i;
-                 NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } }",(unsigned long)j];
-                 NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN] fixForAECrappiness:i];
-                 NSLog(@"sent event cur %d max %d",i,numPlaylists);
-                 [playlists addObject:theObj];
-          }
-          NSLog(@"playlists end");
-          return [playlists autorelease];
-}
-
-- (int)numberOfSongsInPlaylistAtIndex:(int)index
-{
-    return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]]
-
-;
-}
-
-- (NSString *)classOfPlaylistAtIndex:(int)index
-{
-    int realResult = [[ITAppleEventCenter sharedCenter]
-                sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-
-    if (realResult == 'cRTP') return @"radio tuner playlist";
-    else return @"playlist";
-}
-
-- (int)currentPlaylistIndex
-{
-    int result;
-    result = [[ITAppleEventCenter sharedCenter]
-                sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-    return result;
-}
-
-- (NSString *)songTitleAtIndex:(int)index
-{
-    return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-}
-
-- (int)currentSongIndex
-{
-    int result;
-    result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-    return result;
-}
-
-- (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:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-}
-
-- (NSString *)currentSongRemaining
-{
-    long duration = [[ITAppleEventCenter sharedCenter]
-                        sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-    long current = [[ITAppleEventCenter sharedCenter]
-                        sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-
-    return [[NSNumber numberWithLong:duration - current] stringValue];
-}
-
-- (NSArray *)eqPresets;
-{
-    int i;
-    long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:[self iTunesPSN]];
-    NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
-
-
-<<<<<<< iTunesRemote.m
-          for (i = 1; i <= numPresets; i++) {
-                 NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:%d, from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]];
-                 [presets addObject:theObj];
-=======
-          for (i = 0; i < numPresets; i++) {
-                 NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN] fixForAECrappiness:i];
-                 if (theObj) [presets addObject:theObj];
->>>>>>> 1.16
-          }
-          return [presets autorelease];
-}
-
-- (int)currentEQPresetIndex
-{
-    int result;
-    result = [[ITAppleEventCenter sharedCenter]
-                sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
-                                                                                                         fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"
-                                                                                                                                   appPSN:[self iTunesPSN]];
-    return result;
-}
-
-- (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;
-}
-
-- (BOOL)goToPreviousSong
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
-                                                                                                                                                  appPSN:[self iTunesPSN]];
-    return YES;
-}
-
-- (BOOL)fastForward
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast"
-                                                                                                                                                  appPSN:[self iTunesPSN]];
-    return YES;
+- (ITMTPlayer *)currentPlayer {
+    if (_activated) {
+        return _player;
+    } else {
+        return nil;
+    }
 }
 
 }
 
-- (BOOL)rewind
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd"
-                                                                                                                                                  appPSN:[self iTunesPSN]];
-    return YES;
+- (NSArray *)players {
+    if (_activated) {
+        return [NSArray arrayWithObject:_player];
+    } else {
+        return nil;
+    }
 }
 
 }
 
+#pragma mark -
+#pragma mark INTERNAL METHODS
+#pragma mark -
 
 
-- (BOOL)switchToPlaylistAtIndex:(int)index
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'name', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"hook" eventID:@"Play" appPSN:[self iTunesPSN]];
+- (BOOL)launchiTunes {
     return NO;
 }
 
     return NO;
 }
 
-- (BOOL)switchToSongAtIndex:(int)index
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",index] eventClass:@"hook" eventID:@"Play" appPSN:[self iTunesPSN]];
-    return NO;
-}
-
-- (BOOL)switchToEQAtIndex:(int)index
-{
-    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'name', want:type('cEQP'), seld:long(%lu), from:'null'() }, '----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }",index] eventClass:@"core" eventID:@"setd" appPSN:[self iTunesPSN]];
-    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:[self iTunesPSN]];
+- (BOOL)iTunesIsRunning {
     return NO;
 }
 
 - (ProcessSerialNumber)iTunesPSN
 {
     return NO;
 }
 
 - (ProcessSerialNumber)iTunesPSN
 {
-    NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
     ProcessSerialNumber number;
     ProcessSerialNumber number;
-    int i, count = [apps count];
-
-              number.highLongOfPSN = kNoProcess;
-
-              for (i = 0; i < count; i++)
-                     {
-                         NSDictionary *curApp = [apps objectAtIndex:i];
-                        
-                                if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
-                                    {
-                                               number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
-                                               number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
-                                           }
-                            }
-    return number;}
-
-- (void)applicationLaunched:(NSNotification *)note
-{
-    NSDictionary *info = [note userInfo];
-
-    if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
-        iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
-        iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
-
-        [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
-    }
-}
-
-- (void)applicationTerminated:(NSNotification *)note
-{
-    NSDictionary *info = [note userInfo];
-
-    if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
-        iTunesPSN.highLongOfPSN = kNoProcess;
-        [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
+    number.highLongOfPSN = kNoProcess;
+    number.lowLongOfPSN = 0;
+    
+    while ( (GetNextProcess(&number) == noErr) ) 
+    {
+        CFStringRef name;
+        if ( (CopyProcessName(&number, &name) == noErr) )
+        {
+            if ([(NSString *)name isEqualToString:@"iTunes"])
+            {
+                return number;
+            }
+            [(NSString *)name release];
+        }
     }
     }
+    return number;
 }
 
 @end
 }
 
 @end