Finally fixed all the memory leaks, I hope. Fixed iTunesRemote to not
[MenuTunes.git] / iTunesRemote.m
index 7bbe551..0689704 100755 (executable)
     
     ITDebugLog(@"Getting playlists.");
     if (numSources == 0) {
+               [allSources release];
         ITDebugLog(@"No sources.");
         return nil;
     }
 
 - (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);
-    temp1 = [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', 'getd', &savedPSN) int32Value];
+       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;
 }
 {
     NSString *temp1;
     ITDebugLog(@"Getting current unique identifier.");
-    SInt32 cls = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value];
+       NSAppleEventDescriptor *descriptor = ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN);
+       if ((descriptor == nil) || ([descriptor int32Value] == 'prop')) {
+               return @"0-0";
+       }
+    SInt32 cls = [descriptor int32Value];
     if ( ([self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist) || (cls == 'cURT') ) {
+               NSString *bad = [NSString stringWithUTF8String:"浳湧"];
         temp1 = [ITSendAEWithKey('pStT', 'core', 'getd', &savedPSN) stringValue];
-               NSLog(@"%@", temp1);
+        if ([temp1 isEqualToString:bad]) {
+            temp1 = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) stringValue];
+        }
     } else {
         temp1 = [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pDID'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value]];
     }
 {
     int temp1;
     ITDebugLog(@"Getting current song index.");
-    temp1 = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value];
+       temp1 = (([ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value] != 'prop') ? [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pidx'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value] : -1);
     ITDebugLog(@"Getting current song index done.");
     return temp1;
 }
         NSString *bad = [NSString stringWithUTF8String:"浳湧"];
         temp1 = [ITSendAEWithKey('pStT', 'core', 'getd', &savedPSN) stringValue];
         if ([temp1 isEqualToString:bad]) {
-                       NSLog(@"arrrr");
             temp1 = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) stringValue];
         }
         temp1 = [temp1 stringByAppendingString:@" (Stream)"];
     NSString *finalString;
     
     ITDebugLog(@"Getting current song elapsed time.");
-    final = (long)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pPos'), from:'null'() }", 'core', 'getd', &savedPSN) int32Value];
+       final = (long)[ITSendAEWithKey('pPos', 'core', 'getd', &savedPSN) int32Value];
     finalString = [self formatTimeInSeconds:final];
     ITDebugLog(@"Getting current song elapsed time done.");
     return finalString;
 - (BOOL)makePlaylistWithTerm:(NSString *)term ofType:(int)type
 {
     int i;
-    
+       
     //Get fixed indexing status
     BOOL fixed = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pFix'), from:'null'() }", 'core', 'getd', &savedPSN) booleanValue];
     
     //If MenuTunes playlist exists
     if ([ITSendAEWithString(@"'----':obj { form:'name', want:type('cPly'), seld:\"MenuTunes\", from:'null'() }", 'core', 'doex', &savedPSN) booleanValue]) {
         //Clear old MenuTunes playlist
-        int numSongs = [ITSendAEWithString(@"kocl:type('cTrk'), '----':obj { form:'name', want:type('cPly'), seld:\"MenuTunes\", from:obj { form:'prop', want:type('prop'), seld:type('ctnr'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }", 'core', 'cnte', &savedPSN) int32Value];
+               int numSongs = [ITSendAEWithString(@"kocl:type('cTrk'), '----':obj { form:'name', want:type('cPly'), seld:\"MenuTunes\", from:'null'() }", 'core', 'cnte', &savedPSN) int32Value];
         for (i = 1; i <= numSongs; i++) {
             ITSendAEWithString(@"'----':obj { form:'indx', want:type('cTrk'), seld:long(1), from:obj { form:'name', want:type('cPly'), seld:\"MenuTunes\", from:'null'() } }", 'core', 'delo', &savedPSN);
         }