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