Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSMediaLyrics.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 <CommonsJavaScript/JSDOMExceptionFactory.h>
23
24 #include "JSMediacontent.h"
25 #include "JSMediaLyrics.h"
26
27 namespace TizenApis {
28 namespace Tizen1_0 {
29 namespace Mediacontent {
30
31 #define TIZEN_MEDIACONTENT_MEDIA_LYRICS         "MediaLyrics"
32 #define TIZEN_MEDIACONTENT_LYRICS_TYPE          "type"
33 #define TIZEN_MEDIACONTENT_LYRICS_TIMESTAMP "timeStamps"
34 #define TIZEN_MEDIACONTENT_LYRICS_TEXT          "texts"
35
36
37
38
39
40
41 JSClassDefinition JSMediaLyrics::m_classInfo =
42 {
43     0,
44     kJSClassAttributeNone,
45     TIZEN_MEDIACONTENT_MEDIA_LYRICS,
46     0,
47     m_property,
48     NULL, //    m_function,
49     initialize,
50     finalize,
51     NULL, //hasProperty,
52     NULL, //getProperty,
53     NULL, //setProperty,
54     NULL, //DeleteProperty,
55     NULL, //GetPropertyNames,
56     NULL, //CallAsFunction,
57     NULL, //CallAsConstructor,
58     NULL, //HasInstance,
59     NULL  //ConvertToType
60 };
61
62 JSStaticValue JSMediaLyrics::m_property[] =
63 {
64     //EventProperties
65     { TIZEN_MEDIACONTENT_LYRICS_TYPE, getPropertyLyricsType, NULL, kJSPropertyAttributeReadOnly},
66     { TIZEN_MEDIACONTENT_LYRICS_TIMESTAMP, getPropertyTimeStamps, NULL, kJSPropertyAttributeReadOnly},
67     { TIZEN_MEDIACONTENT_LYRICS_TEXT, getPropertyTexts, NULL, kJSPropertyAttributeReadOnly},    
68     { 0, 0, 0, 0 }
69 };
70
71
72 JSClassRef JSMediaLyrics::m_jsClassRef = JSClassCreate(JSMediaLyrics::getClassInfo());
73
74 void JSMediaLyrics::initialize(JSContextRef context, JSObjectRef object)
75 {
76     LogDebug( "entered" );
77     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) );
78     if (!priv) 
79         {
80         MediacontentLyricsPtr privateData(new MediacontentLyrics());
81         priv = new LyricsPrivObject(context, privateData);
82         JSObjectSetPrivate(object, static_cast<void*>(priv));
83         LogDebug("private object is created");
84     }
85     else 
86         {
87         LogDebug("private object already exists");
88     }
89 }
90
91 void JSMediaLyrics::finalize(JSObjectRef object)
92 {
93     LogDebug( "entered" );
94     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) ) ;
95     delete priv;
96 }
97
98 const JSClassRef JSMediaLyrics::getClassRef()
99 {
100         LogDebug("JSMediaLyrics::getClassRef()");
101     if (!m_jsClassRef) 
102         {
103             m_jsClassRef = JSClassCreate(&m_classInfo);
104     }
105     return m_jsClassRef;
106 }
107
108 const JSClassDefinition* JSMediaLyrics::getClassInfo()
109 {
110     return &m_classInfo;
111 }
112
113 MediacontentLyricsPtr JSMediaLyrics::getLyricsObject(JSObjectRef object)
114 {
115     LogDebug("entered");
116     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>(JSObjectGetPrivate(object));
117     if(!priv) 
118         {
119         ThrowMsg(NullPointerException, "Private object is null");
120     }
121     MediacontentLyricsPtr result = priv->getObject();
122     if (!result) 
123         {
124         ThrowMsg(NullPointerException, "Private object is null");
125     }
126     return result;
127 }
128
129
130 JSValueRef      JSMediaLyrics::getPropertyLyricsType(
131                                         JSContextRef context,
132                                         JSObjectRef object,
133                                         JSStringRef propertyName,
134                                         JSValueRef* exception)
135 {
136     LogDebug("entered");
137     Try
138     {
139                 Converter converter(context);
140
141         MediacontentLyricsPtr lyrics = getLyricsObject(object);
142
143         return converter.toJSValueRef(lyrics->getMediaLyricsType());
144     }
145     Catch(Exception)
146     {
147         LogWarning("trying to get incorrect value");
148     }
149
150     return JSValueMakeUndefined(context);
151
152 }
153
154 JSValueRef      JSMediaLyrics::getPropertyTimeStamps(
155                                         JSContextRef context,
156                                         JSObjectRef object,
157                                         JSStringRef propertyName,
158                                         JSValueRef* exception)
159 {
160     LogDebug("entered");
161     Try
162     {
163         Converter converter(context);
164         MediacontentLyricsPtr lyrics = getLyricsObject(object);
165         
166                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
167                 vector<unsigned long> timeStamps = lyrics->getMediaLyricsTimeStamps();
168                 if(timeStamps.size() > 0)
169                 {
170                         if (NULL == jsResult) 
171                         {
172                 ThrowMsg(NullPointerException, "Could not create js array object");
173             }
174             for(unsigned int i=0; i<timeStamps.size(); i++) 
175             {
176                 JSValueRef val = converter.toJSValueRef(timeStamps.at(i));
177                 if(!JSSetArrayElement(context, jsResult, i, val)) 
178                 {
179                    ThrowMsg(UnknownException, "Could not insert value into js array");
180                 }
181             }
182                 }
183         return jsResult;
184
185     }
186     Catch(Exception)
187     {
188         LogWarning("trying to get incorrect value");
189     }
190
191     return JSValueMakeUndefined(context);
192
193 }
194
195 JSValueRef      JSMediaLyrics::getPropertyTexts(
196                                         JSContextRef context,
197                                         JSObjectRef object,
198                                         JSStringRef propertyName,
199                                         JSValueRef* exception)
200 {
201     LogDebug("entered");
202     Try
203     {
204                 Converter converter(context);
205         MediacontentLyricsPtr lyrics = getLyricsObject(object);
206                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
207                 vector<string> texts = lyrics->getMediaLyricsTexts();
208                 if(texts.size() >0)
209                 {
210                         if (NULL == jsResult) 
211                         {
212                 ThrowMsg(NullPointerException, "Could not create js array object");
213             }
214             for(unsigned int i=0; i<texts.size(); i++) 
215             {
216                 JSValueRef val = converter.toJSValueRef(texts.at(i));
217                 if(!JSSetArrayElement(context, jsResult, i, val)) 
218                 {
219                    ThrowMsg(UnknownException, "Could not insert value into js array");
220                 }
221             }
222                 }
223
224         return jsResult;
225     }
226     Catch(Exception)
227     {
228         LogWarning("trying to get incorrect value");
229     }
230
231     return JSValueMakeUndefined(context);
232
233 }
234
235
236 }
237 }
238 }