Fix memory leaks.
[pithos-macos] / pithos-macos / PithosObjectNode.m
1 //
2 //  PithosObjectNode.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 "PithosObjectNode.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 @implementation PithosObjectNode
49 @synthesize pithos, pithosContainer, pithosObject;
50 @synthesize isPublic;
51
52 #pragma mark -
53 #pragma mark Object Lifecycle
54
55 - (id)initWithPithos:(ASIPithos *)aPithos 
56      pithosContainer:(ASIPithosContainer *)aPithosContainer 
57         pithosObject:(ASIPithosObject *)aPithosObject {
58     if ((self = [super init])) {
59         self.pithos = aPithos;
60         self.pithosContainer = aPithosContainer;
61         self.pithosObject = aPithosObject;
62         isLeafItem = YES;
63     }
64     return self;
65 }
66
67 - (void)dealloc {
68     [refreshMetadataObjectRequest clearDelegatesAndCancel];
69     [refreshMetadataObjectRequest release];
70     [applyMetadataObjectRequest clearDelegatesAndCancel];
71     [applyMetadataObjectRequest release];
72     [versions release];
73     [pithosObject release];
74     [pithosContainer release];
75     [pithos release];
76     [super dealloc];
77 }
78
79 #pragma mark -
80 #pragma mark Properties
81
82 - (void)setPithos:(ASIPithos *)aPithos {
83     if (aPithos && ![aPithos isEqualTo:pithos]) {
84         [pithos release];
85         pithos = [aPithos retain];
86         [url release];
87         url = nil;
88     }
89 }
90
91 - (NSString *)url {
92     if (url == nil)
93         url = [[NSString alloc] initWithFormat:@"object %@/%@/%@%@", 
94                (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL), 
95                pithosContainer.name, 
96                pithosObject.name, 
97                (shared ? @"?shared" : @"")];
98     return url;
99 }
100
101 - (NSArray *)children {
102     return nil;
103 }
104
105 - (NSString *)displayName {
106     if (displayName == nil) {
107         displayName = [pithosObject.name lastPathComponent];
108         if([pithosObject.name hasSuffix:@"/"])
109             displayName = [displayName stringByAppendingString:@"/"];
110         [displayName retain];
111     }
112     return [[displayName copy] autorelease];
113 }
114
115 - (void)setDisplayName:(NSString *)aDisplayName {    
116 }
117
118 - (NSImage *)icon {
119     if (icon == nil)
120         icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
121     return icon;
122 }
123
124 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
125     if (![pithosObject isEqualTo:aPithosObject]) {
126         [pithosObject release];
127         pithosObject = [aPithosObject retain];
128     }
129     self.isPublic = (pithosObject.publicURI != nil);
130     // Refresh browser if the object is in my shared and is no longer shared
131     if (shared && !pithosObject.sharing)
132         [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
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     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
163     NSLog(@"URL: %@", [request url]);
164     NSLog(@"cached: %d", [request didUseCachedResponse]);
165     
166     if ([request isEqualTo:applyMetadataObjectRequest]) {
167         int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
168         if (responseStatusCode != 202)
169             [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
170         @synchronized(self) {
171             [applyMetadataObjectRequest release];
172             applyMetadataObjectRequest = nil;
173         }
174         if (responseStatusCode == 202)
175             [self refreshInfo];
176     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
177         [[pithosNodeInfoController window] makeFirstResponder:nil];
178         self.pithosObject = [refreshMetadataObjectRequest object];
179         @synchronized(self) {
180             [refreshMetadataObjectRequest release];
181             refreshMetadataObjectRequest = nil;
182         }
183     }
184     [pool drain];
185 }
186
187 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
188     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
189     NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
190     if (retries > 0) {
191         ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[[PithosUtilities copyRequest:request] autorelease];
192         [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
193         if ([request isEqualTo:applyMetadataObjectRequest]) {
194             @synchronized(self) {
195                 [applyMetadataObjectRequest release];
196                 applyMetadataObjectRequest = newRequest;
197             }
198         } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
199             @synchronized(self) {
200                 [refreshMetadataObjectRequest release];
201                 refreshMetadataObjectRequest = newRequest;
202             }
203         }
204         [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
205     } else {
206         if ([request isEqualTo:applyMetadataObjectRequest]) {
207             dispatch_async(dispatch_get_main_queue(), ^{
208                 [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
209             });
210             @synchronized(self) {
211                 [applyMetadataObjectRequest release];
212                 applyMetadataObjectRequest = nil;
213             }
214         } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
215             dispatch_async(dispatch_get_main_queue(), ^{
216                 [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
217             });
218             @synchronized(self) {
219                 [refreshMetadataObjectRequest release];
220                 refreshMetadataObjectRequest = nil;
221             }
222         }
223     }
224     [pool drain];
225 }
226
227 #pragma mark -
228 #pragma mark Info
229
230 - (void)applyInfo {
231     @synchronized(self) {
232         if (applyMetadataObjectRequest == nil) {
233             [[pithosNodeInfoController window] makeFirstResponder:nil];
234             if (sharingAccount) {
235                 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos 
236                                                                                               containerName:pithosContainer.name 
237                                                                                                  objectName:pithosObject.name 
238                                                                                             contentEncoding:nil 
239                                                                                          contentDisposition:nil 
240                                                                                                    manifest:nil 
241                                                                                                     sharing:nil
242                                                                                                    isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
243                                                                                                    metadata:pithosObject.metadata
244                                                                                                      update:NO] retain];
245                 [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
246             } else {
247                 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos 
248                                                                                               containerName:pithosContainer.name 
249                                                                                                  objectName:pithosObject.name 
250                                                                                             contentEncoding:pithosObject.contentEncoding 
251                                                                                          contentDisposition:pithosObject.contentDisposition 
252                                                                                                    manifest:pithosObject.manifest 
253                                                                                                     sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
254                                                                                                    isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
255                                                                                                    metadata:pithosObject.metadata
256                                                                                                      update:NO] retain];
257             }
258             applyMetadataObjectRequest.delegate = self;
259             applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
260             applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
261             applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
262                                                    [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
263                                                    [NSNumber numberWithUnsignedInteger:10], @"retries", 
264                                                    NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", 
265                                                    NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", 
266                                                    nil];
267             [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
268         }
269     }
270 }
271
272 - (void)refreshInfo {
273     @synchronized(self) {
274         if (refreshMetadataObjectRequest == nil) {
275             refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos 
276                                                                                       containerName:pithosContainer.name 
277                                                                                          objectName:pithosObject.name] retain];
278             if (sharingAccount)
279                 [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
280             refreshMetadataObjectRequest.delegate = self;
281             refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
282             refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
283             refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
284                                                      [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
285                                                      [NSNumber numberWithUnsignedInteger:10], @"retries", 
286                                                      NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", 
287                                                      NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", 
288                                                      nil];
289             refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
290             [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
291         }
292     }
293 }
294
295 #pragma mark -
296 #pragma mark Actions
297
298 - (void)showPithosNodeInfo:(id)sender {
299     if (!pithosNodeInfoController) {
300         pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
301         [self refreshInfo];
302     }
303     [pithosNodeInfoController showWindow:sender];
304     [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
305     [NSApp activateIgnoringOtherApps:YES];
306 }
307
308 @end