root / pithos-macos / PithosSharingAccountsNode.m @ 9990166e
History | View | Annotate | Download (11.9 kB)
1 |
// |
---|---|
2 |
// PithosAccountNode.m |
3 |
// pithos-macos |
4 |
// |
5 |
// Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
// |
7 |
// Redistribution and use in source and binary forms, with or |
8 |
// without modification, are permitted provided that the following |
9 |
// conditions are met: |
10 |
// |
11 |
// 1. Redistributions of source code must retain the above |
12 |
// copyright notice, this list of conditions and the following |
13 |
// disclaimer. |
14 |
// |
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. |
19 |
// |
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. |
32 |
// |
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. |
37 |
|
38 |
#import "PithosSharingAccountsNode.h" |
39 |
#import "PithosAccountNode.h" |
40 |
#import "ASIPithosRequest.h" |
41 |
#import "ASIPithos.h" |
42 |
#import "ASIPithosAccount.h" |
43 |
#import "ASIDownloadCache.h" |
44 |
#import "PithosUtilities.h" |
45 |
#import "PithosActivityFacility.h" |
46 |
|
47 |
@implementation PithosSharingAccountsNode |
48 |
@synthesize pithos; |
49 |
|
50 |
#pragma mark - |
51 |
#pragma mark Object Lifecycle |
52 |
|
53 |
- (id)initWithPithos:(ASIPithos *)aPithos { |
54 |
if ((self = [super init])) { |
55 |
pithos = [aPithos retain]; |
56 |
self.sharingAccount = @""; |
57 |
} |
58 |
return self; |
59 |
} |
60 |
|
61 |
- (void)dealloc { |
62 |
[sharingAccountsRequest clearDelegatesAndCancel]; |
63 |
[sharingAccountsRequest release]; |
64 |
[sharingAccounts release]; |
65 |
[pithos release]; |
66 |
[super dealloc]; |
67 |
} |
68 |
|
69 |
#pragma mark - |
70 |
#pragma mark Properties |
71 |
|
72 |
- (void)setPithos:(ASIPithos *)aPithos { |
73 |
if (aPithos && ![aPithos isEqualTo:pithos]) { |
74 |
[pithos release]; |
75 |
pithos = [aPithos retain]; |
76 |
[url release]; |
77 |
url = nil; |
78 |
[sharingAccountsRequest clearDelegatesAndCancel]; |
79 |
[sharingAccountsRequest release]; |
80 |
sharingAccountsRequest = nil; |
81 |
reset = YES; |
82 |
} |
83 |
} |
84 |
|
85 |
- (NSString *)url { |
86 |
if (url == nil) |
87 |
url = [pithos.storageURLPrefix copy]; |
88 |
return url; |
89 |
} |
90 |
|
91 |
- (NSArray *)children { |
92 |
@synchronized(self) { |
93 |
if (reset) { |
94 |
[sharingAccountsRequest clearDelegatesAndCancel]; |
95 |
[sharingAccountsRequest release]; |
96 |
sharingAccountsRequest = nil; |
97 |
[children release]; |
98 |
children = nil; |
99 |
[newChildren release]; |
100 |
newChildren = nil; |
101 |
freshness = PithosNodeStateRefreshNeeded; |
102 |
forcedRefresh = YES; |
103 |
reset = NO; |
104 |
[self postChildrenUpdatedNotificationName]; |
105 |
} |
106 |
switch (freshness) { |
107 |
case PithosNodeStateFresh: |
108 |
break; |
109 |
case PithosNodeStateRefreshNeeded: |
110 |
freshness = PithosNodeStateRefreshing; |
111 |
sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithPithos:pithos limit:0 marker:nil] retain]; |
112 |
sharingAccountsRequest.delegate = self; |
113 |
sharingAccountsRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
114 |
sharingAccountsRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
115 |
sharingAccountsRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
116 |
[NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", |
117 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
118 |
NSStringFromSelector(@selector(sharingAccountsRequestFinished:)), @"didFinishSelector", |
119 |
NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", |
120 |
nil]; |
121 |
// if (!forcedRefresh) |
122 |
// sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache]; |
123 |
[[PithosUtilities prepareRequest:sharingAccountsRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous]; |
124 |
break; |
125 |
case PithosNodeStateRefreshing: |
126 |
break; |
127 |
case PithosNodeStateRefreshFinished: |
128 |
if (newChildren) { |
129 |
[children release]; |
130 |
children = newChildren; |
131 |
newChildren = nil; |
132 |
} |
133 |
freshness = PithosNodeStateFresh; |
134 |
default: |
135 |
break; |
136 |
} |
137 |
return children; |
138 |
} |
139 |
} |
140 |
|
141 |
- (NSString *)displayName { |
142 |
if (displayName == nil) |
143 |
return [NSString stringWithString:@"sharing accounts"]; |
144 |
return [[displayName copy] autorelease]; |
145 |
} |
146 |
|
147 |
#pragma mark - |
148 |
#pragma mark ASIHTTPRequestDelegate |
149 |
|
150 |
- (void)sharingAccountsRequestFailed:(ASIPithosRequest *)request { |
151 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
152 |
NSString *message; |
153 |
NSError *error = [sharingAccountsRequest error]; |
154 |
if (error) |
155 |
message = [NSString stringWithFormat:@"Sharing accounts listing %@ failed: %@", |
156 |
sharingAccountsRequest.url, [error localizedDescription]]; |
157 |
else |
158 |
message = [NSString stringWithFormat:@"Sharing accounts listing %@ failed: (%d) %@", |
159 |
sharingAccountsRequest.url, sharingAccountsRequest.responseStatusCode, sharingAccountsRequest.responseStatusMessage]; |
160 |
dispatch_async(dispatch_get_main_queue(), ^{ |
161 |
[[PithosActivityFacility defaultPithosActivityFacility] startAndEndActivityWithType:PithosActivityOther message:message]; |
162 |
}); |
163 |
NSUInteger retries = [[sharingAccountsRequest.userInfo objectForKey:@"retries"] unsignedIntegerValue]; |
164 |
if (retries > 0) { |
165 |
ASIPithosRequest *newSharingAccountsRequest = (ASIPithosRequest *)[PithosUtilities copyRequest:sharingAccountsRequest]; |
166 |
[(NSMutableDictionary *)(newSharingAccountsRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"]; |
167 |
[sharingAccountsRequest release]; |
168 |
sharingAccountsRequest = newSharingAccountsRequest; |
169 |
[[PithosUtilities prepareRequest:sharingAccountsRequest priority:[[sharingAccountsRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous]; |
170 |
} else { |
171 |
[newChildren release]; |
172 |
newChildren = nil; |
173 |
[sharingAccountsRequest release]; |
174 |
sharingAccountsRequest = nil; |
175 |
[sharingAccounts release]; |
176 |
sharingAccounts = nil; |
177 |
forcedRefresh = NO; |
178 |
@synchronized(self) { |
179 |
freshness = PithosNodeStateRefreshNeeded; |
180 |
} |
181 |
} |
182 |
[pool drain]; |
183 |
} |
184 |
|
185 |
- (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request { |
186 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
187 |
DLog(@"List sharing accounts finished: %@", [sharingAccountsRequest url]); |
188 |
DLog(@"Cached: %d", [sharingAccountsRequest didUseCachedResponse]); |
189 |
if (sharingAccountsRequest.responseStatusCode == 200) { |
190 |
NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts]; |
191 |
if (sharingAccounts == nil) { |
192 |
sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts]; |
193 |
} else { |
194 |
[sharingAccounts addObjectsFromArray:someSharingAccounts]; |
195 |
} |
196 |
if ([someSharingAccounts count] < 10000) { |
197 |
if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) { |
198 |
// Save new children |
199 |
DLog(@"using newChildren"); |
200 |
newChildren = [[NSMutableArray alloc] init]; |
201 |
NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet]; |
202 |
for (ASIPithosAccount *account in sharingAccounts) { |
203 |
if (![account.name isEqualToString:pithos.authUser]) { |
204 |
PithosAccountNode *node = [[[PithosAccountNode alloc] initWithPithos:pithos] autorelease]; |
205 |
node.parent = self; |
206 |
node.shared = shared; |
207 |
node.sharingAccount = account.name; |
208 |
node.inheritChildrenUpdatedNotificationName = inheritChildrenUpdatedNotificationName; |
209 |
if (children) { |
210 |
NSUInteger oldIndex = [children indexOfObject:node]; |
211 |
if (oldIndex != NSNotFound) { |
212 |
// Use the same pointer value, if possible |
213 |
node = [children objectAtIndex:oldIndex]; |
214 |
[keptNodes addIndex:oldIndex]; |
215 |
} |
216 |
} |
217 |
[newChildren addObject:node]; |
218 |
} |
219 |
} |
220 |
[[children objectsAtIndexes: |
221 |
[[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){ |
222 |
if ([keptNodes containsIndex:idx]) |
223 |
return NO; |
224 |
return YES; |
225 |
}]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)]; |
226 |
} |
227 |
// Else cache was used and all results were fetched during this request, so existing children can be reused |
228 |
[sharingAccountsRequest release]; |
229 |
sharingAccountsRequest = nil; |
230 |
[sharingAccounts release]; |
231 |
sharingAccounts = nil; |
232 |
forcedRefresh = NO; |
233 |
@synchronized(self) { |
234 |
freshness = PithosNodeStateRefreshFinished; |
235 |
} |
236 |
[self postChildrenUpdatedNotificationName]; |
237 |
} else { |
238 |
[sharingAccountsRequest release]; |
239 |
// Do an additional request to fetch more objects |
240 |
sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithPithos:pithos |
241 |
limit:0 |
242 |
marker:[[someSharingAccounts lastObject] name]] retain]; |
243 |
sharingAccountsRequest.delegate = self; |
244 |
sharingAccountsRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
245 |
sharingAccountsRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
246 |
sharingAccountsRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
247 |
[NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", |
248 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
249 |
NSStringFromSelector(@selector(sharingAccountsRequestFinished:)), @"didFinishSelector", |
250 |
NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", |
251 |
nil]; |
252 |
// if (!forcedRefresh) |
253 |
// sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache]; |
254 |
[[PithosUtilities prepareRequest:sharingAccountsRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous]; |
255 |
} |
256 |
} else { |
257 |
[self sharingAccountsRequestFailed:sharingAccountsRequest]; |
258 |
} |
259 |
[pool drain]; |
260 |
} |
261 |
|
262 |
@end |