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