Memory leaks plugged.
[pithos-macos] / pithos-macos / PithosSubdirNode.m
1 //
2 //  PithosSubdirNode.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 "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"
46
47 static NSImage *sharedIcon = nil;
48
49 @implementation PithosSubdirNode
50 @synthesize pithosObject;
51 @synthesize isPublic;
52
53 + (void)initialize {
54         if (self == [PithosSubdirNode class])
55         sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain];
56 }
57
58 #pragma mark -
59 #pragma mark Object Lifecycle
60
61 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
62     if ((self = [super init])) {
63         self.pithosContainer = aPithosContainer;
64         self.pithosObject = aPithosObject;
65         childrenUpdatedNotificationName = @"PithosSubdirNodeChildrenUpdated";
66         refreshParent = NO;
67     }
68     return self;
69 }
70
71 - (void)dealloc {
72     [refreshMetadataObjectRequest clearDelegatesAndCancel];
73     [refreshMetadataObjectRequest release];
74     [applyMetadataObjectRequest clearDelegatesAndCancel];
75     [applyMetadataObjectRequest release];
76     [pithosObject release];
77     [super dealloc];
78 }
79
80 #pragma mark -
81 #pragma mark Properties
82
83 - (NSString *)url {
84     if (url == nil)
85         url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@", 
86                (sharingAccount ? [ASIPithosRequest storageURLWithAuthUser:sharingAccount] : [ASIPithosRequest storageURL]), 
87                pithosContainer.name, 
88                prefix, 
89                (shared ? @"?shared" : @"")];
90     return url;
91 }
92
93 - (NSString *)displayName {
94     if (displayName == nil) {
95         displayName = [pithosObject.name lastPathComponent];
96         if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"])
97             displayName = [displayName stringByAppendingString:@"/"];
98         [displayName retain];
99     } 
100     return [[displayName copy] autorelease];
101 }
102
103 - (void)setDisplayName:(NSString *)aDisplayName {    
104 }
105
106 - (NSImage *)icon {
107     if (icon == nil)
108         icon = [sharedIcon retain];
109     return icon;
110 }
111
112 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
113     if (![pithosObject isEqualTo:aPithosObject]) {
114         [pithosObject release];
115         pithosObject = [aPithosObject retain];
116     }
117     if (pithosObject.subdir) {
118         self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)];
119     } else {
120         self.prefix = [NSString stringWithString:pithosObject.name];
121     }
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];
126     }
127 }
128
129 #pragma mark -
130 #pragma mark ASIHTTPRequestDelegate
131
132 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
133     NSLog(@"URL: %@", [request url]);
134     NSLog(@"cached: %d", [request didUseCachedResponse]);
135     
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;
143         }
144         if ((responseStatusCode == 201) || (responseStatusCode == 202))
145             [self refreshInfo];
146     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
147         [[pithosNodeInfoController window] makeFirstResponder:nil];
148         self.pithosObject = [refreshMetadataObjectRequest object];
149         if (refreshParent) {
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
152             [parent refresh];
153             refreshParent = NO;
154         }
155         @synchronized(self) {
156             [refreshMetadataObjectRequest release];
157             refreshMetadataObjectRequest = nil;
158         }
159     }
160 }
161
162 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
163     if ([request isEqualTo:applyMetadataObjectRequest]) {
164         [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
165         @synchronized(self) {
166             [applyMetadataObjectRequest release];
167             applyMetadataObjectRequest = nil;
168         }
169     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
170         [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
171         @synchronized(self) {
172             [refreshMetadataObjectRequest release];
173             refreshMetadataObjectRequest = nil;
174         }
175     }
176 }
177
178 #pragma mark -
179 #pragma mark Info
180
181 - (void)applyInfo {
182     @synchronized(self) {
183         if (applyMetadataObjectRequest == nil) {
184             if (pithosObject.subdir) {
185                 BOOL createObject = NO;
186                 NSAlert *alert;
187                 ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name 
188                                                                                                       objectName:prefix];
189                 [[PithosUtilities prepareRequest:request] startAsynchronous];
190                 while (![request isFinished]) {
191                     usleep(1);
192                 }
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"];
198                     [alert runModal];
199                     return;
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 
209                                                                                        objectName:prefix];
210                         [[PithosUtilities prepareRequest:request] startAsynchronous];
211                         while (![request isFinished]) {
212                             usleep(1);
213                         }
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"];
219                             [alert runModal];
220                             return;
221                         } else if (request.responseStatusCode != 204) {
222                             [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
223                             return;
224                         }
225                         refreshParent = YES;
226                         createObject = YES;
227                     } else {
228                         return;
229                     }
230                 } else if (request.responseStatusCode == 404) {
231                     createObject = YES;
232                 } else {
233                     [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
234                     return;
235                 }
236                 if (createObject) {
237                     [[pithosNodeInfoController window] makeFirstResponder:nil];
238                     applyMetadataObjectRequest = [[ASIPithosObjectRequest writeObjectDataRequestWithContainerName:pithosContainer.name 
239                                                                                                        objectName:prefix 
240                                                                                                              eTag:nil
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];
254                 }
255             } else {
256                 [[pithosNodeInfoController window] makeFirstResponder:nil];
257                 if (sharingAccount) {
258                     applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name 
259                                                                                                             objectName:pithosObject.name 
260                                                                                                        contentEncoding:nil
261                                                                                                     contentDisposition:nil
262                                                                                                               manifest:nil 
263                                                                                                                sharing:nil 
264                                                                                                               isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
265                                                                                                               metadata:pithosObject.metadata
266                                                                                                                 update:NO] retain];
267                     [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount];
268                 } else {
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
277                                                                                                                 update:NO] retain];
278                 }
279                 applyMetadataObjectRequest.delegate = self;
280                 applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
281                 applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
282                 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
283             }
284         }
285     }
286 }
287
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];
295             if (sharingAccount)
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];
302         }
303     }
304 }
305
306 #pragma mark -
307 #pragma mark Actions
308
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];
315 }
316
317 @end