summaryrefslogtreecommitdiff
path: root/mobile_src/Contact/JSAddressBook.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mobile_src/Contact/JSAddressBook.cpp')
-rw-r--r--mobile_src/Contact/JSAddressBook.cpp1657
1 files changed, 1657 insertions, 0 deletions
diff --git a/mobile_src/Contact/JSAddressBook.cpp b/mobile_src/Contact/JSAddressBook.cpp
new file mode 100644
index 0000000..eee5bd2
--- /dev/null
+++ b/mobile_src/Contact/JSAddressBook.cpp
@@ -0,0 +1,1657 @@
+//
+// Tizen Web Device API
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file JSAddressBook.cpp
+ * @version 0.1
+ * @brief Implementation of the JSAddressBook class
+ */
+
+#include <CommonsJavaScript/Validator.h>
+#include <CommonsJavaScript/Converter.h>
+#include <CommonsJavaScript/JSCallbackManager.h>
+#include <CommonsJavaScript/JSUtils.h>
+#include <CommonsJavaScript/JSPendingOperation.h>
+#include <SecurityExceptions.h>
+#include <FilterConverter.h>
+#include "ContactFactory.h"
+#include "EventAddressBookAddBatch.h"
+#include "EventAddressBookUpdateBatch.h"
+#include "EventAddressBookRemoveBatch.h"
+#include "EventAddressBookFind.h"
+#include "EventAddressBookChangeListener.h"
+#include "EventAddressBookRemoveBatch.h"
+#include "plugin_config_impl.h"
+#include "JSAddressBook.h"
+#include "JSContact.h"
+#include "JSContactGroup.h"
+#include "ContactConverter.h"
+#include "AddressBookController.h"
+#include "JSAddressBookChangeCallbackManager.h"
+#include "ContactAsyncCallbackManager.h"
+#include "ContactListenerManager.h"
+#include "ContactFilterConverter.h"
+#include <ArgumentValidator.h>
+#include <JSWebAPIErrorFactory.h>
+#include <JSUtil.h>
+#include <TimeTracer.h>
+#include <Security.h>
+#include <Logger.h>
+#include "ContactUtility.h"
+
+using namespace std;
+
+#define TIZEN_ADDRESS_BOOK_ID "id"
+#define TIZEN_ADDRESS_BOOK_NAME "name"
+#define TIZEN_ADDRESS_BOOK_READ_ONLY "readOnly"
+#define TIZEN_ADDRESS_BOOK_ACCOUNT_ID "accountId"
+
+#define TIZEN_ADDRESS_BOOK "AddressBook"
+
+namespace DeviceAPI {
+namespace Contact {
+
+using namespace DeviceAPI::Common;
+using namespace DeviceAPI::Tizen;
+using namespace WrtDeviceApis::Commons;
+using namespace WrtDeviceApis::CommonsJavaScript;
+
+JSClassRef JSAddressBook::m_jsClassRef = NULL;
+
+JSClassDefinition JSAddressBook::m_classInfo = {
+ 0,
+ kJSClassAttributeNone,
+ TIZEN_ADDRESS_BOOK,
+ 0,
+ m_property,
+ m_function,
+ Initialize,
+ Finalize,
+ NULL, //HasProperty,
+ NULL, //GetProperty,
+ NULL, //SetProperty,
+ NULL, //DeleteProperty,
+ NULL, //GetPropertyNames,
+ NULL, //CallAsFunction,
+ NULL, //CallAsConstructor,
+ NULL, //HasInstance,
+ NULL, //ConvertToType,
+};
+
+JSStaticValue JSAddressBook::m_property[] = {
+ { TIZEN_ADDRESS_BOOK_ID, getId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
+ { TIZEN_ADDRESS_BOOK_NAME, getName, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
+ { TIZEN_ADDRESS_BOOK_READ_ONLY, getReadOnly, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
+// { TIZEN_ADDRESS_BOOK_ACCOUNT_ID, getAccountId, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
+ { 0, 0, 0, 0 }
+};
+
+JSStaticFunction JSAddressBook::m_function[] = {
+ { "get", get, kJSPropertyAttributeNone },
+ { "add", add, kJSPropertyAttributeNone },
+ { "addBatch", addBatch, kJSPropertyAttributeNone },
+ { "update", update, kJSPropertyAttributeNone },
+ { "updateBatch", updateBatch, kJSPropertyAttributeNone },
+ { "remove", remove, kJSPropertyAttributeNone },
+ { "removeBatch", removeBatch, kJSPropertyAttributeNone },
+ { "find", find, kJSPropertyAttributeNone },
+ { "addChangeListener", addChangeListener, kJSPropertyAttributeNone },
+ { "removeChangeListener", removeChangeListener, kJSPropertyAttributeNone },
+ { "getGroup", getGroup, kJSPropertyAttributeNone },
+ { "addGroup", addGroup, kJSPropertyAttributeNone },
+ { "updateGroup", updateGroup, kJSPropertyAttributeNone },
+ { "removeGroup", removeGroup, kJSPropertyAttributeNone },
+ { "getGroups", getGroups, kJSPropertyAttributeNone },
+ { 0, 0, 0 }
+};
+
+const JSClassDefinition* JSAddressBook::getClassInfo()
+{
+ return &m_classInfo;
+}
+
+const JSClassRef JSAddressBook::getClassRef()
+{
+ LoggerI("entered");
+ if (!m_jsClassRef) {
+ m_jsClassRef = JSClassCreate(&m_classInfo);
+ }
+ return m_jsClassRef;
+}
+
+void JSAddressBook::Initialize(JSContextRef context,
+ JSObjectRef object)
+{
+// AddressBookController *priv =
+// static_cast<AddressBookController*>(JSObjectGetPrivate(object));
+}
+
+void JSAddressBook::Finalize(JSObjectRef object)
+{
+ AddressBookController *priv =
+ static_cast<AddressBookController*>(JSObjectGetPrivate(object));
+
+ delete priv;
+}
+
+bool JSAddressBook::isObjectOfClass(JSContextRef context, JSValueRef value)
+{
+ return JSValueIsObjectOfClass(context, value, getClassRef());
+}
+
+AddressBookPtr JSAddressBook::getAddressBook(JSContextRef context, JSValueRef value)
+{
+ if (!isObjectOfClass(context, value)) {
+ Throw(InvalidArgumentException);
+ }
+ JSObjectRef object = JSValueToObject(context, value, NULL);
+ if (!object) {
+ Throw(InvalidArgumentException);
+ }
+ AddressBookController *priv = static_cast<AddressBookController*>(JSObjectGetPrivate(object));
+ if (!priv) {
+ Throw(NullPointerException);
+ }
+ return priv->getObject();
+}
+
+AddressBookPtr JSAddressBook::getPrivData(JSObjectRef object)
+{
+ AddressBookController *priv =
+ static_cast<AddressBookController*>(JSObjectGetPrivate(object));
+ if (priv) {
+ return priv->getObject();
+ }
+ Throw(NullPointerException);
+}
+
+JSValueRef JSAddressBook::getId(JSContextRef context,
+ JSObjectRef object,
+ JSStringRef propertyName,
+ JSValueRef* exception)
+{
+ Try
+ {
+ ContactConverterFactory::ConverterType converter =
+ ContactConverterFactory::getConverter(context);
+ AddressBookPtr addressBook = getPrivData(object);
+ string id = addressBook->getId();
+ if(id != "")
+ return converter->toJSValueRef(id);
+ else
+ return JSValueMakeNull(context);
+ }
+ Catch(WrtDeviceApis::Commons::Exception)
+ {
+ LoggerW("trying to get incorrect value");
+ }
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::getName(JSContextRef context,
+ JSObjectRef object,
+ JSStringRef propertyName,
+ JSValueRef* exception)
+{
+ Try
+ {
+ ContactConverterFactory::ConverterType converter =
+ ContactConverterFactory::getConverter(context);
+ AddressBookPtr addressBook = getPrivData(object);
+ return converter->toJSValueRef(addressBook->getName());
+ }
+ Catch(WrtDeviceApis::Commons::Exception)
+ {
+ LoggerW("trying to get incorrect value");
+ }
+
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::getReadOnly(JSContextRef context,
+ JSObjectRef object,
+ JSStringRef propertyName,
+ JSValueRef* exception)
+{
+ Try
+ {
+ ContactConverterFactory::ConverterType converter =
+ ContactConverterFactory::getConverter(context);
+ AddressBookPtr addressBook = getPrivData(object);
+ return converter->toJSValueRef(addressBook->getReadOnly());
+ }
+ Catch(WrtDeviceApis::Commons::Exception)
+ {
+ LoggerW("trying to get incorrect value");
+ }
+
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::getAccountId(JSContextRef context,
+ JSObjectRef object,
+ JSStringRef propertyName,
+ JSValueRef* exception)
+{
+ Try
+ {
+ ContactConverterFactory::ConverterType converter =
+ ContactConverterFactory::getConverter(context);
+ AddressBookPtr addressBook = getPrivData(object);
+ string accountId = addressBook->getAccountId();
+ if(accountId != "")
+ return converter->toJSValueRef(accountId);
+ else
+ return JSValueMakeNull(context);
+ }
+ Catch(WrtDeviceApis::Commons::Exception)
+ {
+ LoggerW("trying to get incorrect value");
+ }
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::get(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_GET);
+
+ string id;
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ id = validator.toString(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ if(!ContactUtility::checkStrIsUInt(id))
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "");
+
+ EventAddressBookGetPtr dplEvent(new EventAddressBookGet());
+
+ dplEvent->setId(id);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->get(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult() || !dplEvent->getContactIsSet())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ LoggerE("Not Found error : " << id);
+ oss << "No Contact id '" << id << "'";
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+
+ ContactPtr contact = dplEvent->getContact();
+ JSValueRef result;
+ Try {
+ result = converter->toJSValueRef(contact);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return result;
+}
+
+JSValueRef JSAddressBook::add(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_ADD);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ validator.toObject(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactPtr contact(NULL);
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+ Try {
+ if (!JSContact::isObjectOfClass(context, arguments[0]))
+ ThrowMsg(InvalidArgumentException, "1st argument is not a 'Contact object'.");
+ contact = converter->toContact(arguments[0]);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'Contact object'");
+ }
+
+ EventAddressBookAddPtr dplEvent(new EventAddressBookAdd());
+
+ dplEvent->setContact(contact);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->add(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ case ExceptionCodes::InvalidArgumentException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "There is Invalid input value");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::addBatch(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ bool js2ndParamIsFunction = false;
+ bool js3rdParamIsFunction = false;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_ADD_BATCH);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+
+ validator.toObject(0, false);
+ JSObjectRef successObj = validator.toFunction(1, true);
+ if(successObj)
+ js2ndParamIsFunction = true;
+
+ JSObjectRef errorObj = validator.toFunction(2, true);
+ if(errorObj)
+ js3rdParamIsFunction = true;
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
+
+ JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
+
+ if(js2ndParamIsFunction)
+ callbackManager->setOnSuccess(arguments[1]);
+
+ if(js3rdParamIsFunction)
+ callbackManager->setOnError(arguments[2]);
+
+ callbackManager->setObject(thisObject);
+
+ EventAddressBookAddBatchPtr dplEvent(new EventAddressBookAddBatch());
+ Try {
+ dplEvent->setContacts(converter->toContactArray(arguments[0]));
+ } Catch(ConversionException) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be an array of 'Contact object'");
+ }
+
+ // set event handler's data
+ dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
+ dplEvent->setForAsynchronousCall(controller);
+
+// DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
+// IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
+
+ Try {
+ addressBook->addBatch(dplEvent);
+ ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+// return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::update(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_UPDATE);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ validator.toObject(0, false);
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactPtr contact(NULL);
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+ Try {
+ if (!JSContact::isObjectOfClass(context, arguments[0]))
+ ThrowMsg(InvalidArgumentException, "1st argument is not a 'Contact object'.");
+ contact = converter->toContact(arguments[0]);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument is not a 'Contact object'");
+ }
+
+ EventAddressBookUpdatePtr dplEvent(new EventAddressBookUpdate());
+
+ dplEvent->setContact(contact);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->update(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ oss << "No Contact id '" << contact->getId() << "'";
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::updateBatch(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ bool js2ndParamIsFunction = false;
+ bool js3rdParamIsFunction = false;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_UPDATE_BATCH);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+
+ validator.toObject(0, false);
+
+ JSObjectRef successObj = validator.toFunction(1, true);
+ if(successObj)
+ js2ndParamIsFunction = true;
+
+ JSObjectRef errorObj = validator.toFunction(2, true);
+ if(errorObj)
+ js3rdParamIsFunction = true;
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
+
+ JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
+
+ if(js2ndParamIsFunction)
+ callbackManager->setOnSuccess(arguments[1]);
+
+ if(js3rdParamIsFunction)
+ callbackManager->setOnError(arguments[2]);
+
+ callbackManager->setObject(thisObject);
+
+ EventAddressBookUpdateBatchPtr dplEvent(new EventAddressBookUpdateBatch());
+ Try {
+ dplEvent->setContacts(converter->toContactArray(arguments[0]));
+ } Catch(ConversionException) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "First argument must be an array of 'Contact object'");
+ }
+
+ // set event handler's data
+ dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
+ dplEvent->setForAsynchronousCall(controller);
+
+// DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
+// IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
+
+ Try {
+ addressBook->updateBatch(dplEvent);
+ ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+// return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::remove(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_REMOVE);
+
+ ArgumentValidator validator(context, argumentCount, arguments);
+ string contactId;
+ try {
+ contactId = validator.toString(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+ if(!ContactUtility::checkStrIsUInt(contactId))
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "");
+
+ EventAddressBookRemovePtr dplEvent(new EventAddressBookRemove());
+
+ dplEvent->setContactId(contactId);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->remove(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ LoggerE("Not Found error : " << contactId);
+ oss << "No Contact id '" << contactId << "'";
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::removeBatch(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ bool js2ndParamIsFunction = false;
+ bool js3rdParamIsFunction = false;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_REMOVE_BATCH);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+
+ validator.toObject(0, false);
+ JSObjectRef successObj = validator.toFunction(1, true);
+ if(successObj)
+ js2ndParamIsFunction = true;
+
+ JSObjectRef errorObj = validator.toFunction(2, true);
+ if(errorObj)
+ js3rdParamIsFunction = true;
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
+
+ JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
+
+ if(js2ndParamIsFunction)
+ callbackManager->setOnSuccess(arguments[1]);
+
+ if(js3rdParamIsFunction)
+ callbackManager->setOnError(arguments[2]);
+
+ callbackManager->setObject(thisObject);
+
+ EventAddressBookRemoveBatchPtr dplEvent(new EventAddressBookRemoveBatch());
+ Try {
+ dplEvent->setContactIds(converter->toStringArray(arguments[0]));
+ } Catch(ConversionException) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "3rd argument must be an array of 'Contact id'");
+ }
+
+ // set event handler's data
+ dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
+ dplEvent->setForAsynchronousCall(controller);
+
+// DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
+// IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
+
+ Try {
+ addressBook->removeBatch(dplEvent);
+ ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+// return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::find(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ bool js2ndParamIsFunction = false;
+ bool js3rdParamIsObject = false;
+ bool js4thParamIsObject = false;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_FIND);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+
+ validator.toFunction(0, false);
+ JSObjectRef errorObj = validator.toFunction(1, true);
+ if(errorObj)
+ js2ndParamIsFunction = true;
+
+ JSObjectRef filterObj = validator.toObject(2, true);
+ if(filterObj)
+ js3rdParamIsObject = true;
+
+ JSObjectRef sortObj = validator.toObject(3, true);
+ if(sortObj)
+ js4thParamIsObject = true;
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
+
+ callbackManager->setOnSuccess(arguments[0]);
+
+ if (js2ndParamIsFunction)
+ callbackManager->setOnError(arguments[1]);
+
+ callbackManager->setObject(thisObject);
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
+ ContactFilterConverterFactory::ConverterType filterConverter = ContactFilterConverterFactory::getConverter(context);
+
+ EventAddressBookFindPtr dplEvent(new EventAddressBookFind());
+ Try {
+ if (js3rdParamIsObject){
+ FilterPtr filter = filterConverter->toFilter(arguments[2]);
+ if(filter)
+ dplEvent->setFilter(filter);
+ else
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "3rd argument must be an Correct 'Filter object' or 'null'");
+ }
+ } Catch(Exception) {
+ LoggerE("Error on 3rd parameter conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "3rd argument must be an 'Filter object' or 'null'");
+ }
+
+ Try {
+ if (js4thParamIsObject){
+ SortModePtr sortMode = filterConverter->toSortMode(arguments[3]);
+ dplEvent->setSortMode(sortMode);
+ }
+ } Catch(Exception) {
+ LoggerE("Error on 4th parameter conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "4th argument must be an 'SortMode object' or 'null'");
+ }
+
+ // set event handler's data
+ dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
+ dplEvent->setForAsynchronousCall(controller);
+
+// DPL::SharedPtr<IEventController> eventContr = DPL::StaticPointerCast< IEventController>(dplEvent);
+// IJSPendingOperationPrivateObject *gcPendingOperation = new IJSPendingOperationPrivateObject(eventContr);
+
+ Try {
+ addressBook->find(dplEvent);
+ ContactAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+// return JSObjectMake(gContext, JSPendingOperation::getClassRef(), gcPendingOperation);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::addChangeListener(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ bool js2ndParamIsFunction = false;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_ADD_CHANGE_LISTENER);
+
+ BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
+ try {
+ ArgumentValidator Argvalidator(context, argumentCount, arguments);
+ Argvalidator.toObject(0, false);
+ JSObjectRef errorObj = Argvalidator.toFunction(1, true);
+ if(errorObj)
+ js2ndParamIsFunction = true;
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
+
+ JSValueRef oncontactsadded;
+ JSValueRef oncontactsupdated;
+ JSValueRef oncontactsdeleted;
+ Try {
+ JSObjectRef callbackObject = converter->toJSObjectRef(arguments[0]);
+
+ oncontactsadded = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "oncontactsadded");
+ if (validator->isNullOrUndefined(oncontactsadded))
+ oncontactsadded = NULL;
+ else if (!validator->isCallback(oncontactsadded)) {
+ ThrowMsg(ConversionException, "2nd argument's oncontactsadded attribute is not a 'function'");
+ }
+
+ oncontactsupdated = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "oncontactsupdated");
+ if (validator->isNullOrUndefined(oncontactsupdated))
+ oncontactsupdated = NULL;
+ else if (!validator->isCallback(oncontactsupdated)) {
+ ThrowMsg(ConversionException, "2nd argument's oncontactsupdated attribute is not a 'function'");
+ }
+
+ oncontactsdeleted = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "oncontactsremoved");
+ if (validator->isNullOrUndefined(oncontactsdeleted))
+ oncontactsdeleted = NULL;
+ else if (!validator->isCallback(oncontactsdeleted)) {
+ ThrowMsg(ConversionException, "2nd argument's oncontactsremoved attribute is not a 'function'");
+ }
+
+ if (oncontactsadded == NULL && oncontactsupdated == NULL && oncontactsdeleted == NULL)
+ ThrowMsg(ConversionException, "2nd argument must have at least one function");
+
+ } Catch(ConversionException) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
+ }
+
+ JSAddressBookChangeCallbackManagerPtr callbackManager = JSAddressBookChangeCallbackManager::createObject(gContext);
+
+ callbackManager->setOnContactsAdded(oncontactsadded);
+ callbackManager->setOnContactsUpdated(oncontactsupdated);
+ callbackManager->setOnContactsDeleted(oncontactsdeleted);
+ if(js2ndParamIsFunction)
+ callbackManager->setOnError(arguments[1]);
+
+ EventAddressBookChangeListenerEmitterPtr emitter(new EventAddressBookChangeListenerEmitter());
+
+ emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
+ emitter->setListener(controller);
+
+ EventAddressBookAddChangeListenerPtr dplEvent(new EventAddressBookAddChangeListener());
+
+ dplEvent->setEmitter(emitter);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->addChangeListener(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult() || !dplEvent->getIdIsSet())
+ {
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::InvalidArgumentException:
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ long watchId = dplEvent->getId();
+
+ ContactsChangeListenerCancellerPtr canceller = ContactsChangeListenerCancellerPtr(new ContactsChangeListenerCanceller(gContext, thisObject, watchId));
+ DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
+ ContactListenerManagerSingleton::Instance().registerListener(listenerItem, gContext);
+
+ JSValueRef result;
+ Try {
+ result = JSUtil::toJSValueRef(context, watchId);
+// result = converter->toJSValueRefLong(watchId);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return result;
+}
+
+JSValueRef JSAddressBook::removeChangeListener(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ JSContextRef gContext;
+ AddressBookController *controller;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_REMOVE_CHANGE_LISTENER);
+
+ long watchId = 0;
+ string id;
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ watchId = validator.toLong(0, false);
+ id = validator.toString(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ if(!ContactUtility::checkStrIsUInt(id))
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "");
+
+ EventAddressBookRemoveChangeListenerPtr dplEvent(new EventAddressBookRemoveChangeListener());
+
+ dplEvent->setId(watchId);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->removeChangeListener(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ ContactsChangeListenerCancellerPtr canceller = ContactsChangeListenerCancellerPtr(new ContactsChangeListenerCanceller(gContext, thisObject, watchId));
+ DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
+ ContactListenerManagerSingleton::Instance().unregisterListener(listenerItem);
+
+ if (!dplEvent->getResult())
+ {
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::InvalidArgumentException:
+ case ExceptionCodes::NotFoundException:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, "Watch id not found");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::getGroup(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_GET_GROUP);
+
+ string id;
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ id = validator.toString(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ if(!ContactUtility::checkStrIsUInt(id))
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "");
+
+ EventAddressBookGetGroupPtr dplEvent(new EventAddressBookGetGroup());
+
+ dplEvent->setId(id);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->getGroup(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult() || !dplEvent->getContactGroupIsSet())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ LoggerE("Not Found error : " << id);
+ oss << "No Group id '" << id << "'";
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ ContactGroupPtr group = dplEvent->getContactGroup();
+
+ JSValueRef result;
+ Try {
+ result = converter->toJSValueRef(group);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return result;
+}
+
+JSValueRef JSAddressBook::addGroup(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_ADD_GROUP);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ validator.toObject(0, false);
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactGroupPtr group(NULL);
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+ Try {
+ if (!JSContactGroup::isObjectOfClass(context, arguments[0]))
+ ThrowMsg(InvalidArgumentException, "1st argument is not a 'ContactGroup object'.");
+
+ group = converter->toContactGroup(arguments[0]);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactGroup object'");
+ }
+
+ EventAddressBookAddGroupPtr dplEvent(new EventAddressBookAddGroup());
+
+ dplEvent->setContactGroup(group);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->addGroup(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::updateGroup(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_UPDATE_GROUP);
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ validator.toObject(0, false);
+
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+
+ ContactGroupPtr contact(NULL);
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+ Try {
+ if (!JSContactGroup::isObjectOfClass(context, arguments[0]))
+ ThrowMsg(InvalidArgumentException, "1st argument is not a 'ContactGroup object'.");
+ contact = converter->toContactGroup(arguments[0]);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument is not a 'ContactGroup object'");
+ }
+
+ EventAddressBookUpdateGroupPtr dplEvent(new EventAddressBookUpdateGroup());
+
+ dplEvent->setContactGroup(contact);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->updateGroup(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ oss << "No Group id '" << contact->getId() << "'";
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::removeGroup(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_REMOVE_GROUP);
+
+ string groupId;
+
+ try {
+ ArgumentValidator validator(context, argumentCount, arguments);
+ groupId = validator.toString(0, false);
+ } catch (const TypeMismatchException& err ) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const BasePlatformException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, err);
+ } catch(const ConversionException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ } catch(const NullPointerException& err) {
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
+ }
+ if(!ContactUtility::checkStrIsUInt(groupId))
+ return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "");
+
+ EventAddressBookRemoveGroupPtr dplEvent(new EventAddressBookRemoveGroup());
+
+ dplEvent->setContactGroupId(groupId);
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->removeGroup(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ LoggerE("Not Found error : " << groupId);
+ oss << "No Group id '" << groupId << "'";
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return JSValueMakeUndefined(context);
+}
+
+JSValueRef JSAddressBook::getGroups(JSContextRef context,
+ JSObjectRef object,
+ JSObjectRef thisObject,
+ size_t argumentCount,
+ const JSValueRef arguments[],
+ JSValueRef* exception)
+{
+ LoggerD("entered");
+ TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
+ AddressBookPtr addressBook;
+ AddressBookController *controller;
+ JSContextRef gContext;
+
+ Try {
+ controller = static_cast<AddressBookController*>(JSObjectGetPrivate(thisObject));
+ if (!controller) {
+ ThrowMsg(InvalidArgumentException, "No private object.");
+ }
+ addressBook = controller->getObject();
+ gContext = controller->getContext();
+ } Catch(Exception) {
+ LoggerE("No private object");
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong object");
+ }
+
+ TIZEN_CHECK_ACCESS(context, exception, controller, CONTACT_FUNCTION_API_ADDRESS_BOOK_GET_GROUPS);
+
+ string id;
+
+ ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
+
+ EventAddressBookGetGroupsPtr dplEvent(new EventAddressBookGetGroups());
+
+ dplEvent->setForSynchronousCall();
+
+ Try {
+ addressBook->getGroups(dplEvent);
+ } Catch(Exception) {
+ LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+
+ if (!dplEvent->getResult() || !dplEvent->getContactGroupsIsSet())
+ {
+ std::stringstream oss;
+ switch (dplEvent->getExceptionCode())
+ {
+ case ExceptionCodes::NotFoundException:
+ case ExceptionCodes::InvalidArgumentException:
+ LoggerE("Not Found error : " << id);
+ oss << "No Group id '" << id << "'";
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::NOT_FOUND_ERROR, oss.str());
+ break;
+ case ExceptionCodes::PlatformException:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ default:
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ break;
+ }
+ }
+
+ ContactGroupArrayPtr groups = dplEvent->getContactGroups();
+
+ JSValueRef result;
+ Try {
+ result = converter->toJSValueRef(groups);
+ } Catch(Exception) {
+ LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
+ return JSWebAPIErrorFactory::postException(context, exception,
+ JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
+ }
+ TIME_TRACER_ITEM_END(__FUNCTION__, 0);
+ return result;
+}
+
+} // Contact
+} // DeviceAPI