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 JSContactEmailAddress.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactEmailAddress 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 "JSContactEmailAddressTypeArray.h"
30 #include "JSContactEmailAddress.h"
32 #define CONTACT_CLASS_NAME "EmailAddress"
34 #define CONTACT_ATTR_EMAIL "email"
35 #define CONTACT_ATTR_TYPES "types"
41 using namespace TizenApis::Commons;
42 using namespace TizenApis::Api::Contact;
44 JSClassRef JSContactEmailAddress::m_classRef = NULL;
46 JSClassDefinition JSContactEmailAddress::m_classInfo =
49 kJSClassAttributeNone,
59 NULL, //DeleteProperty,
60 NULL, //getPropertyNames,
64 NULL, //ConvertToType,
67 JSStaticValue JSContactEmailAddress::m_property[] = {
68 { CONTACT_ATTR_EMAIL, getEmail, setEmail, kJSPropertyAttributeNone },
69 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
73 JSStaticFunction JSContactEmailAddress::m_functions[] =
78 JSClassRef JSContactEmailAddress::getClassRef() {
80 m_classRef = JSClassCreate(&m_classInfo);
85 JSValueRef JSContactEmailAddress::createJSObject(JSContextRef context,
86 const std::string email,
87 const ContactEmailAddressTypeArrayPtr types)
89 ContactEmailAddressPtr privateData = ContactEmailAddressPtr(new ContactEmailAddress());
91 privateData->setEmail(email);
92 privateData->setTypes(types);
94 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(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 JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
105 return JSValueIsObjectOfClass(context, value, getClassRef());
108 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(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 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
119 Throw(WrtDeviceApis::Commons::NullPointerException);
121 return priv->getObject();
124 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
126 assert(NULL != JSObjectGetPrivate(object));
129 void JSContactEmailAddress::Finalize(JSObjectRef object)
131 //delete (JSObjectGetPrivate(object));
134 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
136 //LogDebug("entered");
137 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
139 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
141 ContactEmailAddressPtr result = priv->getObject();
143 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
148 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
153 //LogDebug("entered");
156 ContactConverterFactory::ConverterType converter =
157 ContactConverterFactory::getConverter(context);
158 ContactEmailAddressPtr emailAddress = getPrivData(object);
159 return converter->toJSValueRef(emailAddress->getEmail());
161 Catch(WrtDeviceApis::Commons::Exception)
163 LogWarning("trying to get incorrect value");
165 return JSValueMakeUndefined(context);
168 bool JSContactEmailAddress::setEmail(JSContextRef context,
170 JSStringRef propertyName,
172 JSValueRef* exception)
176 ContactEmailAddressPtr emailAddress = getPrivData(object);
177 ContactConverterFactory::ConverterType converter =
178 ContactConverterFactory::getConverter(context);
179 emailAddress->setEmail(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 JSContactEmailAddress::getTypes(JSContextRef context,
193 JSStringRef propertyName,
194 JSValueRef* exception)
196 //LogDebug("entered");
199 ContactConverterFactory::ConverterType converter =
200 ContactConverterFactory::getConverter(context);
201 ContactEmailAddressPtr emailAddress = getPrivData(object);
202 return JSContactEmailAddressTypeArray::createArray(context, emailAddress->getTypes());
204 Catch(WrtDeviceApis::Commons::Exception)
206 LogWarning("trying to get incorrect value");
208 return JSValueMakeUndefined(context);
211 bool JSContactEmailAddress::setTypes(JSContextRef context,
213 JSStringRef propertyName,
215 JSValueRef* exception)
219 ContactEmailAddressPtr emailAddress = getPrivData(object);
220 ContactConverterFactory::ConverterType converter =
221 ContactConverterFactory::getConverter(context);
223 if(JSContactEmailAddressTypeArray::isObjectOfClass(context, value))
224 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value)); // TODO to implement this function on converter
226 emailAddress->setTypes(JSContactEmailAddressTypeArray::getContactEmailAddressTypeArray(context, value));
230 Catch(WrtDeviceApis::Commons::Exception)
232 LogWarning("trying to set incorrect value");
234 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");