root / Classes / Folder.m @ 45f2fce6
History | View | Annotate | Download (6.3 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]; |
22 |
folder.objects = [[NSMutableDictionary alloc] init]; |
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]; |
32 |
self.objects = [[NSMutableDictionary alloc] init]; |
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 |
[self.objects release]; |
64 |
|
65 |
NSMutableDictionary *folderedFiles = [[NSMutableDictionary alloc] init]; |
66 |
NSMutableDictionary *files = [[NSMutableDictionary alloc] init]; |
67 |
folders = [[NSMutableDictionary alloc] init]; |
68 |
|
69 |
for (NSString *key in objs) { |
70 |
StorageObject *object = [objs objectForKey:key]; |
71 |
BOOL objectHasTrailingSlash = NO; |
72 |
if ([object.name hasSuffix:@"/"]) { |
73 |
objectHasTrailingSlash = YES; |
74 |
object.name = [object.name substringToIndex:(object.name.length - 1)]; |
75 |
} |
76 |
|
77 |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/" options:NSRegularExpressionCaseInsensitive error:nil]; |
78 |
NSInteger matches = [regex numberOfMatchesInString:object.name options:0 range:NSMakeRange(0, [object.name length])]; |
79 |
|
80 |
if (matches > 0) { |
81 |
// build up the folder structure |
82 |
NSMutableArray *components = [NSMutableArray arrayWithArray:[object.name componentsSeparatedByString:@"/"]]; |
83 |
NSString *folderName = [components objectAtIndex:0]; |
84 |
|
85 |
object.name = [components lastObject]; |
86 |
if (objectHasTrailingSlash) |
87 |
object.name = [object.name stringByAppendingString:@"/"]; |
88 |
|
89 |
for (int i = [components count] - 2; i > 0; i--) { |
90 |
object.name = [NSString stringWithFormat:@"%@/%@", [components objectAtIndex:i], object.name]; |
91 |
} |
92 |
|
93 |
if (![folderedFiles objectForKey:folderName]) { |
94 |
[folderedFiles setObject:[[NSMutableDictionary alloc] init] forKey:folderName]; |
95 |
} |
96 |
|
97 |
NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName]; |
98 |
[folderFiles setObject:object forKey:object.name]; |
99 |
} else if ([PithosUtilities isContentTypeDirectory:object.contentType]) { |
100 |
Folder *folder = [[Folder alloc] init]; |
101 |
if (objectHasTrailingSlash) |
102 |
folder.name = [NSString stringWithFormat:@"%@/", object.name]; |
103 |
else |
104 |
folder.name = object.name; |
105 |
folder.parent = self; |
106 |
folder.metadata = object.metadata; |
107 |
folder.sharing = object.sharing; |
108 |
folder.contentType = object.contentType; |
109 |
[self.folders setObject:folder forKey:folder.name]; |
110 |
[folder release]; |
111 |
} else { |
112 |
// put the files in this folder |
113 |
if (objectHasTrailingSlash) |
114 |
object.name = [object.name stringByAppendingString:@"/"]; |
115 |
[files setObject:object forKey:object.name]; |
116 |
} |
117 |
} |
118 |
|
119 |
// take the foldered files and recursively build the rest of the folder structure |
120 |
NSArray *keys = [folderedFiles allKeys]; |
121 |
for (int i = 0; i < [keys count]; i++) { |
122 |
NSString *folderName = [keys objectAtIndex:i]; |
123 |
NSMutableDictionary *folderFiles = [folderedFiles objectForKey:folderName]; |
124 |
Folder *folder = [[Folder alloc] init]; |
125 |
folder.name = folderName; |
126 |
folder.parent = self; |
127 |
folder.objects = folderFiles; |
128 |
StorageObject *object = [objs objectForKey:folderName]; |
129 |
if ([PithosUtilities isContentTypeDirectory:object.contentType]) { |
130 |
folder.metadata = object.metadata; |
131 |
folder.sharing = object.sharing; |
132 |
folder.contentType = object.contentType; |
133 |
} |
134 |
[self.folders setObject:folder forKey:folder.name]; |
135 |
[folder release]; |
136 |
} |
137 |
|
138 |
[folderedFiles release]; |
139 |
objects = files; |
140 |
} |
141 |
} |
142 |
|
143 |
- (NSArray *)sortedContents { |
144 |
NSMutableArray *contents = [[NSMutableArray alloc] initWithArray:[self.folders allValues]]; |
145 |
|
146 |
[contents addObjectsFromArray:[self.objects allValues]]; |
147 |
if (!sortedContents || ![sortedContents isEqualToArray:contents]) { |
148 |
sortedContents = [[NSArray alloc] initWithArray:[contents sortedArrayUsingSelector:@selector(compare:)]]; |
149 |
} |
150 |
[contents release]; |
151 |
return sortedContents; |
152 |
} |
153 |
|
154 |
- (NSComparisonResult)compare:(Folder *)aFolder { |
155 |
return [self.name caseInsensitiveCompare:aFolder.name]; |
156 |
} |
157 |
|
158 |
- (NSString *)fullPath { |
159 |
NSString *result = self.name; |
160 |
if (parent) { |
161 |
result = [NSString stringWithFormat:@"%@/%@", [parent fullPath], self.name]; |
162 |
} |
163 |
if (!result) { |
164 |
result = @""; |
165 |
} |
166 |
return result; |
167 |
} |
168 |
|
169 |
-(void)dealloc { |
170 |
[name release]; |
171 |
[parent release]; |
172 |
[folders release]; |
173 |
[sharing release]; |
174 |
[objects release]; |
175 |
[sortedContents release]; |
176 |
[contentType release]; |
177 |
[super dealloc]; |
178 |
} |
179 |
|
180 |
@end |