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 JSContactAddress.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactAddress class
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactAddress.h"
33 #define CONTACT_CLASS_NAME "ContactAddress"
35 #define CONTACT_ATTR_COUNTRY "country"
36 #define CONTACT_ATTR_REGION "region"
37 #define CONTACT_ATTR_CITY "city"
38 #define CONTACT_ATTR_STREET_ADDRESS "streetAddress"
39 #define CONTACT_ATTR_ADDITIONAL_INFORMATION "additionalInformation"
40 #define CONTACT_ATTR_POSTAL_CODE "postalCode"
41 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
42 #define CONTACT_ATTR_TYPES "types"
47 using namespace DeviceAPI::Common;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
51 JSClassDefinition JSContactAddress::m_classInfo =
54 kJSClassAttributeNone,
64 NULL, //DeleteProperty,
65 NULL, //getPropertyNames,
66 NULL, //CallAsFunction,
67 constructor, //CallAsConstructor,
68 hasInstance, //HasInstance,
69 NULL, //ConvertToType,
72 JSStaticValue JSContactAddress::m_property[] = {
73 { CONTACT_ATTR_COUNTRY, getCountry, setCountry, kJSPropertyAttributeNone },
74 { CONTACT_ATTR_REGION, getRegion, setRegion, kJSPropertyAttributeNone },
75 { CONTACT_ATTR_CITY, getCity, setCity, kJSPropertyAttributeNone },
76 { CONTACT_ATTR_STREET_ADDRESS, getStreetAddress, setStreetAddress, kJSPropertyAttributeNone },
77 { CONTACT_ATTR_ADDITIONAL_INFORMATION, getAdditionalInformation, setAdditionalInformation, kJSPropertyAttributeNone },
78 { CONTACT_ATTR_POSTAL_CODE, getPostalCode, setPostalCode, kJSPropertyAttributeNone },
79 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
80 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
84 JSStaticFunction JSContactAddress::m_functions[] =
89 JSClassRef JSContactAddress::m_classRef = JSClassCreate(&m_classInfo);
91 JSClassRef DLL_EXPORT JSContactAddress::getClassRef() {
93 m_classRef = JSClassCreate(&m_classInfo);
98 bool JSContactAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
100 return JSValueIsObjectOfClass(context, value, getClassRef());
103 ContactAddressPtr JSContactAddress::getContactAddress(JSContextRef context, JSValueRef value)
105 if (!isObjectOfClass(context, value)) {
106 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
108 JSObjectRef object = JSValueToObject(context, value, NULL);
110 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
112 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
114 Throw(WrtDeviceApis::Commons::NullPointerException);
116 return priv->getObject();
119 void JSContactAddress::Initialize(JSContextRef context, JSObjectRef object)
121 if (!JSObjectGetPrivate(object))
123 ContactAddressPtr address(new ContactAddress());
124 JSContactAddressPriv *priv = new JSContactAddressPriv(context, ContactAddressPtr(address));
125 if (!JSObjectSetPrivate(object, priv)) {
131 void JSContactAddress::Finalize(JSObjectRef object)
133 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
139 JSObjectRef JSContactAddress::createJSObject(JSContextRef context, const ContactAddressPtr contactAddress)
141 JSContactAddressPriv *priv = new JSContactAddressPriv(context, contactAddress);
142 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
143 if (NULL == jsObjectRef) {
144 LoggerE("object creation error");
150 ContactAddressPtr JSContactAddress::getPrivData(JSObjectRef object)
152 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156 ContactAddressPtr result = priv->getObject();
158 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163 JSObjectRef JSContactAddress::constructor(JSContextRef context,
164 JSObjectRef constructor,
165 size_t argumentCount,
166 const JSValueRef arguments[],
167 JSValueRef* exception)
169 bool js1stParamIsObject = false;
171 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(constructor));
173 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 JSContextRef gContext = priv->getContext();
177 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
179 if (argumentCount >= 1)
181 if (JSValueIsObject(gContext, arguments[0]))
182 js1stParamIsObject = true;
184 if (!js1stParamIsObject &&
185 !JSValueIsNull(gContext, arguments[0]) &&
186 !JSValueIsUndefined(gContext, arguments[0]))
187 ThrowMsg(InvalidArgumentException, "1st argument must be a 'ContactAddressInit object'");
190 } Catch(Exception ) {
191 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
192 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
196 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
198 ContactAddressPtr contactEmailAddress(NULL);
201 if(js1stParamIsObject)
202 contactEmailAddress = converter->toContactAddressFromInit(arguments[0]);
204 contactEmailAddress = ContactAddressPtr(new ContactAddress());
207 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
208 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
212 JSObjectRef jsobject;
215 jsobject = createJSObject(gContext, contactEmailAddress);
217 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
218 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
225 bool JSContactAddress::hasInstance(JSContextRef context,
226 JSObjectRef constructor,
227 JSValueRef possibleInstance,
228 JSValueRef* exception)
230 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
233 JSValueRef JSContactAddress::getCountry(JSContextRef context,
235 JSStringRef propertyName,
236 JSValueRef* exception)
240 ContactConverterFactory::ConverterType converter =
241 ContactConverterFactory::getConverter(context);
242 ContactAddressPtr contactAddress = getPrivData(object);
243 if(!contactAddress->getCountryIsSet())
244 return JSValueMakeNull(context);
246 return converter->toJSValueRef(contactAddress->getCountry());
248 Catch(WrtDeviceApis::Commons::Exception)
250 LoggerW("trying to get incorrect value");
252 return JSValueMakeUndefined(context);
255 bool JSContactAddress::setCountry(JSContextRef context,
257 JSStringRef propertyName,
259 JSValueRef* exception)
263 ContactAddressPtr contactAddress = getPrivData(object);
264 ContactConverterFactory::ConverterType converter =
265 ContactConverterFactory::getConverter(context);
266 BasicValidator validator =
267 BasicValidatorFactory::getValidator(context, exception);
268 if(validator->isNullOrUndefined(value))
269 contactAddress->unsetCountry();
271 contactAddress->setCountry(converter->toString(value));
273 Catch(WrtDeviceApis::Commons::Exception)
275 LoggerW("trying to set incorrect value");
276 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
282 JSValueRef JSContactAddress::getRegion(JSContextRef context,
284 JSStringRef propertyName,
285 JSValueRef* exception)
289 ContactConverterFactory::ConverterType converter =
290 ContactConverterFactory::getConverter(context);
291 ContactAddressPtr contactAddress = getPrivData(object);
292 if(!contactAddress->getRegionIsSet())
293 return JSValueMakeNull(context);
295 return converter->toJSValueRef(contactAddress->getRegion());
297 Catch(WrtDeviceApis::Commons::Exception)
299 LoggerW("trying to get incorrect value");
301 return JSValueMakeUndefined(context);
304 bool JSContactAddress::setRegion(JSContextRef context,
306 JSStringRef propertyName,
308 JSValueRef* exception)
312 ContactAddressPtr contactAddress = getPrivData(object);
313 ContactConverterFactory::ConverterType converter =
314 ContactConverterFactory::getConverter(context);
315 BasicValidator validator =
316 BasicValidatorFactory::getValidator(context, exception);
317 if(validator->isNullOrUndefined(value))
318 contactAddress->unsetRegion();
320 contactAddress->setRegion(converter->toString(value));
322 Catch(WrtDeviceApis::Commons::Exception)
324 LoggerW("trying to set incorrect value");
325 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
330 JSValueRef JSContactAddress::getCity(JSContextRef context,
332 JSStringRef propertyName,
333 JSValueRef* exception)
337 ContactConverterFactory::ConverterType converter =
338 ContactConverterFactory::getConverter(context);
339 ContactAddressPtr contactAddress = getPrivData(object);
340 if(!contactAddress->getCityIsSet())
341 return JSValueMakeNull(context);
343 return converter->toJSValueRef(contactAddress->getCity());
345 Catch(WrtDeviceApis::Commons::Exception)
347 LoggerW("trying to get incorrect value");
349 return JSValueMakeUndefined(context);
352 bool JSContactAddress::setCity(JSContextRef context,
354 JSStringRef propertyName,
356 JSValueRef* exception)
360 ContactAddressPtr contactAddress = getPrivData(object);
361 ContactConverterFactory::ConverterType converter =
362 ContactConverterFactory::getConverter(context);
363 BasicValidator validator =
364 BasicValidatorFactory::getValidator(context, exception);
365 if(validator->isNullOrUndefined(value))
366 contactAddress->unsetCity();
368 contactAddress->setCity(converter->toString(value));
370 Catch(WrtDeviceApis::Commons::Exception)
372 LoggerW("trying to set incorrect value");
373 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
379 JSValueRef JSContactAddress::getStreetAddress(JSContextRef context,
381 JSStringRef propertyName,
382 JSValueRef* exception)
386 ContactConverterFactory::ConverterType converter =
387 ContactConverterFactory::getConverter(context);
388 ContactAddressPtr contactAddress = getPrivData(object);
389 if(!contactAddress->getStreetAddressIsSet())
390 return JSValueMakeNull(context);
392 return converter->toJSValueRef(contactAddress->getStreetAddress());
394 Catch(WrtDeviceApis::Commons::Exception)
396 LoggerW("trying to get incorrect value");
398 return JSValueMakeUndefined(context);
401 bool JSContactAddress::setStreetAddress(JSContextRef context,
403 JSStringRef propertyName,
405 JSValueRef* exception)
409 ContactAddressPtr contactAddress = getPrivData(object);
410 ContactConverterFactory::ConverterType converter =
411 ContactConverterFactory::getConverter(context);
412 BasicValidator validator =
413 BasicValidatorFactory::getValidator(context, exception);
414 if(validator->isNullOrUndefined(value))
415 contactAddress->unsetStreetAddress();
417 contactAddress->setStreetAddress(converter->toString(value));
419 Catch(WrtDeviceApis::Commons::Exception)
421 LoggerW("trying to set incorrect value");
422 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
428 JSValueRef JSContactAddress::getAdditionalInformation(JSContextRef context,
430 JSStringRef propertyName,
431 JSValueRef* exception)
435 ContactConverterFactory::ConverterType converter =
436 ContactConverterFactory::getConverter(context);
437 ContactAddressPtr contactAddress = getPrivData(object);
438 if(!contactAddress->getAdditionalInformationIsSet())
439 return JSValueMakeNull(context);
441 return converter->toJSValueRef(contactAddress->getAdditionalInformation());
443 Catch(WrtDeviceApis::Commons::Exception)
445 LoggerW("trying to get incorrect value");
447 return JSValueMakeUndefined(context);
450 bool JSContactAddress::setAdditionalInformation(JSContextRef context,
452 JSStringRef propertyName,
454 JSValueRef* exception)
458 ContactAddressPtr contactAddress = getPrivData(object);
459 ContactConverterFactory::ConverterType converter =
460 ContactConverterFactory::getConverter(context);
461 BasicValidator validator =
462 BasicValidatorFactory::getValidator(context, exception);
463 if(validator->isNullOrUndefined(value))
464 contactAddress->unsetAdditionalInformation();
466 contactAddress->setAdditionalInformation(converter->toString(value));
468 Catch(WrtDeviceApis::Commons::Exception)
470 LoggerW("trying to set incorrect value");
471 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
477 JSValueRef JSContactAddress::getPostalCode(JSContextRef context,
479 JSStringRef propertyName,
480 JSValueRef* exception)
484 ContactConverterFactory::ConverterType converter =
485 ContactConverterFactory::getConverter(context);
486 ContactAddressPtr contactAddress = getPrivData(object);
487 if(!contactAddress->getPostalCodeIsSet())
488 return JSValueMakeNull(context);
490 return converter->toJSValueRef(contactAddress->getPostalCode());
492 Catch(WrtDeviceApis::Commons::Exception)
494 LoggerW("trying to get incorrect value");
496 return JSValueMakeUndefined(context);
499 bool JSContactAddress::setPostalCode(JSContextRef context,
501 JSStringRef propertyName,
503 JSValueRef* exception)
507 ContactAddressPtr contactAddress = getPrivData(object);
508 ContactConverterFactory::ConverterType converter =
509 ContactConverterFactory::getConverter(context);
510 BasicValidator validator =
511 BasicValidatorFactory::getValidator(context, exception);
512 if(validator->isNullOrUndefined(value))
513 contactAddress->unsetPostalCode();
515 contactAddress->setPostalCode(converter->toString(value));
517 Catch(WrtDeviceApis::Commons::Exception)
519 LoggerW("trying to set incorrect value");
520 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
526 JSValueRef JSContactAddress::getIsDefault(JSContextRef context,
528 JSStringRef propertyName,
529 JSValueRef* exception)
533 ContactConverterFactory::ConverterType converter =
534 ContactConverterFactory::getConverter(context);
535 ContactAddressPtr contactAddress = getPrivData(object);
536 return converter->toJSValueRef(contactAddress->getIsDefault());
538 Catch(WrtDeviceApis::Commons::Exception)
540 LoggerW("trying to get incorrect value");
542 return JSValueMakeUndefined(context);
545 bool JSContactAddress::setIsDefault(JSContextRef context,
547 JSStringRef propertyName,
549 JSValueRef* exception)
553 ContactAddressPtr contactAddress = getPrivData(object);
554 ContactConverterFactory::ConverterType converter =
555 ContactConverterFactory::getConverter(context);
556 BasicValidator validator =
557 BasicValidatorFactory::getValidator(context, exception);
558 contactAddress->setIsDefault(converter->toBool(value));
560 Catch(WrtDeviceApis::Commons::Exception)
562 LoggerW("trying to set incorrect value");
563 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
569 JSValueRef JSContactAddress::getTypes(JSContextRef context,
571 JSStringRef propertyName,
572 JSValueRef* exception)
576 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
578 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
580 JSContextRef gContext = priv->getContext();
581 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
582 ContactAddressPtr contactAddress = getPrivData(object);
584 if(contactAddress->IsTypesSetJSArray()){
585 return contactAddress->getTypesJSObj();
587 JSValueRef tempJSValue = contactAddress->getTypesJSArray();
588 tempJSValue = converter->toJSValueRef(contactAddress->getTypes());
590 JSObjectRef convertedJSObject = contactAddress->getTypesJSObj();
591 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
592 contactAddress->setTypesJSArray(true, convertedJSObject);
594 JSValueProtect(gContext, convertedJSObject);
595 contactAddress->setContext(gContext);
599 Catch(WrtDeviceApis::Commons::Exception)
601 LoggerW("trying to get incorrect value");
603 return JSValueMakeUndefined(context);
606 bool JSContactAddress::setTypes(JSContextRef context,
608 JSStringRef propertyName,
610 JSValueRef* exception)
614 ContactAddressPtr contactAddress = getPrivData(object);
615 ContactConverterFactory::ConverterType converter =
616 ContactConverterFactory::getConverter(context);
618 contactAddress->setTypes(converter->toContactAddressTypeArray(value));
619 contactAddress->resetTypesJSObj();
621 Catch(WrtDeviceApis::Commons::Exception)
623 LoggerW("trying to set incorrect value");
624 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");