From 133b1f2dd604d3f27b8980166722de1e838ee108 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Mon, 19 Sep 2011 21:44:03 +0300 Subject: [PATCH] Move to trash rename checks also for subdir names. Menu works for multiple selections. --- pithos-macos/PithosBrowserController.m | 165 ++++++++++++++++++-------------- pithos-macos/PithosFileUtilities.m | 20 +++- 2 files changed, 111 insertions(+), 74 deletions(-) diff --git a/pithos-macos/PithosBrowserController.m b/pithos-macos/PithosBrowserController.m index 7633585..5d86b74 100644 --- a/pithos-macos/PithosBrowserController.m +++ b/pithos-macos/PithosBrowserController.m @@ -339,11 +339,11 @@ - (BOOL)browser:(NSBrowser *)aBrowser writeRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column toPasteboard:(NSPasteboard *)pasteboard { NSMutableArray *propertyList = [NSMutableArray arrayWithCapacity:[rowIndexes count]]; - NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; - for (NSUInteger i = [rowIndexes firstIndex]; i <= [rowIndexes lastIndex]; i = [rowIndexes indexGreaterThanIndex:i]) { - PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]]; + NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; + [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){ + PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:idx]]; [propertyList addObject:[node.pithosObject.name pathExtension]]; - } + }]; [pasteboard declareTypes:[NSArray arrayWithObject:NSFilesPromisePboardType] owner:self]; [pasteboard setPropertyList:propertyList forType:NSFilesPromisePboardType]; @@ -354,9 +354,9 @@ - (NSArray *)browser:(NSBrowser *)aBrowser namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column { NSMutableArray *names = [NSMutableArray arrayWithCapacity:[rowIndexes count]]; - NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; - for (NSUInteger i = [rowIndexes firstIndex]; i <= [rowIndexes lastIndex]; i = [rowIndexes indexGreaterThanIndex:i]) { - PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]]; + NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; + [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){ + PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:idx]]; // If the node is a subdir ask if the whole tree should be downloaded if ([node class] == [PithosSubdirNode class]) { @@ -394,7 +394,7 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column { [objectRequest startAsynchronous]; } } - } + }]; return names; } @@ -712,6 +712,15 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column { return 220; } +- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex { + if (proposedPosition < 120) + return 120; + else if (proposedPosition > 220) + return 220; + else + return proposedPosition; +} + #pragma mark - #pragma mark NSOutlineViewDelegate @@ -743,22 +752,27 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column { if ((column == -1) || (row == -1)) { // General context menu } else { + NSIndexPath *clickedNodeIndexPath = [[browser indexPathForColumn:column] indexPathByAddingIndex:row]; + NSArray *menuNodesIndexPaths = [browser selectionIndexPaths]; + if (![menuNodesIndexPaths containsObject:clickedNodeIndexPath]) + menuNodesIndexPaths = [NSArray arrayWithObject:clickedNodeIndexPath]; + // Move to Trash (pithos container only) // Delete if ([rootNode class] == [PithosContainerNode class]) { if ([rootNode.pithosContainer.name isEqualToString:@"pithos"]) { menuItem = [[[NSMenuItem alloc] initWithTitle:@"Move to Trash" action:@selector(moveToTrash:) keyEquivalent:@""] autorelease]; - [menuItem setRepresentedObject:[browser itemAtRow:row inColumn:column]]; + [menuItem setRepresentedObject:menuNodesIndexPaths]; [menu addItem:menuItem]; } menuItem = [[[NSMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteObject:) keyEquivalent:@""] autorelease]; - [menuItem setRepresentedObject:[browser itemAtRow:row inColumn:column]]; + [menuItem setRepresentedObject:menuNodesIndexPaths]; [menu addItem:menuItem]; [menu addItem:[NSMenuItem separatorItem]]; } // Get Info menuItem = [[[NSMenuItem alloc] initWithTitle:@"Get Info" action:@selector(getInfo:) keyEquivalent:@""] autorelease]; - [menuItem setRepresentedObject:[browser itemAtRow:row inColumn:column]]; + [menuItem setRepresentedObject:menuNodesIndexPaths]; [menu addItem:menuItem]; } } @@ -767,78 +781,85 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column { #pragma mark Menu Actions - (void)getInfo:(NSMenuItem *)sender { - [(PithosNode *)[sender representedObject] showPithosNodeInfo:sender]; + for (NSIndexPath *nodeIndexPath in ((NSArray *)[sender representedObject])) { + PithosNode *node = [browser itemAtIndexPath:nodeIndexPath]; + [node showPithosNodeInfo:sender]; + } } - (void)deleteObject:(NSMenuItem *)sender { - PithosNode *node = (PithosNode *)[sender representedObject]; - if (([node class] == [PithosObjectNode class]) || - (([node class] == [PithosSubdirNode class]) && - !node.pithosObject.subdir && - [node.pithosObject.name hasSuffix:@"/"])) { - ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest deleteObjectRequestWithContainerName:node.pithosContainer.name - objectName:node.pithosObject.name]; - objectRequest.delegate = self; - objectRequest.didFinishSelector = @selector(deleteObjectFinished:); - objectRequest.didFailSelector = @selector(deleteObjectFailed:); - [objectRequest startAsynchronous]; - } else if ([node class] == [PithosSubdirNode class]) { - NSArray *objectRequests = [PithosFileUtilities deleteObjectRequestsForSubdirWithContainerName:node.pithosContainer.name - objectName:node.pithosObject.name]; - if (objectRequests) { - for (ASIPithosObjectRequest *objectRequest in objectRequests) { - objectRequest.delegate = self; - objectRequest.didFinishSelector = @selector(deleteObjectFinished:); - objectRequest.didFailSelector = @selector(deleteObjectFailed:); - [objectRequest startAsynchronous]; - } - } - - } -} - -- (void)moveToTrash:(NSMenuItem *)sender { - PithosNode *node = (PithosNode *)[sender representedObject]; - if (([node class] == [PithosObjectNode class]) || - (([node class] == [PithosSubdirNode class]) && - !node.pithosObject.subdir && - [node.pithosObject.name hasSuffix:@"/"])) { - NSString *safeObjectName = [PithosFileUtilities safeObjectNameForContainerName:@"trash" - objectName:node.pithosObject.name]; - if (safeObjectName) { - ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest moveObjectDataRequestWithContainerName:node.pithosContainer.name - objectName:node.pithosObject.name - contentType:nil - contentEncoding:nil - contentDisposition:nil - manifest:nil - sharing:nil - isPublic:ASIPithosObjectRequestPublicIgnore - metadata:nil - destinationContainerName:@"trash" - destinationObjectName:safeObjectName]; + for (NSIndexPath *nodeIndexPath in ((NSArray *)[sender representedObject])) { + PithosNode *node = [browser itemAtIndexPath:nodeIndexPath]; + if (([node class] == [PithosObjectNode class]) || + (([node class] == [PithosSubdirNode class]) && + !node.pithosObject.subdir && + [node.pithosObject.name hasSuffix:@"/"])) { + ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest deleteObjectRequestWithContainerName:node.pithosContainer.name + objectName:node.pithosObject.name]; objectRequest.delegate = self; - objectRequest.didFinishSelector = @selector(moveToTrashFinished:); - objectRequest.didFailSelector = @selector(moveToTrashFailed:); + objectRequest.didFinishSelector = @selector(deleteObjectFinished:); + objectRequest.didFailSelector = @selector(deleteObjectFailed:); [objectRequest startAsynchronous]; - } - } else if ([node class] == [PithosSubdirNode class]) { - NSString *safeObjectName = [PithosFileUtilities safeSubdirNameForContainerName:@"trash" - subdirName:node.pithosObject.name]; - if (safeObjectName) { - NSArray *objectRequests = [PithosFileUtilities moveObjectRequestsForSubdirWithContainerName:node.pithosContainer.name - objectName:node.pithosObject.name - destinationContainerName:@"trash" - destinationObjectName:safeObjectName]; + } else if ([node class] == [PithosSubdirNode class]) { + NSArray *objectRequests = [PithosFileUtilities deleteObjectRequestsForSubdirWithContainerName:node.pithosContainer.name + objectName:node.pithosObject.name]; if (objectRequests) { for (ASIPithosObjectRequest *objectRequest in objectRequests) { objectRequest.delegate = self; - objectRequest.didFinishSelector = @selector(moveToTrashFinished:); - objectRequest.didFailSelector = @selector(moveToTrashFailed:); + objectRequest.didFinishSelector = @selector(deleteObjectFinished:); + objectRequest.didFailSelector = @selector(deleteObjectFailed:); [objectRequest startAsynchronous]; } } - } + + } + } +} + +- (void)moveToTrash:(NSMenuItem *)sender { + for (NSIndexPath *nodeIndexPath in ((NSArray *)[sender representedObject])) { + PithosNode *node = [browser itemAtIndexPath:nodeIndexPath]; + if (([node class] == [PithosObjectNode class]) || + (([node class] == [PithosSubdirNode class]) && + !node.pithosObject.subdir && + [node.pithosObject.name hasSuffix:@"/"])) { + NSString *safeObjectName = [PithosFileUtilities safeObjectNameForContainerName:@"trash" + objectName:node.pithosObject.name]; + if (safeObjectName) { + ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest moveObjectDataRequestWithContainerName:node.pithosContainer.name + objectName:node.pithosObject.name + contentType:nil + contentEncoding:nil + contentDisposition:nil + manifest:nil + sharing:nil + isPublic:ASIPithosObjectRequestPublicIgnore + metadata:nil + destinationContainerName:@"trash" + destinationObjectName:safeObjectName]; + objectRequest.delegate = self; + objectRequest.didFinishSelector = @selector(moveToTrashFinished:); + objectRequest.didFailSelector = @selector(moveToTrashFailed:); + [objectRequest startAsynchronous]; + } + } else if ([node class] == [PithosSubdirNode class]) { + NSString *safeObjectName = [PithosFileUtilities safeSubdirNameForContainerName:@"trash" + subdirName:node.pithosObject.name]; + if (safeObjectName) { + NSArray *objectRequests = [PithosFileUtilities moveObjectRequestsForSubdirWithContainerName:node.pithosContainer.name + objectName:node.pithosObject.name + destinationContainerName:@"trash" + destinationObjectName:safeObjectName]; + if (objectRequests) { + for (ASIPithosObjectRequest *objectRequest in objectRequests) { + objectRequest.delegate = self; + objectRequest.didFinishSelector = @selector(moveToTrashFinished:); + objectRequest.didFailSelector = @selector(moveToTrashFailed:); + [objectRequest startAsynchronous]; + } + } + } + } } } diff --git a/pithos-macos/PithosFileUtilities.m b/pithos-macos/PithosFileUtilities.m index 703f063..75c5879 100644 --- a/pithos-macos/PithosFileUtilities.m +++ b/pithos-macos/PithosFileUtilities.m @@ -618,12 +618,28 @@ objectNamePrefix = [objectNamePrefix substringToIndex:([objectNamePrefix length] - 1)]; objectNameExtraSuffix = [NSString stringWithString:@"/"]; } - NSArray *objects = [self objectsWithContainerName:containerName objectNamePrefix:objectNamePrefix delimiter:nil]; + NSArray *objects = [self objectsWithContainerName:containerName + objectNamePrefix:[objectNamePrefix stringByDeletingLastPathComponent] + delimiter:@"/"]; if (objects == nil) return nil; if ([objects count] == 0) return objectName; - NSArray *objectNames = [objects valueForKey:@"name"]; + NSMutableArray *objectNames = [NSMutableArray arrayWithArray: + [[objects objectsAtIndexes: + [objects indexesOfObjectsPassingTest:^(ASIPithosObject *pithosObject, NSUInteger idx, BOOL *stop){ + if (pithosObject.subdir) + return NO; + return YES; + }]] valueForKey:@"name"]]; + for (NSString *name in [[objects objectsAtIndexes: + [objects indexesOfObjectsPassingTest:^(ASIPithosObject *pithosObject, NSUInteger idx, BOOL *stop){ + if (pithosObject.subdir) + return YES; + return NO; + }]] valueForKey:@"name"]) { + [objectNames addObject:[name substringToIndex:([name length] - 1)]]; + } if (![objectNames containsObject:objectName]) return objectName; NSUInteger objectNameSuffix = 1; -- 1.7.10.4