+ ITDebugLog(@"Showing player primary interface.");
+
+ if ([self playerRunningState] == ITMTRemotePlayerRunning) {
+ ITDebugLog(@"Showing player interface.");
+ //If not minimized and visible
+ if ( ([ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pMin'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } }", 'core', 'getd', &savedPSN) booleanValue] == 0) &&
+ ([ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pvis'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } }", 'core', 'getd', &savedPSN) booleanValue] != 0) &&
+ [[[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"] ) {
+ //set minimized of browser window 1 to true
+ ITSendAEWithString(@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pMin'), from:obj { form:'indx', want:type('cBrW'), seld:long(1), from:'null'() } }", 'core', 'setd', &savedPSN);
+ } else {
+ //set minimized of browser window 1 to false
+ ITSendAEWithString(@"data:long(0), '----':obj { form:'prop', want:type('prop'), seld:type('pMin'), from:obj { form:'indx', want:type('cBrW'), seld:long(1), from:'null'() } }", 'core', 'setd', &savedPSN);
+ }
+ //set visible of browser window 1 to true
+ ITSendAEWithString(@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pvis'), from:obj { form:'indx', want:type('cBrW'), seld:long(1), from:'null'() } }", 'core', 'setd', &savedPSN);
+ //active iTunes
+ ITSendAEWithString(@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }", 'core', 'setd', &savedPSN);
+ ITDebugLog(@"Done showing player primary interface.");
+ return YES;
+ } else {
+ NSString *path;
+ ITDebugLog(@"Launching player.");
+ if ( (path = [[NSUserDefaults standardUserDefaults] stringForKey:@"CustomPlayerPath"]) ) {
+ } else {
+ path = [self playerFullName];
+ }
+ if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
+ ITDebugLog(@"Error Launching Player");
+ return NO;
+ }
+ return YES;
+ }
+}
+
+- (ITMTRemotePlayerRunningState)playerRunningState
+{
+ NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
+ int i;
+ int count = [apps count];
+
+ for (i = 0; i < count; i++) {
+ if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
+ ITDebugLog(@"Player running state: 1");
+ return ITMTRemotePlayerRunning;
+ }
+ }
+ ITDebugLog(@"Player running state: 0");
+ return ITMTRemotePlayerNotRunning;
+}
+
+- (ITMTRemotePlayerPlayingState)playerPlayingState
+{
+ SInt32 result;
+
+ ITDebugLog(@"Getting player playing state");
+ result = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }", 'core', 'getd', &savedPSN) typeCodeValue];
+ switch (result)
+ {
+ case 'kPSP':
+ ITDebugLog(@"Getting player playing state done. Player state: Playing");
+ return ITMTRemotePlayerPlaying;
+ case 'kPSp':
+ ITDebugLog(@"Getting player playing state done. Player state: Paused");
+ return ITMTRemotePlayerPaused;
+ case 'kPSR':
+ ITDebugLog(@"Getting player playing state done. Player state: Rewinding");
+ return ITMTRemotePlayerRewinding;
+ case 'kPSF':
+ ITDebugLog(@"Getting player playing state done. Player state: Forwarding");
+ return ITMTRemotePlayerForwarding;
+ case 'kPSS':
+ default:
+ ITDebugLog(@"Getting player playing state done. Player state: Stopped");
+ return ITMTRemotePlayerStopped;
+ }
+ ITDebugLog(@"Getting player playing state done. Player state: Stopped");
+ return ITMTRemotePlayerStopped;
+}
+
+/*- (NSArray *)playlists
+{
+ long i = 0;
+ const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
+ NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
+
+ 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:savedPSN];
+ [playlists addObject:theObj];
+ }
+ return [playlists autorelease];
+}*/
+
+//Full source awareness
+/*- (NSArray *)playlists
+{
+ unsigned long i, k;
+ SInt32 numSources = [ITSendAEWithString(@"kocl:type('cSrc'), '----':()", 'core', 'cnte', &savedPSN) int32Value];
+ NSMutableArray *allSources = [[NSMutableArray alloc] init];
+
+ ITDebugLog(@"Getting playlists.");
+ if (numSources == 0) {
+ [allSources release];
+ ITDebugLog(@"No sources.");
+ return nil;
+ }
+
+ for (k = 1; k <= numSources ; k++) {
+ SInt32 numPlaylists = [ITSendAEWithString([NSString stringWithFormat:@"kocl:type('cPly'), '----':obj { form:'indx', want:type('cSrc'), seld:long(%u), from:() }",k], 'core', 'cnte', &savedPSN) int32Value];
+ SInt32 fourcc = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pKnd'), from:obj { form:'indx', want:type('cSrc'), seld:long(%u), from:() } }",k], 'core', 'getd', &savedPSN) int32Value];
+ NSString *sourceName = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cSrc'), seld:long(%u), from:() } }",k], 'core', 'getd', &savedPSN) stringValue];
+ SInt32 index = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'indx', want:type('cSrc'), seld:long(%u), from:() } }",k], 'core', 'getd', &savedPSN) int32Value];
+ unsigned long class;
+ if (sourceName) {
+ NSMutableArray *aSource = [[NSMutableArray alloc] init];
+ [aSource addObject:sourceName];
+ switch (fourcc) {
+ case 'kTun':
+ class = ITMTRemoteRadioSource;
+ break;
+ case 'kDev':
+ class = ITMTRemoteGenericDeviceSource;
+ break;
+ case 'kPod':
+ class = ITMTRemoteiPodSource;
+ break;
+ case 'kMCD':
+ case 'kACD':
+ class = ITMTRemoteCDSource;
+ break;
+ case 'kShd':
+ class = ITMTRemoteSharedLibrarySource;
+ break;
+ case 'kUnk':
+ case 'kLib':
+ default:
+ class = ITMTRemoteLibrarySource;
+ break;
+ }
+ ITDebugLog(@"Adding source %@ of type %i at index %i", sourceName, class, index);
+ [aSource addObject:[NSNumber numberWithInt:class]];
+ [aSource addObject:[NSNumber numberWithInt:index]];
+ for (i = 1; i <= numPlaylists; i++) {
+ NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%u), from:obj { form:'indx', want:type('cSrc'), seld:long(%u), from:() } } }",i,k];
+ NSString *theObj = [ITSendAEWithString(sendStr, 'core', 'getd', &savedPSN) stringValue];
+ ITDebugLog(@" - Adding playlist %@", theObj);
+ if (theObj) {
+ [aSource addObject:theObj];
+ }
+ }
+ [allSources addObject:[aSource autorelease]];
+ } else {
+ ITDebugLog(@"Source at index %i disappeared.", k);
+ }
+ }
+ NSLog(@"playlists: %@", allSources);
+ ITDebugLog(@"Finished getting playlists.");
+ return [allSources autorelease];
+}*/
+
+- (NSArray *)playlists
+{
+ SInt32 numSources = [ITSendAEWithString(@"kocl:type('cSrc'), '----':()", 'core', 'cnte', &savedPSN) int32Value];
+ NSMutableArray *sources = [[NSMutableArray alloc] init];
+ int i;
+
+ ITDebugLog(@"Getting playlists.");
+ if (numSources == 0) {
+ [sources release];
+ ITDebugLog(@"No sources.");
+ return nil;
+ }
+
+ //Loop through each source
+ for (i = 1; i <= numSources; i++) {
+ FourCharCode fourcc = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pKnd'), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } }", i], 'core', 'getd', &savedPSN) typeCodeValue]; //Type of the current source
+ NSString *sourceName = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } }", i], 'core', 'getd', &savedPSN) stringValue]; //Name of the current source
+ SInt32 index = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } }", i], 'core', 'getd', &savedPSN) int32Value]; //Index of the current source
+ ITMTRemotePlayerSource class; //The class of the current source
+
+ //Make a new PlaylistNode for this source
+ PlaylistNode *sourceNode = [PlaylistNode playlistNodeWithName:sourceName type:ITMTSourceNode index:index];
+
+ switch (fourcc) {
+ case 'kTun':
+ class = ITMTRemoteRadioSource;
+ break;
+ case 'kDev':
+ class = ITMTRemoteGenericDeviceSource;
+ break;
+ case 'kPod':
+ class = ITMTRemoteiPodSource;
+ break;
+ case 'kMCD':
+ case 'kACD':
+ class = ITMTRemoteCDSource;
+ break;
+ case 'kShd':
+ class = ITMTRemoteSharedLibrarySource;
+ break;
+ case 'kUnk':
+ case 'kLib':
+ default:
+ class = ITMTRemoteLibrarySource;
+ break;
+ }
+ [sourceNode setSourceType:class];
+ ITDebugLog(@"New source %@ of type %i at index %i", sourceName, class, index);
+
+ int j;
+ SInt32 numPlaylists = [ITSendAEWithString([NSString stringWithFormat:@"kocl:type('cPly'), '----':obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() }", i], 'core', 'cnte', &savedPSN) int32Value]; //Number of playlists in the current source
+
+ //Pass 1, add all the playlists into the main array
+ for (j = 1; j <= numPlaylists; j++) {
+ NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%i), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } } }", j, (_iTunesVersion >= 5) ? i : index];
+ NSString *parentSendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'prop', want:type('prop'), seld:type('pPlP'), from:obj { form:'indx', want:type('cPly'), seld:long(%i), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } } } }", j, i];
+ NSString *theObj = [ITSendAEWithString(sendStr, 'core', 'getd', &savedPSN) stringValue], *parent = [ITSendAEWithString(parentSendStr, 'core', 'getd', &savedPSN) stringValue];
+ ITDebugLog(@" - Adding playlist %@", theObj);
+ if (theObj) {
+ FourCharCode code = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pSpK'), from:obj { form:'indx', want:type('cPly'), seld:long(%i), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } } }", j, i], 'core', 'getd', &savedPSN) typeCodeValue];
+ ITMTNodeType type;
+ switch (code) {
+ case 'kSpN':
+ type = ITMTPlaylistNode;
+ break;
+ case 'kSpF':
+ type = ITMTFolderNode;
+ break;
+ case 'kSpS':
+ type = ITMTPartyShuffleNode;
+ break;
+ case 'kSpP':
+ type = ITMTPodcastsNode;
+ break;
+ case 'kSpM':
+ type = ITMTPurchasedMusicNode;
+ break;
+ case 'kSpV':
+ type = ITMTVideosNode;
+ break;
+ }
+ PlaylistNode *node = [PlaylistNode playlistNodeWithName:theObj type:type index:j];
+ [[sourceNode children] addObject:node];
+ if (parent) {
+ int parentIndex = [ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'prop', want:type('prop'), seld:type('pPlP'), from:obj { form:'indx', want:type('cPly'), seld:long(%i), from:obj { form:'indx', want:type('cSrc'), seld:long(%i), from:() } } } }", j, i], 'core', 'getd', &savedPSN) int32Value];
+ [node setParent:[PlaylistNode playlistNodeWithName:parent type:ITMTFolderNode index:parentIndex]];
+ } else {
+ [node setParent:sourceNode];
+ }
+ }
+ }
+
+ //Pass 2, nest each item under its proper parent. Once everything has been nested, delete the original from the main array.
+ NSEnumerator *enumerator = [[sourceNode children] objectEnumerator];
+ PlaylistNode *nextNode;
+ NSMutableArray *nested = [[NSMutableArray alloc] init];
+
+ while ( (nextNode = [enumerator nextObject]) ) {
+ PlaylistNode *pNode = [nextNode parent];
+ if ([pNode type] == ITMTFolderNode) {
+ PlaylistNode *newParent = nil;
+ int k;
+ for (k = 0; !newParent; k++) {
+ PlaylistNode *test = [[sourceNode children] objectAtIndex:k];
+ if ([test index] == [pNode index]) {
+ newParent = test;
+ }
+ }
+ [[[nextNode parent] children] removeObject:nextNode];
+ [nextNode setParent:newParent];
+ [[newParent children] addObject:nextNode];
+ [newParent setType:ITMTFolderNode];
+ [nested addObject:nextNode];
+ }
+ }
+
+ NSEnumerator *nestEnumerator = [nested objectEnumerator];
+ while ( (nextNode = [nestEnumerator nextObject]) ) {
+ [[sourceNode children] removeObject:nextNode];
+ [nested removeObject:nextNode];
+ }
+ [nested release];
+
+ //Move all the folders to the beginning of the list
+ //Move the podcasts playlist to the top
+ BOOL movedPodcasts = NO;
+ enumerator = [[sourceNode children] reverseObjectEnumerator];
+ while ( (nextNode = [enumerator nextObject]) ) {
+ if ([nextNode type] == ITMTPodcastsNode) {
+ [[sourceNode children] removeObject:nextNode];
+ [[sourceNode children] insertObject:nextNode atIndex:1];
+ movedPodcasts = YES;
+ } else if ([nextNode type] == ITMTFolderNode) {
+ [[sourceNode children] removeObject:nextNode];
+ [[sourceNode children] insertObject:nextNode atIndex:1 + movedPodcasts];
+ }
+ }
+
+ [sources addObject:sourceNode];
+ }
+
+ return [sources autorelease];
+}
+
+- (NSArray *)artists
+{
+ NSAppleEventDescriptor *rawr = ITSendAEWithStringAndTimeout(@"'----':obj { form:'prop', want:type('prop'), seld:type('pArt'), from:obj { form:'indx', want:type('cTrk'), seld:abso($616C6C20$), from:obj { form:'indx', want:type('cPly'), seld:long(1), from:obj { form:'indx', want:type('cSrc'), seld:long(1), from:() } } } }", 'core', 'getd', &savedPSN, 600);
+ int i;
+ NSMutableArray *array = [[NSMutableArray alloc] init];
+ NSArray *returnArray;
+ for (i = 1; i <= [rawr numberOfItems]; i++) {
+ NSString *artist = [[rawr descriptorAtIndex:i] stringValue];
+ if (artist && [artist length] && ![array containsObject:artist]) {
+ [array addObject:artist];
+ }
+ }
+ [array sortUsingSelector:@selector(caseInsensitiveCompare:)];
+ returnArray = [NSArray arrayWithArray:array];
+ [array release];
+ return returnArray;
+}
+
+- (NSArray *)albums
+{
+ NSAppleEventDescriptor *rawr = ITSendAEWithStringAndTimeout(@"'----':obj { form:'prop', want:type('prop'), seld:type('pAlb'), from:obj { form:'indx', want:type('cTrk'), seld:abso($616C6C20$), from:obj { form:'indx', want:type('cPly'), seld:long(1), from:obj { form:'indx', want:type('cSrc'), seld:long(1), from:() } } } }", 'core', 'getd', &savedPSN, 600);
+ int i;
+ NSMutableArray *array = [[NSMutableArray alloc] init];
+ NSArray *returnArray;
+ for (i = 1; i <= [rawr numberOfItems]; i++) {
+ NSString *album = [[rawr descriptorAtIndex:i] stringValue];
+ if (album && [album length] && ![array containsObject:album]) {
+ [array addObject:album];
+ }
+ }
+ [array sortUsingSelector:@selector(caseInsensitiveCompare:)];
+ returnArray = [NSArray arrayWithArray:array];
+ [array release];
+ return returnArray;
+}
+
+- (int)numberOfSongsInPlaylistAtIndex:(int)index
+{
+ /*
+ This method only returns the proper number if there's something playing.
+ This is because it gets the container of the current playlist so that it
+ gets the playlist index from the current source. Operating this way is fine,
+ since MT only ever calls this method when there is something playlist.
+ A working version of this that works in just the main source is in the
+ makePlaylistWithTerm:ofType: method.
+ */
+ int temp1;
+ NSAppleEventDescriptor *result;
+ ITDebugLog(@"Getting number of songs in playlist at index %i", index);
+ result = ITSendAEWithString([NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('ctnr'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }", index], 'core', 'cnte', &savedPSN);
+ temp1 = (result == nil) ? -1 : (int)[result int32Value];
+ ITDebugLog(@"Getting number of songs in playlist at index %i done", index);
+ return temp1;
+}
+
+- (ITMTRemotePlayerSource)currentSource
+{
+ SInt32 fourcc;
+
+ ITDebugLog(@"Getting current source.");
+
+ fourcc = ([self isPlaying]) ? [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pKnd'), from:obj { form:'prop', want:type('prop'), seld:type('ctnr'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }", 'core', 'getd', &savedPSN) typeCodeValue] : 'kLib';
+
+ switch (fourcc) {
+ case 'kTun':
+ ITDebugLog(@"Getting current source done. Source: Radio.");
+ return ITMTRemoteRadioSource;
+ break;
+ case 'kDev':
+ ITDebugLog(@"Getting current source done. Source: Generic Device.");
+ return ITMTRemoteGenericDeviceSource;
+ case 'kPod':
+ ITDebugLog(@"Getting current source done. Source: iPod.");
+ return ITMTRemoteiPodSource; //this is stupid
+ break;
+ case 'kMCD':
+ case 'kACD':
+ ITDebugLog(@"Getting current source done. Source: CD.");
+ return ITMTRemoteCDSource;
+ break;
+ case 'kShd':
+ ITDebugLog(@"Getting current source done. Source: Shared Library.");
+ return ITMTRemoteSharedLibrarySource;
+ break;
+ case 'kUnk':
+ case 'kLib':
+ default:
+ ITDebugLog(@"Getting current source done. Source: Library.");
+ return ITMTRemoteLibrarySource;
+ break;
+ }
+}
+
+- (int)currentSourceIndex
+{
+ ITDebugLog(@"Getting current source.");
+ return [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'prop', want:type('prop'), seld:type('ctnr'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }", 'core', 'getd', &savedPSN) int32Value];
+}
+
+- (ITMTRemotePlayerPlaylistClass)currentPlaylistClass
+{
+ FourCharCode realResult;
+ ITDebugLog(@"Getting current playlist class");
+ realResult = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }", 'core', 'getd', &savedPSN) typeCodeValue];
+ switch (realResult)
+ {
+ case 'cLiP':
+ ITDebugLog(@"Getting current playlist class done. Class: Library.");
+ return ITMTRemotePlayerLibraryPlaylist;
+ break;
+ case 'cRTP':
+ ITDebugLog(@"Getting current playlist class done. Class: Radio.");
+ return ITMTRemotePlayerRadioPlaylist;
+ break;
+ default:
+ ITDebugLog(@"Getting current playlist class done. Class: Standard playlist.");
+ return ITMTRemotePlayerPlaylist;
+ }