JSDOC/ScopeNamer.js
[app.jsdoc] / File.js
1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
3 Gio = imports.gi.Gio;
4
5 String  = imports.String.String; 
6 /**
7 * @class File
8 * @singleton
9
10 * Library to wrap GLib and Gio basic File related methods
11
12 * usage:
13
14 * File = import.File.File;
15
16 * var contents = File.read("/tmp/test.txt");
17 *
18
19
20 */
21 var File = {
22     
23     /**
24      * @static
25      * @type {String} File seperator. (should be dynamic....)
26      */
27      
28     
29     SEPARATOR : '/',
30
31  // fixme - this needs a bitter location.. 
32     // they where in a string class before, but  overriding String methods is not a good normally a good idea..
33       
34     /**
35      * Right trim (in here to reduce dependancies)
36      * @param {String} s the string to trim
37      * @param {String} string to trim off right..
38      * @return {String} the trimmed string
39      */
40     rtrim : function (s,toTrim) {
41         if (s.substr(s.length - toTrim.length) == toTrim) {
42             return s.slice(0, s.length - toTrim.length);
43         }
44    
45         return s;
46     },
47     /**
48      * Trim a string (in here to reduce dependancies)
49      * @param {String} s the string to trim
50      * @param {String} string to trim off right..
51      * @return {String} the trimmed string
52      */
53     trim : function (s,toTrim) {
54         var out = this.ltrim(s,toTrim);
55         out = this.rtrim(out,toTrim);
56         return out;
57     },
58     /**
59      * Left Trim a string (in here to reduce dependancies)
60      * @param {String} s the string to trim
61      * @param {String} string to trim off right..
62      * @return {String} the trimmed string
63      */
64     ltrim : function (s, toTrim) {
65         if (s.substr(0, toTrim.length) == toTrim) {
66             return s.slice(toTrim.length);
67         }
68         
69         return s;
70     },
71     /**
72      * Get the base name of a path.
73      * @param {String} path
74      * @returns {String} basename
75     */
76     basename : function(path)
77     {
78         return path.split(File.SEPARATOR).pop();
79     },
80     
81     /**
82      * Get the directory name of a path. (could use Glib really)
83      * @param {String} path
84      * @returns {String} dirname
85     */
86     dirname : function(path)
87     {
88        var r = path.split(File.SEPARATOR)
89        r.pop();
90        return r.join(File.SEPARATOR);
91     },
92     
93     
94     /**
95      * Join a path with the Correct File seperator (unix only at present...)
96      * Takes a variable number of arguments, and joins them together.
97      * @return {String} the joined path
98      */
99     join : function () {
100         var out = "";
101         for (var i = 0; i < arguments.length; i++) {
102             if (i == 0) {
103               out += this.rtrim(arguments[i], File.SEPARATOR);
104             }
105             else if (i == arguments.length - 1) {
106               out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
107             }
108             else {
109               out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
110             }
111         }
112         return out;
113     },
114     /**
115      * Read a file and return as string
116      * @param {String} path The file location
117      * @return {String} the joined path
118      */
119     read : function (path) {
120         var out = {};
121         GLib.file_get_contents(path, out, null, null);
122         return out['value'];
123     },
124     /**
125      * Check if a path points to a file.
126      * @param {String} path The location
127      * @return {Boolean} true if it's a file
128      */
129     isFile : function (path) {
130       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
131     },
132     /**
133      * Check if a path points to a file, directory or link..
134      * @param {String} path The location
135      * @return {Boolean} true if it exists
136      */
137     exists : function (path) {
138       return GLib.file_test(path, GLib.FileTest.EXISTS);
139     },
140     /**
141      * Check if a path points to a directory.
142      * @param {String} path The location
143      * @return {Boolean} true if it's a directory
144      */
145     isDirectory : function (path) {
146       return GLib.file_test(path, GLib.FileTest.IS_DIR);
147     },
148     /**
149      * list files in a directory.
150      * @param {String} path The directory
151      * @return {Array} list of files (with full path?)
152      */
153     list : function (path) {
154         var listing = [];
155
156         var f = Gio.file_new_for_path(String(path));
157         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
158
159         var next_file = null;
160
161         while ((next_file = file_enum.next_file(null)) != null) {
162           listing.push(next_file.get_display_name());
163         }
164
165         file_enum.close(null);
166
167         listing.sort();
168
169         return listing;
170     },
171     /**
172      * Get the last modification time of a file
173      * @param {String} path The location
174      * @return {Date} when the file was last modified
175      */
176     mtime : function (path) {
177         var f = Gio.file_new_for_path(String(path));
178         var mtime = new GLib.TimeVal();
179
180         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
181         info.get_modification_time(mtime);
182
183         return new Date(mtime.tv_sec * 1000);
184     },
185     /**
186      * Resovle the absolute path of a file
187      * @param {String} path The location (relative) to current working directory?
188      * @return {String} the full path
189      */
190     canonical : function (path) {
191         var f = Gio.file_new_for_path(String(path));
192         var can = f.resolve_relative_path('');
193         return can.get_path();
194     },
195     
196     /**
197      * write a string to file
198      * @param {String} pathFile to write to
199      * @param {String} string  Contents of file.
200      * 
201      */
202     write : function (path, string) {
203         var f = Gio.file_new_for_path(String(path));
204         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
205         data_out.put_string(string, null);
206         data_out.close(null);
207     },
208     /**
209      * append a string to a file
210      * @param {String} path  File to write to
211      * @param {String}  string string to append to file.
212      * 
213      */
214     append : function (path, string) {
215         var f = Gio.file_new_for_path(String(path));
216         var data_out = new Gio.DataOutputStream({
217                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
218         });
219         data_out.put_string(string, null);
220         data_out.close(null);
221     },
222     /**
223      * Delete a file.
224      * @param  {String} path  File to remove    
225      */
226     remove : function (path)
227     {
228         var f = Gio.file_new_for_path(String(path));
229         return f['delete']();
230     },
231     /**
232      * copy files recursively from fromDir, silently ignore them if they already exist in toDir
233      *        unless you select overwrite..
234      * @param {String} src source path
235      * @param {String} dest destination path
236      * @param {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to 
237      *      otherwise they will not be copied
238      * 
239      */
240     silentRecursiveCopy : function (fromDir, toDir, opts) {
241         
242         var filesToCopy = File.recursiveListing(fromDir);
243         var srcPath, destPath, src, dest;
244         if (typeof(opts) =='undefined') {
245             opts = Gio.FileCopyFlags.NONE;
246         }
247         
248         for (var index in filesToCopy) {
249             srcPath = File.join(String(fromDir), filesToCopy[index]);
250             destPath = File.join(String(toDir), filesToCopy[index]);
251
252             if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
253                 File.mkdir(destPath);
254                 continue;
255             }
256             // source is not file..?!?!?
257             if (!File.isFile(srcPath)) {
258                 continue;
259             }
260             if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
261                 // do not overwrite.. - if file exists and we are not flaged to overwrite.
262                 continue;
263             }
264             
265             File.copyFile(srcPath, destPath, opts);
266            
267         }
268     },
269     
270     /**
271      * make a directory..
272      * @param {String} dstPath directory to make
273      */
274     mkdir : function (destPath) {
275         var dest = Gio.file_new_for_path(String(destPath));
276         
277         return dest.make_directory(null, null);
278     },
279     /**
280      * copy a File
281      * @param {String} src source path
282      * @param {String} dest destination path
283      * @param {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
284      * 
285      */
286     copyFile : function (srcPath, destPath, opts) {
287         if (typeof(opts) =='undefined') {
288             opts = Gio.FileCopyFlags.NONE;
289         }
290         var dest = Gio.file_new_for_path(String(destPath));
291         var src = Gio.file_new_for_path(String(srcPath));
292
293         // a bit of a hack for the fact that Gio.File.copy arguments
294         // can be nulled, but not according to the GIR file
295         return src.copy(dest, opts);
296     },
297     /**
298      * recursively list files in a directory.
299      * @param {String} path The directory
300      * @return {Array} list of files (with full path?)
301      */
302     recursiveListing : function (dir) {
303
304         function recursiveListingInternal(prefix, listing, dir) {
305             var entries = File.list(dir);
306             var next, fullPath;
307   
308             for (var index in entries) {
309               next = entries[index];
310               fullPath = File.join(prefix, dir, next);
311   
312               if (File.isDirectory(fullPath)) {
313                 listing.push(next);
314                 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
315               }
316               else {
317                 if (prefix) {
318                   next = File.join(prefix, next);
319                 }
320                 listing.push(next);
321               }
322             }
323   
324             return listing;
325         }
326
327         return recursiveListingInternal('', [], dir);
328     }
329
330 };