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