root / Classes / Folder.m @ e06c24cf
History | View | Annotate | Download (6.4 kB)
1 |
// |
---|---|
2 |
// Folder.m |
3 |
// OpenStack |
4 |
// |
5 |
// Created by Mike Mayo on 12/7/10. |
6 |
// The OpenStack project is provided under the Apache 2.0 license. |
7 |
// |
8 |
|
9 |
#import "Folder.h" |
10 |
#import "StorageObject.h" |
11 |
#import "NSObject+NSCoding.h" |
12 |
#import "PithosUtilities.h" |
13 |
|
14 |
|
15 |
@implementation Folder |
16 |
|
17 |
@synthesize name, parent, folders, objects, metadata, sharing, contentType; |
18 |
|
19 |
+ (id)folder { |
20 |
Folder *folder = [[[self alloc] init] autorelease]; |
21 |
folder.folders = [[[NSMutableDictionary alloc] init] autorelease]; |
22 |
folder.objects = [[[NSMutableDictionary alloc] init] autorelease]; |
23 |
return folder; |
24 |
} |
25 |
|
26 |
#pragma mark - |
27 |
#pragma mark Serialization |
28 |
|
29 |
- (id)init { |
30 |
if (self = [super init]) { |
31 |
self.folders = [[[NSMutableDictionary alloc] init] autorelease]; |
32 |
self.objects = [[[NSMutableDictionary alloc] init] autorelease]; |
33 |
} |
34 |
return self; |
35 |
} |
36 |
|
37 |
- (void)encodeWithCoder:(NSCoder *)coder { |
38 |
[self autoEncodeWithCoder:coder]; |
39 |
/* |
40 |
[coder encodeObject:name forKey:@"name"]; |
41 |
[coder encodeObject:parent forKey:@"parent"]; |
42 |
[coder encodeObject:folders forKey:@"folders"]; |
43 |
[coder encodeObject:objects forKey:@"objects"]; |
44 |
*/ |
45 |
} |
46 |
|
47 |
- (id)initWithCoder:(NSCoder *)coder { |
48 |
if (self = [super init]) { |
49 |
[self autoDecode:coder]; |
50 |
/* |
51 |
name = [[coder decodeObjectForKey:@"name"] retain]; |
52 |
parent = [[coder decodeObjectForKey:@"parent"] retain]; |
53 |
folders = [[coder decodeObjectForKey:@"folders"] retain]; |
54 |
objects = [[coder decodeObjectForKey:@"objects"] retain]; |
55 |
*/ |
56 |
} |
57 |
return self; |
58 |
} |
59 |
|
60 |
|
61 |
- (void)setObjects:(NSMutableDictionary *)objs { |
62 |
if (self.objects != objs) { |
63 |
[objects release]; |
64 |
NSMutableDictionary *folderedFiles = [[NSMutableDictionary alloc] init]; |
65 |
NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; |
66 |
folders = [[NSMutableDictionary alloc] init]; |
67 |
|
68 |
for (NSString *key in objs) { |
69 |
StorageObject *object = [objs objectForKey:key]; |
70 |
BOOL objectHasTrailingSlash = NO; |
71 |
if ([object.name hasSuffix:@"/"]) { |
72 |
objectHasTrailingSlash = YES; |
73 |
object.name = [object.name substringToIndex:(object.name.length - 1)]; |
74 |
} |
75 |
|
76 |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/" options:NSRegularExpressionCaseInsensitive error:nil]; |
77 |
NSInteger matches = [regex numberOfMatchesInString:object.name options:0 range:NSMakeRange(0, [object.name length])]; |
78 |
|
79 |
if (matches > 0) { |
80 |
// build up the folder structure |
81 |
NSMutableArray *components = [NSMutableArray arrayWithArray:[object.name componentsSeparatedByString:@"/"]]; |
82 |
NSString *folderName = [components objectAtIndex:0]; |
83 |
|
84 |
object.name = [components lastObject]; |
85 |
if (objectHasTrailingSlash) |
86 |
object.name = [object.name stringByAppendingString:@"/"]; |
87 |
|
88 |
for (int i = [components count] - 2; i > 0; i--) { |
89 |
object.name = [NSString stringWithFormat:@"%@/%@", [components objectAtIndex:i], object.name]; |
90 |
} |
91 |
|
92 |
if (![folderedFiles objectForKey:folderName]) { |
93 |
[folderedFiles setObject:[NSMutableDictionary dictionary] forKey:folderName]; |
94 |
} |
95 |
|
96 |
NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName]; |
97 |
[folderFiles setObject:object forKey:object.name]; |
98 |
} else if ([PithosUtilities isContentTypeDirectory:object.contentType]) { |
99 |
Folder *folder = [[Folder alloc] init]; |
100 |
if (objectHasTrailingSlash) |
101 |
folder.name = [NSString stringWithFormat:@"%@/", object.name]; |
102 |
else |
103 |
folder.name = object.name; |
104 |
folder.parent = self; |
105 |
folder.metadata = object.metadata; |
106 |
folder.sharing = object.sharing; |
107 |
folder.contentType = object.contentType; |
108 |
[self.folders setObject:folder forKey:folder.name]; |
109 |
[folder release]; |
110 |
} else { |
111 |
// put the files in this folder |
112 |
if (objectHasTrailingSlash) |
113 |
object.name = [object.name stringByAppendingString:@"/"]; |
114 |
[files setObject:object forKey:object.name]; |
115 |
} |
116 |
} |
117 |
|
118 |
// take the foldered files and recursively build the rest of the folder structure |
119 |
NSArray *keys = [folderedFiles allKeys]; |
120 |
for (int i = 0; i < [keys count]; i++) { |
121 |
NSString *folderName = [keys objectAtIndex:i]; |
122 |
NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName]; |
123 |
Folder *folder = [[Folder alloc] init]; |
124 |
folder.name = folderName; |
125 |
folder.parent = self; |
126 |
folder.objects = folderFiles; |
127 |
StorageObject *object = [objs objectForKey:folderName]; |
128 |
if ([PithosUtilities isContentTypeDirectory:object.contentType]) { |
129 |
folder.metadata = object.metadata; |
130 |
folder.sharing = object.sharing; |
131 |
folder.contentType = object.contentType; |
132 |
} else { |
133 |
folder.metadata = [NSMutableDictionary dictionary]; |
134 |
} |
135 |
[self.folders setObject:folder forKey:folder.name]; |
136 |
[folder release]; |
137 |
} |
138 |
|
139 |
[folderedFiles release]; |
140 |
objects = files; |
141 |
} |
142 |
} |
143 |
|
144 |
- (NSArray *)sortedContents { |
145 |
NSMutableArray *contents = [[NSMutableArray alloc] initWithArray:[self.folders allValues]]; |
146 |
|
147 |
[contents addObjectsFromArray:[self.objects allValues]]; |
148 |
if (!sortedContents || ![sortedContents isEqualToArray:contents]) { |
149 |
sortedContents = [[NSArray alloc] initWithArray:[contents sortedArrayUsingSelector:@selector(compare:)]]; |
150 |
} |
151 |
[contents release]; |
152 |
return sortedContents; |
153 |
} |
154 |
|
155 |
- (NSComparisonResult)compare:(Folder *)aFolder { |
156 |
return [self.name caseInsensitiveCompare:aFolder.name]; |
157 |
} |
158 |
|
159 |
- (NSString *)fullPath { |
160 |
NSString *result = self.name; |
161 |
if (parent) { |
162 |
result = [NSString stringWithFormat:@"%@/%@", [parent fullPath], self.name]; |
163 |
} |
164 |
if (!result) { |
165 |
result = @""; |
166 |
} |
167 |
return result; |
168 |
} |
169 |
|
170 |
-(void)dealloc { |
171 |
[name release]; |
172 |
[parent release]; |
173 |
[folders release]; |
174 |
[sharing release]; |
175 |
[objects release]; |
176 |
[sortedContents release]; |
177 |
[contentType release]; |
178 |
[super dealloc]; |
179 |
} |
180 |
|
181 |
@end |