2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file JSContactProperties.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactProperties class
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactProperties.h"
32 #define CONTACT_CLASS_NAME "ContactProperties"
33 #define CONTACT_PROP_ATTR_NAME "name"
34 #define CONTACT_PROP_ATTR_ACCOUNT "account"
35 #define CONTACT_PROP_ATTR_ADDRESSES "addresses"
36 #define CONTACT_PROP_ATTR_PHOTO_URI "photoURI"
37 #define CONTACT_PROP_ATTR_PHONE_NUMBERS "phoneNumbers"
38 #define CONTACT_PROP_ATTR_EMAILS "emails"
39 #define CONTACT_PROP_ATTR_BIRTHDAY "birthday"
40 #define CONTACT_PROP_ATTR_ANNIVERSARIES "anniversaries"
41 #define CONTACT_PROP_ATTR_ORGANIZATION "organization"
42 #define CONTACT_PROP_ATTR_NOTES "notes"
43 #define CONTACT_PROP_ATTR_URLS "urls"
44 #define CONTACT_PROP_ATTR_LAST_UPDATED "lastUpdated"
45 #define CONTACT_PROP_ATTR_IS_FAVORITE "isFavorite"
46 #define CONTACT_PROP_ATTR_RINGTONE_URI "ringtoneURI"
47 #define CONTACT_PROP_ATTR_CATEGORIES "categories"
53 using namespace TizenApis::Commons;
54 using namespace TizenApis::Api::Contact;
56 JSClassRef JSContactProperties::m_classRef = NULL;
58 JSClassDefinition JSContactProperties::m_classInfo =
61 kJSClassAttributeNone,
71 NULL, //DeleteProperty,
72 NULL, //getPropertyNames,
76 NULL, //ConvertToType,
79 JSStaticValue JSContactProperties::m_property[] = {
80 { CONTACT_PROP_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
81 { CONTACT_PROP_ATTR_ACCOUNT, getAccount, setAccount, kJSPropertyAttributeNone },
82 { CONTACT_PROP_ATTR_ADDRESSES, getAddresses, setAddresses, kJSPropertyAttributeNone },
83 { CONTACT_PROP_ATTR_PHOTO_URI, getPhotoURI, setPhotoURI, kJSPropertyAttributeNone },
84 { CONTACT_PROP_ATTR_PHONE_NUMBERS, getPhoneNumbers, setPhoneNumbers, kJSPropertyAttributeNone },
85 { CONTACT_PROP_ATTR_EMAILS, getEmails, setEmails, kJSPropertyAttributeNone },
86 { CONTACT_PROP_ATTR_BIRTHDAY, getBirthday, setBirthday, kJSPropertyAttributeNone },
87 { CONTACT_PROP_ATTR_ANNIVERSARIES, getAnniversaries, setAnniversaries, kJSPropertyAttributeNone },
88 { CONTACT_PROP_ATTR_ORGANIZATION, getOrganization, setOrganization, kJSPropertyAttributeNone },
89 { CONTACT_PROP_ATTR_NOTES, getNotes, setNotes, kJSPropertyAttributeNone },
90 { CONTACT_PROP_ATTR_URLS, getUrls, setUrls, kJSPropertyAttributeNone },
91 { CONTACT_PROP_ATTR_IS_FAVORITE, getIsFavorite, setIsFavorite, kJSPropertyAttributeNone },
92 { CONTACT_PROP_ATTR_RINGTONE_URI, getRingtoneURI, setRingtoneURI, kJSPropertyAttributeNone },
93 { CONTACT_PROP_ATTR_CATEGORIES, getCategories, setCategories, kJSPropertyAttributeNone },
97 JSStaticFunction JSContactProperties::m_functions[] =
102 JSClassRef JSContactProperties::getClassRef()
105 m_classRef = JSClassCreate(&m_classInfo);
110 JSValueRef JSContactProperties::createJSObject(JSContextRef context
114 ContactPropertiesPtr privateData = ContactPropertiesPtr(new ContactProperties());
115 JSContactPropertiesPriv *priv = new JSContactPropertiesPriv(context, privateData);
116 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117 if (NULL == jsValueRef)
119 LogError("object creation error");
120 return JSValueMakeUndefined(context);
125 bool JSContactProperties::isObjectOfClass(JSContextRef context, JSValueRef value)
127 return JSValueIsObjectOfClass(context, value, getClassRef());
130 ContactPropertiesPtr JSContactProperties::getContactProperties(JSContextRef context, JSValueRef value)
132 if (!isObjectOfClass(context, value)) {
133 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
136 JSObjectRef object = JSValueToObject(context, value, NULL);
138 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
141 JSContactPropertiesPriv *priv = static_cast<JSContactPropertiesPriv*>(JSObjectGetPrivate(object));
143 Throw(WrtDeviceApis::Commons::NullPointerException);
146 return priv->getObject();
149 void JSContactProperties::Initialize(JSContextRef context, JSObjectRef object)
151 assert(NULL != JSObjectGetPrivate(object));
154 void JSContactProperties::Finalize(JSObjectRef object)
156 //delete (JSObjectGetPrivate(object));
159 ContactPropertiesPtr JSContactProperties::getPrivData(JSObjectRef object)
161 //LogDebug("entered");
162 JSContactPropertiesPriv *priv = static_cast<JSContactPropertiesPriv*>(JSObjectGetPrivate(object));
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
167 ContactPropertiesPtr result = priv->getObject();
169 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175 JSValueRef JSContactProperties::getName(JSContextRef context,
177 JSStringRef propertyName,
178 JSValueRef* exception)
180 //LogDebug("entered");
183 ContactConverterFactory::ConverterType converter =
184 ContactConverterFactory::getConverter(context);
185 ContactPropertiesPtr contactProperties = getPrivData(object);
186 return converter->toJSValueRef(contactProperties->getName());
188 Catch(WrtDeviceApis::Commons::Exception)
190 LogWarning("trying to get incorrect value");
193 return JSValueMakeUndefined(context);
196 bool JSContactProperties::setName(JSContextRef context,
198 JSStringRef propertyName,
200 JSValueRef* exception)
204 ContactPropertiesPtr contactProperties = getPrivData(object);
205 ContactConverterFactory::ConverterType converter =
206 ContactConverterFactory::getConverter(context);
207 contactProperties->setName(converter->toContactName(value));
210 Catch(WrtDeviceApis::Commons::Exception)
212 LogWarning("trying to set incorrect value");
215 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
219 JSValueRef JSContactProperties::getAccount(JSContextRef context,
221 JSStringRef propertyName,
222 JSValueRef* exception)
224 //LogDebug("entered");
227 ContactConverterFactory::ConverterType converter =
228 ContactConverterFactory::getConverter(context);
229 ContactPropertiesPtr contactProperties = getPrivData(object);
230 return converter->toJSValueRef(contactProperties->getAccount());
232 Catch(WrtDeviceApis::Commons::Exception)
234 LogWarning("trying to get incorrect value");
237 return JSValueMakeUndefined(context);
240 bool JSContactProperties::setAccount(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
248 ContactPropertiesPtr contactProperties = getPrivData(object);
249 ContactConverterFactory::ConverterType converter =
250 ContactConverterFactory::getConverter(context);
251 contactProperties->setAccount(converter->toContactAccount(value));
254 Catch(WrtDeviceApis::Commons::Exception)
256 LogWarning("trying to set incorrect value");
259 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
263 JSValueRef JSContactProperties::getAddresses(JSContextRef context,
265 JSStringRef propertyName,
266 JSValueRef* exception)
268 //LogDebug("entered");
271 ContactConverterFactory::ConverterType converter =
272 ContactConverterFactory::getConverter(context);
273 ContactPropertiesPtr contactProperties = getPrivData(object);
274 return converter->toJSValueRef(contactProperties->getAddresses());
276 Catch(WrtDeviceApis::Commons::Exception)
278 LogWarning("trying to get incorrect value");
281 return JSValueMakeUndefined(context);
284 bool JSContactProperties::setAddresses(JSContextRef context,
286 JSStringRef propertyName,
288 JSValueRef* exception)
292 ContactPropertiesPtr contactProperties = getPrivData(object);
293 ContactConverterFactory::ConverterType converter =
294 ContactConverterFactory::getConverter(context);
295 contactProperties->setAddresses(
296 converter->toContactAddressArray(value));
299 Catch(WrtDeviceApis::Commons::Exception)
301 LogWarning("trying to set incorrect value");
304 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
308 JSValueRef JSContactProperties::getPhotoURI(JSContextRef context,
310 JSStringRef propertyName,
311 JSValueRef* exception)
313 //LogDebug("entered");
316 ContactConverterFactory::ConverterType converter =
317 ContactConverterFactory::getConverter(context);
318 ContactPropertiesPtr contactProperties = getPrivData(object);
319 return converter->toJSValueRef(contactProperties->getPhotoURI());
321 Catch(WrtDeviceApis::Commons::Exception)
323 LogWarning("trying to get incorrect value");
326 return JSValueMakeUndefined(context);
329 bool JSContactProperties::setPhotoURI(JSContextRef context,
331 JSStringRef propertyName,
333 JSValueRef* exception)
337 ContactPropertiesPtr contactProperties = getPrivData(object);
338 ContactConverterFactory::ConverterType converter =
339 ContactConverterFactory::getConverter(context);
340 contactProperties->setPhotoURI(converter->toString(value));
343 Catch(WrtDeviceApis::Commons::Exception)
345 LogWarning("trying to set incorrect value");
348 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
352 JSValueRef JSContactProperties::getPhoneNumbers(JSContextRef context,
354 JSStringRef propertyName,
355 JSValueRef* exception)
357 //LogDebug("entered");
360 ContactConverterFactory::ConverterType converter =
361 ContactConverterFactory::getConverter(context);
362 ContactPropertiesPtr contactProperties = getPrivData(object);
363 return converter->toJSValueRef(contactProperties->getPhoneNumbers());
365 Catch(WrtDeviceApis::Commons::Exception)
367 LogWarning("trying to get incorrect value");
370 return JSValueMakeUndefined(context);
373 bool JSContactProperties::setPhoneNumbers(JSContextRef context,
375 JSStringRef propertyName,
377 JSValueRef* exception)
381 ContactPropertiesPtr contactProperties = getPrivData(object);
382 ContactConverterFactory::ConverterType converter =
383 ContactConverterFactory::getConverter(context);
384 contactProperties->setPhoneNumbers(
385 converter->toContactPhoneNumberArray(value));
388 Catch(WrtDeviceApis::Commons::Exception)
390 LogWarning("trying to set incorrect value");
393 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
397 JSValueRef JSContactProperties::getEmails(JSContextRef context,
399 JSStringRef propertyName,
400 JSValueRef* exception)
402 //LogDebug("entered");
405 ContactConverterFactory::ConverterType converter =
406 ContactConverterFactory::getConverter(context);
407 ContactPropertiesPtr contactProperties = getPrivData(object);
408 return converter->toJSValueRef(contactProperties->getEmails());
410 Catch(WrtDeviceApis::Commons::Exception)
412 LogWarning("trying to get incorrect value");
415 return JSValueMakeUndefined(context);
418 bool JSContactProperties::setEmails(JSContextRef context,
420 JSStringRef propertyName,
422 JSValueRef* exception)
426 ContactPropertiesPtr contactProperties = getPrivData(object);
427 ContactConverterFactory::ConverterType converter =
428 ContactConverterFactory::getConverter(context);
429 contactProperties->setEmails(converter->toContactEmailAddressArray(value));
432 Catch(WrtDeviceApis::Commons::Exception)
434 LogWarning("trying to set incorrect value");
437 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
441 JSValueRef JSContactProperties::getBirthday(JSContextRef context,
443 JSStringRef propertyName,
444 JSValueRef* exception)
446 //LogDebug("entered");
449 ContactConverterFactory::ConverterType converter =
450 ContactConverterFactory::getConverter(context);
451 ContactPropertiesPtr contactProperties = getPrivData(object);
452 if(contactProperties->getBirthdayIsSet())
453 return converter->toJSValueRef(contactProperties->getBirthday());
455 return JSValueMakeNull(context);
457 Catch(WrtDeviceApis::Commons::Exception)
459 LogWarning("trying to get incorrect value");
462 return JSValueMakeUndefined(context);
465 bool JSContactProperties::setBirthday(JSContextRef context,
467 JSStringRef propertyName,
469 JSValueRef* exception)
473 ContactPropertiesPtr contactProperties = getPrivData(object);
474 ContactConverterFactory::ConverterType converter =
475 ContactConverterFactory::getConverter(context);
476 contactProperties->setBirthday(converter->toDateTm(value));
479 Catch(WrtDeviceApis::Commons::Exception)
481 LogWarning("trying to set incorrect value");
484 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
488 JSValueRef JSContactProperties::getAnniversaries(JSContextRef context,
490 JSStringRef propertyName,
491 JSValueRef* exception)
493 //LogDebug("entered");
496 ContactConverterFactory::ConverterType converter =
497 ContactConverterFactory::getConverter(context);
498 ContactPropertiesPtr contactProperties = getPrivData(object);
499 return converter->toJSValueRef(contactProperties->getAnniversaries());
501 Catch(WrtDeviceApis::Commons::Exception)
503 LogWarning("trying to get incorrect value");
506 return JSValueMakeUndefined(context);
509 bool JSContactProperties::setAnniversaries(JSContextRef context,
511 JSStringRef propertyName,
513 JSValueRef* exception)
517 ContactPropertiesPtr contactProperties = getPrivData(object);
518 ContactConverterFactory::ConverterType converter =
519 ContactConverterFactory::getConverter(context);
520 contactProperties->setAnniversaries(
521 converter->toContactAnniversaryArray(value));
524 Catch(WrtDeviceApis::Commons::Exception)
526 LogWarning("trying to set incorrect value");
529 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
533 JSValueRef JSContactProperties::getOrganization(JSContextRef context,
535 JSStringRef propertyName,
536 JSValueRef* exception)
538 //LogDebug("entered");
541 ContactConverterFactory::ConverterType converter =
542 ContactConverterFactory::getConverter(context);
543 ContactPropertiesPtr contactProperties = getPrivData(object);
544 return converter->toJSValueRef(contactProperties->getOrganization());
546 Catch(WrtDeviceApis::Commons::Exception)
548 LogWarning("trying to get incorrect value");
551 return JSValueMakeUndefined(context);
554 bool JSContactProperties::setOrganization(JSContextRef context,
556 JSStringRef propertyName,
558 JSValueRef* exception)
562 ContactPropertiesPtr contactProperties = getPrivData(object);
563 ContactConverterFactory::ConverterType converter =
564 ContactConverterFactory::getConverter(context);
565 contactProperties->setOrganization(converter->toContactOrganization(value));
568 Catch(WrtDeviceApis::Commons::Exception)
570 LogWarning("trying to set incorrect value");
573 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
577 JSValueRef JSContactProperties::getNotes(JSContextRef context,
579 JSStringRef propertyName,
580 JSValueRef* exception)
582 //LogDebug("entered");
585 ContactConverterFactory::ConverterType converter =
586 ContactConverterFactory::getConverter(context);
587 ContactPropertiesPtr contactProperties = getPrivData(object);
588 return converter->toJSValueRef(contactProperties->getNotes());
590 Catch(WrtDeviceApis::Commons::Exception)
592 LogWarning("trying to get incorrect value");
595 return JSValueMakeUndefined(context);
598 bool JSContactProperties::setNotes(JSContextRef context,
600 JSStringRef propertyName,
602 JSValueRef* exception)
606 ContactPropertiesPtr contactProperties = getPrivData(object);
607 ContactConverterFactory::ConverterType converter =
608 ContactConverterFactory::getConverter(context);
609 contactProperties->setNotes(converter->toStringArray(value));
612 Catch(WrtDeviceApis::Commons::Exception)
614 LogWarning("trying to set incorrect value");
617 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
621 JSValueRef JSContactProperties::getUrls(JSContextRef context,
623 JSStringRef propertyName,
624 JSValueRef* exception)
626 //LogDebug("entered");
629 ContactConverterFactory::ConverterType converter =
630 ContactConverterFactory::getConverter(context);
631 ContactPropertiesPtr contactProperties = getPrivData(object);
632 return converter->toJSValueRef(contactProperties->getUrls());
634 Catch(WrtDeviceApis::Commons::Exception)
636 LogWarning("trying to get incorrect value");
639 return JSValueMakeUndefined(context);
642 bool JSContactProperties::setUrls(JSContextRef context,
644 JSStringRef propertyName,
646 JSValueRef* exception)
650 ContactPropertiesPtr contactProperties = getPrivData(object);
651 ContactConverterFactory::ConverterType converter =
652 ContactConverterFactory::getConverter(context);
653 contactProperties->setUrls(converter->toContactWebSiteArray(value));
656 Catch(WrtDeviceApis::Commons::Exception)
658 LogWarning("trying to set incorrect value");
661 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
665 JSValueRef JSContactProperties::getIsFavorite(JSContextRef context,
667 JSStringRef propertyName,
668 JSValueRef* exception)
670 //LogDebug("entered");
673 ContactConverterFactory::ConverterType converter =
674 ContactConverterFactory::getConverter(context);
675 ContactPropertiesPtr contactProperties = getPrivData(object);
676 return converter->toJSValueRef(contactProperties->getIsFavorite());
678 Catch(WrtDeviceApis::Commons::Exception)
680 LogWarning("trying to get incorrect value");
683 return JSValueMakeUndefined(context);
686 bool JSContactProperties::setIsFavorite(JSContextRef context,
688 JSStringRef propertyName,
690 JSValueRef* exception)
694 ContactPropertiesPtr contactProperties = getPrivData(object);
695 ContactConverterFactory::ConverterType converter =
696 ContactConverterFactory::getConverter(context);
697 contactProperties->setIsFavorite(converter->toBool(value));
700 Catch(WrtDeviceApis::Commons::Exception)
702 LogWarning("trying to set incorrect value");
705 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
709 JSValueRef JSContactProperties::getRingtoneURI(JSContextRef context,
711 JSStringRef propertyName,
712 JSValueRef* exception)
714 //LogDebug("entered");
717 ContactConverterFactory::ConverterType converter =
718 ContactConverterFactory::getConverter(context);
719 ContactPropertiesPtr contactProperties = getPrivData(object);
720 return converter->toJSValueRef(contactProperties->getRingtoneURI());
722 Catch(WrtDeviceApis::Commons::Exception)
724 LogWarning("trying to get incorrect value");
727 return JSValueMakeUndefined(context);
730 bool JSContactProperties::setRingtoneURI(JSContextRef context,
732 JSStringRef propertyName,
734 JSValueRef* exception)
738 ContactPropertiesPtr contactProperties = getPrivData(object);
739 ContactConverterFactory::ConverterType converter =
740 ContactConverterFactory::getConverter(context);
741 contactProperties->setRingtoneURI(converter->toString(value));
744 Catch(WrtDeviceApis::Commons::Exception)
746 LogWarning("trying to set incorrect value");
749 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
753 JSValueRef JSContactProperties::getCategories(JSContextRef context,
755 JSStringRef propertyName,
756 JSValueRef* exception)
758 //LogDebug("entered");
761 ContactConverterFactory::ConverterType converter =
762 ContactConverterFactory::getConverter(context);
763 ContactPropertiesPtr contactProperties = getPrivData(object);
764 return converter->toJSValueRef(contactProperties->getCategories());
766 Catch(WrtDeviceApis::Commons::Exception)
768 LogWarning("trying to get incorrect value");
771 return JSValueMakeUndefined(context);
774 bool JSContactProperties::setCategories(JSContextRef context,
776 JSStringRef propertyName,
778 JSValueRef* exception)
782 ContactPropertiesPtr contactProperties = getPrivData(object);
783 ContactConverterFactory::ConverterType converter =
784 ContactConverterFactory::getConverter(context);
785 contactProperties->setCategories(converter->toStringArray(value));
788 Catch(WrtDeviceApis::Commons::Exception)
790 LogWarning("trying to set incorrect value");
793 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");