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"
43 #define CONTACT_ATTR_LABEL "label"
48 using namespace DeviceAPI::Common;
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
52 JSClassDefinition JSContactAddress::m_classInfo =
55 kJSClassAttributeNone,
65 NULL, //DeleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //CallAsFunction,
68 constructor, //CallAsConstructor,
69 hasInstance, //HasInstance,
70 NULL, //ConvertToType,
73 JSStaticValue JSContactAddress::m_property[] = {
74 { CONTACT_ATTR_COUNTRY, getCountry, setCountry, kJSPropertyAttributeNone },
75 { CONTACT_ATTR_REGION, getRegion, setRegion, kJSPropertyAttributeNone },
76 { CONTACT_ATTR_CITY, getCity, setCity, kJSPropertyAttributeNone },
77 { CONTACT_ATTR_STREET_ADDRESS, getStreetAddress, setStreetAddress, kJSPropertyAttributeNone },
78 { CONTACT_ATTR_ADDITIONAL_INFORMATION, getAdditionalInformation, setAdditionalInformation, kJSPropertyAttributeNone },
79 { CONTACT_ATTR_POSTAL_CODE, getPostalCode, setPostalCode, kJSPropertyAttributeNone },
80 { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
81 { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
82 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
86 JSStaticFunction JSContactAddress::m_functions[] =
91 JSClassRef JSContactAddress::m_classRef = JSClassCreate(&m_classInfo);
93 JSClassRef DLL_EXPORT JSContactAddress::getClassRef() {
95 m_classRef = JSClassCreate(&m_classInfo);
100 bool JSContactAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
102 return JSValueIsObjectOfClass(context, value, getClassRef());
105 ContactAddressPtr JSContactAddress::getContactAddress(JSContextRef context, JSValueRef value)
107 if (!isObjectOfClass(context, value)) {
108 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
110 JSObjectRef object = JSValueToObject(context, value, NULL);
112 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
114 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
116 Throw(WrtDeviceApis::Commons::NullPointerException);
118 return priv->getObject();
121 void JSContactAddress::Initialize(JSContextRef context, JSObjectRef object)
123 if (!JSObjectGetPrivate(object))
125 ContactAddressPtr address(new ContactAddress());
126 JSContactAddressPriv *priv = new JSContactAddressPriv(context, ContactAddressPtr(address));
127 if (!JSObjectSetPrivate(object, priv)) {
133 void JSContactAddress::Finalize(JSObjectRef object)
135 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
141 JSObjectRef JSContactAddress::createJSObject(JSContextRef context, const ContactAddressPtr contactAddress)
143 JSContactAddressPriv *priv = new JSContactAddressPriv(context, contactAddress);
144 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
145 if (NULL == jsObjectRef) {
146 LoggerE("object creation error");
152 ContactAddressPtr JSContactAddress::getPrivData(JSObjectRef object)
154 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
156 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
158 ContactAddressPtr result = priv->getObject();
160 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165 JSObjectRef JSContactAddress::constructor(JSContextRef context,
166 JSObjectRef constructor,
167 size_t argumentCount,
168 const JSValueRef arguments[],
169 JSValueRef* exception)
171 bool js1stParamIsObject = false;
173 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(constructor));
175 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
177 JSContextRef gContext = priv->getContext();
179 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
181 if (argumentCount >= 1)
183 if (JSValueIsObject(gContext, arguments[0]))
184 js1stParamIsObject = true;
186 if (!js1stParamIsObject &&
187 !JSValueIsNull(gContext, arguments[0]) &&
188 !JSValueIsUndefined(gContext, arguments[0]))
189 ThrowMsg(InvalidArgumentException, "1st argument must be a 'ContactAddressInit object'");
192 } Catch(Exception ) {
193 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
194 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
198 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
200 ContactAddressPtr contactAddress(NULL);
203 if(js1stParamIsObject)
204 contactAddress = converter->toContactAddressFromInit(arguments[0]);
206 contactAddress = ContactAddressPtr(new ContactAddress());
209 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
210 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
214 JSObjectRef jsobject;
217 jsobject = createJSObject(gContext, contactAddress);
219 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
220 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
227 bool JSContactAddress::hasInstance(JSContextRef context,
228 JSObjectRef constructor,
229 JSValueRef possibleInstance,
230 JSValueRef* exception)
232 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
235 JSValueRef JSContactAddress::getCountry(JSContextRef context,
237 JSStringRef propertyName,
238 JSValueRef* exception)
242 ContactConverterFactory::ConverterType converter =
243 ContactConverterFactory::getConverter(context);
244 ContactAddressPtr contactAddress = getPrivData(object);
245 if(!contactAddress->getCountryIsSet())
246 return JSValueMakeNull(context);
248 return converter->toJSValueRef(contactAddress->getCountry());
250 Catch(WrtDeviceApis::Commons::Exception)
252 LoggerW("trying to get incorrect value");
254 return JSValueMakeUndefined(context);
257 bool JSContactAddress::setCountry(JSContextRef context,
259 JSStringRef propertyName,
261 JSValueRef* exception)
265 ContactAddressPtr contactAddress = getPrivData(object);
266 ContactConverterFactory::ConverterType converter =
267 ContactConverterFactory::getConverter(context);
268 BasicValidator validator =
269 BasicValidatorFactory::getValidator(context, exception);
270 if(validator->isNullOrUndefined(value))
271 contactAddress->unsetCountry();
273 contactAddress->setCountry(converter->toString(value));
275 Catch(WrtDeviceApis::Commons::Exception)
277 LoggerW("trying to set incorrect value");
278 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
284 JSValueRef JSContactAddress::getRegion(JSContextRef context,
286 JSStringRef propertyName,
287 JSValueRef* exception)
291 ContactConverterFactory::ConverterType converter =
292 ContactConverterFactory::getConverter(context);
293 ContactAddressPtr contactAddress = getPrivData(object);
294 if(!contactAddress->getRegionIsSet())
295 return JSValueMakeNull(context);
297 return converter->toJSValueRef(contactAddress->getRegion());
299 Catch(WrtDeviceApis::Commons::Exception)
301 LoggerW("trying to get incorrect value");
303 return JSValueMakeUndefined(context);
306 bool JSContactAddress::setRegion(JSContextRef context,
308 JSStringRef propertyName,
310 JSValueRef* exception)
314 ContactAddressPtr contactAddress = getPrivData(object);
315 ContactConverterFactory::ConverterType converter =
316 ContactConverterFactory::getConverter(context);
317 BasicValidator validator =
318 BasicValidatorFactory::getValidator(context, exception);
319 if(validator->isNullOrUndefined(value))
320 contactAddress->unsetRegion();
322 contactAddress->setRegion(converter->toString(value));
324 Catch(WrtDeviceApis::Commons::Exception)
326 LoggerW("trying to set incorrect value");
327 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
332 JSValueRef JSContactAddress::getCity(JSContextRef context,
334 JSStringRef propertyName,
335 JSValueRef* exception)
339 ContactConverterFactory::ConverterType converter =
340 ContactConverterFactory::getConverter(context);
341 ContactAddressPtr contactAddress = getPrivData(object);
342 if(!contactAddress->getCityIsSet())
343 return JSValueMakeNull(context);
345 return converter->toJSValueRef(contactAddress->getCity());
347 Catch(WrtDeviceApis::Commons::Exception)
349 LoggerW("trying to get incorrect value");
351 return JSValueMakeUndefined(context);
354 bool JSContactAddress::setCity(JSContextRef context,
356 JSStringRef propertyName,
358 JSValueRef* exception)
362 ContactAddressPtr contactAddress = getPrivData(object);
363 ContactConverterFactory::ConverterType converter =
364 ContactConverterFactory::getConverter(context);
365 BasicValidator validator =
366 BasicValidatorFactory::getValidator(context, exception);
367 if(validator->isNullOrUndefined(value))
368 contactAddress->unsetCity();
370 contactAddress->setCity(converter->toString(value));
372 Catch(WrtDeviceApis::Commons::Exception)
374 LoggerW("trying to set incorrect value");
375 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
381 JSValueRef JSContactAddress::getStreetAddress(JSContextRef context,
383 JSStringRef propertyName,
384 JSValueRef* exception)
388 ContactConverterFactory::ConverterType converter =
389 ContactConverterFactory::getConverter(context);
390 ContactAddressPtr contactAddress = getPrivData(object);
391 if(!contactAddress->getStreetAddressIsSet())
392 return JSValueMakeNull(context);
394 return converter->toJSValueRef(contactAddress->getStreetAddress());
396 Catch(WrtDeviceApis::Commons::Exception)
398 LoggerW("trying to get incorrect value");
400 return JSValueMakeUndefined(context);
403 bool JSContactAddress::setStreetAddress(JSContextRef context,
405 JSStringRef propertyName,
407 JSValueRef* exception)
411 ContactAddressPtr contactAddress = getPrivData(object);
412 ContactConverterFactory::ConverterType converter =
413 ContactConverterFactory::getConverter(context);
414 BasicValidator validator =
415 BasicValidatorFactory::getValidator(context, exception);
416 if(validator->isNullOrUndefined(value))
417 contactAddress->unsetStreetAddress();
419 contactAddress->setStreetAddress(converter->toString(value));
421 Catch(WrtDeviceApis::Commons::Exception)
423 LoggerW("trying to set incorrect value");
424 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
430 JSValueRef JSContactAddress::getAdditionalInformation(JSContextRef context,
432 JSStringRef propertyName,
433 JSValueRef* exception)
437 ContactConverterFactory::ConverterType converter =
438 ContactConverterFactory::getConverter(context);
439 ContactAddressPtr contactAddress = getPrivData(object);
440 if(!contactAddress->getAdditionalInformationIsSet())
441 return JSValueMakeNull(context);
443 return converter->toJSValueRef(contactAddress->getAdditionalInformation());
445 Catch(WrtDeviceApis::Commons::Exception)
447 LoggerW("trying to get incorrect value");
449 return JSValueMakeUndefined(context);
452 bool JSContactAddress::setAdditionalInformation(JSContextRef context,
454 JSStringRef propertyName,
456 JSValueRef* exception)
460 ContactAddressPtr contactAddress = getPrivData(object);
461 ContactConverterFactory::ConverterType converter =
462 ContactConverterFactory::getConverter(context);
463 BasicValidator validator =
464 BasicValidatorFactory::getValidator(context, exception);
465 if(validator->isNullOrUndefined(value))
466 contactAddress->unsetAdditionalInformation();
468 contactAddress->setAdditionalInformation(converter->toString(value));
470 Catch(WrtDeviceApis::Commons::Exception)
472 LoggerW("trying to set incorrect value");
473 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
479 JSValueRef JSContactAddress::getPostalCode(JSContextRef context,
481 JSStringRef propertyName,
482 JSValueRef* exception)
486 ContactConverterFactory::ConverterType converter =
487 ContactConverterFactory::getConverter(context);
488 ContactAddressPtr contactAddress = getPrivData(object);
489 if(!contactAddress->getPostalCodeIsSet())
490 return JSValueMakeNull(context);
492 return converter->toJSValueRef(contactAddress->getPostalCode());
494 Catch(WrtDeviceApis::Commons::Exception)
496 LoggerW("trying to get incorrect value");
498 return JSValueMakeUndefined(context);
501 bool JSContactAddress::setPostalCode(JSContextRef context,
503 JSStringRef propertyName,
505 JSValueRef* exception)
509 ContactAddressPtr contactAddress = getPrivData(object);
510 ContactConverterFactory::ConverterType converter =
511 ContactConverterFactory::getConverter(context);
512 BasicValidator validator =
513 BasicValidatorFactory::getValidator(context, exception);
514 if(validator->isNullOrUndefined(value))
515 contactAddress->unsetPostalCode();
517 contactAddress->setPostalCode(converter->toString(value));
519 Catch(WrtDeviceApis::Commons::Exception)
521 LoggerW("trying to set incorrect value");
522 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
528 JSValueRef JSContactAddress::getIsDefault(JSContextRef context,
530 JSStringRef propertyName,
531 JSValueRef* exception)
535 ContactConverterFactory::ConverterType converter =
536 ContactConverterFactory::getConverter(context);
537 ContactAddressPtr contactAddress = getPrivData(object);
538 return converter->toJSValueRef(contactAddress->getIsDefault());
540 Catch(WrtDeviceApis::Commons::Exception)
542 LoggerW("trying to get incorrect value");
544 return JSValueMakeUndefined(context);
547 bool JSContactAddress::setIsDefault(JSContextRef context,
549 JSStringRef propertyName,
551 JSValueRef* exception)
555 ContactAddressPtr contactAddress = getPrivData(object);
556 ContactConverterFactory::ConverterType converter =
557 ContactConverterFactory::getConverter(context);
558 BasicValidator validator =
559 BasicValidatorFactory::getValidator(context, exception);
560 contactAddress->setIsDefault(converter->toBool(value));
562 Catch(WrtDeviceApis::Commons::Exception)
564 LoggerW("trying to set incorrect value");
565 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
571 JSValueRef JSContactAddress::getTypes(JSContextRef context,
573 JSStringRef propertyName,
574 JSValueRef* exception)
578 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
580 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
582 JSContextRef gContext = priv->getContext();
583 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
584 ContactAddressPtr contactAddress = getPrivData(object);
586 if(contactAddress->IsTypesSetJSArray()){
587 return contactAddress->getTypesJSObj();
589 JSValueRef tempJSValue = contactAddress->getTypesJSArray();
590 tempJSValue = converter->toJSValueRef(contactAddress->getTypes());
592 JSObjectRef convertedJSObject = contactAddress->getTypesJSObj();
593 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
594 contactAddress->setTypesJSArray(true, convertedJSObject);
596 JSValueProtect(gContext, convertedJSObject);
597 contactAddress->setContext(gContext);
601 Catch(WrtDeviceApis::Commons::Exception)
603 LoggerW("trying to get incorrect value");
605 return JSValueMakeUndefined(context);
608 bool JSContactAddress::setTypes(JSContextRef context,
610 JSStringRef propertyName,
612 JSValueRef* exception)
616 ContactAddressPtr contactAddress = getPrivData(object);
617 ContactConverterFactory::ConverterType converter =
618 ContactConverterFactory::getConverter(context);
620 contactAddress->setTypes(converter->toContactAddressTypeArray(value));
621 contactAddress->resetTypesJSObj();
623 Catch(WrtDeviceApis::Commons::Exception)
625 LoggerW("trying to set incorrect value");
626 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
631 JSValueRef JSContactAddress::getLabel(JSContextRef context,
633 JSStringRef propertyName,
634 JSValueRef* exception)
638 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
639 ContactAddressPtr contactAddress = getPrivData(object);
640 if(!contactAddress->getLabelIsSet())
641 return JSValueMakeNull(context);
643 return converter->toJSValueRef(contactAddress->getLabel());
645 Catch(WrtDeviceApis::Commons::Exception)
647 LoggerW("trying to get incorrect value");
649 return JSValueMakeUndefined(context);
652 bool JSContactAddress::setLabel(JSContextRef context,
654 JSStringRef propertyName,
656 JSValueRef* exception)
660 ContactAddressPtr contactAddress = getPrivData(object);
661 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
662 BasicValidator validator =
663 BasicValidatorFactory::getValidator(context, exception);
664 contactAddress->setLabel(converter->toString(value));
666 Catch(WrtDeviceApis::Commons::Exception)
668 LoggerW("trying to set incorrect value");
669 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");