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/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactPhoneNumber.h"
33 #define FILTER_CLASS_NAME "ContactPhoneNumber"
35 #define CONTACT_ATTR_NUMBER "number"
36 #define CONTACT_ATTR_TYPES "types"
37 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
38 #define CONTACT_ATTR_LABEL "label"
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 },
72 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
76 JSStaticFunction JSContactPhoneNumber::m_functions[] =
81 JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
83 JSClassRef DLL_EXPORT JSContactPhoneNumber::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value)) {
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
113 if (!JSObjectGetPrivate(object))
115 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
116 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
117 if (!JSObjectSetPrivate(object, priv)) {
123 void JSContactPhoneNumber::Finalize(JSObjectRef object)
125 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
131 JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
133 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
134 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
135 if (NULL == jsObjectRef) {
136 LoggerE("object creation error");
142 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
144 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
146 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
148 ContactPhoneNumberPtr result = priv->getObject();
150 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155 JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
156 JSObjectRef constructor,
157 size_t argumentCount,
158 const JSValueRef arguments[],
159 JSValueRef* exception)
161 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
163 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165 JSContextRef gContext = priv->getContext();
166 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
169 ContactPhoneNumberTypeArrayPtr types(NULL);
170 bool isDefault = false;
174 ArgumentValidator Argvalidator(context, argumentCount, arguments);
175 number = Argvalidator.toString(0, false);
177 if (argumentCount >= 2){
178 Argvalidator.toArrayObject(1, true);
179 types = converter->toContactPhoneNumberTypeArray(arguments[1]);
181 types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
183 if(types->size() == 0)
184 types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
186 if (argumentCount >= 3)
187 isDefault = Argvalidator.toBool(2, false);
189 if (argumentCount >= 4)
190 label = Argvalidator.toString(3, false);
192 } catch (const TypeMismatchException& err ) {
193 JSWebAPIErrorFactory::postException(context, exception, err);
195 } catch(const BasePlatformException& err) {
196 JSWebAPIErrorFactory::postException(context, exception, err);
198 } catch(const ConversionException& err) {
199 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
201 } catch(const NullPointerException& err) {
202 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
206 ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
207 contactPhoneNumber->setNumber(number);
208 contactPhoneNumber->setTypes(types);
209 contactPhoneNumber->setIsDefault(isDefault);
210 contactPhoneNumber->setLabel(label);
212 JSObjectRef jsobject;
215 jsobject = createJSObject(gContext, contactPhoneNumber);
217 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
218 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
225 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
226 JSObjectRef constructor,
227 JSValueRef possibleInstance,
228 JSValueRef* exception)
230 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
233 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
235 JSStringRef propertyName,
236 JSValueRef* exception)
240 ContactConverterFactory::ConverterType converter =
241 ContactConverterFactory::getConverter(context);
242 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
243 return converter->toJSValueRef(contactPhoneNumber->getNumber());
245 Catch(WrtDeviceApis::Commons::Exception)
247 LoggerW("trying to get incorrect value");
249 return JSValueMakeUndefined(context);
252 bool JSContactPhoneNumber::setNumber(JSContextRef context,
254 JSStringRef propertyName,
256 JSValueRef* exception)
260 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
261 ContactConverterFactory::ConverterType converter =
262 ContactConverterFactory::getConverter(context);
263 contactPhoneNumber->setNumber(converter->toString(value));
265 Catch(WrtDeviceApis::Commons::Exception)
267 LoggerW("trying to set incorrect value");
268 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
274 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
276 JSStringRef propertyName,
277 JSValueRef* exception)
281 ContactConverterFactory::ConverterType converter =
282 ContactConverterFactory::getConverter(context);
283 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
284 LoggerD("contactPhoneNumber->getIsDefault() : " << contactPhoneNumber->getIsDefault());
286 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
288 Catch(WrtDeviceApis::Commons::Exception)
290 LoggerW("trying to get incorrect value");
292 return JSValueMakeUndefined(context);
295 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
297 JSStringRef propertyName,
299 JSValueRef* exception)
303 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
304 ContactConverterFactory::ConverterType converter =
305 ContactConverterFactory::getConverter(context);
306 BasicValidator validator =
307 BasicValidatorFactory::getValidator(context, exception);
308 contactPhoneNumber->setIsDefault(converter->toBool(value));
310 Catch(WrtDeviceApis::Commons::Exception)
312 LoggerW("trying to set incorrect value");
313 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
319 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
321 JSStringRef propertyName,
322 JSValueRef* exception)
326 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
328 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
330 JSContextRef gContext = priv->getContext();
331 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
332 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
334 if(contactPhoneNumber->IsTypesSetJSArray()){
335 return contactPhoneNumber->getTypesJSObj();
337 JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
338 tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
340 JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
341 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
342 contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
344 JSValueProtect(gContext, convertedJSObject);
345 contactPhoneNumber->setContext(gContext);
349 Catch(WrtDeviceApis::Commons::Exception)
351 LoggerW("trying to get incorrect value");
353 return JSValueMakeUndefined(context);
356 bool JSContactPhoneNumber::setTypes(JSContextRef context,
358 JSStringRef propertyName,
360 JSValueRef* exception)
364 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
365 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
367 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
368 contactPhoneNumber->resetTypesJSObj();
370 Catch(WrtDeviceApis::Commons::Exception)
372 LoggerW("trying to set incorrect value");
373 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
378 JSValueRef JSContactPhoneNumber::getLabel(JSContextRef context,
380 JSStringRef propertyName,
381 JSValueRef* exception)
385 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
386 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
388 return converter->toJSValueRef(contactPhoneNumber->getLabel());
390 Catch(WrtDeviceApis::Commons::Exception)
392 LoggerW("trying to get incorrect value");
394 return JSValueMakeUndefined(context);
397 bool JSContactPhoneNumber::setLabel(JSContextRef context,
399 JSStringRef propertyName,
401 JSValueRef* exception)
405 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
406 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
407 contactPhoneNumber->setLabel(converter->toString(value));
409 Catch(WrtDeviceApis::Commons::Exception)
411 LoggerW("trying to set incorrect value");
412 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");