]> err.no Git - sope/blob - sope-core/NGExtensions/NGFileManager+JS.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGFileManager+JS.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "common.h"
23 #include "NGFileManager.h"
24 #include "NSFileManager+Extensions.h"
25 #include "EOCacheDataSource.h"
26
27 /*
28   JavaScript
29   
30     Properties
31     
32       cwd - readonly - current working directory, string
33     
34     Methods
35     
36       bool   cd(path)
37       Object ls([path|paths])
38       bool   mkdir(path[,path..])
39       bool   rmdir(path[,path..])
40       bool   rm(path[,path..])
41       bool   trash(path[,path..])
42       bool   cp(frompath[,frompath..], topath)
43       bool   mv(frompath[,frompath..], topath)
44       bool   ln(frompath, topath)
45       
46       bool   exists(path[,path..])
47       bool   isdir(path[,path..])
48       bool   islink(path[,path..])
49       
50       Object getDataSource([String path, [bool cache]])
51 */
52
53 @implementation NGFileManager(JSSupport)
54
55 static NSNumber *boolYes = nil;
56 static NSNumber *boolNo  = nil;
57
58 static void _ensureBools(void) {
59   if (boolYes == nil) boolYes = [[NSNumber numberWithBool:YES] retain];
60   if (boolNo  == nil) boolNo  = [[NSNumber numberWithBool:NO]  retain];
61 }
62
63 /* properties */
64
65 - (id)_jsprop_cwd {
66   return [self currentDirectoryPath];
67 }
68
69 /* methods */
70
71 - (id)_jsfunc_cd:(NSArray *)_args {
72   _ensureBools();
73   return [self changeCurrentDirectoryPath:[_args objectAtIndex:0]]
74     ? boolYes
75     : boolNo;
76 }
77
78 - (id)_jsfunc_ls:(NSArray *)_args {
79   unsigned count;
80   
81   if ((count = [_args count]) == 0) {
82     return [self directoryContentsAtPath:@"."];
83   }
84   else if (count == 1) {
85     return [self directoryContentsAtPath:
86                    [[_args objectAtIndex:0] stringValue]];
87   }
88   else {
89     NSMutableDictionary *md;
90     unsigned i;
91     
92     md = [NSMutableDictionary dictionaryWithCapacity:count];
93     for (i = 0; i < count; i++) {
94       NSString *path;
95       NSArray  *contents;
96
97       path     = [_args objectAtIndex:i];
98       contents = [self directoryContentsAtPath:path];
99
100       if (contents)
101         [md setObject:contents forKey:path];
102     }
103     
104     return md;
105   }
106 }
107
108 - (id)_jsfunc_mkdir:(NSArray *)_args {
109   unsigned count;
110   _ensureBools();
111
112   if ((count = [_args count]) == 0) {
113     return boolNo;
114   }
115   else {
116     unsigned i;
117     
118     for (i = 0; i < count; i++) {
119       NSString *path;
120       
121       path = [_args objectAtIndex:i];
122       
123       if (![self createDirectoryAtPath:path attributes:nil])
124         return boolNo;
125     }
126     
127     return boolYes;
128   }
129 }
130
131 - (id)_jsfunc_rmdir:(NSArray *)_args {
132   unsigned count;
133   _ensureBools();
134
135   if ((count = [_args count]) == 0) {
136     return boolNo;
137   }
138   else {
139     unsigned i;
140     
141     for (i = 0; i < count; i++) {
142       NSString *path;
143       BOOL isDir;
144       
145       path = [_args objectAtIndex:i];
146       
147       if (![self fileExistsAtPath:path isDirectory:&isDir])
148         return boolNo;
149       
150       if (!isDir)
151         /* not a directory ! */
152         return boolNo;
153       
154       if ([[self directoryContentsAtPath:path] count] > 0)
155         /* directory has contents */
156         return boolNo;
157
158       if (![self removeFileAtPath:path handler:nil])
159         /* remove failed */
160         return boolNo;
161     }
162     return boolYes;
163   }
164 }
165
166 - (id)_jsfunc_rm:(NSArray *)_args {
167   unsigned count;
168   _ensureBools();
169   
170   if ((count = [_args count]) == 0) {
171     return boolNo;
172   }
173   else {
174     unsigned i;
175     
176     for (i = 0; i < count; i++) {
177       NSString *path;
178       BOOL isDir;
179       
180       path = [_args objectAtIndex:i];
181       
182       if (![self fileExistsAtPath:path isDirectory:&isDir])
183         return boolNo;
184
185       if (isDir) {
186         if ([[self directoryContentsAtPath:path] count] > 0)
187           /* directory has contents */
188           return boolNo;
189       }
190       
191       if (![self removeFileAtPath:path handler:nil])
192         /* remove failed */
193         return boolNo;
194     }
195     return boolYes;
196   }
197 }
198
199 - (id)_jsfunc_trash:(NSArray *)_args {
200   unsigned count;
201   _ensureBools();
202
203   if ((count = [_args count]) == 0) {
204     return boolNo;
205   }
206   else {
207     unsigned i;
208     
209     for (i = 0; i < count; i++) {
210       NSString *path;
211       BOOL isDir;
212       
213       path = [_args objectAtIndex:i];
214       if (![self supportsTrashFolderAtPath:path])
215         return boolNo;
216       
217       if (![self fileExistsAtPath:path isDirectory:&isDir])
218         return boolNo;
219       
220       if (![self trashFileAtPath:path handler:nil])
221         /* remove failed */
222         return boolNo;
223     }
224     return boolYes;
225   }
226 }
227
228 - (id)_jsfunc_mv:(NSArray *)_args {
229   unsigned count;
230   _ensureBools();
231   
232   if ((count = [_args count]) == 0)
233     return boolNo;
234   else if (count == 1)
235     /* missing target path */
236     return boolNo;
237   else {
238     NSString *destpath;
239     unsigned i;
240     BOOL isDir;
241     
242     destpath = [_args objectAtIndex:(count - 1)];
243
244     if (![self fileExistsAtPath:destpath isDirectory:&isDir])
245       isDir = NO;
246     
247     for (i = 0; i < (count - 1); i++) {
248       NSString *path, *dpath = nil;
249       
250       path = [_args objectAtIndex:i];
251       
252       dpath = isDir
253         ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
254         : destpath;
255       
256       if (![self movePath:path toPath:dpath handler:nil])
257         /* move failed */
258         return boolNo;
259     }
260     
261     return boolYes;
262   }
263 }
264
265 - (id)_jsfunc_cp:(NSArray *)_args {
266   unsigned count;
267   _ensureBools();
268    
269   if ((count = [_args count]) == 0)
270     return boolNo;
271   else if (count == 1)
272     /* missing target path */
273     return boolNo;
274   else {
275     NSString *destpath;
276     unsigned i;
277     BOOL isDir;
278     
279     destpath = [_args objectAtIndex:(count - 1)];
280
281     if (![self fileExistsAtPath:destpath isDirectory:&isDir])
282       isDir = NO;
283     
284     for (i = 0; i < (count - 1); i++) {
285       NSString *path, *dpath = nil;
286       
287       path = [_args objectAtIndex:i];
288
289       dpath = isDir
290         ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
291         : destpath;
292       
293       if (![self copyPath:path toPath:dpath handler:nil])
294         /* copy failed */
295         return boolNo;
296     }
297     
298     return boolYes;
299   }
300 }
301
302 - (id)_jsfunc_ln:(NSArray *)_args {
303   unsigned count;
304   _ensureBools();
305   
306   if ((count = [_args count]) == 0)
307     return boolNo;
308   else if (count == 1)
309     /* missing target path */
310     return boolNo;
311   else {
312     NSString *srcpath;
313     NSString *destpath;
314     
315     srcpath  = [_args objectAtIndex:0];
316     destpath = [_args objectAtIndex:1];
317     
318     if (![self createSymbolicLinkAtPath:destpath pathContent:srcpath])
319       return boolNo;
320     
321     return boolYes;
322   }
323 }
324
325 - (id)_jsfunc_exists:(NSArray *)_args {
326   unsigned count;
327   _ensureBools();
328   
329   if ((count = [_args count]) == 0)
330     return boolYes;
331   else {
332     unsigned i;
333     
334     for (i = 0; i < count; i++) {
335       NSString *path;
336       
337       path = [_args objectAtIndex:i];
338       
339       if (![self fileExistsAtPath:path])
340         return boolNo;
341     }
342     return boolYes;
343   }
344 }
345
346 - (id)_jsfunc_isdir:(NSArray *)_args {
347   unsigned count;
348   _ensureBools();
349   
350   if ((count = [_args count]) == 0) {
351     return boolYes;
352   }
353   else {
354     unsigned i;
355     
356     for (i = 0; i < count; i++) {
357       NSString *path;
358       BOOL isDir;
359       
360       path = [_args objectAtIndex:i];
361
362 #if 0
363       NSLog(@"CHECK PATH: %@", path);
364 #endif
365       
366       if (![self fileExistsAtPath:path isDirectory:&isDir]) {
367 #if 0
368         NSLog(@"  does not exist ..");
369 #endif
370         return boolNo;
371       }
372
373       if (!isDir) {
374 #if 0
375         NSLog(@"  not a directory ..");
376 #endif
377         return boolNo;
378       }
379     }
380
381 #if 0
382     NSLog(@"%s: returning yes, %@ are directories",
383           __PRETTY_FUNCTION__, _args);
384 #endif
385     return boolYes;
386   }
387 }
388
389 - (id)_jsfunc_islink:(NSArray *)_args {
390   unsigned count;
391   _ensureBools();
392   
393   if ((count = [_args count]) == 0)
394     return boolYes;
395   else {
396     unsigned i;
397     
398     for (i = 0; i < count; i++) {
399       NSString     *path;
400       BOOL         isDir;
401       NSDictionary *attrs;
402       
403       path = [_args objectAtIndex:i];
404       
405       if (![self fileExistsAtPath:path isDirectory:&isDir])
406         return boolNo;
407       
408       if (isDir)
409         return boolNo;
410
411       if ((attrs = [self fileAttributesAtPath:path traverseLink:NO])==nil)
412         return boolNo;
413
414       if (![[attrs objectForKey:NSFileType]
415                    isEqualToString:NSFileTypeSymbolicLink])
416         return boolNo;
417     }
418     return boolYes;
419   }
420 }
421
422 /* datasource */
423
424 - (id)_jsfunc_getDataSource:(NSArray *)_args {
425   unsigned count;
426   NSString *path = nil;
427   BOOL     lcache;
428   id       ds;
429   _ensureBools();
430   
431   lcache = NO;
432   
433   if ((count = [_args count]) == 0) {
434     path = @".";
435   }
436   else if (count == 1) {
437     path = [[_args objectAtIndex:0] stringValue];
438   }
439   else if (count == 2) {
440     path  = [[_args objectAtIndex:0] stringValue];
441     lcache = [[_args objectAtIndex:1] boolValue];
442   }
443   
444   if (![self supportsFolderDataSourceAtPath:path])
445     return nil;
446   
447   if ((ds = [(id<NGFileManagerDataSources>)self dataSourceAtPath:path])==nil)
448     return nil;
449   
450   if (lcache) 
451     ds = [[[EOCacheDataSource alloc] initWithDataSource:ds] autorelease];
452   
453   return ds;
454 }
455
456 @end /* NGFileManager(JSSupport) */