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