2 /*global tizen, SystemIO, $, app */
12 (function () { // strict mode wrapper
17 * file open unlock flag
20 openFileUnLocked: true,
30 storages: [{label: 'root', type: 'INTERNAL'}],
38 * API module initialisation
40 init: function Model_init() {
41 this.systemIO = new SystemIO();
45 * @returns {FileSystemStorage[]} storages
47 getInternalStorages: function Model_getInternalStorages() {
53 * @param {function} onSuccess callback
55 loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
58 this.systemIO.getStorages('INTERNAL', function (storages) {
59 self.storages = storages;
60 if (typeof onSuccess === 'function') {
68 * @param {string} path Node path
69 * @param {function} onSuccess Success callback
70 * @param {function} onError Error callback
72 getFolderData: function Model_getFolderData(path, onSuccess, onError) {
74 onOpenSuccess = function (dir) {
77 self.currentPath = dir.fullPath;
78 onSuccess(dir, files);
81 console.error('Model_getFolderData listFiles error', e);
85 onOpenError = function (e) {
86 console.error('Model_getFolderData openDir error', e);
89 this.systemIO.openDir(path, onOpenSuccess, onOpenError);
92 isStorageExists: function (nodeName, success, error) {
93 tizen.filesystem.resolve(nodeName, success, error);
97 * Launch a service to open the file
98 * @param {string} fullUri ext
99 * @param {string} mime uri
101 openFile: function Model_openFile(fullUri, mime) {
102 if (this.openFileUnLocked) {
103 var self = this, serviceReplyCB = {
104 onsuccess: function (reply) {
105 self.openFileUnLocked = true;
107 onfailure: function () {
108 self.openFileUnLocked = true;
109 console.error('Launch service failed');
112 this.openFileUnLocked = false;
114 tizen.application.launchAppControl(new tizen.ApplicationControl(
115 'http://tizen.org/appcontrol/operation/view',
121 setTimeout(function () {
122 self.openFileUnLocked = true;
126 self.openFileUnLocked = true;
127 console.error('launch sevice failed. reason :' + e.message);
132 self.openFileUnLocked = true;
133 console.error('openFile error:', e);
138 refreshContent: function (path) {
139 tizen.content.scanFile(path, null, null);
142 resolveAndRefresh: function (path) {
144 tizen.filesystem.resolve(path, function(file) {
145 self.refreshContent(file.toURI());
150 * @param {File[]} nodes Collection of node objects
151 * @param {File} dir Directory handle
152 * @param {function} onSuccess
153 * @param {function} onError
155 deleteNodes: function Model_deleteNodes(nodes, dir, onSuccess, onError) {
156 var len = nodes.length,
158 onDeleteNodeSuccess = function (nodeId, isDir) {
159 if (typeof onSuccess === 'function') {
163 onDeleteNodeError = function (e) {
164 console.error('Folder delete error', e);
165 if (typeof onError === 'function') {
171 for (i = 0; i < len; i = i + 1) {
172 if (nodes[i].folder) {
176 onDeleteNodeSuccess.bind(this, nodes[i].id, true),
182 onDeleteNodeSuccess.bind(this, nodes[i].id, false),
185 this.resolveAndRefresh(nodes[i]);
191 * Copy specified files to destination path
192 * Overwrites existing files
194 * @param {File} dir Directory handle
195 * @param {string[]} paths Array with absolute virtual file paths
196 * @param {string} destinationPath
197 * @param {function} onSuccess callback
199 copyNodes: function Model_copyNodes(dir, paths, destinationPath, onSuccess) {
200 var len = paths.length, self = this,
202 onCopyNodeSuccess = function (file) {
204 self.refreshContent(file.toURI());
205 if (copied === len) {
209 onCopyNodeFailure = function () {
210 app.ui.alertPopup('Copying error');
216 this.systemIO.getFilesList(dir, function (filesList) {
217 for (i = 0; i < len; i = i + 1) {
218 if (destinationPath.indexOf(paths[i]) !== -1) {
219 app.ui.alertPopup('Copying error');
224 for (i = 0; i < len; i = i + 1) {
226 sourceName = paths[i].split('/').pop();
227 sourceName = app.helpers.getCopyFileName(sourceName, filesList);
230 dir.copyTo(paths[i], destinationPath + '/' + sourceName, true, onCopyNodeSuccess, onCopyNodeFailure);
231 self.resolveAndRefresh(paths[i]);
240 * Move specified files to destination path
241 * Overwrites existing files
243 * @param {File} dir Directory handle
244 * @param {string[]} paths Array with absolute virtual file paths
245 * @param {string} destinationPath
246 * @param {function} onSuccess callback
248 moveNodes: function Model_moveNodes(dir, paths, destinationPath, onSuccess) {
249 var len = paths.length, self = this,
251 onMoveNodeSuccess = function (file) {
252 self.refreshContent(file.toURI());
258 onMoveNodeFailure = function () {
259 app.ui.alertPopup('Moving error');
265 this.systemIO.getFilesList(dir, function (filesList) {
266 for (i = 0; i < len; i = i + 1) {
267 if (destinationPath.indexOf(paths[i]) !== -1) {
268 app.ui.alertPopup('Moving error');
273 for (i = 0; i < len; i = i + 1) {
275 sourceName = paths[i].split('/').pop();
278 dir.moveTo(paths[i], destinationPath + '/' + sourceName, true, onMoveNodeSuccess, onMoveNodeFailure);
279 self.resolveAndRefresh(paths[i]);