root/Footagehead/trunk/FHFileHandler.m

Revision 5050, 5.9 kB (checked in by morris, 10 months ago)

Ignore /home

  • Property svn:keywords set to author date id revision
Line 
1 /* $Id$ */
2
3 /*
4  *  Copyright (c) 2003-2007 Axel Andersson
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "NSImage-FHAdditions.h"
30 #import "FHArchiveHandler.h"
31 #import "FHFile.h"
32 #import "FHFileHandler.h"
33
34 @interface FHFileHandler(Private)
35
36 + (NSArray *)_directoryHandledFileTypes;
37
38 @end
39
40
41 @implementation FHFileHandler(Private)
42
43 + (NSArray *)_directoryHandledFileTypes {
44         static NSMutableArray           *types;
45        
46         if(!types) {
47                 types = [[NSMutableArray alloc] init];
48                 [types addObjectsFromArray:[FHArchiveHandler handledFileTypes]];
49         }
50        
51         return types;
52 }
53
54 @end
55
56
57 @implementation FHFileHandler
58
59 + (BOOL)handlesURL:(WIURL *)url isPrimary:(BOOL)primary {
60         if(primary && [url isFileURL]) {
61                 if([[url path] length] == 0)
62                         return YES;
63
64                 if([[NSFileManager defaultManager] directoryExistsAtPath:[url path]])
65                         return YES;
66         }
67        
68         return NO;
69 }
70
71
72
73 + (BOOL)handlesURLAsDirectory:(WIURL *)url {
74         if(![url isFileURL])
75                 return NO;
76        
77         if([[NSFileManager defaultManager] directoryExistsAtPath:[url path]])
78                 return YES;
79
80         return [[self _directoryHandledFileTypes] containsObject:[url pathExtension]];
81 }
82
83
84
85 + (NSArray *)handledFileTypes {
86         return [NSImage FHImageFileTypes];
87 }
88
89
90
91 #pragma mark -
92
93 - (id)initHandlerWithURL:(WIURL *)url {
94         return [self initHandlerWithURL:url rootPath:[url path]];
95 }
96
97
98
99 - (id)initHandlerWithURL:(WIURL *)url rootPath:(NSString *)rootPath {
100         self = [super initHandlerWithURL:url];
101        
102         _rootPath = [rootPath retain];
103        
104         return self;
105 }
106
107
108
109 - (void)dealloc {
110         [_rootPath release];
111        
112         [super dealloc];
113 }
114
115
116
117 #pragma mark -
118
119 static NSComparisonResult compareFile(id string1, id string2, void *context) {
120         return [string1 compare:string2 options:NSCaseInsensitiveSearch | NSNumericSearch];
121 }
122
123
124
125 #pragma mark -
126
127 - (NSArray *)files {
128         NSFileManager           *fileManager;
129         NSEnumerator            *enumerator;
130         NSSet                           *types;
131         NSArray                         *files;
132         NSString                        *volume, *name, *path, *extension, *symlink;
133         WIURL                           *url;
134         LSItemInfoRecord        itemInfoRecord;
135         OSStatus                        err;
136         BOOL                            isDirectory, isRoot;
137         NSUInteger                      i, count;
138
139         if(!_files) {
140                 fileManager = [NSFileManager defaultManager];
141                
142                 if([_rootPath length] == 0) {
143                         _files  = [[NSMutableArray alloc] init];
144                         [_files addObject:[FHFile fileWithURL:[WIURL fileURLWithPath:@"/Network"] isDirectory:YES]];
145                        
146                         enumerator = [[fileManager directoryContentsAtPath:@"/Volumes/"] objectEnumerator];
147                        
148                         while((volume = [enumerator nextObject])) {
149                                 path    = [NSSWF:@"/Volumes/%@", volume];
150                                 symlink = [fileManager pathContentOfSymbolicLinkAtPath:path];
151                                
152                                 if(symlink)
153                                         path = symlink;
154                                
155                                 [_files addObject:[FHFile fileWithURL:[WIURL fileURLWithPath:path] name:volume isDirectory:YES]];
156                         }
157                        
158                         [_files sortUsingSelector:@selector(compareName:)];
159                        
160                         _numberOfFiles = [_files count];
161                 } else {
162                         isRoot  = [_rootPath isEqualToString:@"/"];
163                         files   = [[fileManager directoryContentsWithFileAtPath:_rootPath] sortedArrayUsingFunction:compareFile context:NULL];
164                         count   = [files count];
165                         types   = [NSSet setWithArray:[FHFileHandler handledFileTypes]];
166                         _files  = [[NSMutableArray alloc] initWithCapacity:count];
167                        
168                         for(i = 0; i < count; i++) {
169                                 name = [files objectAtIndex:i];
170                                 path = [_rootPath stringByAppendingPathComponent:name];
171                                
172                                 if([name hasPrefix:@"."])
173                                         continue;
174                                
175                                 url = [WIURL fileURLWithPath:path];
176                                 err = LSCopyItemInfoForURL((CFURLRef) [url URL], kLSRequestBasicFlagsOnly, &itemInfoRecord);
177                                
178                                 if(err != noErr)
179                                         continue;
180                                
181                                 if(itemInfoRecord.flags & kLSItemInfoIsInvisible)
182                                         continue;
183                                        
184                                 if(isRoot) {
185                                         if([path isEqualToString:@"/Network"] ||
186                                            [path isEqualToString:@"/automount"] ||
187                                            [path isEqualToString:@"/etc"] ||
188                                            [path isEqualToString:@"/home"] ||
189                                            [path isEqualToString:@"/mach"] ||
190                                            [path isEqualToString:@"/net"] ||
191                                            [path isEqualToString:@"/tmp"] ||
192                                            [path isEqualToString:@"/var"])
193                                                 continue;
194                                 }
195                                
196                                 isDirectory = (itemInfoRecord.flags & kLSItemInfoIsContainer || itemInfoRecord.flags & kLSItemInfoIsSymlink);
197                                
198                                 if(!isDirectory) {
199                                         extension = [path pathExtension];
200                                        
201                                         if(![types containsObject:extension]) {
202                                                 if([[[self class] _directoryHandledFileTypes] containsObject:extension])
203                                                         isDirectory = YES;
204                                                 else
205                                                         continue;
206                                         }
207                                 }
208                                
209                                 [_files addObject:[FHFile fileWithURL:url isDirectory:isDirectory]];
210                                
211                                 _numberOfFiles++;
212                                
213                                 if(!isDirectory)
214                                         _numberOfImages++;
215                         }
216                 }
217         }
218        
219         return _files;
220 }
221
222
223 - (WIURL *)parentURL {
224         if([_rootPath isEqualToString:@"/"] ||
225            ([_rootPath hasPrefix:@"/Volumes"] && [[_rootPath pathComponents] count] == 3) ||
226            ([_rootPath hasPrefix:@"/Network"] && [[_rootPath pathComponents] count] == 2))
227                 return [WIURL fileURLWithPath:@""];
228        
229         return [super parentURL];
230 }
231
232 @end
Note: See TracBrowser for help on using the browser.