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 "PithosContainer.h"
39 #import "PithosObject.h"
40 #import "PithosSubdir.h"
41 #import "ASIPithosContainerRequest.h"
42 #import "ASIPithosObject.h"
43 #import "ASIDownloadCache.h"
45 static NSImage *icon = nil;
47 @implementation PithosContainer
48 @synthesize containerName;
51 if (self == [PithosContainer class])
52 icon = [[NSImage imageNamed:@"container"] retain];
56 #pragma mark Object Lifecycle
58 - (id)initWithContainerName:(NSString *)aContainerName {
59 if ((self = [super init])) {
60 containerName = [aContainerName copy];
70 [containerName release];
75 #pragma mark ASIHTTPRequestDelegate
77 - (void)requestFinished:(ASIPithosContainerRequest *)containerRequest {
78 NSLog(@"URL: %@", [containerRequest url]);
79 NSLog(@"cached: %d", [containerRequest didUseCachedResponse]);
81 NSArray *someObjects = [containerRequest objects];
83 objects = [[NSMutableArray alloc] initWithArray:someObjects];
85 [objects addObjectsFromArray:someObjects];
87 if ([someObjects count] < 10000) {
88 if (!containerRequest.didUseCachedResponse || ([objects count] != [someObjects count]) || !children) {
90 NSLog(@"using newChildren");
91 NSMutableArray *newChildren = [NSMutableArray array];
92 for (ASIPithosObject *object in objects) {
94 PithosSubdir *node = [[[PithosSubdir alloc] initWithContainerName:containerName subdirName:object.name] autorelease];
96 NSUInteger oldIndex = [children indexOfObject:node];
97 if (oldIndex != NSNotFound)
98 // Use the same pointer value, if possible
99 node = [children objectAtIndex:oldIndex];
101 [newChildren addObject:node];
103 PithosObject *node = [[[PithosObject alloc] initWithContainerName:containerName objectName:object.name] autorelease];
105 NSUInteger oldIndex = [children indexOfObject:node];
106 if (oldIndex != NSNotFound)
107 // Use the same pointer value, if possible
108 node = [children objectAtIndex:oldIndex];
110 [newChildren addObject:node];
114 children = [newChildren retain];
116 // Else cache was used and all results were fetched during this request, so previousChildren can be reused
119 // XXX sort then based on preferences
121 // Notify observers that children are updated
122 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosNodeChildrenUpdated" object:self];
124 // Do an additional request to fetch more objects
125 ASIPithosContainerRequest *newContainerRequest = [ASIPithosContainerRequest listObjectsRequestWithContainerName:containerName
127 marker:[[someObjects lastObject] name]
134 newContainerRequest.delegate = self;
135 newContainerRequest.downloadCache = [ASIDownloadCache sharedCache];
136 [newContainerRequest startAsynchronous];
140 - (void)requestFailed:(ASIPithosContainerRequest *)containerRequest {
141 // XXX do something on error, cleanup
142 NSLog(@"error:%@", [containerRequest error]);
150 #pragma mark Properties
154 url = [[NSString alloc] initWithFormat:@"%@/%@", [ASIPithosRequest storageURL], containerName];
158 - (NSArray *)children {
160 @synchronized (self) {
164 ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest listObjectsRequestWithContainerName:containerName
173 containerRequest.delegate = self;
174 containerRequest.downloadCache = [ASIDownloadCache sharedCache];
175 [containerRequest startAsynchronous];
182 - (NSString *)displayName {
183 return [[containerName copy] autorelease];