wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Content / ContentConverter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <JSUtil.h>
22
23 #include "ContentConverter.h"
24 #include "JSContent.h"
25 #include "JSImage.h"
26 #include "JSVideo.h"
27 #include "JSAudio.h"
28 #include "JSFolder.h"
29 #include "JSAudioLyrics.h"
30 #include <Logger.h>
31
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace Content {
36
37 MediaConverter::MediaConverter(JSContextRef context) : Converter(context)
38 {
39     LoggerD("entered");
40 }
41
42 JSValueRef MediaConverter::toFunctionOrNull(const JSValueRef& arg)
43 {
44     if (Validator(m_context).isCallback(arg)) 
45         {
46         return arg;
47     } else if (!JSValueIsNull(m_context,arg) && !JSValueIsUndefined(m_context, arg)) 
48     {
49         ThrowMsg(ConversionException, "Not a function nor JS null.");
50     }
51     return NULL;
52 }
53
54 JSValueRef MediaConverter::toFunction(const JSValueRef& arg)
55 {
56     if (Validator(m_context).isCallback(arg)) 
57         {
58         return arg;
59     } else if (JSValueIsNull(m_context,arg) || JSValueIsUndefined(m_context, arg))
60     {
61         ThrowMsg(InvalidArgumentException, "JS null passed as function.");
62     }
63     ThrowMsg(ConversionException, "Not a function nor JS null.");
64 }
65
66 /*
67 JSValueRef MediaConverter::toJSValueRefMediacontent(const IMediacontentPtr& arg)
68 {
69     MediacontentPrivObject *priv = new MediacontentPrivObject(m_context, arg);
70     return JSObjectMake(m_context, JSMediacontent::getClassRef(), priv);
71 }
72 */
73
74 JSValueRef MediaConverter::toJSValueRefMedia(const MediacontentMediaPtr& arg)
75 {
76         if(arg->getMediaType() == "IMAGE")
77         {
78             return JSUtils::makeObject(m_context, JSImage::getClassRef(), arg);
79         }
80         if(arg->getMediaType() == "VIDEO")
81         {
82             return JSUtils::makeObject(m_context, JSVideo::getClassRef(), arg);
83         }
84         if(arg->getMediaType() == "AUDIO")
85         {
86             return JSUtils::makeObject(m_context, JSAudio::getClassRef(), arg);
87         }
88         return JSUtils::makeObject(m_context, JSMedia::getClassRef(), arg);
89 }
90
91 JSValueRef MediaConverter::toJSValueRefFolder(const MediacontentFolderPtr& arg)
92 {
93     return JSUtils::makeObject(m_context, JSFolder::getClassRef(), arg);
94 }
95
96 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentMediaPtr> &arg)
97 {
98         return toJSValueRef_(arg, &MediaConverter::toJSValueRefMedia, this);
99 }
100
101 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentFolderPtr> &arg)
102 {
103         return toJSValueRef_(arg, &MediaConverter::toJSValueRefFolder, this);
104 }
105
106 JSValueRef MediaConverter::toJSValueRef(const MediacontentLyricsPtr &arg)
107 {
108         return JSUtils::makeObject(m_context, JSMediaLyrics::getClassRef(), arg);
109 }
110
111
112
113 MediacontentMediaListPtr MediaConverter::toVectorOfMediaItem(JSValueRef events)
114 {
115     LoggerD("entered");
116     MediacontentMediaListPtr result(new MediacontentMediaList());
117
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);
124
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.");
130         }
131
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);
140         }
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);
149         }
150         resultVector.push_back(JSMedia::getMediaObject(arg));
151     }
152     *result = resultVector;
153     return result;
154 }
155
156
157
158
159 }
160 }