File.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     join : function () {
26         // fixme!
27         imports['String.js'].load(String);
28         
29         var out = "";
30         for (var i = 0; i < arguments.length; i++) {
31             if (i == 0) {
32               out += arguments[i].rtrim(File.SEPARATOR);
33             }
34             else if (i == arguments.length - 1) {
35               out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
36             }
37             else {
38               out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
39             }
40         }
41         return out;
42     },
43
44     read : function (path) {
45         var out = {};
46         GLib.file_get_contents(path, out, null, null);
47         return out['value'];
48     },
49
50     isFile : function (path) {
51       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
52     },
53     exists : function (path) {
54       return GLib.file_test(path, GLib.FileTest.EXISTS);
55     },
56     isDirectory : function (path) {
57       return GLib.file_test(path, GLib.FileTest.IS_DIR);
58     },
59
60     list : function (path) {
61         var listing = [];
62
63         var f = Gio.file_new_for_path(String(path));
64         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
65
66         var next_file = null;
67
68         while ((next_file = file_enum.next_file(null)) != null) {
69           listing.push(next_file.get_display_name());
70         }
71
72         file_enum.close(null);
73
74         listing.sort();
75
76         return listing;
77     },
78
79     mtime : function (path) {
80         var f = Gio.file_new_for_path(String(path));
81         var mtime = new GLib.TimeVal();
82
83         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
84         info.get_modification_time(mtime);
85
86         return new Date(mtime.tv_sec * 1000);
87     },
88
89     /**
90      * resolve the real path
91      * @arg path {String} Path to resolve
92      * @returns {String} the resolved path path.
93      * 
94      */
95     realpath :  function (path) { 
96         return this.canonical(path);
97     },
98     canonical : function (path) { 
99         var f = Gio.file_new_for_path(String(path));
100         var can = f.resolve_relative_path('');
101         return can.get_path();
102     },
103     /**
104      * write a string to a file
105      * @arg path {String} File to write to alwasy overwrites.
106      * @arg string {String} Contents of file.
107      * 
108      */
109     write : function (path, string) {
110         var f = Gio.file_new_for_path(String(path));
111         var data_out = new Gio.DataOutputStream({
112                 //base_stream: f.replace(null, false, Gio.FileCreateFlags.NONE, null)
113                 base_stream: f.replace_async(null, false, Gio.FileCreateFlags.NONE, 0, null)
114             });
115         data_out.put_string(string, null);
116         data_out.close(null);
117         
118     },
119     /**
120      * append
121      * @arg path {String} File to write to
122      * @arg string {String} string to append to file.
123      * 
124      */
125     append : function (path, string) {
126         var f = Gio.file_new_for_path(String(path));
127         var data_out = new Gio.DataOutputStream({
128                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
129         });
130         data_out.put_string(string, null);
131         data_out.close(null);
132     },
133     /**
134      * remove 
135      * Delete a file.
136      * @arg path {String} File to remove
137      * 
138      * 
139      */
140     remove : function (path)
141     {
142         var f = Gio.file_new_for_path(String(path));
143         return f['delete']();
144     },
145     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
146     silentRecursiveCopy : function (fromDir, toDir) {
147         var filesToCopy = File.recursiveListing(fromDir);
148         var srcPath, destPath, src, dest;
149
150         for (var index in filesToCopy) {
151           srcPath = File.join(String(fromDir), filesToCopy[index]);
152           destPath = File.join(String(toDir), filesToCopy[index]);
153
154           if (File.isFile(srcPath) && !File.isFile(destPath)) {
155             File.copyFile(srcPath, destPath);
156           }
157           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
158             File.mkdir(destPath);
159           }
160
161         }
162     },
163     /**
164      * Make a symbolic link
165      * @arg  new_link {String} The new link
166      * @arg  target    {String} Where it links to.
167      */
168     link : function (new_link, target) {
169         var dest = Gio.file_new_for_path(String(new_link));
170         return dest.make_symbolic_link(target, null);
171     },
172     /**
173      * Make a directory
174      * FIXME - needs perms setting..
175      * 
176      * @arg  directory  {String} Directory to make
177      */
178
179     mkdir : function (destPath) {
180         var dest = Gio.file_new_for_path(String(destPath));
181         return dest.make_directory(null);
182     },
183
184     /**
185      * Copy a file or (directory maybe?)
186      * @arg  srcPath {String} source file
187      * @arg  destPath {String} destination file
188      * @arg  flags {Gio.FileCopyFlags} to overwrite etc...  Gio.FileCopyFlags.OVERWRITE
189      */
190     copy : function (srcPath, destPath, flags) {
191         return this.copyFile(srcPath, destPath, flags);
192     },
193     copyFile : function (srcPath, destPath, flags) {
194         
195         flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
196         var dest = Gio.file_new_for_path(String(destPath));
197         var src = Gio.file_new_for_path(String(srcPath));
198
199         // a bit of a hack for the fact that Gio.File.copy arguments
200         // can be nulled, but not according to the GIR file
201         return src.copy(dest, flags);
202     },
203     
204     
205     
206
207     recursiveListing : function (dir) {
208
209         function recursiveListingInternal(prefix, listing, dir) {
210           var entries = File.list(dir);
211           var next, fullPath;
212
213           for (var index in entries) {
214             next = entries[index];
215             fullPath = File.join(prefix, dir, next);
216
217             if (File.isDirectory(fullPath)) {
218               listing.push(next);
219               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
220             }
221             else {
222               if (prefix) {
223                 next = File.join(prefix, next);
224               }
225               listing.push(next);
226             }
227           }
228
229           return listing;
230         }
231
232         return recursiveListingInternal('', [], dir);
233     }
234
235 };