2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file JSContactWebSite.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactWebSite class
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactWebSite.h"
31 #define CONTACT_CLASS_NAME "ContactWebSite"
33 #define CONTACT_ATTR_URL "url"
34 #define CONTACT_ATTR_TYPE "type"
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
43 JSClassRef JSContactWebSite::m_classRef = NULL;
45 JSClassDefinition JSContactWebSite::m_classInfo =
48 kJSClassAttributeNone,
58 NULL, //DeleteProperty,
59 NULL, //getPropertyNames,
63 NULL, //ConvertToType,
66 JSStaticValue JSContactWebSite::m_property[] = {
67 { CONTACT_ATTR_URL, getUrl, setUrl, kJSPropertyAttributeNone },
68 { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
72 JSStaticFunction JSContactWebSite::m_functions[] =
77 JSClassRef JSContactWebSite::getClassRef() {
79 m_classRef = JSClassCreate(&m_classInfo);
84 JSValueRef JSContactWebSite::createJSObject(JSContextRef context,
85 const std::string url,
86 const ContactWebSiteType type)
88 ContactWebSitePtr privateData = ContactWebSitePtr(new ContactWebSite());
91 privateData->setUrl(url);
92 privateData->setType(type);
94 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, privateData);
95 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
96 if (NULL == jsValueRef) {
97 LogError("object creation error");
98 return JSValueMakeUndefined(context);
103 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
105 return JSValueIsObjectOfClass(context, value, getClassRef());
108 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
110 if (!isObjectOfClass(context, value)) {
111 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
113 JSObjectRef object = JSValueToObject(context, value, NULL);
115 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
117 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
119 Throw(WrtDeviceApis::Commons::NullPointerException);
121 return priv->getObject();
124 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
126 assert(NULL != JSObjectGetPrivate(object));
129 void JSContactWebSite::Finalize(JSObjectRef object)
131 //delete (JSObjectGetPrivate(object));
134 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
136 //LogDebug("entered");
137 JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
139 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
141 ContactWebSitePtr result = priv->getObject();
143 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
148 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
153 //LogDebug("entered");
156 ContactConverterFactory::ConverterType converter =
157 ContactConverterFactory::getConverter(context);
158 ContactWebSitePtr webSite = getPrivData(object);
159 return converter->toJSValueRef(webSite->getUrl());
161 Catch(WrtDeviceApis::Commons::Exception)
163 LogWarning("trying to get incorrect value");
165 return JSValueMakeUndefined(context);
168 bool JSContactWebSite::setUrl(JSContextRef context,
170 JSStringRef propertyName,
172 JSValueRef* exception)
176 ContactWebSitePtr webSite = getPrivData(object);
177 ContactConverterFactory::ConverterType converter =
178 ContactConverterFactory::getConverter(context);
179 webSite->setUrl(converter->toString(value));
182 Catch(WrtDeviceApis::Commons::Exception)
184 LogWarning("trying to set incorrect value");
186 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
191 JSValueRef JSContactWebSite::getType(JSContextRef context,
193 JSStringRef propertyName,
194 JSValueRef* exception)
196 //LogDebug("entered");
199 ContactConverterFactory::ConverterType converter =
200 ContactConverterFactory::getConverter(context);
201 ContactWebSitePtr webSite = getPrivData(object);
202 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
203 return converter->toJSValueRef(ret);
205 Catch(WrtDeviceApis::Commons::Exception)
207 LogWarning("trying to get incorrect value");
209 return JSValueMakeUndefined(context);
212 bool JSContactWebSite::setType(JSContextRef context,
214 JSStringRef propertyName,
216 JSValueRef* exception)
220 ContactWebSitePtr webSite = getPrivData(object);
221 ContactConverterFactory::ConverterType converter =
222 ContactConverterFactory::getConverter(context);
223 ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
224 webSite->setType(type);
227 Catch(WrtDeviceApis::Commons::Exception)
229 LogWarning("trying to set incorrect value");
231 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");