2 // PithosBrowserController.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
38 #import "PithosBrowserController.h"
39 #import "ASIPithosRequest.h"
40 #import "PithosAccountNode.h"
41 #import "PithosContainerNode.h"
42 #import "PithosEmptyNode.h"
43 #import "ImageAndTextCell.h"
44 #import "FileSystemBrowserCell.h"
46 //@interface PithosBrowserCell : NSBrowserCell {}
47 @interface PithosBrowserCell : FileSystemBrowserCell {}
50 @implementation PithosBrowserCell
53 if ((self = [super init])) {
54 [self setLineBreakMode:NSLineBreakByTruncatingMiddle];
59 - (void)setObjectValue:(id)object {
60 if ([object isKindOfClass:[PithosNode class]]) {
61 PithosNode *node = (PithosNode *)object;
62 [self setStringValue:node.displayName];
63 [self setImage:node.icon];
64 // // All cells are set as leafs because a branchingImage is already set!
65 // // Maybe this cell is already inside an NSBrowserCell
66 // [self setLeaf:YES];
68 [super setObjectValue:object];
74 @interface PithosOutlineViewCell : ImageAndTextCell {}
77 @implementation PithosOutlineViewCell
79 - (void)setObjectValue:(id)object {
80 if ([object isKindOfClass:[PithosNode class]]) {
81 PithosNode *node = (PithosNode *)object;
82 [self setStringValue:node.displayName];
83 [self setImage:node.icon];
84 [self setEditable:NO];
86 [super setObjectValue:object];
92 @interface PithosBrowserController (Private) {}
93 - (void)resetContainers;
96 @implementation PithosBrowserController
97 @synthesize outlineViewDataSourceArray, splitView, outlineView, browser;
100 #pragma Object Lifecycle
103 return [super initWithWindowNibName:@"PithosBrowserController"];
107 [[NSNotificationCenter defaultCenter] removeObserver:self];
108 [sharedPreviewController release];
109 [outlineViewDataSourceArray release];
113 [outlineView release];
117 - (void)awakeFromNib {
118 [browser setCellClass:[PithosBrowserCell class]];
121 - (void)resetContainers {
123 [browser loadColumnZero];
124 self.outlineViewDataSourceArray = nil;
126 // Create the outlineView tree
128 NSTreeNode *containersTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
129 [[[PithosEmptyNode alloc] initWithDisplayName:@"CONTAINERS" icon:nil] autorelease]];
131 [[containersTreeNode mutableChildNodes] addObject:
132 [NSTreeNode treeNodeWithRepresentedObject:
133 [[[PithosContainerNode alloc] initWithContainerName:@"pithos"
134 icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kToolbarHomeIcon)]
137 [[containersTreeNode mutableChildNodes] addObject:
138 [NSTreeNode treeNodeWithRepresentedObject:
139 [[[PithosContainerNode alloc] initWithContainerName:@"trash"
140 icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kFullTrashIcon)]
143 NSTreeNode *sharedTreeNode = [NSTreeNode treeNodeWithRepresentedObject:
144 [[[PithosEmptyNode alloc] initWithDisplayName:@"SHARED" icon:nil] autorelease]];
146 [[sharedTreeNode mutableChildNodes] addObject:
147 [NSTreeNode treeNodeWithRepresentedObject:
148 [[[PithosEmptyNode alloc] initWithDisplayName:@"my shared"
149 icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kUserIcon)]
151 // SHARED/others shared
152 [[sharedTreeNode mutableChildNodes] addObject:
153 [NSTreeNode treeNodeWithRepresentedObject:
154 [[[PithosEmptyNode alloc] initWithDisplayName:@"others shared"
155 icon:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGroupIcon)]
158 self.outlineViewDataSourceArray = [NSMutableArray arrayWithObjects:containersTreeNode, sharedTreeNode, nil];
160 // Expand the folder outline view
161 [outlineView expandItem:nil expandChildren:YES];
162 [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
165 - (void)windowDidLoad {
166 [[[outlineView tableColumns] objectAtIndex:0] setDataCell:[[[PithosOutlineViewCell alloc] init] autorelease]];
168 // Register for updates
169 [[NSNotificationCenter defaultCenter] addObserver:self
170 selector:@selector(pithosNodeChildrenUpdated:)
171 name:@"PithosContainerNodeChildrenUpdated"
173 [[NSNotificationCenter defaultCenter] addObserver:self
174 selector:@selector(pithosNodeChildrenUpdated:)
175 name:@"PithosSubdirNodeChildrenUpdated"
177 [[NSNotificationCenter defaultCenter] addObserver:self
178 selector:@selector(resetContainers)
179 name:@"PithosAuthenticationCredentialsUpdated"
186 - (void)pithosNodeChildrenUpdated:(NSNotification *)notification {
187 PithosNode *node = (PithosNode *)[notification object];
188 NSInteger lastColumn = [browser lastColumn];
189 for (NSInteger column = lastColumn; column >= 0; column--) {
190 if ([[browser parentForItemsInColumn:column] isEqualTo:node]) {
191 [browser reloadColumn:column];
192 if ((column == lastColumn - 1) && ([[browser parentForItemsInColumn:lastColumn] isLeafItem])) {
193 // This reloads the preview column
194 [browser setLastColumn:column];
205 - (IBAction)refresh:(id)sender {
206 for (NSInteger column = [browser lastColumn]; column >= 0; column--) {
207 [(PithosNode *)[browser parentForItemsInColumn:column] invalidateChildren];
209 [browser validateVisibleColumns];
213 #pragma NSBrowserDelegate
215 - (id)rootItemForBrowser:(NSBrowser *)browser {
219 - (NSInteger)browser:(NSBrowser *)browser numberOfChildrenOfItem:(id)item {
220 PithosNode *node = (PithosNode *)item;
221 return node.children.count;
224 - (id)browser:(NSBrowser *)browser child:(NSInteger)index ofItem:(id)item {
225 PithosNode *node = (PithosNode *)item;
226 return [node.children objectAtIndex:index];
229 - (BOOL)browser:(NSBrowser *)browser isLeafItem:(id)item {
230 PithosNode *node = (PithosNode *)item;
231 return node.isLeafItem;
234 - (id)browser:(NSBrowser *)browser objectValueForItem:(id)item {
235 PithosNode *node = (PithosNode *)item;
239 - (NSViewController *)browser:(NSBrowser *)browser previewViewControllerForLeafItem:(id)item {
240 if (sharedPreviewController == nil)
241 sharedPreviewController = [[NSViewController alloc] initWithNibName:@"PithosBrowserPreviewController" bundle:[NSBundle bundleForClass:[self class]]];
242 return sharedPreviewController;
245 //- (CGFloat)browser:(NSBrowser *)browser shouldSizeColumn:(NSInteger)columnIndex forUserResize:(BOOL)forUserResize toWidth:(CGFloat)suggestedWidth {
246 // if (!forUserResize) {
247 // id item = [browser parentForItemsInColumn:columnIndex];
248 // if ([self browser:browser isLeafItem:item]) {
249 // suggestedWidth = 200;
252 // return suggestedWidth;
255 - (BOOL)browser:(NSBrowser *)sender isColumnValid:(NSInteger)column {
260 #pragma NSSplitViewDelegate
262 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex {
266 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex {
271 #pragma mark NSOutlineViewDelegate
273 - (BOOL)outlineView:outlineView shouldSelectItem:(id)item {
274 return ([[item representedObject] isLeaf]);
277 - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
278 return (![[item representedObject] isLeaf]);
281 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
282 PithosNode *node = [[[outlineView itemAtRow:[outlineView selectedRow]] representedObject] representedObject];
285 [browser loadColumnZero];