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