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.
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContact class
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <SecurityExceptions.h>
28 #include "ContactFactory.h"
29 #include "plugin_config.h"
30 #include "ContactConverter.h"
31 #include "JSContact.h"
33 #include <ArgumentValidator.h>
34 #include <JSWebAPIErrorFactory.h>
35 #include <TimeTracer.h>
39 #define FILTER_CLASS_NAME "Contact"
41 #define CONTACT_ATTR_ID "id"
42 #define CONTACT_ATTR_ADDRESSBOOK_ID "addressBookId"
43 #define CONTACT_ATTR_PERSON_ID "personId"
44 #define CONTACT_ATTR_LAST_UPDATED "lastUpdated"
45 #define CONTACT_ATTR_IS_FAVORITE "isFavorite"
46 #define CONTACT_ATTR_NAME "name"
47 #define CONTACT_ATTR_ADDRESSES "addresses"
48 #define CONTACT_ATTR_PHOTO_URI "photoURI"
49 #define CONTACT_ATTR_PHONE_NUMBERS "phoneNumbers"
50 #define CONTACT_ATTR_EMAILS "emails"
51 #define CONTACT_ATTR_BIRTHDAY "birthday"
52 #define CONTACT_ATTR_ANNIVERSARIES "anniversaries"
53 #define CONTACT_ATTR_ORGANIZATIONS "organizations"
54 #define CONTACT_ATTR_NOTES "notes"
55 #define CONTACT_ATTR_URLS "urls"
56 #define CONTACT_ATTR_LAST_UPDATED "lastUpdated"
57 #define CONTACT_ATTR_RINGTONE_URI "ringtoneURI"
58 #define CONTACT_ATTR_GROUP_IDS "groupIds"
59 #define CONTACT_FUNC_CONVERT_TO_STRING "convertToString"
60 #define CONTACT_FUNC_CLONE "clone"
65 using namespace DeviceAPI::Common;
66 using namespace WrtDeviceApis::Commons;
67 using namespace WrtDeviceApis::CommonsJavaScript;
69 JSClassDefinition JSContact::m_classInfo =
72 kJSClassAttributeNone,
82 NULL, //DeleteProperty,
83 NULL, //getPropertyNames,
84 NULL, //CallAsFunction,
85 constructor, //CallAsConstructor,
86 hasInstance, //HasInstance,
87 NULL, //ConvertToType,
90 JSStaticValue JSContact::m_property[] = {
91 { CONTACT_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
92 { CONTACT_ATTR_ADDRESSBOOK_ID, getAddressBookId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
93 { CONTACT_ATTR_PERSON_ID, getPersonId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
94 { CONTACT_ATTR_LAST_UPDATED, getLastUpdated, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
95 { CONTACT_ATTR_IS_FAVORITE, getIsFavorite, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
96 { CONTACT_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
97 { CONTACT_ATTR_ADDRESSES, getAddresses, setAddresses, kJSPropertyAttributeNone },
98 { CONTACT_ATTR_PHOTO_URI, getPhotoURI, setPhotoURI, kJSPropertyAttributeNone },
99 { CONTACT_ATTR_PHONE_NUMBERS, getPhoneNumbers, setPhoneNumbers, kJSPropertyAttributeNone },
100 { CONTACT_ATTR_EMAILS, getEmails, setEmails, kJSPropertyAttributeNone },
101 { CONTACT_ATTR_BIRTHDAY, getBirthday, setBirthday, kJSPropertyAttributeNone },
102 { CONTACT_ATTR_ANNIVERSARIES, getAnniversaries, setAnniversaries, kJSPropertyAttributeNone },
103 { CONTACT_ATTR_ORGANIZATIONS, getOrganizations, setOrganizations, kJSPropertyAttributeNone },
104 { CONTACT_ATTR_NOTES, getNotes, setNotes, kJSPropertyAttributeNone },
105 { CONTACT_ATTR_URLS, getUrls, setUrls, kJSPropertyAttributeNone },
106 { CONTACT_ATTR_RINGTONE_URI, getRingtoneURI, setRingtoneURI, kJSPropertyAttributeNone },
107 { CONTACT_ATTR_GROUP_IDS, getGroupIds, setGroupIds, kJSPropertyAttributeNone },
111 JSStaticFunction JSContact::m_functions[] =
113 { CONTACT_FUNC_CONVERT_TO_STRING, convertToString, kJSPropertyAttributeNone },
114 { CONTACT_FUNC_CLONE, clone, kJSPropertyAttributeNone },
118 JSClassRef JSContact::m_classRef = JSClassCreate(&m_classInfo);
120 JSClassRef DLL_EXPORT JSContact::getClassRef() {
122 m_classRef = JSClassCreate(&m_classInfo);
127 bool JSContact::isObjectOfClass(JSContextRef context, JSValueRef value)
129 return JSValueIsObjectOfClass(context, value, getClassRef());
132 ContactPtr JSContact::getContact(JSContextRef context, JSValueRef value)
134 if (!isObjectOfClass(context, value)) {
135 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
137 JSObjectRef object = JSValueToObject(context, value, NULL);
139 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
141 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
143 Throw(WrtDeviceApis::Commons::NullPointerException);
145 return priv->getObject();
148 void JSContact::Initialize(JSContextRef context, JSObjectRef object)
150 if (!JSObjectGetPrivate(object))
152 ContactPtr contact = ContactFactory::getInstance().createContact();
153 JSContactPriv *priv = new JSContactPriv(context, ContactPtr(contact));
154 if (!JSObjectSetPrivate(object, priv)) {
160 void JSContact::Finalize(JSObjectRef object)
162 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
168 JSObjectRef JSContact::createJSObject(JSContextRef context, ContactPtr contact)
170 JSContactPriv *priv = new JSContactPriv(context, contact);
171 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
172 if (NULL == jsObjectRef) {
173 LoggerE("object creation error");
179 ContactPtr JSContact::getPrivData(JSObjectRef object)
181 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
183 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
185 ContactPtr result = priv->getObject();
187 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null.");
192 JSObjectRef JSContact::constructor(JSContextRef context,
193 JSObjectRef constructor,
194 size_t argumentCount,
195 const JSValueRef arguments[],
196 JSValueRef* exception)
198 bool jsNoParam = false;
199 bool js1stParamIsObject = false;
200 bool js1stParamIsString = false;
202 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(constructor));
204 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
206 JSContextRef gContext = priv->getContext();
208 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
209 if (argumentCount == 0)
215 if (JSValueIsNull(gContext, arguments[0]) || JSValueIsUndefined(gContext, arguments[0]))
217 else if (JSValueIsObject(gContext, arguments[0]))
218 js1stParamIsObject = true;
219 else if (JSValueIsString(gContext, arguments[0]))
220 js1stParamIsString = true;
223 LoggerE("Argument type mismatch : 1nd argument must be ContactInit object or string");
224 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "2nd argument must be 'ContactTextFormat'");
229 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
231 ContactPtr contact(NULL);
232 if(js1stParamIsObject)
235 contact = converter->toContactFromInit(arguments[0]);
237 } Catch(ConversionException) {
238 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
239 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be 'ContactInit object'");
242 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
243 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
247 else if(js1stParamIsString)
250 std::string stringRepresentation; // vCard string
252 stringRepresentation = converter->toString(arguments[0]);
254 contact = ContactFactory::getInstance().createContact();
255 contact->setContactFromString(stringRepresentation);
257 } Catch(ConversionException) {
258 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
259 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "2nd argument must be 'ContactTextFormat'");
261 } Catch(InvalidArgumentException) {
262 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
263 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "1st argument must be vCard string");
265 } Catch(UnsupportedException) {
266 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
267 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, "Only support vCard 3.0");
270 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
271 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
278 contact = ContactFactory::getInstance().createContact();
280 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
281 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
286 JSObjectRef jsobject;
289 jsobject = createJSObject(gContext, contact);
291 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
292 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
299 bool JSContact::hasInstance(JSContextRef context,
300 JSObjectRef constructor,
301 JSValueRef possibleInstance,
302 JSValueRef* exception)
304 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
307 JSValueRef JSContact::getId(JSContextRef context,
309 JSStringRef propertyName,
310 JSValueRef* exception)
314 ContactConverterFactory::ConverterType converter =
315 ContactConverterFactory::getConverter(context);
316 ContactPtr contact = getPrivData(object);
317 if(!contact->getIdIsSet())
318 return JSValueMakeNull(context);
320 return converter->toJSValueRef(contact->getId());
322 Catch(WrtDeviceApis::Commons::Exception)
324 LoggerW("trying to get incorrect value");
326 return JSValueMakeUndefined(context);
329 JSValueRef JSContact::getAddressBookId(JSContextRef context,
331 JSStringRef propertyName,
332 JSValueRef* exception)
336 ContactConverterFactory::ConverterType converter =
337 ContactConverterFactory::getConverter(context);
338 ContactPtr contact = getPrivData(object);
339 if(!contact->getAddressBookIdIsSet())
340 return JSValueMakeNull(context);
342 return converter->toJSValueRef(contact->getAddressBookId());
344 Catch(WrtDeviceApis::Commons::Exception)
346 LoggerW("trying to get incorrect value");
348 return JSValueMakeUndefined(context);
351 JSValueRef JSContact::getPersonId(JSContextRef context,
353 JSStringRef propertyName,
354 JSValueRef* exception)
358 ContactConverterFactory::ConverterType converter =
359 ContactConverterFactory::getConverter(context);
360 ContactPtr contact = getPrivData(object);
361 if(!contact->getPersonIdIsSet())
362 return JSValueMakeNull(context);
364 return converter->toJSValueRef(contact->getPersonId());
366 Catch(WrtDeviceApis::Commons::Exception)
368 LoggerW("trying to get incorrect value");
370 return JSValueMakeUndefined(context);
373 JSValueRef JSContact::getLastUpdated(JSContextRef context,
375 JSStringRef propertyName,
376 JSValueRef* exception)
380 ContactConverterFactory::ConverterType converter =
381 ContactConverterFactory::getConverter(context);
382 ContactPtr contact = getPrivData(object);
383 if(!contact->getLastUpdatedIsSet())
384 return JSValueMakeNull(context);
386 return converter->toJSValueRef(contact->getLastUpdated());
388 Catch(WrtDeviceApis::Commons::Exception)
390 LoggerW("trying to get incorrect value");
393 return JSValueMakeUndefined(context);
396 JSValueRef JSContact::getIsFavorite(JSContextRef context,
398 JSStringRef propertyName,
399 JSValueRef* exception)
403 ContactConverterFactory::ConverterType converter =
404 ContactConverterFactory::getConverter(context);
405 ContactPtr contact = getPrivData(object);
406 return converter->toJSValueRef(contact->getIsFavorite());
408 Catch(WrtDeviceApis::Commons::Exception)
410 LoggerW("trying to get incorrect value");
413 return JSValueMakeUndefined(context);
416 JSValueRef JSContact::getName(JSContextRef context,
418 JSStringRef propertyName,
419 JSValueRef* exception)
423 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
425 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
427 JSContextRef gContext = priv->getContext();
429 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
430 ContactPtr contact = getPrivData(object);
431 ContactNamePtr contactName = contact->getName();
432 if(contactName == NULL)
433 return JSValueMakeNull(context);
435 return converter->toJSValueRef(contactName);
437 Catch(WrtDeviceApis::Commons::Exception)
439 LoggerW("trying to get incorrect value");
442 return JSValueMakeUndefined(context);
445 bool JSContact::setName(JSContextRef context,
447 JSStringRef propertyName,
449 JSValueRef* exception)
453 ContactPtr contact = getPrivData(object);
454 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
455 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
456 if(validator->isNullOrUndefined(value))
457 contact->unsetName();
459 contact->setName(converter->toContactName(value));
461 Catch(WrtDeviceApis::Commons::Exception)
463 LoggerW("trying to set incorrect value");
464 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
470 JSValueRef JSContact::getAddresses(JSContextRef context,
472 JSStringRef propertyName,
473 JSValueRef* exception)
477 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
479 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
481 JSContextRef gContext = priv->getContext();
482 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
483 ContactPtr contact = getPrivData(object);
485 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
486 if(newContactT->addressesJSObjIsSet()){
487 return newContactT->getAddressesJSObj();
489 JSValueRef tempJSValue = newContactT->getAddressesJSValue();
490 tempJSValue = converter->toJSValueRef(newContactT->getAddresses());
492 JSObjectRef convertedJSObject = newContactT->getAddressesJSObj();
493 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
494 newContactT->setAddressesJSObj(true, convertedJSObject);
496 JSValueProtect(gContext, convertedJSObject);
497 newContactT->setContext(gContext);
501 Catch(WrtDeviceApis::Commons::Exception)
503 LoggerW("trying to get incorrect value");
506 return JSValueMakeUndefined(context);
509 bool JSContact::setAddresses(JSContextRef context,
511 JSStringRef propertyName,
513 JSValueRef* exception)
517 ContactPtr contact = getPrivData(object);
518 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
519 contact->setAddresses(converter->toContactAddressArray(value));
520 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
521 newContactT->resetAddressesJSObj();
523 Catch(WrtDeviceApis::Commons::Exception)
525 LoggerW("trying to set incorrect value");
526 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
532 JSValueRef JSContact::getPhotoURI(JSContextRef context,
534 JSStringRef propertyName,
535 JSValueRef* exception)
539 ContactConverterFactory::ConverterType converter =
540 ContactConverterFactory::getConverter(context);
541 ContactPtr contact = getPrivData(object);
542 if(!contact->getPhotoURIIsSet())
543 return JSValueMakeNull(context);
545 return converter->toJSValueRef(contact->getPhotoURI());
547 Catch(WrtDeviceApis::Commons::Exception)
549 LoggerW("trying to get incorrect value");
552 return JSValueMakeUndefined(context);
555 bool JSContact::setPhotoURI(JSContextRef context,
557 JSStringRef propertyName,
559 JSValueRef* exception)
563 ContactPtr contact = getPrivData(object);
564 ContactConverterFactory::ConverterType converter =
565 ContactConverterFactory::getConverter(context);
566 BasicValidator validator =
567 BasicValidatorFactory::getValidator(context, exception);
568 if(validator->isNullOrUndefined(value))
569 contact->unsetPhotoURI();
571 contact->setPhotoURI(converter->toString(value));
573 Catch(WrtDeviceApis::Commons::Exception)
575 LoggerW("trying to set incorrect value");
576 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
582 JSValueRef JSContact::getPhoneNumbers(JSContextRef context,
584 JSStringRef propertyName,
585 JSValueRef* exception)
589 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
591 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
593 JSContextRef gContext = priv->getContext();
594 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
595 ContactPtr contact = getPrivData(object);
597 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
598 if(newContactT->numbersJSObjIsSet()){
599 return newContactT->getNumbersJSObj();
601 JSValueRef tempJSValue = newContactT->getNumbersJSValue();
602 tempJSValue = converter->toJSValueRef(newContactT->getPhoneNumbers());
604 JSObjectRef convertedJSObject = newContactT->getNumbersJSObj();
605 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
606 newContactT->setNumbersJSObj(true, convertedJSObject);
608 JSValueProtect(gContext, convertedJSObject);
609 newContactT->setContext(gContext);
613 Catch(WrtDeviceApis::Commons::Exception)
615 LoggerW("trying to get incorrect value");
618 return JSValueMakeUndefined(context);
621 bool JSContact::setPhoneNumbers(JSContextRef context,
623 JSStringRef propertyName,
625 JSValueRef* exception)
629 ContactPtr contact = getPrivData(object);
630 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
631 contact->setPhoneNumbers(converter->toContactPhoneNumberArray(value));
632 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
633 newContactT->resetNumbersJSObj();
635 Catch(WrtDeviceApis::Commons::Exception)
637 LoggerW("trying to set incorrect value");
638 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
644 JSValueRef JSContact::getEmails(JSContextRef context,
646 JSStringRef propertyName,
647 JSValueRef* exception)
651 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
653 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
655 JSContextRef gContext = priv->getContext();
657 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
658 ContactPtr contact = getPrivData(object);
660 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
661 if(newContactT->EmailsJSObjIsSet()){
662 return newContactT->getEmailsJSObj();
664 JSValueRef tempJSValue = newContactT->getEmailsJSValue();
665 tempJSValue = converter->toJSValueRef(newContactT->getEmails());
667 JSObjectRef convertedJSObject = newContactT->getEmailsJSObj();
668 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
669 newContactT->setEmailsJSObj(true, convertedJSObject);
671 JSValueProtect(gContext, convertedJSObject);
672 newContactT->setContext(gContext);
676 Catch(WrtDeviceApis::Commons::Exception)
678 LoggerW("trying to get incorrect value");
681 return JSValueMakeUndefined(context);
684 bool JSContact::setEmails(JSContextRef context,
686 JSStringRef propertyName,
688 JSValueRef* exception)
692 ContactPtr contact = getPrivData(object);
693 ContactConverterFactory::ConverterType converter =
694 ContactConverterFactory::getConverter(context);
695 contact->setEmails(converter->toContactEmailAddressArray(value));
696 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
697 newContactT->resetEmailsJSObj();
699 Catch(WrtDeviceApis::Commons::Exception)
701 LoggerW("trying to set incorrect value");
702 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
708 JSValueRef JSContact::getBirthday(JSContextRef context,
710 JSStringRef propertyName,
711 JSValueRef* exception)
715 ContactConverterFactory::ConverterType converter =
716 ContactConverterFactory::getConverter(context);
717 ContactPtr contact = getPrivData(object);
718 if(!contact->getBirthdayIsSet())
719 return JSValueMakeNull(context);
721 return converter->toJSValueRef(contact->getBirthday());
723 Catch(WrtDeviceApis::Commons::Exception)
725 LoggerW("trying to get incorrect value");
728 return JSValueMakeUndefined(context);
731 bool JSContact::setBirthday(JSContextRef context,
733 JSStringRef propertyName,
735 JSValueRef* exception)
739 ContactPtr contact = getPrivData(object);
740 ContactConverterFactory::ConverterType converter =
741 ContactConverterFactory::getConverter(context);
742 BasicValidator validator =
743 BasicValidatorFactory::getValidator(context, exception);
744 if(validator->isNullOrUndefined(value))
745 contact->unsetBirthday();
747 contact->setBirthday(converter->toDateTm(value));
749 Catch(WrtDeviceApis::Commons::Exception)
751 LoggerW("trying to set incorrect value");
752 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
758 JSValueRef JSContact::getAnniversaries(JSContextRef context,
760 JSStringRef propertyName,
761 JSValueRef* exception)
765 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
767 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
769 JSContextRef gContext = priv->getContext();
771 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
772 ContactPtr contact = getPrivData(object);
774 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
775 if(newContactT->anniversariesJSObjIsSet()){
776 return newContactT->getAnniversariesJSObj();
778 JSValueRef tempJSValue = newContactT->getAnniversariesJSValue();
779 tempJSValue = converter->toJSValueRef(newContactT->getAnniversaries());
781 JSObjectRef convertedJSObject = newContactT->getAnniversariesJSObj();
782 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
783 newContactT->setAnniversariesJSObj(true, convertedJSObject);
785 JSValueProtect(gContext, convertedJSObject);
786 newContactT->setContext(gContext);
790 Catch(WrtDeviceApis::Commons::Exception)
792 LoggerW("trying to get incorrect value");
795 return JSValueMakeUndefined(context);
798 bool JSContact::setAnniversaries(JSContextRef context,
800 JSStringRef propertyName,
802 JSValueRef* exception)
806 ContactPtr contact = getPrivData(object);
807 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
808 contact->setAnniversaries(converter->toContactAnniversaryArray(value));
809 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
810 newContactT->resetAnniversariesJSObj();
812 Catch(WrtDeviceApis::Commons::Exception)
814 LoggerW("trying to set incorrect value");
815 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
821 JSValueRef JSContact::getOrganizations(JSContextRef context,
823 JSStringRef propertyName,
824 JSValueRef* exception)
828 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
830 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
832 JSContextRef gContext = priv->getContext();
834 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
835 ContactPtr contact = getPrivData(object);
837 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
838 if(newContactT->organizationsJSObjIsSet()){
839 return newContactT->getOrganizationsJSObj();
841 JSValueRef tempJSValue = newContactT->getOrganizationsJSValue();
842 tempJSValue = converter->toJSValueRef(newContactT->getOrganizations());
844 JSObjectRef convertedJSObject = newContactT->getOrganizationsJSObj();
845 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
846 newContactT->setOrganizationsJSObj(true, convertedJSObject);
848 JSValueProtect(gContext, convertedJSObject);
849 newContactT->setContext(gContext);
853 Catch(WrtDeviceApis::Commons::Exception)
855 LoggerW("trying to get incorrect value");
858 return JSValueMakeUndefined(context);
861 bool JSContact::setOrganizations(JSContextRef context,
863 JSStringRef propertyName,
865 JSValueRef* exception)
869 ContactPtr contact = getPrivData(object);
870 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
871 contact->setOrganizations(converter->toContactOrganizationArray(value));
872 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
873 newContactT->resetOrganizationsJSObj();
875 Catch(WrtDeviceApis::Commons::Exception)
877 LoggerW("trying to set incorrect value");
878 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
884 JSValueRef JSContact::getNotes(JSContextRef context,
886 JSStringRef propertyName,
887 JSValueRef* exception)
891 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
893 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
895 JSContextRef gContext = priv->getContext();
897 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
898 ContactPtr contact = getPrivData(object);
900 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
901 if(newContactT->notesJSObjIsSet()){
902 return newContactT->getNotesJSObj();
904 JSValueRef tempJSValue = newContactT->getNotesJSValue();
905 tempJSValue = converter->toJSValueRef(newContactT->getNotes());
907 JSObjectRef convertedJSObject = newContactT->getNotesJSObj();
908 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
909 newContactT->setNotesJSObj(true, convertedJSObject);
911 JSValueProtect(gContext, convertedJSObject);
912 newContactT->setContext(gContext);
916 Catch(WrtDeviceApis::Commons::Exception)
918 LoggerW("trying to get incorrect value");
921 return JSValueMakeUndefined(context);
924 bool JSContact::setNotes(JSContextRef context,
926 JSStringRef propertyName,
928 JSValueRef* exception)
932 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
934 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
936 JSContextRef gContext = priv->getContext();
937 ContactPtr contact = getPrivData(object);
939 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
940 contact->setNotes(converter->toStringArray(value));
941 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
942 newContactT->resetNotesJSObj();
944 Catch(WrtDeviceApis::Commons::Exception)
946 LoggerW("trying to set incorrect value");
947 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
953 JSValueRef JSContact::getUrls(JSContextRef context,
955 JSStringRef propertyName,
956 JSValueRef* exception)
960 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
962 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
964 JSContextRef gContext = priv->getContext();
966 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
967 ContactPtr contact = getPrivData(object);
969 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
970 if(newContactT->urlsJSObjIsSet()){
971 return newContactT->getUrlsJSObj();
973 JSValueRef tempJSValue = newContactT->getUrlsJSValue();
974 tempJSValue = converter->toJSValueRef(newContactT->getUrls());
976 JSObjectRef convertedJSObject = newContactT->getUrlsJSObj();
977 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
978 newContactT->setUrlsJSObj(true, convertedJSObject);
980 JSValueProtect(gContext, convertedJSObject);
981 newContactT->setContext(gContext);
985 Catch(WrtDeviceApis::Commons::Exception)
987 LoggerW("trying to get incorrect value");
990 return JSValueMakeUndefined(context);
993 bool JSContact::setUrls(JSContextRef context,
995 JSStringRef propertyName,
997 JSValueRef* exception)
1001 ContactPtr contact = getPrivData(object);
1002 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1003 contact->setUrls(converter->toContactWebSiteArray(value));
1004 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
1005 newContactT->resetUrlsJSObj();
1007 Catch(WrtDeviceApis::Commons::Exception)
1009 LoggerW("trying to set incorrect value");
1010 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
1016 JSValueRef JSContact::getRingtoneURI(JSContextRef context,
1018 JSStringRef propertyName,
1019 JSValueRef* exception)
1023 ContactConverterFactory::ConverterType converter =
1024 ContactConverterFactory::getConverter(context);
1025 ContactPtr contact = getPrivData(object);
1026 if(!contact->getRingtoneURIIsSet())
1027 return JSValueMakeNull(context);
1029 return converter->toJSValueRef(contact->getRingtoneURI());
1031 Catch(WrtDeviceApis::Commons::Exception)
1033 LoggerW("trying to get incorrect value");
1036 return JSValueMakeUndefined(context);
1039 bool JSContact::setRingtoneURI(JSContextRef context,
1041 JSStringRef propertyName,
1043 JSValueRef* exception)
1047 ContactPtr contact = getPrivData(object);
1048 ContactConverterFactory::ConverterType converter =
1049 ContactConverterFactory::getConverter(context);
1050 BasicValidator validator =
1051 BasicValidatorFactory::getValidator(context, exception);
1052 if(validator->isNullOrUndefined(value))
1053 contact->unsetRingtoneURI();
1055 contact->setRingtoneURI(converter->toString(value));
1057 Catch(WrtDeviceApis::Commons::Exception)
1059 LoggerW("trying to set incorrect value");
1060 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
1066 JSValueRef JSContact::getGroupIds(JSContextRef context,
1068 JSStringRef propertyName,
1069 JSValueRef* exception)
1073 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
1075 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
1077 JSContextRef gContext = priv->getContext();
1079 ContactConverterFactory::ConverterType converter =
1080 ContactConverterFactory::getConverter(gContext);
1081 ContactPtr contact = getPrivData(object);
1082 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
1084 if(newContactT->groupIdsJSObjIsSet()){
1085 return newContactT->getGroupIdsJSObj();
1087 JSValueRef tempJSValue = newContactT->getGroupIdsJSValue();
1088 tempJSValue = converter->toJSValueRef(newContactT->getGroupIds());
1090 JSObjectRef convertedJSObject = newContactT->getGroupIdsJSObj();
1091 convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
1092 newContactT->setGroupIdsJSObj(true, convertedJSObject);
1094 JSValueProtect(gContext, convertedJSObject);
1095 newContactT->setContext(gContext);
1099 Catch(WrtDeviceApis::Commons::Exception)
1101 LoggerW("trying to get incorrect value");
1104 return JSValueMakeUndefined(context);
1107 bool JSContact::setGroupIds(JSContextRef context,
1109 JSStringRef propertyName,
1111 JSValueRef* exception)
1115 ContactPtr contact = getPrivData(object);
1116 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1117 contact->setGroupIds(converter->toStringArray(value));
1118 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
1119 newContactT->resetGroupIdsJSObj();
1121 Catch(WrtDeviceApis::Commons::Exception)
1123 LoggerW("trying to set incorrect value");
1124 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
1130 JSValueRef JSContact::convertToString(JSContextRef context,
1132 JSObjectRef thisObject,
1133 size_t argumentCount,
1134 const JSValueRef arguments[],
1135 JSValueRef* exception)
1138 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1139 ContactPtr contact(NULL);
1142 contact = getPrivData(thisObject);
1143 if (contact == NULL) {
1144 ThrowMsg(InvalidArgumentException, "No private object.");
1146 } Catch(Exception) {
1147 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
1148 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
1151 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(
1152 // CONTACT_FUNCTION_API_CONVERT_TO_STRING);
1153 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1156 if(argumentCount > 0){
1157 ArgumentValidator validator(context, argumentCount, arguments);
1159 format = validator.toString(0, false);
1160 } catch (const TypeMismatchException& err ) {
1161 return JSWebAPIErrorFactory::postException(context, exception, err);
1162 } catch(const BasePlatformException& err) {
1163 return JSWebAPIErrorFactory::postException(context, exception, err);
1164 } catch(const ConversionException& err) {
1165 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
1166 } catch(const NullPointerException& err) {
1167 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
1170 format = "VCARD_30";
1175 vCard = contact->convertToString(format);
1176 } Catch(ConversionException) {
1177 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1178 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be 'ContactTextFormat string'");
1179 } Catch(UnsupportedException) {
1180 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1181 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, "Only support vCard 3.0");
1182 } Catch(Exception) {
1183 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1184 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
1187 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1190 result = converter->toJSValueRef(vCard);
1191 } Catch(Exception) {
1192 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1193 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
1195 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1199 JSValueRef JSContact::clone(JSContextRef context,
1201 JSObjectRef thisObject,
1202 size_t argumentCount,
1203 const JSValueRef arguments[],
1204 JSValueRef* exception)
1207 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1208 ContactPtr contact(NULL);
1211 contact = getPrivData(thisObject);
1212 if (contact == NULL) {
1213 ThrowMsg(InvalidArgumentException, "No private object.");
1215 } Catch(Exception) {
1216 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
1217 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
1220 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(
1221 // CONTACT_FUNCTION_API_CONVERT_TO_STRING);
1222 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1224 ContactPtr clonedContact(NULL);
1226 clonedContact = contact->clone();
1227 } Catch(Exception) {
1228 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1229 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
1234 JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(thisObject));
1236 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
1238 JSContextRef gContext = priv->getContext();
1239 result = createJSObject(gContext, clonedContact);
1240 } Catch(Exception) {
1241 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1242 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
1244 TIME_TRACER_ITEM_END(__FUNCTION__, 0);