3bf4c1c9d2b049f007ebcdf7e7dff358da52af4a
[platform/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 "ContentUtility.h"
31 #include <Logger.h>
32
33 using namespace DeviceAPI::Common;
34
35 namespace DeviceAPI {
36 namespace Content {
37
38 MediaConverter::MediaConverter(JSContextRef context) : Converter(context)
39 {
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     MediacontentMediaListPtr result(new MediacontentMediaList());
116
117     std::vector<MediacontentMediaPtr> resultVector;
118     JSObjectRef objArg = toJSObjectRef(events);
119
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);
123
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.");
129         }
130
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);
141                 }
142                 else{
143                     ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
144                 }
145             }
146             else
147             {
148                 imgPtr->setImageLatitude(DEFAULT_GEOLOCATION);
149                 imgPtr->setImageLongitude(DEFAULT_GEOLOCATION);
150             }
151         }
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);
162                 }
163                 else{
164                     ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "geolocation value is not valid.");
165                 }
166             }
167             else
168             {
169                 vedioPtr->setVideoLatitude(DEFAULT_GEOLOCATION);
170                 vedioPtr->setVideoLongitude(DEFAULT_GEOLOCATION);
171             }
172         }
173         resultVector.push_back(JSMedia::getMediaObject(arg));
174     }
175     *result = resultVector;
176     return result;
177 }
178
179
180
181
182 }
183 }