root / pithos-macos / PithosSubdirNode.m @ 919cb043
History | View | Annotate | Download (19.1 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 "ASIDownloadCache.h" |
45 |
#import "PithosUtilities.h" |
46 |
#import "PithosObjectNodeInfoController.h" |
47 |
|
48 |
static NSImage *sharedIcon = nil; |
49 |
|
50 |
@implementation PithosSubdirNode |
51 |
@synthesize pithosObject; |
52 |
@synthesize isPublic; |
53 |
|
54 |
+ (void)initialize { |
55 |
if (self == [PithosSubdirNode class]) |
56 |
sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain]; |
57 |
} |
58 |
|
59 |
#pragma mark - |
60 |
#pragma mark Object Lifecycle |
61 |
|
62 |
- (id)initWithPithos:(ASIPithos *)aPithos |
63 |
pithosContainer:(ASIPithosContainer *)aPithosContainer |
64 |
pithosObject:(ASIPithosObject *)aPithosObject { |
65 |
if ((self = [super init])) { |
66 |
self.pithos = aPithos; |
67 |
self.pithosContainer = aPithosContainer; |
68 |
self.pithosObject = aPithosObject; |
69 |
childrenUpdatedNotificationName = @"PithosSubdirNodeChildrenUpdated"; |
70 |
refreshParent = NO; |
71 |
} |
72 |
return self; |
73 |
} |
74 |
|
75 |
- (void)dealloc { |
76 |
[refreshMetadataObjectRequest clearDelegatesAndCancel]; |
77 |
[refreshMetadataObjectRequest release]; |
78 |
[applyMetadataObjectRequest clearDelegatesAndCancel]; |
79 |
[applyMetadataObjectRequest release]; |
80 |
[pithosObject release]; |
81 |
[super dealloc]; |
82 |
} |
83 |
|
84 |
#pragma mark - |
85 |
#pragma mark Properties |
86 |
|
87 |
- (NSString *)url { |
88 |
if (url == nil) |
89 |
url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@", |
90 |
(sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL), |
91 |
pithosContainer.name, |
92 |
prefix, |
93 |
(shared ? @"?shared" : @"")]; |
94 |
return url; |
95 |
} |
96 |
|
97 |
- (NSString *)displayName { |
98 |
if (displayName == nil) { |
99 |
displayName = [pithosObject.name lastPathComponent]; |
100 |
if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"]) |
101 |
displayName = [displayName stringByAppendingString:@"/"]; |
102 |
[displayName retain]; |
103 |
} |
104 |
return [[displayName copy] autorelease]; |
105 |
} |
106 |
|
107 |
- (void)setDisplayName:(NSString *)aDisplayName { |
108 |
} |
109 |
|
110 |
- (NSImage *)icon { |
111 |
if (icon == nil) |
112 |
icon = [sharedIcon retain]; |
113 |
return icon; |
114 |
} |
115 |
|
116 |
- (void)setPithosObject:(ASIPithosObject *)aPithosObject { |
117 |
if (![pithosObject isEqualTo:aPithosObject]) { |
118 |
[pithosObject release]; |
119 |
pithosObject = [aPithosObject retain]; |
120 |
} |
121 |
if (pithosObject.subdir) { |
122 |
self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)]; |
123 |
} else { |
124 |
self.prefix = [NSString stringWithString:pithosObject.name]; |
125 |
} |
126 |
self.isPublic = (pithosObject.publicURI != nil); |
127 |
// Refresh browser if the object is in my shared and is no longer shared |
128 |
if (shared && !pithosObject.subdir && !pithosObject.sharing) { |
129 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self]; |
130 |
} |
131 |
} |
132 |
|
133 |
#pragma mark - |
134 |
#pragma mark ASIHTTPRequestDelegate |
135 |
|
136 |
- (void)objectRequestFinished:(ASIPithosObjectRequest *)request { |
137 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
138 |
NSLog(@"URL: %@", [request url]); |
139 |
NSLog(@"cached: %d", [request didUseCachedResponse]); |
140 |
|
141 |
if ([request isEqualTo:applyMetadataObjectRequest]) { |
142 |
int responseStatusCode = applyMetadataObjectRequest.responseStatusCode; |
143 |
if ((responseStatusCode != 201) && (responseStatusCode != 202)) |
144 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest]; |
145 |
@synchronized(self) { |
146 |
[applyMetadataObjectRequest release]; |
147 |
applyMetadataObjectRequest = nil; |
148 |
} |
149 |
if ((responseStatusCode == 201) || (responseStatusCode == 202)) |
150 |
[self refreshInfo]; |
151 |
} else if ([request isEqualTo:refreshMetadataObjectRequest]) { |
152 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
153 |
self.pithosObject = [refreshMetadataObjectRequest object]; |
154 |
if (refreshParent) { |
155 |
// Ask the parent for refresh for the case where an object was removed |
156 |
// It is done here so that it doesn't affect the info window refresh |
157 |
[parent refresh]; |
158 |
refreshParent = NO; |
159 |
} |
160 |
@synchronized(self) { |
161 |
[refreshMetadataObjectRequest release]; |
162 |
refreshMetadataObjectRequest = nil; |
163 |
} |
164 |
} |
165 |
[pool drain]; |
166 |
} |
167 |
|
168 |
- (void)objectRequestFailed:(ASIPithosObjectRequest *)request { |
169 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
170 |
if ([request isEqualTo:applyMetadataObjectRequest]) { |
171 |
[PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest]; |
172 |
@synchronized(self) { |
173 |
[applyMetadataObjectRequest release]; |
174 |
applyMetadataObjectRequest = nil; |
175 |
} |
176 |
} else if ([request isEqualTo:refreshMetadataObjectRequest]) { |
177 |
[PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest]; |
178 |
@synchronized(self) { |
179 |
[refreshMetadataObjectRequest release]; |
180 |
refreshMetadataObjectRequest = nil; |
181 |
} |
182 |
} |
183 |
[pool drain]; |
184 |
} |
185 |
|
186 |
#pragma mark - |
187 |
#pragma mark Info |
188 |
|
189 |
- (void)applyInfo { |
190 |
@synchronized(self) { |
191 |
if (applyMetadataObjectRequest == nil) { |
192 |
if (pithosObject.subdir) { |
193 |
BOOL createObject = NO; |
194 |
NSAlert *alert; |
195 |
ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos |
196 |
containerName:pithosContainer.name |
197 |
objectName:prefix]; |
198 |
[[PithosUtilities prepareRequest:request] startAsynchronous]; |
199 |
while (![request isFinished]) { |
200 |
usleep(1); |
201 |
} |
202 |
if ([request error]) { |
203 |
alert = [[[NSAlert alloc] init] autorelease]; |
204 |
[alert setMessageText:@"HTTP Request Error"]; |
205 |
[alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]]; |
206 |
[alert addButtonWithTitle:@"OK"]; |
207 |
[alert runModal]; |
208 |
return; |
209 |
} else if (request.responseStatusCode == 200) { |
210 |
alert = [[[NSAlert alloc] init] autorelease]; |
211 |
[alert setMessageText:@"Apply changes"]; |
212 |
[alert setInformativeText:[NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?", self.displayName]]; |
213 |
[alert addButtonWithTitle:@"OK"]; |
214 |
[alert addButtonWithTitle:@"Cancel"]; |
215 |
NSInteger choice = [alert runModal]; |
216 |
if (choice == NSAlertFirstButtonReturn) { |
217 |
request = [ASIPithosObjectRequest deleteObjectRequestWithPithos:pithos |
218 |
containerName:pithosContainer.name |
219 |
objectName:prefix]; |
220 |
[[PithosUtilities prepareRequest:request] startAsynchronous]; |
221 |
while (![request isFinished]) { |
222 |
usleep(1); |
223 |
} |
224 |
if ([request error]) { |
225 |
alert = [[[NSAlert alloc] init] autorelease]; |
226 |
[alert setMessageText:@"HTTP Request Error"]; |
227 |
[alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]]; |
228 |
[alert addButtonWithTitle:@"OK"]; |
229 |
[alert runModal]; |
230 |
return; |
231 |
} else if (request.responseStatusCode != 204) { |
232 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:request]; |
233 |
return; |
234 |
} |
235 |
refreshParent = YES; |
236 |
createObject = YES; |
237 |
} else { |
238 |
return; |
239 |
} |
240 |
} else if (request.responseStatusCode == 404) { |
241 |
createObject = YES; |
242 |
} else { |
243 |
[PithosUtilities unexpectedResponseStatusAlertWithRequest:request]; |
244 |
return; |
245 |
} |
246 |
if (createObject) { |
247 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
248 |
applyMetadataObjectRequest = [[ASIPithosObjectRequest writeObjectDataRequestWithPithos:pithos |
249 |
containerName:pithosContainer.name |
250 |
objectName:prefix |
251 |
eTag:nil |
252 |
contentType:@"application/directory" |
253 |
contentEncoding:pithosObject.contentEncoding |
254 |
contentDisposition:pithosObject.contentDisposition |
255 |
manifest:pithosObject.manifest |
256 |
sharing:pithosObject.sharing |
257 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
258 |
metadata:pithosObject.metadata |
259 |
data:[NSData data]] retain]; |
260 |
pithosObject.subdir = NO; |
261 |
applyMetadataObjectRequest.delegate = self; |
262 |
applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
263 |
applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
264 |
applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
265 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
266 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
267 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
268 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
269 |
nil]; |
270 |
[[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
271 |
} |
272 |
} else { |
273 |
[[pithosNodeInfoController window] makeFirstResponder:nil]; |
274 |
if (sharingAccount) { |
275 |
applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos |
276 |
containerName:pithosContainer.name |
277 |
objectName:pithosObject.name |
278 |
contentEncoding:nil |
279 |
contentDisposition:nil |
280 |
manifest:nil |
281 |
sharing:nil |
282 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
283 |
metadata:pithosObject.metadata |
284 |
update:NO] retain]; |
285 |
[applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos]; |
286 |
} else { |
287 |
applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos |
288 |
containerName:pithosContainer.name |
289 |
objectName:pithosObject.name |
290 |
contentEncoding:pithosObject.contentEncoding |
291 |
contentDisposition:pithosObject.contentDisposition |
292 |
manifest:pithosObject.manifest |
293 |
sharing:(pithosObject.sharing ? pithosObject.sharing : @"") |
294 |
isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) |
295 |
metadata:pithosObject.metadata |
296 |
update:NO] retain]; |
297 |
} |
298 |
applyMetadataObjectRequest.delegate = self; |
299 |
applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
300 |
applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
301 |
applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
302 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
303 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
304 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
305 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
306 |
nil]; |
307 |
[[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
308 |
} |
309 |
} |
310 |
} |
311 |
} |
312 |
|
313 |
- (void)refreshInfo { |
314 |
@synchronized(self) { |
315 |
if (pithosObject.subdir) { |
316 |
self.pithosObject = [ASIPithosObject subdirWithName:pithosObject.name]; |
317 |
} else if (refreshMetadataObjectRequest == nil) { |
318 |
refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos |
319 |
containerName:pithosContainer.name |
320 |
objectName:prefix] retain]; |
321 |
if (sharingAccount) |
322 |
[refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos]; |
323 |
refreshMetadataObjectRequest.delegate = self; |
324 |
refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:); |
325 |
refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:); |
326 |
refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
327 |
[NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", |
328 |
[NSNumber numberWithUnsignedInteger:10], @"retries", |
329 |
NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", |
330 |
NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", |
331 |
nil]; |
332 |
refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache]; |
333 |
[[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous]; |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
#pragma mark - |
339 |
#pragma mark Actions |
340 |
|
341 |
- (void)showPithosNodeInfo:(id)sender { |
342 |
if (!pithosNodeInfoController) |
343 |
pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self]; |
344 |
[pithosNodeInfoController showWindow:sender]; |
345 |
[[pithosNodeInfoController window] makeKeyAndOrderFront:sender]; |
346 |
[NSApp activateIgnoringOtherApps:YES]; |
347 |
} |
348 |
|
349 |
@end |