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, SystemIO, $, app */
28 (function () { // strict mode wrapper
33 * file open unlock flag
36 openFileUnLocked: true,
46 storages: [{label: 'root', type: 'INTERNAL'}],
54 * API module initialisation
56 init: function Model_init() {
57 this.systemIO = new SystemIO();
61 * @returns {FileSystemStorage[]} storages
63 getInternalStorages: function Model_getInternalStorages() {
69 * @param {function} onSuccess callback
71 loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
74 this.systemIO.getStorages('INTERNAL', function (storages) {
75 self.storages = storages;
76 if (typeof onSuccess === 'function') {
84 * @param {string} path Node path
85 * @param {function} onSuccess Success callback
86 * @param {function} onError Error callback
88 getFolderData: function Model_getFolderData(path, onSuccess, onError) {
90 onOpenSuccess = function (dir) {
93 self.currentPath = dir.fullPath;
94 onSuccess(dir, files);
98 'Model_getFolderData listFiles error',
104 onOpenError = function (e) {
105 console.error('Model_getFolderData openDir error', e);
108 this.systemIO.openDir(path, onOpenSuccess, onOpenError);
111 isStorageExists: function (nodeName, success, error) {
112 tizen.filesystem.resolve(nodeName, success, error);
116 * Launch a service to open the file
117 * @param {string} fullUri ext
118 * @param {string} mime uri
120 openFile: function Model_openFile(fullUri) {
121 if (this.openFileUnLocked) {
122 var self = this, serviceReplyCB = {
123 onsuccess: function (reply) {
124 self.openFileUnLocked = true;
126 onfailure: function () {
127 self.openFileUnLocked = true;
128 console.error('Launch service failed');
131 this.openFileUnLocked = false;
133 console.log('Launching view for file "' + fullUri + '"');
134 tizen.application.launchAppControl(
135 new tizen.ApplicationControl(
136 'http://tizen.org/appcontrol/operation/view',
141 setTimeout(function () {
142 self.openFileUnLocked = true;
146 self.openFileUnLocked = true;
148 'Service launch failed. Exception message:' +
155 self.openFileUnLocked = true;
156 console.error('openFile failed', e);
161 refreshContent: function (path, successCallback, mode) {
162 successCallback = successCallback || null;
163 mode = mode || 'copy';
166 onResolveSuccess = function () {};
168 tizen.filesystem.resolve(path, function (file) {
170 tizen.content.scanFile(path, successCallback);
177 resolveAndRefresh: function (path, successCallback, mode) {
179 tizen.filesystem.resolve(path, function (file) {
180 self.refreshContent(file.toURI(), successCallback);
185 * @param {File[]} nodes Collection of node objects
186 * @param {File} dir Directory handle
187 * @param {function} onSuccess
188 * @param {function} onError
190 deleteNodes: function Model_deleteNodes(
196 var len = nodes.length,
198 onDeleteNodeSuccess = function (file) {
200 app.clipboard.removeRecursively(file.fullPath);
201 app.ui.refreshPasteActionBtn();
202 if (onSuccess instanceof Function) {
203 onSuccess(file.fullPath);
209 onDeleteNodeError = function (e) {
210 if (typeof onError === 'function') {
215 remove = function (file, success, failure) {
216 var parent = file.parent, fullPath = file.fullPath,
217 removeSuccess = function () {
218 if (success instanceof Function) {
222 removeFailure = function (error) {
223 if (failure instanceof Function) {
224 failure(error, file);
232 tizen.content.scanFile(
243 var len = files.length,
246 removeEmptyDir = function () {
247 parent.deleteDirectory(
261 if (counter === len) {
278 for (i = 0; i < len; i = i + 1) {
279 tizen.filesystem.resolve(
282 remove(file, onDeleteNodeSuccess, onDeleteNodeError);
290 * Copy specified files to destination path
291 * Overwrites existing files
293 * @param {File} dir Directory handle
294 * @param {string[]} paths Array with absolute virtual file paths
295 * @param {string} destinationPath
296 * @param {function} onSuccess callback
298 copyNodes: function Model_copyNodes(
304 var len = paths.length, self = this,
306 scanSuccess = function () {
308 if (scaned === len) {
312 onCopyNodeSuccess = function (file) {
313 self.refreshContent(file.toURI(), scanSuccess, 'copy');
315 onCopyNodeFailure = function (e) {
317 setTimeout(function () {
318 app.refreshCurrentPage();
319 app.ui.alertPopup('Copying error');
326 this.systemIO.getFilesList(dir, function (filesList) {
327 for (i = 0; i < len; i = i + 1) {
328 if (destinationPath.indexOf(paths[i]) !== -1) {
329 setTimeout(function () {
330 app.ui.alertPopup('Copying error');
336 for (i = 0; i < len; i = i + 1) {
338 sourceName = paths[i].split('/').pop();
339 sourceName = app.helpers.getCopyFileName(
347 destinationPath + '/' + sourceName,
360 * Move specified files to destination path
361 * Overwrites existing files
363 * @param {File} dir Directory handle
364 * @param {string[]} paths Array with absolute virtual file paths
365 * @param {string} destinationPath
366 * @param {function} onSuccess callback
368 moveNodes: function Model_moveNodes(
374 var len = paths.length, self = this,
378 scanSuccess = function () {
380 if (scaned === toScan) {
384 onMoveNodeSuccess = function (oldfile, file) {
385 self.refreshContent(oldfile.toURI(), scanSuccess, 'delete');
386 self.refreshContent(file.toURI(), scanSuccess, 'copy');
388 onMoveNodeFailure = function () {
389 app.ui.alertPopup('Moving error');
397 if (destinationPath.match(paths[len])) {
407 setTimeout(function () {
408 app.ui.alertPopup('Can not move catalog into itself.');
413 this.systemIO.getFilesList(dir, function (filesList) {
414 var resolveSuccess = function (
422 destinationPath + '/' + sourceName,
424 onMoveNodeSuccess.bind(self, oldfile),
428 for (i = 0; i < len; i = i + 1) {
429 if (destinationPath.indexOf(paths[i]) !== -1) {
430 app.ui.alertPopup('Moving error');
435 for (i = 0; i < len; i = i + 1) {
437 sourceName = paths[i].split('/').pop();
439 tizen.filesystem.resolve(