summaryrefslogtreecommitdiff
path: root/includes/js/dojox/storage/AirFileStorageProvider.js
blob: 8187a6cc8d7b4b2002e62221d2788ac782c8a3d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
if(!dojo._hasResource["dojox.storage.AirFileStorageProvider"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.storage.AirFileStorageProvider"] = true;
dojo.provide("dojox.storage.AirFileStorageProvider");
dojo.require("dojox.storage.manager");
dojo.require("dojox.storage.Provider");

if (dojo.isAIR) {
	(function(){

		if (!air) {
			var air = {};
		}
		air.File = window.runtime.flash.filesystem.File;
		air.FileStream = window.runtime.flash.filesystem.FileStream;
		air.FileMode = window.runtime.flash.filesystem.FileMode;
		
		// summary: 
		//		Storage provider that uses features in the Adobe AIR runtime to achieve
		//		permanent storage
		dojo.declare("dojox.storage.AirFileStorageProvider", [ dojox.storage.Provider ], {
			initialized: false,
			
			_storagePath: "__DOJO_STORAGE/",
	
			initialize: function(){
				this.initialized = false;

				// need to initialize our storage directory
				try{
					var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath);
					if (!dir.exists){
						dir.createDirectory();
					}
					this.initialized = true;
				}catch(e){
					console.debug("dojox.storage.AirFileStorageProvider.initialize:", e);
				}
				
				// indicate that this storage provider is now loaded
				dojox.storage.manager.loaded();
			},
	
			isAvailable: function(){
				return true;
			},
			
			put: function(key, value, resultsHandler, namespace){
				if(this.isValidKey(key) == false){
					throw new Error("Invalid key given: " + key);
				}
				namespace = namespace||this.DEFAULT_NAMESPACE;
				if(this.isValidKey(namespace) == false){
					throw new Error("Invalid namespace given: " + namespace);
				}
				
				// try to store the value
				try{
					this.remove(key, namespace);
					
					var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace);
					if (!dir.exists){
						dir.createDirectory();
					}
					
					var file = dir.resolvePath(key);
					var stream = new air.FileStream();
					stream.open(file, air.FileMode.WRITE);
					stream.writeObject(value);
					stream.close();
				}catch(e){
					// indicate we failed
					console.debug("dojox.storage.AirFileStorageProvider.put:", e);
					resultsHandler(this.FAILED, key, e.toString());
					return;
				}
				
				if(resultsHandler){
					resultsHandler(this.SUCCESS, key, null);
				}
			},
			
			get: function(key, namespace){
				if(this.isValidKey(key) == false){
					throw new Error("Invalid key given: " + key);
				}
				namespace = namespace||this.DEFAULT_NAMESPACE;
				
				var results = null;
				
				var file = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace + '/' + key);
				if (file.exists && !file.isDirectory){
					var stream = new air.FileStream();
					stream.open(file, air.FileMode.READ);
					results = stream.readObject();
					stream.close();
				}
				
				return results;
			},
			
			getNamespaces: function(){
				var results = [ this.DEFAULT_NAMESPACE ];
				var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath);
				var files = dir.getDirectoryListing();
				for (i = 0; i < files.length; i++) {
					if(files[i].isDirectory && files[i].name != this.DEFAULT_NAMESPACE){
						results.push(files[i].name);
					}
				}
				return results;
			},

			getKeys: function(namespace){
				namespace = namespace||this.DEFAULT_NAMESPACE;
				if(this.isValidKey(namespace) == false){
					throw new Error("Invalid namespace given: " + namespace);
				}

				var results = [];
				var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace);
				if (dir.exists && dir.isDirectory){
					var files = dir.getDirectoryListing();
					for (i = 0; i < files.length; i++) {
						results.push(files[i].name);
					}
				}
				return results;
			},
			
			clear: function(namespace){
				if(this.isValidKey(namespace) == false){
					throw new Error("Invalid namespace given: " + namespace);
				}
				var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace);
				if (dir.exists && dir.isDirectory){
					dir.deleteDirectory(true);
				}
			},
			
			remove: function(key, namespace){
				namespace = namespace||this.DEFAULT_NAMESPACE;
				var file = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace + '/' + key);
				if (file.exists && !file.isDirectory){
					file.deleteFile();
				}
			},
			
			putMultiple: function(keys, values, resultsHandler, namespace) {
 				if(this.isValidKeyArray(keys) === false 
						|| ! values instanceof Array 
						|| keys.length != values.length){
					throw new Error("Invalid arguments: keys = [" + keys + "], values = [" + values + "]");
				}
				
				if(namespace == null || typeof namespace == "undefined"){
					namespace = this.DEFAULT_NAMESPACE;		
				}
	
				if(this.isValidKey(namespace) == false){
					throw new Error("Invalid namespace given: " + namespace);
				}
	
				this._statusHandler = resultsHandler;

				// try to store the value	
				try{
					for(var i=0;i<keys.length;i++) {
						this.put(keys[i], value[i], null, namespace);
					}
				}catch(e){
					// indicate we failed
					console.debug("dojox.storage.AirFileStorageProvider.putMultiple:", e);
					if(resultsHandler){
						resultsHandler(this.FAILED, keys, e.toString());
					}
					return;
				}
				
				if(resultsHandler){
					resultsHandler(this.SUCCESS, key, null);
				}
			},

			getMultiple: function(keys, namespace){
				if(this.isValidKeyArray(keys) === false){
					throw new Error("Invalid key array given: " + keys);
				}
				
				if(namespace == null || typeof namespace == "undefined"){
					namespace = this.DEFAULT_NAMESPACE;		
				}
				
				if(this.isValidKey(namespace) == false){
					throw new Error("Invalid namespace given: " + namespace);
				}
		
				var results = [];
				for(var i=0;i<keys.length;i++){
					results[i] = this.get(keys[i], namespace);
				}
				return results;
			},
			
			removeMultiple: function(keys, namespace){
				namespace = namespace||this.DEFAULT_NAMESPACE;
				
				for(var i=0;i<keys.length;i++){
					this.remove(keys[i], namespace);
				}
			}, 				
			
			isPermanent: function(){ return true; },

			getMaximumSize: function(){ return this.SIZE_NO_LIMIT; },

			hasSettingsUI: function(){ return false; },
			
			showSettingsUI: function(){
				throw new Error(this.declaredClass + " does not support a storage settings user-interface");
			},
			
			hideSettingsUI: function(){
				throw new Error(this.declaredClass + " does not support a storage settings user-interface");
			}
		});

		dojox.storage.manager.register("dojox.storage.AirFileStorageProvider", new dojox.storage.AirFileStorageProvider());
		dojox.storage.manager.initialize();
	})();
}

}