diff options
Diffstat (limited to 'mobile_src/Content/JSAudio.cpp')
-rwxr-xr-x | mobile_src/Content/JSAudio.cpp | 609 |
1 files changed, 609 insertions, 0 deletions
diff --git a/mobile_src/Content/JSAudio.cpp b/mobile_src/Content/JSAudio.cpp new file mode 100755 index 0000000..b63be9b --- /dev/null +++ b/mobile_src/Content/JSAudio.cpp @@ -0,0 +1,609 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <CommonsJavaScript/PrivateObject.h> +#include <CommonsJavaScript/Converter.h> +#include <CommonsJavaScript/JSUtils.h> +#include <JSWebAPIErrorFactory.h> + +#include "JSAudio.h" +#include "JSAudioLyrics.h" +#include "ContentConverter.h" +#include "AudioLyricsUtil.h" +#include "ContentUtility.h" +#include <Logger.h> + +#define TIZEN_CONTENT_AUDIO_ATTRIBUTENAME "AudioContent" +#define TIZEN_CONTENT_AUDIO_ALBUM "album" +#define TIZEN_CONTENT_AUDIO_GENRE "genres" +#define TIZEN_CONTENT_AUDIO_ARTIST "artists" +#define TIZEN_CONTENT_AUDIO_COMPOSER "composers" +#define TIZEN_CONTENT_AUDIO_LYIRICS "lyrics" +#define TIZEN_CONTENT_AUDIO_COPYRIGHT "copyright" +#define TIZEN_CONTENT_AUDIO_BITRATE "bitrate" +#define TIZEN_CONTENT_AUDIO_TRACKNUM "trackNumber" +#define TIZEN_CONTENT_AUDIO_DURATION "duration" +#define TIZEN_CONTENT_AUDIO_SIZE "size" + +using namespace WrtDeviceApis::Commons; +using namespace WrtDeviceApis::CommonsJavaScript; + +namespace DeviceAPI { +namespace Content { + +JSClassDefinition JSAudio::m_classInfo = +{ + 0, + kJSClassAttributeNone, + TIZEN_CONTENT_AUDIO_ATTRIBUTENAME, + JSMedia::getClassRef(), + m_property, + NULL, // m_function, + initialize, + finalize, + NULL, //hasProperty, + NULL, //getProperty, + NULL, //setProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSAudio::m_property[] = +{ + { TIZEN_CONTENT_AUDIO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_GENRE, getPropertyGenre, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_COMPOSER , getPropertyComposer, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_COPYRIGHT, getPropertyCopyright, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_LYIRICS, getPropertyLyrics, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_BITRATE, getPropertyBitrate, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_TRACKNUM, getPropertyTrackNum, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly}, + { TIZEN_CONTENT_AUDIO_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly}, + { 0, 0, 0, 0 } +}; + +JSClassRef JSAudio::m_jsClassRef = JSClassCreate(JSAudio::getClassInfo()); + +void JSAudio::initialize(JSContextRef context, JSObjectRef object) +{ + AudioPrivObject *priv = static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) ); + if (!priv) + { + MediacontentAudioPtr privateData(new MediacontentAudio()); + priv = new AudioPrivObject(context, privateData); + JSObjectSetPrivate(object, static_cast<void*>(priv)); + LoggerD("new Private Object is created" ); + } + else { + LoggerD("private object already exists"); + } +} + +void JSAudio::finalize(JSObjectRef object) +{ + AudioPrivObject *priv = static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) ) ; + if (priv != NULL) + { + delete (priv); + priv = NULL; + JSObjectSetPrivate(object, NULL); + } +} + +const JSClassRef JSAudio::getClassRef() +{ + if (!m_jsClassRef) + { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSAudio::getClassInfo() +{ + return &m_classInfo; +} + + +MediacontentAudioPtr JSAudio::getAudioObject(JSObjectRef object) +{ + AudioPrivObject *priv = static_cast<AudioPrivObject*>(JSObjectGetPrivate(object)); + if(!priv) + { + ThrowMsg(NullPointerException, "Private object is null"); + } + MediacontentAudioPtr result = priv->getObject(); + if (!result) + { + ThrowMsg(NullPointerException, "Private object is null"); + } + return result; +} + +JSValueRef JSAudio::getPropertyAlbum( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + if(!audio->getAudioAlbum().empty()){ + return converter.toJSValueRef(audio->getAudioAlbum()); + } + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyArtist( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + if(audio->getAudioArtist().size() != 0){ + JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL); + if (NULL == jsResult){ + ThrowMsg(NullPointerException, "Could not create js array object"); + } + for( unsigned int i=0; i < audio->getAudioArtist().size(); i++){ + string artist = audio->getAudioArtist().at(i); + JSValueRef val = converter.toJSValueRef(artist); + if(!JSSetArrayElement(context, jsResult, i, val)){ + ThrowMsg(UnknownException, "Could not insert value into js array"); + } + } + return jsResult; + } + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + + + +JSValueRef JSAudio::getPropertyGenre( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + if(audio->getAudioGenre().size() != 0){ + JSObjectRef jsResult = JSCreateArrayObject(context, 0 , NULL); + if (NULL == jsResult) + { + ThrowMsg(NullPointerException, "Could not create js array object"); + } + + for( unsigned int i=0; i < audio->getAudioGenre().size(); i++){ + string genre = audio->getAudioGenre().at(i); + JSValueRef val = converter.toJSValueRef(genre); + if(!JSSetArrayElement(context, jsResult, i, val)){ + ThrowMsg(UnknownException, "Could not insert value into js array"); + } + } + return jsResult; + } + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + + +JSValueRef JSAudio::getPropertyComposer( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + if(audio->getAudioComposer().size() != 0){ + JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL); + if (NULL == jsResult){ + ThrowMsg(NullPointerException, "Could not create js array object"); + } + for( unsigned int i=0; i < audio->getAudioComposer().size(); i++){ + string composer = audio->getAudioComposer().at(i); + JSValueRef val = converter.toJSValueRef(composer); + if(!JSSetArrayElement(context, jsResult, i, val)){ + ThrowMsg(UnknownException, "Could not insert value into js array"); + } + } + return jsResult; + } + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyCopyright( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + if(!audio->getAudioCopyright().empty()){ + return converter.toJSValueRef(audio->getAudioCopyright()); + } + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyLyrics( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context); + + AudioPrivObject *priv = static_cast<AudioPrivObject*>(JSObjectGetPrivate(object)); + if(!priv) + { + ThrowMsg(NullPointerException, "Private object is null"); + } + MediacontentAudioPtr audio = priv->getObject(); + if (!audio) + { + ThrowMsg(NullPointerException, "Private object is null"); + } + + JSContextRef globalContext = priv->getContext(); + + if ( CONTENT_LYRIC_UNKNOWN == audio->getLyricsState() || + CONTENT_LYRIC_EXISTENT == audio->getLyricsState() ) + { + + MediacontentLyricsPtr mediaContent; + + if ( audio->getLyricsState() == CONTENT_LYRIC_UNKNOWN ) + { + mediaContent = MediaLyricsUtilSingleton::Instance().fetchLyrics(ContentUtility::convertUriToPath(audio->getFilePath())); + if ( mediaContent ) + audio->setAudioLyrics(mediaContent); + else + audio->setAudioLyricNonExistent(); + } + else + { + mediaContent = audio->getAudioLyrics(); + } + + if (mediaContent && (audio->getLyricsState() != CONTENT_LYRIC_NON_EXISTENT)) + { + JSObjectRef jsobject; + jsobject = JSMediaLyrics::createJSObject(globalContext, mediaContent); + return jsobject; + } + else + { + audio->setAudioLyricNonExistent(); + } + } + + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyBitrate( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + return converter.toJSValueRef(audio->getAudioBitrate()); + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyTrackNum( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + return converter.toJSValueRef(audio->getAudioTrackNum()); + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertyDuration( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + return converter.toJSValueRef(audio->getAudioDuration()); + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeNull(context); +} + +JSValueRef JSAudio::getPropertySize( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + double audioSize = (double)audio->getAudioSize(); + return converter.toJSValueRef(audioSize); + } + Catch(Exception) + { + LoggerW("trying to get incorrect value"); + } + return JSValueMakeUndefined(context); +} + +bool JSAudio::setPropertyAlbum( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + + MediacontentAudioPtr audio = getAudioObject(object); + string album = converter.toString(value); + audio->setAudioAlbum(album, true); + return true; + } + Catch(Exception) + { + LoggerW("trying to set incorrect value"); + DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + } + + return false; + +} + +bool JSAudio::setPropertyArtist( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + + vector<std::string> artists; + if (!JSValueIsNull(context, value)) { + if (JSIsArrayValue(context, value)) + { + JSObjectRef jsObject = converter.toJSObjectRef(value); + + for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) { + JSValueRef element = JSGetArrayElement(context, jsObject, i); + artists.push_back(converter.toString(element)); + } + } + else + { + DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + return false; + } + } + else + { + artists.push_back(converter.toString(value)); + } + + audio->setAudioArtist(artists, true); + return true; + } + Catch(Exception) + { + LoggerW("trying to set incorrect value"); + return DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + } + + return false; +} + + +bool JSAudio::setPropertyComposer( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + + vector<std::string> composers; + if (!JSValueIsNull(context, value) ) + { + if (JSIsArrayValue(context, value)) + { + JSObjectRef jsObject = converter.toJSObjectRef(value); + + for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) { + JSValueRef element = JSGetArrayElement(context, jsObject, i); + composers.push_back(converter.toString(element)); + } + } + } + else + { + composers.push_back(converter.toString(value)); + } + + audio->setAudioComposer(composers, true); + return true; + } + Catch(Exception) + { + return DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + } + + return false; +} + +bool JSAudio::setPropertyGenre( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + MediacontentAudioPtr audio = getAudioObject(object); + + vector<std::string> genres; + if (!JSValueIsNull(context, value) ) + { + if (JSIsArrayValue(context, value)) + { + JSObjectRef jsObject = converter.toJSObjectRef(value); + + for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) { + JSValueRef element = JSGetArrayElement(context, jsObject, i); + genres.push_back(converter.toString(element)); + } + + } + } + else + { + genres.push_back(converter.toString(value)); + } + + audio->setAudioGenre(genres, true); + return true; + } + Catch(Exception) + { + LoggerW("trying to set incorrect value"); + return DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + } + + return false; +} + +bool JSAudio::setPropertyTrackNumber( + JSContextRef context, + JSObjectRef object, + JSStringRef propertyName, + JSValueRef value, + JSValueRef* exception) +{ + Try + { + Converter converter(context); + + MediacontentAudioPtr audio = getAudioObject(object); + int trackNum = converter.toInt(value); + audio->setAudioTrackNum(trackNum, true); + return true; + } + Catch(Exception) + { + LoggerW("trying to set incorrect value"); + DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR); + } + + return false; +} + +} +} + |