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