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