1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 function getNextReplyId() {
12 function postMessage(msg, callback) {
13 var replyId = getNextReplyId();
14 _callbacks[replyId] = callback;
15 msg.replyId = replyId;
16 extension.postMessage(JSON.stringify(msg));
19 function sendSyncMessage(msg) {
20 return extension.internal.sendSyncMessage(JSON.stringify(msg));
23 extension.setMessageListener(function(msg) {
24 var m = JSON.parse(msg);
25 var replyId = m.replyId;
26 var callback = _callbacks[replyId];
28 if (typeof(callback) === 'function') {
31 delete _callbacks[replyId];
33 console.log('Invalid replyId from Tizen Content API: ' + replyId);
37 function Folder(uri, id, type, title) {
38 Object.defineProperties(this, {
39 'directoryURI': { writable: false, value: uri, enumerable: true },
40 'id': { writable: false, value: id, enumerable: true },
41 'storageType': { writable: false, value: type, enumerable: true },
42 'title': { writable: false, value: title, enumerable: true }
46 function Content(id, name, type, mimeType, title, contentURI, thumnailURIs, releaseDate,
47 modifiedDate, size, description, rating) {
48 Object.defineProperties(this, {
49 'id': { writable: false, value: id, enumerable: true },
50 'name': { writable: false, value: name, enumerable: true },
51 'type': { writable: false, value: type, enumerable: true },
52 'mimeType': { writable: false, value: mimeType, enumerable: true },
53 'title': { writable: false, value: title, enumerable: true },
54 'contentURI': { writable: false, value: contentURI, enumerable: true },
55 'thumnailURIs': { writable: false, value: thumnailURIs, enumerable: true },
56 'releaseDate': { writable: false, value: releaseDate, enumerable: true },
57 'modifiedDate': { writable: false, value: modifiedDate, enumerable: true },
58 'size': { writable: false, value: size, enumerable: true },
59 'description': { writable: false, value: description, enumerable: true },
60 'rating': { writable: false, value: rating, enumerable: true }
64 function ContentAudio(obj, album, genres, artists, composer, copyright,
65 bitrate, trackNumber, duration) {
66 Object.defineProperties(obj, {
67 'album': { writable: false, value: album, enumerable: true },
68 'genres': { writable: false, value: genres, enumerable: true },
69 'artists': { writable: false, value: artists, enumerable: true },
70 'composer': { writable: false, value: composer, enumerable: true },
71 'copyright': { writable: false, value: copyright, enumerable: true },
72 'bitrate': { writable: false, value: bitrate, enumerable: true },
73 'trackNumber': { writable: false, value: trackNumber, enumerable: true },
74 'duration': { writable: false, value: duration, enumerable: true }
78 function ContentImage(obj, width, height, orientation) {
79 Object.defineProperties(obj, {
80 'width': { writable: false, value: width, enumerable: true },
81 'height': { writable: false, value: height, enumerable: true },
82 'orientation': { writable: false, value: orientation, enumerable: true }
86 function ContentVideo(obj, album, artists, duration, width, height) {
87 Object.defineProperties(obj, {
88 'album': { writable: false, value: album, enumerable: true },
89 'artists': { writable: false, value: artists, enumerable: true },
90 'duration': { writable: false, value: duration, enumerable: true },
91 'width': { writable: false, value: width, enumerable: true },
92 'height': { writable: false, value: height, enumerable: true }
96 function ContentManager() {
99 ContentManager.prototype.update = function(content) {
100 if (!xwalk.utils.validateArguments('o', arguments)) {
101 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
105 ContentManager.prototype.updateBatch = function(content, onsuccess, onerror) {
106 if (!xwalk.utils.validateArguments('o?ff', arguments)) {
107 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
111 ContentManager.prototype.getDirectories = function(onsuccess, onerror) {
112 if (!xwalk.utils.validateArguments('f?f', arguments)) {
113 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
117 cmd: 'ContentManager.getDirectories'
118 }, function(result) {
119 if (result.isError) {
121 onerror(new tizen.WebAPIError(result.errorCode));
122 } else if (onsuccess) {
125 for (var i = 0; i < result.value.length; i++) {
126 var folder = result.value[i];
127 var jsonFolder = new Folder(folder.directoryURI,
131 folders.push(jsonFolder);
138 ContentManager.prototype.find = function(onsuccess, onerror, directoryId,
139 filter, sortMode, count, offset) {
140 if (!xwalk.utils.validateArguments('f?fsoonn', arguments)) {
141 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
145 cmd: 'ContentManager.find',
146 directoryId: directoryId,
151 }, function(result) {
152 if (result.isError) {
154 onerror(new tizen.WebAPIError(result.errorCode));
155 } else if (onsuccess) {
157 for (var i = 0; i < result.value.length; i++) {
158 var content = result.value[i];
159 var jsonContent = new Content(content.id,
165 content.thumnailURIs,
167 content.modifiedDate,
172 if (content.type == 'AUDIO') {
173 ContentAudio(jsonContent,
182 } else if (content.type == 'IMAGE') {
183 ContentImage(jsonContent,
186 content.orientation);
187 } else if (content.type == 'VIDEO') {
188 ContentImage(jsonContent,
195 contents.push(jsonContent);
202 ContentManager.prototype.scanFile = function(contentURI, onsuccess, onerror) {
203 if (!xwalk.utils.validateArguments('s?ff', arguments)) {
204 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
207 cmd: 'ContentManager.scanFile',
208 contentURI: contentURI
209 }, function(result) {
210 if (result.isError) {
212 onerror(new tizen.WebAPIError(result.errorCode));
213 } else if (onsuccess) {
214 onsuccess(contentURI);
219 ContentManager.prototype.setChangeListener = function(onchange) {
220 if (!xwalk.utils.validateArguments('f', arguments)) {
221 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
225 ContentManager.prototype.unsetChangeLIstener = function() {
228 exports = new ContentManager();