fc3decd17c21dc1ece720bfacd676426a03c465a
[platform/core/api/webapi-plugins.git] / src / bookmark / bookmark_api.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 var validator_ = xwalk.utils.validator;
18 var privilege_ = xwalk.utils.privilege;
19 var types_ = validator_.Types;
20 var native_ = new xwalk.utils.NativeManager(extension);
21
22 function EditManager() {
23   this.canEdit = false;
24 }
25
26 EditManager.prototype.allow = function() {
27   this.canEdit = true;
28 };
29
30 EditManager.prototype.disallow = function() {
31   this.canEdit = false;
32 };
33
34 var _edit = new EditManager();
35
36 function BookmarkManager() {}
37
38 BookmarkManager.prototype.get = function() {
39   xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_READ);
40
41   var args = validator_.validateArgs(arguments, [
42     {
43       name: 'parentFolder',
44       type: types_.PLATFORM_OBJECT,
45       values: [tizen.BookmarkFolder, tizen.BookmarkItem],
46       optional: true,
47       nullable: true
48     },
49     {
50       name: 'recursive',
51       type: types_.BOOLEAN,
52       optional: true,
53       nullable: true
54     }
55   ]);
56   var result;
57
58   if (arguments.length === 0 || args.parentFolder === null) {
59     result = provider.getFolderItems(provider.getRootId(), args.recursive);
60     if (!result)
61       throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
62     return result;
63   }
64   if (args.parentFolder.id === null || args.parentFolder.id === 0)
65     throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
66
67   result = provider.getFolderItems(args.parentFolder.id, args.recursive);
68   if (!result)
69     throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
70   return result;
71 };
72
73 BookmarkManager.prototype.add = function() {
74   xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_WRITE);
75
76   var args = validator_.validateArgs(arguments, [
77     {
78       name: 'bookmark',
79       type: types_.PLATFORM_OBJECT,
80       values: [tizen.BookmarkFolder, tizen.BookmarkItem],
81       optional: false,
82       nullable: false
83     },
84     {
85       name: 'parentFolder',
86       type: types_.PLATFORM_OBJECT,
87       values: tizen.BookmarkFolder,
88       optional: true,
89       nullable: true
90     }
91   ]);
92   if (arguments.length == 1 || args.parentFolder === null) {
93     if (args.bookmark.id) {
94       throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
95     }
96     if (!provider.addToFolder(args.bookmark, provider.getRootId())) {
97       throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
98     }
99     return;
100   }
101   if (!args.parentFolder.id) {
102     throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
103   }
104   if (!provider.addToFolder(args.bookmark, args.parentFolder.id)) {
105     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
106   }
107 };
108
109 BookmarkManager.prototype.remove = function() {
110   xwalk.utils.checkPrivilegeAccess(privilege_.BOOKMARK_WRITE);
111
112   var args = validator_.validateArgs(arguments, [
113     {
114       name: 'bookmark',
115       type: types_.PLATFORM_OBJECT,
116       values: [tizen.BookmarkFolder, tizen.BookmarkItem],
117       optional: true,
118       nullable: true
119     }
120   ]);
121
122   if (!arguments.length || args.bookmark === null) {
123     if (native_.isFailure(native_.callSync('Bookmark_removeAll')))
124       throw new WebAPIException(WebAPIException.SECURITY_ERR);
125     return;
126   }
127   if (!args.bookmark.id)
128     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
129   if (native_.isFailure(native_.callSync('Bookmark_remove', {
130     id: args.bookmark.id})))
131     throw new WebAPIException(WebAPIException.SECURITY_ERR);
132
133   _edit.allow();
134   args.bookmark.id = null;
135   args.bookmark.parent = undefined;
136   _edit.disallow();
137 };
138
139 function BookmarkProvider() {}
140
141 BookmarkProvider.prototype.addToFolder = function() {
142   var args = validator_.validateArgs(arguments, [
143     {
144       name: 'bookmark',
145       type: types_.PLATFORM_OBJECT,
146       values: [tizen.BookmarkFolder, tizen.BookmarkItem],
147       optional: true,
148       nullable: true
149     },
150     {
151       name: 'parentId',
152       type: types_.DOUBLE,
153       optional: false,
154       nullable: false}
155   ]);
156   var ret = native_.callSync('Bookmark_add',
157     {
158       title: args.bookmark.title,
159       url: String(args.bookmark.url),
160       parentId: args.parentId,
161       type: args.bookmark instanceof tizen.BookmarkFolder ? 1 : 0
162     }
163   );
164   if (native_.isFailure(ret)) {
165     return false;
166   }
167   var ret_id = native_.getResultObject(ret);
168   _edit.allow();
169   args.bookmark.id = ret_id;
170   args.bookmark.parent = this.getFolder(args.parentId);
171   _edit.disallow();
172   return true;
173 };
174
175 BookmarkProvider.prototype.getFolder = function() {
176   var args = validator_.validateArgs(arguments, [
177     {
178       name: 'id',
179       type: types_.DOUBLE,
180       optional: false,
181       nullable: false
182     }
183   ]);
184   if (arguments.length === 0 || args.id <= 0)
185     return null;
186   if (args.id == this.getRootId())
187     return null;
188
189   var ret = native_.callSync('Bookmark_get', {
190     id: args.id,
191     shouldGetItems: 0
192   });
193
194   if (native_.isFailure(ret)) {
195     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
196   }
197
198   var folder = native_.getResultObject(ret);
199   if (folder === undefined || folder === null)
200     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
201
202   var obj = new tizen.BookmarkFolder(folder[0].title);
203   obj.id = folder[0].id;
204   obj.parent = this.getFolder(folder[0].parentId);
205   return obj;
206 };
207
208 BookmarkProvider.prototype.getFolderItems = function() {
209   var args = validator_.validateArgs(arguments, [
210     {
211       name: 'id',
212       type: types_.DOUBLE,
213       optional: false,
214       nullable: false
215     },
216     {
217       name: 'recursive',
218       type: types_.BOOLEAN,
219       optional: true,
220       nullable: true
221     }
222   ]);
223
224   var ret = native_.callSync('Bookmark_get', {
225     id: Number(args.id),
226     shouldGetItems: 1
227   });
228
229   if (native_.isFailure(ret)) {
230     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
231   }
232
233   var folder = native_.getResultObject(ret);
234   if (folder === undefined)
235     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
236
237   var item;
238   var obj;
239   var result = [];
240   var len = folder.length;
241
242   for (var i = 0; item = folder[i], i < len; i++) {
243     if (Number(item.type) === 0)
244       obj = new tizen.BookmarkItem(item.title, item.url);
245     else
246       obj = new tizen.BookmarkFolder(item.title);
247
248     _edit.allow();
249     obj.id = item.id;
250     obj.parent = this.getFolder(item.parentId);
251     _edit.disallow();
252     result.push(obj);
253
254     if (args.recursive && Number(item.type) !== 0)
255       result = result.concat(this.getFolderItems(item.id, true));
256   }
257   return result;
258 };
259
260 BookmarkProvider.prototype.getRootId = function() {
261   var ret = native_.callSync('Bookmark_getRootId');
262   if (native_.isFailure(ret)) {
263     throw native_.getErrorObject(ret);
264   }
265   var rootId = native_.getResultObject(ret);
266   return Number(rootId);
267 };
268
269 var provider = new BookmarkProvider();
270
271 tizen.BookmarkItem = function() {
272   validator_.isConstructorCall(this, tizen.BookmarkItem);
273   var args = validator_.validateArgs(arguments, [
274     {
275       name: 'title',
276       type: types_.STRING,
277       optional: false
278     },
279     {
280       name: 'url',
281       type: types_.STRING,
282       optional: false
283     }
284   ]);
285   var parent_;
286   var id_ = null;
287
288   Object.defineProperties(this, {
289     parent: {
290       get: function() {
291         return parent_;
292       },
293       set: function(new_parent) {
294         if (_edit.canEdit)
295           parent_ = new_parent;
296       },
297       enumerable: true,
298       nullable: true
299     },
300     title: {
301       get: function() {
302         return args.title;
303       },
304       enumerable: true,
305       nullable: false
306     },
307     url: {
308       get: function() {
309         if (args.url === "undefined")
310           args.url = undefined;
311         return args.url;
312       },
313       enumerable: true,
314       nullable: false
315     },
316     id: {
317       get: function() {
318         return id_;
319       },
320       set: function(new_id) {
321         if (_edit.canEdit)
322           id_ = new_id;
323       },
324       enumerable: false,
325       nullable: true
326     }
327   });
328 };
329
330 tizen.BookmarkFolder = function() {
331   validator_.isConstructorCall(this, tizen.BookmarkFolder);
332   var args = validator_.validateArgs(arguments, [
333     {
334       name: 'title',
335       type: types_.STRING,
336       optional: false,
337       nullable: false
338     }
339   ]);
340
341   var parent_;
342   var id_ = null;
343
344   Object.defineProperties(this, {
345     parent: {
346       get: function() {
347         return parent_;
348       },
349       set: function(new_parent) {
350         if (_edit.canEdit)
351           parent_ = new_parent;
352       },
353       enumerable: true,
354       nullable: true
355     },
356     title: {
357       get: function() {
358         return args.title;
359       },
360       enumerable: true,
361       nullable: false
362     },
363     id: {
364       get: function() {
365         return id_;
366       },
367       set: function(new_id) {
368         if (_edit.canEdit)
369           id_ = new_id;
370       },
371       enumerable: false,
372       nullable: true
373     }
374   });
375 };
376 exports = new BookmarkManager();