'my shared' nodes can be copied.
[pithos-macos] / pithos-macos / PithosObjectNode.m
1 //
2 //  PithosObjectNode.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 "PithosFileUtilities.h"
45 #import "PithosObjectNodeInfoController.h"
46
47 @implementation PithosObjectNode
48 @synthesize pithosContainer, pithosObject;
49 @synthesize isPublic;
50
51 #pragma mark -
52 #pragma mark Object Lifecycle
53
54 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
55     if ((self = [super init])) {
56         self.pithosContainer = aPithosContainer;
57         self.pithosObject = aPithosObject;
58         isLeafItem = YES;
59     }
60     return self;
61 }
62
63 - (void)dealloc {
64     [refreshMetadataObjectRequest clearDelegatesAndCancel];
65     [refreshMetadataObjectRequest release];
66     [applyMetadataObjectRequest clearDelegatesAndCancel];
67     [applyMetadataObjectRequest release];
68     [pithosObject release];
69     [pithosContainer release];
70     [super dealloc];
71 }
72
73 #pragma mark -
74 #pragma mark Properties
75
76 - (NSString *)url {
77     if (url == nil) 
78         url = [[NSString alloc] initWithFormat:@"object %@/%@/%@", [ASIPithosRequest storageURL], pithosContainer.name, pithosObject.name];
79     return url;
80 }
81
82 - (NSArray *)children {
83     return nil;
84 }
85
86 - (NSString *)displayName {
87     if (displayName == nil) {
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 - (void)setDisplayName:(NSString *)aDisplayName {    
97 }
98
99 - (NSImage *)icon {
100     if (icon == nil)
101         icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
102     return icon;
103 }
104
105 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
106     if (![pithosObject isEqualTo:aPithosObject]) {
107         [pithosObject release];
108         pithosObject = [aPithosObject retain];
109     }
110     self.isPublic = (pithosObject.publicURI != nil);
111     // Refresh browser if the object is in my shared and is no longer shared
112     if (shared && !pithosObject.sharing)
113         [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
114 }
115
116 #pragma mark -
117 #pragma mark ASIHTTPRequestDelegate
118
119 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
120     NSLog(@"URL: %@", [request url]);
121     NSLog(@"cached: %d", [request didUseCachedResponse]);
122     
123     if ([request isEqualTo:applyMetadataObjectRequest]) {
124         int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
125         if (responseStatusCode != 202)
126             [PithosFileUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
127         @synchronized(self) {
128             [applyMetadataObjectRequest release];
129             applyMetadataObjectRequest = nil;
130         }
131         if (responseStatusCode == 202)
132             [self refreshInfo];
133     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
134         [[pithosNodeInfoController window] makeFirstResponder:nil];
135         self.pithosObject = [refreshMetadataObjectRequest object];
136         @synchronized(self) {
137             [refreshMetadataObjectRequest release];
138             refreshMetadataObjectRequest = nil;
139         }
140     }
141 }
142
143 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
144     if ([request isEqualTo:applyMetadataObjectRequest]) {
145         [PithosFileUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
146         @synchronized(self) {
147             [applyMetadataObjectRequest release];
148             applyMetadataObjectRequest = nil;
149         }
150     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
151         [PithosFileUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
152         @synchronized(self) {
153             [refreshMetadataObjectRequest release];
154             refreshMetadataObjectRequest = nil;
155         }
156     }
157 }
158
159 #pragma mark -
160 #pragma mark Info
161
162 - (void)applyInfo {
163     @synchronized(self) {
164         if (applyMetadataObjectRequest == nil) {
165             [[pithosNodeInfoController window] makeFirstResponder:nil];
166             applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name 
167                                                                                                     objectName:pithosObject.name 
168                                                                                                contentEncoding:pithosObject.contentEncoding 
169                                                                                             contentDisposition:pithosObject.contentDisposition 
170                                                                                                       manifest:pithosObject.manifest 
171                                                                                                        sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
172                                                                                                       isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
173                                                                                                       metadata:pithosObject.metadata
174                                                                                                         update:NO] retain];
175             applyMetadataObjectRequest.delegate = self;
176             applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
177             applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
178             [applyMetadataObjectRequest startAsynchronous];
179         }
180     }
181 }
182
183 - (void)refreshInfo {
184     @synchronized(self) {
185         if (refreshMetadataObjectRequest == nil) {
186             refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name 
187                                                                                                 objectName:pithosObject.name] retain];
188             refreshMetadataObjectRequest.delegate = self;
189             refreshMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
190             refreshMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
191             refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
192             [refreshMetadataObjectRequest startAsynchronous];
193         }
194     }
195 }
196
197 #pragma mark -
198 #pragma mark Actions
199
200 - (void)showPithosNodeInfo:(id)sender {
201     if (!pithosNodeInfoController)
202         pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
203     [pithosNodeInfoController showWindow:sender];
204     [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
205     [NSApp activateIgnoringOtherApps:YES];
206 }
207
208 @end