root / pithos-macos / PithosSubdir.m @ d8d08dc1
History | View | Annotate | Download (6.6 kB)
1 |
// |
---|---|
2 |
// PithosNode.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 "PithosSubdir.h" |
39 |
#import "PithosObject.h" |
40 |
#import "ASIPithosContainerRequest.h" |
41 |
#import "ASIPithosObject.h" |
42 |
|
43 |
@implementation PithosSubdir |
44 |
@synthesize containerName, subdirName; |
45 |
|
46 |
#pragma mark - |
47 |
#pragma mark Object Lifecycle |
48 |
|
49 |
- (id)initWithContainerName:(NSString *)aContainerName subdirName:(NSString *)aSubdirName { |
50 |
if ((self = [super init])) { |
51 |
containerName = [aContainerName copy]; |
52 |
subdirName = [aSubdirName copy]; |
53 |
} |
54 |
return self; |
55 |
} |
56 |
- (void)dealloc { |
57 |
[objects release]; |
58 |
[displayName release]; |
59 |
[subdirName release]; |
60 |
[containerName release]; |
61 |
[super dealloc]; |
62 |
} |
63 |
|
64 |
#pragma mark - |
65 |
#pragma mark ASIHTTPRequestDelegate |
66 |
|
67 |
- (void)requestFinished:(ASIPithosContainerRequest *)containerRequest { |
68 |
NSArray *someObjects = [containerRequest objects]; |
69 |
if (objects == nil) { |
70 |
objects = [[NSMutableArray alloc] initWithArray:someObjects]; |
71 |
} else { |
72 |
[objects addObjectsFromArray:someObjects]; |
73 |
} |
74 |
if ([someObjects count] < 10000) { |
75 |
// Save new children |
76 |
NSMutableArray *newChildren = [NSMutableArray array]; |
77 |
for (ASIPithosObject *object in objects) { |
78 |
if (object.subdir) { |
79 |
PithosSubdir *node = [[[PithosSubdir alloc] initWithContainerName:containerName subdirName:object.name] autorelease]; |
80 |
// XXX reuse pointers |
81 |
[newChildren addObject:node]; |
82 |
} else { |
83 |
PithosObject *node = [[[PithosObject alloc] initWithContainerName:containerName objectName:object.name] autorelease]; |
84 |
// XXX reuse pointers |
85 |
[newChildren addObject:node]; |
86 |
} |
87 |
} |
88 |
[objects release]; |
89 |
[children release]; |
90 |
childrenDirty = NO; |
91 |
// XXX sort then based on preferences |
92 |
children = [newChildren retain]; |
93 |
// Notify observers that children are updated |
94 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"PithosNodeChildrenUpdated" object:self]; |
95 |
} else { |
96 |
// Do an additional request to fetch more objects |
97 |
ASIPithosContainerRequest *newContainerRequest = [ASIPithosContainerRequest listObjectsRequestWithContainerName:containerName |
98 |
limit:0 |
99 |
marker:[[someObjects lastObject] name] |
100 |
prefix:[subdirName substringToIndex:([subdirName length] - 1)] |
101 |
delimiter:@"/" |
102 |
path:nil |
103 |
meta:nil |
104 |
shared:NO |
105 |
until:nil]; |
106 |
newContainerRequest.delegate = self; |
107 |
[newContainerRequest startAsynchronous]; |
108 |
} |
109 |
} |
110 |
|
111 |
- (void)requestFailed:(ASIPithosContainerRequest *)containerRequest { |
112 |
// XXX do something on error, cleanup |
113 |
[objects release]; |
114 |
} |
115 |
|
116 |
#pragma mark - |
117 |
#pragma mark Properties |
118 |
|
119 |
- (NSArray *)children { |
120 |
if (children == nil || childrenDirty) { |
121 |
// XXX maybe keep old children? |
122 |
children = [[NSArray alloc] init]; |
123 |
ASIPithosContainerRequest *containerRequest = [ASIPithosContainerRequest listObjectsRequestWithContainerName:containerName |
124 |
limit:0 |
125 |
marker:nil |
126 |
prefix:[subdirName substringToIndex:([subdirName length] - 1)] |
127 |
delimiter:@"/" |
128 |
path:nil |
129 |
meta:nil |
130 |
shared:NO |
131 |
until:nil]; |
132 |
containerRequest.delegate = self; |
133 |
[containerRequest startAsynchronous]; |
134 |
} |
135 |
|
136 |
return children; |
137 |
} |
138 |
|
139 |
- (NSString *)displayName { |
140 |
return [subdirName lastPathComponent]; |
141 |
} |
142 |
|
143 |
@end |