4c7037733d0381dfa7a0b35dba9ad179b7fb05be
[apps/web/sample/FileManager.git] / project / js / app.systemIO.js
1 /*jslint devel: true*/
2 /*global tizen, localStorage */
3
4 /**
5  * @class SystemIO
6  */
7 function SystemIO() {
8         'use strict';
9 }
10
11 (function () { // strict mode wrapper
12         'use strict';
13         SystemIO.prototype = {
14                 /**
15                  * Creates new empty file in specified location
16                  *
17                  * @param {File} directoryHandle
18                  * @param {string} fileName
19                  */
20                 createFile: function SystemIO_createFile(directoryHandle, fileName) {
21
22                         try {
23                                 return directoryHandle.createFile(fileName);
24                         } catch (e) {
25                                 console.error('SystemIO_createFile error:' + e.message);
26                                 return false;
27                         }
28                 },
29
30                 /**
31                  * Writes content to file stream
32                  *
33                  * @param {File} fileHandle file handler
34                  * @param {string} fileContent file content
35                  * @param {function} onSuccess on success callback
36                  * @param {function} onError on error callback
37                  * @param {string} content encoding
38                  */
39                 writeFile: function SystemIO_writeFile(fileHandle, fileContent, onSuccess, onError, contentEncoding) {
40                         onError = onError || function () {};
41
42                         fileHandle.openStream('w', function (fileStream) {
43                                 if (contentEncoding === 'base64') {
44                                         fileStream.writeBase64(fileContent);
45                                 } else {
46                                         fileStream.write(fileContent);
47                                 }
48
49                                 fileStream.close();
50
51                                 // launch onSuccess callback
52                                 if (typeof onSuccess === 'function') {
53                                         onSuccess();
54                                 }
55                         }, onError, 'UTF-8');
56                 },
57
58                 /**
59                  * Opens specified location
60                  *
61                  * @param {string} directory path
62                  * @param {function} on success callback
63                  * @param {function} on error callback
64                  * @param {string} mode
65                  */
66                 openDir: function SystemIO_openDir(directoryPath, onSuccess, onError, openMode) {
67                         openMode = openMode || 'rw';
68                         onSuccess = onSuccess || function () {};
69
70                         try {
71                                 tizen.filesystem.resolve(directoryPath, onSuccess, onError, openMode);
72                         } catch (e) {
73                         }
74                 },
75
76                 /**
77                  * Parse specified filepath and returns data parts
78                  *
79                  * @param {string} filePath
80                  * @returns {array}
81                  */
82                 getPathData: function SystemIO_getPathData(filePath) {
83                         var path = {
84                                 originalPath: filePath,
85                                 fileName: '',
86                                 dirName: ''
87                         },
88                                 splittedPath = filePath.split('/');
89
90                         path.fileName = splittedPath.pop();
91                         path.dirName = splittedPath.join('/') || '/';
92
93                         return path;
94                 },
95
96                 /**
97                  * Save specified content to file
98                  *
99                  * @param {string} file path
100                  * @param {string} file content
101                  * @param {string} file encoding
102                  */
103                 saveFileContent: function SystemIO_saveFileContent(filePath, fileContent, onSaveSuccess, fileEncoding) {
104                         var pathData = this.getPathData(filePath),
105                                 self = this,
106                                 fileHandle;
107
108                         function onOpenDirSuccess(dir) {
109                                 // create new file
110                                 fileHandle = self.createFile(dir, pathData.fileName);
111                                 if (fileHandle !== false) {
112                                         // save data into this file
113                                         self.writeFile(fileHandle, fileContent, onSaveSuccess, false, fileEncoding);
114                                 }
115                         }
116
117                         // open directory
118                         this.openDir(pathData.dirName, onOpenDirSuccess);
119                 },
120
121                 /**
122                  * Deletes node with specified path
123                  *
124                  * @param {string} node path
125                  * @param {function} success callback
126                  */
127                 deleteNode: function SystemIO_deleteNode(nodePath, onSuccess) {
128                         var pathData = this.getPathData(nodePath),
129                                 self = this;
130
131                         function onDeleteSuccess() {
132                                 onSuccess();
133                         }
134
135                         function onDeleteError(e) {
136                                 console.error('SystemIO_deleteNode:_onDeleteError', e);
137                         }
138
139                         function onOpenDirSuccess(dir) {
140                                 var onListFiles = function (files) {
141                                         if (files.length > 0) {
142                                                 // file exists;
143                                                 if (files[0].isDirectory) {
144                                                         self.deleteDir(dir, files[0].fullPath, onDeleteSuccess, onDeleteError);
145                                                 } else {
146                                                         self.deleteFile(dir, files[0].fullPath, onDeleteSuccess, onDeleteError);
147                                                 }
148                                         } else {
149                                                 onDeleteSuccess();
150                                         }
151                                 };
152
153                                 // check file exists;
154                                 dir.listFiles(onListFiles, function (e) {
155                                         console.error(e);
156                                 }, {
157                                         name: pathData.fileName
158                                 });
159                         }
160
161                         this.openDir(pathData.dirName, onOpenDirSuccess, function (e) {
162                                 console.error('openDir error:' + e.message);
163                         });
164                 },
165
166                 /**
167                  * Deletes specified file
168                  *
169                  * @param {File} dir
170                  * @param {string} file path
171                  * @param {function} delete success callback
172                  * @param {function} delete error callback
173                  */
174                 deleteFile: function SystemIO_deleteFile(dir, filePath, onDeleteSuccess, onDeleteError) {
175                         try {
176                                 dir.deleteFile(filePath, onDeleteSuccess, onDeleteError);
177                         } catch (e) {
178                                 console.error('SystemIO_deleteFile error: ' + e.message);
179                                 return false;
180                         }
181                 },
182
183                 /**
184                  * Deletes specified directory
185                  *
186                  * @param {File} dir
187                  * @param {string} dirPath dir path
188                  * @param {function} onDeleteSuccess delete success callback
189                  * @param {function} onDeleteError delete error callback
190                  * @returns {boolean}
191                  */
192                 deleteDir: function SystemIO_deleteDir(dir, dirPath, onDeleteSuccess, onDeleteError) {
193                         try {
194                                 dir.deleteDirectory(dirPath, false, onDeleteSuccess, onDeleteError);
195                         } catch (e) {
196                                 console.error('SystemIO_deleteDir error:' + e.message);
197                                 return false;
198                         }
199
200                         return true;
201                 },
202
203                 /**
204                  * @param {string} type storage type
205                  * @param {function} onSuccess on success callback
206                  * @param {string} excluded Excluded storage
207                  */
208                 getStorages: function SystemIO_getStorages(type, onSuccess, excluded) {
209                         try {
210                                 tizen.filesystem.listStorages(function (storages) {
211                                         var tmp = [],
212                                                 len = storages.length,
213                                                 i;
214
215                                         if (type !== undefined) {
216                                                 for (i = 0; i < len; i += 1) {
217                                                         if (storages[i].label !== excluded) {
218                                                                 if (storages[i].type === 0 || storages[i].type === type) {
219                                                                         tmp.push(storages[i]);
220                                                                 }
221                                                         }
222                                                 }
223                                         } else {
224                                                 tmp = storages;
225                                         }
226
227                                         if (typeof onSuccess === 'function') {
228                                                 onSuccess(tmp);
229                                         }
230                                 });
231                         } catch (e) {
232                                 console.error('SystemIO_getStorages error:' + e.message);
233                         }
234                 },
235
236                 getFilesList: function SystemIO_getFilesList(dir, onSuccess) {
237                         try {
238                                 dir.listFiles(
239                                         function (files) {
240                                                 var tmp = [],
241                                                         len = files.length,
242                                                         i;
243
244                                                 for (i = 0; i < len; i += 1) {
245                                                         tmp.push(files[i].name);
246                                                 }
247
248                                                 if (typeof onSuccess === 'function') {
249                                                         onSuccess(tmp);
250                                                 }
251                                         },
252                                         function (e) {
253                                                 console.error('SystemIO_getFilesList dir.listFiles() error:', e);
254                                         }
255                                 );
256                         } catch (e) {
257                                 console.error('SystemIO_getFilesList error:', e.message);
258                         }
259                 }
260         };
261 }());