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