Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / Content / JSContent.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/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <JSTizenException.h>
24 #include <JSTizenExceptionFactory.h>
25
26
27 #include "JSContent.h"
28
29 namespace DeviceAPI {
30 namespace Content {
31
32 #define TIZEN_CONTENT_MEDIA_ATTRIBUTENAME               "Content"
33 #define TIZEN_CONTENT_MEDIA_UID                         "id"
34 #define TIZEN_CONTENT_MEDIA_TYPE                        "type"
35 #define TIZEN_CONTENT_MEDIA_MIME_TYPE                   "mimeType"
36 #define TIZEN_CONTENT_MEDIA_NAME                        "name"
37 #define TIZEN_CONTENT_MEDIA_TITLE                       "title"
38 #define TIZEN_CONTENT_MEDIA_FILE_URL                    "contentURI"
39 #define TIZEN_CONTENT_MEDIA_THUMBNAILPATH               "thumbnailURIs"
40 #define TIZEN_CONTENT_MEDIA_RELEASEDDATE                "releaseDate"
41 #define TIZEN_CONTENT_MEDIA_MODIFIEDDATE                "modifiedDate"  
42 #define TIZEN_CONTENT_MEDIA_DESCRIPTION                 "description"   
43 #define TIZEN_CONTENT_MEDIA_RATING                      "rating"        
44 #define TIZEN_CONTENT_MEDIA_SIZE                        "size"  
45 #define TIZEN_CONTENT_MEDIA_EDIABLEATTR                 "editableAttributes"    
46
47
48 JSClassDefinition JSMedia::m_classInfo =
49 {
50     0,
51     kJSClassAttributeNone,
52     TIZEN_CONTENT_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_CONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_CONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
75     { TIZEN_CONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
76     { TIZEN_CONTENT_MEDIA_NAME, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
77     { TIZEN_CONTENT_MEDIA_TITLE, getPropertyTitle, NULL, kJSPropertyAttributeReadOnly},    
78     { TIZEN_CONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
79     { TIZEN_CONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
80     { TIZEN_CONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
81     { TIZEN_CONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
82     { TIZEN_CONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},    
83     { TIZEN_CONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},     
84     { TIZEN_CONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},        
85     { TIZEN_CONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, 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         if (priv != NULL)
114         {
115                 delete (priv);
116                 priv = NULL;
117                 JSObjectSetPrivate(object, NULL);
118         }
119     
120 }
121
122 const JSClassRef JSMedia::getClassRef()
123 {
124         LogDebug("JSMedia::getClassRef()");
125     if (!m_jsClassRef) 
126         {
127             m_jsClassRef = JSClassCreate(&m_classInfo);
128     }
129     return m_jsClassRef;
130 }
131
132 const JSClassDefinition* JSMedia::getClassInfo()
133 {
134     return &m_classInfo;
135 }
136
137 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
138 {
139     LogDebug("entered");
140     MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
141     if(!priv) 
142         {
143         ThrowMsg(NullPointerException, "Private object is null");
144     }
145     MediacontentMediaPtr result = priv->getObject();
146     if (!result) 
147         {
148         ThrowMsg(NullPointerException, "Private object is null");
149     }
150     return result;
151 }
152
153 JSValueRef JSMedia::getPropertyId(
154                                         JSContextRef context, 
155                                         JSObjectRef object,
156                                         JSStringRef propertyName, 
157                                         JSValueRef* exception)
158 {
159     LogDebug("entered");
160     Try
161     {
162         Converter converter(context);
163         MediacontentMediaPtr media = getMediaObject(object);
164
165         return converter.toJSValueRef(media->getMediaUUID());
166     }
167     Catch(Exception)
168     {
169         LogWarning("trying to get incorrect value");
170     }
171     return JSValueMakeUndefined(context);
172
173 }
174
175 JSValueRef      JSMedia::getPropertyMimeType(
176                                         JSContextRef context,
177                                         JSObjectRef object,
178                                         JSStringRef propertyName,
179                                         JSValueRef* exception)
180 {
181     LogDebug("entered");
182     Try
183     {
184         Converter converter(context);
185         MediacontentMediaPtr media = getMediaObject(object);
186         return converter.toJSValueRef(media->getMimeType());
187     }
188     Catch(Exception)
189     {
190         LogWarning("trying to get incorrect value");
191     }
192     return JSValueMakeUndefined(context);
193
194 }
195
196                                         
197 JSValueRef JSMedia::getPropertyDisplayName(
198                                         JSContextRef context,
199                                         JSObjectRef object,
200                                         JSStringRef propertyName,
201                                         JSValueRef* exception)
202 {
203     LogDebug("getPropertyDisplayName::entered");
204     Try
205     {
206         Converter converter(context);
207         MediacontentMediaPtr media = getMediaObject(object);
208         LogDebug("getDisplayName:"<<media->getDisplayName());    
209         return converter.toJSValueRef(media->getDisplayName());
210     }
211     Catch(Exception)
212     {
213         LogWarning("trying to get incorrect value");
214     }
215     return JSValueMakeUndefined(context);
216 }
217
218 JSValueRef JSMedia::getPropertyTitle(
219                                         JSContextRef context,
220                                         JSObjectRef object,
221                                         JSStringRef propertyName,
222                                         JSValueRef* exception)
223 {
224     LogDebug("getPropertyDisplayName::entered");
225     Try
226     {
227         Converter converter(context);
228         MediacontentMediaPtr media = getMediaObject(object);
229                 LogDebug("getTitle:"<<media->getTitle());
230         return converter.toJSValueRef(media->getTitle());
231     }
232     Catch(Exception)
233     {
234         LogWarning("trying to get incorrect value");
235     }
236     return JSValueMakeUndefined(context);
237 }
238
239
240 JSValueRef JSMedia::getPropertyFilePath(
241                                         JSContextRef context,
242                                         JSObjectRef object,
243                                         JSStringRef propertyName,
244                                         JSValueRef* exception)
245 {
246     LogDebug("entered");
247     Try
248     {
249         Converter converter(context);
250         MediacontentMediaPtr media = getMediaObject(object);
251         return converter.toJSValueRef(media->getFilePath());
252     }
253     Catch(Exception)
254     {
255         LogWarning("trying to get incorrect value");
256     }
257     return JSValueMakeUndefined(context);
258 }
259
260
261 JSValueRef JSMedia::getPropertyThumbnailPath(
262                                         JSContextRef context,
263                                         JSObjectRef object, 
264                                         JSStringRef propertyName, 
265                                         JSValueRef* exception)
266 {
267     LogDebug("entered");
268     Try
269     {
270         Converter converter(context);
271         MediacontentMediaPtr media = getMediaObject(object);
272         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
273                 if (NULL == jsResult) 
274                 {
275                         ThrowMsg(NullPointerException, "Could not create js array object");
276                 }
277
278                 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
279                 if(!JSSetArrayElement(context, jsResult, 0, val)) 
280                 {
281                    ThrowMsg(UnknownException, "Could not insert value into js array");
282                 }
283
284                 return jsResult;      
285         
286     }
287     Catch(Exception)
288     {
289         LogWarning("trying to get incorrect value");
290     }
291     return JSValueMakeUndefined(context);
292 }
293
294 JSValueRef JSMedia::getPropertyDescription(
295                                         JSContextRef context,
296                                         JSObjectRef object, 
297                                         JSStringRef propertyName, 
298                                         JSValueRef* exception)
299 {
300     LogDebug("getPropertyDescription::entered");
301     Try
302     {
303         Converter converter(context);
304         MediacontentMediaPtr media = getMediaObject(object);
305         return converter.toJSValueRef(media->getDescription());
306     }
307     Catch(Exception)
308     {
309         LogWarning("trying to get incorrect value");
310     }
311     return JSValueMakeUndefined(context);
312 }
313
314
315 JSValueRef JSMedia::getPropertyModifiedDate(
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         if(media->getModifiedDate() != NULL)
327         {
328             return converter.toJSValueRef(*(media->getModifiedDate()));
329         }
330     }
331     Catch(Exception)
332     {
333         LogWarning("trying to get incorrect value");
334     }
335     return JSValueMakeUndefined(context);
336 }
337
338 JSValueRef JSMedia::getPropertyReleasedDate(
339                                         JSContextRef context,
340                                         JSObjectRef object,
341                                         JSStringRef propertyName,
342                                         JSValueRef* exception)
343 {
344     LogDebug("entered");
345     Try
346     {
347         Converter converter(context);
348         MediacontentMediaPtr media = getMediaObject(object);
349         if(media->getReleasedDate() != NULL)
350         {
351             return converter.toJSValueRef(*(media->getReleasedDate()));
352         }
353     }
354     Catch(Exception)
355     {
356         LogWarning("trying to get incorrect value");
357     }
358     return JSValueMakeUndefined(context);
359
360 }
361
362
363 JSValueRef JSMedia::getPropertyRating(
364                                         JSContextRef context,
365                                         JSObjectRef object, 
366                                         JSStringRef propertyName, 
367                                         JSValueRef* exception)
368 {
369     LogDebug("entered");
370     Try
371     {
372         Converter converter(context);
373         MediacontentMediaPtr objMedia = getMediaObject(object);
374         return converter.toJSValueRef(objMedia->getRating());
375     }
376     Catch(Exception)
377     {
378         LogWarning("trying to get incorrect value");
379     }
380     return JSValueMakeUndefined(context);
381 }
382
383 JSValueRef JSMedia::getPropertyType(
384                                         JSContextRef context,
385                                         JSObjectRef object,
386                                         JSStringRef propertyName,
387                                         JSValueRef* exception)
388 {
389     LogDebug("getPropertyType::entered");
390     Try
391     {
392         Converter converter(context);
393         MediacontentMediaPtr media = getMediaObject(object);
394         return converter.toJSValueRef(media->getMediaType());
395     }
396     Catch(Exception)
397     {
398         LogWarning("trying to get incorrect value");
399     }
400     return JSValueMakeUndefined(context);
401 }
402
403
404 JSValueRef JSMedia::getPropertySize(
405                                         JSContextRef context,
406                                         JSObjectRef object,
407                                         JSStringRef propertyName,
408                                         JSValueRef* exception)
409 {
410     LogDebug("getPropertyType::entered");
411     Try
412     {
413         Converter converter(context);
414         MediacontentMediaPtr media = getMediaObject(object);
415         LogDebug("size:" << media->getSize());
416         double var = (double)media->getSize(); //casting.
417         return converter.toJSValueRef(var);
418     }
419     Catch(Exception)
420     {
421         LogWarning("trying to get incorrect value");
422     }
423
424     return JSValueMakeUndefined(context);
425 }
426
427
428 JSValueRef JSMedia::getPropertyEditableAttr(
429                                         JSContextRef context,
430                                         JSObjectRef object,
431                                         JSStringRef propertyName,
432                                         JSValueRef* exception)
433 {
434     Try
435     {
436                 Converter converter(context);
437                 MediacontentMediaPtr media = getMediaObject(object);
438                 vector<std::string> editableAttrList = media->getEditableAttr();
439                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
440
441                         if (NULL == jsResult) 
442                         {
443                             ThrowMsg(NullPointerException, "Could not create js array object");
444                         }
445                 for(unsigned int i=0; i<editableAttrList.size(); i++) 
446                         {
447                         JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
448                             if(!JSSetArrayElement(context, jsResult, i, val)) 
449                             {
450                                ThrowMsg(UnknownException, "Could not insert value into js array");
451                             }
452                         }
453                 return jsResult;
454         }
455         Catch(Exception)
456         {
457                 LogWarning("trying to get incorrect value");
458         }
459         return JSValueMakeUndefined(context);
460
461 }
462
463
464 bool    JSMedia::setPropertyRating(
465                                 JSContextRef context,
466                                 JSObjectRef object,
467                                 JSStringRef propertyName,
468                                 JSValueRef value,
469                                 JSValueRef* exception)
470 {
471     LogDebug("entered");
472     Try
473     {
474         Converter converter(context);
475         MediacontentMediaPtr objMedia = getMediaObject(object);
476         int rating = converter.toInt(value);
477
478         LogDebug("Inserted value : " << rating);
479
480         if(rating < 0)
481         {
482                 rating = 0;
483                 LogDebug("Rating value set 0 as inserted value is too small");
484         }
485         
486         if ( objMedia->getRating() != rating)
487         {
488                 objMedia->setRating(rating, true);
489         }
490                 
491         return true;
492     }
493     Catch(Exception)
494     {
495         LogWarning("trying to set incorrect value");
496         DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
497     }
498
499     return false;
500
501 }
502
503 bool            JSMedia::setPropertyDisplayName(
504                                         JSContextRef context,
505                                         JSObjectRef object,
506                                         JSStringRef propertyName,
507                                         JSValueRef value,
508                                         JSValueRef* exception)
509 {
510         LogDebug("entered");
511         Try
512         {
513                 Converter converter(context);
514                 MediacontentMediaPtr objMedia = getMediaObject(object);
515                 string displayName = converter.toString(value);
516
517                 LogDebug("Inserted value : " << displayName);
518                 
519                 if ((objMedia->getDescription()).compare(displayName) != 0)
520                 {
521                         objMedia->setDisplayName(displayName, true);
522                 }
523                 
524                 return true;
525         }
526         Catch(Exception)
527         {
528                 LogWarning("trying to set incorrect value");
529                 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
530         }
531
532         return false;
533 }
534
535 bool            JSMedia::setPropertyDescription(
536                                         JSContextRef context,
537                                         JSObjectRef object,
538                                         JSStringRef propertyName,
539                                         JSValueRef value,
540                                         JSValueRef* exception)
541 {
542         LogDebug("entered");
543         Try
544         {
545                 Converter converter(context);
546                 MediacontentMediaPtr objMedia = getMediaObject(object);
547                 string description = converter.toString(value);
548
549                 LogDebug("Inserted value : " << description);
550                 
551                 if ( (objMedia->getDescription()).compare(description) != 0 )
552                 {
553                         objMedia->setDescription(description, true);
554                 }
555                 
556                 return true;
557         }
558         Catch(Exception)
559         {
560                 LogWarning("trying to set incorrect value");
561                 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
562         }
563
564         return false;
565 }
566
567 }
568 }