resources/RooUsage.txt
[app.Builder.js] / old-javascript / 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                 
81                 //print(JSON.stringify(out));
82                 
83         return out['value'];
84     },
85
86     isFile : function (path) {
87       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
88     },
89     exists : function (path) {
90       return GLib.file_test(path, GLib.FileTest.EXISTS);
91     },
92     isDirectory : function (path) {
93       return GLib.file_test(path, GLib.FileTest.IS_DIR);
94     },
95
96     list : function (path) {
97         var listing = [];
98         print(path);
99         var f = Gio.file_new_for_path(String(path));
100         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
101
102         var next_file = null;
103
104         while ((next_file = file_enum.next_file(null)) != null) {
105           listing.push(next_file.get_display_name());
106         }
107
108         file_enum.close(null);
109
110         listing.sort();
111
112         return listing;
113     },
114
115     mtime : function (path) {
116         var f = Gio.file_new_for_path(String(path));
117         var mtime = new GLib.TimeVal();
118
119         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
120         info.get_modification_time(mtime);
121
122         return new Date(mtime.tv_sec * 1000);
123     },
124
125     /**
126      * resolve the real path
127      * @arg path {String} Path to resolve
128      * @returns {String} the resolved path path.
129      * 
130      */
131     realpath :  function (path) { 
132         return this.canonical(path);
133     },
134     canonical : function (path) { 
135         var f = Gio.file_new_for_path(String(path));
136         var can = f.resolve_relative_path('');
137         return can.get_path();
138     },
139     /**
140      * write a string to a file
141      * @arg path {String} File to write to alwasy overwrites.
142      * @arg string {String} Contents of file.
143      * 
144      */
145     write : function (path, string) {
146         var d = new Date();
147         var f = Gio.file_new_for_path(String(path));
148         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
149         data_out.put_string(string, null);
150         data_out.close(null);
151         print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
152         
153     },
154     /**
155      * append
156      * @arg path {String} File to write to
157      * @arg string {String} string to append to file.
158      * 
159      */
160     append : function (path, string) {
161         var f = Gio.file_new_for_path(String(path));
162         var data_out = new Gio.DataOutputStream({
163                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
164         });
165         data_out.put_string(string, null);
166         data_out.close(null);
167     },
168     /**
169      * remove 
170      * Delete a file.
171      * @arg path {String} File to remove
172      * 
173      * 
174      */
175     remove : function (path)
176     {
177         var f = Gio.file_new_for_path(String(path));
178         return f['delete']();
179     },
180     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
181     silentRecursiveCopy : function (fromDir, toDir) {
182         var filesToCopy = File.recursiveListing(fromDir);
183         var srcPath, destPath, src, dest;
184
185         for (var index in filesToCopy) {
186           srcPath = File.join(String(fromDir), filesToCopy[index]);
187           destPath = File.join(String(toDir), filesToCopy[index]);
188
189           if (File.isFile(srcPath) && !File.isFile(destPath)) {
190             File.copyFile(srcPath, destPath);
191           }
192           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
193             File.mkdir(destPath);
194           }
195
196         }
197     },
198     /**
199      * Make a symbolic link
200      * @arg  new_link {String} The new link
201      * @arg  target    {String} Where it links to.
202      */
203     link : function (new_link, target) {
204         var dest = Gio.file_new_for_path(String(new_link));
205         return dest.make_symbolic_link(target, null);
206     },
207     /**
208      * Make a directory
209      * FIXME - needs perms setting..
210      * 
211      * @arg  directory  {String} Directory to make
212      */
213
214     mkdir : function (destPath) {
215         var dest = Gio.file_new_for_path(String(destPath));
216         return dest.make_directory(null);
217     },
218
219     /**
220      * Copy a file or (directory maybe?)
221      * @arg  srcPath {String} source file
222      * @arg  destPath {String} destination file
223      * @arg  flags {Gio.FileCopyFlags} to overwrite etc...  Gio.FileCopyFlags.OVERWRITE
224      */
225     copy : function (srcPath, destPath, flags) {
226         return this.copyFile(srcPath, destPath, flags);
227     },
228     copyFile : function (srcPath, destPath, flags) {
229         
230         flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
231         var dest = Gio.file_new_for_path(String(destPath));
232         var src = Gio.file_new_for_path(String(srcPath));
233
234         // a bit of a hack for the fact that Gio.File.copy arguments
235         // can be nulled, but not according to the GIR file
236         return src.copy(dest, flags);
237     },
238     
239     
240     
241
242     recursiveListing : function (dir) {
243
244         function recursiveListingInternal(prefix, listing, dir) {
245           var entries = File.list(dir);
246           var next, fullPath;
247
248           for (var index in entries) {
249             next = entries[index];
250             fullPath = File.join(prefix, dir, next);
251
252             if (File.isDirectory(fullPath)) {
253               listing.push(next);
254               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
255             }
256             else {
257               if (prefix) {
258                 next = File.join(prefix, next);
259               }
260               listing.push(next);
261             }
262           }
263
264           return listing;
265         }
266
267         return recursiveListingInternal('', [], dir);
268     }
269
270 };