Alex's memory leak fixes and timeout changes.
[ITFoundation.git] / ITAppleEventCenter.m
index 990e454..9efe080 100755 (executable)
@@ -67,7 +67,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -134,7 +134,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
     if (!err) [self printCarbonDesc:&replyEvent];
 
     if (err) {
@@ -202,7 +202,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -260,7 +260,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -328,7 +328,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -395,7 +395,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -444,7 +444,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     cerr = AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
     cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
     [self printCarbonDesc:&event];
-    err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
+    err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, nil);
     [self printCarbonDesc:&reply];
     if (!cerr2) AEDisposeDesc(&dest);
     if (!cerr) AEDisposeDesc(&event);
@@ -464,7 +464,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID   eID    = *((unsigned long*)[eventID UTF8String]);
 
-    NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
+    NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:type('all '), from:'null'() }", [key UTF8String]];
     const char *sendString = [nssendString UTF8String];
     AEArrayDataPointer result = nil;
     int pid;
@@ -488,7 +488,7 @@ if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
     }
 
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -546,7 +546,7 @@ if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -601,7 +601,7 @@ return result;
         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
 
     [self printCarbonDesc:&replyEvent];
 
@@ -626,4 +626,70 @@ return result;
 
     return result;
 }
+
+- (NSData*)sendAEWithSendStringForData:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
+{
+    //Add error checking...
+    AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
+    AEEventID   eID    = *((unsigned long*)[eventID UTF8String]);
+    int pid;
+    
+    const char *sendString = [nssendString UTF8String];
+    NSData  *_finalData = nil;
+    
+    AppleEvent sendEvent, replyEvent;
+    
+    DescType resultType;
+    Size resultSize, charResultSize;
+    
+    AEBuildError buildError;
+    OSStatus berr,err;
+    OSErr err2, err3;
+    
+    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+       ITDebugLog(@"Error getting PID of application.");
+       return nil;
+    }
+    
+    berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
+    if (!berr) [self printCarbonDesc:&sendEvent];
+    
+    if (berr) {
+        ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
+    }
+    
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+    if (!err) [self printCarbonDesc:&replyEvent];
+    
+    if (err) {
+        ITDebugLog(@"Send Error: %i",err);
+    } else {
+        unichar *result = 0;
+       
+        err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
+        if (resultSize != 0) {
+            result = malloc(resultSize);
+        } else {
+            return nil;
+        }
+       
+        if (err2) {
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
+        } else {
+            err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
+           
+            if (err3) {
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
+            } else {
+                _finalData = [NSData dataWithBytesNoCopy:result length:charResultSize freeWhenDone:YES];
+            }
+        }
+    }
+    
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+    
+    return _finalData;
+}
+
 @end
\ No newline at end of file