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>
55 using namespace DeviceAPI::Common;
56 using namespace DeviceAPI::Tizen;
57 using namespace WrtDeviceApis::Commons;
58 using namespace WrtDeviceApis::CommonsJavaScript;
60 JSClassDefinition JSContactManager::m_classInfo = {
62 kJSClassAttributeNone,
72 NULL, //DeleteProperty,
73 NULL, //GetPropertyNames,
74 NULL, //CallAsFunction,
75 NULL, //CallAsConstructor,
77 NULL, //ConvertToType,
80 const JSClassDefinition* JSContactManager::getClassInfo()
85 JSStaticValue JSContactManager::m_property[] = {
89 JSStaticFunction JSContactManager::m_function[] = {
90 { "getAddressBooks", getAddressBooks, kJSPropertyAttributeNone },
91 { "getDefaultAddressBook", getDefaultAddressBook, kJSPropertyAttributeNone },
92 { "getUnifiedAddressBook", getUnifiedAddressBook, kJSPropertyAttributeNone },
93 { "getAddressBook", getAddressBook, kJSPropertyAttributeNone },
94 { "get", get, kJSPropertyAttributeNone },
95 { "update", update, kJSPropertyAttributeNone },
96 { "updateBatch", updateBatch, kJSPropertyAttributeNone },
97 { "remove", remove, kJSPropertyAttributeNone },
98 { "removeBatch", removeBatch, kJSPropertyAttributeNone },
99 { "find", find, kJSPropertyAttributeNone },
100 { "addChangeListener", addChangeListener, kJSPropertyAttributeNone },
101 { "removeChangeListener", removeChangeListener, kJSPropertyAttributeNone },
105 JSClassRef JSContactManager::getClassRef()
108 m_classRef = JSClassCreate(&m_classInfo);
113 JSClassRef JSContactManager::m_classRef = JSClassCreate(JSContactManager::getClassInfo());
115 void JSContactManager::Initialize(JSContextRef context,
119 ContactManagerPtr contactManager = ContactFactory::getInstance().createContactManager();
121 ContactManagerController *priv = new ContactManagerController(context, contactManager);
123 JSObjectSetPrivate(object, static_cast<void*>(priv));
126 void JSContactManager::Finalize(JSObjectRef object)
129 ContactManagerController *priv = static_cast<ContactManagerController*>(JSObjectGetPrivate(object));
130 LoggerI("Delete address book manager");
134 JSValueRef JSContactManager::getAddressBooks(JSContextRef context,
136 JSObjectRef thisObject,
137 size_t argumentCount,
138 const JSValueRef arguments[],
139 JSValueRef* exception)
142 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
143 ContactManagerPtr contactManager;
144 JSContextRef gContext;
145 ContactManagerController *controller;
147 bool js2ndParamIsFunction = false;
150 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
152 ThrowMsg(InvalidArgumentException, "No private object.");
154 contactManager = controller->getObject();
155 gContext = controller->getContext();
157 LoggerE("No private object");
158 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
161 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOKS);
162 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
164 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
166 ArgumentValidator validator(context, argumentCount, arguments);
167 validator.toFunction(0, false);
168 JSObjectRef errorObj = validator.toFunction(1, true);
170 js2ndParamIsFunction = true;
171 } catch (const TypeMismatchException& err ) {
172 return JSWebAPIError::throwException(context, exception, err);
173 } catch(const BasePlatformException& err) {
174 return JSWebAPIError::throwException(context, exception, err);
175 } catch(const ConversionException& err) {
176 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
177 } catch(const NullPointerException& err) {
178 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
181 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
183 callbackManager->setOnSuccess(arguments[0]);
185 if(js2ndParamIsFunction)
186 callbackManager->setOnError(arguments[1]);
188 callbackManager->setObject(thisObject);
190 EventContactManagerGetAddressBooksPtr dplEvent(new EventContactManagerGetAddressBooks());
192 // set event handler's data
193 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
194 dplEvent->setForAsynchronousCall(controller);
196 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
197 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
200 contactManager->getAddressBooks(dplEvent);
201 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
203 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
204 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
207 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
208 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
209 return JSValueMakeUndefined(context);
212 JSValueRef JSContactManager::getDefaultAddressBook(JSContextRef context,
214 JSObjectRef thisObject,
215 size_t argumentCount,
216 const JSValueRef arguments[],
217 JSValueRef* exception)
220 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
221 ContactManagerController *priv =
222 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
224 LoggerE("private object is null");
225 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
228 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_DEFAULT_ADDRESS_BOOK);
229 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
231 ContactManagerPtr contactManager;
232 JSContextRef gContext;
234 contactManager = priv->getObject();
235 gContext = priv->getContext();
237 LoggerE("contact manager or context is null");
238 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
241 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
243 EventContactManagerGetDefaultAddressBookPtr dplEvent(new EventContactManagerGetDefaultAddressBook());
245 dplEvent->setForSynchronousCall();
248 contactManager->getDefaultAddressBook(dplEvent);
250 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
251 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
254 if (!dplEvent->getResult() || !dplEvent->getDefaultAddressBookIsSet())
256 std::stringstream oss;
257 switch (dplEvent->getExceptionCode())
259 case ExceptionCodes::NotFoundException:
260 case ExceptionCodes::InvalidArgumentException:
261 LoggerE("Not Found error");
262 oss << "No default address book";
263 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
265 case ExceptionCodes::PlatformException:
266 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
269 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
274 AddressBookPtr defaultAddressBook = dplEvent->getDefaultAddressBook();
276 JSValueRef resultValue;
278 resultValue = converter->toJSValueRef(defaultAddressBook);
280 LoggerE("Conversion error.");
281 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
284 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
288 JSValueRef JSContactManager::getUnifiedAddressBook(JSContextRef context,
290 JSObjectRef thisObject,
291 size_t argumentCount,
292 const JSValueRef arguments[],
293 JSValueRef* exception)
296 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
297 ContactManagerController *priv =
298 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
300 LoggerE("private object is null");
301 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
304 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_UNIFIED_ADDRESS_BOOK);
305 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
307 ContactManagerPtr contactManager;
308 JSContextRef gContext;
310 contactManager = priv->getObject();
311 gContext = priv->getContext();
313 LoggerE("contact manager or context is null");
314 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
317 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
319 EventContactManagerGetUnifiedAddressBookPtr dplEvent(new EventContactManagerGetUnifiedAddressBook());
321 dplEvent->setForSynchronousCall();
324 contactManager->getUnifiedAddressBook(dplEvent);
326 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
327 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
330 if (!dplEvent->getResult() || !dplEvent->getUnifiedAddressBookIsSet())
332 std::stringstream oss;
333 switch (dplEvent->getExceptionCode())
335 case ExceptionCodes::NotFoundException:
336 case ExceptionCodes::InvalidArgumentException:
337 LoggerE("Not Found error");
338 oss << "No default address book";
339 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
341 case ExceptionCodes::PlatformException:
342 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
350 AddressBookPtr unifiedAddressBook = dplEvent->getUnifiedAddressBook();
352 JSValueRef resultValue;
354 resultValue = converter->toJSValueRef(unifiedAddressBook);
356 LoggerE("Conversion error.");
357 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
360 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
364 JSValueRef JSContactManager::getAddressBook(JSContextRef context,
366 JSObjectRef thisObject,
367 size_t argumentCount,
368 const JSValueRef arguments[],
369 JSValueRef* exception)
372 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
373 ContactManagerController *priv =
374 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
376 LoggerE("private object is null");
377 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
380 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOK);
381 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
383 ContactManagerPtr contactManager;
384 JSContextRef gContext;
386 contactManager = priv->getObject();
387 gContext = priv->getContext();
389 LoggerE("contact manager or context is null");
390 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
393 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
395 std::string addressBookId;
397 ArgumentValidator validator(context, argumentCount, arguments);
398 addressBookId = validator.toString(0, false);
399 } catch (const TypeMismatchException& err ) {
400 return JSWebAPIError::throwException(context, exception, err);
401 } catch(const BasePlatformException& err) {
402 return JSWebAPIError::throwException(context, exception, err);
403 } catch(const ConversionException& err) {
404 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
405 } catch(const NullPointerException& err) {
406 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
409 EventContactManagerGetAddressBookPtr dplEvent(new EventContactManagerGetAddressBook());
411 dplEvent->setAddressBookId(addressBookId);
412 dplEvent->setForSynchronousCall();
415 contactManager->getAddressBook(dplEvent);
417 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
418 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
421 if (!dplEvent->getResult() || !dplEvent->getAddressBookIsSet())
423 std::stringstream oss;
424 switch (dplEvent->getExceptionCode())
426 case ExceptionCodes::NotFoundException:
427 case ExceptionCodes::InvalidArgumentException:
428 LoggerE("Not Found error : " << addressBookId);
429 oss << "No Contact id '" << addressBookId << "'";
430 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
432 case ExceptionCodes::PlatformException:
433 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
436 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
441 AddressBookPtr addressBook = dplEvent->getAddressBook();
443 JSValueRef resultValue;
445 resultValue = converter->toJSValueRef(addressBook);
447 LoggerE("Conversion error.");
448 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
451 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
455 JSValueRef JSContactManager::get(JSContextRef context,
457 JSObjectRef thisObject,
458 size_t argumentCount,
459 const JSValueRef arguments[],
460 JSValueRef* exception)
463 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
464 ContactManagerPtr addressBook;
465 ContactManagerController *controller;
468 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
470 ThrowMsg(InvalidArgumentException, "No private object.");
472 addressBook = controller->getObject();
474 LoggerE("No private object");
475 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
478 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET);
479 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
481 if (argumentCount < 1) {
482 /* 1st Argument must be string. */
483 LoggerE("1st argument must not be undefined.");
484 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "No Contact id 'undefined'");
489 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
491 ArgumentValidator validator(context, argumentCount, arguments);
492 id = validator.toString(0, false);
493 } catch (const TypeMismatchException& err ) {
494 return JSWebAPIError::throwException(context, exception, err);
495 } catch(const BasePlatformException& err) {
496 return JSWebAPIError::throwException(context, exception, err);
497 } catch(const ConversionException& err) {
498 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
499 } catch(const NullPointerException& err) {
500 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
503 EventContactManagerGetPtr dplEvent(new EventContactManagerGet());
506 dplEvent->setForSynchronousCall();
509 addressBook->get(dplEvent);
511 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
512 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
515 if (!dplEvent->getResult() || !dplEvent->getPersonIsSet())
517 std::stringstream oss;
518 switch (dplEvent->getExceptionCode())
520 case ExceptionCodes::NotFoundException:
521 case ExceptionCodes::InvalidArgumentException:
522 LoggerE("Not Found error : " << id);
523 oss << "No Person id '" << id << "'";
524 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
526 case ExceptionCodes::PlatformException:
527 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
530 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
535 PersonPtr person = dplEvent->getPerson();
539 result = converter->toJSValueRef(person);
541 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
542 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
545 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
549 JSValueRef JSContactManager::update(JSContextRef context,
551 JSObjectRef thisObject,
552 size_t argumentCount,
553 const JSValueRef arguments[],
554 JSValueRef* exception)
557 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
558 ContactManagerPtr addressBook;
559 ContactManagerController *controller;
562 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
564 ThrowMsg(InvalidArgumentException, "No private object.");
566 addressBook = controller->getObject();
568 LoggerE("No private object");
569 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
572 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE);
573 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
576 ArgumentValidator validator(context, argumentCount, arguments);
577 validator.toObject(0, false);
578 } catch (const TypeMismatchException& err ) {
579 return JSWebAPIError::throwException(context, exception, err);
580 } catch(const BasePlatformException& err) {
581 return JSWebAPIError::throwException(context, exception, err);
582 } catch(const ConversionException& err) {
583 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
584 } catch(const NullPointerException& err) {
585 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
588 PersonPtr person(NULL);
590 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
592 if (!JSPerson::isObjectOfClass(context, arguments[0]))
593 ThrowMsg(InvalidArgumentException, "1st argument is not a 'Contact object'.");
594 person = converter->toPerson(arguments[0]);
596 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
597 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument is not a 'Contact object'");
600 EventContactManagerUpdatePtr dplEvent(new EventContactManagerUpdate());
602 dplEvent->setPerson(person);
603 dplEvent->setForSynchronousCall();
606 addressBook->update(dplEvent);
608 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
609 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
612 if (!dplEvent->getResult())
614 std::stringstream oss;
615 switch (dplEvent->getExceptionCode())
617 case ExceptionCodes::NotFoundException:
618 case ExceptionCodes::InvalidArgumentException:
619 oss << "No Contact id '" << person->getId() << "'";
620 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
622 case ExceptionCodes::PlatformException:
623 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
626 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
631 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
632 return JSValueMakeUndefined(context);
635 JSValueRef JSContactManager::updateBatch(JSContextRef context,
637 JSObjectRef thisObject,
638 size_t argumentCount,
639 const JSValueRef arguments[],
640 JSValueRef* exception)
643 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
644 ContactManagerPtr addressBook;
645 JSContextRef gContext;
646 ContactManagerController *controller;
648 bool js2ndParamIsFunction = false;
649 bool js3rdParamIsFunction = false;
652 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
654 ThrowMsg(InvalidArgumentException, "No private object.");
656 addressBook = controller->getObject();
657 gContext = controller->getContext();
659 LoggerE("No private object");
660 return JSTizenExceptionFactory::postException(context, exception,
661 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
664 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE_BATCH);
665 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
668 ArgumentValidator validator(context, argumentCount, arguments);
670 validator.toObject(0, false);
671 JSObjectRef successObj = validator.toFunction(1, true);
673 js2ndParamIsFunction = true;
675 JSObjectRef errorObj = validator.toFunction(2, true);
677 js3rdParamIsFunction = true;
678 } catch (const TypeMismatchException& err ) {
679 return JSWebAPIError::throwException(context, exception, err);
680 } catch(const BasePlatformException& err) {
681 return JSWebAPIError::throwException(context, exception, err);
682 } catch(const ConversionException& err) {
683 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
684 } catch(const NullPointerException& err) {
685 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
688 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
690 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
692 if(js2ndParamIsFunction)
693 callbackManager->setOnSuccess(arguments[1]);
695 if(js3rdParamIsFunction)
696 callbackManager->setOnError(arguments[2]);
698 callbackManager->setObject(thisObject);
700 EventContactManagerUpdateBatchPtr dplEvent(new EventContactManagerUpdateBatch());
702 dplEvent->setPersons(converter->toPersonArray(arguments[0]));
703 } Catch(ConversionException) {
704 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
705 return JSTizenExceptionFactory::postException(context, exception,
706 JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be a 'function' or a 'null'");
709 // set event handler's data
710 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
711 dplEvent->setForAsynchronousCall(controller);
713 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
714 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
717 addressBook->updateBatch(dplEvent);
718 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
720 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
721 return JSTizenExceptionFactory::postException(context, exception,
722 JSTizenException::UNKNOWN_ERROR, "Internal error");
725 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
726 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
727 return JSValueMakeUndefined(context);
730 JSValueRef JSContactManager::remove(JSContextRef context,
732 JSObjectRef thisObject,
733 size_t argumentCount,
734 const JSValueRef arguments[],
735 JSValueRef* exception)
738 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
739 ContactManagerPtr addressBook;
740 ContactManagerController *controller;
743 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
745 ThrowMsg(InvalidArgumentException, "No private object.");
747 addressBook = controller->getObject();
749 LoggerE("No private object");
750 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
753 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE);
754 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
756 std::string personId;
758 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
760 ArgumentValidator validator(context, argumentCount, arguments);
761 personId = validator.toString(0, false);
762 } catch (const TypeMismatchException& err ) {
763 return JSWebAPIError::throwException(context, exception, err);
764 } catch(const BasePlatformException& err) {
765 return JSWebAPIError::throwException(context, exception, err);
766 } catch(const ConversionException& err) {
767 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
768 } catch(const NullPointerException& err) {
769 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
772 EventContactManagerRemovePtr dplEvent(new EventContactManagerRemove());
774 dplEvent->setPersonId(personId);
775 dplEvent->setForSynchronousCall();
778 addressBook->remove(dplEvent);
780 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
781 return JSTizenExceptionFactory::postException(context, exception,
782 JSTizenException::UNKNOWN_ERROR, "Internal error");
785 if (!dplEvent->getResult())
787 std::stringstream oss;
788 switch (dplEvent->getExceptionCode())
790 case ExceptionCodes::NotFoundException:
791 case ExceptionCodes::InvalidArgumentException:
792 LoggerE("Not Found error : " << personId);
793 oss << "No Contact id '" << personId << "'";
794 return JSTizenExceptionFactory::postException(context, exception,
795 JSTizenException::NOT_FOUND_ERROR, oss.str());
797 case ExceptionCodes::PlatformException:
798 return JSTizenExceptionFactory::postException(context, exception,
799 JSTizenException::UNKNOWN_ERROR, "Internal error");
802 return JSTizenExceptionFactory::postException(context, exception,
803 JSTizenException::UNKNOWN_ERROR, "Internal error");
808 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
809 return JSValueMakeUndefined(context);
812 JSValueRef JSContactManager::removeBatch(JSContextRef context,
814 JSObjectRef thisObject,
815 size_t argumentCount,
816 const JSValueRef arguments[],
817 JSValueRef* exception)
820 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
821 ContactManagerPtr addressBook;
822 JSContextRef gContext;
823 ContactManagerController *controller;
825 bool js2ndParamIsFunction = false;
826 bool js3rdParamIsFunction = false;
829 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
831 ThrowMsg(InvalidArgumentException, "No private object.");
833 addressBook = controller->getObject();
834 gContext = controller->getContext();
836 LoggerE("No private object");
837 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
840 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_BATCH);
841 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
844 ArgumentValidator validator(context, argumentCount, arguments);
846 validator.toObject(0, false);
847 JSObjectRef successObj = validator.toFunction(1, true);
849 js2ndParamIsFunction = true;
851 JSObjectRef errorObj = validator.toFunction(2, true);
853 js3rdParamIsFunction = true;
854 } catch (const TypeMismatchException& err ) {
855 return JSWebAPIError::throwException(context, exception, err);
856 } catch(const BasePlatformException& err) {
857 return JSWebAPIError::throwException(context, exception, err);
858 } catch(const ConversionException& err) {
859 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
860 } catch(const NullPointerException& err) {
861 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
864 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
866 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
868 if(js2ndParamIsFunction)
869 callbackManager->setOnSuccess(arguments[1]);
871 if(js3rdParamIsFunction)
872 callbackManager->setOnError(arguments[2]);
874 callbackManager->setObject(thisObject);
876 EventContactManagerRemoveBatchPtr dplEvent(new EventContactManagerRemoveBatch());
878 dplEvent->setPersonIds(converter->toStringArray(arguments[0]));
879 } Catch(ConversionException) {
880 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
881 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an array of 'Contact id'");
884 // set event handler's data
885 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
886 dplEvent->setForAsynchronousCall(controller);
888 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
889 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
892 addressBook->removeBatch(dplEvent);
893 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
895 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
896 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
899 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
900 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
901 return JSValueMakeUndefined(context);
904 JSValueRef JSContactManager::find(JSContextRef context,
906 JSObjectRef thisObject,
907 size_t argumentCount,
908 const JSValueRef arguments[],
909 JSValueRef* exception)
912 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
913 ContactManagerPtr addressBook;
914 JSContextRef gContext;
915 ContactManagerController *controller;
917 bool js2ndParamIsFunction = false;
918 bool js3rdParamIsObject = false;
919 bool js4thParamIsObject = false;
922 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
924 ThrowMsg(InvalidArgumentException, "No private object.");
926 addressBook = controller->getObject();
927 gContext = controller->getContext();
929 LoggerE("No private object");
930 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
933 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_FIND);
934 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
937 ArgumentValidator validator(context, argumentCount, arguments);
939 validator.toFunction(0, false);
940 JSObjectRef errorObj = validator.toFunction(1, true);
942 js2ndParamIsFunction = true;
944 JSObjectRef filterObj = validator.toObject(2, true);
946 js3rdParamIsObject = true;
948 JSObjectRef sortObj = validator.toObject(3, true);
950 js4thParamIsObject = true;
952 } catch (const TypeMismatchException& err ) {
953 return JSWebAPIError::throwException(context, exception, err);
954 } catch(const BasePlatformException& err) {
955 return JSWebAPIError::throwException(context, exception, err);
956 } catch(const ConversionException& err) {
957 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
958 } catch(const NullPointerException& err) {
959 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
962 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
964 callbackManager->setOnSuccess(arguments[0]);
966 if (js2ndParamIsFunction)
967 callbackManager->setOnError(arguments[1]);
969 callbackManager->setObject(thisObject);
971 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
972 PersonFilterConverterFactory::ConverterType filterConverter = PersonFilterConverterFactory::getConverter(context);
974 EventContactManagerFindPtr dplEvent(new EventContactManagerFind());
976 if (js3rdParamIsObject)
977 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
979 LoggerE("Error on 3rd parameter conversion : " << _rethrown_exception.GetMessage());
980 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an 'Filter object' or 'null'");
984 if (js4thParamIsObject)
985 dplEvent->setSortMode(filterConverter->toSortMode(arguments[3]));
987 LoggerE("Error on 4th parameter conversion : " << _rethrown_exception.GetMessage());
988 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "4th argument must be an 'SortMode object' or 'null'");
991 // set event handler's data
992 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
993 dplEvent->setForAsynchronousCall(controller);
995 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
996 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
999 addressBook->find(dplEvent);
1000 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
1001 } Catch(Exception) {
1002 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1003 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1006 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1007 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
1008 return JSValueMakeUndefined(context);
1011 JSValueRef JSContactManager::addChangeListener(JSContextRef context,
1013 JSObjectRef thisObject,
1014 size_t argumentCount,
1015 const JSValueRef arguments[],
1016 JSValueRef* exception)
1019 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1020 ContactManagerPtr addressBook;
1021 JSContextRef gContext;
1022 ContactManagerController *controller;
1024 bool js2ndParamIsFunction = false;
1027 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1029 ThrowMsg(InvalidArgumentException, "No private object.");
1031 addressBook = controller->getObject();
1032 gContext = controller->getContext();
1033 } Catch(Exception) {
1034 LoggerE("No private object");
1035 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1038 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_ADD_CHANGE_LISTENER);
1039 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1041 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
1043 ArgumentValidator Argvalidator(context, argumentCount, arguments);
1045 Argvalidator.toObject(0, false);
1046 JSObjectRef errorObj = Argvalidator.toFunction(1, true);
1048 js2ndParamIsFunction = true;
1050 } catch (const TypeMismatchException& err ) {
1051 return JSWebAPIError::throwException(context, exception, err);
1052 } catch(const BasePlatformException& err) {
1053 return JSWebAPIError::throwException(context, exception, err);
1054 } catch(const ConversionException& err) {
1055 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1056 } catch(const NullPointerException& err) {
1057 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1060 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1062 JSValueRef onpersonsadded;
1063 JSValueRef onpersonsupdated;
1064 JSValueRef onpersonsdeleted;
1066 JSObjectRef callbackObject = converter->toJSObjectRef(arguments[0]);
1068 onpersonsadded = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsadded");
1069 if (validator->isNullOrUndefined(onpersonsadded))
1070 onpersonsadded = NULL;
1071 else if (!validator->isCallback(onpersonsadded)) {
1072 ThrowMsg(ConversionException, "2nd argument's onpersonsadded attribute is not a 'function'");
1075 onpersonsupdated = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsupdated");
1076 if (validator->isNullOrUndefined(onpersonsupdated))
1077 onpersonsupdated = NULL;
1078 else if (!validator->isCallback(onpersonsupdated)) {
1079 ThrowMsg(ConversionException, "2nd argument's onpersonsupdated attribute is not a 'function'");
1082 onpersonsdeleted = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsremoved");
1083 if (validator->isNullOrUndefined(onpersonsdeleted))
1084 onpersonsdeleted = NULL;
1085 else if (!validator->isCallback(onpersonsdeleted)) {
1086 ThrowMsg(ConversionException, "2nd argument's onpersonsremoved attribute is not a 'function'");
1089 if (onpersonsadded == NULL && onpersonsupdated == NULL && onpersonsdeleted == NULL)
1090 ThrowMsg(ConversionException, "2nd argument must have at least one function");
1092 } Catch(ConversionException) {
1093 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1094 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1097 JSContactManagerChangeCallbackManagerPtr callbackManager = JSContactManagerChangeCallbackManager::createObject(gContext);
1099 callbackManager->setOnPersonsAdded(onpersonsadded);
1100 callbackManager->setOnPersonsUpdated(onpersonsupdated);
1101 callbackManager->setOnPersonsDeleted(onpersonsdeleted);
1102 if(js2ndParamIsFunction)
1103 callbackManager->setOnError(arguments[1]);
1105 EventContactManagerChangeListenerEmitterPtr emitter(new EventContactManagerChangeListenerEmitter());
1107 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
1108 emitter->setListener(controller);
1110 EventContactManagerAddChangeListenerPtr dplEvent(new EventContactManagerAddChangeListener());
1112 dplEvent->setEmitter(emitter);
1113 dplEvent->setForSynchronousCall();
1116 addressBook->addChangeListener(dplEvent);
1117 } Catch(Exception) {
1118 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1119 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1122 if (!dplEvent->getResult() || !dplEvent->getIdIsSet())
1124 switch (dplEvent->getExceptionCode())
1126 case ExceptionCodes::InvalidArgumentException:
1127 case ExceptionCodes::PlatformException:
1128 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1131 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1136 long watchId = dplEvent->getId();
1138 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1139 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1140 ContactListenerManagerSingleton::Instance().registerListener(listenerItem, gContext);
1144 result = converter->toJSValueRefLong(watchId);
1145 } Catch(Exception) {
1146 LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
1147 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1149 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1153 JSValueRef JSContactManager::removeChangeListener(JSContextRef context,
1155 JSObjectRef thisObject,
1156 size_t argumentCount,
1157 const JSValueRef arguments[],
1158 JSValueRef* exception)
1161 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1162 ContactManagerPtr addressBook;
1163 JSContextRef gContext;
1164 ContactManagerController *controller;
1167 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1169 ThrowMsg(InvalidArgumentException, "No private object.");
1171 addressBook = controller->getObject();
1172 gContext = controller->getContext();
1173 } Catch(Exception) {
1174 LoggerE("No private object");
1175 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1178 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_CHANGE_LISTENER);
1179 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1181 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1185 ArgumentValidator validator(context, argumentCount, arguments);
1186 watchId = validator.toLong(0, false, 0);
1187 } catch (const TypeMismatchException& err ) {
1188 return JSWebAPIError::throwException(context, exception, err);
1189 } catch(const BasePlatformException& err) {
1190 return JSWebAPIError::throwException(context, exception, err);
1191 } catch(const ConversionException& err) {
1192 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1193 } catch(const NullPointerException& err) {
1194 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
1197 EventContactManagerRemoveChangeListenerPtr dplEvent(new EventContactManagerRemoveChangeListener());
1199 dplEvent->setId(watchId);
1200 dplEvent->setForSynchronousCall();
1203 addressBook->removeChangeListener(dplEvent);
1204 } Catch(Exception) {
1205 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1206 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1209 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1210 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1211 ContactListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1213 if (!dplEvent->getResult())
1215 switch (dplEvent->getExceptionCode())
1217 case ExceptionCodes::InvalidArgumentException:
1218 case ExceptionCodes::NotFoundException:
1219 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "Watch id not found");
1222 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1226 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1227 return JSValueMakeUndefined(context);