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