Added missing image assets
[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 var File = {
8
9     SEPARATOR : '/',
10
11     join : function () {
12         var out = "";
13         for (var i = 0; i < arguments.length; i++) {
14             if (i == 0) {
15               out += arguments[i].rtrim(File.SEPARATOR);
16             }
17             else if (i == arguments.length - 1) {
18               out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
19             }
20             else {
21               out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
22             }
23         }
24         return out;
25     },
26
27     read : function (path) {
28         var out = {};
29         GLib.file_get_contents(path, out, null, null);
30         return out['value'];
31     },
32
33     isFile : function (path) {
34       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
35     },
36     exists : function (path) {
37       return GLib.file_test(path, GLib.FileTest.EXISTS);
38     },
39     isDirectory : function (path) {
40       return GLib.file_test(path, GLib.FileTest.IS_DIR);
41     },
42
43     list : function (path) {
44         var listing = [];
45
46         var f = Gio.file_new_for_path(String(path));
47         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
48
49         var next_file = null;
50
51         while ((next_file = file_enum.next_file(null)) != null) {
52           listing.push(next_file.get_display_name());
53         }
54
55         file_enum.close(null);
56
57         listing.sort();
58
59         return listing;
60     },
61
62     mtime : function (path) {
63         var f = Gio.file_new_for_path(String(path));
64         var mtime = new GLib.TimeVal();
65
66         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
67         info.get_modification_time(mtime);
68
69         return new Date(mtime.tv_sec * 1000);
70     },
71
72     canonical : function (path) {
73         var f = Gio.file_new_for_path(String(path));
74         var can = f.resolve_relative_path('');
75         return can.get_path();
76     },
77
78     write : function (path, string) {
79         var f = Gio.file_new_for_path(String(path));
80         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
81         data_out.put_string(string, null);
82         data_out.close(null);
83     },
84
85     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
86     silentRecursiveCopy : function (fromDir, toDir) {
87         var filesToCopy = File.recursiveListing(fromDir);
88         var srcPath, destPath, src, dest;
89
90         for (index in filesToCopy) {
91           srcPath = File.join(String(fromDir), filesToCopy[index]);
92           destPath = File.join(String(toDir), filesToCopy[index]);
93
94           if (File.isFile(srcPath) && !File.isFile(destPath)) {
95             File.copyFile(srcPath, destPath);
96           }
97           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
98             File.mkdir(destPath);
99           }
100
101         }
102     },
103
104     mkdir : function (destPath) {
105         var dest = Gio.file_new_for_path(String(destPath));
106         return dest.make_directory(null, null);
107     },
108
109     copyFile : function (srcPath, destPath) {
110         var dest = Gio.file_new_for_path(String(destPath));
111         var src = Gio.file_new_for_path(String(srcPath));
112
113         // a bit of a hack for the fact that Gio.File.copy arguments
114         // can be nulled, but not according to the GIR file
115         return src.copy(dest, Gio.FileCopyFlags.NONE, null, function(_a,_b,_c) {return true;}, true);
116     },
117
118     recursiveListing : function (dir) {
119         
120         function recursiveListingInternal(prefix, listing, dir) {
121           var entries = File.list(dir);
122           var next, fullPath;
123
124           for (var index in entries) {
125             next = entries[index];
126             fullPath = File.join(prefix, dir, next);
127
128             if (File.isDirectory(fullPath)) {
129               listing.push(next);
130               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
131             }
132             else {
133               if (prefix) {
134                 next = File.join(prefix, next);
135               }
136               listing.push(next);
137             }
138           }
139
140           return listing;
141         }
142         
143         return recursiveListingInternal('', [], dir);
144     }
145
146 };