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/Converter.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <JSTizenException.h>
21 #include <JSTizenExceptionFactory.h>
22 #include <JSSimpleCoordinates.h>
24 #include "ContentConverter.h"
29 #define TIZEN_CONTENT_VIDEO_ATTRIBUTENAME "VideoContent"
30 #define TIZEN_CONTENT_VIDEO_GEOLOCATION "geolocation"
31 #define TIZEN_CONTENT_VIDEO_ALBUM "album"
32 #define TIZEN_CONTENT_VIDEO_ARTIST "artists"
33 #define TIZEN_CONTENT_VIDEO_DURATION "duration"
34 #define TIZEN_CONTENT_VIDEO_WIDTH "width"
35 #define TIZEN_CONTENT_VIDEO_HEIGHT "height"
37 using namespace DeviceAPI::Common;
38 using namespace DeviceAPI::Tizen;
39 using namespace WrtDeviceApis::Commons;
45 JSClassDefinition JSVideo::m_classInfo =
48 kJSClassAttributeNone,
49 TIZEN_CONTENT_VIDEO_ATTRIBUTENAME,
50 JSMedia::getClassRef(),
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSVideo::m_property[] =
68 // { TIZEN_CONTENT_VIDEO_GEOLOCATION, getPropertyGeoLocation, setPropertyGeoLocation, kJSPropertyAttributeNone},
69 { TIZEN_CONTENT_VIDEO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
70 { TIZEN_CONTENT_VIDEO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
71 { TIZEN_CONTENT_VIDEO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},
72 { TIZEN_CONTENT_VIDEO_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
73 { TIZEN_CONTENT_VIDEO_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
78 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
80 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
83 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
86 MediacontentVideoPtr privateData(new MediacontentVideo());
87 priv = new VideoPrivObject(context, privateData);
88 JSObjectSetPrivate(object, static_cast<void*>(priv));
89 LoggerD("new pravite object is created" );
92 LoggerD("private object already exists");
93 MediacontentVideoPtr video = getVideoObject(object);
94 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
95 JSUtil::setProperty(context, object, TIZEN_CONTENT_VIDEO_GEOLOCATION,
96 JSSimpleCoordinates::createJSObject(context,geoPtr),
97 kJSPropertyAttributeNone);
101 void JSVideo::finalize(JSObjectRef object)
103 LoggerD( "entered" );
104 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
109 JSObjectSetPrivate(object, NULL);
116 const JSClassRef JSVideo::getClassRef()
118 LoggerD("JSVideo::getClassRef()");
122 m_jsClassRef = JSClassCreate(&m_classInfo);
127 const JSClassDefinition* JSVideo::getClassInfo()
132 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
135 VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
138 ThrowMsg(NullPointerException, "Private object is null");
140 MediacontentVideoPtr result = priv->getObject();
143 ThrowMsg(NullPointerException, "Private object is null");
148 JSValueRef JSVideo::getPropertyGeoLocation(
149 JSContextRef context,
151 JSStringRef propertyName,
152 JSValueRef* exception)
157 Converter converter(context);
158 MediacontentVideoPtr video = getVideoObject(object);
159 SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
160 return JSSimpleCoordinates::createJSObject(context,geoPtr);
164 LoggerW("trying to get incorrect value");
166 return JSValueMakeNull(context);
169 bool JSVideo::setPropertyGeoLocation(
170 JSContextRef context,
172 JSStringRef propertyName,
174 JSValueRef* exception)
179 Converter converter(context);
180 MediacontentVideoPtr video = getVideoObject(object);
182 DeviceAPI::Tizen::SimpleCoordinatesPtr geoLocation =
183 DeviceAPI::Tizen::JSSimpleCoordinates::getSimpleCoordinates(context, value);
186 video->setVideoLatitude(geoLocation->getLatitude());
187 video->setVideoLongitude(geoLocation->getLongitude());
193 LoggerW("trying to get incorrect value");
194 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);
200 JSValueRef JSVideo::getPropertyAlbum(
201 JSContextRef context,
203 JSStringRef propertyName,
204 JSValueRef* exception)
209 Converter converter(context);
210 MediacontentVideoPtr video = getVideoObject(object);
211 if(!video->getVideoAlbum().empty()){
212 return converter.toJSValueRef(video->getVideoAlbum());
217 LoggerW("trying to get incorrect value");
219 return JSValueMakeNull(context);
223 JSValueRef JSVideo::getPropertyArtist(
224 JSContextRef context,
226 JSStringRef propertyName,
227 JSValueRef* exception)
232 Converter converter(context);
233 MediacontentVideoPtr video = getVideoObject(object);
234 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
235 if (NULL == jsResult) {
236 ThrowMsg(NullPointerException, "Could not create js array object");
239 for( unsigned int i=0; i < video->getVideoArtist().size(); i++) {
240 string artist = video->getVideoArtist().at(i);
241 LoggerD("artist : " << artist);
242 JSValueRef val = converter.toJSValueRef(artist);
244 if(!JSSetArrayElement(context, jsResult, i, val)){
245 ThrowMsg(NullPointerException, "Could not insert value into js array");
251 LoggerW("trying to get incorrect value");
253 return JSValueMakeNull(context);
257 JSValueRef JSVideo::getPropertyDuration(
258 JSContextRef context,
260 JSStringRef propertyName,
261 JSValueRef* exception)
266 Converter converter(context);
267 MediacontentVideoPtr video = getVideoObject(object);
269 return converter.toJSValueRef(video->getVideoDuration());
274 LoggerW("trying to get incorrect value");
276 return JSValueMakeUndefined(context);
279 JSValueRef JSVideo::getPropertyWidth(
280 JSContextRef context,
282 JSStringRef propertyName,
283 JSValueRef* exception)
288 Converter converter(context);
289 MediacontentVideoPtr video = getVideoObject(object);
291 return converter.toJSValueRef(video->getVideoWidth());
296 LoggerW("trying to get incorrect value");
298 return JSValueMakeUndefined(context);
302 JSValueRef JSVideo::getPropertyHeight(
303 JSContextRef context,
305 JSStringRef propertyName,
306 JSValueRef* exception)
311 Converter converter(context);
312 MediacontentVideoPtr video = getVideoObject(object);
314 return converter.toJSValueRef(video->getVideoHeight());
319 LoggerW("trying to get incorrect value");
321 return JSValueMakeUndefined(context);
324 bool JSVideo::setPropertyArtists(
325 JSContextRef context,
327 JSStringRef propertyName,
329 JSValueRef* exception)
334 Converter converter(context);
335 MediacontentVideoPtr video = getVideoObject(object);
337 vector<std::string> artists;
338 if (!JSValueIsNull(context, value)) {
340 if (JSIsArrayValue(context, value)){
341 JSObjectRef jsObject = converter.toJSObjectRef(value);
342 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
343 JSValueRef element = JSGetArrayElement(context, jsObject, i);
344 artists.push_back(converter.toString(element));
348 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
350 video->setVideoArtist(artists, true);
356 LoggerW("trying to set incorrect value");
357 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
362 bool JSVideo::setPropertyAlbum(
363 JSContextRef context,
365 JSStringRef propertyName,
367 JSValueRef* exception)
372 Converter converter(context);
374 MediacontentVideoPtr video = getVideoObject(object);
375 string album = converter.toString(value);
377 video->setVideoAlbum(album, true);
383 LoggerW("trying to set incorrect value");
384 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);