2b74b09a7399aa87c6687751eb721ad28ee86336
[gnome.introspection-doc-generator] / File.js
1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
3 Gio = imports.gi.Gio;
4
5 imports['String.js'].load(String);
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         var out = "";
27         for (var i = 0; i < arguments.length; i++) {
28             if (i == 0) {
29               out += arguments[i].rtrim(File.SEPARATOR);
30             }
31             else if (i == arguments.length - 1) {
32               out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
33             }
34             else {
35               out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
36             }
37         }
38         return out;
39     },
40
41     read : function (path) {
42         var out = {};
43         GLib.file_get_contents(path, out, null, null);
44         return out['value'];
45     },
46
47     isFile : function (path) {
48       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
49     },
50     exists : function (path) {
51       return GLib.file_test(path, GLib.FileTest.EXISTS);
52     },
53     isDirectory : function (path) {
54       return GLib.file_test(path, GLib.FileTest.IS_DIR);
55     },
56
57     list : function (path) {
58         var listing = [];
59
60         var f = Gio.file_new_for_path(String(path));
61         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
62
63         var next_file = null;
64
65         while ((next_file = file_enum.next_file(null)) != null) {
66           listing.push(next_file.get_display_name());
67         }
68
69         file_enum.close(null);
70
71         listing.sort();
72
73         return listing;
74     },
75
76     mtime : function (path) {
77         var f = Gio.file_new_for_path(String(path));
78         var mtime = new GLib.TimeVal();
79
80         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
81         info.get_modification_time(mtime);
82
83         return new Date(mtime.tv_sec * 1000);
84     },
85
86     canonical : function (path) {
87         var f = Gio.file_new_for_path(String(path));
88         var can = f.resolve_relative_path('');
89         return can.get_path();
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      * @arg path {String} File to remove
120      * 
121      * 
122      */
123     remove : function (path)
124     {
125         var f = Gio.file_new_for_path(String(path));
126         return f.delete();
127     }
128     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
129     silentRecursiveCopy : function (fromDir, toDir) {
130         var filesToCopy = File.recursiveListing(fromDir);
131         var srcPath, destPath, src, dest;
132
133         for (var index in filesToCopy) {
134           srcPath = File.join(String(fromDir), filesToCopy[index]);
135           destPath = File.join(String(toDir), filesToCopy[index]);
136
137           if (File.isFile(srcPath) && !File.isFile(destPath)) {
138             File.copyFile(srcPath, destPath);
139           }
140           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
141             File.mkdir(destPath);
142           }
143
144         }
145     },
146
147     mkdir : function (destPath) {
148         var dest = Gio.file_new_for_path(String(destPath));
149         return dest.make_directory(null, null);
150     },
151
152     copyFile : function (srcPath, destPath) {
153         var dest = Gio.file_new_for_path(String(destPath));
154         var src = Gio.file_new_for_path(String(srcPath));
155
156         // a bit of a hack for the fact that Gio.File.copy arguments
157         // can be nulled, but not according to the GIR file
158         return src.copy(dest, Gio.FileCopyFlags.NONE);
159     },
160
161     recursiveListing : function (dir) {
162
163         function recursiveListingInternal(prefix, listing, dir) {
164           var entries = File.list(dir);
165           var next, fullPath;
166
167           for (var index in entries) {
168             next = entries[index];
169             fullPath = File.join(prefix, dir, next);
170
171             if (File.isDirectory(fullPath)) {
172               listing.push(next);
173               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
174             }
175             else {
176               if (prefix) {
177                 next = File.join(prefix, next);
178               }
179               listing.push(next);
180             }
181           }
182
183           return listing;
184         }
185
186         return recursiveListingInternal('', [], dir);
187     }
188
189 };