2 /*global tizen, localStorage */
11 (function () { // strict mode wrapper
13 SystemIO.prototype = {
15 * Creates new empty file in specified location
17 * @param {File} directoryHandle
18 * @param {string} fileName
20 createFile: function SystemIO_createFile(directoryHandle, fileName) {
23 return directoryHandle.createFile(fileName);
25 console.error('SystemIO_createFile error:' + e.message);
31 * Writes content to file stream
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
39 writeFile: function SystemIO_writeFile(fileHandle, fileContent, onSuccess, onError, contentEncoding) {
40 onError = onError || function () {};
42 fileHandle.openStream('w', function (fileStream) {
43 if (contentEncoding === 'base64') {
44 fileStream.writeBase64(fileContent);
46 fileStream.write(fileContent);
51 // launch onSuccess callback
52 if (typeof onSuccess === 'function') {
59 * Opens specified location
61 * @param {string} directory path
62 * @param {function} on success callback
63 * @param {function} on error callback
64 * @param {string} mode
66 openDir: function SystemIO_openDir(directoryPath, onSuccess, onError, openMode) {
67 openMode = openMode || 'rw';
68 onSuccess = onSuccess || function () {};
71 tizen.filesystem.resolve(directoryPath, onSuccess, onError, openMode);
77 * Parse specified filepath and returns data parts
79 * @param {string} filePath
82 getPathData: function SystemIO_getPathData(filePath) {
84 originalPath: filePath,
88 splittedPath = filePath.split('/');
90 path.fileName = splittedPath.pop();
91 path.dirName = splittedPath.join('/') || '/';
97 * Save specified content to file
99 * @param {string} file path
100 * @param {string} file content
101 * @param {string} file encoding
103 saveFileContent: function SystemIO_saveFileContent(filePath, fileContent, onSaveSuccess, fileEncoding) {
104 var pathData = this.getPathData(filePath),
108 function onOpenDirSuccess(dir) {
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);
118 this.openDir(pathData.dirName, onOpenDirSuccess);
122 * Deletes node with specified path
124 * @param {string} node path
125 * @param {function} success callback
127 deleteNode: function SystemIO_deleteNode(nodePath, onSuccess) {
128 var pathData = this.getPathData(nodePath),
131 function onDeleteSuccess() {
135 function onDeleteError(e) {
136 console.error('SystemIO_deleteNode:_onDeleteError', e);
139 function onOpenDirSuccess(dir) {
140 var onListFiles = function (files) {
141 if (files.length > 0) {
143 if (files[0].isDirectory) {
144 self.deleteDir(dir, files[0].fullPath, onDeleteSuccess, onDeleteError);
146 self.deleteFile(dir, files[0].fullPath, onDeleteSuccess, onDeleteError);
153 // check file exists;
154 dir.listFiles(onListFiles, function (e) {
157 name: pathData.fileName
161 this.openDir(pathData.dirName, onOpenDirSuccess, function (e) {
162 console.error('openDir error:' + e.message);
167 * Deletes specified file
170 * @param {string} file path
171 * @param {function} delete success callback
172 * @param {function} delete error callback
174 deleteFile: function SystemIO_deleteFile(dir, filePath, onDeleteSuccess, onDeleteError) {
176 dir.deleteFile(filePath, onDeleteSuccess, onDeleteError);
178 console.error('SystemIO_deleteFile error: ' + e.message);
184 * Deletes specified directory
187 * @param {string} dirPath dir path
188 * @param {function} onDeleteSuccess delete success callback
189 * @param {function} onDeleteError delete error callback
192 deleteDir: function SystemIO_deleteDir(dir, dirPath, onDeleteSuccess, onDeleteError) {
194 dir.deleteDirectory(dirPath, false, onDeleteSuccess, onDeleteError);
196 console.error('SystemIO_deleteDir error:' + e.message);
204 * @param {string} type storage type
205 * @param {function} onSuccess on success callback
206 * @param {string} excluded Excluded storage
208 getStorages: function SystemIO_getStorages(type, onSuccess, excluded) {
210 tizen.filesystem.listStorages(function (storages) {
212 len = storages.length,
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]);
227 if (typeof onSuccess === 'function') {
232 console.error('SystemIO_getStorages error:' + e.message);
236 getFilesList: function SystemIO_getFilesList(dir, onSuccess) {
244 for (i = 0; i < len; i += 1) {
245 tmp.push(files[i].name);
248 if (typeof onSuccess === 'function') {
253 console.error('SystemIO_getFilesList dir.listFiles() error:', e);
257 console.error('SystemIO_getFilesList error:', e.message);