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 JSContactManager.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #include "JSContactManager.h"
28 #include <CommonsJavaScript/JSCallbackManager.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <CommonsJavaScript/Converter.h>
31 #include <CommonsJavaScript/JSUtils.h>
32 #include <CommonsJavaScript/JSPendingOperation.h>
33 #include <JSTizenExceptionFactory.h>
34 #include <JSTizenException.h>
35 #include <SecurityExceptions.h>
36 #include <FilterConverter.h>
37 #include "ContactFactory.h"
38 #include "ContactTypes.h"
39 #include "plugin_config.h"
40 #include "ContactManagerController.h"
41 #include "ContactAsyncCallbackManager.h"
42 #include "ContactListenerManager.h"
43 #include "ContactConverter.h"
45 #include "JSContactManagerChangeCallbackManager.h"
46 #include "ContactFilterConverter.h"
47 #include <ArgumentValidator.h>
48 #include <JSWebAPIError.h>
49 #include <TimeTracer.h>
56 using namespace DeviceAPI::Common;
57 using namespace DeviceAPI::Tizen;
58 using namespace WrtDeviceApis::Commons;
59 using namespace WrtDeviceApis::CommonsJavaScript;
61 JSClassDefinition JSContactManager::m_classInfo = {
63 kJSClassAttributeNone,
73 NULL, //DeleteProperty,
74 NULL, //GetPropertyNames,
75 NULL, //CallAsFunction,
76 NULL, //CallAsConstructor,
78 NULL, //ConvertToType,
81 const JSClassDefinition* JSContactManager::getClassInfo()
86 JSStaticValue JSContactManager::m_property[] = {
90 JSStaticFunction JSContactManager::m_function[] = {
91 { "getAddressBooks", getAddressBooks, kJSPropertyAttributeNone },
92 { "getDefaultAddressBook", getDefaultAddressBook, kJSPropertyAttributeNone },
93 { "getUnifiedAddressBook", getUnifiedAddressBook, kJSPropertyAttributeNone },
94 { "getAddressBook", getAddressBook, kJSPropertyAttributeNone },
95 { "get", get, kJSPropertyAttributeNone },
96 { "update", update, kJSPropertyAttributeNone },
97 { "updateBatch", updateBatch, kJSPropertyAttributeNone },
98 { "remove", remove, kJSPropertyAttributeNone },
99 { "removeBatch", removeBatch, kJSPropertyAttributeNone },
100 { "find", find, kJSPropertyAttributeNone },
101 { "addChangeListener", addChangeListener, kJSPropertyAttributeNone },
102 { "removeChangeListener", removeChangeListener, kJSPropertyAttributeNone },
106 JSClassRef DLL_EXPORT JSContactManager::getClassRef()
109 m_classRef = JSClassCreate(&m_classInfo);
114 JSClassRef JSContactManager::m_classRef = JSClassCreate(JSContactManager::getClassInfo());
116 void JSContactManager::Initialize(JSContextRef context,
120 ContactManagerPtr contactManager = ContactFactory::getInstance().createContactManager();
122 ContactManagerController *priv = new ContactManagerController(context, contactManager);
124 JSObjectSetPrivate(object, static_cast<void*>(priv));
127 void JSContactManager::Finalize(JSObjectRef object)
130 ContactManagerController *priv = static_cast<ContactManagerController*>(JSObjectGetPrivate(object));
131 LoggerI("Delete address book manager");
135 JSValueRef JSContactManager::getAddressBooks(JSContextRef context,
137 JSObjectRef thisObject,
138 size_t argumentCount,
139 const JSValueRef arguments[],
140 JSValueRef* exception)
143 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
144 ContactManagerPtr contactManager;
145 JSContextRef gContext;
146 ContactManagerController *controller;
148 bool js2ndParamIsFunction = false;
151 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
153 ThrowMsg(InvalidArgumentException, "No private object.");
155 contactManager = controller->getObject();
156 gContext = controller->getContext();
158 LoggerE("No private object");
159 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
162 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOKS);
163 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
167 ArgumentValidator validator(context, argumentCount, arguments);
168 validator.toFunction(0, false);
169 JSObjectRef errorObj = validator.toFunction(1, true);
171 js2ndParamIsFunction = true;
172 } catch (const TypeMismatchException& err ) {
173 return JSWebAPIError::throwException(context, exception, err);
174 } catch(const BasePlatformException& err) {
175 return JSWebAPIError::throwException(context, exception, err);
176 } catch(const ConversionException& err) {
177 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
178 } catch(const NullPointerException& err) {
179 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
182 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
184 callbackManager->setOnSuccess(arguments[0]);
186 if(js2ndParamIsFunction)
187 callbackManager->setOnError(arguments[1]);
189 callbackManager->setObject(thisObject);
191 EventContactManagerGetAddressBooksPtr dplEvent(new EventContactManagerGetAddressBooks());
193 // set event handler's data
194 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
195 dplEvent->setForAsynchronousCall(controller);
197 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
198 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
201 contactManager->getAddressBooks(dplEvent);
202 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
204 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
205 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
208 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
209 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
210 return JSValueMakeUndefined(context);
213 JSValueRef JSContactManager::getDefaultAddressBook(JSContextRef context,
215 JSObjectRef thisObject,
216 size_t argumentCount,
217 const JSValueRef arguments[],
218 JSValueRef* exception)
221 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
222 ContactManagerController *priv =
223 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
225 LoggerE("private object is null");
226 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
229 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_DEFAULT_ADDRESS_BOOK);
230 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
232 ContactManagerPtr contactManager;
233 JSContextRef gContext;
235 contactManager = priv->getObject();
236 gContext = priv->getContext();
238 LoggerE("contact manager or context is null");
239 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
242 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
244 EventContactManagerGetDefaultAddressBookPtr dplEvent(new EventContactManagerGetDefaultAddressBook());
246 dplEvent->setForSynchronousCall();
249 contactManager->getDefaultAddressBook(dplEvent);
251 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
252 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
255 if (!dplEvent->getResult() || !dplEvent->getDefaultAddressBookIsSet())
257 std::stringstream oss;
258 switch (dplEvent->getExceptionCode())
260 case ExceptionCodes::NotFoundException:
261 case ExceptionCodes::InvalidArgumentException:
262 LoggerE("Not Found error");
263 oss << "No default address book";
264 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
266 case ExceptionCodes::PlatformException:
267 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
270 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
275 AddressBookPtr defaultAddressBook = dplEvent->getDefaultAddressBook();
277 JSValueRef resultValue;
279 resultValue = converter->toJSValueRef(defaultAddressBook);
281 LoggerE("Conversion error.");
282 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
285 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
289 JSValueRef JSContactManager::getUnifiedAddressBook(JSContextRef context,
291 JSObjectRef thisObject,
292 size_t argumentCount,
293 const JSValueRef arguments[],
294 JSValueRef* exception)
297 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
298 ContactManagerController *priv =
299 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
301 LoggerE("private object is null");
302 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
305 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_UNIFIED_ADDRESS_BOOK);
306 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
308 ContactManagerPtr contactManager;
309 JSContextRef gContext;
311 contactManager = priv->getObject();
312 gContext = priv->getContext();
314 LoggerE("contact manager or context is null");
315 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
318 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
320 EventContactManagerGetUnifiedAddressBookPtr dplEvent(new EventContactManagerGetUnifiedAddressBook());
322 dplEvent->setForSynchronousCall();
325 contactManager->getUnifiedAddressBook(dplEvent);
327 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
328 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
331 if (!dplEvent->getResult() || !dplEvent->getUnifiedAddressBookIsSet())
333 std::stringstream oss;
334 switch (dplEvent->getExceptionCode())
336 case ExceptionCodes::NotFoundException:
337 case ExceptionCodes::InvalidArgumentException:
338 LoggerE("Not Found error");
339 oss << "No default address book";
340 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
342 case ExceptionCodes::PlatformException:
343 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
346 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
351 AddressBookPtr unifiedAddressBook = dplEvent->getUnifiedAddressBook();
353 JSValueRef resultValue;
355 resultValue = converter->toJSValueRef(unifiedAddressBook);
357 LoggerE("Conversion error.");
358 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
361 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
365 JSValueRef JSContactManager::getAddressBook(JSContextRef context,
367 JSObjectRef thisObject,
368 size_t argumentCount,
369 const JSValueRef arguments[],
370 JSValueRef* exception)
373 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
374 ContactManagerController *priv =
375 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
377 LoggerE("private object is null");
378 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
381 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOK);
382 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
384 ContactManagerPtr contactManager;
385 JSContextRef gContext;
387 contactManager = priv->getObject();
388 gContext = priv->getContext();
390 LoggerE("contact manager or context is null");
391 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
394 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
396 std::string addressBookId;
398 ArgumentValidator validator(context, argumentCount, arguments);
399 addressBookId = validator.toString(0, false);
400 } catch (const TypeMismatchException& err ) {
401 return JSWebAPIError::throwException(context, exception, err);
402 } catch(const BasePlatformException& err) {
403 return JSWebAPIError::throwException(context, exception, err);
404 } catch(const ConversionException& err) {
405 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
406 } catch(const NullPointerException& err) {
407 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
410 EventContactManagerGetAddressBookPtr dplEvent(new EventContactManagerGetAddressBook());
412 dplEvent->setAddressBookId(addressBookId);
413 dplEvent->setForSynchronousCall();
416 contactManager->getAddressBook(dplEvent);
418 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
419 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
422 if (!dplEvent->getResult() || !dplEvent->getAddressBookIsSet())
424 std::stringstream oss;
425 switch (dplEvent->getExceptionCode())
427 case ExceptionCodes::NotFoundException:
428 case ExceptionCodes::InvalidArgumentException:
429 LoggerE("Not Found error : " << addressBookId);
430 oss << "No Contact id '" << addressBookId << "'";
431 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
433 case ExceptionCodes::PlatformException:
434 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
437 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
442 AddressBookPtr addressBook = dplEvent->getAddressBook();
444 JSValueRef resultValue;
446 resultValue = converter->toJSValueRef(addressBook);
448 LoggerE("Conversion error.");
449 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
452 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
456 JSValueRef JSContactManager::get(JSContextRef context,
458 JSObjectRef thisObject,
459 size_t argumentCount,
460 const JSValueRef arguments[],
461 JSValueRef* exception)
464 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
465 ContactManagerPtr addressBook;
466 ContactManagerController *controller;
469 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
471 ThrowMsg(InvalidArgumentException, "No private object.");
473 addressBook = controller->getObject();
475 LoggerE("No private object");
476 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
479 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET);
480 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
482 if (argumentCount < 1) {
483 /* 1st Argument must be string. */
484 LoggerE("1st argument must not be undefined.");
485 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "No Contact id 'undefined'");
490 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
492 ArgumentValidator validator(context, argumentCount, arguments);
493 id = validator.toString(0, false);
494 } catch (const TypeMismatchException& err ) {
495 return JSWebAPIError::throwException(context, exception, err);
496 } catch(const BasePlatformException& err) {
497 return JSWebAPIError::throwException(context, exception, err);
498 } catch(const ConversionException& err) {
499 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
500 } catch(const NullPointerException& err) {
501 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
504 EventContactManagerGetPtr dplEvent(new EventContactManagerGet());
507 dplEvent->setForSynchronousCall();
510 addressBook->get(dplEvent);
512 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
513 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
516 if (!dplEvent->getResult() || !dplEvent->getPersonIsSet())
518 std::stringstream oss;
519 switch (dplEvent->getExceptionCode())
521 case ExceptionCodes::NotFoundException:
522 case ExceptionCodes::InvalidArgumentException:
523 LoggerE("Not Found error : " << id);
524 oss << "No Person id '" << id << "'";
525 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
527 case ExceptionCodes::PlatformException:
528 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
531 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
536 PersonPtr person = dplEvent->getPerson();
540 result = converter->toJSValueRef(person);
542 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
543 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
546 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
550 JSValueRef JSContactManager::update(JSContextRef context,
552 JSObjectRef thisObject,
553 size_t argumentCount,
554 const JSValueRef arguments[],
555 JSValueRef* exception)
558 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
559 ContactManagerPtr addressBook;
560 ContactManagerController *controller;
563 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
565 ThrowMsg(InvalidArgumentException, "No private object.");
567 addressBook = controller->getObject();
569 LoggerE("No private object");
570 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
573 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE);
574 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
577 ArgumentValidator validator(context, argumentCount, arguments);
578 validator.toObject(0, false);
579 } catch (const TypeMismatchException& err ) {
580 return JSWebAPIError::throwException(context, exception, err);
581 } catch(const BasePlatformException& err) {
582 return JSWebAPIError::throwException(context, exception, err);
583 } catch(const ConversionException& err) {
584 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
585 } catch(const NullPointerException& err) {
586 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
589 PersonPtr person(NULL);
591 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
593 if (!JSPerson::isObjectOfClass(context, arguments[0]))
594 ThrowMsg(InvalidArgumentException, "1st argument is not a 'Contact object'.");
595 person = converter->toPerson(arguments[0]);
597 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
598 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument is not a 'Contact object'");
601 EventContactManagerUpdatePtr dplEvent(new EventContactManagerUpdate());
603 dplEvent->setPerson(person);
604 dplEvent->setForSynchronousCall();
607 addressBook->update(dplEvent);
609 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
610 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
613 if (!dplEvent->getResult())
615 std::stringstream oss;
616 switch (dplEvent->getExceptionCode())
618 case ExceptionCodes::NotFoundException:
619 case ExceptionCodes::InvalidArgumentException:
620 oss << "No Contact id '" << person->getId() << "'";
621 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
623 case ExceptionCodes::PlatformException:
624 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
627 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
632 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
633 return JSValueMakeUndefined(context);
636 JSValueRef JSContactManager::updateBatch(JSContextRef context,
638 JSObjectRef thisObject,
639 size_t argumentCount,
640 const JSValueRef arguments[],
641 JSValueRef* exception)
644 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
645 ContactManagerPtr addressBook;
646 JSContextRef gContext;
647 ContactManagerController *controller;
649 bool js2ndParamIsFunction = false;
650 bool js3rdParamIsFunction = false;
653 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
655 ThrowMsg(InvalidArgumentException, "No private object.");
657 addressBook = controller->getObject();
658 gContext = controller->getContext();
660 LoggerE("No private object");
661 return JSTizenExceptionFactory::postException(context, exception,
662 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
665 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE_BATCH);
666 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
669 ArgumentValidator validator(context, argumentCount, arguments);
671 validator.toObject(0, false);
672 JSObjectRef successObj = validator.toFunction(1, true);
674 js2ndParamIsFunction = true;
676 JSObjectRef errorObj = validator.toFunction(2, true);
678 js3rdParamIsFunction = true;
679 } catch (const TypeMismatchException& err ) {
680 return JSWebAPIError::throwException(context, exception, err);
681 } catch(const BasePlatformException& err) {
682 return JSWebAPIError::throwException(context, exception, err);
683 } catch(const ConversionException& err) {
684 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
685 } catch(const NullPointerException& err) {
686 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
689 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
691 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
693 if(js2ndParamIsFunction)
694 callbackManager->setOnSuccess(arguments[1]);
696 if(js3rdParamIsFunction)
697 callbackManager->setOnError(arguments[2]);
699 callbackManager->setObject(thisObject);
701 EventContactManagerUpdateBatchPtr dplEvent(new EventContactManagerUpdateBatch());
703 dplEvent->setPersons(converter->toPersonArray(arguments[0]));
704 } Catch(ConversionException) {
705 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
706 return JSTizenExceptionFactory::postException(context, exception,
707 JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be a 'function' or a 'null'");
710 // set event handler's data
711 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
712 dplEvent->setForAsynchronousCall(controller);
714 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
715 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
718 addressBook->updateBatch(dplEvent);
719 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
721 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
722 return JSTizenExceptionFactory::postException(context, exception,
723 JSTizenException::UNKNOWN_ERROR, "Internal error");
726 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
727 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
728 return JSValueMakeUndefined(context);
731 JSValueRef JSContactManager::remove(JSContextRef context,
733 JSObjectRef thisObject,
734 size_t argumentCount,
735 const JSValueRef arguments[],
736 JSValueRef* exception)
739 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
740 ContactManagerPtr addressBook;
741 ContactManagerController *controller;
744 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
746 ThrowMsg(InvalidArgumentException, "No private object.");
748 addressBook = controller->getObject();
750 LoggerE("No private object");
751 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
754 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE);
755 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
757 std::string personId;
759 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
761 ArgumentValidator validator(context, argumentCount, arguments);
762 personId = validator.toString(0, false);
763 } catch (const TypeMismatchException& err ) {
764 return JSWebAPIError::throwException(context, exception, err);
765 } catch(const BasePlatformException& err) {
766 return JSWebAPIError::throwException(context, exception, err);
767 } catch(const ConversionException& err) {
768 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
769 } catch(const NullPointerException& err) {
770 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
773 EventContactManagerRemovePtr dplEvent(new EventContactManagerRemove());
775 dplEvent->setPersonId(personId);
776 dplEvent->setForSynchronousCall();
779 addressBook->remove(dplEvent);
781 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
782 return JSTizenExceptionFactory::postException(context, exception,
783 JSTizenException::UNKNOWN_ERROR, "Internal error");
786 if (!dplEvent->getResult())
788 std::stringstream oss;
789 switch (dplEvent->getExceptionCode())
791 case ExceptionCodes::NotFoundException:
792 case ExceptionCodes::InvalidArgumentException:
793 LoggerE("Not Found error : " << personId);
794 oss << "No Contact id '" << personId << "'";
795 return JSTizenExceptionFactory::postException(context, exception,
796 JSTizenException::NOT_FOUND_ERROR, oss.str());
798 case ExceptionCodes::PlatformException:
799 return JSTizenExceptionFactory::postException(context, exception,
800 JSTizenException::UNKNOWN_ERROR, "Internal error");
803 return JSTizenExceptionFactory::postException(context, exception,
804 JSTizenException::UNKNOWN_ERROR, "Internal error");
809 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
810 return JSValueMakeUndefined(context);
813 JSValueRef JSContactManager::removeBatch(JSContextRef context,
815 JSObjectRef thisObject,
816 size_t argumentCount,
817 const JSValueRef arguments[],
818 JSValueRef* exception)
821 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
822 ContactManagerPtr addressBook;
823 JSContextRef gContext;
824 ContactManagerController *controller;
826 bool js2ndParamIsFunction = false;
827 bool js3rdParamIsFunction = false;
830 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
832 ThrowMsg(InvalidArgumentException, "No private object.");
834 addressBook = controller->getObject();
835 gContext = controller->getContext();
837 LoggerE("No private object");
838 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
841 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_BATCH);
842 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
845 ArgumentValidator validator(context, argumentCount, arguments);
847 validator.toObject(0, false);
848 JSObjectRef successObj = validator.toFunction(1, true);
850 js2ndParamIsFunction = true;
852 JSObjectRef errorObj = validator.toFunction(2, true);
854 js3rdParamIsFunction = true;
855 } catch (const TypeMismatchException& err ) {
856 return JSWebAPIError::throwException(context, exception, err);
857 } catch(const BasePlatformException& err) {
858 return JSWebAPIError::throwException(context, exception, err);
859 } catch(const ConversionException& err) {
860 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
861 } catch(const NullPointerException& err) {
862 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
865 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
867 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
869 if(js2ndParamIsFunction)
870 callbackManager->setOnSuccess(arguments[1]);
872 if(js3rdParamIsFunction)
873 callbackManager->setOnError(arguments[2]);
875 callbackManager->setObject(thisObject);
877 EventContactManagerRemoveBatchPtr dplEvent(new EventContactManagerRemoveBatch());
879 dplEvent->setPersonIds(converter->toStringArray(arguments[0]));
880 } Catch(ConversionException) {
881 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
882 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an array of 'Contact id'");
885 // set event handler's data
886 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
887 dplEvent->setForAsynchronousCall(controller);
889 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
890 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
893 addressBook->removeBatch(dplEvent);
894 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
896 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
897 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
900 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
901 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
902 return JSValueMakeUndefined(context);
905 JSValueRef JSContactManager::find(JSContextRef context,
907 JSObjectRef thisObject,
908 size_t argumentCount,
909 const JSValueRef arguments[],
910 JSValueRef* exception)
913 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
914 ContactManagerPtr addressBook;
915 JSContextRef gContext;
916 ContactManagerController *controller;
918 bool js2ndParamIsFunction = false;
919 bool js3rdParamIsObject = false;
920 bool js4thParamIsObject = false;
923 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
925 ThrowMsg(InvalidArgumentException, "No private object.");
927 addressBook = controller->getObject();
928 gContext = controller->getContext();
930 LoggerE("No private object");
931 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
934 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_FIND);
935 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
938 ArgumentValidator validator(context, argumentCount, arguments);
940 validator.toFunction(0, false);
941 JSObjectRef errorObj = validator.toFunction(1, true);
943 js2ndParamIsFunction = true;
945 JSObjectRef filterObj = validator.toObject(2, true);
947 js3rdParamIsObject = true;
949 JSObjectRef sortObj = validator.toObject(3, true);
951 js4thParamIsObject = true;
953 } catch (const TypeMismatchException& err ) {
954 return JSWebAPIError::throwException(context, exception, err);
955 } catch(const BasePlatformException& err) {
956 return JSWebAPIError::throwException(context, exception, err);
957 } catch(const ConversionException& err) {
958 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
959 } catch(const NullPointerException& err) {
960 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
963 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
965 callbackManager->setOnSuccess(arguments[0]);
967 if (js2ndParamIsFunction)
968 callbackManager->setOnError(arguments[1]);
970 callbackManager->setObject(thisObject);
972 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
973 PersonFilterConverterFactory::ConverterType filterConverter = PersonFilterConverterFactory::getConverter(context);
975 EventContactManagerFindPtr dplEvent(new EventContactManagerFind());
977 if (js3rdParamIsObject)
978 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
980 LoggerE("Error on 3rd parameter conversion : " << _rethrown_exception.GetMessage());
981 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an 'Filter object' or 'null'");
985 if (js4thParamIsObject)
986 dplEvent->setSortMode(filterConverter->toSortMode(arguments[3]));
988 LoggerE("Error on 4th parameter conversion : " << _rethrown_exception.GetMessage());
989 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "4th argument must be an 'SortMode object' or 'null'");
992 // set event handler's data
993 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
994 dplEvent->setForAsynchronousCall(controller);
996 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
997 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
1000 addressBook->find(dplEvent);
1001 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
1002 } Catch(Exception) {
1003 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1004 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1007 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1008 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
1009 return JSValueMakeUndefined(context);
1012 JSValueRef JSContactManager::addChangeListener(JSContextRef context,
1014 JSObjectRef thisObject,
1015 size_t argumentCount,
1016 const JSValueRef arguments[],
1017 JSValueRef* exception)
1020 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1021 ContactManagerPtr addressBook;
1022 JSContextRef gContext;
1023 ContactManagerController *controller;
1025 bool js2ndParamIsFunction = false;
1028 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1030 ThrowMsg(InvalidArgumentException, "No private object.");
1032 addressBook = controller->getObject();
1033 gContext = controller->getContext();
1034 } Catch(Exception) {
1035 LoggerE("No private object");
1036 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1039 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_ADD_CHANGE_LISTENER);
1040 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1042 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
1044 ArgumentValidator Argvalidator(context, argumentCount, arguments);
1046 Argvalidator.toObject(0, false);
1047 JSObjectRef errorObj = Argvalidator.toFunction(1, true);
1049 js2ndParamIsFunction = true;
1051 } catch (const TypeMismatchException& err ) {
1052 return JSWebAPIError::throwException(context, exception, err);
1053 } catch(const BasePlatformException& err) {
1054 return JSWebAPIError::throwException(context, exception, err);
1055 } catch(const ConversionException& err) {
1056 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1057 } catch(const NullPointerException& err) {
1058 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1061 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1063 JSValueRef onpersonsadded;
1064 JSValueRef onpersonsupdated;
1065 JSValueRef onpersonsdeleted;
1067 JSObjectRef callbackObject = converter->toJSObjectRef(arguments[0]);
1069 onpersonsadded = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsadded");
1070 if (validator->isNullOrUndefined(onpersonsadded))
1071 onpersonsadded = NULL;
1072 else if (!validator->isCallback(onpersonsadded)) {
1073 ThrowMsg(ConversionException, "2nd argument's onpersonsadded attribute is not a 'function'");
1076 onpersonsupdated = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsupdated");
1077 if (validator->isNullOrUndefined(onpersonsupdated))
1078 onpersonsupdated = NULL;
1079 else if (!validator->isCallback(onpersonsupdated)) {
1080 ThrowMsg(ConversionException, "2nd argument's onpersonsupdated attribute is not a 'function'");
1083 onpersonsdeleted = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsremoved");
1084 if (validator->isNullOrUndefined(onpersonsdeleted))
1085 onpersonsdeleted = NULL;
1086 else if (!validator->isCallback(onpersonsdeleted)) {
1087 ThrowMsg(ConversionException, "2nd argument's onpersonsremoved attribute is not a 'function'");
1090 if (onpersonsadded == NULL && onpersonsupdated == NULL && onpersonsdeleted == NULL)
1091 ThrowMsg(ConversionException, "2nd argument must have at least one function");
1093 } Catch(ConversionException) {
1094 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1095 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1098 JSContactManagerChangeCallbackManagerPtr callbackManager = JSContactManagerChangeCallbackManager::createObject(gContext);
1100 callbackManager->setOnPersonsAdded(onpersonsadded);
1101 callbackManager->setOnPersonsUpdated(onpersonsupdated);
1102 callbackManager->setOnPersonsDeleted(onpersonsdeleted);
1103 if(js2ndParamIsFunction)
1104 callbackManager->setOnError(arguments[1]);
1106 EventContactManagerChangeListenerEmitterPtr emitter(new EventContactManagerChangeListenerEmitter());
1108 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
1109 emitter->setListener(controller);
1111 EventContactManagerAddChangeListenerPtr dplEvent(new EventContactManagerAddChangeListener());
1113 dplEvent->setEmitter(emitter);
1114 dplEvent->setForSynchronousCall();
1117 addressBook->addChangeListener(dplEvent);
1118 } Catch(Exception) {
1119 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1120 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1123 if (!dplEvent->getResult() || !dplEvent->getIdIsSet())
1125 switch (dplEvent->getExceptionCode())
1127 case ExceptionCodes::InvalidArgumentException:
1128 case ExceptionCodes::PlatformException:
1129 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1132 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1137 long watchId = dplEvent->getId();
1139 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1140 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1141 ContactListenerManagerSingleton::Instance().registerListener(listenerItem, gContext);
1145 result = converter->toJSValueRefLong(watchId);
1146 } Catch(Exception) {
1147 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1148 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1150 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1154 JSValueRef JSContactManager::removeChangeListener(JSContextRef context,
1156 JSObjectRef thisObject,
1157 size_t argumentCount,
1158 const JSValueRef arguments[],
1159 JSValueRef* exception)
1162 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1163 ContactManagerPtr addressBook;
1164 JSContextRef gContext;
1165 ContactManagerController *controller;
1168 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1170 ThrowMsg(InvalidArgumentException, "No private object.");
1172 addressBook = controller->getObject();
1173 gContext = controller->getContext();
1174 } Catch(Exception) {
1175 LoggerE("No private object");
1176 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1179 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_CHANGE_LISTENER);
1180 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1182 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1186 ArgumentValidator validator(context, argumentCount, arguments);
1187 watchId = validator.toLong(0, false, 0);
1188 } catch (const TypeMismatchException& err ) {
1189 return JSWebAPIError::throwException(context, exception, err);
1190 } catch(const BasePlatformException& err) {
1191 return JSWebAPIError::throwException(context, exception, err);
1192 } catch(const ConversionException& err) {
1193 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1194 } catch(const NullPointerException& err) {
1195 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1198 EventContactManagerRemoveChangeListenerPtr dplEvent(new EventContactManagerRemoveChangeListener());
1200 dplEvent->setId(watchId);
1201 dplEvent->setForSynchronousCall();
1204 addressBook->removeChangeListener(dplEvent);
1205 } Catch(Exception) {
1206 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1207 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1210 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1211 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1212 ContactListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1214 if (!dplEvent->getResult())
1216 switch (dplEvent->getExceptionCode())
1218 case ExceptionCodes::InvalidArgumentException:
1219 case ExceptionCodes::NotFoundException:
1220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "Watch id not found");
1223 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1227 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1228 return JSValueMakeUndefined(context);