Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.storage.browser");
12
dojo.require("dojo.storage");
13
dojo.require("dojo.flash");
14
dojo.require("dojo.json");
15
dojo.require("dojo.uri.*");
16
dojo.storage.browser.FileStorageProvider = function () {
17
};
18
dojo.inherits(dojo.storage.browser.FileStorageProvider, dojo.storage);
19
dojo.storage.browser.FileStorageProvider._KEY_INDEX_FILENAME = "__dojoAllKeys";
20
dojo.storage.browser.FileStorageProvider._APPLET_ID = "__dojoFileJavaObj";
21
dojo.lang.extend(dojo.storage.browser.FileStorageProvider, {namespace:"default", initialized:false, _available:null, _statusHandler:null, _keyIndex:new Array(), initialize:function () {
22
	if (djConfig["disableFileStorage"] == true) {
23
		return;
24
	}
25
	this._loadKeyIndex();
26
	this.initialized = true;
27
	dojo.storage.manager.loaded();
28
}, isAvailable:function () {
29
	this._available = false;
30
	var protocol = window.location.protocol;
31
	if (protocol.indexOf("file") != -1 || protocol.indexOf("chrome") != -1) {
32
		this._available = this._isAvailableXPCOM();
33
		if (this._available == false) {
34
			this._available = this._isAvailableActiveX();
35
		}
36
	}
37
	return this._available;
38
}, put:function (key, value, resultsHandler) {
39
	if (this.isValidKey(key) == false) {
40
		dojo.raise("Invalid key given: " + key);
41
	}
42
	this._statusHandler = resultsHandler;
43
	try {
44
		this._save(key, value);
45
		resultsHandler.call(null, dojo.storage.SUCCESS, key);
46
	}
47
	catch (e) {
48
		this._statusHandler.call(null, dojo.storage.FAILED, key, e.toString());
49
	}
50
}, get:function (key) {
51
	if (this.isValidKey(key) == false) {
52
		dojo.raise("Invalid key given: " + key);
53
	}
54
	var results = this._load(key);
55
	return results;
56
}, getKeys:function () {
57
	return this._keyIndex;
58
}, hasKey:function (key) {
59
	if (this.isValidKey(key) == false) {
60
		dojo.raise("Invalid key given: " + key);
61
	}
62
	this._loadKeyIndex();
63
	var exists = false;
64
	for (var i = 0; i < this._keyIndex.length; i++) {
65
		if (this._keyIndex[i] == key) {
66
			exists = true;
67
		}
68
	}
69
	return exists;
70
}, clear:function () {
71
	this._loadKeyIndex();
72
	var keyIndex = new Array();
73
	for (var i = 0; i < this._keyIndex.length; i++) {
74
		keyIndex[keyIndex.length] = new String(this._keyIndex[i]);
75
	}
76
	for (var i = 0; i < keyIndex.length; i++) {
77
		this.remove(keyIndex[i]);
78
	}
79
}, remove:function (key) {
80
	if (this.isValidKey(key) == false) {
81
		dojo.raise("Invalid key given: " + key);
82
	}
83
	this._loadKeyIndex();
84
	for (var i = 0; i < this._keyIndex.length; i++) {
85
		if (this._keyIndex[i] == key) {
86
			this._keyIndex.splice(i, 1);
87
			break;
88
		}
89
	}
90
	this._save(dojo.storage.browser.FileStorageProvider._KEY_INDEX_FILENAME, this._keyIndex, false);
91
	var fullPath = this._getPagePath() + key + ".txt";
92
	if (this._isAvailableXPCOM()) {
93
		this._removeXPCOM(fullPath);
94
	} else {
95
		if (this._isAvailableActiveX()) {
96
			this._removeActiveX(fullPath);
97
		}
98
	}
99
}, isPermanent:function () {
100
	return true;
101
}, getMaximumSize:function () {
102
	return dojo.storage.SIZE_NO_LIMIT;
103
}, hasSettingsUI:function () {
104
	return false;
105
}, showSettingsUI:function () {
106
	dojo.raise(this.getType() + " does not support a storage settings user-interface");
107
}, hideSettingsUI:function () {
108
	dojo.raise(this.getType() + " does not support a storage settings user-interface");
109
}, getType:function () {
110
	return "dojo.storage.browser.FileStorageProvider";
111
}, _save:function (key, value, updateKeyIndex) {
112
	if (typeof updateKeyIndex == "undefined") {
113
		updateKeyIndex = true;
114
	}
115
	if (dojo.lang.isString(value) == false) {
116
		value = dojo.json.serialize(value);
117
		value = "/* JavaScript */\n" + value + "\n\n";
118
	}
119
	var fullPath = this._getPagePath() + key + ".txt";
120
	if (this._isAvailableXPCOM()) {
121
		this._saveFileXPCOM(fullPath, value);
122
	} else {
123
		if (this._isAvailableActiveX()) {
124
			this._saveFileActiveX(fullPath, value);
125
		}
126
	}
127
	if (updateKeyIndex) {
128
		this._updateKeyIndex(key);
129
	}
130
}, _load:function (key) {
131
	var fullPath = this._getPagePath() + key + ".txt";
132
	var results = null;
133
	if (this._isAvailableXPCOM()) {
134
		results = this._loadFileXPCOM(fullPath);
135
	} else {
136
		if (this._isAvailableActiveX()) {
137
			results = this._loadFileActiveX(fullPath);
138
		} else {
139
			if (this._isAvailableJava()) {
140
				results = this._loadFileJava(fullPath);
141
			}
142
		}
143
	}
144
	if (results == null) {
145
		return null;
146
	}
147
	if (!dojo.lang.isUndefined(results) && results != null && /^\/\* JavaScript \*\//.test(results)) {
148
		results = dojo.json.evalJson(results);
149
	}
150
	return results;
151
}, _updateKeyIndex:function (key) {
152
	this._loadKeyIndex();
153
	var alreadyAdded = false;
154
	for (var i = 0; i < this._keyIndex.length; i++) {
155
		if (this._keyIndex[i] == key) {
156
			alreadyAdded = true;
157
			break;
158
		}
159
	}
160
	if (alreadyAdded == false) {
161
		this._keyIndex[this._keyIndex.length] = key;
162
	}
163
	this._save(dojo.storage.browser.FileStorageProvider._KEY_INDEX_FILENAME, this._keyIndex, false);
164
}, _loadKeyIndex:function () {
165
	var indexContents = this._load(dojo.storage.browser.FileStorageProvider._KEY_INDEX_FILENAME);
166
	if (indexContents == null) {
167
		this._keyIndex = new Array();
168
	} else {
169
		this._keyIndex = indexContents;
170
	}
171
}, _saveFileXPCOM:function (filename, value) {
172
	try {
173
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
174
		var f = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
175
		f.initWithPath(filename);
176
		var ouputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
177
		ouputStream.init(f, 32 | 4 | 8, 256 + 128, null);
178
		ouputStream.write(value, value.length);
179
		ouputStream.close();
180
	}
181
	catch (e) {
182
		var msg = e.toString();
183
		if (e.name && e.message) {
184
			msg = e.name + ": " + e.message;
185
		}
186
		dojo.raise("dojo.storage.browser.FileStorageProvider._saveFileXPCOM(): " + msg);
187
	}
188
}, _loadFileXPCOM:function (filename) {
189
	try {
190
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
191
		var f = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
192
		f.initWithPath(filename);
193
		if (f.exists() == false) {
194
			return null;
195
		}
196
		var inp = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
197
		inp.init(f, 1, 4, null);
198
		var inputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
199
		inputStream.init(inp);
200
		var results = inputStream.read(inputStream.available());
201
		return results;
202
	}
203
	catch (e) {
204
		var msg = e.toString();
205
		if (e.name && e.message) {
206
			msg = e.name + ": " + e.message;
207
		}
208
		dojo.raise("dojo.storage.browser.FileStorageProvider._loadFileXPCOM(): " + msg);
209
	}
210
	return null;
211
}, _saveFileActiveX:function (filename, value) {
212
	try {
213
		var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
214
		var f = fileSystem.OpenTextFile(filename, 2, true);
215
		f.Write(value);
216
		f.Close();
217
	}
218
	catch (e) {
219
		var msg = e.toString();
220
		if (e.name && e.message) {
221
			msg = e.name + ": " + e.message;
222
		}
223
		dojo.raise("dojo.storage.browser.FileStorageProvider._saveFileActiveX(): " + msg);
224
	}
225
}, _loadFileActiveX:function (filename) {
226
	try {
227
		var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
228
		if (fileSystem.FileExists(filename) == false) {
229
			return null;
230
		}
231
		var f = fileSystem.OpenTextFile(filename, 1);
232
		var results = f.ReadAll();
233
		f.Close();
234
		return results;
235
	}
236
	catch (e) {
237
		var msg = e.toString();
238
		if (e.name && e.message) {
239
			msg = e.name + ": " + e.message;
240
		}
241
		dojo.raise("dojo.storage.browser.FileStorageProvider._loadFileActiveX(): " + msg);
242
	}
243
}, _saveFileJava:function (filename, value) {
244
	try {
245
		var applet = dojo.byId(dojo.storage.browser.FileStorageProvider._APPLET_ID);
246
		applet.save(filename, value);
247
	}
248
	catch (e) {
249
		var msg = e.toString();
250
		if (e.name && e.message) {
251
			msg = e.name + ": " + e.message;
252
		}
253
		dojo.raise("dojo.storage.browser.FileStorageProvider._saveFileJava(): " + msg);
254
	}
255
}, _loadFileJava:function (filename) {
256
	try {
257
		var applet = dojo.byId(dojo.storage.browser.FileStorageProvider._APPLET_ID);
258
		var results = applet.load(filename);
259
		return results;
260
	}
261
	catch (e) {
262
		var msg = e.toString();
263
		if (e.name && e.message) {
264
			msg = e.name + ": " + e.message;
265
		}
266
		dojo.raise("dojo.storage.browser.FileStorageProvider._loadFileJava(): " + msg);
267
	}
268
}, _isAvailableActiveX:function () {
269
	try {
270
		if (window.ActiveXObject) {
271
			var fileSystem = new window.ActiveXObject("Scripting.FileSystemObject");
272
			return true;
273
		}
274
	}
275
	catch (e) {
276
		dojo.debug(e);
277
	}
278
	return false;
279
}, _isAvailableXPCOM:function () {
280
	try {
281
		if (window.Components) {
282
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
283
			Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
284
			return true;
285
		}
286
	}
287
	catch (e) {
288
		dojo.debug(e);
289
	}
290
	return false;
291
}, _isAvailableJava:function () {
292
	try {
293
		if (dojo.render.html.safari == true || dojo.render.html.opera == true()) {
294
			if (navigator.javaEnabled() == true) {
295
				return true;
296
			}
297
		}
298
	}
299
	catch (e) {
300
		dojo.debug(e);
301
	}
302
	return false;
303
}, _getPagePath:function () {
304
	var path = window.location.pathname;
305
	if (/\.html?$/i.test(path)) {
306
		path = path.replace(/(?:\/|\\)?[^\.\/\\]*\.html?$/, "");
307
	}
308
	if (/^\/?[a-z]+\:/i.test(path)) {
309
		path = path.replace(/^\/?/, "");
310
		path = path.replace(/\//g, "\\");
311
	} else {
312
		if (/^[\/\\]{2,3}[^\/]/.test(path)) {
313
			path = path.replace(/^[\/\\]{2,3}/, "");
314
			path = path.replace(/\//g, "\\");
315
			path = "\\\\" + path;
316
		}
317
	}
318
	if (/\/$/.test(path) == false && /\\$/.test(path) == false) {
319
		if (/\//.test(path)) {
320
			path += "/";
321
		} else {
322
			path += "\\";
323
		}
324
	}
325
	path = unescape(path);
326
	return path;
327
}, _removeXPCOM:function (filename) {
328
	try {
329
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
330
		var f = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
331
		f.initWithPath(filename);
332
		if (f.exists() == false || f.isDirectory()) {
333
			return;
334
		}
335
		if (f.isFile()) {
336
			f.remove(false);
337
		}
338
	}
339
	catch (e) {
340
		dojo.raise("dojo.storage.browser.FileStorageProvider.remove(): " + e.toString());
341
	}
342
}, _removeActiveX:function (filename) {
343
	try {
344
		var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
345
		fileSystem.DeleteFile(filename);
346
	}
347
	catch (e) {
348
		dojo.raise("dojo.storage.browser.FileStorageProvider.remove(): " + e.toString());
349
	}
350
}, _removeJava:function (filename) {
351
	try {
352
		var applet = dojo.byId(dojo.storage.browser.FileStorageProvider._APPLET_ID);
353
		applet.remove(filename);
354
	}
355
	catch (e) {
356
		var msg = e.toString();
357
		if (e.name && e.message) {
358
			msg = e.name + ": " + e.message;
359
		}
360
		dojo.raise("dojo.storage.browser.FileStorageProvider._removeJava(): " + msg);
361
	}
362
}, _writeApplet:function () {
363
	var archive = dojo.uri.moduleUri("dojo", "../DojoFileStorageProvider.jar").toString();
364
	var tag = "<applet " + "id='" + dojo.storage.browser.FileStorageProvider._APPLET_ID + "' " + "style='position: absolute; top: -500px; left: -500px; width: 1px; height: 1px;' " + "code='DojoFileStorageProvider.class' " + "archive='" + archive + "' " + "width='1' " + "height='1' " + ">" + "</applet>";
365
	document.writeln(tag);
366
}});
367
dojo.storage.browser.WhatWGStorageProvider = function () {
368
};
369
dojo.inherits(dojo.storage.browser.WhatWGStorageProvider, dojo.storage);
370
dojo.lang.extend(dojo.storage.browser.WhatWGStorageProvider, {namespace:"default", initialized:false, _domain:null, _available:null, _statusHandler:null, initialize:function () {
371
	if (djConfig["disableWhatWGStorage"] == true) {
372
		return;
373
	}
374
	this._domain = location.hostname;
375
	this.initialized = true;
376
	dojo.storage.manager.loaded();
377
}, isAvailable:function () {
378
	try {
379
		var myStorage = globalStorage[location.hostname];
380
	}
381
	catch (e) {
382
		this._available = false;
383
		return this._available;
384
	}
385
	this._available = true;
386
	return this._available;
387
}, put:function (key, value, resultsHandler) {
388
	if (this.isValidKey(key) == false) {
389
		dojo.raise("Invalid key given: " + key);
390
	}
391
	this._statusHandler = resultsHandler;
392
	if (dojo.lang.isString(value)) {
393
		value = "string:" + value;
394
	} else {
395
		value = dojo.json.serialize(value);
396
	}
397
	window.addEventListener("storage", function (evt) {
398
		resultsHandler.call(null, dojo.storage.SUCCESS, key);
399
	}, false);
400
	try {
401
		var myStorage = globalStorage[this._domain];
402
		myStorage.setItem(key, value);
403
	}
404
	catch (e) {
405
		this._statusHandler.call(null, dojo.storage.FAILED, key, e.toString());
406
	}
407
}, get:function (key) {
408
	if (this.isValidKey(key) == false) {
409
		dojo.raise("Invalid key given: " + key);
410
	}
411
	var myStorage = globalStorage[this._domain];
412
	var results = myStorage.getItem(key);
413
	if (results == null) {
414
		return null;
415
	}
416
	results = results.value;
417
	if (!dojo.lang.isUndefined(results) && results != null && /^string:/.test(results)) {
418
		results = results.substring("string:".length);
419
	} else {
420
		results = dojo.json.evalJson(results);
421
	}
422
	return results;
423
}, getKeys:function () {
424
	var myStorage = globalStorage[this._domain];
425
	var keysArray = new Array();
426
	for (i = 0; i < myStorage.length; i++) {
427
		keysArray[i] = myStorage.key(i);
428
	}
429
	return keysArray;
430
}, clear:function () {
431
	var myStorage = globalStorage[this._domain];
432
	var keys = new Array();
433
	for (var i = 0; i < myStorage.length; i++) {
434
		keys[keys.length] = myStorage.key(i);
435
	}
436
	for (var i = 0; i < keys.length; i++) {
437
		myStorage.removeItem(keys[i]);
438
	}
439
}, remove:function (key) {
440
	var myStorage = globalStorage[this._domain];
441
	myStorage.removeItem(key);
442
}, isPermanent:function () {
443
	return true;
444
}, getMaximumSize:function () {
445
	return dojo.storage.SIZE_NO_LIMIT;
446
}, hasSettingsUI:function () {
447
	return false;
448
}, showSettingsUI:function () {
449
	dojo.raise(this.getType() + " does not support a storage settings user-interface");
450
}, hideSettingsUI:function () {
451
	dojo.raise(this.getType() + " does not support a storage settings user-interface");
452
}, getType:function () {
453
	return "dojo.storage.browser.WhatWGProvider";
454
}});
455
dojo.storage.browser.FlashStorageProvider = function () {
456
};
457
dojo.inherits(dojo.storage.browser.FlashStorageProvider, dojo.storage);
458
dojo.lang.extend(dojo.storage.browser.FlashStorageProvider, {namespace:"default", initialized:false, _available:null, _statusHandler:null, initialize:function () {
459
	if (djConfig["disableFlashStorage"] == true) {
460
		return;
461
	}
462
	var loadedListener = function () {
463
		dojo.storage._flashLoaded();
464
	};
465
	dojo.flash.addLoadedListener(loadedListener);
466
	var swfloc6 = dojo.uri.moduleUri("dojo", "../Storage_version6.swf").toString();
467
	var swfloc8 = dojo.uri.moduleUri("dojo", "../Storage_version8.swf").toString();
468
	dojo.flash.setSwf({flash6:swfloc6, flash8:swfloc8, visible:false});
469
}, isAvailable:function () {
470
	if (djConfig["disableFlashStorage"] == true) {
471
		this._available = false;
472
	} else {
473
		this._available = true;
474
	}
475
	return this._available;
476
}, put:function (key, value, resultsHandler) {
477
	if (this.isValidKey(key) == false) {
478
		dojo.raise("Invalid key given: " + key);
479
	}
480
	this._statusHandler = resultsHandler;
481
	if (dojo.lang.isString(value)) {
482
		value = "string:" + value;
483
	} else {
484
		value = dojo.json.serialize(value);
485
	}
486
	dojo.flash.comm.put(key, value, this.namespace);
487
}, get:function (key) {
488
	if (this.isValidKey(key) == false) {
489
		dojo.raise("Invalid key given: " + key);
490
	}
491
	var results = dojo.flash.comm.get(key, this.namespace);
492
	if (results == "") {
493
		return null;
494
	}
495
	if (!dojo.lang.isUndefined(results) && results != null && /^string:/.test(results)) {
496
		results = results.substring("string:".length);
497
	} else {
498
		results = dojo.json.evalJson(results);
499
	}
500
	return results;
501
}, getKeys:function () {
502
	var results = dojo.flash.comm.getKeys(this.namespace);
503
	if (results == "") {
504
		return [];
505
	}
506
	return results.split(",");
507
}, clear:function () {
508
	dojo.flash.comm.clear(this.namespace);
509
}, remove:function (key) {
510
	dojo.unimplemented("dojo.storage.browser.FlashStorageProvider.remove");
511
}, isPermanent:function () {
512
	return true;
513
}, getMaximumSize:function () {
514
	return dojo.storage.SIZE_NO_LIMIT;
515
}, hasSettingsUI:function () {
516
	return true;
517
}, showSettingsUI:function () {
518
	dojo.flash.comm.showSettings();
519
	dojo.flash.obj.setVisible(true);
520
	dojo.flash.obj.center();
521
}, hideSettingsUI:function () {
522
	dojo.flash.obj.setVisible(false);
523
	if (dojo.storage.onHideSettingsUI != null && !dojo.lang.isUndefined(dojo.storage.onHideSettingsUI)) {
524
		dojo.storage.onHideSettingsUI.call(null);
525
	}
526
}, getType:function () {
527
	return "dojo.storage.browser.FlashStorageProvider";
528
}, _flashLoaded:function () {
529
	this._initialized = true;
530
	dojo.storage.manager.loaded();
531
}, _onStatus:function (statusResult, key) {
532
	var ds = dojo.storage;
533
	var dfo = dojo.flash.obj;
534
	if (statusResult == ds.PENDING) {
535
		dfo.center();
536
		dfo.setVisible(true);
537
	} else {
538
		dfo.setVisible(false);
539
	}
540
	if ((!dj_undef("_statusHandler", ds)) && (ds._statusHandler != null)) {
541
		ds._statusHandler.call(null, statusResult, key);
542
	}
543
}});
544
dojo.storage.manager.register("dojo.storage.browser.FileStorageProvider", new dojo.storage.browser.FileStorageProvider());
545
dojo.storage.manager.register("dojo.storage.browser.WhatWGStorageProvider", new dojo.storage.browser.WhatWGStorageProvider());
546
dojo.storage.manager.register("dojo.storage.browser.FlashStorageProvider", new dojo.storage.browser.FlashStorageProvider());
547
dojo.storage.manager.initialize();
548