root / src / pithos / content / util.js @ 67:0225d11a50b4
History | View | Annotate | Download (7.8 kB)
1 | 46:f16f6e0e5b69 | loverdos | // The Pithos File Manager Firefox Extension is funded by GRNET S.A.
|
---|---|---|---|
2 | 46:f16f6e0e5b69 | loverdos | // (http://www.grnet.gr)
|
3 | 46:f16f6e0e5b69 | loverdos | //
|
4 | 46:f16f6e0e5b69 | loverdos | // Copyright (c) 2009, Christos KK Loverdos, Vassilios Karakoidas.
|
5 | 46:f16f6e0e5b69 | loverdos | // All rights reserved.
|
6 | 46:f16f6e0e5b69 | loverdos | //
|
7 | 46:f16f6e0e5b69 | loverdos | // Redistribution and use in source and binary forms, with or without
|
8 | 46:f16f6e0e5b69 | loverdos | // modification, are permitted provided that the following conditions are
|
9 | 46:f16f6e0e5b69 | loverdos | // met:
|
10 | 46:f16f6e0e5b69 | loverdos | //
|
11 | 46:f16f6e0e5b69 | loverdos | // - Redistributions of source code must retain the above copyright
|
12 | 46:f16f6e0e5b69 | loverdos | // notice, this list of conditions and the following disclaimer.
|
13 | 46:f16f6e0e5b69 | loverdos | // - Redistributions in binary form must reproduce the above
|
14 | 46:f16f6e0e5b69 | loverdos | // copyright notice, this list of conditions and the following
|
15 | 46:f16f6e0e5b69 | loverdos | // disclaimer in the documentation and/or other materials provided
|
16 | 46:f16f6e0e5b69 | loverdos | // with the distribution.
|
17 | 46:f16f6e0e5b69 | loverdos | // - Neither the name of GRNET S.A. nor the names of its contributors
|
18 | 46:f16f6e0e5b69 | loverdos | // may be used to endorse or promote products derived from this
|
19 | 46:f16f6e0e5b69 | loverdos | // software without specific prior written permission.
|
20 | 46:f16f6e0e5b69 | loverdos | //
|
21 | 46:f16f6e0e5b69 | loverdos | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22 | 46:f16f6e0e5b69 | loverdos | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23 | 46:f16f6e0e5b69 | loverdos | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24 | 46:f16f6e0e5b69 | loverdos | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25 | 46:f16f6e0e5b69 | loverdos | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26 | 46:f16f6e0e5b69 | loverdos | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
27 | 46:f16f6e0e5b69 | loverdos | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28 | 46:f16f6e0e5b69 | loverdos | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
29 | 46:f16f6e0e5b69 | loverdos | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
30 | 46:f16f6e0e5b69 | loverdos | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31 | 46:f16f6e0e5b69 | loverdos | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 | 27:105c773158be | loverdos | |
33 | 18:eb03eccf0574 | loverdos | function isWindows() { |
34 | 25:6303472e84ea | loverdos | return (navigator.appVersion.indexOf("Win") != -1); |
35 | 18:eb03eccf0574 | loverdos | } |
36 | 18:eb03eccf0574 | loverdos | |
37 | 18:eb03eccf0574 | loverdos | function isUnix() { |
38 | 25:6303472e84ea | loverdos | return (navigator.appVersion.indexOf("Win") == -1); |
39 | 18:eb03eccf0574 | loverdos | } |
40 | 18:eb03eccf0574 | loverdos | |
41 | 30:baf20476685c | loverdos | function format_number(n) { |
42 | 30:baf20476685c | loverdos | var s = n.toString();
|
43 | 30:baf20476685c | loverdos | var sr = new Array(); |
44 | 30:baf20476685c | loverdos | |
45 | 30:baf20476685c | loverdos | for ( i = (s.length - 1),k = 1; i >= 0; i--, k++ ) { |
46 | 30:baf20476685c | loverdos | sr.push(s.charAt(i)); |
47 | 30:baf20476685c | loverdos | if ( (k % 3) == 0 && (i != 0) ) { |
48 | 30:baf20476685c | loverdos | sr.push(',');
|
49 | 30:baf20476685c | loverdos | } |
50 | 30:baf20476685c | loverdos | } |
51 | 30:baf20476685c | loverdos | |
52 | 30:baf20476685c | loverdos | sr.reverse(); |
53 | 30:baf20476685c | loverdos | |
54 | 30:baf20476685c | loverdos | return sr.join(''); |
55 | 30:baf20476685c | loverdos | } |
56 | 30:baf20476685c | loverdos | |
57 | 18:eb03eccf0574 | loverdos | function getHomeDirectory() { |
58 | 18:eb03eccf0574 | loverdos | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
59 | 18:eb03eccf0574 | loverdos | var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties); |
60 | 19:f71f95fc3f7a | loverdos | var homeDirFile = dirService.get("Home", Components.interfaces.nsIFile); |
61 | 18:eb03eccf0574 | loverdos | |
62 | 18:eb03eccf0574 | loverdos | return homeDirFile.path;
|
63 | 18:eb03eccf0574 | loverdos | } |
64 | 18:eb03eccf0574 | loverdos | |
65 | 29:ec9750e9cceb | loverdos | /* This function received an already parsed JSON object and converts it to a formatted string */
|
66 | 29:ec9750e9cceb | loverdos | function jsonToString(jsonobj) { |
67 | 29:ec9750e9cceb | loverdos | return JSON.stringify(jsonobj);
|
68 | 29:ec9750e9cceb | loverdos | } |
69 | 32:5dc65483ee65 | loverdos | |
70 | 25:6303472e84ea | loverdos | function isRootDirectory(directory) { |
71 | 30:baf20476685c | loverdos | var root_dirs = new Array(); |
72 | 30:baf20476685c | loverdos | |
73 | 30:baf20476685c | loverdos | root_dirs.push(connection_obj.user_homepage().fileroot); |
74 | 30:baf20476685c | loverdos | root_dirs.push(connection_obj.user_homepage().others); |
75 | 30:baf20476685c | loverdos | root_dirs.push(connection_obj.user_homepage().shared); |
76 | 46:f16f6e0e5b69 | loverdos | root_dirs.push(connection_obj.user_homepage().trash); |
77 | 30:baf20476685c | loverdos | |
78 | 40:fa884f08534a | loverdos | var search_dir = connection_obj.rest_url + '/search/'; |
79 | 40:fa884f08534a | loverdos | |
80 | 30:baf20476685c | loverdos | if(root_dirs.indexOf(directory) >= 0) { return true; } |
81 | 25:6303472e84ea | loverdos | |
82 | 40:fa884f08534a | loverdos | if(directory.indexOf(search_dir) >= 0) { return true; } |
83 | 40:fa884f08534a | loverdos | |
84 | 25:6303472e84ea | loverdos | if(isWindows()) {
|
85 | 25:6303472e84ea | loverdos | return (getLocalDrives().indexOf(directory) >= 0); |
86 | 25:6303472e84ea | loverdos | } |
87 | 25:6303472e84ea | loverdos | |
88 | 25:6303472e84ea | loverdos | if(isUnix()) {
|
89 | 25:6303472e84ea | loverdos | return (directory == getHomeDirectory());
|
90 | 25:6303472e84ea | loverdos | } |
91 | 25:6303472e84ea | loverdos | |
92 | 25:6303472e84ea | loverdos | return false; |
93 | 25:6303472e84ea | loverdos | } |
94 | 25:6303472e84ea | loverdos | |
95 | 38:412c03f0cdcd | loverdos | function getRootDirectories() { |
96 | 38:412c03f0cdcd | loverdos | var root_dirs = new Array(); |
97 | 38:412c03f0cdcd | loverdos | |
98 | 38:412c03f0cdcd | loverdos | root_dirs.push(connection_obj.user_homepage().fileroot); |
99 | 38:412c03f0cdcd | loverdos | root_dirs.push(connection_obj.user_homepage().others); |
100 | 38:412c03f0cdcd | loverdos | root_dirs.push(connection_obj.user_homepage().shared); |
101 | 46:f16f6e0e5b69 | loverdos | root_dirs.push(connection_obj.user_homepage().trash); |
102 | 40:fa884f08534a | loverdos | root_dirs.push(connection_obj.rest_url + '/search/');
|
103 | 38:412c03f0cdcd | loverdos | |
104 | 38:412c03f0cdcd | loverdos | return root_dirs;
|
105 | 38:412c03f0cdcd | loverdos | } |
106 | 38:412c03f0cdcd | loverdos | |
107 | 25:6303472e84ea | loverdos | function getParentFolder(path) { |
108 | 25:6303472e84ea | loverdos | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
109 | 25:6303472e84ea | loverdos | var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
110 | 25:6303472e84ea | loverdos | try {
|
111 | 25:6303472e84ea | loverdos | file.initWithPath(path); |
112 | 25:6303472e84ea | loverdos | } catch(error) {
|
113 | 25:6303472e84ea | loverdos | if (isUnix()) {
|
114 | 25:6303472e84ea | loverdos | return getHomeDirectory();
|
115 | 25:6303472e84ea | loverdos | } |
116 | 25:6303472e84ea | loverdos | |
117 | 25:6303472e84ea | loverdos | // is windows
|
118 | 25:6303472e84ea | loverdos | return "C:\\"; |
119 | 25:6303472e84ea | loverdos | } |
120 | 25:6303472e84ea | loverdos | |
121 | 25:6303472e84ea | loverdos | if (isWindows()) {
|
122 | 40:fa884f08534a | loverdos | if ( file.parent == null) { return null; } |
123 | 40:fa884f08534a | loverdos | |
124 | 25:6303472e84ea | loverdos | return file.parent.path + '\\'; |
125 | 25:6303472e84ea | loverdos | } |
126 | 25:6303472e84ea | loverdos | |
127 | 25:6303472e84ea | loverdos | if (isUnix()) {
|
128 | 40:fa884f08534a | loverdos | if ( file.parent == null) { return null; } |
129 | 25:6303472e84ea | loverdos | return file.parent.path;
|
130 | 25:6303472e84ea | loverdos | } |
131 | 25:6303472e84ea | loverdos | |
132 | 40:fa884f08534a | loverdos | return null; |
133 | 25:6303472e84ea | loverdos | } |
134 | 25:6303472e84ea | loverdos | |
135 | 25:6303472e84ea | loverdos | function getLocalDrives() { |
136 | 25:6303472e84ea | loverdos | var drives = [];
|
137 | 25:6303472e84ea | loverdos | |
138 | 25:6303472e84ea | loverdos | if (isWindows()) {
|
139 | 25:6303472e84ea | loverdos | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
140 | 36:7e514685de80 | loverdos | var root = null; //Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
141 | 25:6303472e84ea | loverdos | try {
|
142 | 36:7e514685de80 | loverdos | root = moz.newLocalFile("\\\\.");
|
143 | 25:6303472e84ea | loverdos | } catch(error) {
|
144 | 37:084d37e3398b | loverdos | alert('ERROR: Cannot enumerate available drives, error = ' + error);
|
145 | 25:6303472e84ea | loverdos | return drives;
|
146 | 25:6303472e84ea | loverdos | } |
147 | 25:6303472e84ea | loverdos | |
148 | 25:6303472e84ea | loverdos | var drivesEnum = root.directoryEntries;
|
149 | 25:6303472e84ea | loverdos | while (drivesEnum.hasMoreElements()) {
|
150 | 25:6303472e84ea | loverdos | drives.push(drivesEnum.getNext().QueryInterface(Components.interfaces.nsILocalFile).path + "\\");
|
151 | 25:6303472e84ea | loverdos | } |
152 | 25:6303472e84ea | loverdos | } |
153 | 25:6303472e84ea | loverdos | |
154 | 25:6303472e84ea | loverdos | if (isUnix()) {
|
155 | 25:6303472e84ea | loverdos | drives.push(getHomeDirectory()); |
156 | 25:6303472e84ea | loverdos | } |
157 | 25:6303472e84ea | loverdos | |
158 | 25:6303472e84ea | loverdos | return drives;
|
159 | 25:6303472e84ea | loverdos | } |
160 | 25:6303472e84ea | loverdos | |
161 | 38:412c03f0cdcd | loverdos | function getRemoteParentDir(uri) { |
162 | 38:412c03f0cdcd | loverdos | var fileroots = getRootDirectories();
|
163 | 38:412c03f0cdcd | loverdos | var fileroot = null; |
164 | 27:105c773158be | loverdos | |
165 | 38:412c03f0cdcd | loverdos | if ( isRootDirectory(uri) ) {
|
166 | 38:412c03f0cdcd | loverdos | return null; |
167 | 27:105c773158be | loverdos | } |
168 | 27:105c773158be | loverdos | |
169 | 38:412c03f0cdcd | loverdos | // find the fileroot
|
170 | 38:412c03f0cdcd | loverdos | for ( i = 0; i < fileroots.length; i++ ) { |
171 | 38:412c03f0cdcd | loverdos | if ( uri.indexOf(fileroots[i]) >= 0 ) { |
172 | 38:412c03f0cdcd | loverdos | fileroot = fileroots[i]; |
173 | 38:412c03f0cdcd | loverdos | } |
174 | 38:412c03f0cdcd | loverdos | } |
175 | 38:412c03f0cdcd | loverdos | |
176 | 38:412c03f0cdcd | loverdos | //
|
177 | 38:412c03f0cdcd | loverdos | var result = new RegExp("(.+\\/)[^/]+\\/").exec(uri); |
178 | 38:412c03f0cdcd | loverdos | if ( result != undefined ) { |
179 | 38:412c03f0cdcd | loverdos | return result[1]; |
180 | 27:105c773158be | loverdos | } |
181 | 27:105c773158be | loverdos | |
182 | 38:412c03f0cdcd | loverdos | return null; |
183 | 38:412c03f0cdcd | loverdos | } |
184 | 38:412c03f0cdcd | loverdos | |
185 | 38:412c03f0cdcd | loverdos | function getRemoteParentDirList(uri) { |
186 | 38:412c03f0cdcd | loverdos | // check root, extended version
|
187 | 38:412c03f0cdcd | loverdos | var result = new Array(); |
188 | 38:412c03f0cdcd | loverdos | var uri_local = uri;
|
189 | 38:412c03f0cdcd | loverdos | |
190 | 38:412c03f0cdcd | loverdos | result.push(uri_local); |
191 | 38:412c03f0cdcd | loverdos | |
192 | 38:412c03f0cdcd | loverdos | while (uri_local != null) { |
193 | 38:412c03f0cdcd | loverdos | uri_local = getRemoteParentDir(uri_local); |
194 | 38:412c03f0cdcd | loverdos | if (uri_local == null) { break; } |
195 | 38:412c03f0cdcd | loverdos | result.push(uri_local); |
196 | 38:412c03f0cdcd | loverdos | } |
197 | 27:105c773158be | loverdos | |
198 | 27:105c773158be | loverdos | return result;
|
199 | 27:105c773158be | loverdos | } |
200 | 27:105c773158be | loverdos | |
201 | 25:6303472e84ea | loverdos | function getLocalParentDirList(path) { |
202 | 25:6303472e84ea | loverdos | var result = new Array(); |
203 | 25:6303472e84ea | loverdos | var current = path;
|
204 | 25:6303472e84ea | loverdos | |
205 | 25:6303472e84ea | loverdos | while(!isRootDirectory(current)) {
|
206 | 25:6303472e84ea | loverdos | result.push(current); |
207 | 25:6303472e84ea | loverdos | current = getParentFolder(current); |
208 | 25:6303472e84ea | loverdos | } |
209 | 25:6303472e84ea | loverdos | |
210 | 25:6303472e84ea | loverdos | result.push(current); |
211 | 25:6303472e84ea | loverdos | |
212 | 25:6303472e84ea | loverdos | return result;
|
213 | 40:fa884f08534a | loverdos | } |
214 | 40:fa884f08534a | loverdos | |
215 | 40:fa884f08534a | loverdos | function saveBinaryFile(filename, data) { |
216 | 40:fa884f08534a | loverdos | try {
|
217 | 40:fa884f08534a | loverdos | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
218 | 40:fa884f08534a | loverdos | var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
219 | 40:fa884f08534a | loverdos | |
220 | 40:fa884f08534a | loverdos | aFile.initWithPath( filename ); |
221 | 40:fa884f08534a | loverdos | aFile.createUnique( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 600);
|
222 | 40:fa884f08534a | loverdos | |
223 | 67:0225d11a50b4 | bkarak | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
224 | 40:fa884f08534a | loverdos | var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream); |
225 | 40:fa884f08534a | loverdos | stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate |
226 | 40:fa884f08534a | loverdos | |
227 | 40:fa884f08534a | loverdos | stream.write(data, data.length); |
228 | 40:fa884f08534a | loverdos | if (stream instanceof Components.interfaces.nsISafeOutputStream) { |
229 | 40:fa884f08534a | loverdos | stream.finish(); |
230 | 40:fa884f08534a | loverdos | } else {
|
231 | 40:fa884f08534a | loverdos | stream.close(); |
232 | 40:fa884f08534a | loverdos | } |
233 | 40:fa884f08534a | loverdos | |
234 | 40:fa884f08534a | loverdos | return true; |
235 | 40:fa884f08534a | loverdos | } catch (error) {
|
236 | 40:fa884f08534a | loverdos | return false; |
237 | 40:fa884f08534a | loverdos | } |
238 | 40:fa884f08534a | loverdos | } |
239 | 40:fa884f08534a | loverdos | |
240 | 40:fa884f08534a | loverdos | function loadBinaryFile(filename) { |
241 | 40:fa884f08534a | loverdos | try {
|
242 | 40:fa884f08534a | loverdos | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
243 | 40:fa884f08534a | loverdos | var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); |
244 | 40:fa884f08534a | loverdos | var url = ios.newURI(connection_obj.create_request_get(filename), null, null); |
245 | 40:fa884f08534a | loverdos | |
246 | 40:fa884f08534a | loverdos | if (!url || !url.schemeIs("file")) throw "Expected a file URL."; |
247 | 40:fa884f08534a | loverdos | |
248 | 40:fa884f08534a | loverdos | var afile = url.QueryInterface(Components.interfaces.nsIFileURL).file;
|
249 | 67:0225d11a50b4 | bkarak | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
250 | 40:fa884f08534a | loverdos | var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); |
251 | 40:fa884f08534a | loverdos | |
252 | 40:fa884f08534a | loverdos | istream.init(afile, -1, -1, false); |
253 | 40:fa884f08534a | loverdos | |
254 | 67:0225d11a50b4 | bkarak | netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
255 | 40:fa884f08534a | loverdos | var bstream = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream); |
256 | 40:fa884f08534a | loverdos | bstream.setInputStream(istream); |
257 | 40:fa884f08534a | loverdos | |
258 | 40:fa884f08534a | loverdos | return bstream.readBytes(bstream.available());
|
259 | 40:fa884f08534a | loverdos | } catch (error) {
|
260 | 40:fa884f08534a | loverdos | alert(error); |
261 | 40:fa884f08534a | loverdos | return null; |
262 | 40:fa884f08534a | loverdos | } |
263 | 18:eb03eccf0574 | loverdos | } |