merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSVideo.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenException.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/Tizen/JSSimpleCoordinates.h>
26
27
28 #include "JSMediacontent.h"
29 #include "JSVideo.h"
30
31 #define TIZEN_MEDIACONTENT_VIDEO_ATTRIBUTENAME          "Video"
32 #define TIZEN_MEDIACONTENT_VIDEO_GEOLOCATION            "geolocation"
33 #define TIZEN_MEDIACONTENT_VIDEO_ALBUM                          "album"
34 #define TIZEN_MEDIACONTENT_VIDEO_ARTIST                         "artist"
35 #define TIZEN_MEDIACONTENT_VIDEO_PLAYEDTIME             "playedTime" 
36 #define TIZEN_MEDIACONTENT_VIDEO_PLAYCOUNT                      "playCount" 
37 #define TIZEN_MEDIACONTENT_VIDEO_DURATION                       "duration"      
38 #define TIZEN_MEDIACONTENT_VIDEO_WIDTH                          "width"
39 #define TIZEN_MEDIACONTENT_VIDEO_HEIGHT                         "height"
40
41 using namespace TizenApis::Tizen1_0::Tizen;
42
43 namespace TizenApis {
44 namespace Tizen1_0 {
45 namespace Mediacontent {
46
47
48 JSClassDefinition JSVideo::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         TIZEN_MEDIACONTENT_VIDEO_ATTRIBUTENAME,
53         JSMedia::getClassRef(),
54         m_property,
55         NULL, //    m_function,
56         initialize,
57         finalize,
58         NULL, //hasProperty,
59         NULL, //getProperty,
60         NULL, //setProperty,
61         NULL, //DeleteProperty,
62         NULL, //GetPropertyNames,
63         NULL, //CallAsFunction,
64         NULL, //CallAsConstructor,
65         NULL, //HasInstance,
66         NULL  //ConvertToType
67 };
68
69 JSStaticValue JSVideo::m_property[] =
70 {
71         { TIZEN_MEDIACONTENT_VIDEO_GEOLOCATION, getPropertyGeoLocation, NULL, kJSPropertyAttributeReadOnly},
72         { TIZEN_MEDIACONTENT_VIDEO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
73         { TIZEN_MEDIACONTENT_VIDEO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
74         { TIZEN_MEDIACONTENT_VIDEO_PLAYEDTIME, getPropertyPlayedTime, setPropertyPlayedTime, kJSPropertyAttributeNone},
75         { TIZEN_MEDIACONTENT_VIDEO_PLAYCOUNT, getPropertyPlayCount, setPropertyPlayCount, kJSPropertyAttributeNone},    
76         { TIZEN_MEDIACONTENT_VIDEO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},    
77         { TIZEN_MEDIACONTENT_VIDEO_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
78         { TIZEN_MEDIACONTENT_VIDEO_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},    
79         { 0, 0, 0, 0 }
80 };
81
82
83 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
84
85 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
86 {
87     LogDebug( "entered" );
88     VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
89     if (!priv) 
90     {
91         MediacontentVideoPtr privateData(new MediacontentVideo());
92         priv = new VideoPrivObject(context, privateData);
93         JSObjectSetPrivate(object, static_cast<void*>(priv));
94         LogDebug("new event is created" );
95     }
96     else {
97         LogDebug("private object already exists");
98     }
99 }
100
101 void JSVideo::finalize(JSObjectRef object)
102 {
103     LogDebug( "entered" );
104         VideoPrivObject *priv =  static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
105     delete priv;
106 }
107
108 const JSClassRef JSVideo::getClassRef()
109 {
110         LogDebug("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
125 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
126 {
127     LogDebug("entered");
128     VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
129     if(!priv) 
130     {
131         ThrowMsg(NullPointerException, "Private object is null");
132     }
133     MediacontentVideoPtr result = priv->getObject();
134     if (!result) 
135     {
136         ThrowMsg(NullPointerException, "Private object is null");
137     }
138     return result;
139 }
140
141                         
142 JSValueRef JSVideo::getPropertyGeoLocation(
143                                                                                 JSContextRef context,
144                                                                         JSObjectRef object,
145                                                                         JSStringRef propertyName,
146                                                                         JSValueRef* exception)
147 {
148     LogDebug("entered");
149     Try
150     {
151         Converter converter(context);
152         MediacontentVideoPtr event = getVideoObject(object);
153                 SimpleCoordinatesPtr geoPtr(new SimpleCoordinates(event->getVideoLatitude(),event->getVideoLongitude()));
154         return JSSimpleCoordinates::createJSObject(context,geoPtr);
155         //return converter.toJSValueRef(event->getVideoLongitude());
156     }
157     Catch(Exception)
158     {
159         LogWarning("trying to get incorrect value");
160     }
161     return JSValueMakeUndefined(context);
162 }
163
164
165 JSValueRef JSVideo::getPropertyAlbum(
166                                                                         JSContextRef context,
167                                                                 JSObjectRef object, 
168                                                                 JSStringRef propertyName, 
169                                                                 JSValueRef* exception)
170 {
171     LogDebug("entered");
172     Try
173     {
174         Converter converter(context);
175         MediacontentVideoPtr event = getVideoObject(object);
176         return converter.toJSValueRef(event->getVideoAlbum());
177     }
178     Catch(Exception)
179     {
180         LogWarning("trying to get incorrect value");
181     }
182     return JSValueMakeUndefined(context);
183 }
184
185
186 JSValueRef JSVideo::getPropertyArtist(
187                                                                         JSContextRef context,
188                                                                 JSObjectRef object, 
189                                                                 JSStringRef propertyName, 
190                                                                 JSValueRef* exception)
191 {
192     LogDebug("entered");
193     Try
194     {
195         Converter converter(context);
196         MediacontentVideoPtr event = getVideoObject(object);
197         return converter.toJSValueRef(event->getVideoArtist());
198     }
199     Catch(Exception)
200     {
201         LogWarning("trying to get incorrect value");
202     }
203     return JSValueMakeUndefined(context);
204 }
205
206
207 JSValueRef JSVideo::getPropertyPlayedTime(
208                                                                         JSContextRef context,
209                                                                 JSObjectRef object,
210                                                                 JSStringRef propertyName,
211                                                                 JSValueRef* exception)
212 {
213     LogDebug("entered");
214     Try
215     {
216         Converter converter(context);
217         MediacontentVideoPtr event = getVideoObject(object);
218         return converter.toJSValueRef(event->getVideoPlayedTime());
219     }
220     Catch(Exception)
221     {
222         LogWarning("trying to get incorrect value");
223     }
224     return JSValueMakeUndefined(context);
225 }
226
227 JSValueRef JSVideo::getPropertyPlayCount(
228                                                                         JSContextRef context,
229                                                                 JSObjectRef object,
230                                                                 JSStringRef propertyName,
231                                                                 JSValueRef* exception)
232 {
233     LogDebug("entered");
234     Try
235     {
236         Converter converter(context);
237         MediacontentVideoPtr event = getVideoObject(object);
238         return converter.toJSValueRef(event->getVideoPlayCount());
239     }
240     Catch(Exception)
241     {
242         LogWarning("trying to get incorrect value");
243     }
244     return JSValueMakeUndefined(context);
245 }
246
247
248
249 JSValueRef JSVideo::getPropertyDuration(
250                                                                         JSContextRef context,
251                                                                 JSObjectRef object,
252                                                                 JSStringRef propertyName,
253                                                                 JSValueRef* exception)
254 {
255     LogDebug("entered");
256     Try
257     {
258         Converter converter(context);
259         MediacontentVideoPtr event = getVideoObject(object);
260         return converter.toJSValueRef(event->getVideoDuration());
261     }
262     Catch(Exception)
263     {
264         LogWarning("trying to get incorrect value");
265     }
266     return JSValueMakeUndefined(context);
267 }
268
269 JSValueRef JSVideo::getPropertyWidth(
270                                                                         JSContextRef context,
271                                                                 JSObjectRef object,
272                                                                 JSStringRef propertyName,
273                                                                 JSValueRef* exception)
274 {
275     LogDebug("entered");
276     Try
277     {
278         Converter converter(context);
279         MediacontentVideoPtr event = getVideoObject(object);
280         return converter.toJSValueRef(event->getVideoWidth());
281     }
282     Catch(Exception)
283     {
284         LogWarning("trying to get incorrect value");
285     }
286     return JSValueMakeUndefined(context);
287 }
288
289
290 JSValueRef JSVideo::getPropertyHeight(
291                                                                         JSContextRef context,
292                                                                 JSObjectRef object,
293                                                                 JSStringRef propertyName,
294                                                                 JSValueRef* exception)
295 {
296     LogDebug("entered");
297     Try
298     {
299         Converter converter(context);
300         MediacontentVideoPtr event = getVideoObject(object);
301         return converter.toJSValueRef(event->getVideoHeight());
302     }
303     Catch(Exception)
304     {
305         LogWarning("trying to get incorrect value");
306     }
307     return JSValueMakeUndefined(context);
308 }
309
310
311
312 bool JSVideo::setPropertyPlayedTime(
313                                         JSContextRef context,
314                                         JSObjectRef object,
315                                         JSStringRef propertyName,
316                                         JSValueRef value,
317                                         JSValueRef* exception)
318 {
319     LogDebug("entered");
320     Try
321     {
322         Converter converter(context);
323         MediacontentVideoPtr event = getVideoObject(object);
324         int rating = converter.toLong(value);
325         event->setVideoPlayedTime(rating);
326         return true;
327     }
328     Catch(Exception)
329     {
330         LogWarning("trying to set incorrect value");
331         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
332     }
333
334     return false;
335
336
337 }
338 bool JSVideo::setPropertyPlayCount(
339                                         JSContextRef context,
340                                         JSObjectRef object,
341                                         JSStringRef propertyName,
342                                         JSValueRef value,
343                                         JSValueRef* exception)
344 {
345     LogDebug("entered");
346     Try
347     {
348         Converter converter(context);
349         MediacontentVideoPtr event = getVideoObject(object);
350         int rating = converter.toInt(value);
351         event->setVideoPlayCount(rating);
352         return true;
353     }
354     Catch(Exception)
355     {
356         LogWarning("trying to set incorrect value");
357         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
358     }
359
360     return false;
361
362 }
363
364
365
366 }
367 }
368 }