File.js
[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 var 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 const  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         print(path);
66         var ret = GLib.file_get_contents(path, out, null, null);
67         //print(ret[1]);
68         //throw "oops";
69         return '' + ((typeof(ret) == 'object') ? ret[1] : out['value']);
70     },
71
72     isFile : function (path) {
73       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
74     },
75     exists : function (path) {
76       return GLib.file_test(path, GLib.FileTest.EXISTS);
77     },
78     isDirectory : function (path) {
79       return GLib.file_test(path, GLib.FileTest.IS_DIR);
80     },
81
82     list : function (path) {
83         var listing = [];
84
85         var f = Gio.file_new_for_path('' +(path));
86         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
87
88         var next_file = null;
89
90         while ((next_file = file_enum.next_file(null)) != null) {
91           listing.push(next_file.get_display_name());
92         }
93
94         file_enum.close(null);
95
96         listing.sort();
97
98         return listing;
99     },
100
101     mtime : function (path) {
102         var f = Gio.file_new_for_path('' +(path));
103         var mtime = new GLib.TimeVal();
104
105         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
106         info.get_modification_time(mtime);
107
108         return new Date(mtime.tv_sec * 1000);
109     },
110
111     canonical : function (path) {
112         var f = Gio.file_new_for_path('' +(path));
113         var can = f.resolve_relative_path('');
114         return can.get_path();
115     },
116     
117     /**
118      * write
119      * @arg path {String} File to write to
120      * @arg string {String} Contents of file.
121      * 
122      */
123     write : function (path, string) {
124         var f = Gio.file_new_for_path('' +(path));
125         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
126         data_out.put_string(string, null);
127         data_out.close(null);
128     },
129     /**
130      * append
131      * @arg path {String} File to write to
132      * @arg string {String} string to append to file.
133      * 
134      */
135     append : function (path, string) {
136         var f = Gio.file_new_for_path('' +(path));
137         var data_out = new Gio.DataOutputStream({
138                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
139         });
140         data_out.put_string(string, null);
141         data_out.close(null);
142     },
143     /**
144      * remove 
145      * Delete a file.
146      * @arg path {String} File to remove
147      * 
148      * 
149      */
150     remove : function (path)
151     {
152         var f = Gio.file_new_for_path('' +(path));
153         var c = new Gio.Cancellable();
154         return f['delete'](c);
155     },
156     /**
157      * silentRecursiveCopy
158      * copy files recursively from fromDir, silently ignore them if they already exist in toDir
159      *        unless you select overwrite..
160      * @arg {String} src source path
161      * @arg {String} dest destination path
162      * @arg {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to 
163      *      otherwise they will not be copied
164      * 
165      */
166     silentRecursiveCopy : function (fromDir, toDir, opts) {
167         
168         var filesToCopy = File.recursiveListing(fromDir);
169         var srcPath, destPath, src, dest;
170         if (typeof(opts) =='undefined') {
171             opts = Gio.FileCopyFlags.NONE;
172         }
173         
174         for (var index in filesToCopy) {
175             srcPath = File.join('' +(fromDir), filesToCopy[index]);
176             destPath = File.join('' +(toDir), filesToCopy[index]);
177
178             if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
179                 File.mkdir(destPath);
180                 continue;
181             }
182             // source is not file..?!?!?
183             if (!File.isFile(srcPath)) {
184                 continue;
185             }
186             if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
187                 // do not overwrite.. - if file exists and we are not flaged to overwrite.
188                 continue;
189             }
190             
191             File.copyFile(srcPath, destPath, opts);
192            
193         }
194     },
195     
196     /**
197      * mkdir
198      * make a directory..
199      * @arg {String} dstPath directory to make
200      */
201     mkdir : function (destPath) {
202         var dest = Gio.file_new_for_path('' +(destPath));
203         
204         return dest.make_directory(null, null);
205     },
206     /**
207      * copyFile
208      * @arg {String} src source path
209      * @arg {String} dest destination path
210      * @arg {Gio.FileCopyFlags} options (optional)  - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
211      * 
212      */
213     copyFile : function (srcPath, destPath, opts) {
214         if (typeof(opts) =='undefined') {
215             opts = Gio.FileCopyFlags.NONE;
216         }
217         var dest = Gio.file_new_for_path('' +(destPath));
218         var src = Gio.file_new_for_path('' +(srcPath));
219
220         // a bit of a hack for the fact that Gio.File.copy arguments
221         // can be nulled, but not according to the GIR file
222         return src.copy(dest, opts);
223     },
224
225     recursiveListing : function (dir) {
226
227         function recursiveListingInternal(prefix, listing, dir) {
228           var entries = File.list(dir);
229           var next, fullPath;
230
231           for (var index in entries) {
232             next = entries[index];
233             fullPath = File.join(prefix, dir, next);
234
235             if (File.isDirectory(fullPath)) {
236               listing.push(next);
237               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
238             }
239             else {
240               if (prefix) {
241                 next = File.join(prefix, next);
242               }
243               listing.push(next);
244             }
245           }
246
247           return listing;
248         }
249
250         return recursiveListingInternal('', [], dir);
251     }
252
253 };