Beta merge 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
26 #include "JSMediacontent.h"
27 #include "JSVideo.h"
28
29 #define TIZEN_MEDIACONTENT_VIDEO_ATTRIBUTENAME          "Video"
30 #define TIZEN_MEDIACONTENT_VIDEO_GEOLOCATION            "geolocation"
31 #define TIZEN_MEDIACONTENT_VIDEO_ALBUM                          "album"
32 #define TIZEN_MEDIACONTENT_VIDEO_ARTIST                         "artist"
33 #define TIZEN_MEDIACONTENT_VIDEO_PLAYEDTIME             "playedTime" 
34 #define TIZEN_MEDIACONTENT_VIDEO_PLAYCOUNT                      "playCount" 
35 #define TIZEN_MEDIACONTENT_VIDEO_DURATION                       "duration"      
36 #define TIZEN_MEDIACONTENT_VIDEO_WIDTH                          "width"
37 #define TIZEN_MEDIACONTENT_VIDEO_HEIGHT                         "height"
38
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace TizenApis::Api::Mediacontent;
42
43
44 namespace TizenApis {
45 namespace Tizen1_0 {
46 namespace Mediacontent {
47
48
49 JSClassDefinition JSVideo::m_classInfo =
50 {
51         0,
52         kJSClassAttributeNone,
53         TIZEN_MEDIACONTENT_VIDEO_ATTRIBUTENAME,
54         JSMedia::getClassRef(),
55         m_property,
56         NULL, //    m_function,
57         initialize,
58         finalize,
59         NULL, //hasProperty,
60         NULL, //getProperty,
61         NULL, //setProperty,
62         NULL, //DeleteProperty,
63         NULL, //GetPropertyNames,
64         NULL, //CallAsFunction,
65         NULL, //CallAsConstructor,
66         NULL, //HasInstance,
67         NULL  //ConvertToType
68 };
69
70 JSStaticValue JSVideo::m_property[] =
71 {
72         { TIZEN_MEDIACONTENT_VIDEO_GEOLOCATION, getPropertyGeoLocation, NULL, kJSPropertyAttributeReadOnly},
73         { TIZEN_MEDIACONTENT_VIDEO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
74         { TIZEN_MEDIACONTENT_VIDEO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
75         { TIZEN_MEDIACONTENT_VIDEO_PLAYEDTIME, getPropertyPlayedTime, setPropertyPlayedTime, kJSPropertyAttributeNone},
76         { TIZEN_MEDIACONTENT_VIDEO_PLAYCOUNT, getPropertyPlayCount, setPropertyPlayCount, kJSPropertyAttributeNone},    
77         { TIZEN_MEDIACONTENT_VIDEO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},    
78         { TIZEN_MEDIACONTENT_VIDEO_WIDTH, getPropertyWidth, NULL, kJSPropertyAttributeReadOnly},
79         { TIZEN_MEDIACONTENT_VIDEO_HEIGHT, getPropertyHeight, NULL, kJSPropertyAttributeReadOnly},    
80         { 0, 0, 0, 0 }
81 };
82
83
84 JSClassRef JSVideo::m_jsClassRef = JSClassCreate(JSVideo::getClassInfo());
85
86 void JSVideo::initialize(JSContextRef context, JSObjectRef object)
87 {
88     LogDebug( "entered" );
89     VideoPrivObject *priv = static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) );
90     if (!priv) 
91     {
92         MediacontentVideoPtr privateData(new MediacontentVideo());
93         priv = new VideoPrivObject(context, privateData);
94         JSObjectSetPrivate(object, static_cast<void*>(priv));
95         LogDebug("new event is created" );
96     }
97     else {
98         LogDebug("private object already exists");
99     }
100 }
101
102 void JSVideo::finalize(JSObjectRef object)
103 {
104     LogDebug( "entered" );
105         VideoPrivObject *priv =  static_cast<VideoPrivObject*>( JSObjectGetPrivate( object ) ) ;
106     delete priv;
107 }
108
109 const JSClassRef JSVideo::getClassRef()
110 {
111         LogDebug("JSVideo::getClassRef()");
112
113     if (!m_jsClassRef) 
114     {
115             m_jsClassRef = JSClassCreate(&m_classInfo);
116     }
117     return m_jsClassRef;
118 }
119
120 const JSClassDefinition* JSVideo::getClassInfo()
121 {
122     return &m_classInfo;
123 }
124
125
126 MediacontentVideoPtr JSVideo::getVideoObject(JSObjectRef object)
127 {
128     LogDebug("entered");
129     VideoPrivObject *priv = static_cast<VideoPrivObject*>(JSObjectGetPrivate(object));
130     if(!priv) 
131     {
132         ThrowMsg(NullPointerException, "Private object is null");
133     }
134     MediacontentVideoPtr result = priv->getObject();
135     if (!result) 
136     {
137         ThrowMsg(NullPointerException, "Private object is null");
138     }
139     return result;
140 }
141
142                         
143 JSValueRef JSVideo::getPropertyGeoLocation(
144                                                                                 JSContextRef context,
145                                                                         JSObjectRef object,
146                                                                         JSStringRef propertyName,
147                                                                         JSValueRef* exception)
148 {
149     LogDebug("entered");
150     Try
151     {
152         Converter converter(context);
153         MediacontentVideoPtr event = getVideoObject(object);
154         //Todo
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 }