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