Update change log and spec for wrt-plugins-tizen_0.4.15
[platform/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             ThrowMsg(NullPointerException, "Could not create js array object");
275         }
276
277         if(!(media->getThumbnailPath().empty())){
278             JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
279             if(!JSSetArrayElement(context, jsResult, 0, val)){
280                 ThrowMsg(UnknownException, "Could not insert value into js array");
281             }
282             return jsResult;
283         }
284     }
285     Catch(Exception)
286     {
287         LogWarning("trying to get incorrect value");
288     }
289     return JSValueMakeNull(context);
290 }
291
292 JSValueRef JSMedia::getPropertyDescription(
293                                         JSContextRef context,
294                                         JSObjectRef object, 
295                                         JSStringRef propertyName, 
296                                         JSValueRef* exception)
297 {
298     LogDebug("getPropertyDescription::entered");
299     Try
300     {
301         Converter converter(context);
302         MediacontentMediaPtr media = getMediaObject(object);
303         if(!(media->getDescription().empty()))
304         {
305             return converter.toJSValueRef(media->getDescription());
306         }
307     }
308     Catch(Exception)
309     {
310         LogWarning("trying to get incorrect value");
311     }
312     return JSValueMakeNull(context);
313 }
314
315
316 JSValueRef JSMedia::getPropertyModifiedDate(
317                                         JSContextRef context,
318                                         JSObjectRef object, 
319                                         JSStringRef propertyName, 
320                                         JSValueRef* exception)
321 {
322     LogDebug("entered");
323     Try
324     {
325         Converter converter(context);
326         MediacontentMediaPtr media = getMediaObject(object);
327         if(media->getModifiedDate() != NULL)
328         {
329             return converter.toJSValueRef(*(media->getModifiedDate()));
330         }
331     }
332     Catch(Exception)
333     {
334         LogWarning("trying to get incorrect value");
335     }
336     return JSValueMakeNull(context);
337 }
338
339 JSValueRef JSMedia::getPropertyReleasedDate(
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 media = getMediaObject(object);
350         if(media->getReleasedDate() != NULL)
351         {
352             return converter.toJSValueRef(*(media->getReleasedDate()));
353         }
354     }
355     Catch(Exception)
356     {
357         LogWarning("trying to get incorrect value");
358     }
359     return JSValueMakeNull(context);
360
361 }
362
363
364 JSValueRef JSMedia::getPropertyRating(
365                                         JSContextRef context,
366                                         JSObjectRef object, 
367                                         JSStringRef propertyName, 
368                                         JSValueRef* exception)
369 {
370     LogDebug("entered");
371     Try
372     {
373         Converter converter(context);
374         MediacontentMediaPtr objMedia = getMediaObject(object);
375         return converter.toJSValueRef(objMedia->getRating());
376     }
377     Catch(Exception)
378     {
379         LogWarning("trying to get incorrect value");
380     }
381     return JSValueMakeNull(context);
382 }
383
384 JSValueRef JSMedia::getPropertyType(
385                                         JSContextRef context,
386                                         JSObjectRef object,
387                                         JSStringRef propertyName,
388                                         JSValueRef* exception)
389 {
390     LogDebug("getPropertyType::entered");
391     Try
392     {
393         Converter converter(context);
394         MediacontentMediaPtr media = getMediaObject(object);
395         return converter.toJSValueRef(media->getMediaType());
396     }
397     Catch(Exception)
398     {
399         LogWarning("trying to get incorrect value");
400     }
401     return JSValueMakeUndefined(context);
402 }
403
404
405 JSValueRef JSMedia::getPropertySize(
406                                         JSContextRef context,
407                                         JSObjectRef object,
408                                         JSStringRef propertyName,
409                                         JSValueRef* exception)
410 {
411     LogDebug("getPropertyType::entered");
412     Try
413     {
414         Converter converter(context);
415         MediacontentMediaPtr media = getMediaObject(object);
416         LogDebug("size:" << media->getSize());
417         double var = (double)media->getSize(); //casting.
418         return converter.toJSValueRef(var);
419     }
420     Catch(Exception)
421     {
422         LogWarning("trying to get incorrect value");
423     }
424
425     return JSValueMakeNull(context);
426 }
427
428
429 JSValueRef JSMedia::getPropertyEditableAttr(
430                                         JSContextRef context,
431                                         JSObjectRef object,
432                                         JSStringRef propertyName,
433                                         JSValueRef* exception)
434 {
435     Try
436     {
437                 Converter converter(context);
438                 MediacontentMediaPtr media = getMediaObject(object);
439                 vector<std::string> editableAttrList = media->getEditableAttr();
440                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
441
442                         if (NULL == jsResult) 
443                         {
444                             ThrowMsg(NullPointerException, "Could not create js array object");
445                         }
446                 for(unsigned int i=0; i<editableAttrList.size(); i++) 
447                         {
448                         JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
449                             if(!JSSetArrayElement(context, jsResult, i, val)) 
450                             {
451                                ThrowMsg(UnknownException, "Could not insert value into js array");
452                             }
453                         }
454                 return jsResult;
455         }
456         Catch(Exception)
457         {
458                 LogWarning("trying to get incorrect value");
459         }
460         return JSValueMakeUndefined(context);
461
462 }
463
464
465 bool    JSMedia::setPropertyRating(
466                                 JSContextRef context,
467                                 JSObjectRef object,
468                                 JSStringRef propertyName,
469                                 JSValueRef value,
470                                 JSValueRef* exception)
471 {
472     LogDebug("entered");
473     Try
474     {
475         Converter converter(context);
476         MediacontentMediaPtr objMedia = getMediaObject(object);
477         int rating = converter.toInt(value);
478
479         LogDebug("Inserted value : " << rating);
480
481         if(rating < 0)
482         {
483                 rating = 0;
484                 LogDebug("Rating value set 0 as inserted value is too small");
485         }
486         
487         if ( objMedia->getRating() != rating)
488         {
489                 objMedia->setRating(rating, true);
490         }
491                 
492         return true;
493     }
494     Catch(Exception)
495     {
496         LogWarning("trying to set incorrect value");
497         DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
498     }
499
500     return false;
501
502 }
503
504 bool            JSMedia::setPropertyDisplayName(
505                                         JSContextRef context,
506                                         JSObjectRef object,
507                                         JSStringRef propertyName,
508                                         JSValueRef value,
509                                         JSValueRef* exception)
510 {
511         LogDebug("entered");
512         Try
513         {
514                 Converter converter(context);
515                 MediacontentMediaPtr objMedia = getMediaObject(object);
516                 string displayName = converter.toString(value);
517
518                 LogDebug("Inserted value : " << displayName);
519                 
520                 if ((objMedia->getDescription()).compare(displayName) != 0)
521                 {
522                         objMedia->setDisplayName(displayName, true);
523                 }
524                 
525                 return true;
526         }
527         Catch(Exception)
528         {
529                 LogWarning("trying to set incorrect value");
530                 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
531         }
532
533         return false;
534 }
535
536 bool            JSMedia::setPropertyDescription(
537                                         JSContextRef context,
538                                         JSObjectRef object,
539                                         JSStringRef propertyName,
540                                         JSValueRef value,
541                                         JSValueRef* exception)
542 {
543         LogDebug("entered");
544         Try
545         {
546                 Converter converter(context);
547                 MediacontentMediaPtr objMedia = getMediaObject(object);
548                 string description = converter.toString(value);
549
550                 LogDebug("Inserted value : " << description);
551                 
552                 if ( (objMedia->getDescription()).compare(description) != 0 )
553                 {
554                         objMedia->setDescription(description, true);
555                 }
556                 
557                 return true;
558         }
559         Catch(Exception)
560         {
561                 LogWarning("trying to set incorrect value");
562                 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
563         }
564
565         return false;
566 }
567
568 }
569 }