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