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"
44 #import "PithosFileUtilities.h"
46 @implementation PithosObjectNode
47 @synthesize pithosContainer, pithosObject;
51 #pragma mark Object Lifecycle
53 - (id)initWithPithosContainer:(ASIPithosContainer *)aPithosContainer pithosObject:(ASIPithosObject *)aPithosObject {
54 if ((self = [super init])) {
55 self.pithosContainer = aPithosContainer;
56 self.pithosObject = aPithosObject;
63 [refreshMetadataObjectRequest clearDelegatesAndCancel];
64 [refreshMetadataObjectRequest release];
65 [applyMetadataObjectRequest clearDelegatesAndCancel];
66 [applyMetadataObjectRequest release];
67 [pithosObject release];
68 [pithosContainer release];
73 #pragma mark Properties
77 url = [[NSString alloc] initWithFormat:@"object %@/%@/%@", [ASIPithosRequest storageURL], pithosContainer.name, pithosObject.name];
81 - (NSArray *)children {
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:@"/"];
93 return [[displayName copy] autorelease];
98 icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
102 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
103 if (![pithosObject isEqualTo:aPithosObject]) {
104 [pithosObject release];
105 pithosObject = [aPithosObject retain];
107 self.isPublic = (pithosObject.publicURI != nil);
114 @synchronized(self) {
115 if (applyMetadataObjectRequest == nil) {
116 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithContainerName:pithosContainer.name
117 objectName:pithosObject.name
119 contentDisposition:nil
121 sharing:pithosObject.sharing
122 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
123 metadata:pithosObject.metadata
125 applyMetadataObjectRequest.delegate = self;
126 applyMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
127 applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
128 [applyMetadataObjectRequest startAsynchronous];
133 - (void)refreshInfo {
134 @synchronized(self) {
135 if (refreshMetadataObjectRequest == nil) {
136 refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithContainerName:pithosContainer.name
137 objectName:pithosObject.name] retain];
138 refreshMetadataObjectRequest.delegate = self;
139 refreshMetadataObjectRequest.didFinishSelector = @selector(objectRequestFinished:);
140 refreshMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
141 refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
142 [refreshMetadataObjectRequest startAsynchronous];
148 #pragma mark ASIHTTPRequestDelegate
150 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
151 NSLog(@"URL: %@", [request url]);
152 NSLog(@"cached: %d", [request didUseCachedResponse]);
154 if ([request isEqualTo:applyMetadataObjectRequest]) {
155 @synchronized(self) {
156 [applyMetadataObjectRequest release];
157 applyMetadataObjectRequest = nil;
160 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
161 self.pithosObject = [refreshMetadataObjectRequest object];
162 @synchronized(self) {
163 [refreshMetadataObjectRequest release];
164 refreshMetadataObjectRequest = nil;
169 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
170 if ([request isEqualTo:applyMetadataObjectRequest]) {
171 [PithosFileUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
172 @synchronized(self) {
173 [applyMetadataObjectRequest release];
174 applyMetadataObjectRequest = nil;
176 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
177 [PithosFileUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
178 @synchronized(self) {
179 [refreshMetadataObjectRequest release];
180 refreshMetadataObjectRequest = nil;