5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
38 #import "PithosObjectNode.h"
39 #import "ASIPithosRequest.h"
40 #import "ASIPithosObjectRequest.h"
41 #import "ASIPithosContainer.h"
42 #import "ASIPithosObject.h"
43 #import "ASIDownloadCache.h"
45 @implementation PithosObjectNode
46 @synthesize pithosContainer, pithosObject;
50 #pragma mark Object Lifecycle
52 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
53 if ((self = [super init])) {
54 self.pithosContainer = aPithosContainer;
55 self.pithosObject = aPithosObject;
62 [refreshMetadataObjectRequest clearDelegatesAndCancel];
63 [refreshMetadataObjectRequest release];
64 [applyMetadataObjectRequest clearDelegatesAndCancel];
65 [applyMetadataObjectRequest release];
66 [pithosObject release];
67 [pithosContainer release];
72 #pragma mark Properties
76 url = [[NSString alloc] initWithFormat:@"object %@/%@/%@", [ASIPithosRequest storageURL], pithosContainer.name, pithosObject.name];
80 - (NSArray *)children {
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:@"/"];
92 return [[displayName copy] autorelease];
97 icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
101 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
102 if (![pithosObject isEqualTo:aPithosObject]) {
103 [pithosObject release];
104 pithosObject = [aPithosObject retain];
106 self.isPublic = (pithosObject.publicURI != nil);
113 @synchronized(self) {
114 if (applyMetadataObjectRequest == nil) {
115 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name
116 objectName:pithosObject.name
118 contentDisposition:nil
120 sharing:pithosObject.sharing
121 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
122 metadata:pithosObject.metadata
124 applyMetadataObjectRequest.delegate = self;
125 applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
126 applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
127 [applyMetadataObjectRequest startAsynchronous];
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];
147 #pragma mark ASIHTTPRequestDelegate
149 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
150 NSLog(@"URL: %@", [request url]);
151 NSLog(@"cached: %d", [request didUseCachedResponse]);
153 if ([request isEqualTo:applyMetadataObjectRequest]) {
154 @synchronized(self) {
155 [applyMetadataObjectRequest release];
156 applyMetadataObjectRequest = nil;
159 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
160 self.pithosObject = [refreshMetadataObjectRequest object];
161 @synchronized(self) {
162 [refreshMetadataObjectRequest release];
163 refreshMetadataObjectRequest = nil;
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;
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;