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 <JSWebAPIErrorFactory.h>
21 #include <JSSimpleCoordinates.h>
23 #include "ContentConverter.h"
28 #define TIZEN_CONTENT_VIDEO_ATTRIBUTENAME "VideoContent"
29 #define TIZEN_CONTENT_VIDEO_GEOLOCATION "geolocation"
30 #define TIZEN_CONTENT_VIDEO_ALBUM "album"
31 #define TIZEN_CONTENT_VIDEO_ARTIST "artists"
32 #define TIZEN_CONTENT_VIDEO_DURATION "duration"
33 #define TIZEN_CONTENT_VIDEO_WIDTH "width"
34 #define TIZEN_CONTENT_VIDEO_HEIGHT "height"
36 using namespace DeviceAPI::Common;
37 using namespace DeviceAPI::Tizen;
38 using namespace WrtDeviceApis::Commons;
44 JSClassDefinition JSVideo::m_classInfo =
47 kJSClassAttributeNone,
48 TIZEN_CONTENT_VIDEO_ATTRIBUTENAME,
49 JSMedia::getClassRef(),
57 NULL, //DeleteProperty,
58 NULL, //GetPropertyNames,
59 NULL, //CallAsFunction,
60 NULL, //CallAsConstructor,
65 JSStaticValue JSVideo::m_property[] =
67 { TIZEN_CONTENT_VIDEO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
68 { TIZEN_CONTENT_VIDEO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
69 { TIZEN_CONTENT_VIDEO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},
70 { TIZEN_CONTENT_VIDEO_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
71 { TIZEN_CONTENT_VIDEO_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},
76 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
78 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
80 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
83 MediacontentVideoPtr privateData(new MediacontentVideo());
84 priv = new VideoPrivObject(context, privateData);
85 JSObjectSetPrivate(object, static_cast<void*>(priv));
86 LoggerD("new pravite object is created" );
89 LoggerD("private object already exists");
90 MediacontentVideoPtr video = getVideoObject(object);
91 DeviceAPI::Tizen::SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
92 JSUtil::setProperty(context, object, TIZEN_CONTENT_VIDEO_GEOLOCATION,
93 JSSimpleCoordinates::createJSObject(context,geoPtr),
94 kJSPropertyAttributeNone);
98 void JSVideo::finalize(JSObjectRef object)
100 VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
105 JSObjectSetPrivate(object, NULL);
110 const JSClassRef JSVideo::getClassRef()
114 m_jsClassRef = JSClassCreate(&m_classInfo);
119 const JSClassDefinition* JSVideo::getClassInfo()
124 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
126 VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
129 ThrowMsg(NullPointerException, "Private object is null");
131 MediacontentVideoPtr result = priv->getObject();
134 ThrowMsg(NullPointerException, "Private object is null");
139 JSValueRef JSVideo::getPropertyGeoLocation(
140 JSContextRef context,
142 JSStringRef propertyName,
143 JSValueRef* exception)
147 Converter converter(context);
148 MediacontentVideoPtr video = getVideoObject(object);
149 SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
150 return JSSimpleCoordinates::createJSObject(context,geoPtr);
154 LoggerW("trying to get incorrect value");
156 return JSValueMakeNull(context);
159 bool JSVideo::setPropertyGeoLocation(
160 JSContextRef context,
162 JSStringRef propertyName,
164 JSValueRef* exception)
168 Converter converter(context);
169 MediacontentVideoPtr video = getVideoObject(object);
171 DeviceAPI::Tizen::SimpleCoordinatesPtr geoLocation =
172 DeviceAPI::Tizen::JSSimpleCoordinates::getSimpleCoordinates(context, value);
175 video->setVideoLatitude(geoLocation->getLatitude());
176 video->setVideoLongitude(geoLocation->getLongitude());
182 LoggerW("trying to get incorrect value");
183 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
189 JSValueRef JSVideo::getPropertyAlbum(
190 JSContextRef context,
192 JSStringRef propertyName,
193 JSValueRef* exception)
197 Converter converter(context);
198 MediacontentVideoPtr video = getVideoObject(object);
199 if(!video->getVideoAlbum().empty()){
200 return converter.toJSValueRef(video->getVideoAlbum());
205 LoggerW("trying to get incorrect value");
207 return JSValueMakeNull(context);
211 JSValueRef JSVideo::getPropertyArtist(
212 JSContextRef context,
214 JSStringRef propertyName,
215 JSValueRef* exception)
219 Converter converter(context);
220 MediacontentVideoPtr video = getVideoObject(object);
221 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
222 if (NULL == jsResult) {
223 ThrowMsg(NullPointerException, "Could not create js array object");
226 for( unsigned int i=0; i < video->getVideoArtist().size(); i++) {
227 string artist = video->getVideoArtist().at(i);
228 JSValueRef val = converter.toJSValueRef(artist);
230 if(!JSSetArrayElement(context, jsResult, i, val)){
231 ThrowMsg(NullPointerException, "Could not insert value into js array");
237 LoggerW("trying to get incorrect value");
239 return JSValueMakeNull(context);
243 JSValueRef JSVideo::getPropertyDuration(
244 JSContextRef context,
246 JSStringRef propertyName,
247 JSValueRef* exception)
251 Converter converter(context);
252 MediacontentVideoPtr video = getVideoObject(object);
254 return converter.toJSValueRef(video->getVideoDuration());
259 LoggerW("trying to get incorrect value");
261 return JSValueMakeUndefined(context);
264 JSValueRef JSVideo::getPropertyWidth(
265 JSContextRef context,
267 JSStringRef propertyName,
268 JSValueRef* exception)
273 Converter converter(context);
274 MediacontentVideoPtr video = getVideoObject(object);
276 return converter.toJSValueRef(video->getVideoWidth());
281 LoggerW("trying to get incorrect value");
283 return JSValueMakeUndefined(context);
287 JSValueRef JSVideo::getPropertyHeight(
288 JSContextRef context,
290 JSStringRef propertyName,
291 JSValueRef* exception)
295 Converter converter(context);
296 MediacontentVideoPtr video = getVideoObject(object);
298 return converter.toJSValueRef(video->getVideoHeight());
303 LoggerW("trying to get incorrect value");
305 return JSValueMakeUndefined(context);
308 bool JSVideo::setPropertyArtists(
309 JSContextRef context,
311 JSStringRef propertyName,
313 JSValueRef* exception)
317 Converter converter(context);
318 MediacontentVideoPtr video = getVideoObject(object);
320 vector<std::string> artists;
321 if (!JSValueIsNull(context, value)) {
323 if (JSIsArrayValue(context, value)){
324 JSObjectRef jsObject = converter.toJSObjectRef(value);
325 for (std::size_t i = 0; i < JSGetArrayLength(context, jsObject); ++i) {
326 JSValueRef element = JSGetArrayElement(context, jsObject, i);
327 artists.push_back(converter.toString(element));
331 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
333 video->setVideoArtist(artists, true);
339 LoggerW("trying to set incorrect value");
340 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
345 bool JSVideo::setPropertyAlbum(
346 JSContextRef context,
348 JSStringRef propertyName,
350 JSValueRef* exception)
354 Converter converter(context);
356 MediacontentVideoPtr video = getVideoObject(object);
357 string album = converter.toString(value);
359 video->setVideoAlbum(album, true);
365 LoggerW("trying to set incorrect value");
366 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);