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