PithosNode http request errors now produce alerts.
[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
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.didFinishSelector = @selector(objectRequestFinished:);
127             applyMetadataObjectRequest.didFailSelector = @selector(objectRequestFailed:);
128             [applyMetadataObjectRequest startAsynchronous];
129         }
130     }
131 }
132
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];
143         }
144     }
145 }
146
147 #pragma mark -
148 #pragma mark ASIHTTPRequestDelegate
149
150 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
151     NSLog(@"URL: %@", [request url]);
152     NSLog(@"cached: %d", [request didUseCachedResponse]);
153     
154     if ([request isEqualTo:applyMetadataObjectRequest]) {
155         @synchronized(self) {
156             [applyMetadataObjectRequest release];
157             applyMetadataObjectRequest = nil;
158         }
159         [self refreshInfo];
160     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
161         self.pithosObject = [refreshMetadataObjectRequest object];
162         @synchronized(self) {
163             [refreshMetadataObjectRequest release];
164             refreshMetadataObjectRequest = nil;
165         }
166     }
167 }
168
169 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
170     if ([request isEqualTo:applyMetadataObjectRequest]) {
171         [PithosFileUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
172         @synchronized(self) {
173             [applyMetadataObjectRequest release];
174             applyMetadataObjectRequest = nil;
175         }
176     } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
177         [PithosFileUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
178         @synchronized(self) {
179             [refreshMetadataObjectRequest release];
180             refreshMetadataObjectRequest = nil;
181         }
182     }
183 }
184
185 @end