Drag and drop download fixes.
[pithos-macos] / pithos-macos / PithosBrowserController.m
1 //
2 //  PithosBrowserController.m
3 //  pithos-macos
4 //
5 // Copyright 2011 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
15 //   2. Redistributions in binary form must reproduce the above
16 //      copyright notice, this list of conditions and the following
17 //      disclaimer in the documentation and/or other materials
18 //      provided with the distribution.
19 // 
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 // 
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
37
38 #import "PithosBrowserController.h"
39 #import "PithosNode.h"
40 #import "PithosAccountNode.h"
41 #import "PithosContainerNode.h"
42 #import "PithosSubdirNode.h"
43 #import "PithosObjectNode.h"
44 #import "PithosEmptyNode.h"
45 #import "ImageAndTextCell.h"
46 #import "FileSystemBrowserCell.h"
47 #import "ASIPithosRequest.h"
48 #import "ASIPithosContainerRequest.h"
49 #import "ASIPithosObjectRequest.h"
50 #import "ASIPithosContainer.h"
51 #import "ASIPithosObject.h"
52 #import "PithosFileUtilities.h"
53
54 //@interface PithosBrowserCell : NSBrowserCell {}
55 @interface PithosBrowserCell : FileSystemBrowserCell {}
56 @end
57
58 @implementation PithosBrowserCell
59
60 - (id)init {
61     if ((self = [super init])) {
62         [self setLineBreakMode:NSLineBreakByTruncatingMiddle];
63     }
64     return self;
65 }
66
67 - (void)setObjectValue:(id)object {
68     if ([object isKindOfClass:[PithosNode class]]) {
69         PithosNode *node = (PithosNode *)object;
70         [self setStringValue:node.displayName];
71         [self setImage:node.icon];
72 //        // All cells are set as leafs because a branchingImage is already set!
73 //        // Maybe this cell is already inside an NSBrowserCell
74 //        [self setLeaf:YES];
75     } else {
76         [super setObjectValue:object];
77     }
78 }
79
80 @end
81
82 @interface PithosOutlineViewCell : ImageAndTextCell {}
83 @end
84
85 @implementation PithosOutlineViewCell
86
87 - (void)setObjectValue:(id)object {
88     if ([object isKindOfClass:[PithosNode class]]) {
89         PithosNode *node = (PithosNode *)object;
90         [self setStringValue:node.displayName];
91         [self setImage:node.icon];
92         [self setEditable:NO];
93     } else {
94         [super setObjectValue:object];
95     }
96 }
97
98 @end
99
100 @interface PithosBrowserController (Private) {}
101 - (void)resetContainers;
102 - (void)getInfo:(NSMenuItem *)sender;
103 - (void)downloadObjectFinished:(ASIPithosObjectRequest *)objectRequest;
104 - (void)downloadObjectFailed:(ASIPithosObjectRequest *)objectRequest;
105 - (void)uploadObjectUsingHashMapFinished:(ASIPithosObjectRequest *)objectRequest;
106 - (void)uploadObjectUsingHashMapFailed:(ASIPithosObjectRequest *)objectRequest;
107 - (void)uploadMissingHashesFinished:(ASIPithosObjectRequest *)objectRequest;
108 - (void)uploadMissingHashesFailed:(ASIPithosObjectRequest *)objectRequest;
109 @end
110
111 @implementation PithosBrowserController
112 @synthesize outlineViewDataSourceArray, splitView, outlineView, browser;
113
114 #pragma mark -
115 #pragma Object Lifecycle
116
117 - (id)init {
118     return [super initWithWindowNibName:@"PithosBrowserController"];
119 }
120
121 - (void)dealloc {
122     [[NSNotificationCenter defaultCenter] removeObserver:self];
123     [browserMenu release];
124     [sharedPreviewController release];
125     [outlineViewDataSourceArray release];
126     [accountNode release];
127     [rootNode release];
128     [super dealloc];
129 }
130
131 - (void)awakeFromNib {
132     [super awakeFromNib];
133     
134     [browser registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
135     [browser setDraggingSourceOperationMask:NSDragOperationNone forLocal:YES];
136     [browser setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
137     
138     [browser setCellClass:[PithosBrowserCell class]];
139     
140     browserMenu = [[NSMenu alloc] init];
141     [browserMenu setDelegate:self];
142     [browser setMenu:browserMenu];
143 }
144
145 - (void)resetContainers {
146     rootNode = nil;
147     [browser loadColumnZero];
148     self.outlineViewDataSourceArray = nil;
149     
150     // Create the outlineView tree
151     // CONTAINERS
152         NSTreeNode *containersTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
153                             [[[PithosEmptyNode alloc] initWithDisplayName:@"CONTAINERS" icon:nil] autorelease]];
154 //    // CONTAINERS/pithos
155 //      [[containersTreeNode mutableChildNodes] addObject:
156 //     [NSTreeNode treeNodeWithRepresentedObject:
157 //      [[[PithosContainerNode alloc] initWithContainerName:@"pithos" 
158 //                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)]
159 //        ] autorelease]]];
160 //    // CONTAINERS/trash
161 //      [[containersTreeNode mutableChildNodes] addObject:
162 //     [NSTreeNode treeNodeWithRepresentedObject:
163 //      [[[PithosContainerNode alloc] initWithContainerName:@"trash"
164 //                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)]
165 //        ] autorelease]]];
166     // SHARED
167         NSTreeNode *sharedTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
168                                       [[[PithosEmptyNode alloc] initWithDisplayName:@"SHARED" icon:nil] autorelease]];
169     // SHARED/my shared
170         [[sharedTreeNode mutableChildNodes] addObject:
171      [NSTreeNode treeNodeWithRepresentedObject:
172       [[[PithosEmptyNode alloc] initWithDisplayName:@"my shared" 
173                                                icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kUserIcon)]
174         ] autorelease]]];
175     // SHARED/others shared
176         [[sharedTreeNode mutableChildNodes] addObject:
177      [NSTreeNode treeNodeWithRepresentedObject:
178       [[[PithosEmptyNode alloc] initWithDisplayName:@"others shared"
179                                                icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGroupIcon)]
180         ] autorelease]]];
181     
182     self.outlineViewDataSourceArray = [NSMutableArray arrayWithObjects:containersTreeNode, sharedTreeNode, nil];
183     
184         // Expand the folder outline view
185     [outlineView expandItem:nil expandChildren:YES];
186         [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
187     
188     // Create accountNode and trigger a refresh
189     accountNode = [[PithosAccountNode alloc] init];
190     accountNode.children;
191 }
192
193 - (void)windowDidLoad {
194     [super windowDidLoad];
195     
196     [[[outlineView tableColumns] objectAtIndex:0] setDataCell:[[[PithosOutlineViewCell alloc] init] autorelease]];
197     
198     // Register for updates
199     [[NSNotificationCenter defaultCenter] addObserver:self 
200                                              selector:@selector(pithosNodeChildrenUpdated:) 
201                                                  name:@"PithosContainerNodeChildrenUpdated" 
202                                                object:nil];
203     [[NSNotificationCenter defaultCenter] addObserver:self 
204                                              selector:@selector(pithosNodeChildrenUpdated:) 
205                                                  name:@"PithosSubdirNodeChildrenUpdated" 
206                                                object:nil];
207     [[NSNotificationCenter defaultCenter] addObserver:self 
208                                              selector:@selector(pithosAccountNodeChildrenUpdated:) 
209                                                  name:@"PithosAccountNodeChildrenUpdated" 
210                                                object:nil];
211     [[NSNotificationCenter defaultCenter] addObserver:self 
212                                              selector:@selector(resetContainers) 
213                                                  name:@"PithosAuthenticationCredentialsUpdated" 
214                                                object:nil];
215 }
216
217 #pragma mark -
218 #pragma Observers
219
220 - (void)pithosNodeChildrenUpdated:(NSNotification *)notification {
221     PithosNode *node = (PithosNode *)[notification object];
222     NSInteger lastColumn = [browser lastColumn];
223     for (NSInteger column = lastColumn; column >= 0; column--) {
224         if ([[browser parentForItemsInColumn:column] isEqualTo:node]) {
225             [browser reloadColumn:column];
226             if ((column == lastColumn - 1) && ([[browser parentForItemsInColumn:lastColumn] isLeafItem])) {
227                 // This reloads the preview column
228                 [browser setLastColumn:column];
229                 [browser addColumn];
230             }
231             return;
232         }
233     }
234 }
235
236 - (void)pithosAccountNodeChildrenUpdated:(NSNotification *)notification {
237     BOOL containerPithosFound = NO;
238     BOOL containerTrashFound = NO;
239     //NSMutableArray *containersTreeNodeChildren = [[outlineViewDataSourceArray objectAtIndex:0] mutableChildNodes];
240     NSMutableArray *containersTreeNodeChildren = [NSMutableArray array];
241     for (PithosContainerNode *containerNode in accountNode.children) {
242         if ([containerNode.pithosContainer.name isEqualToString:@"pithos"]) {
243             containerNode.icon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)];
244             [containersTreeNodeChildren insertObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode] atIndex:0];
245             containerPithosFound = YES;
246         } else if ([containerNode.pithosContainer.name isEqualToString:@"trash"]) {
247             containerNode.icon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)];            
248             NSUInteger insertIndex = 1;
249             if (!containerPithosFound)
250                 insertIndex = 0;
251             [containersTreeNodeChildren insertObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode] atIndex:insertIndex];
252             containerTrashFound = YES;
253         } else {
254             [containersTreeNodeChildren addObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode]];
255         }
256     }
257     BOOL refreshAccountNode = NO;
258     if (!containerPithosFound) {
259         // create pithos
260         ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest createOrUpdateContainerRequestWithContainerName:@"pithos"];
261         [containerRequest startSynchronous];
262         if ([containerRequest error]) {
263             NSLog(@"error:%@", [containerRequest error]);
264             // XXX do something on error
265         } else {
266             refreshAccountNode = YES;
267         }
268     }
269     if (!containerTrashFound) {
270         // create trash
271         ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest createOrUpdateContainerRequestWithContainerName:@"trash"];
272         [containerRequest startSynchronous];
273         if ([containerRequest error]) {
274             NSLog(@"error:%@", [containerRequest error]);
275             // XXX do something on error
276         } else {
277             refreshAccountNode = YES;
278         }
279     }
280     if (refreshAccountNode) {
281         [accountNode invalidateChildren];
282         accountNode.children;
283     } else {
284         [[[outlineViewDataSourceArray objectAtIndex:0] mutableChildNodes] setArray:containersTreeNodeChildren];
285         self.outlineViewDataSourceArray = outlineViewDataSourceArray;
286         
287         // Expand the folder outline view
288         [outlineView expandItem:nil expandChildren:YES];
289         [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
290         
291         [self refresh:nil];
292     }
293 }
294
295 #pragma mark -
296 #pragma Actions
297
298 - (IBAction)refresh:(id)sender {
299     for (NSInteger column = [browser lastColumn]; column >= 0; column--) {
300         [(PithosNode *)[browser parentForItemsInColumn:column] invalidateChildren];
301     }
302     [browser validateVisibleColumns];
303 }
304
305 #pragma mark -
306 #pragma NSBrowserDelegate
307
308 - (id)rootItemForBrowser:(NSBrowser *)browser {
309     return rootNode;    
310 }
311
312 - (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(id)item {
313     PithosNode *node = (PithosNode *)item;
314     return node.children.count;
315 }
316
317 - (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(id)item {
318     PithosNode *node = (PithosNode *)item;
319     return [node.children objectAtIndex:index];
320 }
321
322 - (BOOL)browser:(NSBrowser *)browser isLeafItem:(id)item {
323     PithosNode *node = (PithosNode *)item;
324     return node.isLeafItem;
325 }
326
327 - (id)browser:(NSBrowser *)browser objectValueForItem:(id)item {
328     PithosNode *node = (PithosNode *)item;
329     return node;
330 }
331
332 - (NSViewController *)browser:(NSBrowser *)browser previewViewControllerForLeafItem:(id)item {
333     if (sharedPreviewController == nil)
334         sharedPreviewController = [[NSViewController alloc] initWithNibName:@"PithosBrowserPreviewController" bundle:[NSBundle bundleForClass:[self class]]];
335     return sharedPreviewController;
336 }
337
338 //- (CGFloat)browser:(NSBrowser *)browser shouldSizeColumn:(NSInteger)columnIndex forUserResize:(BOOL)forUserResize toWidth:(CGFloat)suggestedWidth  {
339 //    if (!forUserResize) {
340 //        id item = [browser parentForItemsInColumn:columnIndex]; 
341 //        if ([self browser:browser isLeafItem:item]) {
342 //            suggestedWidth = 200; 
343 //        }
344 //    }
345 //    return suggestedWidth;
346 //}
347
348 - (BOOL)browser:(NSBrowser *)sender isColumnValid:(NSInteger)column {
349     return NO;
350 }
351
352 #pragma mark Drag and Drop source
353
354 - (BOOL)browser:(NSBrowser *)aBrowser writeRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column 
355    toPasteboard:(NSPasteboard *)pasteboard {
356     NSMutableArray *propertyList = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
357     NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; 
358     for (NSUInteger i = [rowIndexes firstIndex]; i <= [rowIndexes lastIndex]; i = [rowIndexes indexGreaterThanIndex:i]) {
359         PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]];
360         [propertyList addObject:[node.pithosObject.name pathExtension]];
361     }
362
363     [pasteboard declareTypes:[NSArray arrayWithObject:NSFilesPromisePboardType] owner:self];
364     [pasteboard setPropertyList:propertyList forType:NSFilesPromisePboardType];
365     
366     return YES;
367 }
368
369 - (NSArray *)browser:(NSBrowser *)aBrowser namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination 
370 forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
371     NSMutableArray *names = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
372     NSIndexPath *baseIndexPath = [browser indexPathForColumn:column]; 
373     for (NSUInteger i = [rowIndexes firstIndex]; i <= [rowIndexes lastIndex]; i = [rowIndexes indexGreaterThanIndex:i]) {
374         PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]];
375         
376         // If the node is a subdir ask if the whole tree should be downloaded
377         if ([node class] == [PithosSubdirNode class]) {
378             NSAlert *alert = [[[NSAlert alloc] init] autorelease];
379             [alert setMessageText:@"Download directory"];
380             [alert setInformativeText:[NSString stringWithFormat:@"'%@' is a directory, do you want to download its contents?", node.displayName]];
381             [alert addButtonWithTitle:@"OK"];
382             [alert addButtonWithTitle:@"Cancel"];
383             NSInteger choice = [alert runModal];
384             if (choice == NSAlertFirstButtonReturn) {
385                 NSArray *objectRequests = [PithosFileUtilities objectDataRequestsForSubdirWithContainerName:node.pithosContainer.name 
386                                                                                                  objectName:node.pithosObject.name 
387                                                                                                 toDirectory:[dropDestination path] 
388                                                                                               checkIfExists:YES];
389                 if (objectRequests) {
390                     for (ASIPithosObjectRequest *objectRequest in objectRequests) {
391                         [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
392                         objectRequest.delegate = self;
393                         objectRequest.didFinishSelector = @selector(downloadObjectFinished:);
394                         objectRequest.didFailSelector = @selector(downloadObjectFailed:);
395                         [objectRequest startAsynchronous];
396                     }
397                 }
398             }
399         } else {
400             ASIPithosObjectRequest *objectRequest = [PithosFileUtilities objectDataRequestWithContainerName:node.pithosContainer.name 
401                                                                                                  objectName:node.pithosObject.name 
402                                                                                                 toDirectory:[dropDestination path] 
403                                                                                               checkIfExists:YES];
404             if (objectRequest) {
405                 [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
406                 objectRequest.delegate = self;
407                 objectRequest.didFinishSelector = @selector(downloadObjectFinished:);
408                 objectRequest.didFailSelector = @selector(downloadObjectFailed:);
409                 [objectRequest startAsynchronous];
410             }
411         }
412     }
413     return names;
414 }
415
416 #pragma mark Drag and Drop destination
417
418 - (NSDragOperation)browser:aBrowser 
419               validateDrop:(id<NSDraggingInfo>)info 
420                proposedRow:(NSInteger *)row 
421                     column:(NSInteger *)column 
422              dropOperation:(NSBrowserDropOperation *)dropOperation {
423     NSDragOperation result = NSDragOperationNone;
424     // Files from the finder are accepted
425     if ([[[info draggingPasteboard] types] indexOfObject:NSFilenamesPboardType] != -1) {
426         // For a between drop, we let the user drop "on" the parent item
427         if (*dropOperation == NSBrowserDropAbove)
428             *row = -1;
429         // Only allow dropping in folders
430         if (*column != -1) {
431             if (*row != -1) {
432                 PithosNode *node = [browser itemAtRow:*row inColumn:*column];
433                 if ([node class] != [PithosSubdirNode class])
434                     *row = -1;
435             *dropOperation = NSBrowserDropOn;
436             result = NSDragOperationCopy;
437             }
438         }
439     }
440     // XXX else local file promises
441     return result;
442 }
443
444 - (BOOL)browser:(NSBrowser *)aBrowser 
445      acceptDrop:(id<NSDraggingInfo>)info 
446           atRow:(NSInteger)row 
447          column:(NSInteger)column 
448   dropOperation:(NSBrowserDropOperation)dropOperation {
449     NSArray *filenames = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
450     NSLog(@"drag in filenames: %@", filenames);
451     PithosNode *node = nil;
452     if ((column != -1) && (filenames != nil)) {
453         if (row != -1)
454             node = [browser itemAtRow:row inColumn:column];
455         else
456             node = [browser parentForItemsInColumn:column];
457         NSLog(@"drag in node: %@", node.url);
458         if (([node class] != [PithosSubdirNode class]) && ([node class] != [PithosContainerNode class]))
459             return NO;
460         
461         NSFileManager *defaultManager = [NSFileManager defaultManager];
462         NSString *containerName = [NSString stringWithString:node.pithosContainer.name];
463         NSString *objectNamePrefix;
464         if ([node class] == [PithosSubdirNode class])
465             objectNamePrefix = [NSString stringWithString:node.pithosObject.name];
466         else
467             objectNamePrefix = [NSString stringWithString:@""];
468         NSUInteger blockSize = node.pithosContainer.blockSize;
469         NSString *blockHash = node.pithosContainer.blockHash;
470         
471         for (NSString *filePath in filenames) {
472             BOOL isDirectory;
473             if ([defaultManager fileExistsAtPath:filePath isDirectory:&isDirectory]) {
474                 if (!isDirectory) {
475                     // Upload file
476                     NSString *objectName = [objectNamePrefix stringByAppendingPathComponent:[filePath lastPathComponent]];
477                     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
478                     dispatch_async(queue, ^{
479                         NSError *error = nil;
480                         NSURLResponse *response = nil;
481                         [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath] 
482                                                                                  cachePolicy:NSURLCacheStorageNotAllowed 
483                                                                              timeoutInterval:.1] 
484                                               returningResponse:&response 
485                                                           error:&error];
486                         NSString *contentType = [response MIMEType];
487                         if (contentType == nil)
488                             contentType = @"application/binary";
489                         if (error)
490                             NSLog(@"contentType detection error: %@", error);
491                         NSArray *hashes = nil;
492                         ASIPithosObjectRequest *objectRequest = [PithosFileUtilities writeObjectDataRequestWithContainerName:containerName 
493                                                                                                                   objectName:objectName 
494                                                                                                                  contentType:contentType 
495                                                                                                                    blockSize:blockSize 
496                                                                                                                    blockHash:blockHash 
497                                                                                                                      forFile:filePath 
498                                                                                                                checkIfExists:YES 
499                                                                                                                       hashes:&hashes];
500                         if (objectRequest) {
501                             // XXX set delegates and queue
502                             objectRequest.delegate = self;
503                             objectRequest.didFinishSelector = @selector(uploadObjectUsingHashMapFinished:);
504                             objectRequest.didFailSelector = @selector(uploadObjectUsingHashMapFailed:);
505                             objectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
506                                                       containerName, @"containerName", 
507                                                       objectName, @"objectName", 
508                                                       contentType, @"contentType", 
509                                                       [NSNumber numberWithUnsignedInteger:blockSize], @"blockSize", 
510                                                       blockHash, @"blockHash", 
511                                                       filePath, @"filePath", 
512                                                       hashes, @"hashes", 
513                                                       [NSNumber numberWithBool:YES], @"checkIfExists", 
514                                                       node, @"node", 
515                                                       [NSNumber numberWithUnsignedInteger:0], @"iteration", 
516                                                       nil];
517                             [objectRequest startAsynchronous];
518                         }
519                         // XXX else show alert?
520                     });
521                 } else {
522                     // Upload directory, confirm first
523                     // XXX implement this
524                 }
525             }
526             
527         }
528         return YES;
529     }
530
531     return NO;
532 }
533
534 #pragma mark -
535 #pragma mark ASIHTTPRequestDelegate
536
537 - (void)downloadObjectFinished:(ASIPithosObjectRequest *)objectRequest {
538     NSLog(@"download completed: %@", [objectRequest url]);
539     if ([objectRequest bytes] == 0) {
540         NSLog(@"downloaded  0 bytes");
541         NSFileManager *defaultManager = [NSFileManager defaultManager];
542         NSString *filePath = [objectRequest.userInfo objectForKey:@"filePath"];
543         if (![defaultManager fileExistsAtPath:filePath]) {
544             if (![defaultManager createFileAtPath:filePath contents:nil attributes:nil]) {
545                 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
546                 [alert setMessageText:@"File Creation Error"];
547                 [alert setInformativeText:[NSString stringWithFormat:@"Couldn't create zero length file at %@", filePath]];
548                 [alert addButtonWithTitle:@"OK"];
549                 [alert runModal];
550             }
551         }
552     }
553 }
554
555 - (void)downloadObjectFailed:(ASIPithosObjectRequest *)objectRequest {
556     NSLog(@"download failed: %@, error: %@", [objectRequest url], [objectRequest error]);
557     NSAlert *alert = [[[NSAlert alloc] init] autorelease];
558     [alert setMessageText:@"HTTP Request Error"];
559     [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [objectRequest error]]];
560     [alert addButtonWithTitle:@"OK"];
561     [alert runModal];
562 }
563
564 - (void)uploadObjectUsingHashMapFinished:(ASIPithosObjectRequest *)objectRequest {
565     NSLog(@"upload using hashmap completed: %@", [objectRequest url]);
566     if (objectRequest.responseStatusCode == 201) {
567         NSLog(@"object created: %@", [objectRequest url]);
568         PithosNode *node = [objectRequest.userInfo objectForKey:@"node"];
569         [node invalidateChildren];
570         node.children;
571     } else if (objectRequest.responseStatusCode == 409) {
572         NSUInteger iteration = [[objectRequest.userInfo objectForKey:@"iteration"] unsignedIntegerValue] + 1;
573         if (iteration > 10) {
574             NSLog(@"upload iteration limit reached: %@", [objectRequest url]);
575             // XXX show alert
576             return;
577         }
578         NSLog(@"object is missing hashes: %@", [objectRequest url]);
579         ASIPithosObjectRequest *newObjectRequest = [PithosFileUtilities updateObjectDataRequestWithContainerName:[objectRequest.userInfo objectForKey:@"containerName"]
580                                                                                                       objectName:@".upload" 
581                                                                                                      contentType:[objectRequest.userInfo objectForKey:@"contentType"]
582                                                                                                        blockSize:[[objectRequest.userInfo objectForKey:@"blockSize"] unsignedIntegerValue]
583                                                                                                          forFile:[objectRequest.userInfo objectForKey:@"filePath"]
584                                                                                                           hashes:[objectRequest.userInfo objectForKey:@"hashes"] 
585                                                                                            missingHashesResponse:[objectRequest responseString] 
586                                                                                                    checkIfExists:[[objectRequest.userInfo objectForKey:@"checkIfExists"] boolValue]];
587         newObjectRequest.shouldAttemptPersistentConnection = NO;
588         newObjectRequest.delegate = self;
589         newObjectRequest.didFinishSelector = @selector(uploadMissingHashesFinished:);
590         newObjectRequest.didFailSelector = @selector(uploadMissingHashesFailed:);
591         newObjectRequest.userInfo = objectRequest.userInfo;
592         [(NSMutableDictionary *)(newObjectRequest.userInfo) setObject:[NSNumber numberWithBool:NO] forKey:@"checkIfExists"];
593         [(NSMutableDictionary *)(newObjectRequest.userInfo) setObject:[NSNumber numberWithUnsignedInteger:iteration] forKey:@"iteration"];
594         [newObjectRequest startAsynchronous];
595     } else {
596         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
597         [alert setMessageText:@"Unexpected Response Status"];
598         [alert setInformativeText:[NSString stringWithFormat:@"Unexpected response status %d - %@", objectRequest.responseStatusCode, objectRequest.responseStatusMessage]];
599         [alert addButtonWithTitle:@"OK"];
600         [alert runModal];
601     }
602 }
603
604 - (void)uploadObjectUsingHashMapFailed:(ASIPithosObjectRequest *)objectRequest {
605     NSLog(@"upload failed: %@, error: %@", [objectRequest url], [objectRequest error]);
606     NSAlert *alert = [[[NSAlert alloc] init] autorelease];
607     [alert setMessageText:@"HTTP Request Error"];
608     [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [objectRequest error]]];
609     [alert addButtonWithTitle:@"OK"];
610     [alert runModal];
611 }
612
613 - (void)uploadMissingHashesFinished:(ASIPithosObjectRequest *)objectRequest {
614     NSLog(@"upload of missing hashes completed: %@", [objectRequest url]);
615     if ((objectRequest.responseStatusCode == 201) || (objectRequest.responseStatusCode == 204)) {
616         NSArray *hashes = [objectRequest.userInfo objectForKey:@"hashes"];
617         ASIPithosObjectRequest *newObjectRequest = [PithosFileUtilities writeObjectDataRequestWithContainerName:[objectRequest.userInfo objectForKey:@"containerName"] 
618                                                                                                      objectName:[objectRequest.userInfo objectForKey:@"objectName"] 
619                                                                                                     contentType:[objectRequest.userInfo objectForKey:@"contentType"] 
620                                                                                                       blockSize:[[objectRequest.userInfo objectForKey:@"blockSize"] unsignedIntegerValue] 
621                                                                                                       blockHash:[objectRequest.userInfo objectForKey:@"blockHash"]
622                                                                                                         forFile:[objectRequest.userInfo objectForKey:@"filePath"] 
623                                                                                                   checkIfExists:NO 
624                                                                                                          hashes:&hashes];
625         newObjectRequest.delegate = self;
626         newObjectRequest.didFinishSelector = @selector(uploadObjectUsingHashMapFinished:);
627         newObjectRequest.didFailSelector = @selector(uploadObjectUsingHashMapFailed:);
628         newObjectRequest.userInfo = objectRequest.userInfo;
629         [newObjectRequest startAsynchronous];
630     } else {
631         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
632         [alert setMessageText:@"Unexpected Response Status"];
633         [alert setInformativeText:[NSString stringWithFormat:@"Unexpected response status %d - %@", objectRequest.responseStatusCode, objectRequest.responseStatusMessage]];
634         [alert addButtonWithTitle:@"OK"];
635         [alert runModal];
636     }
637 }
638
639 - (void)uploadMissingHashesFailed:(ASIPithosObjectRequest *)objectRequest {    
640     NSLog(@"upload of missing hashes failed: %@, error: %@", [objectRequest url], [objectRequest error]);
641     NSAlert *alert = [[[NSAlert alloc] init] autorelease];
642     [alert setMessageText:@"HTTP Request Error"];
643     [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [objectRequest error]]];
644     [alert addButtonWithTitle:@"OK"];
645     [alert runModal];
646 }
647
648 #pragma mark -
649 #pragma mark NSSplitViewDelegate
650
651 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex {
652     return 120;
653 }
654
655 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex {
656     return 220;
657 }
658
659 #pragma mark -
660 #pragma mark NSOutlineViewDelegate
661
662 - (BOOL)outlineView:outlineView shouldSelectItem:(id)item {
663     return ([[item representedObject] isLeaf]);
664 }
665
666 - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
667         return (![[item representedObject] isLeaf]);
668 }
669
670 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
671     PithosNode *node = [[[outlineView itemAtRow:[outlineView selectedRow]] representedObject] representedObject];
672     if (node) {
673         rootNode = node;
674         [browser loadColumnZero];
675     }
676 }
677
678 #pragma mark -
679 #pragma mark NSMenuDelegate
680
681 - (void)menuNeedsUpdate:(NSMenu *)menu {
682     NSInteger column = [browser clickedColumn];
683     NSInteger row = [browser clickedRow];
684     [menu removeAllItems];
685     if ((column == -1) || (row == -1)) {
686         // General context menu has 0
687     } else {
688         // PithosNode menu has 1 items
689         // Get Info
690         NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Get Info" action:@selector(getInfo:) keyEquivalent:@""];
691         [menuItem setRepresentedObject:[browser itemAtRow:row inColumn:column]];
692         [menu addItem:menuItem];
693     }
694 }
695
696 #pragma mark -
697 #pragma mark Menu Actions
698
699 - (void)getInfo:(NSMenuItem *)sender {
700     [(PithosNode *)[sender representedObject] showPithosNodeInfo:sender];
701 }
702
703 @end