5 // Copyright 2011-2012 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"
41 #import "ASIPithosObjectRequest.h"
42 #import "ASIPithosContainer.h"
43 #import "ASIPithosObject.h"
44 #import "ASIDownloadCache.h"
45 #import "PithosUtilities.h"
46 #import "PithosObjectNodeInfoController.h"
48 @implementation PithosObjectNode
49 @synthesize pithos, pithosContainer, pithosObject;
53 #pragma mark Object Lifecycle
55 - (id)initWithPithos:(ASIPithos *)aPithos
56 pithosContainer:(ASIPithosContainer *)aPithosContainer
57 pithosObject:(ASIPithosObject *)aPithosObject {
58 if ((self = [super init])) {
59 self.pithos = aPithos;
60 self.pithosContainer = aPithosContainer;
61 self.pithosObject = aPithosObject;
68 [refreshMetadataObjectRequest clearDelegatesAndCancel];
69 [refreshMetadataObjectRequest release];
70 [applyMetadataObjectRequest clearDelegatesAndCancel];
71 [applyMetadataObjectRequest release];
72 [pithosObject release];
73 [pithosContainer release];
79 #pragma mark Properties
81 - (void)setPithos:(ASIPithos *)aPithos {
82 if (aPithos && ![aPithos isEqualTo:pithos]) {
84 pithos = [aPithos retain];
92 url = [[NSString alloc] initWithFormat:@"object %@/%@/%@%@",
93 (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL),
96 (shared ? @"?shared" : @"")];
100 - (NSArray *)children {
104 - (NSString *)displayName {
105 if (displayName == nil) {
106 displayName = [pithosObject.name lastPathComponent];
107 if([pithosObject.name hasSuffix:@"/"])
108 displayName = [displayName stringByAppendingString:@"/"];
109 [displayName retain];
111 return [[displayName copy] autorelease];
114 - (void)setDisplayName:(NSString *)aDisplayName {
119 icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
123 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
124 if (![pithosObject isEqualTo:aPithosObject]) {
125 [pithosObject release];
126 pithosObject = [aPithosObject retain];
128 self.isPublic = (pithosObject.publicURI != nil);
129 // Refresh browser if the object is in my shared and is no longer shared
130 if (shared && !pithosObject.sharing)
131 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
135 #pragma mark ASIHTTPRequestDelegate
137 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
138 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
139 NSLog(@"URL: %@", [request url]);
140 NSLog(@"cached: %d", [request didUseCachedResponse]);
142 if ([request isEqualTo:applyMetadataObjectRequest]) {
143 int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
144 if (responseStatusCode != 202)
145 [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
146 @synchronized(self) {
147 [applyMetadataObjectRequest release];
148 applyMetadataObjectRequest = nil;
150 if (responseStatusCode == 202)
152 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
153 [[pithosNodeInfoController window] makeFirstResponder:nil];
154 self.pithosObject = [refreshMetadataObjectRequest object];
155 @synchronized(self) {
156 [refreshMetadataObjectRequest release];
157 refreshMetadataObjectRequest = nil;
163 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
164 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
165 NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
167 ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[PithosUtilities copyRequest:request];
168 [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
169 if ([request isEqualTo:applyMetadataObjectRequest]) {
170 @synchronized(self) {
171 [applyMetadataObjectRequest release];
172 applyMetadataObjectRequest = newRequest;
174 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
175 @synchronized(self) {
176 [refreshMetadataObjectRequest release];
177 refreshMetadataObjectRequest = newRequest;
180 [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
182 if ([request isEqualTo:applyMetadataObjectRequest]) {
183 dispatch_async(dispatch_get_main_queue(), ^{
184 [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
186 @synchronized(self) {
187 [applyMetadataObjectRequest release];
188 applyMetadataObjectRequest = nil;
190 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
191 dispatch_async(dispatch_get_main_queue(), ^{
192 [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
194 @synchronized(self) {
195 [refreshMetadataObjectRequest release];
196 refreshMetadataObjectRequest = nil;
207 @synchronized(self) {
208 if (applyMetadataObjectRequest == nil) {
209 [[pithosNodeInfoController window] makeFirstResponder:nil];
210 if (sharingAccount) {
211 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
212 containerName:pithosContainer.name
213 objectName:pithosObject.name
215 contentDisposition:nil
218 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
219 metadata:pithosObject.metadata
221 [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
223 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
224 containerName:pithosContainer.name
225 objectName:pithosObject.name
226 contentEncoding:pithosObject.contentEncoding
227 contentDisposition:pithosObject.contentDisposition
228 manifest:pithosObject.manifest
229 sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
230 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
231 metadata:pithosObject.metadata
234 applyMetadataObjectRequest.delegate = self;
235 applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
236 applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
237 applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
238 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
239 [NSNumber numberWithUnsignedInteger:10], @"retries",
240 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
241 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
243 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
248 - (void)refreshInfo {
249 @synchronized(self) {
250 if (refreshMetadataObjectRequest == nil) {
251 refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos
252 containerName:pithosContainer.name
253 objectName:pithosObject.name] retain];
255 [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
256 refreshMetadataObjectRequest.delegate = self;
257 refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
258 refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
259 refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
260 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
261 [NSNumber numberWithUnsignedInteger:10], @"retries",
262 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
263 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
265 refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
266 [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
274 - (void)showPithosNodeInfo:(id)sender {
275 if (!pithosNodeInfoController)
276 pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
277 [pithosNodeInfoController showWindow:sender];
278 [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
279 [NSApp activateIgnoringOtherApps:YES];