2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
17 /*jslint devel: true*/
18 /*global tizen, localStorage */
27 (function () { // strict mode wrapper
29 SystemIO.prototype = {
31 * Creates new empty file in specified location
33 * @param {File} directoryHandle
34 * @param {string} fileName
36 createFile: function SystemIO_createFile(directoryHandle, fileName) {
39 return directoryHandle.createFile(fileName);
41 console.error('SystemIO_createFile error:' + e.message);
47 * Writes content to file stream
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
55 writeFile: function SystemIO_writeFile(
62 onError = onError || function () {};
64 fileHandle.openStream('w', function (fileStream) {
65 if (contentEncoding === 'base64') {
66 fileStream.writeBase64(fileContent);
68 fileStream.write(fileContent);
73 // launch onSuccess callback
74 if (typeof onSuccess === 'function') {
81 * Opens specified location
83 * @param {string} directory path
84 * @param {function} on success callback
85 * @param {function} on error callback
86 * @param {string} mode
88 openDir: function SystemIO_openDir(
94 openMode = openMode || 'rw';
95 onSuccess = onSuccess || function () {};
98 tizen.filesystem.resolve(
109 * Parse specified filepath and returns data parts
111 * @param {string} filePath
114 getPathData: function SystemIO_getPathData(filePath) {
116 originalPath: filePath,
120 splittedPath = filePath.split('/');
122 path.fileName = splittedPath.pop();
123 path.dirName = splittedPath.join('/') || '/';
129 * Save specified content to file
131 * @param {string} file path
132 * @param {string} file content
133 * @param {string} file encoding
135 saveFileContent: function SystemIO_saveFileContent(
141 var pathData = this.getPathData(filePath),
145 function onOpenDirSuccess(dir) {
147 fileHandle = self.createFile(dir, pathData.fileName);
148 if (fileHandle !== false) {
149 // save data into this file
161 this.openDir(pathData.dirName, onOpenDirSuccess);
165 * Deletes node with specified path
167 * @param {string} node path
168 * @param {function} success callback
170 deleteNode: function SystemIO_deleteNode(nodePath, onSuccess) {
171 var pathData = this.getPathData(nodePath),
174 function onDeleteSuccess() {
178 function onDeleteError(e) {
179 console.error('SystemIO_deleteNode:_onDeleteError', e);
182 function onOpenDirSuccess(dir) {
183 var onListFiles = function (files) {
184 if (files.length > 0) {
186 if (files[0].isDirectory) {
206 // check file exists;
207 dir.listFiles(onListFiles, function (e) {
210 name: pathData.fileName
214 this.openDir(pathData.dirName, onOpenDirSuccess, function (e) {
215 console.error('openDir error:' + e.message);
220 * Deletes specified file
223 * @param {string} file path
224 * @param {function} delete success callback
225 * @param {function} delete error callback
227 deleteFile: function SystemIO_deleteFile(
234 dir.deleteFile(filePath, onDeleteSuccess, onDeleteError);
236 console.error('SystemIO_deleteFile error: ' + e.message);
242 * Deletes specified directory
245 * @param {string} dirPath dir path
246 * @param {function} onDeleteSuccess delete success callback
247 * @param {function} onDeleteError delete error callback
250 deleteDir: function SystemIO_deleteDir(
264 console.error('SystemIO_deleteDir error:' + e.message);
272 * @param {string} type storage type
273 * @param {function} onSuccess on success callback
274 * @param {string} excluded Excluded storage
276 getStorages: function SystemIO_getStorages(type, onSuccess, excluded) {
278 tizen.filesystem.listStorages(function (storages) {
280 len = storages.length,
283 if (type !== undefined) {
284 for (i = 0; i < len; i += 1) {
285 if (storages[i].label !== excluded) {
287 storages[i].type === 0 ||
288 storages[i].type === type
290 tmp.push(storages[i]);
298 if (typeof onSuccess === 'function') {
303 console.error('SystemIO_getStorages error:' + e.message);
307 getFilesList: function SystemIO_getFilesList(dir, onSuccess) {
315 for (i = 0; i < len; i += 1) {
318 isDirectory: files[i].isDirectory
322 if (typeof onSuccess === 'function') {
328 'SystemIO_getFilesList dir.listFiles() error:',
334 console.error('SystemIO_getFilesList error:', e.message);