Statistics
| Branch: | Tag: | Revision:

root / pithos-macos / PithosAccountNode.m @ d8426ffb

History | View | Annotate | Download (18.7 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 "PithosAccountNode.h"
39
#import "PithosContainerNode.h"
40
#import "ASIPithos.h"
41
#import "ASIPithosAccountRequest.h"
42
#import "ASIPithosAccount.h"
43
#import "ASIPithosContainer.h"
44
#import "ASIDownloadCache.h"
45
#import "PithosUtilities.h"
46
#import "PithosActivityFacility.h"
47

    
48
static NSImage *sharedIcon = nil;
49

    
50
@implementation PithosAccountNode
51
@synthesize pithos, pithosAccount;
52

    
53
+ (void)initialize {
54
	if (self == [PithosAccountNode class])
55
        sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kUserIcon)] retain];
56
}
57

    
58
#pragma mark -
59
#pragma mark Object Lifecycle
60

    
61
- (id)initWithPithos:(ASIPithos *)aPithos {
62
    if ((self = [super init])) {
63
        self.pithos = aPithos;
64
    }
65
    return self;
66
}
67

    
68
- (void)dealloc {
69
    [accountRequest clearDelegatesAndCancel];
70
    [accountRequest release];
71
    [refreshMetadataAccountRequest clearDelegatesAndCancel];
72
    [refreshMetadataAccountRequest release];
73
    [applyMetadataAccountRequest clearDelegatesAndCancel];
74
    [applyMetadataAccountRequest release];
75
    [containers release];
76
    [pithosAccount release];
77
    [pithos release];
78
    [super dealloc];
79
}
80

    
81
#pragma mark -
82
#pragma mark Properties
83

    
84
- (void)setPithos:(ASIPithos *)aPithos {
85
    if (aPithos && ![aPithos isEqualTo:pithos]) {
86
        [pithos release];
87
        pithos = [aPithos retain];
88
        [url release];
89
        url = nil;
90
    }
91
}
92

    
93
- (NSString *)url {
94
    if (url == nil)
95
        url = [[NSString alloc] initWithFormat:@"%@%@", 
96
               (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL), 
97
               (shared ? @"?shared" : @"")];
98
    return url;
99
}
100

    
101
- (NSArray *)children {
102
    @synchronized(self) {
103
        switch (freshness) {
104
            case PithosNodeStateFresh:
105
                break;
106
            case PithosNodeStateRefreshNeeded:
107
                freshness = PithosNodeStateRefreshing;
108
                accountRequest = [[ASIPithosAccountRequest listContainersRequestWithPithos:pithos 
109
                                                                                     limit:0 
110
                                                                                    marker:nil 
111
                                                                                    shared:shared 
112
                                                                                     until:nil] retain];
113
                if (sharingAccount)
114
                    [accountRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
115
                accountRequest.delegate = self;
116
                accountRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
117
                accountRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
118
                accountRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
119
                                           [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", 
120
                                           [NSNumber numberWithUnsignedInteger:10], @"retries", 
121
                                           NSStringFromSelector(@selector(accountRequestFinished:)), @"didFinishSelector", 
122
                                           NSStringFromSelector(@selector(accountRequestFailed:)), @"didFailSelector", 
123
                                           nil];
124
                if (!forcedRefresh)
125
                    accountRequest.downloadCache = [ASIDownloadCache sharedCache];
126
                [[PithosUtilities prepareRequest:accountRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous];
127
                break;
128
            case PithosNodeStateRefreshing:
129
                break;
130
            case PithosNodeStateRefreshFinished:
131
                if (newChildren) {
132
                    [children release];
133
                    children = newChildren;
134
                    newChildren = nil;
135
                }
136
                freshness = PithosNodeStateFresh;
137
            default:
138
                break;
139
        }
140
        return children;
141
    }
142
}
143

    
144
- (NSString *)displayName {
145
    if (displayName == nil)
146
        return [NSString stringWithString:(sharingAccount ? sharingAccount : @"account")];
147
    return [[displayName copy] autorelease];
148
}
149

    
150
- (NSImage *)icon {
151
    if (icon == nil)
152
        icon = [sharedIcon retain];
153
    return icon;
154
}
155

    
156
#pragma mark -
157
#pragma mark ASIHTTPRequestDelegate
158

    
159
- (void)accountRequestFailed:(ASIPithosAccountRequest *)request {
160
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
161
    NSUInteger retries = [[accountRequest.userInfo objectForKey:@"retries"] unsignedIntegerValue];
162
    if (retries > 0) {
163
        ASIPithosAccountRequest *newAccountRequest = (ASIPithosAccountRequest *)[PithosUtilities copyRequest:accountRequest];
164
        [(NSMutableDictionary *)(newAccountRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
165
        [accountRequest release];
166
        accountRequest = newAccountRequest;
167
        [[PithosUtilities prepareRequest:accountRequest priority:[[accountRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
168
    } else {
169
        NSString *message;
170
        NSError *error = [accountRequest error];
171
        if (error)
172
            message = [NSString stringWithFormat:@"Account listing failed: %@", error];
173
        else
174
            message = [NSString stringWithFormat:@"Account listing failed: (%d) %@", 
175
                       accountRequest.responseStatusCode, accountRequest.responseStatusMessage];
176
        dispatch_async(dispatch_get_main_queue(), ^{
177
            [[PithosActivityFacility defaultPithosActivityFacility] startAndEndActivityWithType:PithosActivityOther message:message];
178
        });
179
        [newChildren release];
180
        newChildren = nil;
181
        [accountRequest release];
182
        accountRequest = nil;
183
        [containers release];
184
        containers = nil;
185
        forcedRefresh = NO;
186
        @synchronized(self) {
187
            freshness = PithosNodeStateRefreshNeeded;
188
        }
189
    }
190
    [pool drain];
191
}
192

    
193
- (void)accountRequestFinished:(ASIPithosAccountRequest *)request {
194
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
195
    NSLog(@"List account finished: %@", [accountRequest url]);
196
    NSLog(@"Cached: %d", [accountRequest didUseCachedResponse]);
197
    if (accountRequest.responseStatusCode == 200) {
198
        self.pithosAccount = [accountRequest account];
199
        
200
        NSArray *someContainers = [accountRequest containers];
201
        if (containers == nil) {
202
            containers = [[NSMutableArray alloc] initWithArray:someContainers];
203
        } else {
204
            [containers addObjectsFromArray:someContainers];
205
        }
206
        if ([someContainers count] < 10000) {
207
            if (!accountRequest.didUseCachedResponse || ([containers count] != [someContainers count]) || !children) {
208
                // Save new children
209
                NSLog(@"using newChildren");
210
                newChildren = [[NSMutableArray alloc] init];
211
                NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
212
                for (ASIPithosContainer *container in containers) {
213
                    PithosContainerNode *node = [[[PithosContainerNode alloc] initWithPithos:pithos pithosContainer:container] autorelease];
214
                    node.parent = self;
215
                    node.shared = shared;
216
                    node.sharingAccount = sharingAccount;
217
                    if (children) {
218
                        NSUInteger oldIndex = [children indexOfObject:node];
219
                        if (oldIndex != NSNotFound) {
220
                            // Use the same pointer value, if possible
221
                            node = [children objectAtIndex:oldIndex];
222
                            node.pithosContainer = container;
223
                            [keptNodes addIndex:oldIndex];
224
                        }
225
                    }
226
                    [newChildren addObject:node];
227
                }
228
                [[children objectsAtIndexes:
229
                  [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
230
                    if ([keptNodes containsIndex:idx])
231
                        return NO;
232
                    return YES;
233
                }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
234
            }
235
            // Else cache was used and all results were fetched during this request, so existing children can be reused
236
            [accountRequest release];
237
            accountRequest = nil;
238
            [containers release];
239
            containers = nil;
240
            forcedRefresh = NO;
241
            @synchronized(self) {
242
                freshness = PithosNodeStateRefreshFinished;
243
            }
244
            // Notify observers that children are updated
245
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosAccountNodeChildrenUpdated" object:self];
246
        } else {
247
            [accountRequest release];
248
            // Do an additional request to fetch more objects
249
            accountRequest = [[ASIPithosAccountRequest listContainersRequestWithPithos:pithos 
250
                                                                                 limit:0 
251
                                                                                marker:[[someContainers lastObject] name] 
252
                                                                                shared:shared 
253
                                                                                 until:nil] retain];
254
            if (sharingAccount)
255
                [accountRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
256
            accountRequest.delegate = self;
257
            accountRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
258
            accountRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
259
            accountRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
260
                                       [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", 
261
                                       [NSNumber numberWithUnsignedInteger:10], @"retries", 
262
                                       NSStringFromSelector(@selector(accountRequestFinished:)), @"didFinishSelector", 
263
                                       NSStringFromSelector(@selector(accountRequestFailed:)), @"didFailSelector", 
264
                                       nil];
265
            if (!forcedRefresh)
266
                accountRequest.downloadCache = [ASIDownloadCache sharedCache];
267
            [[PithosUtilities prepareRequest:accountRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous];
268
        }
269
    } else if (accountRequest.responseStatusCode == 304) {
270
        // Account is not modified, so existing children can be reused
271
        [accountRequest release];
272
        accountRequest = nil;
273
        [containers release];
274
        containers = nil;
275
        forcedRefresh = NO;
276
        @synchronized(self) {
277
            freshness = PithosNodeStateRefreshFinished;
278
        }
279
        // Notify observers that children are updated
280
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosAccountNodeChildrenUpdated" object:self];
281
    } else {
282
        [self accountRequestFailed:accountRequest];
283
    }
284
    [pool drain];
285
}
286

    
287
- (void)accountMetadataRequestFinished:(ASIPithosAccountRequest *)request {
288
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
289
    NSLog(@"URL: %@", [request url]);
290
    NSLog(@"cached: %d", [request didUseCachedResponse]);
291
    
292
    if ([request isEqualTo:applyMetadataAccountRequest]) {
293
        @synchronized(self) {
294
            [applyMetadataAccountRequest release];
295
            applyMetadataAccountRequest = nil;
296
        }
297
        [self refreshInfo];
298
    } else if ([request isEqualTo:refreshMetadataAccountRequest]) {
299
        self.pithosAccount = [refreshMetadataAccountRequest account];
300
        @synchronized(self) {
301
            [refreshMetadataAccountRequest release];
302
            refreshMetadataAccountRequest = nil;
303
        }
304
    }
305
    [pool drain];
306
}
307

    
308
- (void)accountMetadataRequestFailed:(ASIPithosAccountRequest *)request {
309
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
310
    NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
311
    if (retries > 0) {
312
        ASIPithosAccountRequest *newRequest = (ASIPithosAccountRequest *)[PithosUtilities copyRequest:request];
313
        [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
314
        if ([request isEqualTo:applyMetadataAccountRequest]) {
315
            @synchronized(self) {
316
                [applyMetadataAccountRequest release];
317
                applyMetadataAccountRequest = newRequest;
318
            }
319
        } else if ([request isEqualTo:refreshMetadataAccountRequest]) {
320
            @synchronized(self) {
321
                [refreshMetadataAccountRequest release];
322
                refreshMetadataAccountRequest = newRequest;
323
            }
324
        }
325
        [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
326
    } else {
327
        if ([request isEqualTo:applyMetadataAccountRequest]) {
328
            dispatch_async(dispatch_get_main_queue(), ^{
329
                [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataAccountRequest];
330
            });
331
            @synchronized(self) {
332
                [applyMetadataAccountRequest release];
333
                applyMetadataAccountRequest = nil;
334
            }
335
        } else if ([request isEqualTo:refreshMetadataAccountRequest]) {
336
            dispatch_async(dispatch_get_main_queue(), ^{
337
                [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataAccountRequest];
338
            });
339
            @synchronized(self) {
340
                [refreshMetadataAccountRequest release];
341
                refreshMetadataAccountRequest = nil;
342
            }
343
        }
344
    }
345
    [pool drain];
346
}
347

    
348
#pragma mark -
349
#pragma mark Info
350

    
351
- (void)applyInfo {
352
    @synchronized(self) {
353
        if (applyMetadataAccountRequest == nil) {
354
            NSMutableDictionary *groups = pithosAccount.groups;
355
            if ([groups count] == 0)
356
                groups = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"group"];
357
            applyMetadataAccountRequest = [[ASIPithosAccountRequest updateAccountMetadataRequestWithPithos:pithos 
358
                                                                                                    groups:groups 
359
                                                                                                  metadata:pithosAccount.metadata 
360
                                                                                                    update:NO] retain];
361
            applyMetadataAccountRequest.delegate = self;
362
            applyMetadataAccountRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
363
            applyMetadataAccountRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
364
            applyMetadataAccountRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
365
                                                    [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
366
                                                    [NSNumber numberWithUnsignedInteger:10], @"retries", 
367
                                                    NSStringFromSelector(@selector(accountMetadataRequestFinished:)), @"didFinishSelector", 
368
                                                    NSStringFromSelector(@selector(accountMetadataRequestFailed:)), @"didFailSelector", 
369
                                                    nil];
370
            [[PithosUtilities prepareRequest:applyMetadataAccountRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
371
        }
372
    }
373
}
374

    
375
- (void)refreshInfo {
376
    @synchronized(self) {
377
        if (refreshMetadataAccountRequest == nil) {
378
            refreshMetadataAccountRequest = [[ASIPithosAccountRequest accountMetadataRequestWithPithos:pithos] retain];
379
            refreshMetadataAccountRequest.delegate = self;
380
            refreshMetadataAccountRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
381
            refreshMetadataAccountRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
382
            refreshMetadataAccountRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
383
                                                      [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
384
                                                      [NSNumber numberWithUnsignedInteger:10], @"retries", 
385
                                                      NSStringFromSelector(@selector(accountMetadataRequestFinished:)), @"didFinishSelector", 
386
                                                      NSStringFromSelector(@selector(accountMetadataRequestFailed:)), @"didFailSelector", 
387
                                                      nil];
388
            refreshMetadataAccountRequest.downloadCache = [ASIDownloadCache sharedCache];
389
            [[PithosUtilities prepareRequest:refreshMetadataAccountRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
390
        }
391
    }
392
}
393

    
394
@end