2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file ContactManagerController.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
24 #include <CommonsJavaScript/JSCallbackManager.h>
25 #include <Tizen/Common/JSTizenExceptionFactory.h>
26 #include <Tizen/Common/JSTizenException.h>
27 #include "ContactConverter.h"
28 #include "ContactManagerController.h"
34 using namespace TizenApis::Commons;
35 using namespace TizenApis::Api::Contact;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
39 ContactManagerController::ContactManagerController(JSContextRef context, IContactManagerPtr contactManager) :
40 EventContactManagerGetAddressBooksAnswerReceiver(ThreadEnum::NULL_THREAD),
41 ContactManagerPrivObject(context, contactManager)
45 ContactManagerController::~ContactManagerController()
49 void ContactManagerAsyncAnswerHandler::getAddressBooks(const EventContactManagerGetAddressBooksPtr &event)
51 JSCallbackManagerPtr callbackManager =
52 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
53 if (!callbackManager) {
54 LogError("no callback manager");
58 JSContextRef gContext = callbackManager->getContext();
60 JSValueRef error = NULL;
61 if (ExceptionCodes::None != event->getExceptionCode())
63 switch (event->getExceptionCode())
65 case ExceptionCodes::PlatformException:
66 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
68 case ExceptionCodes::InvalidArgumentException:
69 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Invalid value error");
71 case ExceptionCodes::ConversionException:
72 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Wrong argument");
75 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
79 callbackManager->callOnError(error);
84 if (!event->getResult())
85 ThrowMsg(PlatformException, "No result.");
87 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
88 IAddressBookArrayPtr addressBooks = event->getAddressBooks();
90 JSValueRef result = converter->toJSValueRef(addressBooks);
92 callbackManager->callOnSuccess(result);
95 LogError("error during processing answer" << _rethrown_exception.GetMessage());
96 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
98 callbackManager->callOnError(error);
103 void ContactManagerController::OnAnswerReceived(const EventContactManagerGetAddressBooksPtr &event)
105 ContactManagerAsyncAnswerHandler::getAddressBooks(event);
108 void ContactManagerGetAddressBooksPendingOperation::handleCallingCallbacks()
110 ContactManagerAsyncAnswerHandler::getAddressBooks(DPL::StaticPointerCast<EventContactManagerGetAddressBooks>(m_event));