2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <JSTizenException.h>
24 #include <JSTizenExceptionFactory.h>
27 #include "JSAudioLyrics.h"
28 #include "ContentConverter.h"
29 #include "AudioLyricsUtil.h"
30 #include "ContentUtility.h"
33 #define TIZEN_CONTENT_AUDIO_ATTRIBUTENAME "AudioContent"
34 #define TIZEN_CONTENT_AUDIO_ALBUM "album"
35 #define TIZEN_CONTENT_AUDIO_GENRE "genres"
36 #define TIZEN_CONTENT_AUDIO_ARTIST "artists"
37 #define TIZEN_CONTENT_AUDIO_COMPOSER "composers"
38 #define TIZEN_CONTENT_AUDIO_LYIRICS "lyrics"
39 #define TIZEN_CONTENT_AUDIO_COPYRIGHT "copyright"
40 #define TIZEN_CONTENT_AUDIO_BITRATE "bitrate"
41 #define TIZEN_CONTENT_AUDIO_TRACKNUM "trackNumber"
42 #define TIZEN_CONTENT_AUDIO_DURATION "duration"
43 #define TIZEN_CONTENT_AUDIO_SIZE "size"
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
51 JSClassDefinition JSAudio::m_classInfo =
54 kJSClassAttributeNone,
55 TIZEN_CONTENT_AUDIO_ATTRIBUTENAME,
56 JSMedia::getClassRef(),
64 NULL, //DeleteProperty,
65 NULL, //GetPropertyNames,
66 NULL, //CallAsFunction,
67 NULL, //CallAsConstructor,
72 JSStaticValue JSAudio::m_property[] =
74 { TIZEN_CONTENT_AUDIO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_CONTENT_AUDIO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_CONTENT_AUDIO_GENRE, getPropertyGenre, NULL, kJSPropertyAttributeReadOnly},
77 { TIZEN_CONTENT_AUDIO_COMPOSER , getPropertyComposer, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_CONTENT_AUDIO_COPYRIGHT, getPropertyCopyright, NULL, kJSPropertyAttributeReadOnly},
79 { TIZEN_CONTENT_AUDIO_LYIRICS, getPropertyLyrics, NULL, kJSPropertyAttributeReadOnly},
80 { TIZEN_CONTENT_AUDIO_BITRATE, getPropertyBitrate, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_CONTENT_AUDIO_TRACKNUM, getPropertyTrackNum, NULL, kJSPropertyAttributeReadOnly},
82 { TIZEN_CONTENT_AUDIO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},
83 { TIZEN_CONTENT_AUDIO_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},
87 JSClassRef JSAudio::m_jsClassRef = JSClassCreate(JSAudio::getClassInfo());
89 void JSAudio::initialize(JSContextRef context, JSObjectRef object)
91 LogDebug( "entered" );
92 AudioPrivObject *priv = static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) );
95 MediacontentAudioPtr privateData(new MediacontentAudio());
96 priv = new AudioPrivObject(context, privateData);
97 JSObjectSetPrivate(object, static_cast<void*>(priv));
98 LogDebug("new Private Object is created" );
101 LogDebug("private object already exists");
105 void JSAudio::finalize(JSObjectRef object)
107 LogDebug( "entered" );
108 AudioPrivObject *priv = static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) ) ;
113 JSObjectSetPrivate(object, NULL);
117 const JSClassRef JSAudio::getClassRef()
119 LogDebug("JSAudio::getClassRef()");
123 m_jsClassRef = JSClassCreate(&m_classInfo);
128 const JSClassDefinition* JSAudio::getClassInfo()
134 MediacontentAudioPtr JSAudio::getAudioObject(JSObjectRef object)
137 AudioPrivObject *priv = static_cast<AudioPrivObject*>(JSObjectGetPrivate(object));
140 ThrowMsg(NullPointerException, "Private object is null");
142 MediacontentAudioPtr result = priv->getObject();
145 ThrowMsg(NullPointerException, "Private object is null");
150 JSValueRef JSAudio::getPropertyAlbum(
151 JSContextRef context,
153 JSStringRef propertyName,
154 JSValueRef* exception)
159 Converter converter(context);
160 MediacontentAudioPtr audio = getAudioObject(object);
161 if(!audio->getAudioAlbum().empty()){
162 return converter.toJSValueRef(audio->getAudioAlbum());
167 LogWarning("trying to get incorrect value");
169 return JSValueMakeNull(context);
172 JSValueRef JSAudio::getPropertyArtist(
173 JSContextRef context,
175 JSStringRef propertyName,
176 JSValueRef* exception)
178 LogDebug("getPropertyArtist::entered");
181 Converter converter(context);
182 MediacontentAudioPtr audio = getAudioObject(object);
183 if(audio->getAudioArtist().size() != 0){
184 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
185 if (NULL == jsResult){
186 ThrowMsg(NullPointerException, "Could not create js array object");
188 for( unsigned int i=0; i < audio->getAudioArtist().size(); i++){
189 string artist = audio->getAudioArtist().at(i);
190 LogDebug("artist : " << artist);
191 JSValueRef val = converter.toJSValueRef(artist);
192 if(!JSSetArrayElement(context, jsResult, i, val)){
193 ThrowMsg(UnknownException, "Could not insert value into js array");
201 LogWarning("trying to get incorrect value");
203 return JSValueMakeNull(context);
208 JSValueRef JSAudio::getPropertyGenre(
209 JSContextRef context,
211 JSStringRef propertyName,
212 JSValueRef* exception)
214 LogDebug("getPropertyGenre::entered");
217 Converter converter(context);
218 MediacontentAudioPtr audio = getAudioObject(object);
219 if(audio->getAudioGenre().size() != 0){
220 JSObjectRef jsResult = JSCreateArrayObject(context, 0 , NULL);
221 if (NULL == jsResult)
223 ThrowMsg(NullPointerException, "Could not create js array object");
226 for( unsigned int i=0; i < audio->getAudioGenre().size(); i++){
227 string genre = audio->getAudioGenre().at(i);
228 JSValueRef val = converter.toJSValueRef(genre);
229 if(!JSSetArrayElement(context, jsResult, i, val)){
230 ThrowMsg(UnknownException, "Could not insert value into js array");
238 LogWarning("trying to get incorrect value");
240 return JSValueMakeNull(context);
244 JSValueRef JSAudio::getPropertyComposer(
245 JSContextRef context,
247 JSStringRef propertyName,
248 JSValueRef* exception)
250 LogDebug("getPropertyComposer::entered");
253 Converter converter(context);
254 MediacontentAudioPtr audio = getAudioObject(object);
255 if(audio->getAudioComposer().size() != 0){
256 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
257 if (NULL == jsResult){
258 ThrowMsg(NullPointerException, "Could not create js array object");
260 for( unsigned int i=0; i < audio->getAudioComposer().size(); i++){
261 string composer = audio->getAudioComposer().at(i);
262 JSValueRef val = converter.toJSValueRef(composer);
263 if(!JSSetArrayElement(context, jsResult, i, val)){
264 ThrowMsg(UnknownException, "Could not insert value into js array");
272 LogWarning("trying to get incorrect value");
274 return JSValueMakeNull(context);
277 JSValueRef JSAudio::getPropertyCopyright(
278 JSContextRef context,
280 JSStringRef propertyName,
281 JSValueRef* exception)
286 Converter converter(context);
287 MediacontentAudioPtr audio = getAudioObject(object);
288 if(!audio->getAudioCopyright().empty()){
289 return converter.toJSValueRef(audio->getAudioCopyright());
294 LogWarning("trying to get incorrect value");
296 return JSValueMakeNull(context);
299 JSValueRef JSAudio::getPropertyLyrics(
300 JSContextRef context,
302 JSStringRef propertyName,
303 JSValueRef* exception)
308 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
310 AudioPrivObject *priv = static_cast<AudioPrivObject*>(JSObjectGetPrivate(object));
313 ThrowMsg(NullPointerException, "Private object is null");
315 MediacontentAudioPtr audio = priv->getObject();
318 ThrowMsg(NullPointerException, "Private object is null");
321 JSContextRef globalContext = priv->getContext();
324 LogDebug("lyrics state : " << audio->getLyricsState());
326 if ( CONTENT_LYRIC_UNKNOWN == audio->getLyricsState() ||
327 CONTENT_LYRIC_EXISTENT == audio->getLyricsState() )
330 MediacontentLyricsPtr mediaContent;
332 if ( audio->getLyricsState() == CONTENT_LYRIC_UNKNOWN )
334 mediaContent = MediaLyricsUtilSingleton::Instance().fetchLyrics(ContentUtility::convertUriToPath(audio->getFilePath()));
336 audio->setAudioLyrics(mediaContent);
338 audio->setAudioLyricNonExistent();
342 mediaContent = audio->getAudioLyrics();
345 if (mediaContent && (audio->getLyricsState() != CONTENT_LYRIC_NON_EXISTENT))
347 LogDebug(" lyrics success ");
348 JSObjectRef jsobject;
349 jsobject = JSMediaLyrics::createJSObject(globalContext, mediaContent);
354 LogDebug(" lyrics success ");
355 audio->setAudioLyricNonExistent();
362 LogWarning("trying to get incorrect value");
365 return JSValueMakeNull(context);
368 JSValueRef JSAudio::getPropertyBitrate(
369 JSContextRef context,
371 JSStringRef propertyName,
372 JSValueRef* exception)
377 Converter converter(context);
378 MediacontentAudioPtr audio = getAudioObject(object);
379 return converter.toJSValueRef(audio->getAudioBitrate());
383 LogWarning("trying to get incorrect value");
385 return JSValueMakeNull(context);
388 JSValueRef JSAudio::getPropertyTrackNum(
389 JSContextRef context,
391 JSStringRef propertyName,
392 JSValueRef* exception)
397 Converter converter(context);
398 MediacontentAudioPtr audio = getAudioObject(object);
399 return converter.toJSValueRef(audio->getAudioTrackNum());
403 LogWarning("trying to get incorrect value");
405 return JSValueMakeNull(context);
408 JSValueRef JSAudio::getPropertyDuration(
409 JSContextRef context,
411 JSStringRef propertyName,
412 JSValueRef* exception)
417 Converter converter(context);
418 MediacontentAudioPtr audio = getAudioObject(object);
419 return converter.toJSValueRef(audio->getAudioDuration());
423 LogWarning("trying to get incorrect value");
425 return JSValueMakeNull(context);
428 JSValueRef JSAudio::getPropertySize(
429 JSContextRef context,
431 JSStringRef propertyName,
432 JSValueRef* exception)
437 Converter converter(context);
438 MediacontentAudioPtr audio = getAudioObject(object);
439 double audioSize = (double)audio->getAudioSize();
440 return converter.toJSValueRef(audioSize);
444 LogWarning("trying to get incorrect value");
446 return JSValueMakeUndefined(context);
449 bool JSAudio::setPropertyAlbum(
450 JSContextRef context,
452 JSStringRef propertyName,
454 JSValueRef* exception)
459 Converter converter(context);
461 MediacontentAudioPtr audio = getAudioObject(object);
462 string album = converter.toString(value);
463 audio->setAudioAlbum(album, true);
468 LogWarning("trying to set incorrect value");
469 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
476 bool JSAudio::setPropertyArtist(
477 JSContextRef context,
479 JSStringRef propertyName,
481 JSValueRef* exception)
486 Converter converter(context);
487 MediacontentAudioPtr audio = getAudioObject(object);
489 vector<std::string> artists;
490 if (!JSValueIsNull(context, value)) {
491 if (JSIsArrayValue(context, value))
493 JSObjectRef jsObject = converter.toJSObjectRef(value);
495 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
496 JSValueRef element = JSGetArrayElement(context, jsObject, i);
497 artists.push_back(converter.toString(element));
502 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
508 artists.push_back(converter.toString(value));
511 audio->setAudioArtist(artists, true);
516 LogWarning("trying to set incorrect value");
517 return DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
524 bool JSAudio::setPropertyComposer(
525 JSContextRef context,
527 JSStringRef propertyName,
529 JSValueRef* exception)
534 Converter converter(context);
535 MediacontentAudioPtr audio = getAudioObject(object);
537 vector<std::string> composers;
538 if (!JSValueIsNull(context, value) )
540 if (JSIsArrayValue(context, value))
542 JSObjectRef jsObject = converter.toJSObjectRef(value);
544 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
545 JSValueRef element = JSGetArrayElement(context, jsObject, i);
546 composers.push_back(converter.toString(element));
552 composers.push_back(converter.toString(value));
555 audio->setAudioComposer(composers, true);
560 LogWarning("trying to set incorrect value");
561 return DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
567 bool JSAudio::setPropertyGenre(
568 JSContextRef context,
570 JSStringRef propertyName,
572 JSValueRef* exception)
577 Converter converter(context);
578 MediacontentAudioPtr audio = getAudioObject(object);
580 vector<std::string> genres;
581 if (!JSValueIsNull(context, value) )
583 if (JSIsArrayValue(context, value))
585 JSObjectRef jsObject = converter.toJSObjectRef(value);
587 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
588 JSValueRef element = JSGetArrayElement(context, jsObject, i);
589 genres.push_back(converter.toString(element));
596 genres.push_back(converter.toString(value));
599 audio->setAudioGenre(genres, true);
604 LogWarning("trying to set incorrect value");
605 return DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
611 bool JSAudio::setPropertyTrackNumber(
612 JSContextRef context,
614 JSStringRef propertyName,
616 JSValueRef* exception)
621 Converter converter(context);
623 MediacontentAudioPtr audio = getAudioObject(object);
624 int trackNum = converter.toInt(value);
625 audio->setAudioTrackNum(trackNum, true);
630 LogWarning("trying to set incorrect value");
631 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);