Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / MediaConverter.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/JSUtils.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/ScopedJSStringRef.h>
22
23 #include "MediaConverter.h"
24 #include "JSMedia.h"
25 #include "JSImage.h"
26 #include "JSVideo.h"
27 #include "JSAudio.h"
28 #include "JSFolder.h"
29 #include "JSMediacontent.h"
30 #include "JSMediaLyrics.h"
31
32
33
34 namespace TizenApis {
35 namespace Tizen1_0 {
36 namespace Mediacontent {
37
38 MediaConverter::MediaConverter(JSContextRef context) : Converter(context)
39 {
40     LogDebug("entered");
41 }
42
43 JSValueRef MediaConverter::toFunctionOrNull(const JSValueRef& arg)
44 {
45     if (Validator(m_context).isCallback(arg)) 
46         {
47         return arg;
48     } else if (!JSValueIsNull(m_context,arg) && !JSValueIsUndefined(m_context, arg)) 
49     {
50         ThrowMsg(ConversionException, "Not a function nor JS null.");
51     }
52     return NULL;
53 }
54
55 JSValueRef MediaConverter::toFunction(const JSValueRef& arg)
56 {
57     if (Validator(m_context).isCallback(arg)) 
58         {
59         return arg;
60     } else if (JSValueIsNull(m_context,arg) || JSValueIsUndefined(m_context, arg))
61     {
62         ThrowMsg(InvalidArgumentException, "JS null passed as function.");
63     }
64     ThrowMsg(ConversionException, "Not a function nor JS null.");
65 }
66
67 JSValueRef MediaConverter::toJSValueRefMediacontent(const IMediacontentPtr& arg)
68 {
69     MediacontentPrivObject *priv = new MediacontentPrivObject(m_context, arg);
70     return JSObjectMake(m_context, JSMediacontent::getClassRef(), priv);
71 }
72
73
74 JSValueRef MediaConverter::toJSValueRefMedia(const MediacontentMediaPtr& arg)
75 {
76         if(arg->getMediaType() == "IMAGE")
77         {
78             return JSUtils::makeObject(m_context, JSImage::getClassRef(), arg);
79         }
80         if(arg->getMediaType() == "VIDEO")
81         {
82             return JSUtils::makeObject(m_context, JSVideo::getClassRef(), arg);
83         }
84         if(arg->getMediaType() == "AUDIO")
85         {
86             return JSUtils::makeObject(m_context, JSAudio::getClassRef(), arg);
87         }
88         return JSUtils::makeObject(m_context, JSMedia::getClassRef(), arg);
89 }
90
91 JSValueRef MediaConverter::toJSValueRefFolder(const MediacontentFolderPtr& arg)
92 {
93     return JSUtils::makeObject(m_context, JSFolder::getClassRef(), arg);
94 }
95
96 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentMediaPtr> &arg)
97 {
98         return toJSValueRef_(arg, &MediaConverter::toJSValueRefMedia, this);
99 }
100
101 JSValueRef MediaConverter::toJSValueRef(const std::vector<MediacontentFolderPtr> &arg)
102 {
103         return toJSValueRef_(arg, &MediaConverter::toJSValueRefFolder, this);
104 }
105
106 JSValueRef MediaConverter::toJSValueRef(const MediacontentLyricsPtr &arg)
107 {
108         return JSUtils::makeObject(m_context, JSMediaLyrics::getClassRef(), arg);
109 }
110
111
112 }
113 }
114 }