Subversion Repositories Applications.papyrus

Rev

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