[FileManager]Updated Private -> RSA
[samples/web/FileManager.git] / js / app.model.js
1 /*jslint devel: true*/
2 /*global tizen, SystemIO, $, app */
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                  * @type String
34                  */
35                 currentPath: '',
36
37                 /**
38                  * API module initialisation
39                  */
40                 init: function Model_init() {
41                         this.systemIO = new SystemIO();
42                 },
43
44                 /**
45                  * @returns {FileSystemStorage[]} storages
46                  */
47                 getInternalStorages: function Model_getInternalStorages() {
48                         return this.storages;
49                 },
50
51                 /**
52                  * Saves storages
53                  * @param {function} onSuccess callback
54                  */
55                 loadInternalStorages: function Model_loadInternalStorages(onSuccess) {
56                         var self = this;
57
58                         this.systemIO.getStorages('INTERNAL', function (storages) {
59                                 self.storages = storages;
60                                 if (typeof onSuccess === 'function') {
61                                         onSuccess();
62                                 }
63                         }, 'internal0');
64                 },
65
66                 /**
67                  * Returns folder data
68                  * @param {string} path Node path
69                  * @param {function} onSuccess Success callback
70                  * @param {function} onError Error callback
71                  */
72                 getFolderData: function Model_getFolderData(path, onSuccess, onError) {
73                         var self = this,
74                                 onOpenSuccess = function (dir) {
75                                 dir.listFiles(
76                                         function (files) {
77                                                 self.currentPath = dir.fullPath;
78                                                 onSuccess(dir, files);
79                                         },
80                                         function (e) {
81                                                 console.error('Model_getFolderData listFiles error', e);
82                                         }
83                                 );
84                         },
85                                 onOpenError = function (e) {
86                                         console.error('Model_getFolderData openDir error', e);
87                                 };
88
89                         this.systemIO.openDir(path, onOpenSuccess, onOpenError);
90                 },
91
92                 isStorageExists: function (nodeName, success, error) {
93                         tizen.filesystem.resolve(nodeName, success, error);
94                 },
95
96                 /**
97                  * Launch a service to open the file
98                  * @param {string} fullUri ext
99                  * @param {string} mime uri
100                  */
101                 openFile: function Model_openFile(fullUri, mime) {
102                         if (this.openFileUnLocked) {
103                                 var self = this, serviceReplyCB = {
104                                         onsuccess: function (reply) {
105                                                 self.openFileUnLocked = true;
106                                         },
107                                         onfailure: function () {
108                                                 self.openFileUnLocked = true;
109                                                 console.error('Launch service failed');
110                                         }
111                                 };
112                                 this.openFileUnLocked = false;
113                                 try {
114                                         tizen.application.launchAppControl(new tizen.ApplicationControl(
115                                                 'http://tizen.org/appcontrol/operation/view',
116                                                 fullUri,
117                                                 mime
118                                         ),
119                                                 null,
120                                                 function () {
121                                                         setTimeout(function () {
122                                                                 self.openFileUnLocked = true;
123                                                         }, 500);
124                                                 },
125                                                 function (e) {
126                                                         self.openFileUnLocked = true;
127                                                         console.error('launch sevice failed. reason :' + e.message);
128                                                 },
129                                                 serviceReplyCB
130                                                 );
131                                 } catch (e) {
132                                         self.openFileUnLocked = true;
133                                         console.error('openFile error:', e);
134                                 }
135                         }
136                 },
137
138                 refreshContent: function (path) {
139                         tizen.content.scanFile(path, null, null);
140                 },
141
142                 resolveAndRefresh: function (path) {
143                         var self = this;
144                         tizen.filesystem.resolve(path, function(file) {
145                                 self.refreshContent(file.toURI());
146                         }, null);
147                 },
148
149                 /**
150                  * @param {File[]} nodes Collection of node objects
151                  * @param {File} dir Directory handle
152                  * @param {function} onSuccess
153                  * @param {function} onError
154                  */
155                 deleteNodes: function Model_deleteNodes(nodes, dir, onSuccess, onError) {
156                         var len = nodes.length,
157                                 self = this,
158                                 onDeleteNodeSuccess = function (nodeId, isDir) {
159                                         if (typeof onSuccess === 'function') {
160                                                 onSuccess(nodeId);
161                                         }
162                                 },
163                                 onDeleteNodeError = function (e) {
164                                         console.error('Folder delete error', e);
165                                         if (typeof onError === 'function') {
166                                                 onError();
167                                         }
168                                 },
169                                 i;
170
171                         for (i = 0; i < len; i = i + 1) {
172                                 if (nodes[i].folder) {
173                                         dir.deleteDirectory(
174                                                 nodes[i].uri,
175                                                 true,
176                                                 onDeleteNodeSuccess.bind(this, nodes[i].id, true),
177                                                 onDeleteNodeError
178                                         );
179                                 } else {
180                                         dir.deleteFile(
181                                                 nodes[i].uri,
182                                                 onDeleteNodeSuccess.bind(this, nodes[i].id, false),
183                                                 onDeleteNodeError
184                                         );
185                                         this.resolveAndRefresh(nodes[i]);
186                                 }
187                         }
188                 },
189
190                 /**
191                  * Copy specified files to destination path
192                  * Overwrites existing files
193                  *
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
198                  */
199                 copyNodes: function Model_copyNodes(dir, paths, destinationPath, onSuccess) {
200                         var len = paths.length, self = this,
201                                 copied = 0,
202                                 onCopyNodeSuccess = function (file) {
203                                         copied += 1;
204                                         self.refreshContent(file.toURI());
205                                         if (copied === len) {
206                                                 onSuccess();
207                                         }
208                                 },
209                                 onCopyNodeFailure = function () {
210                                         app.ui.alertPopup('Copying error');
211                                 },
212                                 i,
213                                 sourceName,
214                                 decision;
215
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');
220                                                 return;
221                                         }
222                                 }
223
224                                 for (i = 0; i < len; i = i + 1) {
225                                         decision = true;
226                                         sourceName = paths[i].split('/').pop();
227                                         sourceName = app.helpers.getCopyFileName(sourceName, filesList);
228
229                                         try {
230                                                 dir.copyTo(paths[i], destinationPath + '/' + sourceName, true, onCopyNodeSuccess, onCopyNodeFailure);
231                                                 self.resolveAndRefresh(paths[i]);
232                                         } catch (e) {
233                                                 console.error(e);
234                                         }
235                                 }
236                         });
237                 },
238
239                 /**
240                  * Move specified files to destination path
241                  * Overwrites existing files
242                  *
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
247                  */
248                 moveNodes: function Model_moveNodes(dir, paths, destinationPath, onSuccess) {
249                         var len = paths.length, self = this,
250                                 moved = 0,
251                                 onMoveNodeSuccess = function (file) {
252                                         self.refreshContent(file.toURI());
253                                         moved += 1;
254                                         if (moved === len) {
255                                                 onSuccess();
256                                         }
257                                 },
258                                 onMoveNodeFailure = function () {
259                                         app.ui.alertPopup('Moving error');
260                                 },
261                                 i,
262                                 sourceName,
263                                 decision;
264
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');
269                                                 return;
270                                         }
271                                 }
272
273                                 for (i = 0; i < len; i = i + 1) {
274                                         decision = true;
275                                         sourceName = paths[i].split('/').pop();
276
277                                         try {
278                                                 dir.moveTo(paths[i], destinationPath + '/' + sourceName, true, onMoveNodeSuccess, onMoveNodeFailure);
279                                                 self.resolveAndRefresh(paths[i]);
280                                         } catch (e) {
281                                                 console.error(e);
282                                         }
283                                 }
284                         });
285                 }
286         };
287 }());