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.
19 * @file JSContactPhoneNumber.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactPhoneNumber class
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.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"
38 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
43 using namespace DeviceAPI::Common;
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
47 JSClassDefinition JSContactPhoneNumber::m_classInfo =
50 kJSClassAttributeNone,
60 NULL, //DeleteProperty,
61 NULL, //getPropertyNames,
62 NULL, //CallAsFunction,
63 constructor, //CallAsConstructor,
64 hasInstance, //HasInstance,
65 NULL, //ConvertToType,
68 JSStaticValue JSContactPhoneNumber::m_property[] = {
69 { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
70 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
71 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, 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 bool js2ndParamIsArray = false;
164 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
166 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
168 JSContextRef gContext = priv->getContext();
170 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
172 if (argumentCount >= 2)
174 if (JSIsArrayValue(gContext, arguments[1]))
175 js2ndParamIsArray = true;
177 if (!js2ndParamIsArray &&
178 !JSValueIsNull(gContext, arguments[1]) &&
179 !JSValueIsUndefined(gContext, arguments[1]))
180 ThrowMsg(InvalidArgumentException, "2nd argument must be array of ContactPhoneNumber types");
183 } Catch(Exception ) {
184 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
185 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "2nd argument must be array of ContactPhoneNumber types");
189 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
192 ContactPhoneNumberTypeArrayPtr types(NULL);
196 if(argumentCount >= 1)
197 number = converter->toString(arguments[0]);
199 number = converter->toString(JSValueMakeUndefined(context));
202 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
203 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
208 if(js2ndParamIsArray)
209 types = converter->toContactPhoneNumberTypeArray(arguments[1]);
211 types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
213 if(types->size() == 0)
214 types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
217 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
218 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
223 if(argumentCount >= 3)
224 isDefault = converter->toBool(arguments[2]);
226 isDefault = converter->toBool(JSValueMakeUndefined(context));
229 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
230 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
234 ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
235 contactPhoneNumber->setNumber(number);
236 contactPhoneNumber->setTypes(types);
237 contactPhoneNumber->setIsDefault(isDefault);
239 JSObjectRef jsobject;
242 jsobject = createJSObject(gContext, contactPhoneNumber);
244 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
245 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
252 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
253 JSObjectRef constructor,
254 JSValueRef possibleInstance,
255 JSValueRef* exception)
257 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
260 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
262 JSStringRef propertyName,
263 JSValueRef* exception)
267 ContactConverterFactory::ConverterType converter =
268 ContactConverterFactory::getConverter(context);
269 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
270 return converter->toJSValueRef(contactPhoneNumber->getNumber());
272 Catch(WrtDeviceApis::Commons::Exception)
274 LogWarning("trying to get incorrect value");
276 return JSValueMakeUndefined(context);
279 bool JSContactPhoneNumber::setNumber(JSContextRef context,
281 JSStringRef propertyName,
283 JSValueRef* exception)
287 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
288 ContactConverterFactory::ConverterType converter =
289 ContactConverterFactory::getConverter(context);
290 contactPhoneNumber->setNumber(converter->toString(value));
293 Catch(WrtDeviceApis::Commons::Exception)
295 LogWarning("trying to set incorrect value");
297 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
302 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
304 JSStringRef propertyName,
305 JSValueRef* exception)
309 ContactConverterFactory::ConverterType converter =
310 ContactConverterFactory::getConverter(context);
311 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
312 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
314 Catch(WrtDeviceApis::Commons::Exception)
316 LogWarning("trying to get incorrect value");
318 return JSValueMakeUndefined(context);
321 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
323 JSStringRef propertyName,
325 JSValueRef* exception)
329 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
330 ContactConverterFactory::ConverterType converter =
331 ContactConverterFactory::getConverter(context);
332 BasicValidator validator =
333 BasicValidatorFactory::getValidator(context, exception);
334 contactPhoneNumber->setIsDefault(converter->toBool(value));
337 Catch(WrtDeviceApis::Commons::Exception)
339 LogWarning("trying to set incorrect value");
341 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
346 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
348 JSStringRef propertyName,
349 JSValueRef* exception)
353 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
355 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
357 JSContextRef gContext = priv->getContext();
358 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
359 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
361 if(contactPhoneNumber->IsTypesSetJSArray()){
362 return contactPhoneNumber->getTypesJSObj();
364 JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
365 tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
367 JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
368 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
369 contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
371 JSValueProtect(gContext, convertedJSObject);
372 contactPhoneNumber->setContext(gContext);
376 Catch(WrtDeviceApis::Commons::Exception)
378 LogWarning("trying to get incorrect value");
380 return JSValueMakeUndefined(context);
383 bool JSContactPhoneNumber::setTypes(JSContextRef context,
385 JSStringRef propertyName,
387 JSValueRef* exception)
391 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
392 ContactConverterFactory::ConverterType converter =
393 ContactConverterFactory::getConverter(context);
395 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
399 Catch(WrtDeviceApis::Commons::Exception)
401 LogWarning("trying to set incorrect value");
403 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");