Loading all account containers.
authorMiltiadis Vasilakis <mvasilak@gmail.com>
Sun, 14 Aug 2011 22:14:57 +0000 (01:14 +0300)
committerMiltiadis Vasilakis <mvasilak@gmail.com>
Sun, 14 Aug 2011 22:14:57 +0000 (01:14 +0300)
If 'pithos' and 'trash' don't exist, an attempt is done to create them.
Other minor changes and fixes.

pithos-macos/PithosAccountNode.m
pithos-macos/PithosBrowserController.h
pithos-macos/PithosBrowserController.m
pithos-macos/PithosContainerNode.h
pithos-macos/PithosContainerNode.m

index 168bd0a..e0a3e0f 100644 (file)
@@ -70,7 +70,7 @@
         if (!accountRequest.didUseCachedResponse || ([containers count] != [someContainers count]) || !children) {
             // Save new children
             NSLog(@"using newChildren");
-            newChildren = [NSMutableArray array];
+            newChildren = [[NSMutableArray alloc] init];
             for (ASIPithosContainer *container in containers) {
                 PithosContainerNode *node = [[[PithosContainerNode alloc] initWithPithosContainer:container] autorelease];
                 if (children) {
index 0ff6617..fd4406b 100644 (file)
 
 #import <Cocoa/Cocoa.h>
 @class PithosNode;
+@class PithosAccountNode;
 
 @interface PithosBrowserController : NSWindowController <NSBrowserDelegate, NSSplitViewDelegate, NSOutlineViewDelegate> {    
     PithosNode *rootNode;
+    PithosAccountNode *accountNode;
     
     NSMutableArray *outlineViewDataSourceArray;
     
index b79e3c4..71abdfd 100644 (file)
 // or implied, of GRNET S.A.
 
 #import "PithosBrowserController.h"
-#import "ASIPithosRequest.h"
+#import "PithosNode.h"
 #import "PithosAccountNode.h"
 #import "PithosContainerNode.h"
 #import "PithosEmptyNode.h"
 #import "ImageAndTextCell.h"
 #import "FileSystemBrowserCell.h"
+#import "ASIPithosRequest.h"
+#import "ASIPithosContainerRequest.h"
+#import "ASIPithosContainer.h"
 
 //@interface PithosBrowserCell : NSBrowserCell {}
 @interface PithosBrowserCell : FileSystemBrowserCell {}
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [sharedPreviewController release];
     [outlineViewDataSourceArray release];
+    [accountNode release];
     [rootNode release];
     [browser release];
     [splitView release];
     // CONTAINERS
        NSTreeNode *containersTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
                             [[[PithosEmptyNode alloc] initWithDisplayName:@"CONTAINERS" icon:nil] autorelease]];
-    // CONTAINERS/pithos
-       [[containersTreeNode mutableChildNodes] addObject:
-     [NSTreeNode treeNodeWithRepresentedObject:
-      [[[PithosContainerNode alloc] initWithContainerName:@"pithos" 
-                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)]
-        ] autorelease]]];
-    // CONTAINERS/trash
-       [[containersTreeNode mutableChildNodes] addObject:
-     [NSTreeNode treeNodeWithRepresentedObject:
-      [[[PithosContainerNode alloc] initWithContainerName:@"trash"
-                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)]
-        ] autorelease]]];
+//    // CONTAINERS/pithos
+//     [[containersTreeNode mutableChildNodes] addObject:
+//     [NSTreeNode treeNodeWithRepresentedObject:
+//      [[[PithosContainerNode alloc] initWithContainerName:@"pithos" 
+//                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)]
+//        ] autorelease]]];
+//    // CONTAINERS/trash
+//     [[containersTreeNode mutableChildNodes] addObject:
+//     [NSTreeNode treeNodeWithRepresentedObject:
+//      [[[PithosContainerNode alloc] initWithContainerName:@"trash"
+//                                                     icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)]
+//        ] autorelease]]];
     // SHARED
        NSTreeNode *sharedTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
                                       [[[PithosEmptyNode alloc] initWithDisplayName:@"SHARED" icon:nil] autorelease]];
        // Expand the folder outline view
     [outlineView expandItem:nil expandChildren:YES];
        [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
+    
+    // Create accountNode and trigger a refresh
+    accountNode = [[PithosAccountNode alloc] init];
+    accountNode.children;
 }
 
 - (void)windowDidLoad {
                                                  name:@"PithosSubdirNodeChildrenUpdated" 
                                                object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self 
+                                             selector:@selector(pithosAccountNodeChildrenUpdated:) 
+                                                 name:@"PithosAccountNodeChildrenUpdated" 
+                                               object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self 
                                              selector:@selector(resetContainers) 
                                                  name:@"PithosAuthenticationCredentialsUpdated" 
                                                object:nil];
     }
 }
 
+- (void)pithosAccountNodeChildrenUpdated:(NSNotification *)notification {
+    BOOL containerPithosFound = NO;
+    BOOL containerTrashFound = NO;
+    //NSMutableArray *containersTreeNodeChildren = [[outlineViewDataSourceArray objectAtIndex:0] mutableChildNodes];
+    NSMutableArray *containersTreeNodeChildren = [NSMutableArray array];
+    for (PithosContainerNode *containerNode in accountNode.children) {
+        if ([containerNode.pithosContainer.name isEqualToString:@"pithos"]) {
+            containerNode.icon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)];
+            [containersTreeNodeChildren insertObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode] atIndex:0];
+            containerPithosFound = YES;
+        } else if ([containerNode.pithosContainer.name isEqualToString:@"trash"]) {
+            containerNode.icon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)];            
+            NSUInteger insertIndex = 1;
+            if (!containerPithosFound)
+                insertIndex = 0;
+            [containersTreeNodeChildren insertObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode] atIndex:insertIndex];
+            containerTrashFound = YES;
+        } else {
+            [containersTreeNodeChildren addObject:[NSTreeNode treeNodeWithRepresentedObject:containerNode]];
+        }
+    }
+    BOOL refreshAccountNode = NO;
+    if (!containerPithosFound) {
+        // create pithos
+        ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest createOrUpdateContainerRequestWithContainerName:@"pithos"];
+        [containerRequest startSynchronous];
+        if ([containerRequest error]) {
+            NSLog(@"error:%@", [containerRequest error]);
+            // XXX do something on error
+        } else {
+            refreshAccountNode = YES;
+        }
+    }
+    if (!containerTrashFound) {
+        // create trash
+        ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest createOrUpdateContainerRequestWithContainerName:@"trash"];
+        [containerRequest startSynchronous];
+        if ([containerRequest error]) {
+            NSLog(@"error:%@", [containerRequest error]);
+            // XXX do something on error
+        } else {
+            refreshAccountNode = YES;
+        }
+    }
+    if (refreshAccountNode) {
+        [accountNode invalidateChildren];
+        accountNode.children;
+    } else {
+        [[[outlineViewDataSourceArray objectAtIndex:0] mutableChildNodes] setArray:containersTreeNodeChildren];
+        self.outlineViewDataSourceArray = outlineViewDataSourceArray;
+        
+        // Expand the folder outline view
+        [outlineView expandItem:nil expandChildren:YES];
+        [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
+    }
+}
+
+
 #pragma mark -
 #pragma Actions
 
index ccc88f6..f014bf6 100644 (file)
@@ -55,4 +55,6 @@
 
 @property(retain) ASIPithosContainer *pithosContainer;
 
+- (void)setIcon:(NSImage *)anIcon;
+
 @end
\ No newline at end of file
index 8e1679c..aac59b7 100644 (file)
@@ -50,7 +50,7 @@ static NSImage *sharedIcon = nil;
 
 + (void)initialize {
        if (self == [PithosContainerNode class])
-        sharedIcon = [[NSImage imageNamed:@"container"] retain];
+        sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericHardDiskIcon)] retain];
 }
 
 #pragma mark -
@@ -236,4 +236,9 @@ static NSImage *sharedIcon = nil;
     return sharedIcon;
 }
 
+- (void)setIcon:(NSImage *)anIcon {
+    [icon release];
+    icon = [anIcon retain];
+}
+
 @end