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