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];
73 [pithosObject release];
74 [pithosContainer release];
80 #pragma mark Properties
82 - (void)setPithos:(ASIPithos *)aPithos {
83 if (aPithos && ![aPithos isEqualTo:pithos]) {
85 pithos = [aPithos retain];
93 url = [[NSString alloc] initWithFormat:@"object %@/%@/%@%@",
94 (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL),
97 (shared ? @"?shared" : @"")];
101 - (NSArray *)children {
105 - (NSString *)displayName {
106 if (displayName == nil) {
107 displayName = [pithosObject.name lastPathComponent];
108 if([pithosObject.name hasSuffix:@"/"])
109 displayName = [displayName stringByAppendingString:@"/"];
110 [displayName retain];
112 return [[displayName copy] autorelease];
115 - (void)setDisplayName:(NSString *)aDisplayName {
120 icon = [[[NSWorkspace sharedWorkspace] iconForFileType:[pithosObject.name pathExtension]] retain];
124 - (void)setPithosObject:(ASIPithosObject *)aPithosObject {
125 if (![pithosObject isEqualTo:aPithosObject]) {
126 [pithosObject release];
127 pithosObject = [aPithosObject retain];
129 self.isPublic = (pithosObject.publicURI != nil);
130 // Refresh browser if the object is in my shared and is no longer shared
131 if (shared && !pithosObject.sharing)
132 [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
135 - (void)setLimitedPithosObject:(ASIPithosObject *)aPithosObject {
136 if (![pithosObject isEqualTo:aPithosObject]) {
137 self.pithosObject.subdir = aPithosObject.subdir;
138 self.pithosObject.name = aPithosObject.name;
139 self.pithosObject.hash = aPithosObject.hash;
140 self.pithosObject.objectHash = aPithosObject.objectHash;
141 self.pithosObject.UUID = aPithosObject.UUID;
142 self.pithosObject.bytes = aPithosObject.bytes;
143 self.pithosObject.contentType = aPithosObject.contentType;
144 self.pithosObject.lastModified = aPithosObject.lastModified;
145 self.pithosObject.version = aPithosObject.version;
146 self.pithosObject.versionTimestamp = aPithosObject.versionTimestamp;
147 self.pithosObject.modifiedBy = aPithosObject.modifiedBy;
148 self.pithosObject.sharedBy = aPithosObject.sharedBy;
149 self.pithosObject.allowedTo = aPithosObject.allowedTo;
150 if (!pithosNodeInfoController) {
151 self.pithosObject.sharing = aPithosObject.sharing;
152 self.pithosObject.publicURI = aPithosObject.publicURI;
153 self.pithosObject = pithosObject;
159 #pragma mark ASIHTTPRequestDelegate
161 - (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
162 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
163 NSLog(@"URL: %@", [request url]);
164 NSLog(@"cached: %d", [request didUseCachedResponse]);
166 if ([request isEqualTo:applyMetadataObjectRequest]) {
167 int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
168 if (responseStatusCode != 202)
169 [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
170 @synchronized(self) {
171 [applyMetadataObjectRequest release];
172 applyMetadataObjectRequest = nil;
174 if (responseStatusCode == 202)
176 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
177 [[pithosNodeInfoController window] makeFirstResponder:nil];
178 self.pithosObject = [refreshMetadataObjectRequest object];
179 @synchronized(self) {
180 [refreshMetadataObjectRequest release];
181 refreshMetadataObjectRequest = nil;
187 - (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
188 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
189 NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
191 ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[[PithosUtilities copyRequest:request] autorelease];
192 [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
193 if ([request isEqualTo:applyMetadataObjectRequest]) {
194 @synchronized(self) {
195 [applyMetadataObjectRequest release];
196 applyMetadataObjectRequest = newRequest;
198 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
199 @synchronized(self) {
200 [refreshMetadataObjectRequest release];
201 refreshMetadataObjectRequest = newRequest;
204 [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
206 if ([request isEqualTo:applyMetadataObjectRequest]) {
207 dispatch_async(dispatch_get_main_queue(), ^{
208 [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
210 @synchronized(self) {
211 [applyMetadataObjectRequest release];
212 applyMetadataObjectRequest = nil;
214 } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
215 dispatch_async(dispatch_get_main_queue(), ^{
216 [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
218 @synchronized(self) {
219 [refreshMetadataObjectRequest release];
220 refreshMetadataObjectRequest = nil;
231 @synchronized(self) {
232 if (applyMetadataObjectRequest == nil) {
233 [[pithosNodeInfoController window] makeFirstResponder:nil];
234 if (sharingAccount) {
235 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
236 containerName:pithosContainer.name
237 objectName:pithosObject.name
239 contentDisposition:nil
242 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
243 metadata:pithosObject.metadata
245 [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
247 applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos
248 containerName:pithosContainer.name
249 objectName:pithosObject.name
250 contentEncoding:pithosObject.contentEncoding
251 contentDisposition:pithosObject.contentDisposition
252 manifest:pithosObject.manifest
253 sharing:(pithosObject.sharing ? pithosObject.sharing : @"")
254 isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse)
255 metadata:pithosObject.metadata
258 applyMetadataObjectRequest.delegate = self;
259 applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
260 applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
261 applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
262 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
263 [NSNumber numberWithUnsignedInteger:10], @"retries",
264 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
265 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
267 [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
272 - (void)refreshInfo {
273 @synchronized(self) {
274 if (refreshMetadataObjectRequest == nil) {
275 refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos
276 containerName:pithosContainer.name
277 objectName:pithosObject.name] retain];
279 [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
280 refreshMetadataObjectRequest.delegate = self;
281 refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
282 refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
283 refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
284 [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority",
285 [NSNumber numberWithUnsignedInteger:10], @"retries",
286 NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector",
287 NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector",
289 refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
290 [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
298 - (void)showPithosNodeInfo:(id)sender {
299 if (!pithosNodeInfoController) {
300 pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
303 [pithosNodeInfoController showWindow:sender];
304 [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
305 [NSApp activateIgnoringOtherApps:YES];