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 JSContactPhoneNumber.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactPhoneNumber class
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactPhoneNumberTypeArray.h"
31 #include "JSContactPhoneNumber.h"
33 #define FILTER_CLASS_NAME "ContactPhoneNumber"
35 #define CONTACT_ATTR_NUMBER "number"
36 #define CONTACT_ATTR_TYPES "types"
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Contact;
45 JSClassRef JSContactPhoneNumber::m_classRef = NULL;
47 JSClassDefinition JSContactPhoneNumber::m_classInfo =
50 kJSClassAttributeNone,
60 NULL, //DeleteProperty,
61 NULL, //getPropertyNames,
65 NULL, //ConvertToType,
68 JSStaticValue JSContactPhoneNumber::m_property[] = {
69 { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
70 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
74 JSStaticFunction JSContactPhoneNumber::m_functions[] =
79 JSClassRef JSContactPhoneNumber::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 JSValueRef JSContactPhoneNumber::createJSObject(JSContextRef context
90 ContactPhoneNumberPtr privateData = ContactPhoneNumberPtr(new ContactPhoneNumber());
91 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, privateData);
92 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
93 if (NULL == jsValueRef) {
94 LogError("object creation error");
95 return JSValueMakeUndefined(context);
100 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
102 return JSValueIsObjectOfClass(context, value, getClassRef());
105 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
107 if (!isObjectOfClass(context, value)) {
108 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
110 JSObjectRef object = JSValueToObject(context, value, NULL);
112 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
114 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
116 Throw(WrtDeviceApis::Commons::NullPointerException);
118 return priv->getObject();
121 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
123 assert(NULL != JSObjectGetPrivate(object));
126 void JSContactPhoneNumber::Finalize(JSObjectRef object)
128 //delete (JSObjectGetPrivate(object));
131 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
133 //LogDebug("entered");
134 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
136 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
138 ContactPhoneNumberPtr result = priv->getObject();
140 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
145 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
147 JSStringRef propertyName,
148 JSValueRef* exception)
150 //LogDebug("entered");
153 ContactConverterFactory::ConverterType converter =
154 ContactConverterFactory::getConverter(context);
155 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
156 return converter->toJSValueRef(contactPhoneNumber->getNumber());
158 Catch(WrtDeviceApis::Commons::Exception)
160 LogWarning("trying to get incorrect value");
162 return JSValueMakeUndefined(context);
165 bool JSContactPhoneNumber::setNumber(JSContextRef context,
167 JSStringRef propertyName,
169 JSValueRef* exception)
173 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
174 ContactConverterFactory::ConverterType converter =
175 ContactConverterFactory::getConverter(context);
176 contactPhoneNumber->setNumber(converter->toString(value));
179 Catch(WrtDeviceApis::Commons::Exception)
181 LogWarning("trying to set incorrect value");
183 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
188 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
190 JSStringRef propertyName,
191 JSValueRef* exception)
193 //LogDebug("entered");
196 ContactConverterFactory::ConverterType converter =
197 ContactConverterFactory::getConverter(context);
198 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
199 //return JSContactPhoneNumberTypeArray::createArray(context, contactPhoneNumber->getTypes());
200 return converter->toJSValueRef(contactPhoneNumber->getTypes());
202 Catch(WrtDeviceApis::Commons::Exception)
204 LogWarning("trying to get incorrect value");
206 return JSValueMakeUndefined(context);
209 bool JSContactPhoneNumber::setTypes(JSContextRef context,
211 JSStringRef propertyName,
213 JSValueRef* exception)
217 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
218 ContactConverterFactory::ConverterType converter =
219 ContactConverterFactory::getConverter(context);
221 if(JSContactPhoneNumberTypeArray::isObjectOfClass(context, value)) {
222 contactPhoneNumber->setTypes(converter->toStringArray(value));
223 //contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value)); // TODO to implement this function on converter
225 contactPhoneNumber->setTypes(converter->toStringArray(value));
226 // contactPhoneNumber->setTypes(JSContactPhoneNumberTypeArray::getContactPhoneNumberTypeArray(context, value));
231 Catch(WrtDeviceApis::Commons::Exception)
233 LogWarning("trying to set incorrect value");
235 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");