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 "PithosSharingAccountsNode.h"
39 #import "PithosAccountNode.h"
40 #import "ASIPithosRequest.h"
41 #import "ASIPithosAccount.h"
42 #import "ASIDownloadCache.h"
43 #import "PithosFileUtilities.h"
45 @implementation PithosSharingAccountsNode
48 #pragma mark Object Lifecycle
51 if ((self == [super init])) {
52 self.sharingAccount = @"";
58 [sharingAccountsRequest clearDelegatesAndCancel];
59 [sharingAccountsRequest release];
60 [sharingAccounts release];
65 #pragma mark Properties
69 url = [[ASIPithosRequest storageURLPrefix] copy];
73 - (NSArray *)children {
76 case PithosNodeStateFresh:
78 case PithosNodeStateRefreshNeeded:
79 freshness = PithosNodeStateRefreshing;
80 sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0
82 sharingAccountsRequest.delegate = self;
83 sharingAccountsRequest.didFinishSelector = @selector(sharingAccountsRequestFinished:);
84 sharingAccountsRequest.didFailSelector = @selector(sharingAccountsRequestFailed:);
86 sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
87 [sharingAccountsRequest startAsynchronous];
89 case PithosNodeStateRefreshing:
91 case PithosNodeStateRefreshFinished:
94 children = newChildren;
97 freshness = PithosNodeStateFresh;
105 - (NSString *)displayName {
106 if (displayName == nil)
107 return [NSString stringWithString:@"sharing accounts"];
108 return [[displayName copy] autorelease];
112 #pragma mark ASIHTTPRequestDelegate
114 - (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request {
115 NSLog(@"URL: %@", [sharingAccountsRequest url]);
116 NSLog(@"cached: %d", [sharingAccountsRequest didUseCachedResponse]);
118 NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts];
119 if (sharingAccounts == nil) {
120 sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts];
122 [sharingAccounts addObjectsFromArray:someSharingAccounts];
124 if ([someSharingAccounts count] < 10000) {
125 if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) {
127 NSLog(@"using newChildren");
128 newChildren = [[NSMutableArray alloc] init];
129 NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
130 for (ASIPithosAccount *account in sharingAccounts) {
131 if (![account.name isEqualToString:[ASIPithosRequest authUser]]) {
132 PithosAccountNode *node = [[[PithosAccountNode alloc] init] autorelease];
134 node.shared = shared;
135 node.sharingAccount = account.name;
137 NSUInteger oldIndex = [children indexOfObject:node];
138 if (oldIndex != NSNotFound) {
139 // Use the same pointer value, if possible
140 node = [children objectAtIndex:oldIndex];
141 [keptNodes addIndex:oldIndex];
144 [newChildren addObject:node];
147 [[children objectsAtIndexes:
148 [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
149 if ([keptNodes containsIndex:idx])
152 }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
154 // Else cache was used and all results were fetched during this request, so existing children can be reused
155 [sharingAccountsRequest release];
156 sharingAccountsRequest = nil;
157 [sharingAccounts release];
158 sharingAccounts = nil;
160 @synchronized(self) {
161 freshness = PithosNodeStateRefreshFinished;
163 // Notify observers that children are updated
164 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosSharingAccountsNodeChildrenUpdated" object:self];
166 [sharingAccountsRequest release];
167 // Do an additional request to fetch more objects
168 sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0
169 marker:[[someSharingAccounts lastObject] name]] retain];
170 sharingAccountsRequest.delegate = self;
172 sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
173 [sharingAccountsRequest startAsynchronous];
177 - (void)accountRequestFailed:(ASIPithosRequest *)request {
178 [PithosFileUtilities httpRequestErrorAlertWithRequest:request];
179 [newChildren release];
181 [sharingAccountsRequest release];
182 sharingAccountsRequest = nil;
183 [sharingAccounts release];
184 sharingAccounts = nil;
186 @synchronized(self) {
187 freshness = PithosNodeStateRefreshNeeded;