Object or subdir rename is supported through browser node edit.
authorMiltiadis Vasilakis <mvasilak@gmail.com>
Thu, 22 Sep 2011 18:35:33 +0000 (21:35 +0300)
committerMiltiadis Vasilakis <mvasilak@gmail.com>
Thu, 22 Sep 2011 18:35:33 +0000 (21:35 +0300)
pithos-macos/PithosBrowserController.m

index f8a45af..b74f946 100644 (file)
@@ -59,6 +59,7 @@
 - (id)init {
     if ((self = [super init])) {
         [self setLineBreakMode:NSLineBreakByTruncatingMiddle];
+        [self setEditable:YES];
     }
     return self;
 }
     return NO;
 }
 
+#pragma mark Editing
+
+- (BOOL)browser:(NSBrowser *)browser shouldEditItem:(id)item {
+    return YES;
+}
+
+- (void)browser:(NSBrowser *)browser setObjectValue:(id)object forItem:(id)item {
+    PithosNode *node = (PithosNode *)item;
+    NSString *newName = (NSString *)object;
+    NSUInteger newNameLength = [newName length];
+    NSRange firstSlashRange = [newName rangeOfString:@"/"];
+    if ((newNameLength == 0) || 
+        ((firstSlashRange.length == 1) && (firstSlashRange.location != (newNameLength - 1))) || 
+        ([newName isEqualToString:node.displayName])) {
+        return;
+    }
+    if (([node class] == [PithosObjectNode class]) || 
+        (([node class] == [PithosSubdirNode class]) && 
+         !node.pithosObject.subdir &&
+         [node.pithosObject.name hasSuffix:@"/"])) {
+        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+        dispatch_async(queue, ^{
+            NSString *destinationObjectName = [[node.pithosObject.name stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
+            if ([newName hasSuffix:@"/"])
+                destinationObjectName = [destinationObjectName stringByAppendingString:@"/"];
+            NSError *error = nil;
+            BOOL isDirectory;
+            if ([PithosFileUtilities objectExistsAtContainerName:node.pithosContainer.name 
+                                                      objectName:destinationObjectName 
+                                                           error:&error 
+                                                     isDirectory:&isDirectory]) {
+                NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                [alert setMessageText:@"Name Taken"];
+                [alert setInformativeText:[NSString stringWithFormat:@"The name '%@' is already taken. Please choose a different name", newName]];
+                [alert addButtonWithTitle:@"OK"];
+                [alert runModal];
+                return;
+            } else if (error) {
+                return;
+            }
+            ASIPithosObjectRequest *objectRequest = [PithosFileUtilities moveObjectRequestWithContainerName:node.pithosContainer.name 
+                                                                                                 objectName:node.pithosObject.name 
+                                                                                   destinationContainerName:node.pithosContainer.name 
+                                                                                      destinationObjectName:destinationObjectName 
+                                                                                              checkIfExists:NO];
+            if (objectRequest) {
+                objectRequest.delegate = self;
+                objectRequest.didFinishSelector = @selector(moveFinished:);
+                objectRequest.didFailSelector = @selector(moveFailed:);
+                objectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+                                          node.parent, @"node", 
+                                          nil];
+                [objectRequest startAsynchronous];
+            }
+        });
+    } else if ([node class] == [PithosSubdirNode class]) {
+        if (firstSlashRange.length == 1)
+            return;
+        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+        dispatch_async(queue, ^{
+            NSString *destinationObjectName = [[node.pithosObject.name stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
+            NSError *error = nil;
+            BOOL isDirectory;
+            if ([PithosFileUtilities objectExistsAtContainerName:node.pithosContainer.name 
+                                                      objectName:destinationObjectName 
+                                                           error:&error 
+                                                     isDirectory:&isDirectory]) {
+                NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                [alert setMessageText:@"Name Taken"];
+                [alert setInformativeText:[NSString stringWithFormat:@"The name '%@' is already taken. Please choose a different name", newName]];
+                [alert addButtonWithTitle:@"OK"];
+                [alert runModal];
+                return;
+            } else if (error) {
+                return;
+            }
+            if (node.pithosObject.subdir)
+                destinationObjectName = [destinationObjectName stringByAppendingString:@"/"];
+            NSArray *objectRequests = [PithosFileUtilities moveObjectRequestsForSubdirWithContainerName:node.pithosContainer.name 
+                                                                                             objectName:node.pithosObject.name 
+                                                                               destinationContainerName:node.pithosContainer.name 
+                                                                                  destinationObjectName:destinationObjectName 
+                                                                                          checkIfExists:NO];
+            if (objectRequests) {
+                for (ASIPithosObjectRequest *objectRequest in objectRequests) {
+                    objectRequest.delegate = self;
+                    objectRequest.didFinishSelector = @selector(moveFinished:);
+                    objectRequest.didFailSelector = @selector(moveFailed:);
+                    objectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+                                              node.parent, @"node", 
+                                              nil];
+                    [objectRequest startAsynchronous];
+                }
+            }
+        });
+    }
+}
+
 #pragma mark Drag and Drop source
 
 - (BOOL)browser:(NSBrowser *)aBrowser writeRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column 
@@ -648,7 +747,7 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
                         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                         dispatch_async(queue, ^{
                             NSString *destinationObjectName = [objectNamePrefix stringByAppendingPathComponent:node.displayName];
-                            if ([node.pithosObject.name hasSuffix:@"/"])
+                            if (node.pithosObject.subdir)
                                 destinationObjectName = [destinationObjectName stringByAppendingString:@"/"];
                             NSArray *objectRequests = [PithosFileUtilities moveObjectRequestsForSubdirWithContainerName:node.pithosContainer.name 
                                                                                                              objectName:node.pithosObject.name 
@@ -708,7 +807,7 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
                             NSString *destinationObjectName;
                             if (![dropNode isEqualTo:node.parent]) {
                                 destinationObjectName = [objectNamePrefix stringByAppendingPathComponent:node.displayName];
-                                if ([node.pithosObject.name hasSuffix:@"/"])
+                                if (node.pithosObject.subdir)
                                     destinationObjectName = [destinationObjectName stringByAppendingString:@"/"];
                             } else {
                                 destinationObjectName = [PithosFileUtilities safeSubdirNameForContainerName:containerName