53394ce4b7e00c2e033a89191f80783ee0ad94f1
[pithos-macos] / pithos-macos / PithosSharingAccountsNode.m
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 "PithosFileUtilities.h"
44
45 @implementation PithosSharingAccountsNode
46
47 #pragma mark -
48 #pragma mark Object Lifecycle
49
50 - (id)init {
51     if ((self == [super init])) {
52         self.sharingAccount = @"";
53     }
54     return self;
55 }
56
57 - (void)dealloc {
58     [sharingAccountsRequest clearDelegatesAndCancel];
59     [sharingAccountsRequest release];
60     [sharingAccounts release];
61     [super dealloc];
62 }
63
64 #pragma mark -
65 #pragma mark Properties
66
67 - (NSString *)url {
68     if (url == nil) 
69         url = [[ASIPithosRequest storageURLPrefix] copy];
70     return url;
71 }
72
73 - (NSArray *)children {
74     @synchronized(self) {
75         switch (freshness) {
76             case PithosNodeStateFresh:
77                 break;
78             case PithosNodeStateRefreshNeeded:
79                 freshness = PithosNodeStateRefreshing;
80                 sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0 
81                                                                                          marker:nil] retain];
82                 sharingAccountsRequest.delegate = self;
83                 sharingAccountsRequest.didFinishSelector = @selector(sharingAccountsRequestFinished:);
84                 sharingAccountsRequest.didFailSelector = @selector(sharingAccountsRequestFailed:);
85                 sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
86                 [sharingAccountsRequest startAsynchronous];
87                 break;
88             case PithosNodeStateRefreshing:
89                 break;
90             case PithosNodeStateRefreshFinished:
91                 if (newChildren) {
92                     [children release];
93                     children = newChildren;
94                     newChildren = nil;
95                 }
96                 freshness = PithosNodeStateFresh;
97             default:
98                 break;
99         }
100         return children;
101     }
102 }
103
104 - (NSString *)displayName {
105     if (displayName == nil)
106         return [NSString stringWithString:@"sharing accounts"];
107     return [[displayName copy] autorelease];
108 }
109
110 #pragma mark -
111 #pragma mark ASIHTTPRequestDelegate
112
113 - (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request {
114     NSLog(@"URL: %@", [sharingAccountsRequest url]);
115     NSLog(@"cached: %d", [sharingAccountsRequest didUseCachedResponse]);
116     
117     NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts];
118     if (sharingAccounts == nil) {
119         sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts];
120     } else {
121         [sharingAccounts addObjectsFromArray:someSharingAccounts];
122     }
123     if ([someSharingAccounts count] < 10000) {
124         if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) {
125             // Save new children
126             NSLog(@"using newChildren");
127             newChildren = [[NSMutableArray alloc] init];
128             NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
129             for (ASIPithosAccount *account in sharingAccounts) {
130                 if (![account.name isEqualToString:[ASIPithosRequest authUser]]) {
131                     PithosAccountNode *node = [[[PithosAccountNode alloc] init] autorelease];
132                     node.parent = self;
133                     node.shared = shared;
134                     node.sharingAccount = account.name;
135                     if (children) {
136                         NSUInteger oldIndex = [children indexOfObject:node];
137                         if (oldIndex != NSNotFound) {
138                             // Use the same pointer value, if possible
139                             node = [children objectAtIndex:oldIndex];
140                             [keptNodes addIndex:oldIndex];
141                         }
142                     }
143                     [newChildren addObject:node];
144                 }
145             }
146             [[children objectsAtIndexes:
147               [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
148                 if ([keptNodes containsIndex:idx])
149                     return NO;
150                 return YES;
151             }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
152         }
153         // Else cache was used and all results were fetched during this request, so existing children can be reused
154         [sharingAccountsRequest release];
155         sharingAccountsRequest = nil;
156         [sharingAccounts release];
157         sharingAccounts = nil;
158         @synchronized(self) {
159             freshness = PithosNodeStateRefreshFinished;
160         }
161         // Notify observers that children are updated
162         [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosSharingAccountsNodeChildrenUpdated" object:self];
163     } else {
164         [sharingAccountsRequest release];
165         // Do an additional request to fetch more objects
166         sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0 
167                                                                                  marker:[[someSharingAccounts lastObject] name]] retain];
168         sharingAccountsRequest.delegate = self;
169         sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
170         [sharingAccountsRequest startAsynchronous];
171     }
172 }
173
174 - (void)accountRequestFailed:(ASIPithosRequest *)request {
175     [PithosFileUtilities httpRequestErrorAlertWithRequest:request];
176     [newChildren release];
177     newChildren = nil;
178     [sharingAccountsRequest release];
179     sharingAccountsRequest = nil;
180     [sharingAccounts release];
181     sharingAccounts = nil;
182     @synchronized(self) {
183         freshness = PithosNodeStateRefreshNeeded;
184     }
185 }
186
187 @end