diff options
Diffstat (limited to 'js/io/system/chromeapi.js')
-rw-r--r-- | js/io/system/chromeapi.js | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js new file mode 100644 index 00000000..2fc8769c --- /dev/null +++ b/js/io/system/chromeapi.js | |||
@@ -0,0 +1,219 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | ||
9 | NOTES: | ||
10 | The init function starts up the file system API, and a size must be | ||
11 | set, no unlimited available as of now. | ||
12 | |||
13 | Core API reference in NINJA: this.application.ninja.coreIoApi | ||
14 | |||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | ///////////////////////////////////////////////////////////////////// */ | ||
17 | // | ||
18 | var Montage = require("montage/core/core").Montage; | ||
19 | //////////////////////////////////////////////////////////////////////// | ||
20 | // | ||
21 | exports.ChromeApi = Montage.create(Object.prototype, { | ||
22 | //////////////////////////////////////////////////////////////////// | ||
23 | //Needs size in MBs for fileSystem init | ||
24 | init: { | ||
25 | enumerable: true, | ||
26 | value: function(size) { | ||
27 | // | ||
28 | if (window.webkitRequestFileSystem) { | ||
29 | //Current way to init Chrome's fileSystem API | ||
30 | window.webkitRequestFileSystem(window.PERSISTENT, size*1024*1024, function (fs) { | ||
31 | //Storing reference to instance | ||
32 | this.fileSystem = fs; | ||
33 | //Dispatching action ready event | ||
34 | var readyEvent = document.createEvent("CustomEvent"); | ||
35 | readyEvent.initEvent('ready', true, true); | ||
36 | this.dispatchEvent(readyEvent); | ||
37 | //Building data of local Ninja Library | ||
38 | this._listNinjaChromeLibrary(); | ||
39 | }.bind(this), function (e) {return false}); //Returns false on error (not able to init) | ||
40 | // | ||
41 | return true; | ||
42 | } else { | ||
43 | //No fileSystem API | ||
44 | return false; | ||
45 | } | ||
46 | } | ||
47 | }, | ||
48 | //////////////////////////////////////////////////////////////////// | ||
49 | // | ||
50 | _fileSystem: { | ||
51 | enumerable: false, | ||
52 | value: null | ||
53 | }, | ||
54 | //////////////////////////////////////////////////////////////////// | ||
55 | // | ||
56 | fileSystem: { | ||
57 | enumerable: false, | ||
58 | get: function() { | ||
59 | return this._fileSystem; | ||
60 | }, | ||
61 | set: function(value) { | ||
62 | this._fileSystem = value; | ||
63 | } | ||
64 | }, | ||
65 | //////////////////////////////////////////////////////////////////// | ||
66 | // | ||
67 | fileNew: { | ||
68 | enumerable: true, | ||
69 | value: function(filePath, content, callback) { | ||
70 | // | ||
71 | this.fileSystem.root.getFile(filePath, {create: true}, function(f) { | ||
72 | // | ||
73 | f.createWriter(function(writer) { | ||
74 | // | ||
75 | var b, mime, type = filePath.split('.'); | ||
76 | type = type[type.length-1]; | ||
77 | switch (type) { | ||
78 | case 'bmp': | ||
79 | mime = 'image/bmp'; | ||
80 | break; | ||
81 | case 'gif': | ||
82 | mime = 'image/gif'; | ||
83 | break; | ||
84 | case 'jpeg': | ||
85 | mime = 'image/jpeg'; | ||
86 | break; | ||
87 | case 'jpg': | ||
88 | mime = 'image/jpeg'; | ||
89 | break; | ||
90 | case 'png': | ||
91 | mime = 'image/png'; | ||
92 | break; | ||
93 | case 'rtf': | ||
94 | mime = 'application/rtf'; | ||
95 | break; | ||
96 | case 'tif': | ||
97 | mime = 'image/tiff'; | ||
98 | break; | ||
99 | case 'tiff': | ||
100 | mime = 'image/tiff'; | ||
101 | break; | ||
102 | case 'pdf': | ||
103 | mime = 'application/pdf'; | ||
104 | break; | ||
105 | case 'zip': | ||
106 | mime = 'application/zip'; | ||
107 | break; | ||
108 | case 'svg': | ||
109 | mime = 'image/svg+xml'; | ||
110 | break; | ||
111 | default: | ||
112 | mime = 'text/'+type; | ||
113 | break; | ||
114 | } | ||
115 | // | ||
116 | b = new window.WebKitBlobBuilder; | ||
117 | b.append(content); | ||
118 | writer.write(b.getBlob(mime)); | ||
119 | // | ||
120 | if (callback) callback(true); | ||
121 | }, function (e) {if (callback) callback(false)}); | ||
122 | }, function (e) {if (callback) callback(false)}); | ||
123 | } | ||
124 | }, | ||
125 | //////////////////////////////////////////////////////////////////// | ||
126 | //Creating directory from path, callback optional | ||
127 | directoryNew: { | ||
128 | enumerable: true, | ||
129 | value: function(directoryPath, callback) { | ||
130 | //Checking for directory not to already exist | ||
131 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
132 | if (callback) callback(false); | ||
133 | return; //Directory already exists | ||
134 | }); | ||
135 | //Creating new directory | ||
136 | this.fileSystem.root.getDirectory(directoryPath, {create: true}, function(dir) { | ||
137 | if (callback) callback(true); | ||
138 | }, function (e) {if (callback) callback(false)}); | ||
139 | } | ||
140 | }, | ||
141 | //////////////////////////////////////////////////////////////////// | ||
142 | // | ||
143 | directoryDelete: { | ||
144 | enumerable: true, | ||
145 | value: function(directoryPath, callback) { | ||
146 | // | ||
147 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
148 | // | ||
149 | dir.removeRecursively(function() { | ||
150 | if (callback) callback(true); | ||
151 | }); | ||
152 | }, function (e) {if (callback) callback(false)}); | ||
153 | } | ||
154 | }, | ||
155 | //////////////////////////////////////////////////////////////////// | ||
156 | //Returns the directory contents to a callback function | ||
157 | directoryContents: { | ||
158 | enumerable: true, | ||
159 | value: function(directory, callback) { | ||
160 | //Creating instance of directory reader | ||
161 | this.fileSystem.directoryReader = directory.createReader(); | ||
162 | //Getting directory contents and sending results to callback | ||
163 | this.fileSystem.directoryReader.readEntries(function(results) { | ||
164 | //Calling callback with results (null if invalid directory) | ||
165 | callback(results); | ||
166 | }, function (e) {callback(null)}); | ||
167 | } | ||
168 | }, | ||
169 | //////////////////////////////////////////////////////////////////// | ||
170 | // | ||
171 | directoryCopy: { | ||
172 | enumerable: true, | ||
173 | value: function() { | ||
174 | } | ||
175 | }, | ||
176 | //////////////////////////////////////////////////////////////////// | ||
177 | // | ||
178 | directoryRename: { | ||
179 | enumerable: true, | ||
180 | value: function() { | ||
181 | } | ||
182 | }, | ||
183 | //////////////////////////////////////////////////////////////////// | ||
184 | // | ||
185 | directoryMove: { | ||
186 | enumerable: true, | ||
187 | value: function() { | ||
188 | } | ||
189 | }, | ||
190 | //////////////////////////////////////////////////////////////////// | ||
191 | // | ||
192 | _listNinjaChromeLibrary: { | ||
193 | enumerable: false, | ||
194 | value: function () { | ||
195 | function parseLibrary (contents) { | ||
196 | // | ||
197 | var lib = []; | ||
198 | // | ||
199 | for(var i=0; contents[i]; i++) { | ||
200 | // | ||
201 | if (contents[i].isDirectory) { | ||
202 | lib.push(contents[i].name); | ||
203 | } | ||
204 | } | ||
205 | //Dispatching action ready event | ||
206 | var libraryEvent = document.createEvent("CustomEvent"); | ||
207 | libraryEvent.initEvent('library', true, true); | ||
208 | libraryEvent.ninjaChromeLibrary = lib; | ||
209 | this.dispatchEvent(libraryEvent); | ||
210 | }; | ||
211 | // | ||
212 | this.directoryContents(this.fileSystem.root, parseLibrary.bind(this)); | ||
213 | } | ||
214 | } | ||
215 | //////////////////////////////////////////////////////////////////// | ||
216 | //////////////////////////////////////////////////////////////////// | ||
217 | }); | ||
218 | //////////////////////////////////////////////////////////////////////// | ||
219 | //////////////////////////////////////////////////////////////////////// \ No newline at end of file | ||