Statistics
| Branch: | Tag: | Revision:

root / pithos-macos / PithosSharingAccountsNode.m @ f059e0e6

History | View | Annotate | Download (9.8 kB)

1
//
2
//  PithosAccountNode.m
3
//  pithos-macos
4
//
5
// Copyright 2011 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 "ASIPithosAccount.h"
42
#import "ASIDownloadCache.h"
43
#import "PithosUtilities.h"
44
#import "PithosActivityFacility.h"
45

    
46
@implementation PithosSharingAccountsNode
47

    
48
#pragma mark -
49
#pragma mark Object Lifecycle
50

    
51
- (id)init {
52
    if ((self = [super init])) {
53
        self.sharingAccount = @"";
54
    }
55
    return self;
56
}
57

    
58
- (void)dealloc {
59
    [sharingAccountsRequest clearDelegatesAndCancel];
60
    [sharingAccountsRequest release];
61
    [sharingAccounts release];
62
    [super dealloc];
63
}
64

    
65
#pragma mark -
66
#pragma mark Properties
67

    
68
- (NSString *)url {
69
    if (url == nil) 
70
        url = [[ASIPithosRequest storageURLPrefix] copy];
71
    return url;
72
}
73

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

    
109
- (NSString *)displayName {
110
    if (displayName == nil)
111
        return [NSString stringWithString:@"sharing accounts"];
112
    return [[displayName copy] autorelease];
113
}
114

    
115
#pragma mark -
116
#pragma mark ASIHTTPRequestDelegate
117

    
118
- (void)sharingAccountsRequestFailed:(ASIPithosRequest *)request {
119
    NSUInteger retries = [[sharingAccountsRequest.userInfo objectForKey:@"retries"] unsignedIntegerValue];
120
    if (retries > 0) {
121
        ASIPithosRequest *newSharingAccountsRequest = (ASIPithosRequest *)[PithosUtilities copyRequest:sharingAccountsRequest];
122
        [(NSMutableDictionary *)(newSharingAccountsRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
123
        [sharingAccountsRequest release];
124
        sharingAccountsRequest = newSharingAccountsRequest;
125
        [[PithosUtilities prepareRequest:sharingAccountsRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous];
126
    } else {
127
        NSString *message;
128
        NSError *error = [sharingAccountsRequest error];
129
        if (error)
130
            message = [NSString stringWithFormat:@"Sharing accounts listing failed: %@", error];
131
        else
132
            message = [NSString stringWithFormat:@"Sharing accounts listing failed: (%d) %@", 
133
                       sharingAccountsRequest.responseStatusCode, sharingAccountsRequest.responseStatusMessage];
134
        [[PithosActivityFacility defaultPithosActivityFacility] startAndEndActivityWithType:PithosActivityOther message:message];
135
        [newChildren release];
136
        newChildren = nil;
137
        [sharingAccountsRequest release];
138
        sharingAccountsRequest = nil;
139
        [sharingAccounts release];
140
        sharingAccounts = nil;
141
        forcedRefresh = NO;
142
        @synchronized(self) {
143
            freshness = PithosNodeStateRefreshNeeded;
144
        }
145
    }
146
}
147

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

    
219
@end