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.
18 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
23 #include "ContentConverter.h"
24 #include "JSContent.h"
29 #include "JSAudioLyrics.h"
30 #include "ContentUtility.h"
33 using namespace DeviceAPI::Common;
38 MediaConverter::MediaConverter(JSContextRef context) : Converter(context)
42 JSValueRef MediaConverter::toFunctionOrNull(const JSValueRef& arg)
44 if (Validator(m_context).isCallback(arg))
47 } else if (!JSValueIsNull(m_context,arg) && !JSValueIsUndefined(m_context, arg))
49 ThrowMsg(ConversionException, "Not a function nor JS null.");
54 JSValueRef MediaConverter::toFunction(const JSValueRef& arg)
56 if (Validator(m_context).isCallback(arg))
59 } else if (JSValueIsNull(m_context,arg) || JSValueIsUndefined(m_context, arg))
61 ThrowMsg(InvalidArgumentException, "JS null passed as function.");
63 ThrowMsg(ConversionException, "Not a function nor JS null.");
67 JSValueRef MediaConverter::toJSValueRefMediacontent(const IMediacontentPtr& arg)
69 MediacontentPrivObject *priv = new MediacontentPrivObject(m_context, arg);
70 return JSObjectMake(m_context, JSMediacontent::getClassRef(), priv);
74 JSValueRef MediaConverter::toJSValueRefMedia(const MediacontentMediaPtr& arg)
76 if(arg->getMediaType() == "IMAGE")
78 return JSUtils::makeObject(m_context, JSImage::getClassRef(), arg);
80 if(arg->getMediaType() == "VIDEO")
82 return JSUtils::makeObject(m_context, JSVideo::getClassRef(), arg);
84 if(arg->getMediaType() == "AUDIO")
86 return JSUtils::makeObject(m_context, JSAudio::getClassRef(), arg);
88 return JSUtils::makeObject(m_context, JSMedia::getClassRef(), arg);
91 JSValueRef MediaConverter::toJSValueRefFolder(const MediacontentFolderPtr& arg)
93 return JSUtils::makeObject(m_context, JSFolder::getClassRef(), arg);
96 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentMediaPtr> &arg)
98 return toJSValueRef_(arg, &MediaConverter::toJSValueRefMedia, this);
101 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentFolderPtr> &arg)
103 return toJSValueRef_(arg, &MediaConverter::toJSValueRefFolder, this);
106 JSValueRef MediaConverter::toJSValueRef(const MediacontentLyricsPtr &arg)
108 return JSUtils::makeObject(m_context, JSMediaLyrics::getClassRef(), arg);
113 MediacontentMediaListPtr MediaConverter::toVectorOfMediaItem(JSValueRef events)
115 MediacontentMediaListPtr result(new MediacontentMediaList());
117 std::vector<MediacontentMediaPtr> resultVector;
118 JSObjectRef objArg = toJSObjectRef(events);
120 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
121 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
122 JSObjectRef arg = JSValueToObject(m_context, element, NULL);
124 if (!JSValueIsObjectOfClass(m_context, element, JSMedia::getClassRef()) &&
125 !JSValueIsObjectOfClass(m_context, element, JSImage::getClassRef()) &&
126 !JSValueIsObjectOfClass(m_context, element, JSAudio::getClassRef()) &&
127 !JSValueIsObjectOfClass(m_context, element, JSVideo::getClassRef())) {
128 throw TypeMismatchException("content type mismatched.");
131 if(JSValueIsObjectOfClass(m_context, element, JSImage::getClassRef())){
132 MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
133 JSValueRef geoValRef = JSUtil::getProperty(m_context , JSUtil::JSValueToObject(m_context, element), "geolocation");
134 if(!(JSValueIsNull(m_context, geoValRef) || JSValueIsUndefined(m_context, geoValRef))){
135 JSObjectRef geoObjRef = JSUtil::JSValueToObject(m_context, geoValRef);
136 double latitude = JSUtil::JSValueToDouble(m_context, JSUtil::getProperty(m_context, geoObjRef, "latitude"));
137 double longitude = JSUtil::JSValueToDouble(m_context,JSUtil::getProperty(m_context, geoObjRef, "longitude"));
138 if(ContentUtility::checkLocation(latitude, latitude)){
139 imgPtr->setImageLatitude(latitude);
140 imgPtr->setImageLongitude(longitude);
143 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
148 imgPtr->setImageLatitude(DEFAULT_GEOLOCATION);
149 imgPtr->setImageLongitude(DEFAULT_GEOLOCATION);
152 if(JSValueIsObjectOfClass(m_context, element, JSVideo::getClassRef())){
153 MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
154 JSValueRef geoValRef = JSUtil::getProperty(m_context , JSUtil::JSValueToObject(m_context, element), "geolocation");
155 if(!(JSValueIsNull(m_context, geoValRef) || JSValueIsUndefined(m_context, geoValRef))){
156 JSObjectRef geoObjRef = JSUtil::JSValueToObject(m_context, geoValRef);
157 double latitude = JSUtil::JSValueToDouble(m_context, JSUtil::getProperty(m_context, geoObjRef, "latitude"));
158 double longitude = JSUtil::JSValueToDouble(m_context,JSUtil::getProperty(m_context, geoObjRef, "longitude"));
159 if(ContentUtility::checkLocation(latitude, latitude)){
160 vedioPtr->setVideoLatitude(latitude);
161 vedioPtr->setVideoLongitude(longitude);
164 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
169 vedioPtr->setVideoLatitude(DEFAULT_GEOLOCATION);
170 vedioPtr->setVideoLongitude(DEFAULT_GEOLOCATION);
173 resultVector.push_back(JSMedia::getMediaObject(arg));
175 *result = resultVector;