1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 String = imports.String.String;
10 * Library to wrap GLib and Gio basic File related methods
14 * File = import.File.File;
16 * var contents = File.read("/tmp/test.txt");
30 // fixme - this needs a bitter location..
31 // they where in a string class before, but overriding String methods is not a good normally a good idea..
34 * Right trim (in here to reduce dependancies)
35 * @param {String} s the string to trim
36 * @param {String} string to trim off right..
37 * @return {String} the trimmed string
39 rtrim : function (s,toTrim) {
40 if (s.substr(s.length - toTrim.length) == toTrim) {
41 return s.slice(0, s.length - toTrim.length);
47 * Trim a string (in here to reduce dependancies)
48 * @param {String} s the string to trim
49 * @param {String} string to trim off right..
50 * @return {String} the trimmed string
52 trim : function (s,toTrim) {
53 var out = this.ltrim(s,toTrim);
54 out = this.rtrim(out,toTrim);
58 * Left Trim a string (in here to reduce dependancies)
59 * @param {String} s the string to trim
60 * @param {String} string to trim off right..
61 * @return {String} the trimmed string
63 ltrim : function (s, toTrim) {
64 if (s.substr(0, toTrim.length) == toTrim) {
65 return s.slice(toTrim.length);
71 * Join a path with the Correct File seperator (unix only at present...)
72 * Takes a variable number of arguments, and joins them together.
73 * @return {String} the joined path
77 for (var i = 0; i < arguments.length; i++) {
79 out += this.rtrim(arguments[i], File.SEPARATOR);
81 else if (i == arguments.length - 1) {
82 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
85 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
90 read : function (path) {
92 GLib.file_get_contents(path, out, null, null);
96 isFile : function (path) {
97 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
99 exists : function (path) {
100 return GLib.file_test(path, GLib.FileTest.EXISTS);
102 isDirectory : function (path) {
103 return GLib.file_test(path, GLib.FileTest.IS_DIR);
106 list : function (path) {
109 var f = Gio.file_new_for_path(String(path));
110 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
112 var next_file = null;
114 while ((next_file = file_enum.next_file(null)) != null) {
115 listing.push(next_file.get_display_name());
118 file_enum.close(null);
125 mtime : function (path) {
126 var f = Gio.file_new_for_path(String(path));
127 var mtime = new GLib.TimeVal();
129 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
130 info.get_modification_time(mtime);
132 return new Date(mtime.tv_sec * 1000);
135 canonical : function (path) {
136 var f = Gio.file_new_for_path(String(path));
137 var can = f.resolve_relative_path('');
138 return can.get_path();
143 * @arg path {String} File to write to
144 * @arg string {String} Contents of file.
147 write : function (path, string) {
148 var f = Gio.file_new_for_path(String(path));
149 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
150 data_out.put_string(string, null);
151 data_out.close(null);
155 * @arg path {String} File to write to
156 * @arg string {String} string to append to file.
159 append : function (path, string) {
160 var f = Gio.file_new_for_path(String(path));
161 var data_out = new Gio.DataOutputStream({
162 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
164 data_out.put_string(string, null);
165 data_out.close(null);
170 * @arg path {String} File to remove
174 remove : function (path)
176 var f = Gio.file_new_for_path(String(path));
177 return f['delete']();
180 * silentRecursiveCopy
181 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
182 * unless you select overwrite..
183 * @arg {String} src source path
184 * @arg {String} dest destination path
185 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
186 * otherwise they will not be copied
189 silentRecursiveCopy : function (fromDir, toDir, opts) {
191 var filesToCopy = File.recursiveListing(fromDir);
192 var srcPath, destPath, src, dest;
193 if (typeof(opts) =='undefined') {
194 opts = Gio.FileCopyFlags.NONE;
197 for (var index in filesToCopy) {
198 srcPath = File.join(String(fromDir), filesToCopy[index]);
199 destPath = File.join(String(toDir), filesToCopy[index]);
201 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
202 File.mkdir(destPath);
205 // source is not file..?!?!?
206 if (!File.isFile(srcPath)) {
209 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
210 // do not overwrite.. - if file exists and we are not flaged to overwrite.
214 File.copyFile(srcPath, destPath, opts);
222 * @arg {String} dstPath directory to make
224 mkdir : function (destPath) {
225 var dest = Gio.file_new_for_path(String(destPath));
227 return dest.make_directory(null, null);
231 * @arg {String} src source path
232 * @arg {String} dest destination path
233 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
236 copyFile : function (srcPath, destPath, opts) {
237 if (typeof(opts) =='undefined') {
238 opts = Gio.FileCopyFlags.NONE;
240 var dest = Gio.file_new_for_path(String(destPath));
241 var src = Gio.file_new_for_path(String(srcPath));
243 // a bit of a hack for the fact that Gio.File.copy arguments
244 // can be nulled, but not according to the GIR file
245 return src.copy(dest, opts);
248 recursiveListing : function (dir) {
250 function recursiveListingInternal(prefix, listing, dir) {
251 var entries = File.list(dir);
254 for (var index in entries) {
255 next = entries[index];
256 fullPath = File.join(prefix, dir, next);
258 if (File.isDirectory(fullPath)) {
260 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
264 next = File.join(prefix, next);
273 return recursiveListingInternal('', [], dir);