tools/build_gtk_tree.js
[app.Builder.js] / File.js
1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
3 Gio = imports.gi.Gio;
4
5
6
7 /**
8 * @namespace File
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     SEPARATOR : '/',
24
25     // fixme - this needs a bitter location.. 
26     // they where in a string class before, but  overriding String methods is not a good normally a good idea..
27        
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     trim : function (s,toTrim) {
36         var out = s.ltrim(toTrim);
37         out = out.rtrim(toTrim);
38         return out;
39     },
40     
41     ltrim : function (s, toTrim) {
42         if (s.substr(0, toTrim.length) == toTrim) {
43             return s.slice(toTrim.length);
44         }
45         
46         return s;
47     },
48     
49     join : function () {
50         var out = "";
51         for (var i = 0; i < arguments.length; i++) {
52             if (i == 0) {
53               out += this.rtrim(arguments[i], File.SEPARATOR);
54             }
55             else if (i == arguments.length - 1) {
56               out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
57             }
58             else {
59               out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
60             }
61         }
62         return out;
63     },
64     
65     dirname : function(s)
66     {
67         var ar = s.split(File.SEPARATOR);
68         ar.pop();
69         return ar.join(File.SEPARATOR);
70     },
71     basename : function(s)
72     {
73         var ar = s.split(File.SEPARATOR);
74         return ar.pop();
75     },
76     
77     read : function (path) {
78         var out = {};
79         GLib.file_get_contents(path, out, null, null);
80         return out['value'];
81     },
82
83     isFile : function (path) {
84       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
85     },
86     exists : function (path) {
87       return GLib.file_test(path, GLib.FileTest.EXISTS);
88     },
89     isDirectory : function (path) {
90       return GLib.file_test(path, GLib.FileTest.IS_DIR);
91     },
92
93     list : function (path) {
94         var listing = [];
95
96         var f = Gio.file_new_for_path(String(path));
97         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
98
99         var next_file = null;
100
101         while ((next_file = file_enum.next_file(null)) != null) {
102           listing.push(next_file.get_display_name());
103         }
104
105         file_enum.close(null);
106
107         listing.sort();
108
109         return listing;
110     },
111
112     mtime : function (path) {
113         var f = Gio.file_new_for_path(String(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     /**
123      * resolve the real path
124      * @arg path {String} Path to resolve
125      * @returns {String} the resolved path path.
126      * 
127      */
128     realpath :  function (path) { 
129         return this.canonical(path);
130     },
131     canonical : function (path) { 
132         var f = Gio.file_new_for_path(String(path));
133         var can = f.resolve_relative_path('');
134         return can.get_path();
135     },
136     /**
137      * write a string to a file
138      * @arg path {String} File to write to alwasy overwrites.
139      * @arg string {String} Contents of file.
140      * 
141      */
142     write : function (path, string) {
143         var d = new Date();
144         var f = Gio.file_new_for_path(String(path));
145         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
146         data_out.put_string(string, null);
147         data_out.close(null);
148         print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
149         
150     },
151     /**
152      * append
153      * @arg path {String} File to write to
154      * @arg string {String} string to append to file.
155      * 
156      */
157     append : function (path, string) {
158         var f = Gio.file_new_for_path(String(path));
159         var data_out = new Gio.DataOutputStream({
160                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
161         });
162         data_out.put_string(string, null);
163         data_out.close(null);
164     },
165     /**
166      * remove 
167      * Delete a file.
168      * @arg path {String} File to remove
169      * 
170      * 
171      */
172     remove : function (path)
173     {
174         var f = Gio.file_new_for_path(String(path));
175         return f['delete']();
176     },
177     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
178     silentRecursiveCopy : function (fromDir, toDir) {
179         var filesToCopy = File.recursiveListing(fromDir);
180         var srcPath, destPath, src, dest;
181
182         for (var index in filesToCopy) {
183           srcPath = File.join(String(fromDir), filesToCopy[index]);
184           destPath = File.join(String(toDir), filesToCopy[index]);
185
186           if (File.isFile(srcPath) && !File.isFile(destPath)) {
187             File.copyFile(srcPath, destPath);
188           }
189           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
190             File.mkdir(destPath);
191           }
192
193         }
194     },
195     /**
196      * Make a symbolic link
197      * @arg  new_link {String} The new link
198      * @arg  target    {String} Where it links to.
199      */
200     link : function (new_link, target) {
201         var dest = Gio.file_new_for_path(String(new_link));
202         return dest.make_symbolic_link(target, null);
203     },
204     /**
205      * Make a directory
206      * FIXME - needs perms setting..
207      * 
208      * @arg  directory  {String} Directory to make
209      */
210
211     mkdir : function (destPath) {
212         var dest = Gio.file_new_for_path(String(destPath));
213         return dest.make_directory(null);
214     },
215
216     /**
217      * Copy a file or (directory maybe?)
218      * @arg  srcPath {String} source file
219      * @arg  destPath {String} destination file
220      * @arg  flags {Gio.FileCopyFlags} to overwrite etc...  Gio.FileCopyFlags.OVERWRITE
221      */
222     copy : function (srcPath, destPath, flags) {
223         return this.copyFile(srcPath, destPath, flags);
224     },
225     copyFile : function (srcPath, destPath, flags) {
226         
227         flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
228         var dest = Gio.file_new_for_path(String(destPath));
229         var src = Gio.file_new_for_path(String(srcPath));
230
231         // a bit of a hack for the fact that Gio.File.copy arguments
232         // can be nulled, but not according to the GIR file
233         return src.copy(dest, flags);
234     },
235     
236     
237     
238
239     recursiveListing : function (dir) {
240
241         function recursiveListingInternal(prefix, listing, dir) {
242           var entries = File.list(dir);
243           var next, fullPath;
244
245           for (var index in entries) {
246             next = entries[index];
247             fullPath = File.join(prefix, dir, next);
248
249             if (File.isDirectory(fullPath)) {
250               listing.push(next);
251               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
252             }
253             else {
254               if (prefix) {
255                 next = File.join(prefix, next);
256               }
257               listing.push(next);
258             }
259           }
260
261           return listing;
262         }
263
264         return recursiveListingInternal('', [], dir);
265     }
266
267 };