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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <JSTizenException.h>
23 #include <JSTizenExceptionFactory.h>
24 #include <JSSimpleCoordinates.h>
26 #include "ContentConverter.h"
30 #define TIZEN_CONTENT_VIDEO_ATTRIBUTENAME "VideoContent"
31 #define TIZEN_CONTENT_VIDEO_GEOLOCATION "geolocation"
32 #define TIZEN_CONTENT_VIDEO_ALBUM "album"
33 #define TIZEN_CONTENT_VIDEO_ARTIST "artists"
34 #define TIZEN_CONTENT_VIDEO_DURATION "duration"
35 #define TIZEN_CONTENT_VIDEO_WIDTH "width"
36 #define TIZEN_CONTENT_VIDEO_HEIGHT "height"
38 using namespace DeviceAPI::Common;
39 using namespace DeviceAPI::Tizen;
40 using namespace WrtDeviceApis::Commons;
46 JSClassDefinition JSVideo::m_classInfo =
49 kJSClassAttributeNone,
50 TIZEN_CONTENT_VIDEO_ATTRIBUTENAME,
51 JSMedia::getClassRef(),
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
62 NULL, //CallAsConstructor,
67 JSStaticValue JSVideo::m_property[] =
69 // { TIZEN_CONTENT_VIDEO_GEOLOCATION, getPropertyGeoLocation, setPropertyGeoLocation, kJSPropertyAttributeNone},
70 { TIZEN_CONTENT_VIDEO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
71 { TIZEN_CONTENT_VIDEO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
72 { TIZEN_CONTENT_VIDEO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},
73 { TIZEN_CONTENT_VIDEO_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
74 { TIZEN_CONTENT_VIDEO_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
79 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
81 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
83 LogDebug( "entered" );
84 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
87 MediacontentVideoPtr privateData(new MediacontentVideo());
88 priv = new VideoPrivObject(context, privateData);
89 JSObjectSetPrivate(object, static_cast<void*>(priv));
90 LogDebug("new pravite object is created" );
93 LogDebug("private object already exists");
94 MediacontentVideoPtr video = getVideoObject(object);
95 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
96 JSUtil::setProperty(context, object, TIZEN_CONTENT_VIDEO_GEOLOCATION,
97 JSSimpleCoordinates::createJSObject(context,geoPtr),
98 kJSPropertyAttributeNone);
102 void JSVideo::finalize(JSObjectRef object)
104 LogDebug( "entered" );
105 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
110 JSObjectSetPrivate(object, NULL);
117 const JSClassRef JSVideo::getClassRef()
119 LogDebug("JSVideo::getClassRef()");
123 m_jsClassRef = JSClassCreate(&m_classInfo);
128 const JSClassDefinition* JSVideo::getClassInfo()
133 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
136 VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
139 ThrowMsg(NullPointerException, "Private object is null");
141 MediacontentVideoPtr result = priv->getObject();
144 ThrowMsg(NullPointerException, "Private object is null");
149 JSValueRef JSVideo::getPropertyGeoLocation(
150 JSContextRef context,
152 JSStringRef propertyName,
153 JSValueRef* exception)
158 Converter converter(context);
159 MediacontentVideoPtr video = getVideoObject(object);
160 SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
161 return JSSimpleCoordinates::createJSObject(context,geoPtr);
165 LogWarning("trying to get incorrect value");
167 return JSValueMakeUndefined(context);
170 bool JSVideo::setPropertyGeoLocation(
171 JSContextRef context,
173 JSStringRef propertyName,
175 JSValueRef* exception)
180 Converter converter(context);
181 MediacontentVideoPtr video = getVideoObject(object);
183 DeviceAPI::Tizen::SimpleCoordinatesPtr geoLocation =
184 DeviceAPI::Tizen::JSSimpleCoordinates::getSimpleCoordinates(context, value);
187 video->setVideoLatitude(geoLocation->getLatitude());
188 video->setVideoLongitude(geoLocation->getLongitude());
194 LogWarning("trying to get incorrect value");
195 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);
201 JSValueRef JSVideo::getPropertyAlbum(
202 JSContextRef context,
204 JSStringRef propertyName,
205 JSValueRef* exception)
210 Converter converter(context);
211 MediacontentVideoPtr video = getVideoObject(object);
213 return converter.toJSValueRef(video->getVideoAlbum());
218 LogWarning("trying to get incorrect value");
220 return JSValueMakeUndefined(context);
224 JSValueRef JSVideo::getPropertyArtist(
225 JSContextRef context,
227 JSStringRef propertyName,
228 JSValueRef* exception)
233 Converter converter(context);
234 MediacontentVideoPtr video = getVideoObject(object);
235 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
236 if (NULL == jsResult) {
237 ThrowMsg(NullPointerException, "Could not create js array object");
240 for( unsigned int i=0; i < video->getVideoArtist().size(); i++) {
241 string artist = video->getVideoArtist().at(i);
242 LogDebug("artist : " << artist);
243 JSValueRef val = converter.toJSValueRef(artist);
245 if(!JSSetArrayElement(context, jsResult, i, val)){
246 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Could not insert value into js array");
253 LogWarning("trying to get incorrect value");
255 return JSValueMakeUndefined(context);
259 JSValueRef JSVideo::getPropertyDuration(
260 JSContextRef context,
262 JSStringRef propertyName,
263 JSValueRef* exception)
268 Converter converter(context);
269 MediacontentVideoPtr video = getVideoObject(object);
271 return converter.toJSValueRef(video->getVideoDuration());
276 LogWarning("trying to get incorrect value");
278 return JSValueMakeUndefined(context);
281 JSValueRef JSVideo::getPropertyWidth(
282 JSContextRef context,
284 JSStringRef propertyName,
285 JSValueRef* exception)
290 Converter converter(context);
291 MediacontentVideoPtr video = getVideoObject(object);
293 return converter.toJSValueRef(video->getVideoWidth());
298 LogWarning("trying to get incorrect value");
300 return JSValueMakeUndefined(context);
304 JSValueRef JSVideo::getPropertyHeight(
305 JSContextRef context,
307 JSStringRef propertyName,
308 JSValueRef* exception)
313 Converter converter(context);
314 MediacontentVideoPtr video = getVideoObject(object);
316 return converter.toJSValueRef(video->getVideoHeight());
321 LogWarning("trying to get incorrect value");
323 return JSValueMakeUndefined(context);
326 bool JSVideo::setPropertyArtists(
327 JSContextRef context,
329 JSStringRef propertyName,
331 JSValueRef* exception)
336 Converter converter(context);
337 MediacontentVideoPtr video = getVideoObject(object);
339 vector<std::string> artists;
340 if (!JSValueIsNull(context, value)) {
342 if (JSIsArrayValue(context, value)){
343 JSObjectRef jsObject = converter.toJSObjectRef(value);
344 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
345 JSValueRef element = JSGetArrayElement(context, jsObject, i);
346 artists.push_back(converter.toString(element));
350 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
352 video->setVideoArtist(artists, true);
358 LogWarning("trying to set incorrect value");
359 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
364 bool JSVideo::setPropertyAlbum(
365 JSContextRef context,
367 JSStringRef propertyName,
369 JSValueRef* exception)
374 Converter converter(context);
376 MediacontentVideoPtr video = getVideoObject(object);
377 string album = converter.toString(value);
379 video->setVideoAlbum(album, true);
385 LogWarning("trying to set incorrect value");
386 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);