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/Validator.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 //#include <Tizen/Common/JSGlobalContextFactory.h>
30 #include "ContactConverter.h"
31 #include "JSContactPhoneNumberTypeArray.h"
32 #include "JSContactPhoneNumber.h"
34 #define FILTER_CLASS_NAME "ContactPhoneNumber"
36 #define CONTACT_ATTR_NUMBER "number"
37 #define CONTACT_ATTR_TYPES "types"
43 using namespace TizenApis::Commons;
44 using namespace TizenApis::Api::Contact;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassDefinition JSContactPhoneNumber::m_classInfo =
51 kJSClassAttributeNone,
61 NULL, //DeleteProperty,
62 NULL, //getPropertyNames,
63 NULL, //CallAsFunction,
64 constructor, //CallAsConstructor,
65 hasInstance, //HasInstance,
66 NULL, //ConvertToType,
69 JSStaticValue JSContactPhoneNumber::m_property[] = {
70 { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
71 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
75 JSStaticFunction JSContactPhoneNumber::m_functions[] =
80 JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
82 JSClassRef JSContactPhoneNumber::getClassRef() {
84 m_classRef = JSClassCreate(&m_classInfo);
89 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
91 return JSValueIsObjectOfClass(context, value, getClassRef());
94 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
96 if (!isObjectOfClass(context, value)) {
97 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99 JSObjectRef object = JSValueToObject(context, value, NULL);
101 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
105 Throw(WrtDeviceApis::Commons::NullPointerException);
107 return priv->getObject();
110 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
112 if (!JSObjectGetPrivate(object))
114 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
115 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
116 if (!JSObjectSetPrivate(object, priv)) {
122 void JSContactPhoneNumber::Finalize(JSObjectRef object)
124 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
130 JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
132 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
133 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134 if (NULL == jsObjectRef) {
135 LogError("object creation error");
141 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
143 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
145 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147 ContactPhoneNumberPtr result = priv->getObject();
149 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
155 JSObjectRef constructor,
156 size_t argumentCount,
157 const JSValueRef arguments[],
158 JSValueRef* exception)
162 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
163 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
167 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
169 JSContextRef gContext = priv->getContext();
171 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
173 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
175 if (argumentCount < 1 || argumentCount > 2)
176 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
178 if (!JSValueIsString(gContext, arguments[0]))
179 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
181 if (argumentCount >= 2)
183 if (!JSIsArrayValue(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
184 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
187 } Catch(Exception ) {
188 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
193 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
196 ContactPhoneNumberTypeArrayPtr types(NULL);
199 number = converter->toString(arguments[0]);
201 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
207 if(argumentCount >= 2)
209 if(JSIsArrayValue(gContext, arguments[1]))
210 types = converter->toContactPhoneNumberTypeArray(arguments[1]);
214 types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
215 types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
218 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
219 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
223 ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
224 contactPhoneNumber->setNumber(number);
225 contactPhoneNumber->setTypes(types);
227 JSObjectRef jsobject;
230 jsobject = createJSObject(gContext, contactPhoneNumber);
232 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
233 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
240 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
241 JSObjectRef constructor,
242 JSValueRef possibleInstance,
243 JSValueRef* exception)
245 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
248 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
250 JSStringRef propertyName,
251 JSValueRef* exception)
255 ContactConverterFactory::ConverterType converter =
256 ContactConverterFactory::getConverter(context);
257 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
258 return converter->toJSValueRef(contactPhoneNumber->getNumber());
260 Catch(WrtDeviceApis::Commons::Exception)
262 LogWarning("trying to get incorrect value");
264 return JSValueMakeUndefined(context);
267 bool JSContactPhoneNumber::setNumber(JSContextRef context,
269 JSStringRef propertyName,
271 JSValueRef* exception)
275 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
276 ContactConverterFactory::ConverterType converter =
277 ContactConverterFactory::getConverter(context);
278 contactPhoneNumber->setNumber(converter->toString(value));
281 Catch(WrtDeviceApis::Commons::Exception)
283 LogWarning("trying to set incorrect value");
285 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
290 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
292 JSStringRef propertyName,
293 JSValueRef* exception)
297 ContactConverterFactory::ConverterType converter =
298 ContactConverterFactory::getConverter(context);
299 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
301 return converter->toJSValueRef(contactPhoneNumber->getTypes());
303 Catch(WrtDeviceApis::Commons::Exception)
305 LogWarning("trying to get incorrect value");
307 return JSValueMakeUndefined(context);
310 bool JSContactPhoneNumber::setTypes(JSContextRef context,
312 JSStringRef propertyName,
314 JSValueRef* exception)
318 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
319 ContactConverterFactory::ConverterType converter =
320 ContactConverterFactory::getConverter(context);
322 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
326 Catch(WrtDeviceApis::Commons::Exception)
328 LogWarning("trying to set incorrect value");
330 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");