Initial implementation of browser context menu.
[pithos-macos] / pithos-macos / PithosBrowserController.m
index 71abdfd..11e2bdc 100644 (file)
 #import "PithosNode.h"
 #import "PithosAccountNode.h"
 #import "PithosContainerNode.h"
+#import "PithosSubdirNode.h"
+#import "PithosObjectNode.h"
 #import "PithosEmptyNode.h"
 #import "ImageAndTextCell.h"
 #import "FileSystemBrowserCell.h"
 #import "ASIPithosRequest.h"
 #import "ASIPithosContainerRequest.h"
+#import "ASIPithosObjectRequest.h"
 #import "ASIPithosContainer.h"
+#import "ASIPithosObject.h"
+#import "PithosFileUtilities.h"
 
 //@interface PithosBrowserCell : NSBrowserCell {}
 @interface PithosBrowserCell : FileSystemBrowserCell {}
@@ -94,6 +99,7 @@
 
 @interface PithosBrowserController (Private) {}
 - (void)resetContainers;
+- (void)getInfo:(NSMenuItem *)sender;
 @end
 
 @implementation PithosBrowserController
 
 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
+    [browserMenu release];
     [sharedPreviewController release];
     [outlineViewDataSourceArray release];
     [accountNode release];
     [rootNode release];
-    [browser release];
-    [splitView release];
-    [outlineView release];
     [super dealloc];
 }
 
 - (void)awakeFromNib {
+    [super awakeFromNib];
+    
+    [browser setDraggingSourceOperationMask:NSDragOperationNone forLocal:YES];
+    [browser setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
+    
     [browser setCellClass:[PithosBrowserCell class]];
+    
+    browserMenu = [[NSMenu alloc] init];
+    [browserMenu setDelegate:self];
+    [browser setMenu:browserMenu];
 }
 
 - (void)resetContainers {
 }
 
 - (void)windowDidLoad {
+    [super windowDidLoad];
+    
     [[[outlineView tableColumns] objectAtIndex:0] setDataCell:[[[PithosOutlineViewCell alloc] init] autorelease]];
     
     // Register for updates
     }
 }
 
-
 #pragma mark -
 #pragma Actions
 
     return NO;
 }
 
+#pragma mark Drag and Drop
+
+//- (BOOL)browser:(NSBrowser *)aBrowser canDragRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column 
+//      withEvent:(NSEvent *)event {    
+//    // XXX allow only objects? or when a subdir is dragged download the whole tree?
+//    NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; 
+//    for (NSUInteger i = [rowIndexes firstIndex]; i <= [rowIndexes lastIndex]; i = [rowIndexes indexGreaterThanIndex:i]) {
+//        PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]];
+//        if (node.pithosObject.subdir)
+//            return NO;
+//    }
+//    return YES;
+//}
+
+- (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]];
+        [propertyList addObject:[node.pithosObject.name pathExtension]];
+    }
+
+    [pasteboard declareTypes:[NSArray arrayWithObject:NSFilesPromisePboardType] owner:self];
+    [pasteboard setPropertyList:propertyList forType:NSFilesPromisePboardType];
+    
+    return YES;
+}
+
+- (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]];
+        
+        
+        // If the node is a subdir ask if the whole tree should be downloaded
+        if (node.pithosObject.subdir) {
+            NSAlert *alert = [[NSAlert alloc] init];
+            [alert setMessageText:@"Download directory"];
+            [alert setInformativeText:[NSString stringWithFormat:@"'%@' is a directory, do you want to download its contents?", node.displayName]];
+            [alert addButtonWithTitle:@"OK"];
+            [alert addButtonWithTitle:@"Cancel"];
+            NSInteger choice = [alert runModal];
+            if (choice == NSAlertFirstButtonReturn) {
+                NSArray *objectRequests = [PithosFileUtilities objectDataRequestsForSubdirWithContainerName:node.pithosContainer.name 
+                                                                                                 objectName:node.pithosObject.name 
+                                                                                                toDirectory:[dropDestination path] 
+                                                                                              checkIfExists:YES];
+                if (objectRequests) {
+                    for (ASIPithosObjectRequest *objectRequest in objectRequests) {
+                        [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
+                        // XXX set delegates and queue
+                        [objectRequest setCompletionBlock:^{
+                            NSLog(@"dl completed: %@", [objectRequest url]);
+                        }];
+                        [objectRequest setFailedBlock:^{
+                            NSLog(@"dl failed: %@, error: %@", [objectRequest url], [objectRequest error]);
+                        }];
+                        [objectRequest startAsynchronous];
+                    }
+                }
+            }
+        } else {
+            ASIPithosObjectRequest *objectRequest = [PithosFileUtilities objectDataRequestWithContainerName:node.pithosContainer.name 
+                                                                                                 objectName:node.pithosObject.name 
+                                                                                                toDirectory:[dropDestination path] 
+                                                                                              checkIfExists:YES];
+            if (objectRequest) {
+                [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
+                // XXX set delegates and queue
+                [objectRequest setCompletionBlock:^{
+                    NSLog(@"dl completed: %@", [objectRequest url]);
+                }];
+                [objectRequest setFailedBlock:^{
+                    NSLog(@"dl failed: %@, error: %@", [objectRequest url], [objectRequest error]);
+                }];
+                [objectRequest startAsynchronous];
+            }
+        }
+    }
+    return names;
+}
+
 #pragma mark -
-#pragma NSSplitViewDelegate
+#pragma mark NSSplitViewDelegate
 
 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex {
     return 120;
     }
 }
 
+#pragma mark -
+#pragma mark NSMenuDelegate
+
+- (void)menuNeedsUpdate:(NSMenu *)menu {
+    NSInteger column = [browser clickedColumn];
+    NSInteger row = [browser clickedRow];
+    [menu removeAllItems];
+    if ((column == -1) || (row == -1)) {
+        // General context menu has 0
+    } else {
+        // PithosNode menu has 1 items
+        // Get Info
+        NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Get Info" action:@selector(getInfo:) keyEquivalent:@""];
+        [menuItem setRepresentedObject:[browser itemAtRow:row inColumn:column]];
+        [menu addItem:menuItem];
+    }
+}
+
+#pragma mark -
+#pragma mark Menu Actions
+
+- (void)getInfo:(NSMenuItem *)sender {
+    [(PithosNode *)[sender representedObject] showPithosNodeInfo:sender];
+}
+
 @end
\ No newline at end of file