wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Content / JSAudioLyrics.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <CommonsJavaScript/PrivateObject.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22
23 #include "JSAudioLyrics.h"
24 #include <Logger.h>
25
26 namespace DeviceAPI {
27 namespace Content {
28
29 #define TIZEN_CONTENT_MEDIA_LYRICS      "AudioContentLyrics"
30 #define TIZEN_CONTENT_LYRICS_TYPE       "type"
31 #define TIZEN_CONTENT_LYRICS_TIMESTAMP  "timestamps"
32 #define TIZEN_CONTENT_LYRICS_TEXT       "texts"
33
34 JSClassDefinition JSMediaLyrics::m_classInfo =
35 {
36     0,
37     kJSClassAttributeNone,
38     TIZEN_CONTENT_MEDIA_LYRICS,
39     0,
40     m_property,
41     NULL, //    m_function,
42     initialize,
43     finalize,
44     NULL, //hasProperty,
45     NULL, //getProperty,
46     NULL, //setProperty,
47     NULL, //DeleteProperty,
48     NULL, //GetPropertyNames,
49     NULL, //CallAsFunction,
50     NULL, //CallAsConstructor,
51     NULL, //HasInstance,
52     NULL  //ConvertToType
53 };
54
55 JSStaticValue JSMediaLyrics::m_property[] =
56 {
57     //EventProperties
58     { TIZEN_CONTENT_LYRICS_TYPE, getPropertyLyricsType, NULL, kJSPropertyAttributeReadOnly},
59     { TIZEN_CONTENT_LYRICS_TIMESTAMP, getPropertyTimeStamps, NULL, kJSPropertyAttributeReadOnly},
60     { TIZEN_CONTENT_LYRICS_TEXT, getPropertyTexts, NULL, kJSPropertyAttributeReadOnly},    
61     { 0, 0, 0, 0 }
62 };
63
64
65 JSClassRef JSMediaLyrics::m_jsClassRef = JSClassCreate(JSMediaLyrics::getClassInfo());
66
67 void JSMediaLyrics::initialize(JSContextRef context, JSObjectRef object)
68 {
69     LoggerD( "entered" );
70     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) );
71     if (!priv) 
72         {
73         MediacontentLyricsPtr privateData(new MediacontentLyrics());
74         priv = new LyricsPrivObject(context, privateData);
75         JSObjectSetPrivate(object, static_cast<void*>(priv));
76         LoggerD("private object is created");
77     }
78     else 
79         {
80         LoggerD("private object already exists");
81     }
82 }
83
84 void JSMediaLyrics::finalize(JSObjectRef object)
85 {
86     LoggerD( "entered" );
87     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) ) ;
88     delete priv;
89 }
90
91 const JSClassRef JSMediaLyrics::getClassRef()
92 {
93         LoggerD("JSMediaLyrics::getClassRef()");
94     if (!m_jsClassRef) 
95         {
96             m_jsClassRef = JSClassCreate(&m_classInfo);
97     }
98     return m_jsClassRef;
99 }
100
101 const JSClassDefinition* JSMediaLyrics::getClassInfo()
102 {
103     return &m_classInfo;
104 }
105
106 MediacontentLyricsPtr JSMediaLyrics::getLyricsObject(JSObjectRef object)
107 {
108     LoggerD("entered");
109     LyricsPrivObject *priv = static_cast<LyricsPrivObject*>(JSObjectGetPrivate(object));
110     if(!priv) 
111         {
112         ThrowMsg(NullPointerException, "Private object is null");
113     }
114     MediacontentLyricsPtr result = priv->getObject();
115     if (!result) 
116         {
117         ThrowMsg(NullPointerException, "Private object is null");
118     }
119     return result;
120 }
121
122
123 JSValueRef      JSMediaLyrics::getPropertyLyricsType(
124                                         JSContextRef context,
125                                         JSObjectRef object,
126                                         JSStringRef propertyName,
127                                         JSValueRef* exception)
128 {
129     LoggerD("entered");
130     Try
131     {
132                 Converter converter(context);
133
134         MediacontentLyricsPtr lyrics = getLyricsObject(object);
135
136         return converter.toJSValueRef(lyrics->getMediaLyricsType());
137     }
138     Catch(Exception)
139     {
140         LoggerW("trying to get incorrect value");
141     }
142
143     return JSValueMakeUndefined(context);
144
145 }
146
147 JSValueRef      JSMediaLyrics::getPropertyTimeStamps(
148                                         JSContextRef context,
149                                         JSObjectRef object,
150                                         JSStringRef propertyName,
151                                         JSValueRef* exception)
152 {
153     LoggerD("entered");
154     Try
155     {
156         Converter converter(context);
157         MediacontentLyricsPtr lyrics = getLyricsObject(object);
158         
159         if ( 0 == (lyrics->getMediaLyricsType()).compare("SYNCHRONIZED") )
160         {
161         
162             JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
163             vector<unsigned long> timeStamps = lyrics->getMediaLyricsTimeStamps();
164             if(timeStamps.size() > 0)
165             {
166                 if (NULL == jsResult) 
167                 {
168                     ThrowMsg(NullPointerException, "Could not create js array object");
169                 }
170                 for(unsigned int i=0; i<timeStamps.size(); i++) 
171                 {
172                     JSValueRef val = converter.toJSValueRef(timeStamps.at(i));
173                     if(!JSSetArrayElement(context, jsResult, i, val)) 
174                     {
175                        ThrowMsg(UnknownException, "Could not insert value into js array");
176                     }
177                 }
178                 
179                 return jsResult;
180             }
181        }
182
183     }
184     Catch(Exception)
185     {
186         LoggerW("trying to get incorrect value");
187     }
188
189     return JSValueMakeUndefined(context);
190
191 }
192
193 JSValueRef      JSMediaLyrics::getPropertyTexts(
194                                         JSContextRef context,
195                                         JSObjectRef object,
196                                         JSStringRef propertyName,
197                                         JSValueRef* exception)
198 {
199     LoggerD("entered");
200     Try
201     {
202                 Converter converter(context);
203         MediacontentLyricsPtr lyrics = getLyricsObject(object);
204                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
205                 vector<string> texts = lyrics->getMediaLyricsTexts();
206                 if(texts.size() >0)
207                 {
208                         if (NULL == jsResult) 
209                         {
210                 ThrowMsg(NullPointerException, "Could not create js array object");
211             }
212             for(unsigned int i=0; i<texts.size(); i++) 
213             {
214                 JSValueRef val = converter.toJSValueRef(texts.at(i));
215                 if(!JSSetArrayElement(context, jsResult, i, val)) 
216                 {
217                    ThrowMsg(UnknownException, "Could not insert value into js array");
218                 }
219             }
220                 }
221
222         return jsResult;
223     }
224     Catch(Exception)
225     {
226         LoggerW("trying to get incorrect value");
227     }
228
229     return JSValueMakeUndefined(context);
230
231 }
232
233 JSObjectRef JSMediaLyrics::createJSObject(JSContextRef context, 
234                 const MediacontentLyricsPtr mediaContent)
235 {
236         LyricsPrivObject *priv = new LyricsPrivObject(context, mediaContent);
237         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
238         if (NULL == jsObjectRef) {
239                 LoggerE("object creation error");
240                 return NULL;
241         }
242         return jsObjectRef;
243 }
244
245
246 }
247 }
248