Develop and Download Open Source Software

Browse CVS Repository

Contents of /lupin/lupin/LupinFramework/LPFile.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download)
Fri May 20 07:10:29 2005 UTC (18 years, 10 months ago) by letter
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +5 -13 lines
fix same-name-file(or folder) problem.

1 /*
2 * Copyright (c) 2004-2005 The Lupin Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted
5 * provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
8 * and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11 * conditions and the following disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE LUPIN PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LUPIN PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #import "LPFile.h"
25
26 static NSComparisonResult compareFile(LPFile* in1, LPFile* in2, NSString* inKind)
27 {
28 if ([inKind isEqualToString:@"name"])
29 return [[in1 name] compare:[in2 name]];
30 else if ([inKind isEqualToString:@"kind"])
31 return [[in1 kind] compare:[in2 kind]];
32 else if ([inKind isEqualToString:@"modifieddate"])
33 return [[in1 modifiedDate] compare:[in2 modifiedDate]];
34 else if ([inKind isEqualToString:@"createddate"])
35 return [[in1 createdDate] compare:[in2 createdDate]];
36 else if ([inKind isEqualToString:@"size"])
37 return [[in1 size] compare:[in2 size]];
38 else if ([inKind isEqualToString:@"kind"])
39 return [[in1 kind] compare:[in2 kind]];
40 else
41 return NSOrderedSame;
42 }
43
44 static NSComparisonResult compare(id in1, id in2, void* inKind)
45 {
46 return compareFile(in1,in2,inKind);
47 }
48
49 static NSComparisonResult compareReverce(id in1, id in2, void* inKind)
50 {
51 return compareFile(in2,in1,inKind);
52 }
53
54
55 @implementation LPFile
56
57 //! root�����������������g�p���������I
58 - (LPFile*)init
59 {
60 self = [super init];
61 _infoDict = [NSMutableDictionary dictionary];
62 [_infoDict retain];
63 [_infoDict setObject:[NSMutableArray array] forKey:@"children"];
64 _isDir = YES; //! root������
65 return self;
66 }
67
68 - (LPFile*)init:(NSString*)filePath name:(NSString*)name modifiedDate:(NSDate*)modifiedDate createdDate:(NSDate*)createdDate size:(NSNumber*)size kind:(NSString*)kind isDir:(BOOL)isDir
69 {
70 self = [super init];
71 _infoDict = [NSMutableDictionary dictionary];
72 [_infoDict retain];
73 [_infoDict setObject:[NSMutableArray array] forKey:@"children"];
74 /*
75 if (filePath) { //! �t�@�C���p�X���������X���b�V�����t������������������
76 NSMutableArray* array = [NSMutableArray arrayWithArray:[filePath pathComponents]];
77 if ([[array lastObject] isEqualToString:@"/"]) {
78 [array removeLastObject];
79 }
80 [_infoDict setObject:[NSString pathWithComponents:array] forKey:@"path"];
81 }
82 */
83 if (filePath) [_infoDict setObject:filePath forKey:@"path"];
84 if (name) [_infoDict setObject:name forKey:@"name"];
85 else if (filePath) [_infoDict setObject:[filePath lastPathComponent] forKey:@"name"];
86 if (modifiedDate) [_infoDict setObject:modifiedDate forKey:@"modifieddate"];
87 if (createdDate) [_infoDict setObject:createdDate forKey:@"createddate"];
88 if (size) [_infoDict setObject:size forKey:@"size"];
89 _isDir = isDir; //!�f�B���N�g��������YES���n���B�t�@�C�����A�t�@�C�����t�H���_�����������������ANO���n���B
90 if (kind) [_infoDict setObject:kind forKey:@"kind"];
91 else if (filePath) [_infoDict setObject:[self kindForFile:filePath] forKey:@"kind"];
92 return self;
93 }
94
95 - (void)appendFile:(LPFile*)file hierarchy:(BOOL)hierarchy
96 {
97 if (hierarchy)
98 [self appendFile:file index:0];
99 else
100 [self addChild:file];
101 }
102
103 - (void)appendFile:(LPFile*)file index:(int)index
104 {
105 if (!file) return;
106 NSString* path = [file path];
107 NSMutableArray *arr = [NSMutableArray arrayWithArray:[path pathComponents]];
108 if ([[arr lastObject] isEqualToString:@"/"]) [arr removeLastObject];
109
110 //NSLog(@"str=%@,lastpath=%@,count=%d,index=%d",path,lastPath,[arr count],index);
111
112 if ([arr count] <= index) return;
113 NSEnumerator* enumerator = [[self children] objectEnumerator];
114 LPFile* childFile;
115 int childIndex = 0; //For replacing a child when another child who has same name is added.
116 while (childFile = [enumerator nextObject]) {
117 //NSLog(@"path=%@,childFilePath=%@",path,[childFile path]);
118 NSMutableArray* childPathArray = [NSMutableArray arrayWithArray:[[childFile path] pathComponents]];
119 if ([[childPathArray lastObject] isEqualToString:@"/"]) [childPathArray removeLastObject];
120 if ([[NSString pathWithComponents:arr] isEqualToString:[NSString pathWithComponents:childPathArray]] && ([childFile isDir] == [file isDir])) {
121 //already appended to root.
122 [[file children] setArray:[childFile children]];
123 [[self children] replaceObjectAtIndex:childIndex withObject:file];
124 return;
125 }
126 //NSLog(@"path=%@,childFilePath=%@",path,[NSString pathWithComponents:childPathArray]);
127 if ([path hasPrefix:[NSString stringWithFormat:@"%@/",[NSString pathWithComponents:childPathArray]]]) { //! foo/var(file)��foo/(childFile)��������
128 [childFile appendFile:file index:index+1];
129 return;
130 }
131 childIndex++;
132 }
133 //NSLog(@"index=%d,%@",index,[NSString pathWithComponents:[arr subarrayWithRange:NSMakeRange(0,index+1)]]);
134 if ([arr count] - index == 1)
135 [self addChild:file];
136 else {
137 [self addChild:[[LPFile alloc] init:[NSString pathWithComponents:[arr subarrayWithRange:NSMakeRange(0,index+1)]] name:nil modifiedDate:nil createdDate:nil size:nil kind:nil isDir:YES]];
138 [[[self children] lastObject] appendFile:file index:index+1];
139 }
140 }
141
142 #pragma mark -
143
144 - (NSString*)path
145 {
146 return [_infoDict objectForKey:@"path"];
147 }
148
149 - (NSString*)name
150 {
151 return [_infoDict objectForKey:@"name"];
152 }
153
154 - (BOOL)isDir
155 {
156 return _isDir;
157 }
158
159 - (BOOL)isExpanded
160 {
161 return _isExpanded;
162 }
163
164 - (void)setExpanded:(BOOL)expanded
165 {
166 _isExpanded = expanded;
167 }
168
169 - (NSString*)kind
170 {
171 return [_infoDict objectForKey:@"kind"];
172 }
173
174 - (NSString*)kindForFile:(NSString*)path
175 {
176 NSString *res;
177 OSType type,creator;
178 if (_isDir || [[self children] count] != 0) {
179 type = 'fold';
180 creator = 'MACS';
181 } else {
182 type = kLSUnknownType;
183 creator = kLSUnknownCreator;
184 }
185
186 LSCopyKindStringForTypeInfo(type,creator,(CFStringRef)[path pathExtension],(CFStringRef*)&res);
187 return (res) ? [res autorelease] : nil;
188 }
189
190 - (NSImage*)icon
191 {
192 return [_infoDict objectForKey:@"icon"];
193 }
194
195 - (void)setIcon:(NSImage*)icon
196 {
197 [_infoDict setObject:icon forKey:@"icon"];
198 }
199
200 - (NSNumber*)size
201 {
202 return [_infoDict objectForKey:@"size"];
203 }
204
205 - (NSNumber*)totalSize
206 {
207 NSNumber* _totalSize = [_infoDict objectForKey:@"totalsize"];
208 if (_totalSize) return _totalSize;
209 unsigned long long childrenSize = ([self size]) ? [[self size] unsignedLongLongValue] : 0;
210 NSEnumerator* enumerator = [[self children] objectEnumerator];
211 LPFile* childFile;
212 while (childFile = [enumerator nextObject]) {
213 childrenSize += [[childFile totalSize] unsignedLongLongValue];
214 }
215 return [NSNumber numberWithUnsignedLongLong:childrenSize];
216 }
217
218 - (void)setTotalSize:(NSNumber*)_totalSize
219 {
220 [_infoDict setObject:_totalSize forKey:@"totalsize"];
221 }
222
223 //�t�@�C���������q������������
224 - (NSNumber*)totalNumOfChildren
225 {
226 NSNumber* _totalNumOfChildren = [_infoDict objectForKey:@"totalchildren"];
227 if (_totalNumOfChildren) return _totalNumOfChildren;
228 unsigned int numOfChildren = 0;
229 NSEnumerator* enumerator = [[self children] objectEnumerator];
230 LPFile* childFile;
231 while (childFile = [enumerator nextObject]) {
232 numOfChildren += [[childFile totalNumOfChildren] unsignedIntValue];
233 numOfChildren++;
234 }
235 return [NSNumber numberWithUnsignedInt:numOfChildren];
236 }
237
238 - (void)setTotalNumOfChildren:(NSNumber*)_totalNumOfChildren
239 {
240 [_infoDict setObject:_totalNumOfChildren forKey:@"totalchildren"];
241 }
242
243 - (NSString*)convertSize
244 {
245 NSNumber* _fileSize = [self size];
246 if (!_fileSize) return nil;
247 unsigned long long fileSize = [_fileSize unsignedLongLongValue];
248 NSString* sizeStr;
249 if (fileSize < 1024)
250 sizeStr = [NSString stringWithFormat:@"%lld %@",fileSize,
251 [[NSBundle bundleForClass:[self class]] localizedStringForKey:@"SizeByteFormat" value:nil table:nil]];
252 else if (fileSize < 1024*1024) {
253 fileSize >>= 10;
254 sizeStr = [NSString stringWithFormat:@"%lld %@",fileSize,@"KB"];
255 } else {
256 NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
257 [numberFormatter setFormat:@"###,###,###.#"];
258
259 double moreFileSize = fileSize / 1048576;
260 sizeStr = [NSString stringWithFormat:@"%@ %@",[numberFormatter stringForObjectValue:[NSNumber numberWithDouble:moreFileSize]],@"MB"];
261 }
262 return sizeStr;
263 }
264
265 - (NSDate*)modifiedDate
266 {
267 return [_infoDict objectForKey:@"modifieddate"];
268 }
269
270 - (NSDate*)createdDate
271 {
272 return [_infoDict objectForKey:@"createddate"];
273 }
274
275 - (NSString*)convertModifiedDateWithWidth:(float)width
276 {
277 return [self convertDate:[_infoDict objectForKey:@"modifieddate"] width:width];
278 }
279
280 - (NSString*)convertCreatedDateWithWidth:(float)width
281 {
282 return [self convertDate:[_infoDict objectForKey:@"createddate"] width:width];
283 }
284
285 - (NSString*)convertDate:(NSDate*)_date width:(float)width
286 {
287 if (!_date) return nil;
288 NSDictionary* locale = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
289 NSString* dateTypeStr;
290 if (width < 120) dateTypeStr = @"DateFormatFlat";
291 else if (width < 150) dateTypeStr = @"DateFormatShort";
292 else if (width < 200) dateTypeStr = @"DateFormatMiddle";
293 else dateTypeStr = @"DateFormatLong";
294 return [_date descriptionWithCalendarFormat:[[NSBundle bundleForClass:[self class]] localizedStringForKey:dateTypeStr value:@"" table:nil]
295 timeZone:[NSTimeZone systemTimeZone]
296 locale:locale];
297 }
298
299 #pragma mark -
300
301 - (void)sortBy:(NSString*)identifier order:(BOOL)order
302 {
303 NSMutableArray* children = [self children];
304 if (order)
305 [children sortUsingFunction:compare context:identifier];
306 else
307 [children sortUsingFunction:compareReverce context:identifier];
308 NSEnumerator* enumerator = [children objectEnumerator];
309 LPFile* file;
310 while (file = [enumerator nextObject]) {
311 [file sortBy:identifier order:order];
312 }
313 }
314
315 #pragma mark -
316
317 - (NSMutableArray*)children
318 {
319 return [_infoDict objectForKey:@"children"];
320 }
321
322 - (void)addChild:(LPFile*)child
323 {
324 [[_infoDict objectForKey:@"children"] addObject:child];
325 if (!_isDir) {
326 _isDir = YES;
327 [_infoDict setObject:[self kindForFile:[self path]] forKey:@"kind"];
328 }
329 }
330
331 #pragma mark -
332
333 - (void)dealloc
334 {
335 [_infoDict release];
336 [super dealloc];
337 }
338
339 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26