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 JSContactName.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactName 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 "JSContactName.h"
33 #define CONTACT_CLASS_NAME "ContactName"
34 #define CONTACT_PROP_ATTR_PREFIX "prefix"
35 #define CONTACT_PROP_ATTR_SUFFIX "suffix"
36 #define CONTACT_PROP_ATTR_FIRST_NAME "firstName"
37 #define CONTACT_PROP_ATTR_MIDDLE_NAME "middleName"
38 #define CONTACT_PROP_ATTR_LAST_NAME "lastName"
39 #define CONTACT_PROP_ATTR_NICKNAMES "nicknames"
40 #define CONTACT_PROP_ATTR_PHONETIC_NAME "phoneticName"
41 #define CONTACT_PROP_ATTR_PHONETIC_FIRST_NAME "phoneticFirstName"
42 #define CONTACT_PROP_ATTR_PHONETIC_LAST_NAME "phoneticLastName"
43 #define CONTACT_PROP_ATTR_DISPLAY_NAME "displayName"
48 using namespace DeviceAPI::Common;
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
52 JSClassDefinition JSContactName::m_classInfo =
55 kJSClassAttributeNone,
65 NULL, //DeleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //CallAsFunction,
68 constructor, //CallAsConstructor,
69 hasInstance, //HasInstance,
70 NULL, //ConvertToType,
73 JSStaticValue JSContactName::m_property[] = {
74 { CONTACT_PROP_ATTR_PREFIX, getPrefix, setPrefix, kJSPropertyAttributeNone },
75 { CONTACT_PROP_ATTR_SUFFIX, getSuffix, setSuffix, kJSPropertyAttributeNone },
76 { CONTACT_PROP_ATTR_FIRST_NAME, getFirstName, setFirstName, kJSPropertyAttributeNone },
77 { CONTACT_PROP_ATTR_MIDDLE_NAME, getMiddleName, setMiddleName, kJSPropertyAttributeNone },
78 { CONTACT_PROP_ATTR_LAST_NAME, getLastName, setLastName, kJSPropertyAttributeNone },
79 { CONTACT_PROP_ATTR_NICKNAMES, getNicknames, setNicknames, kJSPropertyAttributeNone },
80 { CONTACT_PROP_ATTR_PHONETIC_NAME, getPhoneticName, setPhoneticName, kJSPropertyAttributeNone },
81 { CONTACT_PROP_ATTR_PHONETIC_FIRST_NAME, getPhoneticFirstName, setPhoneticFirstName, kJSPropertyAttributeNone },
82 { CONTACT_PROP_ATTR_PHONETIC_LAST_NAME, getPhoneticLastName, setPhoneticLastName, kJSPropertyAttributeNone },
83 { CONTACT_PROP_ATTR_DISPLAY_NAME, getDisplayName, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
87 JSStaticFunction JSContactName::m_functions[] =
92 JSClassRef JSContactName::m_classRef = JSClassCreate(&m_classInfo);
94 JSClassRef JSContactName::getClassRef()
97 m_classRef = JSClassCreate(&m_classInfo);
102 bool JSContactName::isObjectOfClass(JSContextRef context, JSValueRef value)
104 return JSValueIsObjectOfClass(context, value, getClassRef());
107 ContactNamePtr JSContactName::getContactName(JSContextRef context, JSValueRef value)
109 if (!isObjectOfClass(context, value)) {
110 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
113 JSObjectRef object = JSValueToObject(context, value, NULL);
115 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
118 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
120 Throw(WrtDeviceApis::Commons::NullPointerException);
123 return priv->getObject();
126 void JSContactName::Initialize(JSContextRef context, JSObjectRef object)
128 if (!JSObjectGetPrivate(object))
130 ContactNamePtr name(new ContactName());
131 JSContactNamePriv *priv = new JSContactNamePriv(context, ContactNamePtr(name));
132 if (!JSObjectSetPrivate(object, priv)) {
138 void JSContactName::Finalize(JSObjectRef object)
140 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
146 JSObjectRef JSContactName::createJSObject(JSContextRef context, ContactNamePtr contactName)
148 JSContactNamePriv *priv = new JSContactNamePriv(context, contactName);
149 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
150 if (NULL == jsObjectRef)
152 LoggerE("object creation error");
158 ContactNamePtr JSContactName::getPrivData(JSObjectRef object)
160 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
162 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165 ContactNamePtr result = priv->getObject();
167 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
173 JSObjectRef JSContactName::constructor(JSContextRef context,
174 JSObjectRef constructor,
175 size_t argumentCount,
176 const JSValueRef arguments[],
177 JSValueRef* exception)
181 bool js1stParamIsObject = false;
183 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(constructor));
185 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
187 JSContextRef gContext = priv->getContext();
189 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
191 if (argumentCount >= 1)
193 if (JSValueIsObject(gContext, arguments[0]))
194 js1stParamIsObject = true;
196 if (!js1stParamIsObject &&
197 !JSValueIsNull(gContext, arguments[0]) &&
198 !JSValueIsUndefined(gContext, arguments[0]))
199 ThrowMsg(InvalidArgumentException, "1st argument must be 'object'");
202 } Catch(Exception ) {
203 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
204 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be 'object'");
208 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
210 ContactNamePtr contactName(NULL);
212 if(js1stParamIsObject)
213 contactName = converter->toContactNameFromInit(arguments[0]);
215 contactName = ContactNamePtr(new ContactName());
218 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
219 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
223 JSObjectRef jsobject;
226 jsobject = createJSObject(gContext, contactName);
228 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
229 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
236 bool JSContactName::hasInstance(JSContextRef context,
237 JSObjectRef constructor,
238 JSValueRef possibleInstance,
239 JSValueRef* exception)
241 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
244 JSValueRef JSContactName::getPrefix(JSContextRef context,
246 JSStringRef propertyName,
247 JSValueRef* exception)
251 ContactConverterFactory::ConverterType converter =
252 ContactConverterFactory::getConverter(context);
253 ContactNamePtr contactName = getPrivData(object);
254 if(!contactName->getPrefixIsSet())
255 return JSValueMakeNull(context);
257 return converter->toJSValueRef(contactName->getPrefix());
259 Catch(WrtDeviceApis::Commons::Exception)
261 LoggerW("trying to get incorrect value");
264 return JSValueMakeUndefined(context);
267 bool JSContactName::setPrefix(JSContextRef context,
269 JSStringRef propertyName,
271 JSValueRef* exception)
275 ContactNamePtr contactName = getPrivData(object);
276 ContactConverterFactory::ConverterType converter =
277 ContactConverterFactory::getConverter(context);
278 BasicValidator validator =
279 BasicValidatorFactory::getValidator(context, exception);
280 if(validator->isNullOrUndefined(value))
281 contactName->unsetPrefix();
283 contactName->setPrefix(converter->toString(value));
285 Catch(WrtDeviceApis::Commons::Exception)
287 LoggerW("trying to set incorrect value");
288 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
293 JSValueRef JSContactName::getSuffix(JSContextRef context,
295 JSStringRef propertyName,
296 JSValueRef* exception)
300 ContactConverterFactory::ConverterType converter =
301 ContactConverterFactory::getConverter(context);
302 ContactNamePtr contactName = getPrivData(object);
303 if(!contactName->getSuffixIsSet())
304 return JSValueMakeNull(context);
306 return converter->toJSValueRef(contactName->getSuffix());
308 Catch(WrtDeviceApis::Commons::Exception)
310 LoggerW("trying to get incorrect value");
313 return JSValueMakeUndefined(context);
316 bool JSContactName::setSuffix(JSContextRef context,
318 JSStringRef propertyName,
320 JSValueRef* exception)
324 ContactNamePtr contactName = getPrivData(object);
325 ContactConverterFactory::ConverterType converter =
326 ContactConverterFactory::getConverter(context);
327 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
328 if(validator->isNullOrUndefined(value))
329 contactName->unsetSuffix();
331 contactName->setSuffix(converter->toString(value));
333 Catch(WrtDeviceApis::Commons::Exception)
335 LoggerW("trying to set incorrect value");
336 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
341 JSValueRef JSContactName::getFirstName(JSContextRef context,
343 JSStringRef propertyName,
344 JSValueRef* exception)
348 ContactConverterFactory::ConverterType converter =
349 ContactConverterFactory::getConverter(context);
350 ContactNamePtr contactName = getPrivData(object);
351 if(!contactName->getFirstNameIsSet())
352 return JSValueMakeNull(context);
354 return converter->toJSValueRef(contactName->getFirstName());
356 Catch(WrtDeviceApis::Commons::Exception)
358 LoggerW("trying to get incorrect value");
361 return JSValueMakeUndefined(context);
364 bool JSContactName::setFirstName(JSContextRef context,
366 JSStringRef propertyName,
368 JSValueRef* exception)
372 ContactNamePtr contactName = getPrivData(object);
373 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
374 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
375 if(validator->isNullOrUndefined(value))
376 contactName->unsetFirstName();
378 contactName->setFirstName(converter->toString(value));
380 Catch(WrtDeviceApis::Commons::Exception)
382 LoggerW("trying to set incorrect value");
383 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
389 JSValueRef JSContactName::getMiddleName(JSContextRef context,
391 JSStringRef propertyName,
392 JSValueRef* exception)
396 ContactConverterFactory::ConverterType converter =
397 ContactConverterFactory::getConverter(context);
398 ContactNamePtr contactName = getPrivData(object);
399 if(!contactName->getMiddleNameIsSet())
400 return JSValueMakeNull(context);
402 return converter->toJSValueRef(contactName->getMiddleName());
404 Catch(WrtDeviceApis::Commons::Exception)
406 LoggerW("trying to get incorrect value");
409 return JSValueMakeUndefined(context);
412 bool JSContactName::setMiddleName(JSContextRef context,
414 JSStringRef propertyName,
416 JSValueRef* exception)
420 ContactNamePtr contactName = getPrivData(object);
421 ContactConverterFactory::ConverterType converter =
422 ContactConverterFactory::getConverter(context);
423 BasicValidator validator =
424 BasicValidatorFactory::getValidator(context, exception);
425 if(validator->isNullOrUndefined(value))
426 contactName->unsetMiddleName();
428 contactName->setMiddleName(converter->toString(value));
430 Catch(WrtDeviceApis::Commons::Exception)
432 LoggerW("trying to set incorrect value");
433 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
439 JSValueRef JSContactName::getLastName(JSContextRef context,
441 JSStringRef propertyName,
442 JSValueRef* exception)
446 ContactConverterFactory::ConverterType converter =
447 ContactConverterFactory::getConverter(context);
448 ContactNamePtr contactName = getPrivData(object);
449 if(!contactName->getLastNameIsSet())
450 return JSValueMakeNull(context);
452 return converter->toJSValueRef(contactName->getLastName());
454 Catch(WrtDeviceApis::Commons::Exception)
456 LoggerW("trying to get incorrect value");
459 return JSValueMakeUndefined(context);
462 bool JSContactName::setLastName(JSContextRef context,
464 JSStringRef propertyName,
466 JSValueRef* exception)
470 ContactNamePtr contactName = getPrivData(object);
471 ContactConverterFactory::ConverterType converter =
472 ContactConverterFactory::getConverter(context);
473 BasicValidator validator =
474 BasicValidatorFactory::getValidator(context, exception);
475 if(validator->isNullOrUndefined(value))
476 contactName->unsetLastName();
478 contactName->setLastName(converter->toString(value));
480 Catch(WrtDeviceApis::Commons::Exception)
482 LoggerW("trying to set incorrect value");
483 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
489 JSValueRef JSContactName::getNicknames(JSContextRef context,
491 JSStringRef propertyName,
492 JSValueRef* exception)
496 JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
498 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
500 JSContextRef gContext = priv->getContext();
502 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
503 ContactNamePtr contactName = getPrivData(object);
505 if(contactName->IsTypesSetJSArray()){
506 return contactName->getTypesJSObj();
508 JSValueRef tempJSValue = contactName->getTypesJSArray();
509 tempJSValue = converter->toJSValueRef(contactName->getNicknames());
511 JSObjectRef convertedJSObject = contactName->getTypesJSObj();
512 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
513 contactName->setTypesJSArray(true, convertedJSObject);
515 JSValueProtect(gContext, convertedJSObject);
516 contactName->setContext(gContext);
520 Catch(WrtDeviceApis::Commons::Exception)
522 LoggerW("trying to get incorrect value");
525 return JSValueMakeUndefined(context);
528 bool JSContactName::setNicknames(JSContextRef context,
530 JSStringRef propertyName,
532 JSValueRef* exception)
536 ContactNamePtr contactName = getPrivData(object);
537 ContactConverterFactory::ConverterType converter =
538 ContactConverterFactory::getConverter(context);
539 contactName->setNicknames(converter->toStringArray(value));
540 contactName->resetTypesJSObj();
542 Catch(WrtDeviceApis::Commons::Exception)
544 LoggerW("trying to set incorrect value");
545 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
551 JSValueRef JSContactName::getPhoneticName(JSContextRef context,
553 JSStringRef propertyName,
554 JSValueRef* exception)
556 // NOTE: Deprecated field.
557 return JSValueMakeNull(context);
560 bool JSContactName::setPhoneticName(JSContextRef context,
562 JSStringRef propertyName,
564 JSValueRef* exception)
566 // NOTE: Deprecated field.
570 JSValueRef JSContactName::getPhoneticFirstName(JSContextRef context,
572 JSStringRef propertyName,
573 JSValueRef* exception)
577 ContactConverterFactory::ConverterType converter =
578 ContactConverterFactory::getConverter(context);
579 ContactNamePtr contactName = getPrivData(object);
580 if(!contactName->getPhoneticFirstNameIsSet())
581 return JSValueMakeNull(context);
583 return converter->toJSValueRef(contactName->getPhoneticFirstName());
585 Catch(WrtDeviceApis::Commons::Exception)
587 LoggerW("trying to get incorrect value");
590 return JSValueMakeUndefined(context);
593 bool JSContactName::setPhoneticFirstName(JSContextRef context,
595 JSStringRef propertyName,
597 JSValueRef* exception)
601 ContactNamePtr contactName = getPrivData(object);
602 ContactConverterFactory::ConverterType converter =
603 ContactConverterFactory::getConverter(context);
604 BasicValidator validator =
605 BasicValidatorFactory::getValidator(context, exception);
606 if(validator->isNullOrUndefined(value))
607 contactName->unsetPhoneticFirstName();
609 contactName->setPhoneticFirstName(converter->toString(value));
611 Catch(WrtDeviceApis::Commons::Exception)
613 LoggerW("trying to set incorrect value");
614 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
620 JSValueRef JSContactName::getPhoneticLastName(JSContextRef context,
622 JSStringRef propertyName,
623 JSValueRef* exception)
627 ContactConverterFactory::ConverterType converter =
628 ContactConverterFactory::getConverter(context);
629 ContactNamePtr contactName = getPrivData(object);
630 if(!contactName->getPhoneticLastNameIsSet())
631 return JSValueMakeNull(context);
633 return converter->toJSValueRef(contactName->getPhoneticLastName());
635 Catch(WrtDeviceApis::Commons::Exception)
637 LoggerW("trying to get incorrect value");
640 return JSValueMakeUndefined(context);
643 bool JSContactName::setPhoneticLastName(JSContextRef context,
645 JSStringRef propertyName,
647 JSValueRef* exception)
651 ContactNamePtr contactName = getPrivData(object);
652 ContactConverterFactory::ConverterType converter =
653 ContactConverterFactory::getConverter(context);
654 BasicValidator validator =
655 BasicValidatorFactory::getValidator(context, exception);
656 if(validator->isNullOrUndefined(value))
657 contactName->unsetPhoneticLastName();
659 contactName->setPhoneticLastName(converter->toString(value));
661 Catch(WrtDeviceApis::Commons::Exception)
663 LoggerW("trying to set incorrect value");
664 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
670 JSValueRef JSContactName::getDisplayName(JSContextRef context,
672 JSStringRef propertyName,
673 JSValueRef* exception)
677 ContactConverterFactory::ConverterType converter =
678 ContactConverterFactory::getConverter(context);
679 ContactNamePtr contactName = getPrivData(object);
680 if(!contactName->getDisplayNameIsSet())
681 return JSValueMakeNull(context);
683 return converter->toJSValueRef(contactName->getDisplayName());
685 Catch(WrtDeviceApis::Commons::Exception)
687 LoggerW("trying to get incorrect value");
690 return JSValueMakeUndefined(context);