Use user catalog in sync
[pithos-macos] / pithos-macos / PithosUtilities.m
index 273bbe2..d48928a 100644 (file)
@@ -72,7 +72,7 @@
     if (ifExists && [fileManager fileExistsAtPath:destinationPath]) {
         __block NSInteger choice;
         dispatch_sync(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"File Exists"];
             [alert setInformativeText:[NSString stringWithFormat:@"A file or directory named '%@' already exists, do you want to replace it?", fileName]];
             [alert addButtonWithTitle:@"OK"];
@@ -94,7 +94,7 @@
     if (error) {
         DLog(@"Cannot remove existing file '%@': %@", fileName, error);
         dispatch_async(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"Removal Error"];
             [alert setInformativeText:[NSString stringWithFormat:@"Cannot remove existing file '%@': %@", 
                                        fileName, [error localizedDescription]]];
     if (ifExists && [[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
         __block NSInteger choice;
         dispatch_sync(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"File exists"];
             [alert setInformativeText:[NSString stringWithFormat:@"A file or directory named '%@' already exists, do you want to replace it?", subdirName]];
             [alert addButtonWithTitle:@"OK"];
     if (error) {
         DLog(@"Cannot create directory at '%@': %@", directoryPath, error);
         dispatch_async(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"Create Directory Error"];
             [alert setInformativeText:[NSString stringWithFormat:@"Cannot create directory at '%@': %@", 
                                        directoryPath, [error localizedDescription]]];
                 if (error) {
                     DLog(@"Cannot create directory at '%@': %@", subdirDirectoryPath, error);
                     dispatch_async(dispatch_get_main_queue(), ^{
-                        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                        NSAlert *alert = [[NSAlert alloc] init];
                         [alert setMessageText:@"Create Directory Error"];
                         [alert setInformativeText:[NSString stringWithFormat:@"Cannot create directory at '%@': %@", 
                                                    subdirDirectoryPath, [error localizedDescription]]];
                 if (error) {
                     DLog(@"Cannot remove existing file at '%@': %@", subdirDirectoryPath, error);
                     dispatch_async(dispatch_get_main_queue(), ^{
-                        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                        NSAlert *alert = [[NSAlert alloc] init];
                         [alert setMessageText:@"Remove File Error"];
                         [alert setInformativeText:[NSString stringWithFormat:@"Cannot remove existing file at '%@': %@", 
                                                    subdirDirectoryPath, [error localizedDescription]]];
     strcpy(tempFileNameCString, tempFileTemplateCString);
     int fileDescriptor = mkstemp(tempFileNameCString);
     NSString *tempFilePath = [fileManager stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)];
+    free(tempFileNameCString);
     if (fileDescriptor == -1) {
         dispatch_async(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"Create Temporary File Error"];
             [alert setInformativeText:[NSString stringWithFormat:@"Cannot create temporary file at '%@'", tempFilePath]];
             [alert addButtonWithTitle:@"OK"];
         });
         return nil;
     }
-    free(tempFileNameCString);
     NSFileHandle *tempFileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileDescriptor closeOnDealloc:YES];
 
     [missingBlocks enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){
     NSArray *subPaths = [fileManager subpathsOfDirectoryAtPath:directoryPath error:&error];
     if (error) {
         dispatch_async(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             [alert setMessageText:@"Directory Read Error"];
             [alert setInformativeText:[NSString stringWithFormat:@"Cannot read contents of directory '%@': %@", 
                                        [directoryPath lastPathComponent], [error localizedDescription]]];
 #pragma mark -
 #pragma mark Copy
 
-+ (ASIPithosObjectRequest *)copyObjectRequestWithPithos:(ASIPithos *)pithos 
++ (ASIPithosObjectRequest *)cpyObjectRequestWithPithos:(ASIPithos *)pithos 
                                           containerName:(NSString *)containerName 
                                              objectName:(NSString *)objectName 
                                destinationContainerName:(NSString *)destinationContainerName 
                                           sharingAccount:nil])
         return nil;
     
-    ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest copyObjectDataRequestWithPithos:pithos 
+    ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest cpyObjectDataRequestWithPithos:pithos 
                                                                                       containerName:containerName 
                                                                                          objectName:objectName 
                                                                                         contentType:nil 
     return objectRequest;
 }
 
-+ (NSArray *)copyObjectRequestsForSubdirWithPithos:(ASIPithos *)pithos 
++ (NSArray *)cpyObjectRequestsForSubdirWithPithos:(ASIPithos *)pithos 
                                      containerName:(NSString *)containerName 
                                         objectName:(NSString *)objectName 
                           destinationContainerName:(NSString *)destinationContainerName 
     ASIPithosObjectRequest *objectRequest;
     if ([objectName isEqualToString:destinationObjectName]) {
         if (![objectName hasSuffix:@"/"]) {
-            objectRequest = [ASIPithosObjectRequest copyObjectDataRequestWithPithos:pithos 
+            objectRequest = [ASIPithosObjectRequest cpyObjectDataRequestWithPithos:pithos 
                                                                       containerName:containerName 
                                                                          objectName:objectName 
                                                                         contentType:nil 
             [objectRequests addObject:objectRequest];
         }
         for (ASIPithosObject *object in objects) {
-            objectRequest = [ASIPithosObjectRequest copyObjectDataRequestWithPithos:pithos 
+            objectRequest = [ASIPithosObjectRequest cpyObjectDataRequestWithPithos:pithos 
                                                                       containerName:containerName 
                                                                          objectName:object.name 
                                                                         contentType:nil 
         }
     } else {
         if (![objectName hasSuffix:@"/"]) {
-            objectRequest = [ASIPithosObjectRequest copyObjectDataRequestWithPithos:pithos 
+            objectRequest = [ASIPithosObjectRequest cpyObjectDataRequestWithPithos:pithos 
                                                                       containerName:containerName 
                                                                          objectName:objectName 
                                                                         contentType:nil 
                                                                    withString:destinationObjectName
                                                                       options:NSAnchoredSearch
                                                                         range:prefixRange];
-            objectRequest = [ASIPithosObjectRequest copyObjectDataRequestWithPithos:pithos 
+            objectRequest = [ASIPithosObjectRequest cpyObjectDataRequestWithPithos:pithos 
                                                                       containerName:containerName 
                                                                          objectName:object.name 
                                                                         contentType:nil 
         if (uti != NULL) {
             CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
             CFRelease(uti);
-            return (NSString *)MIMEType;
+            CFRelease(url);
+            return (__bridge_transfer NSString *)MIMEType;
         }
     }
+    CFRelease(url);
     return nil;
 }
 
     return YES;
 }
 
-// Removes contents of a directory
-+ (void)removeContentsAtPath:(NSString *)dirPath {
+// Removes contents of a directory and the directory itself if selected
++ (void)removeContentsAtPath:(NSString *)dirPath andDirectory:(BOOL)removeDirectory {
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSError *error = nil;
     BOOL isDirectory;
             }
             error = nil;
         }
+        if (removeDirectory && (![fileManager removeItemAtPath:dirPath error:&error] || error)) {
+            [self fileActionFailedAlertWithTitle:@"Remove Directory Error"
+                                         message:[NSString stringWithFormat:@"Cannot remove directory at '%@'", dirPath]
+                                           error:error];
+        }
     } else if (![fileManager removeItemAtPath:dirPath error:&error] || error) {
         [self fileActionFailedAlertWithTitle:@"Remove File Error" 
                                      message:[NSString stringWithFormat:@"Cannot remove file at '%@'", dirPath] 
     }
 }
 
+// Removes contents of a directory
++ (void)removeContentsAtPath:(NSString *)dirPath {
+    [self removeContentsAtPath:dirPath andDirectory:NO];
+}
+
 // Returns if an object is a directory based on its content type
 + (BOOL)isContentTypeDirectory:(NSString *)contentType {
     return ([contentType isEqualToString:@"application/directory"] ||
                                                                                          objectName:objectName];
     if (sharingAccount)
         [objectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
-    ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
-    [networkQueue go];
-    [networkQueue addOperations:[NSArray arrayWithObject:[self prepareRequest:objectRequest]] waitUntilFinished:YES];
-    *error = [objectRequest error];
-    if (*error) {
-        [self httpRequestErrorAlertWithRequest:objectRequest];
-        return NO;
-    } else if (objectRequest.responseStatusCode == 200) {
-        *isDirectory = [self isContentTypeDirectory:[objectRequest contentType]];
-        return YES;
+    [self startAndWaitForRequest:objectRequest];
+    if (error != NULL) {
+        *error = [objectRequest error];
+        if (*error) {
+            [self httpRequestErrorAlertWithRequest:objectRequest];
+            return NO;
+        } else if (objectRequest.responseStatusCode == 200) {
+            *isDirectory = [self isContentTypeDirectory:[objectRequest contentType]];
+            return YES;
+        }
     }
     return NO;
 }
     } else if (objectExists) {
         __block NSInteger choice;
         dispatch_sync(dispatch_get_main_queue(), ^{
-            NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+            NSAlert *alert = [[NSAlert alloc] init];
             if (isDirectory) {
                 [alert setMessageText:@"Directory Exists"];
                 if (sharingAccount)
                                                                                                         until:nil];
         if (sharingAccount)
             [containerRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
-        ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
-        [networkQueue go];
-        [networkQueue addOperations:[NSArray arrayWithObject:[self prepareRequest:containerRequest]] waitUntilFinished:YES];
+        [self startAndWaitForRequest:containerRequest];
         if ([containerRequest error]) {
             [self httpRequestErrorAlertWithRequest:containerRequest];
             return nil;
         objectNameExtraSuffix = [objectName substringFromIndex:lastDotRange.location];
     } else if ([objectName hasSuffix:@"/"]) {
         objectNamePrefix = [objectName substringToIndex:([objectName length] - 1)];
-        objectNameExtraSuffix = [NSString stringWithString:@"/"];
+        objectNameExtraSuffix = @"/";
     } else {
         objectNamePrefix = [NSString stringWithString:objectName];
         objectNameExtraSuffix = [NSString string];
     NSString *subdirNameExtraSuffix;
     if ([subdirName hasSuffix:@"/"]) {
         subdirNamePrefix = [subdirName substringToIndex:([subdirName length] - 1)];
-        subdirNameExtraSuffix = [NSString stringWithString:@"/"];
+        subdirNameExtraSuffix = @"/";
     } else {
         subdirNamePrefix = [NSString stringWithString:subdirName];
         subdirNameExtraSuffix = [NSString string];
                          [request responseString]];
     DLog(@"%@", message);
     dispatch_async(dispatch_get_main_queue(), ^{    
-        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-        [alert setMessageText:@"HTTP Request Error"];
-        [alert setInformativeText:message];
-        [alert addButtonWithTitle:@"OK"];
-        [alert runModal];
-        [pool drain];
+        @autoreleasepool {
+            NSAlert *alert = [[NSAlert alloc] init];
+            [alert setMessageText:@"HTTP Request Error"];
+            [alert setInformativeText:message];
+            [alert addButtonWithTitle:@"OK"];
+            [alert runModal];
+        }
     });
 }
 
                          [request responseString]];
     DLog(@"%@", message);
     dispatch_async(dispatch_get_main_queue(), ^{
-        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-        [alert setMessageText:@"Unexpected Response Status"];
-        [alert setInformativeText:message];
-        [alert addButtonWithTitle:@"OK"];
-        [alert runModal];
-        [pool drain];
+        @autoreleasepool {
+            NSAlert *alert = [[NSAlert alloc] init];
+            [alert setMessageText:@"Unexpected Response Status"];
+            [alert setInformativeText:message];
+            [alert addButtonWithTitle:@"OK"];
+            [alert runModal];
+        }
     });
 }
 
 + (void)httpAuthenticationError {
     dispatch_async(dispatch_get_main_queue(), ^{
-        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-        [alert setMessageText:@"Authentication Error"];
-        [alert setInformativeText:@"Authentication error, please check your token or login again"];
-        [alert addButtonWithTitle:@"OK"];
-        [alert runModal];
-        [pool drain];
+        @autoreleasepool {
+            NSAlert *alert = [[NSAlert alloc] init];
+            [alert setMessageText:@"Authentication Error"];
+            [alert setInformativeText:@"Authentication error, please check your token or login again"];
+            [alert addButtonWithTitle:@"OK"];
+            [alert runModal];
+        }
     });
 }
 
 + (void)fileActionFailedAlertWithTitle:(NSString *)title message:(NSString *)message error:(NSError *)error {
     dispatch_async(dispatch_get_main_queue(), ^{
-        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
-        [alert setMessageText:title];
-        if (error)
-            [alert setInformativeText:[NSString stringWithFormat:@"%@: %@", message, [error localizedDescription]]];
-        else
-            [alert setInformativeText:message];
-        [alert addButtonWithTitle:@"OK"];
-        [alert runModal];
-        [pool drain];
+        @autoreleasepool {
+            NSAlert *alert = [[NSAlert alloc] init];
+            [alert setMessageText:title];
+            if (error)
+                [alert setInformativeText:[NSString stringWithFormat:@"%@: %@", message, [error localizedDescription]]];
+            else
+                [alert setInformativeText:message];
+            [alert addButtonWithTitle:@"OK"];
+            [alert runModal];
+        }
     });
 }
 
 }
 
 + (ASIPithosRequest *)copyRequest:(ASIPithosRequest *)request {
-    NSMutableDictionary *userInfo = (NSMutableDictionary *)[[request.userInfo retain] autorelease];
+    NSMutableDictionary *userInfo = (NSMutableDictionary *)request.userInfo;
     request.userInfo = nil;
     ASIPithosRequest *newRequest = [request copy];
     newRequest.userInfo = userInfo;
     return newRequest;
 }
 
++ (void)startAndWaitForRequest:(ASIPithosRequest *)request {
+    ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
+    [networkQueue go];
+    [networkQueue addOperations:[NSArray arrayWithObject:[self prepareRequest:request]] waitUntilFinished:YES];
+}
+
 @end