5 // Copyright 2011-2012 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
38 #import "PithosSubdirNode.h"
39 #import "ASIPithosRequest.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"
49 static NSImage *sharedIcon = nil;
51 @implementation PithosSubdirNode
52 @synthesize pithosObject;
56 if (self == [PithosSubdirNode class])
57 sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain];
61 #pragma mark Object Lifecycle
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 childrenUpdatedNotificationName = @"PithosSubdirNodeChildrenUpdated";
77 [refreshMetadataObjectRequest clearDelegatesAndCancel];
78 [refreshMetadataObjectRequest release];
79 [applyMetadataObjectRequest clearDelegatesAndCancel];
80 [applyMetadataObjectRequest release];
81 [pithosObject release];
86 #pragma mark Properties
88 - (void)setPithos:(ASIPithos *)aPithos {
89 if (aPithos && ![aPithos isEqualTo:pithos]) {
91 pithos = [aPithos retain];
99 url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@",
100 (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL),
101 pithosContainer.name,
103 (shared ? @"?shared" : @"")];
107 - (NSString *)displayName {
108 if (displayName == nil) {
109 displayName = [pithosObject.name lastPathComponent];
110 if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"])
111 displayName = [displayName stringByAppendingString:@"/"];
112 [displayName retain];
114 return [[displayName copy] autorelease];
117 - (void)setDisplayName:(NSString *)aDisplayName {
122 icon = [sharedIcon retain];
126 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
127 if (![pithosObject isEqualTo:aPithosObject]) {
128 [pithosObject release];
129 pithosObject = [aPithosObject retain];
131 if (pithosObject.subdir) {
132 self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)];
134 self.prefix = [NSString stringWithString:pithosObject.name];
136 self.isPublic = (pithosObject.publicURI != nil);
137 // Refresh browser if the object is in my shared and is no longer shared
138 if (shared && !pithosObject.subdir && !pithosObject.sharing) {
139 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
144 #pragma mark ASIHTTPRequestDelegate
146 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
147 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
148 NSLog(@"URL: %@", [request url]);
149 NSLog(@"cached: %d", [request didUseCachedResponse]);
151 if ([request isEqualTo:applyMetadataObjectRequest]) {
152 int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
153 if ((responseStatusCode != 201) && (responseStatusCode != 202))
154 [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
155 @synchronized(self) {
156 [applyMetadataObjectRequest release];
157 applyMetadataObjectRequest = nil;
159 if ((responseStatusCode == 201) || (responseStatusCode == 202))
161 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
162 [[pithosNodeInfoController window] makeFirstResponder:nil];
163 self.pithosObject = [refreshMetadataObjectRequest object];
165 // Ask the parent for refresh for the case where an object was removed
166 // It is done here so that it doesn't affect the info window refresh
170 @synchronized(self) {
171 [refreshMetadataObjectRequest release];
172 refreshMetadataObjectRequest = nil;
178 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
179 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
180 NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
182 ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[PithosUtilities copyRequest:request];
183 [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
184 if ([request isEqualTo:applyMetadataObjectRequest]) {
185 @synchronized(self) {
186 [applyMetadataObjectRequest release];
187 applyMetadataObjectRequest = newRequest;
189 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
190 @synchronized(self) {
191 [refreshMetadataObjectRequest release];
192 refreshMetadataObjectRequest = newRequest;
195 [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
197 if ([request isEqualTo:applyMetadataObjectRequest]) {
198 dispatch_async(dispatch_get_main_queue(), ^{
199 [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
201 @synchronized(self) {
202 [applyMetadataObjectRequest release];
203 applyMetadataObjectRequest = nil;
205 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
206 dispatch_async(dispatch_get_main_queue(), ^{
207 [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
209 @synchronized(self) {
210 [refreshMetadataObjectRequest release];
211 refreshMetadataObjectRequest = nil;
222 @synchronized(self) {
223 if (applyMetadataObjectRequest == nil) {
224 if (pithosObject.subdir) {
225 BOOL createObject = NO;
227 ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos
228 containerName:pithosContainer.name
230 ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
232 [networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES];
233 if ([request error]) {
234 alert = [[[NSAlert alloc] init] autorelease];
235 [alert setMessageText:@"HTTP Request Error"];
236 [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
237 [alert addButtonWithTitle:@"OK"];
240 } else if (request.responseStatusCode == 200) {
241 alert = [[[NSAlert alloc] init] autorelease];
242 [alert setMessageText:@"Apply changes"];
243 [alert setInformativeText:[NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?", self.displayName]];
244 [alert addButtonWithTitle:@"OK"];
245 [alert addButtonWithTitle:@"Cancel"];
246 NSInteger choice = [alert runModal];
247 if (choice == NSAlertFirstButtonReturn) {
248 request = [ASIPithosObjectRequest deleteObjectRequestWithPithos:pithos
249 containerName:pithosContainer.name
251 ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
253 [networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES];
254 if ([request error]) {
255 alert = [[[NSAlert alloc] init] autorelease];
256 [alert setMessageText:@"HTTP Request Error"];
257 [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
258 [alert addButtonWithTitle:@"OK"];
261 } else if (request.responseStatusCode != 204) {
262 [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
270 } else if (request.responseStatusCode == 404) {
273 [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
277 [[pithosNodeInfoController window] makeFirstResponder:nil];
278 applyMetadataObjectRequest = [[ASIPithosObjectRequest writeObjectDataRequestWithPithos:pithos
279 containerName:pithosContainer.name
282 contentType:@"application/directory"
283 contentEncoding:pithosObject.contentEncoding
284 contentDisposition:pithosObject.contentDisposition
285 manifest:pithosObject.manifest
286 sharing:pithosObject.sharing
287 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
288 metadata:pithosObject.metadata
289 data:[NSData data]] retain];
290 pithosObject.subdir = NO;
291 applyMetadataObjectRequest.delegate = self;
292 applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
293 applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
294 applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
295 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
296 [NSNumber numberWithUnsignedInteger:10], @"retries",
297 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
298 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
300 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
303 [[pithosNodeInfoController window] makeFirstResponder:nil];
304 if (sharingAccount) {
305 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
306 containerName:pithosContainer.name
307 objectName:pithosObject.name
309 contentDisposition:nil
312 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
313 metadata:pithosObject.metadata
315 [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
317 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
318 containerName:pithosContainer.name
319 objectName:pithosObject.name
320 contentEncoding:pithosObject.contentEncoding
321 contentDisposition:pithosObject.contentDisposition
322 manifest:pithosObject.manifest
323 sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
324 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
325 metadata:pithosObject.metadata
328 applyMetadataObjectRequest.delegate = self;
329 applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
330 applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
331 applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
332 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
333 [NSNumber numberWithUnsignedInteger:10], @"retries",
334 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
335 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
337 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
343 - (void)refreshInfo {
344 @synchronized(self) {
345 if (pithosObject.subdir) {
346 self.pithosObject = [ASIPithosObject subdirWithName:pithosObject.name];
347 } else if (refreshMetadataObjectRequest == nil) {
348 refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos
349 containerName:pithosContainer.name
350 objectName:prefix] retain];
352 [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
353 refreshMetadataObjectRequest.delegate = self;
354 refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
355 refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
356 refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
357 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
358 [NSNumber numberWithUnsignedInteger:10], @"retries",
359 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
360 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
362 refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
363 [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
371 - (void)showPithosNodeInfo:(id)sender {
372 if (!pithosNodeInfoController)
373 pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
374 [pithosNodeInfoController showWindow:sender];
375 [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
376 [NSApp activateIgnoringOtherApps:YES];