Statistics
| Branch: | Tag: | Revision:

root / pithos-macos / PithosSharingAccountsNode.m @ 919cb043

History | View | Annotate | Download (11 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
        self.pithos = aPithos;
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
- (NSString *)url {
73
    if (url == nil) 
74
        url = [pithos.storageURLPrefix copy];
75
    return url;
76
}
77

    
78
- (NSArray *)children {
79
    @synchronized(self) {
80
        switch (freshness) {
81
            case PithosNodeStateFresh:
82
                break;
83
            case PithosNodeStateRefreshNeeded:
84
                freshness = PithosNodeStateRefreshing;
85
                sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithPithos:pithos limit:0 marker:nil] retain];
86
                sharingAccountsRequest.delegate = self;
87
                sharingAccountsRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
88
                sharingAccountsRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
89
                sharingAccountsRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
90
                                                   [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", 
91
                                                   [NSNumber numberWithUnsignedInteger:10], @"retries", 
92
                                                   NSStringFromSelector(@selector(sharingAccountsRequestFinished:)), @"didFinishSelector", 
93
                                                   NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", 
94
                                                   nil];
95
                if (!forcedRefresh)
96
                    sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
97
                [[PithosUtilities prepareRequest:sharingAccountsRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous];
98
                break;
99
            case PithosNodeStateRefreshing:
100
                break;
101
            case PithosNodeStateRefreshFinished:
102
                if (newChildren) {
103
                    [children release];
104
                    children = newChildren;
105
                    newChildren = nil;
106
                }
107
                freshness = PithosNodeStateFresh;
108
            default:
109
                break;
110
        }
111
        return children;
112
    }
113
}
114

    
115
- (NSString *)displayName {
116
    if (displayName == nil)
117
        return [NSString stringWithString:@"sharing accounts"];
118
    return [[displayName copy] autorelease];
119
}
120

    
121
#pragma mark -
122
#pragma mark ASIHTTPRequestDelegate
123

    
124
- (void)sharingAccountsRequestFailed:(ASIPithosRequest *)request {
125
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
126
    NSUInteger retries = [[sharingAccountsRequest.userInfo objectForKey:@"retries"] unsignedIntegerValue];
127
    if (retries > 0) {
128
        ASIPithosRequest *newSharingAccountsRequest = (ASIPithosRequest *)[PithosUtilities copyRequest:sharingAccountsRequest];
129
        [(NSMutableDictionary *)(newSharingAccountsRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
130
        [sharingAccountsRequest release];
131
        sharingAccountsRequest = newSharingAccountsRequest;
132
        [[PithosUtilities prepareRequest:sharingAccountsRequest priority:[[sharingAccountsRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
133
    } else {
134
        NSString *message;
135
        NSError *error = [sharingAccountsRequest error];
136
        if (error)
137
            message = [NSString stringWithFormat:@"Sharing accounts listing failed: %@", error];
138
        else
139
            message = [NSString stringWithFormat:@"Sharing accounts listing failed: (%d) %@", 
140
                       sharingAccountsRequest.responseStatusCode, sharingAccountsRequest.responseStatusMessage];
141
        dispatch_async(dispatch_get_main_queue(), ^{
142
            [[PithosActivityFacility defaultPithosActivityFacility] startAndEndActivityWithType:PithosActivityOther message:message];
143
        });
144
        [newChildren release];
145
        newChildren = nil;
146
        [sharingAccountsRequest release];
147
        sharingAccountsRequest = nil;
148
        [sharingAccounts release];
149
        sharingAccounts = nil;
150
        forcedRefresh = NO;
151
        @synchronized(self) {
152
            freshness = PithosNodeStateRefreshNeeded;
153
        }
154
    }
155
    [pool drain];
156
}
157

    
158
- (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request {
159
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
160
    NSLog(@"List sharing accounts finished: %@", [sharingAccountsRequest url]);
161
    NSLog(@"Cached: %d", [sharingAccountsRequest didUseCachedResponse]);
162
    if (sharingAccountsRequest.responseStatusCode == 200) {
163
        NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts];
164
        if (sharingAccounts == nil) {
165
            sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts];
166
        } else {
167
            [sharingAccounts addObjectsFromArray:someSharingAccounts];
168
        }
169
        if ([someSharingAccounts count] < 10000) {
170
            if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) {
171
                // Save new children
172
                NSLog(@"using newChildren");
173
                newChildren = [[NSMutableArray alloc] init];
174
                NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
175
                for (ASIPithosAccount *account in sharingAccounts) {
176
                    if (![account.name isEqualToString:pithos.authUser]) {
177
                        PithosAccountNode *node = [[[PithosAccountNode alloc] init] autorelease];
178
                        node.parent = self;
179
                        node.shared = shared;
180
                        node.sharingAccount = account.name;
181
                        if (children) {
182
                            NSUInteger oldIndex = [children indexOfObject:node];
183
                            if (oldIndex != NSNotFound) {
184
                                // Use the same pointer value, if possible
185
                                node = [children objectAtIndex:oldIndex];
186
                                [keptNodes addIndex:oldIndex];
187
                            }
188
                        }
189
                        [newChildren addObject:node];
190
                    }
191
                }
192
                [[children objectsAtIndexes:
193
                  [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
194
                    if ([keptNodes containsIndex:idx])
195
                        return NO;
196
                    return YES;
197
                }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
198
            }
199
            // Else cache was used and all results were fetched during this request, so existing children can be reused
200
            [sharingAccountsRequest release];
201
            sharingAccountsRequest = nil;
202
            [sharingAccounts release];
203
            sharingAccounts = nil;
204
            forcedRefresh = NO;
205
            @synchronized(self) {
206
                freshness = PithosNodeStateRefreshFinished;
207
            }
208
            // Notify observers that children are updated
209
            [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosSharingAccountsNodeChildrenUpdated" object:self];
210
        } else {
211
            [sharingAccountsRequest release];
212
            // Do an additional request to fetch more objects
213
            sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithPithos:pithos 
214
                                                                                       limit:0 
215
                                                                                      marker:[[someSharingAccounts lastObject] name]] retain];
216
            sharingAccountsRequest.delegate = self;
217
            sharingAccountsRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
218
            sharingAccountsRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
219
            sharingAccountsRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
220
                                               [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", 
221
                                               [NSNumber numberWithUnsignedInteger:10], @"retries", 
222
                                               NSStringFromSelector(@selector(sharingAccountsRequestFinished:)), @"didFinishSelector", 
223
                                               NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", 
224
                                               nil];
225
            if (!forcedRefresh)
226
                sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
227
            [[PithosUtilities prepareRequest:sharingAccountsRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous];
228
        }
229
    } else {
230
        [self sharingAccountsRequestFailed:sharingAccountsRequest];
231
    }
232
    [pool drain];
233
}
234

    
235
@end