Sharing in the object info window can be updated.
[pithos-macos] / pithos-macos / PithosObjectNode.m
1 //
2 //  PithosNode.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 "PithosObjectNode.h"
39 #import "ASIPithosRequest.h"
40 #import "ASIPithosObjectRequest.h"
41 #import "ASIPithosContainer.h"
42 #import "ASIPithosObject.h"
43 #import "ASIDownloadCache.h"
44 #import "PithosNodeInfoController.h"
45
46 @implementation PithosObjectNode
47 @synthesize pithosContainer, pithosObject;
48 @synthesize isPublic;
49
50 #pragma mark -
51 #pragma mark Object Lifecycle
52
53 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
54     if ((self = [super init])) {
55         self.pithosContainer = aPithosContainer;
56         self.pithosObject = aPithosObject;
57         isLeafItem = YES;
58     }
59     return self;
60 }
61
62 - (void)dealloc {
63     [refreshMetadataObjectRequest clearDelegatesAndCancel];
64     [refreshMetadataObjectRequest release];
65     [applyMetadataObjectRequest clearDelegatesAndCancel];
66     [applyMetadataObjectRequest release];
67     [pithosObject release];
68     [pithosContainer release];
69     [super dealloc];
70 }
71
72 #pragma mark -
73 #pragma mark Properties
74
75 - (NSString *)url {
76     if (url == nil) 
77         url = [[NSString alloc] initWithFormat:@"object %@/%@/%@", [ASIPithosRequest storageURL], pithosContainer.name, pithosObject.name];
78     return url;
79 }
80
81 - (NSArray *)children {
82     return nil;
83 }
84
85 - (NSString *)displayName {
86     if (displayName == nil) {
87         // XXX check if there are problems with . or other special characters
88         displayName = [pithosObject.name lastPathComponent];
89         if([pithosObject.name hasSuffix:@"/"])
90             displayName = [displayName stringByAppendingString:@"/"];
91         [displayName retain];
92     }
93     return [[displayName copy] autorelease];
94 }
95
96 - (NSImage *)icon {
97     if (icon == nil)
98         icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
99     return icon;
100 }
101
102 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
103     if (![pithosObject isEqualTo:aPithosObject]) {
104         [pithosObject release];
105         pithosObject = [aPithosObject retain];
106     }
107     self.isPublic = (pithosObject.publicURI != nil);
108 }
109
110 #pragma mark -
111 #pragma mark Info
112
113 - (void)applyInfo {
114     @synchronized(self) {
115         if (applyMetadataObjectRequest == nil) {
116             applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name 
117                                                                                                     objectName:pithosObject.name 
118                                                                                                contentEncoding:nil
119                                                                                             contentDisposition:nil 
120                                                                                                       manifest:nil 
121                                                                                                        sharing:pithosObject.sharing 
122                                                                                                       isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
123                                                                                                       metadata:pithosObject.metadata
124                                                                                                         update:NO] retain];
125             applyMetadataObjectRequest.delegate = self;
126             [applyMetadataObjectRequest startAsynchronous];
127         }
128     }
129 }
130
131 - (void)refreshInfo {
132     @synchronized(self) {
133         if (refreshMetadataObjectRequest == nil) {
134             refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name 
135                                                                                                 objectName:pithosObject.name] retain];
136             refreshMetadataObjectRequest.delegate = self;
137             refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
138             [refreshMetadataObjectRequest startAsynchronous];
139         }
140     }
141 }
142
143 #pragma mark -
144 #pragma mark ASIHTTPRequestDelegate
145
146 - (void)requestFinished:(ASIHTTPRequest *)request {
147     NSLog(@"URL: %@", [request url]);
148     NSLog(@"cached: %d", [request didUseCachedResponse]);
149     
150     if ([request isEqualTo:applyMetadataObjectRequest]) {
151         @synchronized(self) {
152             [applyMetadataObjectRequest release];
153             applyMetadataObjectRequest = nil;
154         }
155         [self refreshInfo];
156     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
157         self.pithosObject = [refreshMetadataObjectRequest object];
158         @synchronized(self) {
159             [refreshMetadataObjectRequest release];
160             refreshMetadataObjectRequest = nil;
161         }
162     }
163 }
164
165 - (void)requestFailed:(ASIHTTPRequest *)request {
166     if ([request isEqualTo:applyMetadataObjectRequest]) {
167         // XXX do something on error, cleanup
168         NSLog(@"error:%@", [applyMetadataObjectRequest error]);
169         @synchronized(self) {
170             [applyMetadataObjectRequest release];
171             applyMetadataObjectRequest = nil;
172         }
173     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
174         // XXX do something on error, cleanup
175         NSLog(@"error:%@", [refreshMetadataObjectRequest error]);
176         @synchronized(self) {
177             [refreshMetadataObjectRequest release];
178             refreshMetadataObjectRequest = nil;
179         }
180     }
181 }
182
183 @end