Updated application sources
[apps/web/sample/FileManager.git] / project / js / app.systemIO.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global tizen, localStorage */
19
20 /**
21  * @class SystemIO
22  */
23 function SystemIO() {
24     'use strict';
25 }
26
27 (function () { // strict mode wrapper
28     'use strict';
29     SystemIO.prototype = {
30         /**
31          * Creates new empty file in specified location
32          *
33          * @param {File} directoryHandle
34          * @param {string} fileName
35          */
36         createFile: function SystemIO_createFile(directoryHandle, fileName) {
37
38             try {
39                 return directoryHandle.createFile(fileName);
40             } catch (e) {
41                 console.error('SystemIO_createFile error:' + e.message);
42                 return false;
43             }
44         },
45
46         /**
47          * Writes content to file stream
48          *
49          * @param {File} fileHandle file handler
50          * @param {string} fileContent file content
51          * @param {function} onSuccess on success callback
52          * @param {function} onError on error callback
53          * @param {string} content encoding
54          */
55         writeFile: function SystemIO_writeFile(
56             fileHandle,
57             fileContent,
58             onSuccess,
59             onError,
60             contentEncoding
61         ) {
62             onError = onError || function () {};
63
64             fileHandle.openStream('w', function (fileStream) {
65                 if (contentEncoding === 'base64') {
66                     fileStream.writeBase64(fileContent);
67                 } else {
68                     fileStream.write(fileContent);
69                 }
70
71                 fileStream.close();
72
73                 // launch onSuccess callback
74                 if (typeof onSuccess === 'function') {
75                     onSuccess();
76                 }
77             }, onError, 'UTF-8');
78         },
79
80         /**
81          * Opens specified location
82          *
83          * @param {string} directory path
84          * @param {function} on success callback
85          * @param {function} on error callback
86          * @param {string} mode
87          */
88         openDir: function SystemIO_openDir(
89             directoryPath,
90             onSuccess,
91             onError,
92             openMode
93         ) {
94             openMode = openMode || 'rw';
95             onSuccess = onSuccess || function () {};
96
97             try {
98                 tizen.filesystem.resolve(
99                     directoryPath,
100                     onSuccess,
101                     onError,
102                     openMode
103                 );
104             } catch (e) {
105             }
106         },
107
108         /**
109          * Parse specified filepath and returns data parts
110          *
111          * @param {string} filePath
112          * @returns {array}
113          */
114         getPathData: function SystemIO_getPathData(filePath) {
115             var path = {
116                 originalPath: filePath,
117                 fileName: '',
118                 dirName: ''
119             },
120                 splittedPath = filePath.split('/');
121
122             path.fileName = splittedPath.pop();
123             path.dirName = splittedPath.join('/') || '/';
124
125             return path;
126         },
127
128         /**
129          * Save specified content to file
130          *
131          * @param {string} file path
132          * @param {string} file content
133          * @param {string} file encoding
134          */
135         saveFileContent: function SystemIO_saveFileContent(
136             filePath,
137             fileContent,
138             onSaveSuccess,
139             fileEncoding
140         ) {
141             var pathData = this.getPathData(filePath),
142                 self = this,
143                 fileHandle;
144
145             function onOpenDirSuccess(dir) {
146                 // create new file
147                 fileHandle = self.createFile(dir, pathData.fileName);
148                 if (fileHandle !== false) {
149                     // save data into this file
150                     self.writeFile(
151                         fileHandle,
152                         fileContent,
153                         onSaveSuccess,
154                         false,
155                         fileEncoding
156                     );
157                 }
158             }
159
160             // open directory
161             this.openDir(pathData.dirName, onOpenDirSuccess);
162         },
163
164         /**
165          * Deletes node with specified path
166          *
167          * @param {string} node path
168          * @param {function} success callback
169          */
170         deleteNode: function SystemIO_deleteNode(nodePath, onSuccess) {
171             var pathData = this.getPathData(nodePath),
172                 self = this;
173
174             function onDeleteSuccess() {
175                 onSuccess();
176             }
177
178             function onDeleteError(e) {
179                 console.error('SystemIO_deleteNode:_onDeleteError', e);
180             }
181
182             function onOpenDirSuccess(dir) {
183                 var onListFiles = function (files) {
184                     if (files.length > 0) {
185                         // file exists;
186                         if (files[0].isDirectory) {
187                             self.deleteDir(
188                                 dir,
189                                 files[0].fullPath,
190                                 onDeleteSuccess,
191                                 onDeleteError
192                             );
193                         } else {
194                             self.deleteFile(
195                                 dir,
196                                 files[0].fullPath,
197                                 onDeleteSuccess,
198                                 onDeleteError
199                             );
200                         }
201                     } else {
202                         onDeleteSuccess();
203                     }
204                 };
205
206                 // check file exists;
207                 dir.listFiles(onListFiles, function (e) {
208                     console.error(e);
209                 }, {
210                     name: pathData.fileName
211                 });
212             }
213
214             this.openDir(pathData.dirName, onOpenDirSuccess, function (e) {
215                 console.error('openDir error:' + e.message);
216             });
217         },
218
219         /**
220          * Deletes specified file
221          *
222          * @param {File} dir
223          * @param {string} file path
224          * @param {function} delete success callback
225          * @param {function} delete error callback
226          */
227         deleteFile: function SystemIO_deleteFile(
228             dir,
229             filePath,
230             onDeleteSuccess,
231             onDeleteError
232         ) {
233             try {
234                 dir.deleteFile(filePath, onDeleteSuccess, onDeleteError);
235             } catch (e) {
236                 console.error('SystemIO_deleteFile error: ' + e.message);
237                 return false;
238             }
239         },
240
241         /**
242          * Deletes specified directory
243          *
244          * @param {File} dir
245          * @param {string} dirPath dir path
246          * @param {function} onDeleteSuccess delete success callback
247          * @param {function} onDeleteError delete error callback
248          * @returns {boolean}
249          */
250         deleteDir: function SystemIO_deleteDir(
251             dir,
252             dirPath,
253             onDeleteSuccess,
254             onDeleteError
255         ) {
256             try {
257                 dir.deleteDirectory(
258                     dirPath,
259                     false,
260                     onDeleteSuccess,
261                     onDeleteError
262                 );
263             } catch (e) {
264                 console.error('SystemIO_deleteDir error:' + e.message);
265                 return false;
266             }
267
268             return true;
269         },
270
271         /**
272          * @param {string} type storage type
273          * @param {function} onSuccess on success callback
274          * @param {string} excluded Excluded storage
275          */
276         getStorages: function SystemIO_getStorages(type, onSuccess, excluded) {
277             try {
278                 tizen.filesystem.listStorages(function (storages) {
279                     var tmp = [],
280                         len = storages.length,
281                         i;
282
283                     if (type !== undefined) {
284                         for (i = 0; i < len; i += 1) {
285                             if (storages[i].label !== excluded) {
286                                 if (
287                                     storages[i].type === 0 ||
288                                         storages[i].type === type
289                                 ) {
290                                     tmp.push(storages[i]);
291                                 }
292                             }
293                         }
294                     } else {
295                         tmp = storages;
296                     }
297
298                     if (typeof onSuccess === 'function') {
299                         onSuccess(tmp);
300                     }
301                 });
302             } catch (e) {
303                 console.error('SystemIO_getStorages error:' + e.message);
304             }
305         },
306
307         getFilesList: function SystemIO_getFilesList(dir, onSuccess) {
308             try {
309                 dir.listFiles(
310                     function (files) {
311                         var tmp = [],
312                             len = files.length,
313                             i;
314
315                         for (i = 0; i < len; i += 1) {
316                             tmp.push({
317                                 name: files[i].name,
318                                 isDirectory: files[i].isDirectory
319                             });
320                         }
321
322                         if (typeof onSuccess === 'function') {
323                             onSuccess(tmp);
324                         }
325                     },
326                     function (e) {
327                         console.error(
328                             'SystemIO_getFilesList dir.listFiles() error:',
329                             e
330                         );
331                     }
332                 );
333             } catch (e) {
334                 console.error('SystemIO_getFilesList error:', e.message);
335             }
336         }
337     };
338 }());