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 <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactPhoneNumberTypeArray.h"
31 #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 LoggerE("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 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(constructor));
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
166 JSContextRef gContext = priv->getContext();
167 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
170 ContactPhoneNumberTypeArrayPtr types(NULL);
171 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 } catch (const TypeMismatchException& err ) {
190 JSWebAPIError::throwException(context, exception, err);
192 } catch(const BasePlatformException& err) {
193 JSWebAPIError::throwException(context, exception, err);
195 } catch(const ConversionException& err) {
196 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
198 } catch(const NullPointerException& err) {
199 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
203 ContactPhoneNumberPtr contactPhoneNumber(new ContactPhoneNumber());
204 contactPhoneNumber->setNumber(number);
205 contactPhoneNumber->setTypes(types);
206 contactPhoneNumber->setIsDefault(isDefault);
208 JSObjectRef jsobject;
211 jsobject = createJSObject(gContext, contactPhoneNumber);
213 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
214 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
221 bool JSContactPhoneNumber::hasInstance(JSContextRef context,
222 JSObjectRef constructor,
223 JSValueRef possibleInstance,
224 JSValueRef* exception)
226 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
229 JSValueRef JSContactPhoneNumber::getNumber(JSContextRef context,
231 JSStringRef propertyName,
232 JSValueRef* exception)
236 ContactConverterFactory::ConverterType converter =
237 ContactConverterFactory::getConverter(context);
238 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
239 return converter->toJSValueRef(contactPhoneNumber->getNumber());
241 Catch(WrtDeviceApis::Commons::Exception)
243 LoggerW("trying to get incorrect value");
245 return JSValueMakeUndefined(context);
248 bool JSContactPhoneNumber::setNumber(JSContextRef context,
250 JSStringRef propertyName,
252 JSValueRef* exception)
256 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
257 ContactConverterFactory::ConverterType converter =
258 ContactConverterFactory::getConverter(context);
259 contactPhoneNumber->setNumber(converter->toString(value));
261 Catch(WrtDeviceApis::Commons::Exception)
263 LoggerW("trying to set incorrect value");
264 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
270 JSValueRef JSContactPhoneNumber::getIsDefault(JSContextRef context,
272 JSStringRef propertyName,
273 JSValueRef* exception)
277 ContactConverterFactory::ConverterType converter =
278 ContactConverterFactory::getConverter(context);
279 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
280 return converter->toJSValueRef(contactPhoneNumber->getIsDefault());
282 Catch(WrtDeviceApis::Commons::Exception)
284 LoggerW("trying to get incorrect value");
286 return JSValueMakeUndefined(context);
289 bool JSContactPhoneNumber::setIsDefault(JSContextRef context,
291 JSStringRef propertyName,
293 JSValueRef* exception)
297 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
298 ContactConverterFactory::ConverterType converter =
299 ContactConverterFactory::getConverter(context);
300 BasicValidator validator =
301 BasicValidatorFactory::getValidator(context, exception);
302 contactPhoneNumber->setIsDefault(converter->toBool(value));
304 Catch(WrtDeviceApis::Commons::Exception)
306 LoggerW("trying to set incorrect value");
307 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
313 JSValueRef JSContactPhoneNumber::getTypes(JSContextRef context,
315 JSStringRef propertyName,
316 JSValueRef* exception)
320 JSContactPhoneNumberPriv *priv = static_cast<JSContactPhoneNumberPriv*>(JSObjectGetPrivate(object));
322 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
324 JSContextRef gContext = priv->getContext();
325 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
326 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
328 if(contactPhoneNumber->IsTypesSetJSArray()){
329 return contactPhoneNumber->getTypesJSObj();
331 JSValueRef tempJSValue = contactPhoneNumber->getTypesJSArray();
332 tempJSValue = converter->toJSValueRef(contactPhoneNumber->getTypes());
334 JSObjectRef convertedJSObject = contactPhoneNumber->getTypesJSObj();
335 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
336 contactPhoneNumber->setTypesJSArray(true, convertedJSObject);
338 JSValueProtect(gContext, convertedJSObject);
339 contactPhoneNumber->setContext(gContext);
343 Catch(WrtDeviceApis::Commons::Exception)
345 LoggerW("trying to get incorrect value");
347 return JSValueMakeUndefined(context);
350 bool JSContactPhoneNumber::setTypes(JSContextRef context,
352 JSStringRef propertyName,
354 JSValueRef* exception)
358 ContactPhoneNumberPtr contactPhoneNumber = getPrivData(object);
359 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
361 contactPhoneNumber->setTypes(converter->toContactPhoneNumberTypeArray(value));
362 contactPhoneNumber->resetTypesJSObj();
364 Catch(WrtDeviceApis::Commons::Exception)
366 LoggerW("trying to set incorrect value");
367 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");