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"
32 using namespace DeviceAPI::Common;
37 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)
116 MediacontentMediaListPtr result(new MediacontentMediaList());
118 std::vector<MediacontentMediaPtr> resultVector;
119 JSObjectRef objArg = toJSObjectRef(events);
120 LoggerD("array length "<<JSGetArrayLength(m_context, objArg));
121 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
122 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
123 JSObjectRef arg = JSValueToObject(m_context, element, NULL);
125 if (!JSValueIsObjectOfClass(m_context, element, JSMedia::getClassRef()) &&
126 !JSValueIsObjectOfClass(m_context, element, JSImage::getClassRef()) &&
127 !JSValueIsObjectOfClass(m_context, element, JSAudio::getClassRef()) &&
128 !JSValueIsObjectOfClass(m_context, element, JSVideo::getClassRef())) {
129 throw TypeMismatchException("content type mismatched.");
132 if(JSValueIsObjectOfClass(m_context, element, JSImage::getClassRef())){
133 JSValueRef geoValRef = JSUtil::getProperty(m_context , JSUtil::JSValueToObject(m_context, element), "geolocation");
134 JSObjectRef geoObjRef = JSUtil::JSValueToObject(m_context, geoValRef);
135 double latitude = JSUtil::JSValueToDouble(m_context, JSUtil::getProperty(m_context, geoObjRef, "latitude"));
136 double longitude = JSUtil::JSValueToDouble(m_context,JSUtil::getProperty(m_context, geoObjRef, "longitude"));
137 MediacontentImagePtr imgPtr = JSImage::getImageObject(arg);
138 imgPtr->setImageLatitude(latitude);
139 imgPtr->setImageLongitude(longitude);
141 if(JSValueIsObjectOfClass(m_context, element, JSVideo::getClassRef())){
142 JSValueRef geoValRef = JSUtil::getProperty(m_context , JSUtil::JSValueToObject(m_context, element), "geolocation");
143 JSObjectRef geoObjRef = JSUtil::JSValueToObject(m_context, geoValRef);
144 double latitude = JSUtil::JSValueToDouble(m_context, JSUtil::getProperty(m_context, geoObjRef, "latitude"));
145 double longitude = JSUtil::JSValueToDouble(m_context,JSUtil::getProperty(m_context, geoObjRef, "longitude"));
146 MediacontentVideoPtr vedioPtr = JSVideo::getVideoObject(arg);
147 vedioPtr->setVideoLatitude(latitude);
148 vedioPtr->setVideoLongitude(longitude);
150 resultVector.push_back(JSMedia::getMediaObject(arg));
152 *result = resultVector;