2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 var T_ = xwalk.utils.type;
18 var privUtils_ = xwalk.utils;
19 var CONTENT_MANAGER_LISTENER_ID = 'ContentManagerChangeCallback';
21 function _ContentManagerChangeCallback(result) {
22 if (result.state === 'oncontentadded' || result.state === 'oncontentupdated') {
23 var content = native_.getResultObject(result);
24 native_.callIfPossible(this[result.state], createContentObject_(content));
26 result.state === 'oncontentdiradded' ||
27 result.state === 'oncontentdirupdated'
29 var contentDir = native_.getResultObject(result);
30 native_.callIfPossible(this[result.state], createContentDirObject_(contentDir));
32 result.state === 'oncontentremoved' ||
33 result.state === 'oncontentdirremoved'
35 native_.callIfPossible(this[result.state], native_.getResultObject(result));
39 var ContentListenersManager = (function() {
40 function changeContent(event) {
41 if (T_.isEmptyObject(this.listeners)) {
47 if (event.state === 'oncontentadded' || event.state === 'oncontentupdated') {
48 result = createContentObject_(native_.getResultObject(event));
50 event.state === 'oncontentdiradded' ||
51 event.state === 'oncontentdirupdated'
53 result = createContentDirObject_(native_.getResultObject(event));
55 event.state === 'oncontentremoved' ||
56 event.state === 'oncontentdirremoved'
58 result = native_.getResultObject(event);
66 for (var listenerId in this.listeners) {
67 if (this.listeners.hasOwnProperty(listenerId)) {
68 callback = this.listeners[listenerId];
69 if (T_.isFunction(callback[event.state])) {
70 callback[event.state](result);
76 function _ContentListenersManager() {
78 this.lastListenerId = 0;
79 this.changeContent = changeContent.bind(this);
82 _ContentListenersManager.prototype.addChangeListener = function(changeCallback) {
83 if (T_.isEmptyObject(this.listeners)) {
84 var result = native_.callSync('ContentManagerAddChangeListener');
85 if (native_.isFailure(result)) {
86 throw native_.getErrorObject(result);
89 native_.addListener(CONTENT_MANAGER_LISTENER_ID, this.changeContent);
92 this.listeners[++this.lastListenerId] = changeCallback;
93 return this.lastListenerId;
96 _ContentListenersManager.prototype.removeChangeListener = function(listenerId) {
97 delete this.listeners[listenerId];
99 if (T_.isEmptyObject(this.listeners)) {
100 var result = native_.callSync('ContentManagerRemoveChangeListener');
101 if (native_.isFailure(result)) {
102 throw native_.getErrorObject(result);
105 native_.removeListener(CONTENT_MANAGER_LISTENER_ID);
109 return _ContentListenersManager;
112 var listenersManager = new ContentListenersManager();
114 function ContentManager() {}
116 var ContentManagerUpdate = function(content) {
117 var args = validator_.validateArgs(arguments, [
118 { name: 'content', type: types_.PLATFORM_OBJECT, values: Content }
122 content: args.content
125 var result = native_.callSync('ContentManagerUpdate', data);
127 if (native_.isFailure(result)) {
128 throw native_.getErrorObject(result);
132 ContentManager.prototype.update = function() {
133 ContentManagerUpdate.apply(this, arguments);
136 ContentManager.prototype.updateBatch = function(
141 var args = validator_.validateArgs(arguments, [
142 { name: 'contents', type: types_.ARRAY, values: Content },
144 name: 'successCallback',
145 type: types_.FUNCTION,
149 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
153 contents: args.contents
156 var callback = function(result) {
157 if (native_.isFailure(result)) {
158 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
161 native_.callIfPossible(args.successCallback);
164 var result = native_.call('ContentManagerUpdatebatch', data, callback);
166 if (native_.isFailure(result)) {
167 throw native_.getErrorObject(result);
171 ContentManager.prototype.getDirectories = function(successCallback, errorCallback) {
172 var args = validator_.validateArgs(arguments, [
173 { name: 'successCallback', type: types_.FUNCTION },
174 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
177 var callback = function(result) {
178 if (native_.isFailure(result)) {
179 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
184 result = native_.getResultObject(result);
185 for (var i = 0, max = result.length; i < max; i++) {
186 out.push(new ContentDirectory(result[i]));
188 native_.callIfPossible(args.successCallback, out);
191 var result = native_.call('ContentManagerGetdirectories', null, callback);
193 if (native_.isFailure(result)) {
194 throw native_.getErrorObject(result);
198 ContentManager.prototype.find = function(
207 var args = validator_.validateArgs(arguments, [
208 { name: 'successCallback', type: types_.FUNCTION },
209 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true },
210 { name: 'directoryId', type: types_.STRING, optional: true, nullable: true },
213 type: types_.PLATFORM_OBJECT,
215 tizen.AttributeFilter,
216 tizen.AttributeRangeFilter,
217 tizen.CompositeFilter
224 type: types_.PLATFORM_OBJECT,
225 values: tizen.SortMode,
229 { name: 'count', type: types_.UNSIGNED_LONG, optional: true, nullable: true },
230 { name: 'offset', type: types_.UNSIGNED_LONG, optional: true, nullable: true }
234 directoryId: args.directoryId,
235 filter: utils_.repackFilter(args.filter),
236 sortMode: args.sortMode,
241 var callback = function(result) {
242 if (native_.isFailure(result)) {
243 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
248 result = native_.getResultObject(result);
250 for (var i = 0, max = result.length; i < max; i++) {
251 out.push(createContentObject_(result[i]));
254 native_.callIfPossible(args.errorCallback, e);
258 native_.callIfPossible(args.successCallback, out);
261 var result = native_.call('ContentManagerFind', data, callback);
263 if (native_.isFailure(result)) {
264 throw native_.getErrorObject(result);
268 ContentManager.prototype.scanFile = function(contentURI, successCallback, errorCallback) {
269 var args = validator_.validateArgs(arguments, [
270 { name: 'contentURI', type: types_.STRING },
272 name: 'successCallback',
273 type: types_.FUNCTION,
277 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
280 var path = args.contentURI.trim();
282 throw new WebAPIException(
283 WebAPIException.INVALID_VALUES_ERR,
284 'File path is not valid.'
289 contentURI: convertUriToPath_(path)
292 var callback = function(result) {
293 if (native_.isFailure(result)) {
294 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
297 native_.callIfPossible(args.successCallback, args.contentURI);
300 var result = native_.call('ContentManagerScanfile', data, callback);
302 if (native_.isFailure(result)) {
303 throw native_.getErrorObject(result);
307 ContentManager.prototype.scanDirectory = function(
313 var args = validator_.validateArgs(arguments, [
314 { name: 'contentDirURI', type: types_.STRING },
315 { name: 'recursive', type: types_.BOOLEAN },
317 name: 'successCallback',
318 type: types_.FUNCTION,
322 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
325 var path = args.contentDirURI.trim();
327 throw new WebAPIException(
328 WebAPIException.INVALID_VALUES_ERR,
329 'Directory path is not valid.'
334 contentDirURI: convertUriToPath_(path),
335 recursive: args.recursive
338 var callback = function(result) {
339 if (native_.isFailure(result)) {
340 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
343 native_.callIfPossible(args.successCallback, args.contentDirURI);
346 var result = native_.call('ContentManagerScanDirectory', data, callback);
348 if (native_.isFailure(result)) {
349 throw native_.getErrorObject(result);
353 ContentManager.prototype.cancelScanDirectory = function(contentDirURI) {
354 var args = validator_.validateArgs(arguments, [
355 { name: 'contentDirURI', type: types_.STRING }
358 var path = args.contentDirURI.trim();
360 throw new WebAPIException(
361 WebAPIException.INVALID_VALUES_ERR,
362 'Directory path is not valid.'
367 contentDirURI: convertUriToPath_(path)
370 var result = native_.callSync('ContentManagerCancelScanDirectory', data);
372 if (native_.isFailure(result)) {
373 throw native_.getErrorObject(result);
377 ContentManager.prototype.addChangeListener = function() {
378 var args = validator_.validateArgs(arguments, [
380 name: 'changeCallback',
381 type: types_.LISTENER,
387 'oncontentdirupdated',
388 'oncontentdirremoved'
393 return listenersManager.addChangeListener(args.changeCallback);
396 ContentManager.prototype.removeChangeListener = function() {
397 var args = validator_.validateArgs(arguments, [
404 listenersManager.removeChangeListener(args.listenerId);
407 ContentManager.prototype.setChangeListener = function(changeCallback) {
408 privUtils_.deprecationWarn(
409 'setChangeListener() is deprecated and will be removed ' +
410 'from next release. Use addChangeListener() instead.',
414 var args = validator_.validateArgs(arguments, [
416 name: 'changeCallback',
417 type: types_.LISTENER,
423 'oncontentdirupdated',
424 'oncontentdirremoved'
429 var listenerId = 'ContentManagerChangeCallback_';
432 listenerId: listenerId
436 oncontentadded: args.changeCallback.oncontentadded,
437 oncontentupdated: args.changeCallback.oncontentupdated,
438 oncontentremoved: args.changeCallback.oncontentremoved,
439 oncontentdiradded: args.changeCallback.oncontentdiradded,
440 oncontentdirupdated: args.changeCallback.oncontentdirupdated,
441 oncontentdirremoved: args.changeCallback.oncontentdirremoved
444 var result = native_.callSync('ContentManagerSetchangelistener', data);
446 if (native_.isFailure(result)) {
447 throw native_.getErrorObject(result);
451 'ContentManagerChangeCallback_',
452 _ContentManagerChangeCallback.bind(callbacks)
456 ContentManager.prototype.unsetChangeListener = function() {
457 privUtils_.deprecationWarn(
458 'unsetChangeListener() is deprecated and will be removed ' +
459 'from next release. Use removeChangeListener() instead.',
465 var result = native_.callSync('ContentManagerUnsetchangelistener', data);
467 if (native_.isFailure(result)) {
468 throw native_.getErrorObject(result);
471 native_.removeListener('ContentManagerChangeCallback_');
474 ContentManager.prototype.getPlaylists = function(successCallback, errorCallback) {
475 var args = validator_.validateArgs(arguments, [
476 { name: 'successCallback', type: types_.FUNCTION },
477 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
482 var callback = function(result) {
483 if (native_.isFailure(result)) {
484 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
488 result = native_.getResultObject(result);
489 for (var i = 0, max = result.length; i < max; i++) {
490 out.push(new Playlist(result[i]));
492 native_.callIfPossible(args.successCallback, out);
495 var result = native_.call('ContentManagerGetplaylists', data, callback);
497 if (native_.isFailure(result)) {
498 throw native_.getErrorObject(result);
502 ContentManager.prototype.createPlaylist = function(
508 var args = validator_.validateArgs(arguments, [
509 { name: 'name', type: types_.STRING },
510 { name: 'successCallback', type: types_.FUNCTION },
511 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true },
513 name: 'sourcePlaylist',
514 type: types_.PLATFORM_OBJECT,
523 !type_.isString(arguments[0]) ||
524 (type_.isString(arguments[0]) && !arguments[0].length)
526 setTimeout(function() {
527 args.errorCallback(new WebAPIException(WebAPIException.INVALID_VALUES_ERR));
534 sourcePlaylist: args.sourcePlaylist
537 var callback = function(result) {
538 if (native_.isFailure(result)) {
539 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
542 native_.callIfPossible(
543 args.successCallback,
544 new Playlist(native_.getResultObject(result))
548 var result = native_.call('ContentManagerCreateplaylist', data, callback);
550 if (native_.isFailure(result)) {
551 throw native_.getErrorObject(result);
555 ContentManager.prototype.removePlaylist = function(id, successCallback, errorCallback) {
556 var args = validator_.validateArgs(arguments, [
557 { name: 'id', type: types_.STRING },
559 name: 'successCallback',
560 type: types_.FUNCTION,
564 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
571 var callback = function(result) {
572 if (native_.isFailure(result)) {
573 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
576 native_.callIfPossible(args.successCallback);
579 var result = native_.call('ContentManagerRemoveplaylist', data, callback);
581 if (native_.isFailure(result)) {
582 throw native_.getErrorObject(result);
586 ContentManager.prototype.createThumbnail = function(
591 var args = validator_.validateArgs(arguments, [
592 { name: 'content', type: types_.PLATFORM_OBJECT, values: Content },
593 { name: 'successCallback', type: types_.FUNCTION },
594 { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true }
601 var callback = function(result) {
602 if (native_.isFailure(result)) {
603 native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
606 args.successCallback(native_.getResultObject(result));
609 var result = native_.call('ContentManagerCreateThumbnail', data, callback);
611 if (native_.isFailure(result)) {
612 throw native_.getErrorObject(result);
616 exports = new ContentManager();