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