5 // Copyright 2011 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"
40 #import "ASIPithosObjectRequest.h"
41 #import "ASIPithosContainer.h"
42 #import "ASIPithosObject.h"
43 #import "ASIDownloadCache.h"
44 #import "PithosUtilities.h"
45 #import "PithosObjectNodeInfoController.h"
47 static NSImage *sharedIcon = nil;
49 @implementation PithosSubdirNode
50 @synthesize pithosObject;
54 if (self == [PithosSubdirNode class])
55 sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain];
59 #pragma mark Object Lifecycle
61 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
62 if ((self = [super init])) {
63 self.pithosContainer = aPithosContainer;
64 self.pithosObject = aPithosObject;
65 childrenUpdatedNotificationName = @"PithosSubdirNodeChildrenUpdated";
72 [refreshMetadataObjectRequest clearDelegatesAndCancel];
73 [refreshMetadataObjectRequest release];
74 [applyMetadataObjectRequest clearDelegatesAndCancel];
75 [applyMetadataObjectRequest release];
76 [pithosObject release];
81 #pragma mark Properties
85 url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@",
86 (sharingAccount ? [ASIPithosRequest storageURLWithAuthUser:sharingAccount] : [ASIPithosRequest storageURL]),
89 (shared ? @"?shared" : @"")];
93 - (NSString *)displayName {
94 if (displayName == nil) {
95 displayName = [pithosObject.name lastPathComponent];
96 if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"])
97 displayName = [displayName stringByAppendingString:@"/"];
100 return [[displayName copy] autorelease];
103 - (void)setDisplayName:(NSString *)aDisplayName {
108 icon = [sharedIcon retain];
112 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
113 if (![pithosObject isEqualTo:aPithosObject]) {
114 [pithosObject release];
115 pithosObject = [aPithosObject retain];
117 if (pithosObject.subdir) {
118 self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)];
120 self.prefix = [NSString stringWithString:pithosObject.name];
122 self.isPublic = (pithosObject.publicURI != nil);
123 // Refresh browser if the object is in my shared and is no longer shared
124 if (shared && !pithosObject.subdir && !pithosObject.sharing) {
125 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
130 #pragma mark ASIHTTPRequestDelegate
132 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
133 NSLog(@"URL: %@", [request url]);
134 NSLog(@"cached: %d", [request didUseCachedResponse]);
136 if ([request isEqualTo:applyMetadataObjectRequest]) {
137 int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
138 if ((responseStatusCode != 201) && (responseStatusCode != 202))
139 [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
140 @synchronized(self) {
141 [applyMetadataObjectRequest release];
142 applyMetadataObjectRequest = nil;
144 if ((responseStatusCode == 201) || (responseStatusCode == 202))
146 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
147 [[pithosNodeInfoController window] makeFirstResponder:nil];
148 self.pithosObject = [refreshMetadataObjectRequest object];
150 // Ask the parent for refresh for the case where an object was removed
151 // It is done here so that it doesn't affect the info window refresh
155 @synchronized(self) {
156 [refreshMetadataObjectRequest release];
157 refreshMetadataObjectRequest = nil;
162 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
163 if ([request isEqualTo:applyMetadataObjectRequest]) {
164 [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
165 @synchronized(self) {
166 [applyMetadataObjectRequest release];
167 applyMetadataObjectRequest = nil;
169 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
170 [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
171 @synchronized(self) {
172 [refreshMetadataObjectRequest release];
173 refreshMetadataObjectRequest = nil;
182 @synchronized(self) {
183 if (applyMetadataObjectRequest == nil) {
184 if (pithosObject.subdir) {
185 BOOL createObject = NO;
187 ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name
189 [[PithosUtilities prepareRequest:request] startAsynchronous];
190 while (![request isFinished]) {
193 if ([request error]) {
194 alert = [[[NSAlert alloc] init] autorelease];
195 [alert setMessageText:@"HTTP Request Error"];
196 [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
197 [alert addButtonWithTitle:@"OK"];
200 } else if (request.responseStatusCode == 200) {
201 alert = [[[NSAlert alloc] init] autorelease];
202 [alert setMessageText:@"Apply changes"];
203 [alert setInformativeText:[NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?", self.displayName]];
204 [alert addButtonWithTitle:@"OK"];
205 [alert addButtonWithTitle:@"Cancel"];
206 NSInteger choice = [alert runModal];
207 if (choice == NSAlertFirstButtonReturn) {
208 request = [ASIPithosObjectRequest deleteObjectRequestWithContainerName:pithosContainer.name
210 [[PithosUtilities prepareRequest:request] startAsynchronous];
211 while (![request isFinished]) {
214 if ([request error]) {
215 alert = [[[NSAlert alloc] init] autorelease];
216 [alert setMessageText:@"HTTP Request Error"];
217 [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
218 [alert addButtonWithTitle:@"OK"];
221 } else if (request.responseStatusCode != 204) {
222 [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
230 } else if (request.responseStatusCode == 404) {
233 [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
237 [[pithosNodeInfoController window] makeFirstResponder:nil];
238 applyMetadataObjectRequest = [[ASIPithosObjectRequest writeObjectDataRequestWithContainerName:pithosContainer.name
241 contentType:@"application/directory"
242 contentEncoding:pithosObject.contentEncoding
243 contentDisposition:pithosObject.contentDisposition
244 manifest:pithosObject.manifest
245 sharing:pithosObject.sharing
246 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
247 metadata:pithosObject.metadata
248 data:[NSData data]] retain];
249 pithosObject.subdir = NO;
250 applyMetadataObjectRequest.delegate = self;
251 applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
252 applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
253 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
256 [[pithosNodeInfoController window] makeFirstResponder:nil];
257 if (sharingAccount) {
258 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name
259 objectName:pithosObject.name
261 contentDisposition:nil
264 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
265 metadata:pithosObject.metadata
267 [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount];
269 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name
270 objectName:pithosObject.name
271 contentEncoding:pithosObject.contentEncoding
272 contentDisposition:pithosObject.contentDisposition
273 manifest:pithosObject.manifest
274 sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
275 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
276 metadata:pithosObject.metadata
279 applyMetadataObjectRequest.delegate = self;
280 applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
281 applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
282 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
288 - (void)refreshInfo {
289 @synchronized(self) {
290 if (pithosObject.subdir) {
291 self.pithosObject = [ASIPithosObject subdirWithName:pithosObject.name];
292 } else if (refreshMetadataObjectRequest == nil) {
293 refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name
294 objectName:prefix] retain];
296 [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount];
297 refreshMetadataObjectRequest.delegate = self;
298 refreshMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
299 refreshMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
300 refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
301 [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
309 - (void)showPithosNodeInfo:(id)sender {
310 if (!pithosNodeInfoController)
311 pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
312 [pithosNodeInfoController showWindow:sender];
313 [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
314 [NSApp activateIgnoringOtherApps:YES];