1 | dojo.provide("dojox.storage.AirFileStorageProvider"); |
---|
2 | dojo.require("dojox.storage.manager"); |
---|
3 | dojo.require("dojox.storage.Provider"); |
---|
4 | |
---|
5 | if (dojo.isAIR) { |
---|
6 | (function(){ |
---|
7 | |
---|
8 | if (!air) { |
---|
9 | var air = {}; |
---|
10 | } |
---|
11 | air.File = window.runtime.flash.filesystem.File; |
---|
12 | air.FileStream = window.runtime.flash.filesystem.FileStream; |
---|
13 | air.FileMode = window.runtime.flash.filesystem.FileMode; |
---|
14 | |
---|
15 | // summary: |
---|
16 | // Storage provider that uses features in the Adobe AIR runtime to achieve |
---|
17 | // permanent storage |
---|
18 | dojo.declare("dojox.storage.AirFileStorageProvider", [ dojox.storage.Provider ], { |
---|
19 | initialized: false, |
---|
20 | |
---|
21 | _storagePath: "__DOJO_STORAGE/", |
---|
22 | |
---|
23 | initialize: function(){ |
---|
24 | this.initialized = false; |
---|
25 | |
---|
26 | // need to initialize our storage directory |
---|
27 | try{ |
---|
28 | var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath); |
---|
29 | if (!dir.exists){ |
---|
30 | dir.createDirectory(); |
---|
31 | } |
---|
32 | this.initialized = true; |
---|
33 | }catch(e){ |
---|
34 | console.debug("dojox.storage.AirFileStorageProvider.initialize:", e); |
---|
35 | } |
---|
36 | |
---|
37 | // indicate that this storage provider is now loaded |
---|
38 | dojox.storage.manager.loaded(); |
---|
39 | }, |
---|
40 | |
---|
41 | isAvailable: function(){ |
---|
42 | return true; |
---|
43 | }, |
---|
44 | |
---|
45 | put: function(key, value, resultsHandler, namespace){ |
---|
46 | if(this.isValidKey(key) == false){ |
---|
47 | throw new Error("Invalid key given: " + key); |
---|
48 | } |
---|
49 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
50 | if(this.isValidKey(namespace) == false){ |
---|
51 | throw new Error("Invalid namespace given: " + namespace); |
---|
52 | } |
---|
53 | |
---|
54 | // try to store the value |
---|
55 | try{ |
---|
56 | this.remove(key, namespace); |
---|
57 | |
---|
58 | var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace); |
---|
59 | if (!dir.exists){ |
---|
60 | dir.createDirectory(); |
---|
61 | } |
---|
62 | |
---|
63 | var file = dir.resolvePath(key); |
---|
64 | var stream = new air.FileStream(); |
---|
65 | stream.open(file, air.FileMode.WRITE); |
---|
66 | stream.writeObject(value); |
---|
67 | stream.close(); |
---|
68 | }catch(e){ |
---|
69 | // indicate we failed |
---|
70 | console.debug("dojox.storage.AirFileStorageProvider.put:", e); |
---|
71 | resultsHandler(this.FAILED, key, e.toString(), namespace); |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | if(resultsHandler){ |
---|
76 | resultsHandler(this.SUCCESS, key, null, namespace); |
---|
77 | } |
---|
78 | }, |
---|
79 | |
---|
80 | get: function(key, namespace){ |
---|
81 | if(this.isValidKey(key) == false){ |
---|
82 | throw new Error("Invalid key given: " + key); |
---|
83 | } |
---|
84 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
85 | |
---|
86 | var results = null; |
---|
87 | |
---|
88 | var file = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace + '/' + key); |
---|
89 | if (file.exists && !file.isDirectory){ |
---|
90 | var stream = new air.FileStream(); |
---|
91 | stream.open(file, air.FileMode.READ); |
---|
92 | results = stream.readObject(); |
---|
93 | stream.close(); |
---|
94 | } |
---|
95 | |
---|
96 | return results; |
---|
97 | }, |
---|
98 | |
---|
99 | getNamespaces: function(){ |
---|
100 | var results = [ this.DEFAULT_NAMESPACE ]; |
---|
101 | var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath); |
---|
102 | var files = dir.getDirectoryListing(), i; |
---|
103 | for (i = 0; i < files.length; i++) { |
---|
104 | if(files[i].isDirectory && files[i].name != this.DEFAULT_NAMESPACE){ |
---|
105 | results.push(files[i].name); |
---|
106 | } |
---|
107 | } |
---|
108 | return results; |
---|
109 | }, |
---|
110 | |
---|
111 | getKeys: function(namespace){ |
---|
112 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
113 | if(this.isValidKey(namespace) == false){ |
---|
114 | throw new Error("Invalid namespace given: " + namespace); |
---|
115 | } |
---|
116 | |
---|
117 | var results = []; |
---|
118 | var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace); |
---|
119 | if (dir.exists && dir.isDirectory){ |
---|
120 | var files = dir.getDirectoryListing(), i; |
---|
121 | for (i = 0; i < files.length; i++) { |
---|
122 | results.push(files[i].name); |
---|
123 | } |
---|
124 | } |
---|
125 | return results; |
---|
126 | }, |
---|
127 | |
---|
128 | clear: function(namespace){ |
---|
129 | if(this.isValidKey(namespace) == false){ |
---|
130 | throw new Error("Invalid namespace given: " + namespace); |
---|
131 | } |
---|
132 | var dir = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace); |
---|
133 | if (dir.exists && dir.isDirectory){ |
---|
134 | dir.deleteDirectory(true); |
---|
135 | } |
---|
136 | }, |
---|
137 | |
---|
138 | remove: function(key, namespace){ |
---|
139 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
140 | var file = air.File.applicationStorageDirectory.resolvePath(this._storagePath + namespace + '/' + key); |
---|
141 | if (file.exists && !file.isDirectory){ |
---|
142 | file.deleteFile(); |
---|
143 | } |
---|
144 | }, |
---|
145 | |
---|
146 | putMultiple: function(keys, values, resultsHandler, namespace) { |
---|
147 | if(this.isValidKeyArray(keys) === false |
---|
148 | || ! values instanceof Array |
---|
149 | || keys.length != values.length){ |
---|
150 | throw new Error("Invalid arguments: keys = [" + keys + "], values = [" + values + "]"); |
---|
151 | } |
---|
152 | |
---|
153 | if(namespace == null || typeof namespace == "undefined"){ |
---|
154 | namespace = this.DEFAULT_NAMESPACE; |
---|
155 | } |
---|
156 | |
---|
157 | if(this.isValidKey(namespace) == false){ |
---|
158 | throw new Error("Invalid namespace given: " + namespace); |
---|
159 | } |
---|
160 | |
---|
161 | this._statusHandler = resultsHandler; |
---|
162 | |
---|
163 | // try to store the value |
---|
164 | try{ |
---|
165 | for(var i=0;i<keys.length;i++) { |
---|
166 | this.put(keys[i], values[i], null, namespace); |
---|
167 | } |
---|
168 | }catch(e){ |
---|
169 | // indicate we failed |
---|
170 | console.debug("dojox.storage.AirFileStorageProvider.putMultiple:", e); |
---|
171 | if(resultsHandler){ |
---|
172 | resultsHandler(this.FAILED, keys, e.toString(), namespace); |
---|
173 | } |
---|
174 | return; |
---|
175 | } |
---|
176 | |
---|
177 | if(resultsHandler){ |
---|
178 | resultsHandler(this.SUCCESS, keys, null, namespace); |
---|
179 | } |
---|
180 | }, |
---|
181 | |
---|
182 | getMultiple: function(keys, namespace){ |
---|
183 | if(this.isValidKeyArray(keys) === false){ |
---|
184 | throw new Error("Invalid key array given: " + keys); |
---|
185 | } |
---|
186 | |
---|
187 | if(namespace == null || typeof namespace == "undefined"){ |
---|
188 | namespace = this.DEFAULT_NAMESPACE; |
---|
189 | } |
---|
190 | |
---|
191 | if(this.isValidKey(namespace) == false){ |
---|
192 | throw new Error("Invalid namespace given: " + namespace); |
---|
193 | } |
---|
194 | |
---|
195 | var results = []; |
---|
196 | for(var i=0;i<keys.length;i++){ |
---|
197 | results[i] = this.get(keys[i], namespace); |
---|
198 | } |
---|
199 | return results; |
---|
200 | }, |
---|
201 | |
---|
202 | removeMultiple: function(keys, namespace){ |
---|
203 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
204 | |
---|
205 | for(var i=0;i<keys.length;i++){ |
---|
206 | this.remove(keys[i], namespace); |
---|
207 | } |
---|
208 | }, |
---|
209 | |
---|
210 | isPermanent: function(){ return true; }, |
---|
211 | |
---|
212 | getMaximumSize: function(){ return this.SIZE_NO_LIMIT; }, |
---|
213 | |
---|
214 | hasSettingsUI: function(){ return false; }, |
---|
215 | |
---|
216 | showSettingsUI: function(){ |
---|
217 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
218 | }, |
---|
219 | |
---|
220 | hideSettingsUI: function(){ |
---|
221 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
222 | } |
---|
223 | }); |
---|
224 | |
---|
225 | dojox.storage.manager.register("dojox.storage.AirFileStorageProvider", new dojox.storage.AirFileStorageProvider()); |
---|
226 | dojox.storage.manager.initialize(); |
---|
227 | })(); |
---|
228 | } |
---|