Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSAudio.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 #include "JSMediacontent.h"
26 #include "JSAudio.h"
27 #include "MediaConverter.h"
28
29
30 #define TIZEN_MEDIACONTENT_AUDIO_ATTRIBUTENAME          "Audio"
31 #define TIZEN_MEDIACONTENT_AUDIO_ALBUM                          "album"
32 #define TIZEN_MEDIACONTENT_AUDIO_GENRE                          "genres"
33 #define TIZEN_MEDIACONTENT_AUDIO_ARTIST                         "artists"
34 #define TIZEN_MEDIACONTENT_AUDIO_COMPOSER                       "composers"
35 #define TIZEN_MEDIACONTENT_AUDIO_LYIRICS                        "lyrics"
36 #define TIZEN_MEDIACONTENT_AUDIO_COPYRIGHT                      "copyright"
37 #define TIZEN_MEDIACONTENT_AUDIO_BITRATE                        "bitrate"
38 #define TIZEN_MEDIACONTENT_AUDIO_TRACKNUM                       "trackNumber"
39 #define TIZEN_MEDIACONTENT_AUDIO_DURATION                       "duration"
40 #define TIZEN_MEDIACONTENT_AUDIO_PLAYEDCOUNT            "playedCount"   
41 #define TIZEN_MEDIACONTENT_AUDIO_PLAYEDTIME                     "playedTime"    
42 #define TIZEN_MEDIACONTENT_AUDIO_SIZE                           "size"  
43
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
46 using namespace TizenApis::Api::Mediacontent;
47
48 namespace TizenApis {
49 namespace Tizen1_0 {
50 namespace Mediacontent {
51
52 JSClassDefinition JSAudio::m_classInfo =
53 {
54         0,
55         kJSClassAttributeNone,
56         TIZEN_MEDIACONTENT_AUDIO_ATTRIBUTENAME,
57         JSMedia::getClassRef(),
58         m_property,
59         NULL, //    m_function,
60         initialize,
61         finalize,
62         NULL, //hasProperty,
63         NULL, //getProperty,
64         NULL, //setProperty,
65         NULL, //DeleteProperty,
66         NULL, //GetPropertyNames,
67         NULL, //CallAsFunction,
68         NULL, //CallAsConstructor,
69         NULL, //HasInstance,
70         NULL  //ConvertToType
71 };
72
73 JSStaticValue JSAudio::m_property[] =
74 {
75         { TIZEN_MEDIACONTENT_AUDIO_ALBUM, getPropertyAlbum, NULL, kJSPropertyAttributeReadOnly},
76         { TIZEN_MEDIACONTENT_AUDIO_ARTIST, getPropertyArtist, NULL, kJSPropertyAttributeReadOnly},
77         { TIZEN_MEDIACONTENT_AUDIO_GENRE, getPropertyGenre, NULL, kJSPropertyAttributeReadOnly},
78         { TIZEN_MEDIACONTENT_AUDIO_COMPOSER , getPropertyComposer, NULL, kJSPropertyAttributeReadOnly},
79         { TIZEN_MEDIACONTENT_AUDIO_COPYRIGHT, getPropertyCopyright, NULL, kJSPropertyAttributeReadOnly},
80         { TIZEN_MEDIACONTENT_AUDIO_LYIRICS, getPropertyLyrics, NULL, kJSPropertyAttributeReadOnly},
81         { TIZEN_MEDIACONTENT_AUDIO_BITRATE, getPropertyBitrate, NULL, kJSPropertyAttributeReadOnly},    
82         { TIZEN_MEDIACONTENT_AUDIO_TRACKNUM, getPropertyTrackNum, NULL, kJSPropertyAttributeReadOnly},  
83         { TIZEN_MEDIACONTENT_AUDIO_DURATION, getPropertyDuration, NULL, kJSPropertyAttributeReadOnly},  
84         { TIZEN_MEDIACONTENT_AUDIO_PLAYEDCOUNT, getPropertyPlayedCount, setPropertyPlayCount, kJSPropertyAttributeNone},  
85         { TIZEN_MEDIACONTENT_AUDIO_PLAYEDTIME, getPropertyPlayedTime, setPropertyPlayedTime, kJSPropertyAttributeNone},  
86         { TIZEN_MEDIACONTENT_AUDIO_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},          
87         { 0, 0, 0, 0 }
88 };
89
90
91 JSClassRef JSAudio::m_jsClassRef = JSClassCreate(JSAudio::getClassInfo());
92
93 void JSAudio::initialize(JSContextRef context, JSObjectRef object)
94 {
95     LogDebug( "entered" );
96     AudioPrivObject *priv = static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) );
97     if (!priv) 
98     {
99         MediacontentAudioPtr privateData(new MediacontentAudio());
100         priv = new AudioPrivObject(context, privateData);
101         JSObjectSetPrivate(object, static_cast<void*>(priv));
102         LogDebug("new event is created" );
103     }
104     else {
105         LogDebug("private object already exists");
106     }
107 }
108
109 void JSAudio::finalize(JSObjectRef object)
110 {
111     LogDebug( "entered" );
112         AudioPrivObject *priv =  static_cast<AudioPrivObject*>( JSObjectGetPrivate( object ) ) ;
113     delete priv;
114 }
115
116 const JSClassRef JSAudio::getClassRef()
117 {
118         LogDebug("JSAudio::getClassRef()");
119
120     if (!m_jsClassRef) 
121     {
122             m_jsClassRef = JSClassCreate(&m_classInfo);
123     }
124     return m_jsClassRef;
125 }
126
127 const JSClassDefinition* JSAudio::getClassInfo()
128 {
129     return &m_classInfo;
130 }
131
132
133 MediacontentAudioPtr JSAudio::getAudioObject(JSObjectRef object)
134 {
135     LogDebug("entered");
136     AudioPrivObject *priv = static_cast<AudioPrivObject*>(JSObjectGetPrivate(object));
137     if(!priv) 
138     {
139         ThrowMsg(NullPointerException, "Private object is null");
140     }
141     MediacontentAudioPtr result = priv->getObject();
142     if (!result) 
143     {
144         ThrowMsg(NullPointerException, "Private object is null");
145     }
146     return result;
147 }
148                                         
149 JSValueRef JSAudio::getPropertyAlbum(
150                                 JSContextRef context,
151                         JSObjectRef object,
152                         JSStringRef propertyName,
153                         JSValueRef* exception)
154 {
155     LogDebug("entered");
156     Try
157     {
158         Converter converter(context);
159         MediacontentAudioPtr event = getAudioObject(object);
160         return converter.toJSValueRef(event->getAudioAlbum());
161     }
162     Catch(Exception)
163     {
164         LogWarning("trying to get incorrect value");
165     }
166     return JSValueMakeUndefined(context);
167 }
168
169
170
171 JSValueRef JSAudio::getPropertyArtist(
172                                 JSContextRef context,
173                                 JSObjectRef object,
174                                 JSStringRef propertyName,
175                                 JSValueRef* exception)
176 {
177     LogDebug("entered");
178     Try
179     {
180        Converter converter(context);
181         MediacontentAudioPtr event = getAudioObject(object);
182         return converter.toJSValueRef(event->getAudioArtist());
183     }
184     Catch(Exception)
185     {
186         LogWarning("trying to get incorrect value");
187     }
188     return JSValueMakeUndefined(context);
189 }
190
191
192
193 JSValueRef JSAudio::getPropertyGenre(
194                                 JSContextRef context,
195                         JSObjectRef object, 
196                         JSStringRef propertyName, 
197                         JSValueRef* exception)
198 {
199     LogDebug("entered");
200     Try
201     {
202         Converter converter(context);
203         MediacontentAudioPtr event = getAudioObject(object);
204         return converter.toJSValueRef(event->getAudioGenre());
205     }
206     Catch(Exception)
207     {
208         LogWarning("trying to get incorrect value");
209     }
210     return JSValueMakeUndefined(context);
211 }
212
213
214 JSValueRef JSAudio::getPropertyComposer(
215                                 JSContextRef context,
216                         JSObjectRef object, 
217                         JSStringRef propertyName, 
218                         JSValueRef* exception)
219 {
220     LogDebug("entered");
221     Try
222     {
223         Converter converter(context);
224         MediacontentAudioPtr event = getAudioObject(object);
225         return converter.toJSValueRef(event->getAudioComposer());
226     }
227     Catch(Exception)
228     {
229         LogWarning("trying to get incorrect value");
230     }
231     return JSValueMakeUndefined(context);
232 }
233
234 JSValueRef JSAudio::getPropertyCopyright(
235                                 JSContextRef context,
236                         JSObjectRef object, 
237                         JSStringRef propertyName, 
238                         JSValueRef* exception)
239 {
240     LogDebug("entered");
241     Try
242     {
243         Converter converter(context);
244         MediacontentAudioPtr event = getAudioObject(object);
245         return converter.toJSValueRef(event->getAudioCopyright());
246     }
247     Catch(Exception)
248     {
249         LogWarning("trying to get incorrect value");
250     }
251     return JSValueMakeUndefined(context);
252 }
253
254 JSValueRef JSAudio::getPropertyLyrics(
255                                 JSContextRef context,
256                         JSObjectRef object,
257                         JSStringRef propertyName,
258                         JSValueRef* exception)
259 {
260     LogDebug("entered");
261     Try
262     {
263                 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
264                 //MediaConverter converter(globalContext);
265
266         MediacontentAudioPtr event = getAudioObject(object);
267         //Todo. Lyrics
268         return converter->toJSValueRef(event->getAudioLyrics());
269     }
270     Catch(Exception)
271     {
272         LogWarning("trying to get incorrect value");
273     }
274     return JSValueMakeUndefined(context);
275 }
276
277 JSValueRef JSAudio::getPropertyBitrate(
278                                 JSContextRef context,
279                         JSObjectRef object,
280                         JSStringRef propertyName,
281                         JSValueRef* exception)
282 {
283     LogDebug("entered");
284     Try
285     {
286         Converter converter(context);
287         MediacontentAudioPtr event = getAudioObject(object);
288         return converter.toJSValueRef(event->getAudioBitrate());
289     }
290     Catch(Exception)
291     {
292         LogWarning("trying to get incorrect value");
293     }
294     return JSValueMakeUndefined(context);
295 }
296
297 JSValueRef JSAudio::getPropertyTrackNum(
298                                 JSContextRef context,
299                         JSObjectRef object,
300                         JSStringRef propertyName,
301                         JSValueRef* exception)
302 {
303     LogDebug("entered");
304     Try
305     {
306         Converter converter(context);
307         MediacontentAudioPtr event = getAudioObject(object);
308         return converter.toJSValueRef(event->getAudioTrackNum());
309     }
310     Catch(Exception)
311     {
312         LogWarning("trying to get incorrect value");
313     }
314     return JSValueMakeUndefined(context);
315 }
316
317 JSValueRef JSAudio::getPropertyDuration(
318                                 JSContextRef context,
319                         JSObjectRef object,
320                         JSStringRef propertyName,
321                         JSValueRef* exception)
322 {
323     LogDebug("entered");
324     Try
325     {
326         Converter converter(context);
327         MediacontentAudioPtr event = getAudioObject(object);
328         return converter.toJSValueRef(event->getAudioDuration());
329     }
330     Catch(Exception)
331     {
332         LogWarning("trying to get incorrect value");
333     }
334     return JSValueMakeUndefined(context);
335 }
336
337
338 JSValueRef JSAudio::getPropertyPlayedCount(
339                                 JSContextRef context,
340                         JSObjectRef object,
341                         JSStringRef propertyName,
342                         JSValueRef* exception)
343 {
344     LogDebug("entered");
345     Try
346     {
347         Converter converter(context);
348         MediacontentAudioPtr event = getAudioObject(object);
349         return converter.toJSValueRef(event->getAudioPlayCount());
350     }
351     Catch(Exception)
352     {
353         LogWarning("trying to get incorrect value");
354     }
355     return JSValueMakeUndefined(context);
356 }
357
358
359
360 JSValueRef JSAudio::getPropertyPlayedTime(
361                                 JSContextRef context,
362                         JSObjectRef object,
363                         JSStringRef propertyName,
364                         JSValueRef* exception)
365 {
366     LogDebug("entered");
367     Try
368     {
369         Converter converter(context);
370         MediacontentAudioPtr event = getAudioObject(object);
371         return converter.toJSValueRef(event->getAudioPlayedTime());
372     }
373     Catch(Exception)
374     {
375         LogWarning("trying to get incorrect value");
376     }
377     return JSValueMakeUndefined(context);
378 }
379
380 JSValueRef JSAudio::getPropertySize(
381                                 JSContextRef context,
382                         JSObjectRef object,
383                         JSStringRef propertyName,
384                         JSValueRef* exception)
385 {
386     LogDebug("entered");
387     Try
388     {
389         Converter converter(context);
390         MediacontentAudioPtr event = getAudioObject(object);
391         return converter.toJSValueRef(event->getAudioSize());
392     }
393     Catch(Exception)
394     {
395         LogWarning("trying to get incorrect value");
396     }
397     return JSValueMakeUndefined(context);
398 }
399
400
401
402 bool JSAudio::setPropertyPlayedTime(
403                                         JSContextRef context,
404                                         JSObjectRef object,
405                                         JSStringRef propertyName,
406                                         JSValueRef value,
407                                         JSValueRef* exception)
408 {
409     LogDebug("entered");
410     Try
411     {
412         Converter converter(context);
413         MediacontentAudioPtr event = getAudioObject(object);
414         int rating = converter.toLong(value);
415         event->setAudioPlayedTime(rating);
416         return true;
417     }
418     Catch(Exception)
419     {
420         LogWarning("trying to set incorrect value");
421         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
422     }
423
424     return false;
425
426
427 }
428 bool JSAudio::setPropertyPlayCount(
429                                         JSContextRef context,
430                                         JSObjectRef object,
431                                         JSStringRef propertyName,
432                                         JSValueRef value,
433                                         JSValueRef* exception)
434 {
435     LogDebug("entered");
436     Try
437     {
438         Converter converter(context);
439         MediacontentAudioPtr event = getAudioObject(object);
440         int rating = converter.toInt(value);
441         event->setAudioPlayCount(rating);
442         return true;
443     }
444     Catch(Exception)
445     {
446         LogWarning("trying to set incorrect value");
447         TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
448     }
449
450     return false;
451
452 }
453
454
455 }
456 }
457 }