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 <dpl/log/log.h>
29 #include <CommonsJavaScript/JSCallbackManager.h>
30 #include <CommonsJavaScript/Validator.h>
31 #include <CommonsJavaScript/Converter.h>
32 #include <CommonsJavaScript/JSUtils.h>
33 #include <CommonsJavaScript/JSPendingOperation.h>
34 #include <JSTizenExceptionFactory.h>
35 #include <JSTizenException.h>
36 #include <SecurityExceptions.h>
37 #include <FilterConverter.h>
38 #include "ContactFactory.h"
39 #include "ContactTypes.h"
40 #include "plugin_config.h"
41 #include "ContactManagerController.h"
42 #include "ContactAsyncCallbackManager.h"
43 #include "ContactListenerManager.h"
44 #include "ContactConverter.h"
46 #include "JSContactManagerChangeCallbackManager.h"
51 using namespace DeviceAPI::Common;
52 using namespace DeviceAPI::Tizen;
53 using namespace WrtDeviceApis::Commons;
54 using namespace WrtDeviceApis::CommonsJavaScript;
56 JSClassDefinition JSContactManager::m_classInfo = {
58 kJSClassAttributeNone,
68 NULL, //DeleteProperty,
69 NULL, //GetPropertyNames,
70 NULL, //CallAsFunction,
71 NULL, //CallAsConstructor,
73 NULL, //ConvertToType,
76 const JSClassDefinition* JSContactManager::getClassInfo()
81 JSStaticValue JSContactManager::m_property[] = {
85 JSStaticFunction JSContactManager::m_function[] = {
86 { "getAddressBooks", getAddressBooks, kJSPropertyAttributeNone },
87 { "getDefaultAddressBook", getDefaultAddressBook, kJSPropertyAttributeNone },
88 { "getUnifiedAddressBook", getUnifiedAddressBook, kJSPropertyAttributeNone },
89 { "getAddressBook", getAddressBook, kJSPropertyAttributeNone },
90 { "get", get, kJSPropertyAttributeNone },
91 { "update", update, kJSPropertyAttributeNone },
92 { "updateBatch", updateBatch, kJSPropertyAttributeNone },
93 { "remove", remove, kJSPropertyAttributeNone },
94 { "removeBatch", removeBatch, kJSPropertyAttributeNone },
95 { "find", find, kJSPropertyAttributeNone },
96 { "addChangeListener", addChangeListener, kJSPropertyAttributeNone },
97 { "removeChangeListener", removeChangeListener, kJSPropertyAttributeNone },
101 JSClassRef JSContactManager::getClassRef()
104 m_classRef = JSClassCreate(&m_classInfo);
109 JSClassRef JSContactManager::m_classRef = JSClassCreate(JSContactManager::getClassInfo());
111 void JSContactManager::Initialize(JSContextRef context,
115 ContactManagerPtr contactManager = ContactFactory::getInstance().createContactManager();
117 ContactManagerController *priv = new ContactManagerController(context, contactManager);
119 JSObjectSetPrivate(object, static_cast<void*>(priv));
122 void JSContactManager::Finalize(JSObjectRef object)
125 ContactManagerController *priv = static_cast<ContactManagerController*>(JSObjectGetPrivate(object));
126 LogInfo("Delete address book manager");
130 JSValueRef JSContactManager::getAddressBooks(JSContextRef context,
132 JSObjectRef thisObject,
133 size_t argumentCount,
134 const JSValueRef arguments[],
135 JSValueRef* exception)
138 ContactManagerPtr contactManager;
139 JSContextRef gContext;
140 ContactManagerController *controller;
142 bool js2ndParamIsFunction = false;
145 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
147 ThrowMsg(InvalidArgumentException, "No private object.");
149 contactManager = controller->getObject();
150 gContext = controller->getContext();
152 LogError("No private object");
153 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
156 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOKS);
157 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
159 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
161 if (argumentCount < 1)
162 ThrowMsg(InvalidArgumentException, "1st argument must be a 'function'");
164 if (!validator->isCallback(arguments[0]))
165 ThrowMsg(InvalidArgumentException, "1st argument must be a 'function'");
167 if (argumentCount >= 2)
169 if (validator->isCallback(arguments[1]))
170 js2ndParamIsFunction = true;
172 if (!js2ndParamIsFunction &&
173 !JSValueIsNull(context, arguments[1]) &&
174 !JSValueIsUndefined(context, arguments[1]))
175 ThrowMsg(InvalidArgumentException, "2st argument must be a 'function' or a 'null");
178 } Catch(Exception ) {
179 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
180 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
183 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
185 callbackManager->setOnSuccess(arguments[0]);
187 if(js2ndParamIsFunction)
188 callbackManager->setOnError(arguments[1]);
190 callbackManager->setObject(thisObject);
192 EventContactManagerGetAddressBooksPtr dplEvent(new EventContactManagerGetAddressBooks());
194 // set event handler's data
195 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
196 dplEvent->setForAsynchronousCall(controller);
198 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
199 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
202 contactManager->getAddressBooks(dplEvent);
203 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
205 LogError("Error on platform : " << _rethrown_exception.GetMessage());
206 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
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 ContactManagerController *priv =
222 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
224 LogError("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 LogError("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 LogError("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 LogError("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 LogError("Conversion error.");
281 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
287 JSValueRef JSContactManager::getUnifiedAddressBook(JSContextRef context,
289 JSObjectRef thisObject,
290 size_t argumentCount,
291 const JSValueRef arguments[],
292 JSValueRef* exception)
295 ContactManagerController *priv =
296 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
298 LogError("private object is null");
299 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
302 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_UNIFIED_ADDRESS_BOOK);
303 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
305 ContactManagerPtr contactManager;
306 JSContextRef gContext;
308 contactManager = priv->getObject();
309 gContext = priv->getContext();
311 LogError("contact manager or context is null");
312 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
315 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
317 EventContactManagerGetUnifiedAddressBookPtr dplEvent(new EventContactManagerGetUnifiedAddressBook());
319 dplEvent->setForSynchronousCall();
322 contactManager->getUnifiedAddressBook(dplEvent);
324 LogError("Error on platform : " << _rethrown_exception.GetMessage());
325 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
328 if (!dplEvent->getResult() || !dplEvent->getUnifiedAddressBookIsSet())
330 std::stringstream oss;
331 switch (dplEvent->getExceptionCode())
333 case ExceptionCodes::NotFoundException:
334 case ExceptionCodes::InvalidArgumentException:
335 LogError("Not Found error");
336 oss << "No default address book";
337 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
339 case ExceptionCodes::PlatformException:
340 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
343 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
348 AddressBookPtr unifiedAddressBook = dplEvent->getUnifiedAddressBook();
350 JSValueRef resultValue;
352 resultValue = converter->toJSValueRef(unifiedAddressBook);
354 LogError("Conversion error.");
355 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
361 JSValueRef JSContactManager::getAddressBook(JSContextRef context,
363 JSObjectRef thisObject,
364 size_t argumentCount,
365 const JSValueRef arguments[],
366 JSValueRef* exception)
369 ContactManagerController *priv =
370 static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
372 LogError("private object is null");
373 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
376 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET_ADDRESS_BOOK);
377 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
379 ContactManagerPtr contactManager;
380 JSContextRef gContext;
382 contactManager = priv->getObject();
383 gContext = priv->getContext();
385 LogError("contact manager or context is null");
386 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
389 if (argumentCount < 1) {
390 /* 1st Argument must be string. */
391 LogError("1st argument must not be undefined.");
392 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "No AddressBook name 'undefined'");
395 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
397 std::string addressBookId;
399 addressBookId = converter->toString(arguments[0]);
402 LogError("Error on conversion " << _rethrown_exception.GetMessage());
403 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong argument");
406 EventContactManagerGetAddressBookPtr dplEvent(new EventContactManagerGetAddressBook());
408 dplEvent->setAddressBookId(addressBookId);
409 dplEvent->setForSynchronousCall();
412 contactManager->getAddressBook(dplEvent);
414 LogError("Error on platform : " << _rethrown_exception.GetMessage());
415 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
418 if (!dplEvent->getResult() || !dplEvent->getAddressBookIsSet())
420 std::stringstream oss;
421 switch (dplEvent->getExceptionCode())
423 case ExceptionCodes::NotFoundException:
424 case ExceptionCodes::InvalidArgumentException:
425 LogError("Not Found error : " << addressBookId);
426 oss << "No Contact id '" << addressBookId << "'";
427 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
429 case ExceptionCodes::PlatformException:
430 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
433 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
438 AddressBookPtr addressBook = dplEvent->getAddressBook();
440 JSValueRef resultValue;
442 resultValue = converter->toJSValueRef(addressBook);
444 LogError("Conversion error.");
445 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
451 JSValueRef JSContactManager::get(JSContextRef context,
453 JSObjectRef thisObject,
454 size_t argumentCount,
455 const JSValueRef arguments[],
456 JSValueRef* exception)
459 ContactManagerPtr addressBook;
460 ContactManagerController *controller;
463 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
465 ThrowMsg(InvalidArgumentException, "No private object.");
467 addressBook = controller->getObject();
469 LogError("No private object");
470 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
473 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_GET);
474 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
476 if (argumentCount < 1) {
477 /* 1st Argument must be string. */
478 LogError("1st argument must not be undefined.");
479 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "No Contact id 'undefined'");
484 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
486 id = converter->toString(arguments[0]);
488 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
489 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
492 EventContactManagerGetPtr dplEvent(new EventContactManagerGet());
495 dplEvent->setForSynchronousCall();
498 addressBook->get(dplEvent);
500 LogError("Error on platform : " << _rethrown_exception.GetMessage());
501 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
504 if (!dplEvent->getResult() || !dplEvent->getPersonIsSet())
506 std::stringstream oss;
507 switch (dplEvent->getExceptionCode())
509 case ExceptionCodes::NotFoundException:
510 case ExceptionCodes::InvalidArgumentException:
511 LogError("Not Found error : " << id);
512 oss << "No Person id '" << id << "'";
513 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
515 case ExceptionCodes::PlatformException:
516 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
519 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
524 PersonPtr person = dplEvent->getPerson();
528 result = converter->toJSValueRef(person);
530 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
531 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
537 JSValueRef JSContactManager::update(JSContextRef context,
539 JSObjectRef thisObject,
540 size_t argumentCount,
541 const JSValueRef arguments[],
542 JSValueRef* exception)
545 ContactManagerPtr addressBook;
546 ContactManagerController *controller;
549 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
551 ThrowMsg(InvalidArgumentException, "No private object.");
553 addressBook = controller->getObject();
555 LogError("No private object");
556 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
559 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE);
560 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
563 if (argumentCount < 1)
564 ThrowMsg(InvalidArgumentException, "1st argument is an 'undefined'.");
566 if (!JSPerson::isObjectOfClass(context, arguments[0]))
567 ThrowMsg(InvalidArgumentException, "1st argument is not a 'Contact object'.");
569 } Catch(Exception ) {
570 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
571 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Contact object'");
574 PersonPtr person(NULL);
576 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
578 person = converter->toPerson(arguments[0]);
580 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
581 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument is not a 'Contact object'");
584 EventContactManagerUpdatePtr dplEvent(new EventContactManagerUpdate());
586 dplEvent->setPerson(person);
587 dplEvent->setForSynchronousCall();
590 addressBook->update(dplEvent);
592 LogError("Error on platform : " << _rethrown_exception.GetMessage());
593 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
596 if (!dplEvent->getResult())
598 std::stringstream oss;
599 switch (dplEvent->getExceptionCode())
601 case ExceptionCodes::NotFoundException:
602 case ExceptionCodes::InvalidArgumentException:
603 oss << "No Contact id '" << person->getId() << "'";
604 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, oss.str());
606 case ExceptionCodes::PlatformException:
607 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
610 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
615 return JSValueMakeUndefined(context);
618 JSValueRef JSContactManager::updateBatch(JSContextRef context,
620 JSObjectRef thisObject,
621 size_t argumentCount,
622 const JSValueRef arguments[],
623 JSValueRef* exception)
626 ContactManagerPtr addressBook;
627 JSContextRef gContext;
628 ContactManagerController *controller;
630 bool js2ndParamIsFunction = false;
631 bool js3rdParamIsFunction = false;
634 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
636 ThrowMsg(InvalidArgumentException, "No private object.");
638 addressBook = controller->getObject();
639 gContext = controller->getContext();
641 LogError("No private object");
642 return JSTizenExceptionFactory::postException(context, exception,
643 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
646 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_UPDATE_BATCH);
647 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
649 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
651 // check 1st argument
652 if (argumentCount < 1)
653 ThrowMsg(InvalidArgumentException, "1st argument must be an array of 'Contact object'");
655 if (!JSValueIsObject(context, arguments[0]))
656 ThrowMsg(InvalidArgumentException, "1st argument must be an array of 'Contact object'");
658 // check 2nd argument
659 if(argumentCount >= 2)
661 if(validator->isCallback(arguments[1]))
662 js2ndParamIsFunction = true;
664 if (!js2ndParamIsFunction &&
665 !JSValueIsNull(context, arguments[1]) &&
666 !JSValueIsUndefined(context, arguments[1]))
667 ThrowMsg(InvalidArgumentException, "2nd argument must be a 'function' or a 'null'");
670 // check 3rd argument
671 if(argumentCount >= 3)
673 if(validator->isCallback(arguments[2]))
674 js3rdParamIsFunction = true;
676 if (!js3rdParamIsFunction &&
677 !JSValueIsNull(context, arguments[2]) &&
678 !JSValueIsUndefined(context, arguments[2]))
679 ThrowMsg(InvalidArgumentException, "3rd argument must be a 'function' or a 'null'");
682 } Catch(Exception ) {
683 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
684 return JSTizenExceptionFactory::postException(context, exception,
685 JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
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 LogError("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 LogError("Error on platform : " << _rethrown_exception.GetMessage());
721 return JSTizenExceptionFactory::postException(context, exception,
722 JSTizenException::UNKNOWN_ERROR, "Internal error");
725 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
726 return JSValueMakeUndefined(context);
729 JSValueRef JSContactManager::remove(JSContextRef context,
731 JSObjectRef thisObject,
732 size_t argumentCount,
733 const JSValueRef arguments[],
734 JSValueRef* exception)
737 ContactManagerPtr addressBook;
738 ContactManagerController *controller;
741 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
743 ThrowMsg(InvalidArgumentException, "No private object.");
745 addressBook = controller->getObject();
747 LogError("No private object");
748 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
751 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE);
752 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
754 if (argumentCount < 1) {
755 /* 1st Argument must be string. */
756 LogError("1st argument must not be undefined.");
757 return JSTizenExceptionFactory::postException(context, exception,
758 JSTizenException::NOT_FOUND_ERROR, "No Contact id 'undefined'");
761 std::string personId;
763 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
765 personId = converter->toString(arguments[0]);
768 LogError("Error on conversion " << _rethrown_exception.GetMessage());
769 return JSTizenExceptionFactory::postException(context, exception,
770 JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments.");
773 EventContactManagerRemovePtr dplEvent(new EventContactManagerRemove());
775 dplEvent->setPersonId(personId);
776 dplEvent->setForSynchronousCall();
779 addressBook->remove(dplEvent);
781 LogError("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 LogError("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 return JSValueMakeUndefined(context);
812 JSValueRef JSContactManager::removeBatch(JSContextRef context,
814 JSObjectRef thisObject,
815 size_t argumentCount,
816 const JSValueRef arguments[],
817 JSValueRef* exception)
820 ContactManagerPtr addressBook;
821 JSContextRef gContext;
822 ContactManagerController *controller;
824 bool js2ndParamIsFunction = false;
825 bool js3rdParamIsFunction = false;
828 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
830 ThrowMsg(InvalidArgumentException, "No private object.");
832 addressBook = controller->getObject();
833 gContext = controller->getContext();
835 LogError("No private object");
836 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
839 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_BATCH);
840 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
842 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
844 // check 1st argument
845 if (argumentCount < 1)
846 ThrowMsg(InvalidArgumentException, "1st argument must be an array of 'Contact id'");
848 if (!JSValueIsObject(context, arguments[0]))
849 ThrowMsg(InvalidArgumentException, "1st argument must be an array of 'Contact id'");
851 // check 2nd argument
852 if(argumentCount >= 2)
854 if(validator->isCallback(arguments[1]))
855 js2ndParamIsFunction = true;
857 if (!js2ndParamIsFunction &&
858 !JSValueIsNull(context, arguments[1]) &&
859 !JSValueIsUndefined(context, arguments[1]))
860 ThrowMsg(InvalidArgumentException, "2nd argument must be a 'function' or a 'null'");
863 // check 3rd argument
864 if(argumentCount >= 3)
866 if(validator->isCallback(arguments[2]))
867 js3rdParamIsFunction = true;
869 if (!js3rdParamIsFunction &&
870 !JSValueIsNull(context, arguments[2]) &&
871 !JSValueIsUndefined(context, arguments[2]))
872 ThrowMsg(InvalidArgumentException, "3rd argument must be a 'function' or a 'null'");
875 } Catch(Exception ) {
876 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
877 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
880 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
882 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
884 if(js2ndParamIsFunction)
885 callbackManager->setOnSuccess(arguments[1]);
887 if(js3rdParamIsFunction)
888 callbackManager->setOnError(arguments[2]);
890 callbackManager->setObject(thisObject);
892 EventContactManagerRemoveBatchPtr dplEvent(new EventContactManagerRemoveBatch());
894 dplEvent->setPersonIds(converter->toStringArray(arguments[0]));
895 } Catch(ConversionException) {
896 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
897 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an array of 'Contact id'");
900 // set event handler's data
901 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
902 dplEvent->setForAsynchronousCall(controller);
904 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
905 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
908 addressBook->removeBatch(dplEvent);
909 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
911 LogError("Error on platform : " << _rethrown_exception.GetMessage());
912 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
915 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
916 return JSValueMakeUndefined(context);
919 JSValueRef JSContactManager::find(JSContextRef context,
921 JSObjectRef thisObject,
922 size_t argumentCount,
923 const JSValueRef arguments[],
924 JSValueRef* exception)
927 ContactManagerPtr addressBook;
928 JSContextRef gContext;
929 ContactManagerController *controller;
931 bool js2ndParamIsFunction = false;
932 bool js3rdParamIsObject = false;
933 bool js4thParamIsObject = false;
936 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
938 ThrowMsg(InvalidArgumentException, "No private object.");
940 addressBook = controller->getObject();
941 gContext = controller->getContext();
943 LogError("No private object");
944 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
947 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_FIND);
948 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
950 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
952 if (argumentCount < 1)
953 ThrowMsg(InvalidArgumentException, "1st argument must be a 'function'");
955 if (!validator->isCallback(arguments[0]))
956 ThrowMsg(InvalidArgumentException, "1st argument must be a 'function'");
958 if (argumentCount >= 2)
960 if(validator->isCallback(arguments[1]))
961 js2ndParamIsFunction = true;
963 if (!js2ndParamIsFunction &&
964 !JSValueIsNull(context, arguments[1]) &&
965 !JSValueIsUndefined(context, arguments[1]))
966 ThrowMsg(InvalidArgumentException, "1st argument must be a 'function' or a 'null'");
969 if (argumentCount >= 3)
971 if(JSValueIsObject(context, arguments[2]))
972 js3rdParamIsObject = true;
974 if (!js3rdParamIsObject &&
975 !JSValueIsNull(context, arguments[2]) &&
976 !JSValueIsUndefined(context, arguments[2]))
977 ThrowMsg(InvalidArgumentException, "3rd argument must be an 'Filter object' or 'null'");
980 if (argumentCount >= 4)
982 if(JSValueIsObject(context, arguments[3]))
983 js4thParamIsObject = true;
985 if (!js4thParamIsObject &&
986 !JSValueIsNull(context, arguments[3]) &&
987 !JSValueIsUndefined(context, arguments[3]))
988 ThrowMsg(InvalidArgumentException, "4th argument must be an 'SortMode object' or 'null'");
990 } Catch(Exception ) {
991 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
992 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
995 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
997 callbackManager->setOnSuccess(arguments[0]);
999 if (js2ndParamIsFunction)
1000 callbackManager->setOnError(arguments[1]);
1002 callbackManager->setObject(thisObject);
1004 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1005 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1007 EventContactManagerFindPtr dplEvent(new EventContactManagerFind());
1009 if (js3rdParamIsObject)
1010 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
1011 } Catch(Exception) {
1012 LogError("Error on 3rd parameter conversion : " << _rethrown_exception.GetMessage());
1013 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "3rd argument must be an 'Filter object' or 'null'");
1017 if (js4thParamIsObject)
1018 dplEvent->setSortMode(filterConverter->toSortMode(arguments[3]));
1019 } Catch(Exception) {
1020 LogError("Error on 4th parameter conversion : " << _rethrown_exception.GetMessage());
1021 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "4th argument must be an 'SortMode object' or 'null'");
1024 // set event handler's data
1025 dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
1026 dplEvent->setForAsynchronousCall(controller);
1028 // DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
1029 // IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
1032 addressBook->find(dplEvent);
1033 ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
1034 } Catch(Exception) {
1035 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1036 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1039 // return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
1040 return JSValueMakeUndefined(context);
1043 JSValueRef JSContactManager::addChangeListener(JSContextRef context,
1045 JSObjectRef thisObject,
1046 size_t argumentCount,
1047 const JSValueRef arguments[],
1048 JSValueRef* exception)
1050 LogDebug("entered");
1051 ContactManagerPtr addressBook;
1052 JSContextRef gContext;
1053 ContactManagerController *controller;
1055 bool js2ndParamIsFunction = false;
1058 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1060 ThrowMsg(InvalidArgumentException, "No private object.");
1062 addressBook = controller->getObject();
1063 gContext = controller->getContext();
1064 } Catch(Exception) {
1065 LogError("No private object");
1066 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1069 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_ADD_CHANGE_LISTENER);
1070 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1072 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
1074 if (argumentCount < 1)
1075 ThrowMsg(InvalidArgumentException, "1st argument must be a 'ContactManagerChangeCallback object'");
1077 if (!JSValueIsObject(context, arguments[0]))
1078 ThrowMsg(InvalidArgumentException, "1st argument must be a 'ContactManagerChangeCallback object'");
1080 if (argumentCount >= 2)
1082 if(validator->isCallback(arguments[1]))
1083 js2ndParamIsFunction = true;
1085 if (!js2ndParamIsFunction &&
1086 !JSValueIsNull(context, arguments[1]) &&
1087 !JSValueIsUndefined(context, arguments[1]))
1088 ThrowMsg(InvalidArgumentException, "2nd argument must be a 'function' or a 'null'");
1091 } Catch(Exception ) {
1092 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
1093 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1096 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1098 JSValueRef onpersonsadded;
1099 JSValueRef onpersonsupdated;
1100 JSValueRef onpersonsdeleted;
1102 JSObjectRef callbackObject = converter->toJSObjectRef(arguments[0]);
1104 onpersonsadded = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsadded");
1105 if (validator->isNullOrUndefined(onpersonsadded))
1106 onpersonsadded = NULL;
1107 else if (!validator->isCallback(onpersonsadded)) {
1108 ThrowMsg(ConversionException, "2nd argument's onpersonsadded attribute is not a 'function'");
1111 onpersonsupdated = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsupdated");
1112 if (validator->isNullOrUndefined(onpersonsupdated))
1113 onpersonsupdated = NULL;
1114 else if (!validator->isCallback(onpersonsupdated)) {
1115 ThrowMsg(ConversionException, "2nd argument's onpersonsupdated attribute is not a 'function'");
1118 onpersonsdeleted = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpersonsremoved");
1119 if (validator->isNullOrUndefined(onpersonsdeleted))
1120 onpersonsdeleted = NULL;
1121 else if (!validator->isCallback(onpersonsdeleted)) {
1122 ThrowMsg(ConversionException, "2nd argument's onpersonsremoved attribute is not a 'function'");
1125 if (onpersonsadded == NULL && onpersonsupdated == NULL && onpersonsdeleted == NULL)
1126 ThrowMsg(ConversionException, "2nd argument must have at least one function");
1128 } Catch(ConversionException) {
1129 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
1130 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1133 JSContactManagerChangeCallbackManagerPtr callbackManager = JSContactManagerChangeCallbackManager::createObject(gContext);
1135 callbackManager->setOnPersonsAdded(onpersonsadded);
1136 callbackManager->setOnPersonsUpdated(onpersonsupdated);
1137 callbackManager->setOnPersonsDeleted(onpersonsdeleted);
1138 if(js2ndParamIsFunction)
1139 callbackManager->setOnError(arguments[1]);
1141 EventContactManagerChangeListenerEmitterPtr emitter(new EventContactManagerChangeListenerEmitter());
1143 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
1144 emitter->setListener(controller);
1146 EventContactManagerAddChangeListenerPtr dplEvent(new EventContactManagerAddChangeListener());
1148 dplEvent->setEmitter(emitter);
1149 dplEvent->setForSynchronousCall();
1152 addressBook->addChangeListener(dplEvent);
1153 } Catch(Exception) {
1154 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1155 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1158 if (!dplEvent->getResult() || !dplEvent->getIdIsSet())
1160 switch (dplEvent->getExceptionCode())
1162 case ExceptionCodes::InvalidArgumentException:
1163 case ExceptionCodes::PlatformException:
1164 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1167 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1172 long watchId = dplEvent->getId();
1174 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1175 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1176 ContactListenerManagerSingleton::Instance().registerListener(listenerItem, gContext);
1180 result = converter->toJSValueRefLong(watchId);
1181 } Catch(Exception) {
1182 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
1183 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1189 JSValueRef JSContactManager::removeChangeListener(JSContextRef context,
1191 JSObjectRef thisObject,
1192 size_t argumentCount,
1193 const JSValueRef arguments[],
1194 JSValueRef* exception)
1196 LogDebug("entered");
1197 ContactManagerPtr addressBook;
1198 JSContextRef gContext;
1199 ContactManagerController *controller;
1202 controller = static_cast<ContactManagerController*>(JSObjectGetPrivate(thisObject));
1204 ThrowMsg(InvalidArgumentException, "No private object.");
1206 addressBook = controller->getObject();
1207 gContext = controller->getContext();
1208 } Catch(Exception) {
1209 LogError("No private object");
1210 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1213 AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_CONTACT_MANAGER_REMOVE_CHANGE_LISTENER);
1214 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1216 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
1217 if (argumentCount < 1)
1219 LogError("1st argument must not be undefined.");
1220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "No watch id 'undefined'");
1223 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
1227 watchId = static_cast<long>(converter->toLong(arguments[0]));
1230 ThrowMsg(PlatformException, "watchId is wrong (" << watchId << ")");
1231 } Catch(Exception) {
1232 LogError("Exception: " << _rethrown_exception.GetMessage());
1233 JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1236 EventContactManagerRemoveChangeListenerPtr dplEvent(new EventContactManagerRemoveChangeListener());
1238 dplEvent->setId(watchId);
1239 dplEvent->setForSynchronousCall();
1242 addressBook->removeChangeListener(dplEvent);
1243 } Catch(Exception) {
1244 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1245 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1248 PersonsChangeListenerCancellerPtr canceller = PersonsChangeListenerCancellerPtr(new PersonsChangeListenerCanceller(gContext, thisObject, watchId));
1249 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1250 ContactListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1252 if (!dplEvent->getResult())
1254 switch (dplEvent->getExceptionCode())
1256 case ExceptionCodes::InvalidArgumentException:
1257 case ExceptionCodes::NotFoundException:
1258 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "Watch id not found");
1261 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1266 return JSValueMakeUndefined(context);