merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSMedia.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 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24
25
26 #include "JSMediacontent.h"
27 #include "JSMedia.h"
28
29 namespace TizenApis {
30 namespace Tizen1_0 {
31 namespace Mediacontent {
32
33 #define TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME          "MediaItem"
34 #define TIZEN_MEDIACONTENT_MEDIA_UID                            "id"
35 #define TIZEN_MEDIACONTENT_MEDIA_TYPE                           "type"
36 #define TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE                      "mimeType"
37 #define TIZEN_MEDIACONTENT_MEDIA_TITLE                          "title"
38 #define TIZEN_MEDIACONTENT_MEDIA_FILE_URL                       "fileURL"
39 #define TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH          "thumbnailPath"
40 #define TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE            "createdDate"   
41 #define TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE           "releasedDate"  
42 #define TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE           "modifiedDate"  
43 #define TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION            "description"   
44 #define TIZEN_MEDIACONTENT_MEDIA_RATING                         "rating"        
45 #define TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR            "editableAttibutes"     
46
47
48
49 JSClassDefinition JSMedia::m_classInfo =
50 {
51     0,
52     kJSClassAttributeNone,
53     TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME,
54     0,
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
71 JSStaticValue JSMedia::m_property[] =
72 {
73     //EventProperties
74     { TIZEN_MEDIACONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
75     { TIZEN_MEDIACONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
76     { TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},    
77     { TIZEN_MEDIACONTENT_MEDIA_TITLE, getPropertyDisplayName, NULL, kJSPropertyAttributeReadOnly},
78     { TIZEN_MEDIACONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
79     { TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
80     { TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE, getPropertyCreatedDate, NULL, kJSPropertyAttributeReadOnly},
81     { TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},    
82     { TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
83     { TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION, getPropertyDescription, NULL, kJSPropertyAttributeReadOnly},    
84     { TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},        
85     { TIZEN_MEDIACONTENT_MEDIA_RATING, getPropertyFavorite, setPropertyFavorite, kJSPropertyAttributeNone},        
86     { 0, 0, 0, 0 }
87 };
88
89
90 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
91
92 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
93 {
94     LogDebug( "entered" );
95     MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
96     if (!priv) 
97         {
98         MediacontentMediaPtr privateData(new MediacontentMedia());
99         priv = new MediaPrivObject(context, privateData);
100         JSObjectSetPrivate(object, static_cast<void*>(priv));
101         LogDebug("new event is created" );
102     }
103     else 
104         {
105         LogDebug("private object already exists");
106     }
107 }
108
109 void JSMedia::finalize(JSObjectRef object)
110 {
111     LogDebug( "entered" );
112     MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
113     delete priv;
114 }
115
116 const JSClassRef JSMedia::getClassRef()
117 {
118         LogDebug("JSMedia::getClassRef()");
119     if (!m_jsClassRef) 
120         {
121             m_jsClassRef = JSClassCreate(&m_classInfo);
122     }
123     return m_jsClassRef;
124 }
125
126 const JSClassDefinition* JSMedia::getClassInfo()
127 {
128     return &m_classInfo;
129 }
130
131 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
132 {
133     LogDebug("entered");
134     MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
135     if(!priv) 
136         {
137         ThrowMsg(NullPointerException, "Private object is null");
138     }
139     MediacontentMediaPtr result = priv->getObject();
140     if (!result) 
141         {
142         ThrowMsg(NullPointerException, "Private object is null");
143     }
144     return result;
145 }
146
147 JSValueRef JSMedia::getPropertyId(
148                                         JSContextRef context, 
149                                         JSObjectRef object,
150                                         JSStringRef propertyName, 
151                                         JSValueRef* exception)
152 {
153     LogDebug("entered");
154     Try
155     {
156         Converter converter(context);
157         MediacontentMediaPtr event = getMediaObject(object);
158
159         return converter.toJSValueRef(event->getMediaUUID());
160     }
161     Catch(Exception)
162     {
163         LogWarning("trying to get incorrect value");
164     }
165     return JSValueMakeUndefined(context);
166
167 }
168
169 JSValueRef      JSMedia::getPropertyMimeType(
170                                         JSContextRef context,
171                                         JSObjectRef object,
172                                         JSStringRef propertyName,
173                                         JSValueRef* exception)
174 {
175     LogDebug("entered");
176     Try
177     {
178         Converter converter(context);
179         MediacontentMediaPtr event = getMediaObject(object);
180         return converter.toJSValueRef(event->getMimeType());
181     }
182     Catch(Exception)
183     {
184         LogWarning("trying to get incorrect value");
185     }
186     return JSValueMakeUndefined(context);
187
188 }
189
190                                         
191 JSValueRef JSMedia::getPropertyDisplayName(
192                                         JSContextRef context,
193                                         JSObjectRef object,
194                                         JSStringRef propertyName,
195                                         JSValueRef* exception)
196 {
197     LogDebug("getPropertyDisplayName::entered");
198     Try
199     {
200         Converter converter(context);
201         MediacontentMediaPtr event = getMediaObject(object);
202         LogDebug("getDisplayName:"<<event->getDisplayName());    
203         return converter.toJSValueRef(event->getDisplayName());
204     }
205     Catch(Exception)
206     {
207         LogWarning("trying to get incorrect value");
208     }
209     return JSValueMakeUndefined(context);
210 }
211
212
213 JSValueRef JSMedia::getPropertyFilePath(
214                                         JSContextRef context,
215                                         JSObjectRef object,
216                                         JSStringRef propertyName,
217                                         JSValueRef* exception)
218 {
219     LogDebug("entered");
220     Try
221     {
222         Converter converter(context);
223         MediacontentMediaPtr event = getMediaObject(object);
224         return converter.toJSValueRef(event->getFilePath());
225     }
226     Catch(Exception)
227     {
228         LogWarning("trying to get incorrect value");
229     }
230     return JSValueMakeUndefined(context);
231 }
232
233
234 JSValueRef JSMedia::getPropertyThumbnailPath(
235                                         JSContextRef context,
236                                         JSObjectRef object, 
237                                         JSStringRef propertyName, 
238                                         JSValueRef* exception)
239 {
240     LogDebug("entered");
241     Try
242     {
243         Converter converter(context);
244         MediacontentMediaPtr event = getMediaObject(object);
245         return converter.toJSValueRef(event->getThumbnailPath());
246     }
247     Catch(Exception)
248     {
249         LogWarning("trying to get incorrect value");
250     }
251     return JSValueMakeUndefined(context);
252 }
253
254 JSValueRef JSMedia::getPropertyDescription(
255                                         JSContextRef context,
256                                         JSObjectRef object, 
257                                         JSStringRef propertyName, 
258                                         JSValueRef* exception)
259 {
260     LogDebug("entered");
261     Try
262     {
263         Converter converter(context);
264         MediacontentMediaPtr event = getMediaObject(object);
265         return converter.toJSValueRef(event->getDescription());
266     }
267     Catch(Exception)
268     {
269         LogWarning("trying to get incorrect value");
270     }
271     return JSValueMakeUndefined(context);
272 }
273
274
275
276 JSValueRef JSMedia::getPropertyCreatedDate(
277                                         JSContextRef context,
278                                         JSObjectRef object,
279                                         JSStringRef propertyName,
280                                         JSValueRef* exception)
281 {
282     LogDebug("entered");
283     Try
284     {
285         Converter converter(context);
286         MediacontentMediaPtr event = getMediaObject(object);
287         return converter.toJSValueRef(event->getCreatedDate());
288     }
289     Catch(Exception)
290     {
291         LogWarning("trying to get incorrect value");
292     }
293     return JSValueMakeUndefined(context);
294
295 }
296
297 JSValueRef JSMedia::getPropertyModifiedDate(
298                                         JSContextRef context,
299                                         JSObjectRef object, 
300                                         JSStringRef propertyName, 
301                                         JSValueRef* exception)
302 {
303     LogDebug("entered");
304     Try
305     {
306         Converter converter(context);
307         MediacontentMediaPtr event = getMediaObject(object);
308         return converter.toJSValueRef(event->getModifiedDate());
309     }
310     Catch(Exception)
311     {
312         LogWarning("trying to get incorrect value");
313     }
314     return JSValueMakeUndefined(context);
315 }
316
317 JSValueRef JSMedia::getPropertyReleasedDate(
318                                         JSContextRef context,
319                                         JSObjectRef object,
320                                         JSStringRef propertyName,
321                                         JSValueRef* exception)
322 {
323     LogDebug("entered");
324     Try
325     {
326         Converter converter(context);
327         MediacontentMediaPtr event = getMediaObject(object);
328         return converter.toJSValueRef(event->getReleasedDate());
329     }
330     Catch(Exception)
331     {
332         LogWarning("trying to get incorrect value");
333     }
334     return JSValueMakeUndefined(context);
335
336 }
337
338
339 JSValueRef JSMedia::getPropertyFavorite(
340                                         JSContextRef context,
341                                         JSObjectRef object, 
342                                         JSStringRef propertyName, 
343                                         JSValueRef* exception)
344 {
345     LogDebug("entered");
346     Try
347     {
348         Converter converter(context);
349         MediacontentMediaPtr event = getMediaObject(object);
350         return converter.toJSValueRef(event->getFavorite());
351     }
352     Catch(Exception)
353     {
354         LogWarning("trying to get incorrect value");
355     }
356     return JSValueMakeUndefined(context);
357 }
358
359 JSValueRef JSMedia::getPropertyType(
360                                         JSContextRef context,
361                                         JSObjectRef object,
362                                         JSStringRef propertyName,
363                                         JSValueRef* exception)
364 {
365     LogDebug("getPropertyType::entered");
366     Try
367     {
368         Converter converter(context);
369         MediacontentMediaPtr event = getMediaObject(object);
370         return converter.toJSValueRef(event->getMediaType());
371     }
372     Catch(Exception)
373     {
374         LogWarning("trying to get incorrect value");
375     }
376     return JSValueMakeUndefined(context);
377 }
378
379 JSValueRef JSMedia::getPropertyEditableAttr(
380                                         JSContextRef context,
381                                         JSObjectRef object,
382                                         JSStringRef propertyName,
383                                         JSValueRef* exception)
384 {
385     Try
386     {
387                 Converter converter(context);
388                 MediacontentMediaPtr event = getMediaObject(object);
389                 EditableAttributeListPtr editableAttrPtr = event->getEditableAttr();
390                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
391
392                 if(editableAttrPtr)
393                 {
394                         if (NULL == jsResult) 
395                         {
396                             ThrowMsg(NullPointerException, "Could not create js array object");
397                         }
398                         for(unsigned int i=0; i<editableAttrPtr->size(); i++) 
399                         {
400                                 JSValueRef val = converter.toJSValueRef(editableAttrPtr->at(i));
401                             if(!JSSetArrayElement(context, jsResult, i, val)) 
402                             {
403                                ThrowMsg(UnknownException, "Could not insert value into js array");
404                             }
405                         }
406                 }
407                 return jsResult;
408         }
409         Catch(Exception)
410         {
411                 LogWarning("trying to get incorrect value");
412         }
413         return JSValueMakeUndefined(context);
414
415 }
416
417
418 bool    JSMedia::setPropertyFavorite(
419                                 JSContextRef context,
420                                 JSObjectRef object,
421                                 JSStringRef propertyName,
422                                 JSValueRef value,
423                                 JSValueRef* exception)
424 {
425     LogDebug("entered");
426     Try
427     {
428         Converter converter(context);
429         MediacontentMediaPtr event = getMediaObject(object);
430             int rating = converter.toInt(value);
431
432             LogDebug("value : " << rating);
433         event->setFavorite(rating);
434         return true;
435     }
436     Catch(Exception)
437     {
438         LogWarning("trying to set incorrect value");
439         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
440     }
441
442     return false;
443
444 }
445                                         
446
447
448 }
449 }
450 }