Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSMedia.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24
25
26 #include "JSMediacontent.h"
27 #include "JSMedia.h"
28
29 namespace TizenApis {
30 namespace Tizen1_0 {
31 namespace Mediacontent {
32
33 #define TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME          "MediaItem"
34 #define TIZEN_MEDIACONTENT_MEDIA_UID                            "id"
35 #define TIZEN_MEDIACONTENT_MEDIA_TYPE                           "type"
36 #define TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE                      "mimeType"
37 #define TIZEN_MEDIACONTENT_MEDIA_TITLE                          "title"
38 #define TIZEN_MEDIACONTENT_MEDIA_FILE_URL                       "fileURL"
39 #define TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH          "thumbnailPath"
40 #define TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE            "createdDate"   
41 #define TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE           "releasedDate"  
42 #define TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE           "modifiedDate"  
43 #define TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION            "description"   
44 #define TIZEN_MEDIACONTENT_MEDIA_RATING                         "rating"        
45
46
47 JSClassDefinition JSMedia::m_classInfo =
48 {
49     0,
50     kJSClassAttributeNone,
51     TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME,
52     0,
53     m_property,
54     NULL, //    m_function,
55     initialize,
56     finalize,
57     NULL, //hasProperty,
58     NULL, //getProperty,
59     NULL, //setProperty,
60     NULL, //DeleteProperty,
61     NULL, //GetPropertyNames,
62     NULL, //CallAsFunction,
63     NULL, //CallAsConstructor,
64     NULL, //HasInstance,
65     NULL  //ConvertToType
66 };
67
68
69 JSStaticValue JSMedia::m_property[] =
70 {
71     //EventProperties
72     { TIZEN_MEDIACONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
73     { TIZEN_MEDIACONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},    
75     { TIZEN_MEDIACONTENT_MEDIA_TITLE, getPropertyDisplayName, NULL, kJSPropertyAttributeReadOnly},
76     { TIZEN_MEDIACONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
77     { TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
78     { TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE, getPropertyCreatedDate, NULL, kJSPropertyAttributeReadOnly},
79     { TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},    
80     { TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
81     { TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION, getPropertyDescription, NULL, kJSPropertyAttributeReadOnly},    
82     { TIZEN_MEDIACONTENT_MEDIA_RATING, getPropertyFavorite, setPropertyFavorite, 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     LogDebug( "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         LogDebug("new event is created" );
99     }
100     else 
101         {
102         LogDebug("private object already exists");
103     }
104 }
105
106 void JSMedia::finalize(JSObjectRef object)
107 {
108     LogDebug( "entered" );
109     MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
110     delete priv;
111 }
112
113 const JSClassRef JSMedia::getClassRef()
114 {
115         LogDebug("JSMedia::getClassRef()");
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     LogDebug("entered");
131     MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
132     if(!priv) 
133         {
134         ThrowMsg(NullPointerException, "Private object is null");
135     }
136     MediacontentMediaPtr result = priv->getObject();
137     if (!result) 
138         {
139         ThrowMsg(NullPointerException, "Private object is null");
140     }
141     return result;
142 }
143
144 JSValueRef JSMedia::getPropertyId(
145                                         JSContextRef context, 
146                                         JSObjectRef object,
147                                         JSStringRef propertyName, 
148                                         JSValueRef* exception)
149 {
150     LogDebug("entered");
151     Try
152     {
153         Converter converter(context);
154         MediacontentMediaPtr event = getMediaObject(object);
155
156         return converter.toJSValueRef(event->getMediaUUID());
157     }
158     Catch(Exception)
159     {
160         LogWarning("trying to get incorrect value");
161     }
162     return JSValueMakeUndefined(context);
163
164 }
165
166 JSValueRef      JSMedia::getPropertyMimeType(
167                                         JSContextRef context,
168                                         JSObjectRef object,
169                                         JSStringRef propertyName,
170                                         JSValueRef* exception)
171 {
172     LogDebug("entered");
173     Try
174     {
175         Converter converter(context);
176         MediacontentMediaPtr event = getMediaObject(object);
177         return converter.toJSValueRef(event->getMimeType());
178     }
179     Catch(Exception)
180     {
181         LogWarning("trying to get incorrect value");
182     }
183     return JSValueMakeUndefined(context);
184
185 }
186
187                                         
188 JSValueRef JSMedia::getPropertyDisplayName(
189                                         JSContextRef context,
190                                         JSObjectRef object,
191                                         JSStringRef propertyName,
192                                         JSValueRef* exception)
193 {
194     LogDebug("getPropertyDisplayName::entered");
195     Try
196     {
197         Converter converter(context);
198         MediacontentMediaPtr event = getMediaObject(object);
199         LogDebug("getDisplayName:"<<event->getDisplayName());    
200         return converter.toJSValueRef(event->getDisplayName());
201     }
202     Catch(Exception)
203     {
204         LogWarning("trying to get incorrect value");
205     }
206     return JSValueMakeUndefined(context);
207 }
208
209
210 JSValueRef JSMedia::getPropertyFilePath(
211                                         JSContextRef context,
212                                         JSObjectRef object,
213                                         JSStringRef propertyName,
214                                         JSValueRef* exception)
215 {
216     LogDebug("entered");
217     Try
218     {
219         Converter converter(context);
220         MediacontentMediaPtr event = getMediaObject(object);
221         return converter.toJSValueRef(event->getFilePath());
222     }
223     Catch(Exception)
224     {
225         LogWarning("trying to get incorrect value");
226     }
227     return JSValueMakeUndefined(context);
228 }
229
230
231 JSValueRef JSMedia::getPropertyThumbnailPath(
232                                         JSContextRef context,
233                                         JSObjectRef object, 
234                                         JSStringRef propertyName, 
235                                         JSValueRef* exception)
236 {
237     LogDebug("entered");
238     Try
239     {
240         Converter converter(context);
241         MediacontentMediaPtr event = getMediaObject(object);
242         return converter.toJSValueRef(event->getThumbnailPath());
243     }
244     Catch(Exception)
245     {
246         LogWarning("trying to get incorrect value");
247     }
248     return JSValueMakeUndefined(context);
249 }
250
251 JSValueRef JSMedia::getPropertyDescription(
252                                         JSContextRef context,
253                                         JSObjectRef object, 
254                                         JSStringRef propertyName, 
255                                         JSValueRef* exception)
256 {
257     LogDebug("entered");
258     Try
259     {
260         Converter converter(context);
261         MediacontentMediaPtr event = getMediaObject(object);
262         return converter.toJSValueRef(event->getDescription());
263     }
264     Catch(Exception)
265     {
266         LogWarning("trying to get incorrect value");
267     }
268     return JSValueMakeUndefined(context);
269 }
270
271
272
273 JSValueRef JSMedia::getPropertyCreatedDate(
274                                         JSContextRef context,
275                                         JSObjectRef object,
276                                         JSStringRef propertyName,
277                                         JSValueRef* exception)
278 {
279     LogDebug("entered");
280     Try
281     {
282         Converter converter(context);
283         MediacontentMediaPtr event = getMediaObject(object);
284         return converter.toJSValueRef(event->getCreatedDate());
285     }
286     Catch(Exception)
287     {
288         LogWarning("trying to get incorrect value");
289     }
290     return JSValueMakeUndefined(context);
291
292 }
293
294 JSValueRef JSMedia::getPropertyModifiedDate(
295                                         JSContextRef context,
296                                         JSObjectRef object, 
297                                         JSStringRef propertyName, 
298                                         JSValueRef* exception)
299 {
300     LogDebug("entered");
301     Try
302     {
303         Converter converter(context);
304         MediacontentMediaPtr event = getMediaObject(object);
305         return converter.toJSValueRef(event->getModifiedDate());
306     }
307     Catch(Exception)
308     {
309         LogWarning("trying to get incorrect value");
310     }
311     return JSValueMakeUndefined(context);
312 }
313
314 JSValueRef JSMedia::getPropertyReleasedDate(
315                                         JSContextRef context,
316                                         JSObjectRef object,
317                                         JSStringRef propertyName,
318                                         JSValueRef* exception)
319 {
320     LogDebug("entered");
321     Try
322     {
323         Converter converter(context);
324         MediacontentMediaPtr event = getMediaObject(object);
325         return converter.toJSValueRef(event->getReleasedDate());
326     }
327     Catch(Exception)
328     {
329         LogWarning("trying to get incorrect value");
330     }
331     return JSValueMakeUndefined(context);
332
333 }
334
335
336 JSValueRef JSMedia::getPropertyFavorite(
337                                         JSContextRef context,
338                                         JSObjectRef object, 
339                                         JSStringRef propertyName, 
340                                         JSValueRef* exception)
341 {
342     LogDebug("entered");
343     Try
344     {
345         Converter converter(context);
346         MediacontentMediaPtr event = getMediaObject(object);
347         return converter.toJSValueRef(event->getFavorite());
348     }
349     Catch(Exception)
350     {
351         LogWarning("trying to get incorrect value");
352     }
353     return JSValueMakeUndefined(context);
354 }
355
356 JSValueRef JSMedia::getPropertyType(
357                                         JSContextRef context,
358                                         JSObjectRef object,
359                                         JSStringRef propertyName,
360                                         JSValueRef* exception)
361 {
362     LogDebug("getPropertyType::entered");
363     Try
364     {
365         Converter converter(context);
366         MediacontentMediaPtr event = getMediaObject(object);
367         return converter.toJSValueRef(event->getMediaType());
368     }
369     Catch(Exception)
370     {
371         LogWarning("trying to get incorrect value");
372     }
373     return JSValueMakeUndefined(context);
374 }
375
376
377 bool    JSMedia::setPropertyFavorite(
378                                 JSContextRef context,
379                                 JSObjectRef object,
380                                 JSStringRef propertyName,
381                                 JSValueRef value,
382                                 JSValueRef* exception)
383 {
384     LogDebug("entered");
385     Try
386     {
387         Converter converter(context);
388         MediacontentMediaPtr event = getMediaObject(object);
389             int rating = converter.toInt(value);
390
391             LogDebug("value : " << rating);
392         event->setFavorite(rating);
393         return true;
394     }
395     Catch(Exception)
396     {
397         LogWarning("trying to set incorrect value");
398         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
399     }
400
401     return false;
402
403 }
404                                         
405
406
407 }
408 }
409 }