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"
42 using namespace DeviceAPI::Common;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
46 JSClassDefinition JSContactPhoneNumber::m_classInfo =
49 kJSClassAttributeNone,
59 NULL, //DeleteProperty,
60 NULL, //getPropertyNames,
61 NULL, //CallAsFunction,
62 constructor, //CallAsConstructor,
63 hasInstance, //HasInstance,
64 NULL, //ConvertToType,
67 JSStaticValue JSContactPhoneNumber::m_property[] = {
68 { CONTACT_ATTR_NUMBER, getNumber, setNumber, kJSPropertyAttributeNone },
69 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
70 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
74 JSStaticFunction JSContactPhoneNumber::m_functions[] =
79 JSClassRef JSContactPhoneNumber::m_classRef = JSClassCreate(&m_classInfo);
81 JSClassRef DLL_EXPORT JSContactPhoneNumber::getClassRef() {
83 m_classRef = JSClassCreate(&m_classInfo);
88 bool JSContactPhoneNumber::isObjectOfClass(JSContextRef context, JSValueRef value)
90 return JSValueIsObjectOfClass(context, value, getClassRef());
93 ContactPhoneNumberPtr JSContactPhoneNumber::getContactPhoneNumber(JSContextRef context, JSValueRef value)
95 if (!isObjectOfClass(context, value)) {
96 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98 JSObjectRef object = JSValueToObject(context, value, NULL);
100 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
104 Throw(WrtDeviceApis::Commons::NullPointerException);
106 return priv->getObject();
109 void JSContactPhoneNumber::Initialize(JSContextRef context, JSObjectRef object)
111 if (!JSObjectGetPrivate(object))
113 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
114 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, ContactPhoneNumberPtr(phoneNumber));
115 if (!JSObjectSetPrivate(object, priv)) {
121 void JSContactPhoneNumber::Finalize(JSObjectRef object)
123 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
129 JSObjectRef JSContactPhoneNumber::createJSObject(JSContextRef context, ContactPhoneNumberPtr contactPhoneNumber)
131 JSContactPhoneNumberPriv *priv = new JSContactPhoneNumberPriv(context, contactPhoneNumber);
132 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
133 if (NULL == jsObjectRef) {
134 LoggerE("object creation error");
140 ContactPhoneNumberPtr JSContactPhoneNumber::getPrivData(JSObjectRef object)
142 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
144 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146 ContactPhoneNumberPtr result = priv->getObject();
148 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 JSObjectRef JSContactPhoneNumber::constructor(JSContextRef context,
154 JSObjectRef constructor,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
159 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
161 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163 JSContextRef gContext = priv->getContext();
164 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
167 ContactPhoneNumberTypeArrayPtr types(NULL);
168 bool isDefault = false;
171 ArgumentValidator Argvalidator(context, argumentCount, arguments);
172 number = Argvalidator.toString(0, false);
174 if (argumentCount >= 2){
175 Argvalidator.toArrayObject(1, true);
176 types = converter->toContactPhoneNumberTypeArray(arguments[1]);
178 types = ContactPhoneNumberTypeArrayPtr(new ContactPhoneNumberTypeArray());
180 if(types->size() == 0)
181 types->push_back(CONTACT_PHONE_NUMBER_TYPE_VOICE);
183 if (argumentCount >= 3)
184 isDefault = Argvalidator.toBool(2, false);
186 } catch (const TypeMismatchException& err ) {
187 JSWebAPIErrorFactory::postException(context, exception, err);
189 } catch(const BasePlatformException& err) {
190 JSWebAPIErrorFactory::postException(context, exception, err);
192 } catch(const ConversionException& err) {
193 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
195 } catch(const NullPointerException& err) {
196 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
200 ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
201 contactPhoneNumber->setNumber(number);
202 contactPhoneNumber->setTypes(types);
203 contactPhoneNumber->setIsDefault(isDefault);
205 JSObjectRef jsobject;
208 jsobject = createJSObject(gContext, contactPhoneNumber);
210 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
211 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
218 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
219 JSObjectRef constructor,
220 JSValueRef possibleInstance,
221 JSValueRef* exception)
223 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
226 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
228 JSStringRef propertyName,
229 JSValueRef* exception)
233 ContactConverterFactory::ConverterType converter =
234 ContactConverterFactory::getConverter(context);
235 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
236 return converter->toJSValueRef(contactPhoneNumber->getNumber());
238 Catch(WrtDeviceApis::Commons::Exception)
240 LoggerW("trying to get incorrect value");
242 return JSValueMakeUndefined(context);
245 bool JSContactPhoneNumber::setNumber(JSContextRef context,
247 JSStringRef propertyName,
249 JSValueRef* exception)
253 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
254 ContactConverterFactory::ConverterType converter =
255 ContactConverterFactory::getConverter(context);
256 contactPhoneNumber->setNumber(converter->toString(value));
258 Catch(WrtDeviceApis::Commons::Exception)
260 LoggerW("trying to set incorrect value");
261 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
267 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
269 JSStringRef propertyName,
270 JSValueRef* exception)
274 ContactConverterFactory::ConverterType converter =
275 ContactConverterFactory::getConverter(context);
276 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
277 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
279 Catch(WrtDeviceApis::Commons::Exception)
281 LoggerW("trying to get incorrect value");
283 return JSValueMakeUndefined(context);
286 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
288 JSStringRef propertyName,
290 JSValueRef* exception)
294 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
295 ContactConverterFactory::ConverterType converter =
296 ContactConverterFactory::getConverter(context);
297 BasicValidator validator =
298 BasicValidatorFactory::getValidator(context, exception);
299 contactPhoneNumber->setIsDefault(converter->toBool(value));
301 Catch(WrtDeviceApis::Commons::Exception)
303 LoggerW("trying to set incorrect value");
304 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
310 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
312 JSStringRef propertyName,
313 JSValueRef* exception)
317 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
319 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
321 JSContextRef gContext = priv->getContext();
322 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
323 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
325 if(contactPhoneNumber->IsTypesSetJSArray()){
326 return contactPhoneNumber->getTypesJSObj();
328 JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
329 tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
331 JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
332 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
333 contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
335 JSValueProtect(gContext, convertedJSObject);
336 contactPhoneNumber->setContext(gContext);
340 Catch(WrtDeviceApis::Commons::Exception)
342 LoggerW("trying to get incorrect value");
344 return JSValueMakeUndefined(context);
347 bool JSContactPhoneNumber::setTypes(JSContextRef context,
349 JSStringRef propertyName,
351 JSValueRef* exception)
355 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
356 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
358 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
359 contactPhoneNumber->resetTypesJSObj();
361 Catch(WrtDeviceApis::Commons::Exception)
363 LoggerW("trying to set incorrect value");
364 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");