Statistics
| Branch: | Tag: | Revision:

root / pithos-macos / PithosSubdirNode.m @ d8426ffb

History | View | Annotate | Download (20.6 kB)

1
//
2
//  PithosSubdirNode.m
3
//  pithos-macos
4
//
5
// Copyright 2011-2012 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 "PithosSubdirNode.h"
39
#import "ASIPithosRequest.h"
40
#import "ASIPithos.h"
41
#import "ASIPithosObjectRequest.h"
42
#import "ASIPithosContainer.h"
43
#import "ASIPithosObject.h"
44
#import "ASINetworkQueue.h"
45
#import "ASIDownloadCache.h"
46
#import "PithosUtilities.h"
47
#import "PithosObjectNodeInfoController.h"
48

    
49
static NSImage *sharedIcon = nil;
50

    
51
@implementation PithosSubdirNode
52
@synthesize pithosObject;
53
@synthesize isPublic;
54

    
55
+ (void)initialize {
56
	if (self == [PithosSubdirNode class])
57
        sharedIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] retain];
58
}
59

    
60
#pragma mark -
61
#pragma mark Object Lifecycle
62

    
63
- (id)initWithPithos:(ASIPithos *)aPithos 
64
     pithosContainer:(ASIPithosContainer *)aPithosContainer 
65
        pithosObject:(ASIPithosObject *)aPithosObject {
66
    if ((self = [super init])) {
67
        self.pithos = aPithos;
68
        self.pithosContainer = aPithosContainer;
69
        self.pithosObject = aPithosObject;
70
        childrenUpdatedNotificationName = @"PithosSubdirNodeChildrenUpdated";
71
        refreshParent = NO;
72
    }
73
    return self;
74
}
75

    
76
- (void)dealloc {
77
    [refreshMetadataObjectRequest clearDelegatesAndCancel];
78
    [refreshMetadataObjectRequest release];
79
    [applyMetadataObjectRequest clearDelegatesAndCancel];
80
    [applyMetadataObjectRequest release];
81
    [pithosObject release];
82
    [super dealloc];
83
}
84

    
85
#pragma mark -
86
#pragma mark Properties
87

    
88
- (void)setPithos:(ASIPithos *)aPithos {
89
    if (aPithos && ![aPithos isEqualTo:pithos]) {
90
        [pithos release];
91
        pithos = [aPithos retain];
92
        [url release];
93
        url = nil;
94
    }
95
}
96

    
97
- (NSString *)url {
98
    if (url == nil)
99
        url = [[NSString alloc] initWithFormat:@"subdir %@/%@/%@%@", 
100
               (sharingAccount ? [pithos storageURLWithAuthUser:sharingAccount] : pithos.storageURL), 
101
               pithosContainer.name, 
102
               prefix, 
103
               (shared ? @"?shared" : @"")];
104
    return url;
105
}
106

    
107
- (NSString *)displayName {
108
    if (displayName == nil) {
109
        displayName = [pithosObject.name lastPathComponent];
110
        if (!pithosObject.subdir && [pithosObject.name hasSuffix:@"/"])
111
            displayName = [displayName stringByAppendingString:@"/"];
112
        [displayName retain];
113
    } 
114
    return [[displayName copy] autorelease];
115
}
116

    
117
- (void)setDisplayName:(NSString *)aDisplayName {    
118
}
119

    
120
- (NSImage *)icon {
121
    if (icon == nil)
122
        icon = [sharedIcon retain];
123
    return icon;
124
}
125

    
126
- (void)setPithosObject:(ASIPithosObject *)aPithosObject {
127
    if (![pithosObject isEqualTo:aPithosObject]) {
128
        [pithosObject release];
129
        pithosObject = [aPithosObject retain];
130
    }
131
    if (pithosObject.subdir) {
132
        self.prefix = [pithosObject.name substringToIndex:([pithosObject.name length] - 1)];
133
    } else {
134
        self.prefix = [NSString stringWithString:pithosObject.name];
135
    }
136
    self.isPublic = (pithosObject.publicURI != nil);
137
    // Refresh browser if the object is in my shared and is no longer shared
138
    if (shared && !pithosObject.subdir && !pithosObject.sharing) {
139
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self];
140
    }
141
}
142

    
143
#pragma mark -
144
#pragma mark ASIHTTPRequestDelegate
145

    
146
- (void)objectRequestFinished:(ASIPithosObjectRequest *)request {
147
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
148
    NSLog(@"URL: %@", [request url]);
149
    NSLog(@"cached: %d", [request didUseCachedResponse]);
150
    
151
    if ([request isEqualTo:applyMetadataObjectRequest]) {
152
        int responseStatusCode = applyMetadataObjectRequest.responseStatusCode;
153
        if ((responseStatusCode != 201) && (responseStatusCode != 202))
154
            [PithosUtilities unexpectedResponseStatusAlertWithRequest:applyMetadataObjectRequest];
155
        @synchronized(self) {
156
            [applyMetadataObjectRequest release];
157
            applyMetadataObjectRequest = nil;
158
        }
159
        if ((responseStatusCode == 201) || (responseStatusCode == 202))
160
            [self refreshInfo];
161
    } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
162
        [[pithosNodeInfoController window] makeFirstResponder:nil];
163
        self.pithosObject = [refreshMetadataObjectRequest object];
164
        if (refreshParent) {
165
            // Ask the parent for refresh for the case where an object was removed
166
            // It is done here so that it doesn't affect the info window refresh
167
            [parent refresh];
168
            refreshParent = NO;
169
        }
170
        @synchronized(self) {
171
            [refreshMetadataObjectRequest release];
172
            refreshMetadataObjectRequest = nil;
173
        }
174
    }
175
    [pool drain];
176
}
177

    
178
- (void)objectRequestFailed:(ASIPithosObjectRequest *)request {
179
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
180
    NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
181
    if (retries > 0) {
182
        ASIPithosObjectRequest *newRequest = (ASIPithosObjectRequest *)[PithosUtilities copyRequest:request];
183
        [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
184
        if ([request isEqualTo:applyMetadataObjectRequest]) {
185
            @synchronized(self) {
186
                [applyMetadataObjectRequest release];
187
                applyMetadataObjectRequest = newRequest;
188
            }
189
        } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
190
            @synchronized(self) {
191
                [refreshMetadataObjectRequest release];
192
                refreshMetadataObjectRequest = newRequest;
193
            }
194
        }
195
        [[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]] startAsynchronous];
196
    } else {
197
        if ([request isEqualTo:applyMetadataObjectRequest]) {
198
            dispatch_async(dispatch_get_main_queue(), ^{
199
                [PithosUtilities httpRequestErrorAlertWithRequest:applyMetadataObjectRequest];
200
            });
201
            @synchronized(self) {
202
                [applyMetadataObjectRequest release];
203
                applyMetadataObjectRequest = nil;
204
            }
205
        } else if ([request isEqualTo:refreshMetadataObjectRequest]) {
206
            dispatch_async(dispatch_get_main_queue(), ^{
207
                [PithosUtilities httpRequestErrorAlertWithRequest:refreshMetadataObjectRequest];
208
            });
209
            @synchronized(self) {
210
                [refreshMetadataObjectRequest release];
211
                refreshMetadataObjectRequest = nil;
212
            }
213
        }
214
    }
215
    [pool drain];
216
}
217

    
218
#pragma mark -
219
#pragma mark Info
220

    
221
- (void)applyInfo {
222
    @synchronized(self) {
223
        if (applyMetadataObjectRequest == nil) {
224
            if (pithosObject.subdir) {
225
                BOOL createObject = NO;
226
                NSAlert *alert;
227
                ASIPithosObjectRequest *request = [ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos 
228
                                                                                            containerName:pithosContainer.name 
229
                                                                                               objectName:prefix];
230
                ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
231
                [networkQueue go];
232
                [networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES];
233
                if ([request error]) {
234
                    alert = [[[NSAlert alloc] init] autorelease];
235
                    [alert setMessageText:@"HTTP Request Error"];
236
                    [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
237
                    [alert addButtonWithTitle:@"OK"];
238
                    [alert runModal];
239
                    return;
240
                } else if (request.responseStatusCode == 200) {
241
                    alert = [[[NSAlert alloc] init] autorelease];
242
                    [alert setMessageText:@"Apply changes"];
243
                    [alert setInformativeText:[NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?", self.displayName]];
244
                    [alert addButtonWithTitle:@"OK"];
245
                    [alert addButtonWithTitle:@"Cancel"];
246
                    NSInteger choice = [alert runModal];
247
                    if (choice == NSAlertFirstButtonReturn) {
248
                        request = [ASIPithosObjectRequest deleteObjectRequestWithPithos:pithos 
249
                                                                          containerName:pithosContainer.name 
250
                                                                             objectName:prefix];
251
                        ASINetworkQueue *networkQueue = [ASINetworkQueue queue];
252
                        [networkQueue go];
253
                        [networkQueue addOperations:[NSArray arrayWithObject:[PithosUtilities prepareRequest:request]] waitUntilFinished:YES];
254
                        if ([request error]) {
255
                            alert = [[[NSAlert alloc] init] autorelease];
256
                            [alert setMessageText:@"HTTP Request Error"];
257
                            [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [request error]]];
258
                            [alert addButtonWithTitle:@"OK"];
259
                            [alert runModal];
260
                            return;
261
                        } else if (request.responseStatusCode != 204) {
262
                            [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
263
                            return;
264
                        }
265
                        refreshParent = YES;
266
                        createObject = YES;
267
                    } else {
268
                        return;
269
                    }
270
                } else if (request.responseStatusCode == 404) {
271
                    createObject = YES;
272
                } else {
273
                    [PithosUtilities unexpectedResponseStatusAlertWithRequest:request];
274
                    return;
275
                }
276
                if (createObject) {
277
                    [[pithosNodeInfoController window] makeFirstResponder:nil];
278
                    applyMetadataObjectRequest = [[ASIPithosObjectRequest writeObjectDataRequestWithPithos:pithos 
279
                                                                                             containerName:pithosContainer.name 
280
                                                                                                objectName:prefix 
281
                                                                                                      eTag:nil
282
                                                                                               contentType:@"application/directory"
283
                                                                                           contentEncoding:pithosObject.contentEncoding 
284
                                                                                        contentDisposition:pithosObject.contentDisposition 
285
                                                                                                  manifest:pithosObject.manifest 
286
                                                                                                   sharing:pithosObject.sharing 
287
                                                                                                  isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
288
                                                                                                  metadata:pithosObject.metadata 
289
                                                                                                      data:[NSData data]] retain];
290
                    pithosObject.subdir = NO;
291
                    applyMetadataObjectRequest.delegate = self;
292
                    applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
293
                    applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
294
                    applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
295
                                                           [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
296
                                                           [NSNumber numberWithUnsignedInteger:10], @"retries", 
297
                                                           NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", 
298
                                                           NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", 
299
                                                           nil];
300
                    [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
301
                }
302
            } else {
303
                [[pithosNodeInfoController window] makeFirstResponder:nil];
304
                if (sharingAccount) {
305
                    applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos 
306
                                                                                                  containerName:pithosContainer.name 
307
                                                                                                     objectName:pithosObject.name 
308
                                                                                                contentEncoding:nil
309
                                                                                             contentDisposition:nil
310
                                                                                                       manifest:nil 
311
                                                                                                        sharing:nil 
312
                                                                                                       isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
313
                                                                                                       metadata:pithosObject.metadata
314
                                                                                                         update:NO] retain];
315
                    [applyMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
316
                } else {
317
                    applyMetadataObjectRequest = [[ASIPithosObjectRequest updateObjectMetadataRequestWithPithos:pithos 
318
                                                                                                  containerName:pithosContainer.name 
319
                                                                                                     objectName:pithosObject.name 
320
                                                                                                contentEncoding:pithosObject.contentEncoding
321
                                                                                             contentDisposition:pithosObject.contentDisposition 
322
                                                                                                       manifest:pithosObject.manifest 
323
                                                                                                        sharing:(pithosObject.sharing ? pithosObject.sharing : @"") 
324
                                                                                                       isPublic:(isPublic ? ASIPithosObjectRequestPublicTrue : ASIPithosObjectRequestPublicFalse) 
325
                                                                                                       metadata:pithosObject.metadata
326
                                                                                                         update:NO] retain];
327
                }
328
                applyMetadataObjectRequest.delegate = self;
329
                applyMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
330
                applyMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
331
                applyMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
332
                                                       [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
333
                                                       [NSNumber numberWithUnsignedInteger:10], @"retries", 
334
                                                       NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", 
335
                                                       NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", 
336
                                                       nil];
337
                [[PithosUtilities prepareRequest:applyMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
338
            }
339
        }
340
    }
341
}
342

    
343
- (void)refreshInfo {
344
    @synchronized(self) {
345
        if (pithosObject.subdir) {
346
            self.pithosObject = [ASIPithosObject subdirWithName:pithosObject.name];
347
        } else if (refreshMetadataObjectRequest == nil) {
348
            refreshMetadataObjectRequest = [[ASIPithosObjectRequest objectMetadataRequestWithPithos:pithos 
349
                                                                                      containerName:pithosContainer.name 
350
                                                                                         objectName:prefix] retain];
351
            if (sharingAccount)
352
                [refreshMetadataObjectRequest setRequestUserFromDefaultTo:sharingAccount withPithos:pithos];
353
            refreshMetadataObjectRequest.delegate = self;
354
            refreshMetadataObjectRequest.didFinishSelector = @selector(performRequestFinishedDelegateInBackground:);
355
            refreshMetadataObjectRequest.didFailSelector = @selector(performRequestFailedDelegateInBackground:);
356
            refreshMetadataObjectRequest.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
357
                                                     [NSNumber numberWithInteger:NSOperationQueuePriorityHigh], @"priority", 
358
                                                     [NSNumber numberWithUnsignedInteger:10], @"retries", 
359
                                                     NSStringFromSelector(@selector(objectRequestFinished:)), @"didFinishSelector", 
360
                                                     NSStringFromSelector(@selector(objectRequestFailed:)), @"didFailSelector", 
361
                                                     nil];
362
            refreshMetadataObjectRequest.downloadCache = [ASIDownloadCache sharedCache];
363
            [[PithosUtilities prepareRequest:refreshMetadataObjectRequest priority:NSOperationQueuePriorityHigh] startAsynchronous];
364
        }
365
    }
366
}
367

    
368
#pragma mark -
369
#pragma mark Actions
370

    
371
- (void)showPithosNodeInfo:(id)sender {
372
    if (!pithosNodeInfoController)
373
        pithosNodeInfoController = [[PithosObjectNodeInfoController alloc] initWithPithosNode:self];
374
    [pithosNodeInfoController showWindow:sender];
375
    [[pithosNodeInfoController window] makeKeyAndOrderFront:sender];
376
    [NSApp activateIgnoringOtherApps:YES];
377
}
378

    
379
@end