6d0131cc6d59a39f7b57f32276d46b2bb75cf778
[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                       "itemURI"
39 #define TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH          "thumbnailURIs"
40 #define TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE           "releaseDate"   
41 #define TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE           "modifiedDate"  
42 #define TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION            "description"   
43 #define TIZEN_MEDIACONTENT_MEDIA_RATING                         "rating"        
44 #define TIZEN_MEDIACONTENT_MEDIA_SIZE                           "size"  
45 #define TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR            "editableAttributes"    
46
47
48 JSClassDefinition JSMedia::m_classInfo =
49 {
50     0,
51     kJSClassAttributeNone,
52     TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME,
53     0,
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
70 JSStaticValue JSMedia::m_property[] =
71 {
72     //EventProperties
73     { TIZEN_MEDIACONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_MEDIACONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
75     { TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},    
76     { TIZEN_MEDIACONTENT_MEDIA_TITLE, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
77     { TIZEN_MEDIACONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
78     { TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
79     { TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},    
80     { TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
81     { TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},    
82     { TIZEN_MEDIACONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},     
83     { TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},        
84     { TIZEN_MEDIACONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, kJSPropertyAttributeNone},        
85     { 0, 0, 0, 0 }
86 };
87
88
89 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
90
91 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
92 {
93     LogDebug( "entered" );
94     MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
95     if (!priv) 
96         {
97         MediacontentMediaPtr privateData(new MediacontentMedia());
98         priv = new MediaPrivObject(context, privateData);
99         JSObjectSetPrivate(object, static_cast<void*>(priv));
100         LogDebug("new event is created" );
101     }
102     else 
103         {
104         LogDebug("private object already exists");
105     }
106 }
107
108 void JSMedia::finalize(JSObjectRef object)
109 {
110     LogDebug( "entered" );
111     MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
112         if (priv != NULL)
113         {
114                 delete (priv);
115                 priv = NULL;
116                 JSObjectSetPrivate(object, NULL);
117         }
118     
119 }
120
121 const JSClassRef JSMedia::getClassRef()
122 {
123         LogDebug("JSMedia::getClassRef()");
124     if (!m_jsClassRef) 
125         {
126             m_jsClassRef = JSClassCreate(&m_classInfo);
127     }
128     return m_jsClassRef;
129 }
130
131 const JSClassDefinition* JSMedia::getClassInfo()
132 {
133     return &m_classInfo;
134 }
135
136 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
137 {
138     LogDebug("entered");
139     MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
140     if(!priv) 
141         {
142         ThrowMsg(NullPointerException, "Private object is null");
143     }
144     MediacontentMediaPtr result = priv->getObject();
145     if (!result) 
146         {
147         ThrowMsg(NullPointerException, "Private object is null");
148     }
149     return result;
150 }
151
152 JSValueRef JSMedia::getPropertyId(
153                                         JSContextRef context, 
154                                         JSObjectRef object,
155                                         JSStringRef propertyName, 
156                                         JSValueRef* exception)
157 {
158     LogDebug("entered");
159     Try
160     {
161         Converter converter(context);
162         MediacontentMediaPtr media = getMediaObject(object);
163
164         return converter.toJSValueRef(media->getMediaUUID());
165     }
166     Catch(Exception)
167     {
168         LogWarning("trying to get incorrect value");
169     }
170     return JSValueMakeUndefined(context);
171
172 }
173
174 JSValueRef      JSMedia::getPropertyMimeType(
175                                         JSContextRef context,
176                                         JSObjectRef object,
177                                         JSStringRef propertyName,
178                                         JSValueRef* exception)
179 {
180     LogDebug("entered");
181     Try
182     {
183         Converter converter(context);
184         MediacontentMediaPtr media = getMediaObject(object);
185         return converter.toJSValueRef(media->getMimeType());
186     }
187     Catch(Exception)
188     {
189         LogWarning("trying to get incorrect value");
190     }
191     return JSValueMakeUndefined(context);
192
193 }
194
195                                         
196 JSValueRef JSMedia::getPropertyDisplayName(
197                                         JSContextRef context,
198                                         JSObjectRef object,
199                                         JSStringRef propertyName,
200                                         JSValueRef* exception)
201 {
202     LogDebug("getPropertyDisplayName::entered");
203     Try
204     {
205         Converter converter(context);
206         MediacontentMediaPtr media = getMediaObject(object);
207         LogDebug("getDisplayName:"<<media->getDisplayName());    
208         return converter.toJSValueRef(media->getDisplayName());
209     }
210     Catch(Exception)
211     {
212         LogWarning("trying to get incorrect value");
213     }
214     return JSValueMakeUndefined(context);
215 }
216
217
218 JSValueRef JSMedia::getPropertyFilePath(
219                                         JSContextRef context,
220                                         JSObjectRef object,
221                                         JSStringRef propertyName,
222                                         JSValueRef* exception)
223 {
224     LogDebug("entered");
225     Try
226     {
227         Converter converter(context);
228         MediacontentMediaPtr media = getMediaObject(object);
229         return converter.toJSValueRef(media->getFilePath());
230     }
231     Catch(Exception)
232     {
233         LogWarning("trying to get incorrect value");
234     }
235     return JSValueMakeUndefined(context);
236 }
237
238
239 JSValueRef JSMedia::getPropertyThumbnailPath(
240                                         JSContextRef context,
241                                         JSObjectRef object, 
242                                         JSStringRef propertyName, 
243                                         JSValueRef* exception)
244 {
245     LogDebug("entered");
246     Try
247     {
248         Converter converter(context);
249         MediacontentMediaPtr media = getMediaObject(object);
250         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
251                 if (NULL == jsResult) 
252                 {
253                         ThrowMsg(NullPointerException, "Could not create js array object");
254                 }
255 //              for(unsigned int i=0; i<event->getThumbnailPath()->size(); i++) 
256                 {
257                         JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
258                         if(!JSSetArrayElement(context, jsResult, 0, val)) 
259                         {
260                            ThrowMsg(UnknownException, "Could not insert value into js array");
261                         }
262                 }
263
264                 return jsResult;      
265         
266     }
267     Catch(Exception)
268     {
269         LogWarning("trying to get incorrect value");
270     }
271     return JSValueMakeUndefined(context);
272 }
273
274 JSValueRef JSMedia::getPropertyDescription(
275                                         JSContextRef context,
276                                         JSObjectRef object, 
277                                         JSStringRef propertyName, 
278                                         JSValueRef* exception)
279 {
280     LogDebug("getPropertyDescription::entered");
281     Try
282     {
283         Converter converter(context);
284         MediacontentMediaPtr media = getMediaObject(object);
285         return converter.toJSValueRef(media->getDescription());
286     }
287     Catch(Exception)
288     {
289         LogWarning("trying to get incorrect value");
290     }
291     return JSValueMakeUndefined(context);
292 }
293
294
295 JSValueRef JSMedia::getPropertyModifiedDate(
296                                         JSContextRef context,
297                                         JSObjectRef object, 
298                                         JSStringRef propertyName, 
299                                         JSValueRef* exception)
300 {
301     LogDebug("entered");
302     Try
303     {
304         Converter converter(context);
305         MediacontentMediaPtr media = getMediaObject(object);
306         return converter.toJSValueRef(media->getModifiedDate());
307     }
308     Catch(Exception)
309     {
310         LogWarning("trying to get incorrect value");
311     }
312     return JSValueMakeUndefined(context);
313 }
314
315 JSValueRef JSMedia::getPropertyReleasedDate(
316                                         JSContextRef context,
317                                         JSObjectRef object,
318                                         JSStringRef propertyName,
319                                         JSValueRef* exception)
320 {
321     LogDebug("entered");
322     Try
323     {
324         Converter converter(context);
325         MediacontentMediaPtr media = getMediaObject(object);
326         return converter.toJSValueRef(media->getReleasedDate());
327     }
328     Catch(Exception)
329     {
330         LogWarning("trying to get incorrect value");
331     }
332     return JSValueMakeUndefined(context);
333
334 }
335
336
337 JSValueRef JSMedia::getPropertyRating(
338                                         JSContextRef context,
339                                         JSObjectRef object, 
340                                         JSStringRef propertyName, 
341                                         JSValueRef* exception)
342 {
343     LogDebug("entered");
344     Try
345     {
346         Converter converter(context);
347         MediacontentMediaPtr objMedia = getMediaObject(object);
348         return converter.toJSValueRef(objMedia->getRating());
349     }
350     Catch(Exception)
351     {
352         LogWarning("trying to get incorrect value");
353     }
354     return JSValueMakeUndefined(context);
355 }
356
357 JSValueRef JSMedia::getPropertyType(
358                                         JSContextRef context,
359                                         JSObjectRef object,
360                                         JSStringRef propertyName,
361                                         JSValueRef* exception)
362 {
363     LogDebug("getPropertyType::entered");
364     Try
365     {
366         Converter converter(context);
367         MediacontentMediaPtr media = getMediaObject(object);
368         return converter.toJSValueRef(media->getMediaType());
369     }
370     Catch(Exception)
371     {
372         LogWarning("trying to get incorrect value");
373     }
374     return JSValueMakeUndefined(context);
375 }
376
377
378 JSValueRef JSMedia::getPropertySize(
379                                         JSContextRef context,
380                                         JSObjectRef object,
381                                         JSStringRef propertyName,
382                                         JSValueRef* exception)
383 {
384     LogDebug("getPropertyType::entered");
385     Try
386     {
387         Converter converter(context);
388         MediacontentMediaPtr media = getMediaObject(object);
389         LogDebug("size:" << media->getSize());
390         double var = (double)media->getSize(); //casting.
391         return converter.toJSValueRef(var);
392     }
393     Catch(Exception)
394     {
395         LogWarning("trying to get incorrect value");
396     }
397
398     return JSValueMakeUndefined(context);
399 }
400
401
402 JSValueRef JSMedia::getPropertyEditableAttr(
403                                         JSContextRef context,
404                                         JSObjectRef object,
405                                         JSStringRef propertyName,
406                                         JSValueRef* exception)
407 {
408     Try
409     {
410                 Converter converter(context);
411                 MediacontentMediaPtr media = getMediaObject(object);
412                 vector<std::string> editableAttrList = media->getEditableAttr();
413                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
414
415                         if (NULL == jsResult) 
416                         {
417                             ThrowMsg(NullPointerException, "Could not create js array object");
418                         }
419                 for(unsigned int i=0; i<editableAttrList.size(); i++) 
420                         {
421                         JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
422                             if(!JSSetArrayElement(context, jsResult, i, val)) 
423                             {
424                                ThrowMsg(UnknownException, "Could not insert value into js array");
425                             }
426                         }
427                 return jsResult;
428         }
429         Catch(Exception)
430         {
431                 LogWarning("trying to get incorrect value");
432         }
433         return JSValueMakeUndefined(context);
434
435 }
436
437
438 bool    JSMedia::setPropertyRating(
439                                 JSContextRef context,
440                                 JSObjectRef object,
441                                 JSStringRef propertyName,
442                                 JSValueRef value,
443                                 JSValueRef* exception)
444 {
445     LogDebug("entered");
446     Try
447     {
448         Converter converter(context);
449         MediacontentMediaPtr objMedia = getMediaObject(object);
450         int rating = converter.toInt(value);
451
452         LogDebug("Inserted value : " << rating);
453
454         if(rating < 0)
455         {
456                 rating = 0;
457                 LogDebug("Rating value set 0 as inserted value is too small");
458         }
459         
460         if ( objMedia->getRating() != rating)
461         {
462                 objMedia->setRating(rating, true);
463         }
464                 
465         return true;
466     }
467     Catch(Exception)
468     {
469         LogWarning("trying to set incorrect value");
470         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
471     }
472
473     return false;
474
475 }
476
477 bool            JSMedia::setPropertyDisplayName(
478                                         JSContextRef context,
479                                         JSObjectRef object,
480                                         JSStringRef propertyName,
481                                         JSValueRef value,
482                                         JSValueRef* exception)
483 {
484         LogDebug("entered");
485         Try
486         {
487                 Converter converter(context);
488                 MediacontentMediaPtr objMedia = getMediaObject(object);
489                 string displayName = converter.toString(value);
490
491                 LogDebug("Inserted value : " << displayName);
492                 
493                 if ((objMedia->getDescription()).compare(displayName) != 0)
494                 {
495                         objMedia->setDisplayName(displayName, true);
496                 }
497                 
498                 return true;
499         }
500         Catch(Exception)
501         {
502                 LogWarning("trying to set incorrect value");
503                 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
504         }
505
506         return false;
507 }
508
509 bool            JSMedia::setPropertyDescription(
510                                         JSContextRef context,
511                                         JSObjectRef object,
512                                         JSStringRef propertyName,
513                                         JSValueRef value,
514                                         JSValueRef* exception)
515 {
516         LogDebug("entered");
517         Try
518         {
519                 Converter converter(context);
520                 MediacontentMediaPtr objMedia = getMediaObject(object);
521                 string description = converter.toString(value);
522
523                 LogDebug("Inserted value : " << description);
524                 
525                 if ( (objMedia->getDescription()).compare(description) != 0 )
526                 {
527                         objMedia->setDescription(description, true);
528                 }
529                 
530                 return true;
531         }
532         Catch(Exception)
533         {
534                 LogWarning("trying to set incorrect value");
535                 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
536         }
537
538         return false;
539 }
540
541 }
542 }
543 }