File.js
[gnome.introspection-doc-generator] / 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 * @namespace File
8
9 * Library to wrap GLib and Gio basic File related methods
10
11 * usage:
12
13 * File = import.File.File;
14
15 * var contents = File.read("/tmp/test.txt");
16 *
17
18
19 */
20 var File = {
21
22     SEPARATOR : '/',
23
24  // fixme - this needs a bitter location.. 
25     // they where in a string class before, but  overriding String methods is not a good normally a good idea..
26        
27     rtrim : function (s,toTrim) {
28         if (s.substr(s.length - toTrim.length) == toTrim) {
29             return s.slice(0, s.length - toTrim.length);
30         }
31    
32         return s;
33     },
34     trim : function (s,toTrim) {
35         var out = this.ltrim(s,toTrim);
36         out = this.rtrim(out,toTrim);
37         return out;
38     },
39     
40     ltrim : function (s, toTrim) {
41         if (s.substr(0, toTrim.length) == toTrim) {
42             return s.slice(toTrim.length);
43         }
44         
45         return s;
46     },
47     
48     join : function () {
49         var out = "";
50         for (var i = 0; i < arguments.length; i++) {
51             if (i == 0) {
52               out += this.rtrim(arguments[i], File.SEPARATOR);
53             }
54             else if (i == arguments.length - 1) {
55               out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
56             }
57             else {
58               out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
59             }
60         }
61         return out;
62     }, 
63     read : function (path) {
64         var out = {};
65         GLib.file_get_contents(path, out, null, null);
66         return out['value'];
67     },
68
69     isFile : function (path) {
70       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
71     },
72     exists : function (path) {
73       return GLib.file_test(path, GLib.FileTest.EXISTS);
74     },
75     isDirectory : function (path) {
76       return GLib.file_test(path, GLib.FileTest.IS_DIR);
77     },
78
79     list : function (path) {
80         var listing = [];
81
82         var f = Gio.file_new_for_path(String(path));
83         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
84
85         var next_file = null;
86
87         while ((next_file = file_enum.next_file(null)) != null) {
88           listing.push(next_file.get_display_name());
89         }
90
91         file_enum.close(null);
92
93         listing.sort();
94
95         return listing;
96     },
97
98     mtime : function (path) {
99         var f = Gio.file_new_for_path(String(path));
100         var mtime = new GLib.TimeVal();
101
102         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
103         info.get_modification_time(mtime);
104
105         return new Date(mtime.tv_sec * 1000);
106     },
107
108     canonical : function (path) {
109         var f = Gio.file_new_for_path(String(path));
110         var can = f.resolve_relative_path('');
111         return can.get_path();
112     },
113     
114     /**
115      * write
116      * @arg path {String} File to write to
117      * @arg string {String} Contents of file.
118      * 
119      */
120     write : function (path, string) {
121         var f = Gio.file_new_for_path(String(path));
122         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
123         data_out.put_string(string, null);
124         data_out.close(null);
125     },
126     /**
127      * append
128      * @arg path {String} File to write to
129      * @arg string {String} string to append to file.
130      * 
131      */
132     append : function (path, string) {
133         var f = Gio.file_new_for_path(String(path));
134         var data_out = new Gio.DataOutputStream({
135                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
136         });
137         data_out.put_string(string, null);
138         data_out.close(null);
139     },
140     /**
141      * remove 
142      * Delete a file.
143      * @arg path {String} File to remove
144      * 
145      * 
146      */
147     remove : function (path)
148     {
149         var f = Gio.file_new_for_path(String(path));
150         return f['delete']();
151     },
152     /**
153      * silentRecursiveCopy
154      * copy files recursively from fromDir, silently ignore them if they already exist in toDir
155      *        unless you select overwrite..
156      * @arg {String} src source path
157      * @arg {String} dest destination path
158      * @arg {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to 
159      *      otherwise they will not be copied
160      * 
161      */
162     silentRecursiveCopy : function (fromDir, toDir, opts) {
163         
164         var filesToCopy = File.recursiveListing(fromDir);
165         var srcPath, destPath, src, dest;
166         if (typeof(opts) =='undefined') {
167             opts = Gio.FileCopyFlags.NONE;
168         }
169         
170         for (var index in filesToCopy) {
171             srcPath = File.join(String(fromDir), filesToCopy[index]);
172             destPath = File.join(String(toDir), filesToCopy[index]);
173
174             if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
175                 File.mkdir(destPath);
176                 continue;
177             }
178             // source is not file..?!?!?
179             if (!File.isFile(srcPath)) {
180                 continue;
181             }
182             if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
183                 // do not overwrite.. - if file exists and we are not flaged to overwrite.
184                 continue;
185             }
186             
187             File.copyFile(srcPath, destPath, opts);
188            
189         }
190     },
191     
192     /**
193      * mkdir
194      * make a directory..
195      * @arg {String} dstPath directory to make
196      */
197     mkdir : function (destPath) {
198         var dest = Gio.file_new_for_path(String(destPath));
199         
200         return dest.make_directory(null, null);
201     },
202     /**
203      * copyFile
204      * @arg {String} src source path
205      * @arg {String} dest destination path
206      * @arg {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
207      * 
208      */
209     copyFile : function (srcPath, destPath, opts) {
210         if (typeof(opts) =='undefined') {
211             opts = Gio.FileCopyFlags.NONE;
212         }
213         var dest = Gio.file_new_for_path(String(destPath));
214         var src = Gio.file_new_for_path(String(srcPath));
215
216         // a bit of a hack for the fact that Gio.File.copy arguments
217         // can be nulled, but not according to the GIR file
218         return src.copy(dest, opts);
219     },
220
221     recursiveListing : function (dir) {
222
223         function recursiveListingInternal(prefix, listing, dir) {
224           var entries = File.list(dir);
225           var next, fullPath;
226
227           for (var index in entries) {
228             next = entries[index];
229             fullPath = File.join(prefix, dir, next);
230
231             if (File.isDirectory(fullPath)) {
232               listing.push(next);
233               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
234             }
235             else {
236               if (prefix) {
237                 next = File.join(prefix, next);
238               }
239               listing.push(next);
240             }
241           }
242
243           return listing;
244         }
245
246         return recursiveListingInternal('', [], dir);
247     }
248
249 };