Update change log and spec for wrt-plugins-tizen_0.4.29
[framework/web/wrt-plugins-tizen.git] / src / Content / JSVideo.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/Converter.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <JSWebAPIErrorFactory.h>
21 #include <JSSimpleCoordinates.h>
22
23 #include "ContentConverter.h"
24 #include "JSVideo.h"
25 #include "JSUtil.h"
26 #include <Logger.h>
27
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"
35
36 using namespace DeviceAPI::Common;
37 using namespace DeviceAPI::Tizen;
38 using namespace WrtDeviceApis::Commons;
39
40 namespace DeviceAPI {
41 namespace Content {
42
43
44 JSClassDefinition JSVideo::m_classInfo =
45 {
46         0,
47         kJSClassAttributeNone,
48         TIZEN_CONTENT_VIDEO_ATTRIBUTENAME,
49         JSMedia::getClassRef(),
50         m_property,
51         NULL, //    m_function,
52         initialize,
53         finalize,
54         NULL, //hasProperty,
55         NULL, //getProperty,
56         NULL, //setProperty,
57         NULL, //DeleteProperty,
58         NULL, //GetPropertyNames,
59         NULL, //CallAsFunction,
60         NULL, //CallAsConstructor,
61         NULL, //HasInstance,
62         NULL  //ConvertToType
63 };
64
65 JSStaticValue JSVideo::m_property[] =
66 {
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},
72         { 0, 0, 0, 0 }
73 };
74
75
76 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
77
78 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
79 {
80     VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
81     if (!priv)
82     {
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" );
87     }
88     else {
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);
95     }
96 }
97
98 void JSVideo::finalize(JSObjectRef object)
99 {
100     VideoPrivObject *priv =  static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
101     if (priv != NULL)
102     {
103         delete (priv);
104         priv = NULL;
105         JSObjectSetPrivate(object, NULL);
106     }
107     priv = NULL;
108 }
109
110 const JSClassRef JSVideo::getClassRef()
111 {
112     if (!m_jsClassRef)
113     {
114         m_jsClassRef = JSClassCreate(&m_classInfo);
115     }
116     return m_jsClassRef;
117 }
118
119 const JSClassDefinition* JSVideo::getClassInfo()
120 {
121     return &m_classInfo;
122 }
123
124 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
125 {
126     VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
127     if(!priv)
128     {
129         ThrowMsg(NullPointerException, "Private object is null");
130     }
131     MediacontentVideoPtr result = priv->getObject();
132     if (!result)
133     {
134         ThrowMsg(NullPointerException, "Private object is null");
135     }
136     return result;
137 }
138
139 JSValueRef JSVideo::getPropertyGeoLocation(
140                                                                                 JSContextRef context,
141                                                                         JSObjectRef object,
142                                                                         JSStringRef propertyName,
143                                                                         JSValueRef* exception)
144 {
145     Try
146     {
147         Converter converter(context);
148         MediacontentVideoPtr video = getVideoObject(object);
149         SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(video->getVideoLatitude(),video->getVideoLongitude()));
150         return JSSimpleCoordinates::createJSObject(context,geoPtr);
151     }
152     Catch(Exception)
153     {
154         LoggerW("trying to get incorrect value");
155     }
156     return JSValueMakeNull(context);
157 }
158
159 bool JSVideo::setPropertyGeoLocation(
160                                         JSContextRef context,
161                                         JSObjectRef object,
162                                         JSStringRef propertyName,
163                                         JSValueRef value,
164                                         JSValueRef* exception)
165 {
166     Try
167     {
168                 Converter converter(context);
169                 MediacontentVideoPtr video = getVideoObject(object);
170
171                 DeviceAPI::Tizen::SimpleCoordinatesPtr geoLocation =
172                     DeviceAPI::Tizen::JSSimpleCoordinates::getSimpleCoordinates(context, value);
173
174                 if(geoLocation){
175                         video->setVideoLatitude(geoLocation->getLatitude());
176                         video->setVideoLongitude(geoLocation->getLongitude());
177                         return true;
178                 }
179     }
180     Catch(Exception)
181     {
182         LoggerW("trying to get incorrect value");
183                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
184     }
185         return false;
186 }
187
188
189 JSValueRef JSVideo::getPropertyAlbum(
190                                                                         JSContextRef context,
191                                                                 JSObjectRef object, 
192                                                                 JSStringRef propertyName, 
193                                                                 JSValueRef* exception)
194 {
195     Try
196     {
197                 Converter converter(context);
198                 MediacontentVideoPtr video = getVideoObject(object);
199                 if(!video->getVideoAlbum().empty()){
200                         return converter.toJSValueRef(video->getVideoAlbum());
201                 }
202     }
203     Catch(Exception)
204     {
205         LoggerW("trying to get incorrect value");
206     }
207     return JSValueMakeNull(context);
208 }
209
210
211 JSValueRef JSVideo::getPropertyArtist(
212                                                                         JSContextRef context,
213                                                                 JSObjectRef object, 
214                                                                 JSStringRef propertyName, 
215                                                                 JSValueRef* exception)
216 {
217     Try
218     {
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");
224         }
225
226         for( unsigned int i=0; i < video->getVideoArtist().size(); i++) {
227             string artist = video->getVideoArtist().at(i);
228             JSValueRef val = converter.toJSValueRef(artist);
229
230             if(!JSSetArrayElement(context, jsResult, i, val)){
231                 ThrowMsg(NullPointerException, "Could not insert value into js array");
232             }
233         }
234         return jsResult;
235     }
236     Catch(Exception){
237         LoggerW("trying to get incorrect value");
238     }
239     return JSValueMakeNull(context);
240 }
241
242
243 JSValueRef JSVideo::getPropertyDuration(
244                                                                         JSContextRef context,
245                                                                 JSObjectRef object,
246                                                                 JSStringRef propertyName,
247                                                                 JSValueRef* exception)
248 {
249     Try
250     {
251         Converter converter(context);
252         MediacontentVideoPtr video = getVideoObject(object);
253         if(video){
254             return converter.toJSValueRef(video->getVideoDuration());
255         }
256     }
257     Catch(Exception)
258     {
259         LoggerW("trying to get incorrect value");
260     }
261     return JSValueMakeUndefined(context);
262 }
263
264 JSValueRef JSVideo::getPropertyWidth(
265                                                                         JSContextRef context,
266                                                                 JSObjectRef object,
267                                                                 JSStringRef propertyName,
268                                                                 JSValueRef* exception)
269 {
270
271     Try
272     {
273         Converter converter(context);
274         MediacontentVideoPtr video = getVideoObject(object);
275         if(video){
276             return converter.toJSValueRef(video->getVideoWidth());
277         }
278     }
279     Catch(Exception)
280     {
281         LoggerW("trying to get incorrect value");
282     }
283     return JSValueMakeUndefined(context);
284 }
285
286
287 JSValueRef JSVideo::getPropertyHeight(
288                                                                         JSContextRef context,
289                                                                 JSObjectRef object,
290                                                                 JSStringRef propertyName,
291                                                                 JSValueRef* exception)
292 {
293     Try
294     {
295         Converter converter(context);
296         MediacontentVideoPtr video = getVideoObject(object);
297         if(video){
298             return converter.toJSValueRef(video->getVideoHeight());
299         }
300     }
301     Catch(Exception)
302     {
303         LoggerW("trying to get incorrect value");
304     }
305     return JSValueMakeUndefined(context);
306 }
307
308 bool JSVideo::setPropertyArtists(
309                                         JSContextRef context,
310                                         JSObjectRef object,
311                                         JSStringRef propertyName,
312                                         JSValueRef value,
313                                         JSValueRef* exception)
314 {
315     Try
316     {
317         Converter converter(context);
318         MediacontentVideoPtr video = getVideoObject(object);
319
320         vector<std::string> artists;
321         if (!JSValueIsNull(context, value)) {
322
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));
328                 }
329             }
330             else{
331                 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
332             }
333             video->setVideoArtist(artists, true);
334             return true;
335         }
336     }
337     Catch(Exception)
338     {
339         LoggerW("trying to set incorrect value");
340         DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
341     }
342         return false;
343 }
344
345 bool JSVideo::setPropertyAlbum(
346                                         JSContextRef context,
347                                         JSObjectRef object,
348                                         JSStringRef propertyName,
349                                         JSValueRef value,
350                                         JSValueRef* exception)
351 {
352     Try
353     {
354         Converter converter(context);
355
356         MediacontentVideoPtr video = getVideoObject(object);
357         string album = converter.toString(value);
358         if(video){
359             video->setVideoAlbum(album, true);
360             return true;
361         }
362     }
363     Catch(Exception)
364     {
365         LoggerW("trying to set incorrect value");
366         DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
367     }
368         return false;
369 }
370
371
372 }
373 }
374