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