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:);
85 sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
86 [sharingAccountsRequest startAsynchronous];
88 case PithosNodeStateRefreshing:
90 case PithosNodeStateRefreshFinished:
93 children = newChildren;
96 freshness = PithosNodeStateFresh;
104 - (NSString *)displayName {
105 if (displayName == nil)
106 return [NSString stringWithString:@"sharing accounts"];
107 return [[displayName copy] autorelease];
111 #pragma mark ASIHTTPRequestDelegate
113 - (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request {
114 NSLog(@"URL: %@", [sharingAccountsRequest url]);
115 NSLog(@"cached: %d", [sharingAccountsRequest didUseCachedResponse]);
117 NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts];
118 if (sharingAccounts == nil) {
119 sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts];
121 [sharingAccounts addObjectsFromArray:someSharingAccounts];
123 if ([someSharingAccounts count] < 10000) {
124 if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) {
126 NSLog(@"using newChildren");
127 newChildren = [[NSMutableArray alloc] init];
128 NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
129 for (ASIPithosAccount *account in sharingAccounts) {
130 if (![account.name isEqualToString:[ASIPithosRequest authUser]]) {
131 PithosAccountNode *node = [[[PithosAccountNode alloc] init] autorelease];
133 node.shared = shared;
134 node.sharingAccount = account.name;
136 NSUInteger oldIndex = [children indexOfObject:node];
137 if (oldIndex != NSNotFound) {
138 // Use the same pointer value, if possible
139 node = [children objectAtIndex:oldIndex];
140 [keptNodes addIndex:oldIndex];
143 [newChildren addObject:node];
146 [[children objectsAtIndexes:
147 [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
148 if ([keptNodes containsIndex:idx])
151 }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
153 // Else cache was used and all results were fetched during this request, so existing children can be reused
154 [sharingAccountsRequest release];
155 sharingAccountsRequest = nil;
156 [sharingAccounts release];
157 sharingAccounts = nil;
158 @synchronized(self) {
159 freshness = PithosNodeStateRefreshFinished;
161 // Notify observers that children are updated
162 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosSharingAccountsNodeChildrenUpdated" object:self];
164 [sharingAccountsRequest release];
165 // Do an additional request to fetch more objects
166 sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0
167 marker:[[someSharingAccounts lastObject] name]] retain];
168 sharingAccountsRequest.delegate = self;
169 sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
170 [sharingAccountsRequest startAsynchronous];
174 - (void)accountRequestFailed:(ASIPithosRequest *)request {
175 [PithosFileUtilities httpRequestErrorAlertWithRequest:request];
176 [newChildren release];
178 [sharingAccountsRequest release];
179 sharingAccountsRequest = nil;
180 [sharingAccounts release];
181 sharingAccounts = nil;
182 @synchronized(self) {
183 freshness = PithosNodeStateRefreshNeeded;