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