Updated application sources
[apps/web/sample/FileManager.git] / project / js / app.model.js
index 7b2b4a1..6d179fe 100644 (file)
@@ -1,3 +1,19 @@
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /*jslint devel: true*/
 /*global tizen, SystemIO, $, app */
 
  * @class Model
  */
 function Model() {
-       'use strict';
-       this.init();
+    'use strict';
+    this.init();
 }
 
 (function () { // strict mode wrapper
-       'use strict';
-       Model.prototype = {
-
-               /**
-                * file open unlock flag
-                * @type {boolean}
-                */
-               openFileUnLocked: true,
-
-               /**
-                * @type SystemIO
-                */
-               systemIO: null,
-
-               /**
-                * @type Array
-                */
-               storages: [{label: 'root', type: 'INTERNAL'}],
-
-               /**
-                * @type String
-                */
-               currentPath: '',
-
-               /**
-                * API module initialisation
-                */
-               init: function Model_init() {
-                       this.systemIO = new SystemIO();
-               },
-
-               /**
-                * @returns {FileSystemStorage[]} storages
-                */
-               getInternalStorages: function Model_getInternalStorages() {
-                       return this.storages;
-               },
-
-               /**
-                * Saves storages
-                * @param {function} onSuccess callback
-                */
-               loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
-                       var self = this;
-
-                       this.systemIO.getStorages('INTERNAL', function (storages) {
-                               self.storages = storages;
-                               if (typeof onSuccess === 'function') {
-                                       onSuccess();
-                               }
-                       }, 'internal0');
-               },
-
-               /**
-                * Returns folder data
-                * @param {string} path Node path
-                * @param {function} onSuccess Success callback
-                * @param {function} onError Error callback
-                */
-               getFolderData: function Model_getFolderData(path, onSuccess, onError) {
-                       var self = this,
-                               onOpenSuccess = function (dir) {
-                               dir.listFiles(
-                                       function (files) {
-                                               self.currentPath = dir.fullPath;
-                                               onSuccess(dir, files);
-                                       },
-                                       function (e) {
-                                               console.error('Model_getFolderData listFiles error', e);
-                                       }
-                               );
-                       },
-                               onOpenError = function (e) {
-                                       console.error('Model_getFolderData openDir error', e);
-                               };
-
-                       this.systemIO.openDir(path, onOpenSuccess, onOpenError);
-               },
-
-               isStorageExists: function (nodeName, success, error) {
-                       tizen.filesystem.resolve(nodeName, success, error);
-               },
-
-               /**
-                * Launch a service to open the file
-                * @param {string} fullUri ext
-                * @param {string} mime uri
-                */
-               openFile: function Model_openFile(fullUri, mime) {
-                       if (this.openFileUnLocked) {
-                               var self = this, serviceReplyCB = {
-                                       onsuccess: function (reply) {
-                                               self.openFileUnLocked = true;
-                                       },
-                                       onfailure: function () {
-                                               self.openFileUnLocked = true;
-                                               console.error('Launch service failed');
-                                       }
-                               };
-                               this.openFileUnLocked = false;
-                               try {
-                                       console.log('Launching view for file "' + fullUri + '" (mime="' + mime + '")...');
-                                       tizen.application.launchAppControl(new tizen.ApplicationControl(
-                                               'http://tizen.org/appcontrol/operation/view',
-                                               fullUri,
-                                               mime
-                                       ),
-                                               null,
-                                               function () {
-                                                       setTimeout(function () {
-                                                               self.openFileUnLocked = true;
-                                                       }, 500);
-                                               },
-                                               function (e) {
-                                                       self.openFileUnLocked = true;
-                                                       console.error('Service launch failed. Exception message:' + e.message);
-                                               },
-                                               serviceReplyCB
-                                               );
-                               } catch (e) {
-                                       self.openFileUnLocked = true;
-                                       console.error('openFile failed', e);
-                               }
-                       }
-               },
-
-               refreshContent: function (path, successCallback, mode) {
-                       successCallback = successCallback || null;
-                       mode = mode || 'copy';
-                       var isDir = true,
-                               onResolveSuccess;
-                       if(app.helpers.getFileExtension(path) !== '') {
-                               isDir = false;
-                       }
-                       onResolveSuccess = function () {};
-
-                       if(isDir) {
-                               switch (mode) {
-                               case 'delete':
-                                       tizen.filesystem.resolve(path, onResolveSuccess, successCallback);
-                               break;
-                               default:
-                                       tizen.filesystem.resolve(path, successCallback);
-                               }
-                       } else {
-                               tizen.content.scanFile(path, successCallback);
-                       }
-               },
-
-               resolveAndRefresh: function (path, successCallback, mode) {
-                       var self = this;
-                       tizen.filesystem.resolve(path, function (file) {
-                               self.refreshContent(file.toURI(), successCallback);
-                       }, null);
-               },
-
-               /**
-                * @param {File[]} nodes Collection of node objects
-                * @param {File} dir Directory handle
-                * @param {function} onSuccess
-                * @param {function} onError
-                */
-               deleteNodes: function Model_deleteNodes(nodes, dir, onSuccess, onError) {
-                       var len = nodes.length,
-                               self = this,
-                               onDeleteNodeSuccess = function (node, isDir, fileobject) {
-                                       try {
-                                               this.refreshContent(fileobject.toURI(), function () {
-                                                       app.clipboard.removeRecursively(node.uri);
-                                                       app.ui.refreshPasteActionBtn();
-                                                       if (typeof onSuccess === 'function') {
-                                                               onSuccess(node.id);
-                                                       }
-                                               }, 'delete');
-                                       } catch (e) {
-                                               console.error(e);
-                                       }
-                               },
-                               onDeleteNodeError = function (e) {
-                                       console.error('Folder delete error', e);
-                                       if (typeof onError === 'function') {
-                                               onError();
-                                       }
-                               },
-                               i;
-
-                       for (i = 0; i < len; i = i + 1) {
-                               tizen.filesystem.resolve(
-                                       nodes[i].uri,
-                                       function (node, fileobject) {
-                                               if (node.folder) {
-                                                       dir.deleteDirectory(
-                                                               node.uri,
-                                                               true,
-                                                               onDeleteNodeSuccess.bind(this, node, true, fileobject),
-                                                               onDeleteNodeError
-                                                       );
-                                               } else {
-                                                       dir.deleteFile(
-                                                               node.uri,
-                                                               onDeleteNodeSuccess.bind(this, node, false, fileobject),
-                                                               onDeleteNodeError
-                                                       );
-                                               }
-                                       }.bind(this, nodes[i]),
-                                       null
-                               );
-                       }
-               },
-
-               /**
-                * Copy specified files to destination path
-                * Overwrites existing files
-                *
-                * @param {File} dir Directory handle
-                * @param {string[]} paths Array with absolute virtual file paths
-                * @param {string} destinationPath
-                * @param {function} onSuccess callback
-                */
-               copyNodes: function Model_copyNodes(dir, paths, destinationPath, onSuccess) {
-                       var len = paths.length, self = this,
-                               scaned = 0,
-                               scanSuccess = function () {
-                                       scaned += 1;
-                                       if (scaned === len) {
-                                               onSuccess();
-                                       }
-                               },
-                               onCopyNodeSuccess = function (file) {
-                                       self.refreshContent(file.toURI(), scanSuccess, 'copy');
-                               },
-                               onCopyNodeFailure = function (e) {
-                                       console.error(e);
-                                       setTimeout(function () {
-                                               app.ui.alertPopup('Copying error');
-                                       }, 200);
-                               },
-                               i,
-                               sourceName,
-                               decision;
-
-                       this.systemIO.getFilesList(dir, function (filesList) {
-                               for (i = 0; i < len; i = i + 1) {
-                                       if (destinationPath.indexOf(paths[i]) !== -1) {
-                                               setTimeout(function () {
-                                                       app.ui.alertPopup('Copying error');
-                                               }, 200);
-                                               return;
-                                       }
-                               }
-
-                               for (i = 0; i < len; i = i + 1) {
-                                       decision = true;
-                                       sourceName = paths[i].split('/').pop();
-                                       sourceName = app.helpers.getCopyFileName(sourceName, filesList);
-
-                                       try {
-                                               dir.copyTo(paths[i], destinationPath + '/' + sourceName, true, onCopyNodeSuccess, onCopyNodeFailure);
-                                               self.resolveAndRefresh(paths[i]);
-                                       } catch (e) {
-                                               console.error(e);
-                                       }
-                               }
-                       });
-               },
-
-               /**
-                * Move specified files to destination path
-                * Overwrites existing files
-                *
-                * @param {File} dir Directory handle
-                * @param {string[]} paths Array with absolute virtual file paths
-                * @param {string} destinationPath
-                * @param {function} onSuccess callback
-                */
-               moveNodes: function Model_moveNodes(dir, paths, destinationPath, onSuccess) {
-                       var len = paths.length, self = this,
-                               scaned = 0,
-                               toScan = len * 2,
-                               illegalMove = false,
-                               scanSuccess = function () {
-                                       scaned += 1;
-                                       if (scaned === toScan) {
-                                               onSuccess();
-                                       }
-                               },
-                               onMoveNodeSuccess = function (oldfile, file) {
-                                       self.refreshContent(oldfile.toURI(), scanSuccess, 'delete');
-                                       self.refreshContent(file.toURI(), scanSuccess, 'copy');
-                               },
-                               onMoveNodeFailure = function () {
-                                       app.ui.alertPopup('Moving error');
-                               },
-                               i,
-                               sourceName,
-                               decision;
-
-                       while (len--) {
-                               if (destinationPath.match(paths[len])) {
-                                       illegalMove = true;
-                                       break;
-                               }
-                       }
-
-                       len = paths.length;
-
-                       if (illegalMove) {
-                               setTimeout(function () {
-                                       app.ui.alertPopup('Can not move catalog into itself.');
-                               }, 200);
-                               return;
-                       }
-
-                       this.systemIO.getFilesList(dir, function (filesList) {
-                               for (i = 0; i < len; i = i + 1) {
-                                       if (destinationPath.indexOf(paths[i]) !== -1) {
-                                               app.ui.alertPopup('Moving error');
-                                               return;
-                                       }
-                               }
-
-                               for (i = 0; i < len; i = i + 1) {
-                                       decision = true;
-                                       sourceName = paths[i].split('/').pop();
-                                       try {
-                                               tizen.filesystem.resolve(
-                                                       paths[i],
-                                                       function (path, destinationPath, sourceName, oldfile) {
-                                                               dir.moveTo(path, destinationPath + '/' + sourceName, false, onMoveNodeSuccess.bind(self, oldfile), onMoveNodeFailure);
-                                                       }.bind(self, paths[i], destinationPath, sourceName),
-                                                       null
-                                               );
-                                       } catch (e) {
-                                               console.error(e);
-                                       }
-                               }
-                       });
-               }
-       };
+    'use strict';
+    Model.prototype = {
+
+        /**
+         * file open unlock flag
+         * @type {boolean}
+         */
+        openFileUnLocked: true,
+
+        /**
+         * @type SystemIO
+         */
+        systemIO: null,
+
+        /**
+         * @type Array
+         */
+        storages: [{label: 'root', type: 'INTERNAL'}],
+
+        /**
+         * @type String
+         */
+        currentPath: '',
+
+        /**
+         * API module initialisation
+         */
+        init: function Model_init() {
+            this.systemIO = new SystemIO();
+        },
+
+        /**
+         * @returns {FileSystemStorage[]} storages
+         */
+        getInternalStorages: function Model_getInternalStorages() {
+            return this.storages;
+        },
+
+        /**
+         * Saves storages
+         * @param {function} onSuccess callback
+         */
+        loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
+            var self = this;
+
+            this.systemIO.getStorages('INTERNAL', function (storages) {
+                self.storages = storages;
+                if (typeof onSuccess === 'function') {
+                    onSuccess();
+                }
+            }, 'internal0');
+        },
+
+        /**
+         * Returns folder data
+         * @param {string} path Node path
+         * @param {function} onSuccess Success callback
+         * @param {function} onError Error callback
+         */
+        getFolderData: function Model_getFolderData(path, onSuccess, onError) {
+            var self = this,
+                onOpenSuccess = function (dir) {
+                    dir.listFiles(
+                        function (files) {
+                            self.currentPath = dir.fullPath;
+                            onSuccess(dir, files);
+                        },
+                        function (e) {
+                            console.error(
+                                'Model_getFolderData listFiles error',
+                                e
+                            );
+                        }
+                    );
+                },
+                onOpenError = function (e) {
+                    console.error('Model_getFolderData openDir error', e);
+                };
+
+            this.systemIO.openDir(path, onOpenSuccess, onOpenError);
+        },
+
+        isStorageExists: function (nodeName, success, error) {
+            tizen.filesystem.resolve(nodeName, success, error);
+        },
+
+        /**
+         * Launch a service to open the file
+         * @param {string} fullUri ext
+         * @param {string} mime uri
+         */
+        openFile: function Model_openFile(fullUri) {
+            if (this.openFileUnLocked) {
+                var self = this, serviceReplyCB = {
+                    onsuccess: function (reply) {
+                        self.openFileUnLocked = true;
+                    },
+                    onfailure: function () {
+                        self.openFileUnLocked = true;
+                        console.error('Launch service failed');
+                    }
+                };
+                this.openFileUnLocked = false;
+                try {
+                    console.log('Launching view for file "' + fullUri + '"');
+                    tizen.application.launchAppControl(
+                        new tizen.ApplicationControl(
+                            'http://tizen.org/appcontrol/operation/view',
+                            fullUri
+                        ),
+                        null,
+                        function () {
+                            setTimeout(function () {
+                                self.openFileUnLocked = true;
+                            }, 500);
+                        },
+                        function (e) {
+                            self.openFileUnLocked = true;
+                            console.error(
+                                'Service launch failed. Exception message:' +
+                                    e.message
+                            );
+                        },
+                        serviceReplyCB
+                    );
+                } catch (e) {
+                    self.openFileUnLocked = true;
+                    console.error('openFile failed', e);
+                }
+            }
+        },
+
+        refreshContent: function (path, successCallback, mode) {
+            successCallback = successCallback || null;
+            mode = mode || 'copy';
+            var isDir = true,
+                onResolveSuccess;
+            onResolveSuccess = function () {};
+
+            tizen.filesystem.resolve(path, function (file) {
+                if (file.isFile) {
+                    tizen.content.scanFile(path, successCallback);
+                } else {
+                    successCallback();
+                }
+            }, null);
+        },
+
+        resolveAndRefresh: function (path, successCallback, mode) {
+            var self = this;
+            tizen.filesystem.resolve(path, function (file) {
+                self.refreshContent(file.toURI(), successCallback);
+            }, null);
+        },
+
+        /**
+         * @param {File[]} nodes Collection of node objects
+         * @param {File} dir Directory handle
+         * @param {function} onSuccess
+         * @param {function} onError
+         */
+        deleteNodes: function Model_deleteNodes(
+            nodes,
+            dir,
+            onSuccess,
+            onError
+        ) {
+            var len = nodes.length,
+                self = this,
+                onDeleteNodeSuccess = function (file) {
+                    try {
+                        app.clipboard.removeRecursively(file.fullPath);
+                        app.ui.refreshPasteActionBtn();
+                        if (onSuccess instanceof Function) {
+                            onSuccess(file.fullPath);
+                        }
+                    } catch (e) {
+                        console.error(e);
+                    }
+                },
+                onDeleteNodeError = function (e) {
+                    if (typeof onError === 'function') {
+                        onError();
+                    }
+                },
+                i,
+                remove = function (file, success, failure) {
+                    var parent = file.parent, fullPath = file.fullPath,
+                        removeSuccess = function () {
+                            if (success instanceof Function) {
+                                success(file);
+                            }
+                        },
+                        removeFailure = function (error) {
+                            if (failure instanceof Function) {
+                                failure(error, file);
+                            }
+                        };
+
+                    if (file.isFile) {
+                        parent.deleteFile(
+                            fullPath,
+                            function () {
+                                tizen.content.scanFile(
+                                    file.toURI(),
+                                    removeSuccess,
+                                    removeFailure
+                                );
+                            },
+                            removeFailure
+                        );
+                    } else {
+                        file.listFiles(
+                            function (files) {
+                                var len = files.length,
+                                    index = len - 1,
+                                    counter = 0,
+                                    removeEmptyDir = function () {
+                                        parent.deleteDirectory(
+                                            fullPath,
+                                            false,
+                                            removeSuccess,
+                                            removeFailure
+                                        );
+                                    };
+
+                                if (len > 0) {
+                                    while (index >= 0) {
+                                        remove(
+                                            files[index],
+                                            function () {
+                                                counter += 1;
+                                                if (counter === len) {
+                                                    removeEmptyDir();
+                                                }
+                                            },
+                                            removeFailure
+                                        );
+                                        index -= 1;
+                                    }
+                                } else {
+                                    removeEmptyDir();
+                                }
+                            },
+                            removeFailure
+                        );
+                    }
+                };
+
+            for (i = 0; i < len; i = i + 1) {
+                tizen.filesystem.resolve(
+                    nodes[i].uri,
+                    function (file) {
+                        remove(file, onDeleteNodeSuccess, onDeleteNodeError);
+                    }.bind(this),
+                    null
+                );
+            }
+        },
+
+        /**
+         * Copy specified files to destination path
+         * Overwrites existing files
+         *
+         * @param {File} dir Directory handle
+         * @param {string[]} paths Array with absolute virtual file paths
+         * @param {string} destinationPath
+         * @param {function} onSuccess callback
+         */
+        copyNodes: function Model_copyNodes(
+            dir,
+            paths,
+            destinationPath,
+            onSuccess
+        ) {
+            var len = paths.length, self = this,
+                scaned = 0,
+                scanSuccess = function () {
+                    scaned += 1;
+                    if (scaned === len) {
+                        onSuccess();
+                    }
+                },
+                onCopyNodeSuccess = function (file) {
+                    self.refreshContent(file.toURI(), scanSuccess, 'copy');
+                },
+                onCopyNodeFailure = function (e) {
+                    console.error(e);
+                    setTimeout(function () {
+                        app.refreshCurrentPage();
+                        app.ui.alertPopup('Copying error');
+                    }, 200);
+                },
+                i,
+                sourceName,
+                decision;
+
+            this.systemIO.getFilesList(dir, function (filesList) {
+                for (i = 0; i < len; i = i + 1) {
+                    if (destinationPath.indexOf(paths[i]) !== -1) {
+                        setTimeout(function () {
+                            app.ui.alertPopup('Copying error');
+                        }, 200);
+                        return;
+                    }
+                }
+
+                for (i = 0; i < len; i = i + 1) {
+                    decision = true;
+                    sourceName = paths[i].split('/').pop();
+                    sourceName = app.helpers.getCopyFileName(
+                        sourceName,
+                        filesList
+                    );
+
+                    try {
+                        dir.copyTo(
+                            paths[i],
+                            destinationPath + '/' + sourceName,
+                            true,
+                            onCopyNodeSuccess,
+                            onCopyNodeFailure
+                        );
+                    } catch (e) {
+                        console.error(e);
+                    }
+                }
+            });
+        },
+
+        /**
+         * Move specified files to destination path
+         * Overwrites existing files
+         *
+         * @param {File} dir Directory handle
+         * @param {string[]} paths Array with absolute virtual file paths
+         * @param {string} destinationPath
+         * @param {function} onSuccess callback
+         */
+        moveNodes: function Model_moveNodes(
+            dir,
+            paths,
+            destinationPath,
+            onSuccess
+        ) {
+            var len = paths.length, self = this,
+                scaned = 0,
+                toScan = len * 2,
+                illegalMove = false,
+                scanSuccess = function () {
+                    scaned += 1;
+                    if (scaned === toScan) {
+                        onSuccess();
+                    }
+                },
+                onMoveNodeSuccess = function (oldfile, file) {
+                    self.refreshContent(oldfile.toURI(), scanSuccess, 'delete');
+                    self.refreshContent(file.toURI(), scanSuccess, 'copy');
+                },
+                onMoveNodeFailure = function () {
+                    app.ui.alertPopup('Moving error');
+                },
+                i,
+                sourceName,
+                decision;
+
+            len -= 1;
+            while (len >= 0) {
+                if (destinationPath.match(paths[len])) {
+                    illegalMove = true;
+                    break;
+                }
+                len -= 1;
+            }
+
+            len = paths.length;
+
+            if (illegalMove) {
+                setTimeout(function () {
+                    app.ui.alertPopup('Can not move catalog into itself.');
+                }, 200);
+                return;
+            }
+
+            this.systemIO.getFilesList(dir, function (filesList) {
+                var resolveSuccess = function (
+                    path,
+                    destinationPath,
+                    sourceName,
+                    oldfile
+                ) {
+                    dir.moveTo(
+                        path,
+                        destinationPath + '/' + sourceName,
+                        false,
+                        onMoveNodeSuccess.bind(self, oldfile),
+                        onMoveNodeFailure
+                    );
+                };
+                for (i = 0; i < len; i = i + 1) {
+                    if (destinationPath.indexOf(paths[i]) !== -1) {
+                        app.ui.alertPopup('Moving error');
+                        return;
+                    }
+                }
+
+                for (i = 0; i < len; i = i + 1) {
+                    decision = true;
+                    sourceName = paths[i].split('/').pop();
+                    try {
+                        tizen.filesystem.resolve(
+                            paths[i],
+                            resolveSuccess.bind(
+                                self,
+                                paths[i],
+                                destinationPath,
+                                sourceName
+                            ),
+                            onMoveNodeFailure
+                        );
+                    } catch (e) {
+                        console.error(e);
+                    }
+                }
+            });
+        }
+    };
 }());