root / pithos-macos / PithosSubdirNode.m @ cb6abe72
History | View | Annotate | Download (23.8 kB)
1 |
// |
---|---|
2 |
// PithosSubdirNode.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 "PithosSubdirNode.h" |
39 |
#import "ASIPithosRequest.h" |
40 |
#import "ASIPithos.h" |
41 |
#import "ASIPithosObjectRequest.h" |
42 |
#import "ASIPithosContainer.h" |
43 |
#import "ASIPithosObject.h" |
44 |
#import "ASINetworkQueue.h" |
45 |
#import "ASIDownloadCache.h" |
46 |
#import "PithosUtilities.h" |
47 |
#import "PithosObjectNodeInfoController.h" |
48 |
|
49 |
static NSImage *sharedIcon = nil; |
50 |
|
51 |
@implementation PithosSubdirNode |
52 |
@synthesize pithosObject, versions; |
53 |
@synthesize isPublic; |
54 |
|
55 |
+ (void)initialize { |
56 |
if (self == [PithosSubdirNode class]) |
57 |
sharedIcon = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)]; |
58 |
} |
59 |
|
60 |
#pragma mark - |
61 |
#pragma mark Object Lifecycle |
62 |
|
63 |
- (id)initWithPithos:(ASIPithos *)aPithos |
64 |
pithosContainer:(ASIPithosContainer *)aPithosContainer |
65 |
pithosObject:(ASIPithosObject *)aPithosObject { |
66 |
if ((self = [super init])) { |
67 |
self.pithos = aPithos; |
68 |
self.pithosContainer = aPithosContainer; |
69 |
self.pithosObject = aPithosObject; |
70 |
refreshParent = NO; |
71 |
} |
72 |
return self; |
73 |
} |
74 |
|
75 |
- (void)dealloc { |
76 |
[refreshVersionsObjectRequest clearDelegatesAndCancel]; |
77 |
[refreshMetadataObjectRequest clearDelegatesAndCancel]; |
78 |
[applyMetadataObjectRequest clearDelegatesAndCancel]; |
79 |
} |
80 |
|
81 |
#pragma mark - |
82 |
#pragma mark Properties |
83 |
|
84 |
- (void)setPithos:(ASIPithos *)aPithos { |
85 |
if (aPithos && ![aPithos isEqualTo:pithos]) { |
86 |
pithos = aPithos; |
87 |
url = nil; |
88 |
} |
89 |
} |
90 |
|
91 |
- (NSString *)url { |
92 |
if (url == nil) |
93 |
url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@", |
94 |
(sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL), |
95 |
pithosContainer.name, |
96 |
prefix, |
97 |
(shared ? @"?shared" : @"")]; |
98 |
return url; |
99 |
} |
100 |
|
101 |
- (NSString *)displayName { |
102 |
if (displayName == nil) { |
103 |
displayName = [pithosObject.name lastPathComponent]; |
104 |
if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"]) |
105 |
displayName = [displayName stringByAppendingString:@"/"]; |
106 |
} |
107 |
return [displayName copy]; |
108 |
} |
109 |
|
110 |
- (void)setDisplayName:(NSString *)aDisplayName { |
111 |
} |
112 |
|
113 |
- (NSImage *)icon { |
114 |
if (icon == nil) |
115 |
icon = sharedIcon; |
116 |
return icon; |
117 |
} |
118 |
|
119 |
- (void)setPithosObject:(ASIPithosObject *)aPithosObject { |
120 |
if (![pithosObject isEqualTo:aPithosObject]) { |
121 |
pithosObject = aPithosObject; |
122 |
} |
123 |
if (pithosObject.subdir) { |
124 |
self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)]; |
125 |
} else { |
126 |
self.prefix = [NSString stringWithString:pithosObject.name]; |
127 |
} |
128 |
self.isPublic = (pithosObject.publicURI != nil); |
129 |
// Refresh browser if the object is in my shared and is no longer shared |
130 |
if (shared && !pithosObject.subdir && !pithosObject.sharing) { |
131 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self]; |
132 |
} |
133 |
} |
134 |
|
135 |
- (void)setLimitedPithosObject:(ASIPithosObject *)aPithosObject { |
136 |
if (![pithosObject isEqualTo:aPithosObject]) { |
137 |
self.pithosObject.subdir = aPithosObject.subdir; |
138 |
self.pithosObject.name = aPithosObject.name; |
139 |
self.pithosObject.hash = aPithosObject.hash; |
140 |
self.pithosObject.objectHash = aPithosObject.objectHash; |
141 |
self.pithosObject.UUID = aPithosObject.UUID; |
142 |
self.pithosObject.bytes = aPithosObject.bytes; |
143 |
self.pithosObject.contentType = aPithosObject.contentType; |
144 |
self.pithosObject.lastModified = aPithosObject.lastModified; |
145 |
self.pithosObject.version = aPithosObject.version; |
146 |
self.pithosObject.versionTimestamp = aPithosObject.versionTimestamp; |
147 |
self.pithosObject.modifiedBy = aPithosObject.modifiedBy; |
148 |
self.pithosObject.sharedBy = aPithosObject.sharedBy; |
149 |
self.pithosObject.allowedTo = aPithosObject.allowedTo; |
150 |
if (!pithosNodeInfoController) { |
151 |
self.pithosObject.sharing = aPithosObject.sharing; |
152 |
self.pithosObject.publicURI = aPithosObject.publicURI; |
153 |
self.pithosObject = pithosObject; |
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
#pragma mark - |
159 |
#pragma mark ASIHTTPRequestDelegate |
160 |
|
161 |
- (void)objectRequestFinished:(ASIPithosObjectRequest *)request { |
162 |
@autoreleasepool { |
163 |
DLog(@"URL: %@", [request url]); |
164 |
DLog(@"cached: %d", [request didUseCachedResponse]); |
165 |
|
166 |
if ([request isEqualTo:applyMetadataObjectRequest]) { |
167 |
int responseStatusCode = applyMetadataObjectRequest.responseStatusCode; |
168 |
if ((responseStatusCode != 201) && (responseStatusCode != 202)) |
169 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest]; |
170 |
@synchronized(self) { |
171 |
applyMetadataObjectRequest = nil; |
172 |
} |
173 |
if ((responseStatusCode == 201) || (responseStatusCode == 202)) |
174 |
[self refreshInfo]; |
175 |
} else if ([request isEqualTo:refreshMetadataObjectRequest]) { |
176 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
177 |
self.pithosObject = [refreshMetadataObjectRequest object]; |
178 |
if (refreshParent) { |
179 |
// Ask the parent for refresh for the case where an object was removed |
180 |
// It is done here so that it doesn't affect the info window refresh |
181 |
[parent refresh]; |
182 |
refreshParent = NO; |
183 |
} |
184 |
@synchronized(self) { |
185 |
refreshMetadataObjectRequest = nil; |
186 |
} |
187 |
} else if ([request isEqualTo:refreshVersionsObjectRequest]) { |
188 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
189 |
self.versions = [refreshVersionsObjectRequest versions]; |
190 |
@synchronized(self) { |
191 |
refreshVersionsObjectRequest = nil; |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
- (void)objectRequestFailed:(ASIPithosObjectRequest *)request { |
198 |
@autoreleasepool { |
199 |
NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue]; |
200 |
if (retries > 0) { |
201 |
ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[PithosUtilities copyRequest:request]; |
202 |
[(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"]; |
203 |
if ([request isEqualTo:applyMetadataObjectRequest]) { |
204 |
@synchronized(self) { |
205 |
applyMetadataObjectRequest = newRequest; |
206 |
} |
207 |
} else if ([request isEqualTo:refreshMetadataObjectRequest]) { |
208 |
@synchronized(self) { |
209 |
refreshMetadataObjectRequest = newRequest; |
210 |
} |
211 |
} else if ([request isEqualTo:refreshVersionsObjectRequest]) { |
212 |
@synchronized(self) { |
213 |
refreshVersionsObjectRequest = newRequest; |
214 |
} |
215 |
} |
216 |
[[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous]; |
217 |
} else { |
218 |
if ([request isEqualTo:applyMetadataObjectRequest]) { |
219 |
[PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest]; |
220 |
@synchronized(self) { |
221 |
applyMetadataObjectRequest = nil; |
222 |
} |
223 |
} else if ([request isEqualTo:refreshMetadataObjectRequest]) { |
224 |
[PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest]; |
225 |
@synchronized(self) { |
226 |
refreshMetadataObjectRequest = nil; |
227 |
} |
228 |
} else if ([request isEqualTo:refreshVersionsObjectRequest]) { |
229 |
[PithosUtilities httpRequestErrorAlertWithRequest:refreshVersionsObjectRequest]; |
230 |
@synchronized(self) { |
231 |
refreshVersionsObjectRequest = nil; |
232 |
} |
233 |
} |
234 |
} |
235 |
} |
236 |
} |
237 |
|
238 |
#pragma mark - |
239 |
#pragma mark Info |
240 |
|
241 |
- (void)applyInfo { |
242 |
@synchronized(self) { |
243 |
if (applyMetadataObjectRequest == nil) { |
244 |
if (pithosObject.subdir) { |
245 |
BOOL createObject = NO; |
246 |
NSAlert *alert; |
247 |
ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos |
248 |
containerName:pithosContainer.name |
249 |
objectName:prefix]; |
250 |
ASINetworkQueue *networkQueue = [ASINetworkQueue queue]; |
251 |
[networkQueue go]; |
252 |
[networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES]; |
253 |
if ([request error]) { |
254 |
alert = [[NSAlert alloc] init]; |
255 |
[alert setMessageText:@"HTTP Request Error"]; |
256 |
[alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [[request error] localizedDescription]]]; |
257 |
[alert addButtonWithTitle:@"OK"]; |
258 |
[alert runModal]; |
259 |
return; |
260 |
} else if (request.responseStatusCode == 200) { |
261 |
alert = [[NSAlert alloc] init]; |
262 |
[alert setMessageText:@"Apply changes"]; |
263 |
[alert setInformativeText:[NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?", self.displayName]]; |
264 |
[alert addButtonWithTitle:@"OK"]; |
265 |
[alert addButtonWithTitle:@"Cancel"]; |
266 |
NSInteger choice = [alert runModal]; |
267 |
if (choice == NSAlertFirstButtonReturn) { |
268 |
request = [ASIPithosObjectRequest deleteObjectRequestWithPithos:pithos |
269 |
containerName:pithosContainer.name |
270 |
objectName:prefix]; |
271 |
ASINetworkQueue *networkQueue = [ASINetworkQueue queue]; |
272 |
[networkQueue go]; |
273 |
[networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES]; |
274 |
if ([request error]) { |
275 |
alert = [[NSAlert alloc] init]; |
276 |
[alert setMessageText:@"HTTP Request Error"]; |
277 |
[alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [[request error] localizedDescription]]]; |
278 |
[alert addButtonWithTitle:@"OK"]; |
279 |
[alert runModal]; |
280 |
return; |
281 |
} else if (request.responseStatusCode != 204) { |
282 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:request]; |
283 |
return; |
284 |
} |
285 |
refreshParent = YES; |
286 |
createObject = YES; |
287 |
} else { |
288 |
return; |
289 |
} |
290 |
} else if (request.responseStatusCode == 404) { |
291 |
createObject = YES; |
292 |
} else { |
293 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:request]; |
294 |
return; |
295 |
} |
296 |
if (createObject) { |
297 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
298 |
applyMetadataObjectRequest = [ASIPithosObjectRequest writeObjectDataRequestWithPithos:pithos |
299 |
containerName:pithosContainer.name |
300 |
objectName:prefix |
301 |
eTag:nil |
302 |
contentType:@"application/directory" |
303 |
contentEncoding:pithosObject.contentEncoding |
304 |
contentDisposition:pithosObject.contentDisposition |
305 |
manifest:pithosObject.manifest |
306 |
sharing:pithosObject.sharing |
307 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
308 |
metadata:pithosObject.metadata |
309 |
data:[NSData data]]; |
310 |
pithosObject.subdir = NO; |
311 |
applyMetadataObjectRequest.delegate = self; |
312 |
applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
313 |
applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
314 |
applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
315 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
316 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
317 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
318 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
319 |
nil]; |
320 |
[[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
321 |
} |
322 |
} else { |
323 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
324 |
if (sharingAccount) { |
325 |
applyMetadataObjectRequest = [ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos |
326 |
containerName:pithosContainer.name |
327 |
objectName:pithosObject.name |
328 |
contentEncoding:nil |
329 |
contentDisposition:nil |
330 |
manifest:nil |
331 |
sharing:nil |
332 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
333 |
metadata:pithosObject.metadata |
334 |
update:NO]; |
335 |
[applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos]; |
336 |
} else { |
337 |
applyMetadataObjectRequest = [ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos |
338 |
containerName:pithosContainer.name |
339 |
objectName:pithosObject.name |
340 |
contentEncoding:pithosObject.contentEncoding |
341 |
contentDisposition:pithosObject.contentDisposition |
342 |
manifest:pithosObject.manifest |
343 |
sharing:(pithosObject.sharing ? pithosObject.sharing : @"") |
344 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
345 |
metadata:pithosObject.metadata |
346 |
update:NO]; |
347 |
} |
348 |
applyMetadataObjectRequest.delegate = self; |
349 |
applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
350 |
applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
351 |
applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
352 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
353 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
354 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
355 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
356 |
nil]; |
357 |
[[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
358 |
} |
359 |
} |
360 |
} |
361 |
} |
362 |
|
363 |
- (void)refreshInfo { |
364 |
@synchronized(self) { |
365 |
if (pithosObject.subdir) { |
366 |
self.pithosObject = [ASIPithosObject subdirWithName:pithosObject.name]; |
367 |
return; |
368 |
} else if (refreshMetadataObjectRequest == nil) { |
369 |
refreshMetadataObjectRequest = [ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos |
370 |
containerName:pithosContainer.name |
371 |
objectName:prefix]; |
372 |
if (sharingAccount) |
373 |
[refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos]; |
374 |
refreshMetadataObjectRequest.delegate = self; |
375 |
refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
376 |
refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
377 |
refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
378 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
379 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
380 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
381 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
382 |
nil]; |
383 |
if (!sharingAccount) |
384 |
refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache]; |
385 |
[[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
386 |
} |
387 |
} |
388 |
[self refreshVersions]; |
389 |
} |
390 |
|
391 |
#pragma mark - |
392 |
#pragma mark Versions |
393 |
|
394 |
- (void)refreshVersions { |
395 |
@synchronized(self) { |
396 |
if (pithosObject.subdir) { |
397 |
return; |
398 |
} else if (refreshVersionsObjectRequest == nil) { |
399 |
refreshVersionsObjectRequest = [ASIPithosObjectRequest objectVersionsRequestWithPithos:pithos |
400 |
containerName:pithosContainer.name |
401 |
objectName:pithosObject.name]; |
402 |
if (sharingAccount) |
403 |
[refreshVersionsObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos]; |
404 |
refreshVersionsObjectRequest.delegate = self; |
405 |
refreshVersionsObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
406 |
refreshVersionsObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
407 |
refreshVersionsObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
408 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
409 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
410 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
411 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
412 |
nil]; |
413 |
if (!sharingAccount) |
414 |
refreshVersionsObjectRequest.downloadCache = [ASIDownloadCache sharedCache]; |
415 |
[[PithosUtilities prepareRequest:refreshVersionsObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
416 |
} |
417 |
} |
418 |
} |
419 |
|
420 |
#pragma mark - |
421 |
#pragma mark Actions |
422 |
|
423 |
- (void)showPithosNodeInfo:(id)sender { |
424 |
if (!pithosNodeInfoController) { |
425 |
pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self]; |
426 |
[self refreshInfo]; |
427 |
} |
428 |
[pithosNodeInfoController showWindow:sender]; |
429 |
[[pithosNodeInfoController window] makeKeyAndOrderFront:sender]; |
430 |
[NSApp activateIgnoringOtherApps:YES]; |
431 |
} |
432 |
|
433 |
@end |