2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <CommonsJavaScript/PrivateObject.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include "JSAudioLyrics.h"
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"
34 JSClassDefinition JSMediaLyrics::m_classInfo =
37 kJSClassAttributeNone,
38 TIZEN_CONTENT_MEDIA_LYRICS,
47 NULL, //DeleteProperty,
48 NULL, //GetPropertyNames,
49 NULL, //CallAsFunction,
50 NULL, //CallAsConstructor,
55 JSStaticValue JSMediaLyrics::m_property[] =
58 { TIZEN_CONTENT_LYRICS_TYPE, getPropertyLyricsType, NULL, kJSPropertyAttributeReadOnly},
59 { TIZEN_CONTENT_LYRICS_TIMESTAMP, getPropertyTimeStamps, NULL, kJSPropertyAttributeReadOnly},
60 { TIZEN_CONTENT_LYRICS_TEXT, getPropertyTexts, NULL, kJSPropertyAttributeReadOnly},
65 JSClassRef JSMediaLyrics::m_jsClassRef = JSClassCreate(JSMediaLyrics::getClassInfo());
67 void JSMediaLyrics::initialize(JSContextRef context, JSObjectRef object)
70 LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) );
73 MediacontentLyricsPtr privateData(new MediacontentLyrics());
74 priv = new LyricsPrivObject(context, privateData);
75 JSObjectSetPrivate(object, static_cast<void*>(priv));
76 LoggerD("private object is created");
80 LoggerD("private object already exists");
84 void JSMediaLyrics::finalize(JSObjectRef object)
87 LyricsPrivObject *priv = static_cast<LyricsPrivObject*>( JSObjectGetPrivate( object ) ) ;
91 const JSClassRef JSMediaLyrics::getClassRef()
93 LoggerD("JSMediaLyrics::getClassRef()");
96 m_jsClassRef = JSClassCreate(&m_classInfo);
101 const JSClassDefinition* JSMediaLyrics::getClassInfo()
106 MediacontentLyricsPtr JSMediaLyrics::getLyricsObject(JSObjectRef object)
109 LyricsPrivObject *priv = static_cast<LyricsPrivObject*>(JSObjectGetPrivate(object));
112 ThrowMsg(NullPointerException, "Private object is null");
114 MediacontentLyricsPtr result = priv->getObject();
117 ThrowMsg(NullPointerException, "Private object is null");
123 JSValueRef JSMediaLyrics::getPropertyLyricsType(
124 JSContextRef context,
126 JSStringRef propertyName,
127 JSValueRef* exception)
132 Converter converter(context);
134 MediacontentLyricsPtr lyrics = getLyricsObject(object);
136 return converter.toJSValueRef(lyrics->getMediaLyricsType());
140 LoggerW("trying to get incorrect value");
143 return JSValueMakeUndefined(context);
147 JSValueRef JSMediaLyrics::getPropertyTimeStamps(
148 JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
156 Converter converter(context);
157 MediacontentLyricsPtr lyrics = getLyricsObject(object);
159 if ( 0 == (lyrics->getMediaLyricsType()).compare("SYNCHRONIZED") )
162 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
163 vector<unsigned long> timeStamps = lyrics->getMediaLyricsTimeStamps();
164 if(timeStamps.size() > 0)
166 if (NULL == jsResult)
168 ThrowMsg(NullPointerException, "Could not create js array object");
170 for(unsigned int i=0; i<timeStamps.size(); i++)
172 JSValueRef val = converter.toJSValueRef(timeStamps.at(i));
173 if(!JSSetArrayElement(context, jsResult, i, val))
175 ThrowMsg(UnknownException, "Could not insert value into js array");
186 LoggerW("trying to get incorrect value");
189 return JSValueMakeUndefined(context);
193 JSValueRef JSMediaLyrics::getPropertyTexts(
194 JSContextRef context,
196 JSStringRef propertyName,
197 JSValueRef* exception)
202 Converter converter(context);
203 MediacontentLyricsPtr lyrics = getLyricsObject(object);
204 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
205 vector<string> texts = lyrics->getMediaLyricsTexts();
208 if (NULL == jsResult)
210 ThrowMsg(NullPointerException, "Could not create js array object");
212 for(unsigned int i=0; i<texts.size(); i++)
214 JSValueRef val = converter.toJSValueRef(texts.at(i));
215 if(!JSSetArrayElement(context, jsResult, i, val))
217 ThrowMsg(UnknownException, "Could not insert value into js array");
226 LoggerW("trying to get incorrect value");
229 return JSValueMakeUndefined(context);
233 JSObjectRef JSMediaLyrics::createJSObject(JSContextRef context,
234 const MediacontentLyricsPtr mediaContent)
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");