Shift-refresh forces no use of cache.
[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                 if (!forcedRefresh)
86                     sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
87                 [sharingAccountsRequest startAsynchronous];
88                 break;
89             case PithosNodeStateRefreshing:
90                 break;
91             case PithosNodeStateRefreshFinished:
92                 if (newChildren) {
93                     [children release];
94                     children = newChildren;
95                     newChildren = nil;
96                 }
97                 freshness = PithosNodeStateFresh;
98             default:
99                 break;
100         }
101         return children;
102     }
103 }
104
105 - (NSString *)displayName {
106     if (displayName == nil)
107         return [NSString stringWithString:@"sharing accounts"];
108     return [[displayName copy] autorelease];
109 }
110
111 #pragma mark -
112 #pragma mark ASIHTTPRequestDelegate
113
114 - (void)sharingAccountsRequestFinished:(ASIPithosRequest *)request {
115     NSLog(@"URL: %@", [sharingAccountsRequest url]);
116     NSLog(@"cached: %d", [sharingAccountsRequest didUseCachedResponse]);
117     
118     NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts];
119     if (sharingAccounts == nil) {
120         sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts];
121     } else {
122         [sharingAccounts addObjectsFromArray:someSharingAccounts];
123     }
124     if ([someSharingAccounts count] < 10000) {
125         if (!sharingAccountsRequest.didUseCachedResponse || ([sharingAccounts count] != [someSharingAccounts count]) || !children) {
126             // Save new children
127             NSLog(@"using newChildren");
128             newChildren = [[NSMutableArray alloc] init];
129             NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet];
130             for (ASIPithosAccount *account in sharingAccounts) {
131                 if (![account.name isEqualToString:[ASIPithosRequest authUser]]) {
132                     PithosAccountNode *node = [[[PithosAccountNode alloc] init] autorelease];
133                     node.parent = self;
134                     node.shared = shared;
135                     node.sharingAccount = account.name;
136                     if (children) {
137                         NSUInteger oldIndex = [children indexOfObject:node];
138                         if (oldIndex != NSNotFound) {
139                             // Use the same pointer value, if possible
140                             node = [children objectAtIndex:oldIndex];
141                             [keptNodes addIndex:oldIndex];
142                         }
143                     }
144                     [newChildren addObject:node];
145                 }
146             }
147             [[children objectsAtIndexes:
148               [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){
149                 if ([keptNodes containsIndex:idx])
150                     return NO;
151                 return YES;
152             }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)];
153         }
154         // Else cache was used and all results were fetched during this request, so existing children can be reused
155         [sharingAccountsRequest release];
156         sharingAccountsRequest = nil;
157         [sharingAccounts release];
158         sharingAccounts = nil;
159         forcedRefresh = NO;
160         @synchronized(self) {
161             freshness = PithosNodeStateRefreshFinished;
162         }
163         // Notify observers that children are updated
164         [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosSharingAccountsNodeChildrenUpdated" object:self];
165     } else {
166         [sharingAccountsRequest release];
167         // Do an additional request to fetch more objects
168         sharingAccountsRequest = [[ASIPithosRequest listSharingAccountsRequestWithLimit:0 
169                                                                                  marker:[[someSharingAccounts lastObject] name]] retain];
170         sharingAccountsRequest.delegate = self;
171         if (!forcedRefresh)
172             sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache];
173         [sharingAccountsRequest startAsynchronous];
174     }
175 }
176
177 - (void)accountRequestFailed:(ASIPithosRequest *)request {
178     [PithosFileUtilities httpRequestErrorAlertWithRequest:request];
179     [newChildren release];
180     newChildren = nil;
181     [sharingAccountsRequest release];
182     sharingAccountsRequest = nil;
183     [sharingAccounts release];
184     sharingAccounts = nil;
185     forcedRefresh = NO;
186     @synchronized(self) {
187         freshness = PithosNodeStateRefreshNeeded;
188     }
189 }
190
191 @end