Revision b58ff732 pithos-macos/PithosSharingAccountsNode.m

b/pithos-macos/PithosSharingAccountsNode.m
140 140

  
141 141
- (NSString *)displayName {
142 142
    if (displayName == nil)
143
        return [NSString stringWithString:@"sharing accounts"];
143
        return @"sharing accounts";
144 144
    return [[displayName copy] autorelease];
145 145
}
146 146

  
......
148 148
#pragma mark ASIHTTPRequestDelegate
149 149

  
150 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;
151
    @autoreleasepool {
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
            }
180 181
        }
181 182
    }
182
    [pool drain];
183 183
}
184 184

  
185 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];
186
    @autoreleasepool {
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
                                }
215 216
                            }
217
                            [newChildren addObject:node];
216 218
                        }
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)];
219 226
                }
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;
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];
235 255
            }
236
            [self postChildrenUpdatedNotificationName];
237 256
        } 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];
257
            [self sharingAccountsRequestFailed:sharingAccountsRequest];
255 258
        }
256
    } else {
257
        [self sharingAccountsRequestFailed:sharingAccountsRequest];
258 259
    }
259
    [pool drain];
260 260
}
261 261

  
262 262
@end

Also available in: Unified diff