diff options
author | Valerio Virgillito | 2012-02-14 11:07:21 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-14 11:07:21 -0800 |
commit | f060190a3bffd9a16718f2ce0499699103d46372 (patch) | |
tree | a308fe7eacec2156c8212dff0892a3de925d5f9a /js/io/system/chromeapi.js | |
parent | f766cc203f30ea43ae8b83cf4b65d45cc4435ee9 (diff) | |
parent | 33bc9d62b8e6694500bf14d5b18187bd99a520a3 (diff) | |
download | ninja-f060190a3bffd9a16718f2ce0499699103d46372.tar.gz |
Merge branch 'FileIO' of https://github.com/joseeight/ninja-internal into integration
Diffstat (limited to 'js/io/system/chromeapi.js')
-rw-r--r-- | js/io/system/chromeapi.js | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js new file mode 100644 index 00000000..e53d4841 --- /dev/null +++ b/js/io/system/chromeapi.js | |||
@@ -0,0 +1,269 @@ | |||
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 | |||
11 | The init function starts up the file system API, and a size must be | ||
12 | set, no unlimited available as of now. | ||
13 | |||
14 | //////////////////////////////////////////////////////////////////////// | ||
15 | ///////////////////////////////////////////////////////////////////// */ | ||
16 | // | ||
17 | var Montage = require("montage/core/core").Montage; | ||
18 | //////////////////////////////////////////////////////////////////////// | ||
19 | // | ||
20 | exports.ChromeApi = Montage.create(Object.prototype, { | ||
21 | //////////////////////////////////////////////////////////////////// | ||
22 | //Needs size in MBs for fileSystem init | ||
23 | init: { | ||
24 | enumerable: true, | ||
25 | value: function(size) { | ||
26 | // | ||
27 | if (window.webkitRequestFileSystem) { | ||
28 | //Current way to init Chrome's fileSystem API | ||
29 | window.webkitRequestFileSystem(window.PERSISTENT, size*1024*1024, function (fs) { | ||
30 | //Storing reference to instance | ||
31 | this.fileSystem = fs; | ||
32 | //Dispatching action ready event | ||
33 | var readyEvent = document.createEvent("CustomEvent"); | ||
34 | readyEvent.initEvent('ready', true, true); | ||
35 | this.dispatchEvent(readyEvent); | ||
36 | //Building data of local Ninja Library | ||
37 | this._listNinjaChromeLibrary(); | ||
38 | }.bind(this), function (e) {return false}); //Returns false on error (not able to init) | ||
39 | // | ||
40 | return true; | ||
41 | } else { | ||
42 | //No fileSystem API | ||
43 | return false; | ||
44 | } | ||
45 | } | ||
46 | }, | ||
47 | //////////////////////////////////////////////////////////////////// | ||
48 | // | ||
49 | _fileSystem: { | ||
50 | enumerable: false, | ||
51 | value: null | ||
52 | }, | ||
53 | //////////////////////////////////////////////////////////////////// | ||
54 | // | ||
55 | fileSystem: { | ||
56 | enumerable: false, | ||
57 | get: function() { | ||
58 | return this._fileSystem; | ||
59 | }, | ||
60 | set: function(value) { | ||
61 | this._fileSystem = value; | ||
62 | } | ||
63 | }, | ||
64 | //////////////////////////////////////////////////////////////////// | ||
65 | // | ||
66 | fileNew: { | ||
67 | enumerable: true, | ||
68 | value: function(filePath, content, callback) { | ||
69 | // | ||
70 | this.fileSystem.root.getFile(filePath, {create: true}, function(f) { | ||
71 | // | ||
72 | f.createWriter(function(writer) { | ||
73 | // | ||
74 | var mime, blob = new window.WebKitBlobBuilder, type = filePath.split('.'); | ||
75 | type = type[type.length-1]; | ||
76 | switch (type) { | ||
77 | case 'bmp': | ||
78 | mime = 'image/bmp'; | ||
79 | break; | ||
80 | case 'gif': | ||
81 | mime = 'image/gif'; | ||
82 | break; | ||
83 | case 'jpeg': | ||
84 | mime = 'image/jpeg'; | ||
85 | break; | ||
86 | case 'jpg': | ||
87 | mime = 'image/jpeg'; | ||
88 | break; | ||
89 | case 'png': | ||
90 | mime = 'image/png'; | ||
91 | break; | ||
92 | case 'rtf': | ||
93 | mime = 'application/rtf'; | ||
94 | break; | ||
95 | case 'tif': | ||
96 | mime = 'image/tiff'; | ||
97 | break; | ||
98 | case 'tiff': | ||
99 | mime = 'image/tiff'; | ||
100 | break; | ||
101 | case 'pdf': | ||
102 | mime = 'application/pdf'; | ||
103 | break; | ||
104 | case 'zip': | ||
105 | mime = 'application/zip'; | ||
106 | break; | ||
107 | case 'svg': | ||
108 | mime = 'image/svg+xml'; | ||
109 | break; | ||
110 | default: | ||
111 | mime = 'text/'+type; | ||
112 | break; | ||
113 | } | ||
114 | // | ||
115 | blob.append(content); | ||
116 | writer.write(blob.getBlob(mime)); | ||
117 | // | ||
118 | if (callback) callback(true); | ||
119 | }, function (e) {if (callback) callback(false)}); | ||
120 | }, function (e) {if (callback) callback(false)}); | ||
121 | } | ||
122 | }, | ||
123 | //////////////////////////////////////////////////////////////////// | ||
124 | // | ||
125 | fileDelete: { | ||
126 | enumerable: true, | ||
127 | value: function(filePath, callback) { | ||
128 | this.fileSystem.root.getFile(filePath, {create: false}, function(file) { | ||
129 | file.remove(function() { | ||
130 | if (callback) callback(true); | ||
131 | }); | ||
132 | }, function (e) {if (callback) callback(false)}); | ||
133 | } | ||
134 | }, | ||
135 | //////////////////////////////////////////////////////////////////// | ||
136 | // | ||
137 | fileContent: { | ||
138 | enumerable: true, | ||
139 | value: function(filePath, callback) { | ||
140 | // | ||
141 | this.fileSystem.root.getFile(filePath, {}, function(f) { | ||
142 | f.file(function(file) { | ||
143 | var reader = new FileReader(); | ||
144 | reader.onloadend = function(e) { | ||
145 | if (callback) { | ||
146 | callback({content: this.result, data: file, file: f, url: f.toURL()}); | ||
147 | } | ||
148 | }; | ||
149 | reader.readAsArrayBuffer(file); | ||
150 | }, function (e) {if (callback) callback(false)}); | ||
151 | }, function (e) {if (callback) callback(false)}); | ||
152 | } | ||
153 | }, | ||
154 | //////////////////////////////////////////////////////////////////// | ||
155 | // | ||
156 | fileCopy: { | ||
157 | enumerable: true, | ||
158 | value: function() { | ||
159 | } | ||
160 | }, | ||
161 | //////////////////////////////////////////////////////////////////// | ||
162 | // | ||
163 | fileRename: { | ||
164 | enumerable: true, | ||
165 | value: function() { | ||
166 | } | ||
167 | }, | ||
168 | //////////////////////////////////////////////////////////////////// | ||
169 | // | ||
170 | fileMove: { | ||
171 | enumerable: true, | ||
172 | value: function() { | ||
173 | } | ||
174 | }, | ||
175 | //////////////////////////////////////////////////////////////////// | ||
176 | //Creating directory from path, callback optional | ||
177 | directoryNew: { | ||
178 | enumerable: true, | ||
179 | value: function(directoryPath, callback) { | ||
180 | //Checking for directory not to already exist | ||
181 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
182 | if (callback) callback(false); | ||
183 | return false; //Directory already exists | ||
184 | }); | ||
185 | //Creating new directory | ||
186 | this.fileSystem.root.getDirectory(directoryPath, {create: true}, function(dir) { | ||
187 | if (callback) callback(true); | ||
188 | }, function (e) {if (callback) callback(false)}); | ||
189 | } | ||
190 | }, | ||
191 | //////////////////////////////////////////////////////////////////// | ||
192 | // | ||
193 | directoryDelete: { | ||
194 | enumerable: true, | ||
195 | value: function(directoryPath, callback) { | ||
196 | // | ||
197 | this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { | ||
198 | // | ||
199 | dir.removeRecursively(function() { | ||
200 | if (callback) callback(true); | ||
201 | }); | ||
202 | }, function (e) {if (callback) callback(false)}); | ||
203 | } | ||
204 | }, | ||
205 | //////////////////////////////////////////////////////////////////// | ||
206 | //Returns the directory contents to a callback function | ||
207 | directoryContents: { | ||
208 | enumerable: true, | ||
209 | value: function(directory, callback) { | ||
210 | //Creating instance of directory reader | ||
211 | this.fileSystem.directoryReader = directory.createReader(); | ||
212 | //Getting directory contents and sending results to callback | ||
213 | this.fileSystem.directoryReader.readEntries(function(results) { | ||
214 | //Calling callback with results (null if invalid directory) | ||
215 | callback(results); | ||
216 | }, function (e) {callback(null)}); | ||
217 | } | ||
218 | }, | ||
219 | //////////////////////////////////////////////////////////////////// | ||
220 | // | ||
221 | directoryCopy: { | ||
222 | enumerable: true, | ||
223 | value: function() { | ||
224 | } | ||
225 | }, | ||
226 | //////////////////////////////////////////////////////////////////// | ||
227 | // | ||
228 |