[FileManager]update FileManager(tizen_2.1)
[samples/web/FileManager.git] / js / app.model.js
1 /*jslint devel: true*/
2 /*global tizen, SystemIO, $ */
3
4 /**
5  * @class Model
6  */
7 function Model() {
8         'use strict';
9         this.init();
10 }
11
12 (function () { // strict mode wrapper
13         'use strict';
14         Model.prototype = {
15
16                 /**
17                  * file open unlock flag
18                  * @type {boolean}
19                  */
20                 openFileUnLocked: true,
21
22                 /**
23                  * @type SystemIO
24                  */
25                 systemIO: null,
26
27                 /**
28                  * @type Array
29                  */
30                 storages: [{label: 'root', type: 'INTERNAL'}],
31
32                 /**
33                  * API module initialisation
34                  */
35                 init: function Model_init() {
36                         this.systemIO = new SystemIO();
37                 },
38
39                 /**
40                  * @returns {FileSystemStorage[]} storages
41                  */
42                 getInternalStorages: function Model_getInternalStorages() {
43                         return this.storages;
44                 },
45
46                 /**
47                  * Saves storages
48                  * @param {function} onSuccess callback
49                  */
50                 loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
51                         var self = this;
52
53                         this.systemIO.getStorages('INTERNAL', function (storages) {
54                                 self.storages = storages;
55                                 if (typeof onSuccess === 'function') {
56                                         onSuccess();
57                                 }
58                         }, 'internal0');
59                 },
60
61                 /**
62                  * Returns folder data
63                  * @param {string} path Node path
64                  * @param {function} onSuccess Success callback
65                  * @param {function} onError Error callback
66                  */
67                 getFolderData: function Model_getFolderData(path, onSuccess, onError) {
68
69                         var onOpenSuccess = function (dir) {
70                                 dir.listFiles(
71                                         function (files) {
72                                                 onSuccess(dir, files);
73                                         },
74                                         function (e) {
75                                                 console.error('Model_getFolderData listFiles error', e);
76                                         }
77                                 );
78                         },
79                                 onOpenError = function (e) {
80                                         console.error('Model_getFolderData openDir error', e);
81                                 };
82
83                         this.systemIO.openDir(path, onOpenSuccess, onOpenError);
84                 },
85
86                 /**
87                  * Launch a service to open the file
88                  * @param {string} fullUri ext
89                  * @param {string} mime uri
90                  */
91                 openFile: function Model_openFile(fullUri, mime) {
92                         if (this.openFileUnLocked) {
93                                 var self = this, serviceReplyCB = {
94                                         onsuccess: function (reply) {
95                                                 self.openFileUnLocked = true;
96                                         },
97                                         onfailure: function () {
98                                                 self.openFileUnLocked = true;
99                                                 console.error('Launch service failed');
100                                         }
101                                 };
102                                 this.openFileUnLocked = false;
103                                 try {
104                                         tizen.application.launchAppControl(new tizen.ApplicationControl(
105                                                 'http://tizen.org/appcontrol/operation/view',
106                                                 fullUri,
107                                                 mime
108                                         ),
109                                                 null,
110                                                 function () {
111                                                         setTimeout(function () {
112                                                                 self.openFileUnLocked = true;
113                                                         }, 500);
114                                                 },
115                                                 function (e) {
116                                                         self.openFileUnLocked = true;
117                                                         console.error('launch sevice failed. reason :' + e.message);
118                                                 },
119                                                 serviceReplyCB
120                                                 );
121                                 } catch (e) {
122                                         self.openFileUnLocked = true;
123                                         console.error('openFile error:', e);
124                                 }
125                         }
126                 },
127
128                 /**
129                  * @param {File[]} nodes Collection of node objects
130                  * @param {File} dir Directory handle
131                  * @param {function} onSuccess
132                  * @param {function} onError
133                  */
134                 deleteNodes: function Model_deleteNodes(nodes, dir, onSuccess, onError) {
135                         var len = nodes.length,
136                                 onDeleteNodeSuccess = function (nodeId, isDir) {
137                                         if (typeof onSuccess === 'function') {
138                                                 onSuccess(nodeId);
139                                         }
140                                 },
141                                 onDeleteNodeError = function (e) {
142                                         console.error('Folder delete error', e);
143                                         if (typeof onError === 'function') {
144                                                 onError();
145                                         }
146                                 },
147                                 i;
148
149                         for (i = 0; i < len; i = i + 1) {
150                                 if (nodes[i].folder) {
151                                         dir.deleteDirectory(
152                                                 nodes[i].uri,
153                                                 true,
154                                                 onDeleteNodeSuccess.bind(this, nodes[i].id, true),
155                                                 onDeleteNodeError
156                                         );
157                                 } else {
158                                         dir.deleteFile(
159                                                 nodes[i].uri,
160                                                 onDeleteNodeSuccess.bind(this, nodes[i].id, false),
161                                                 onDeleteNodeError
162                                         );
163                                 }
164                         }
165                 },
166
167                 /**
168                  * Copy specified files to destination path
169                  * Overwrites existing files
170                  *
171                  * @param {File} dir Directory handle
172                  * @param {string[]} paths Array with absolute virtual file paths
173                  * @param {string} destinationPath
174                  * @param {function} onSuccess callback
175                  */
176                 copyNodes: function Model_copyNodes(dir, paths, destinationPath, onSuccess) {
177                         var len = paths.length,
178                                 copied = 0,
179                                 onCopyNodeSuccess = function () {
180                                         copied += 1;
181                                         if (copied === len) {
182                                                 onSuccess();
183                                         }
184                                 },
185                                 onCopyNodeFailure = function () {
186                                         alert('Copying error');
187                                 },
188                                 i,
189                                 sourceName,
190                                 decision;
191
192                         this.systemIO.getFilesList(dir, function (filesList) {
193                                 for (i = 0; i < len; i = i + 1) {
194                                         if (destinationPath.indexOf(paths[i]) !== -1) {
195                                                 alert('Copying error');
196                                                 return;
197                                         }
198                                 }
199
200                                 for (i = 0; i < len; i = i + 1) {
201                                         decision = true;
202                                         sourceName = paths[i].split('/').pop();
203                                         sourceName = app.helpers.getCopyFileName(sourceName, filesList);
204
205                                         try {
206                                                 dir.copyTo(paths[i], destinationPath + '/' + sourceName, true, onCopyNodeSuccess, onCopyNodeFailure);
207                                         } catch (e) {
208                                                 console.error(e);
209                                         }
210                                 }
211                         });
212                 },
213
214                 /**
215                  * Move specified files to destination path
216                  * Overwrites existing files
217                  *
218                  * @param {File} dir Directory handle
219                  * @param {string[]} paths Array with absolute virtual file paths
220                  * @param {string} destinationPath
221                  * @param {function} onSuccess callback
222                  */
223                 moveNodes: function Model_moveNodes(dir, paths, destinationPath, onSuccess) {
224                         var len = paths.length,
225                                 moved = 0,
226                                 onMoveNodeSuccess = function () {
227                                         moved += 1;
228                                         if (moved === len) {
229                                                 onSuccess();
230                                         }
231                                 },
232                                 onMoveNodeFailure = function () {
233                                         alert('Moving error');
234                                 },
235                                 i,
236                                 sourceName,
237                                 decision;
238
239                         this.systemIO.getFilesList(dir, function (filesList) {
240                                 for (i = 0; i < len; i = i + 1) {
241                                         if (destinationPath.indexOf(paths[i]) !== -1) {
242                                                 alert('Moving error');
243                                                 return;
244                                         }
245                                 }
246
247                                 for (i = 0; i < len; i = i + 1) {
248                                         decision = true;
249                                         sourceName = paths[i].split('/').pop();
250
251                                         if ($.inArray(sourceName, filesList) !== -1) {
252                                                 decision = confirm('A file with (' + sourceName + ') name already exists.\nDo you want to overwrite it?');
253                                         }
254
255                                         if (decision) {
256                                                 dir.moveTo(paths[i], destinationPath + '/' + sourceName, true, onMoveNodeSuccess, onMoveNodeFailure);
257                                         }
258                                 }
259                         });
260                 }
261         };
262 }());