Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / ContactManagerController.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file        ContactManagerController.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
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"
29
30 namespace TizenApis {
31 namespace Tizen1_0 {
32 namespace Contact {
33
34 using namespace TizenApis::Commons;
35 using namespace TizenApis::Api::Contact;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38
39 ContactManagerController::ContactManagerController(JSContextRef context, IContactManagerPtr contactManager) :
40                 EventContactManagerGetAddressBooksAnswerReceiver(ThreadEnum::NULL_THREAD),
41                 ContactManagerPrivObject(context, contactManager)
42 {
43 }
44
45 ContactManagerController::~ContactManagerController()
46 {
47 }
48
49 void ContactManagerAsyncAnswerHandler::getAddressBooks(const EventContactManagerGetAddressBooksPtr &event)
50 {
51         JSCallbackManagerPtr callbackManager =
52                         DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
53         if (!callbackManager) {
54                 LogError("no callback manager");
55                 return;
56         }
57
58         JSContextRef gContext = callbackManager->getContext();
59
60         JSValueRef error = NULL;
61         if (ExceptionCodes::None != event->getExceptionCode())
62         {
63                 switch (event->getExceptionCode())
64                 {
65                 case ExceptionCodes::PlatformException:
66                         error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
67                         break;
68                 case ExceptionCodes::InvalidArgumentException:
69                         error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Invalid value error");
70                         break;
71                 case ExceptionCodes::ConversionException:
72                         error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Wrong argument");
73                         break;
74                 default:
75                         error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Unknown error");
76                         break;
77                 }
78
79                 callbackManager->callOnError(error);
80                 return;
81         }
82
83         Try {
84                 if (!event->getResult())
85                         ThrowMsg(PlatformException, "No result.");
86
87                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
88                 IAddressBookArrayPtr addressBooks = event->getAddressBooks();
89
90                 JSValueRef result = converter->toJSValueRef(addressBooks);
91
92                 callbackManager->callOnSuccess(result);
93                 return;
94         } Catch(Exception) {
95                 LogError("error during processing answer" << _rethrown_exception.GetMessage());
96                 error = JSTizenExceptionFactory::makeErrorObject(gContext, JSTizenException::UNKNOWN_ERROR, "Internal error");
97
98                 callbackManager->callOnError(error);
99                 return;
100         }
101 }
102
103 void ContactManagerController::OnAnswerReceived(const EventContactManagerGetAddressBooksPtr &event)
104 {
105         ContactManagerAsyncAnswerHandler::getAddressBooks(event);
106 }
107
108 void ContactManagerGetAddressBooksPendingOperation::handleCallingCallbacks()
109 {
110         ContactManagerAsyncAnswerHandler::getAddressBooks(DPL::StaticPointerCast<EventContactManagerGetAddressBooks>(m_event));
111 }
112
113 } // Contact
114 } // Tizen1_0
115 } // TizenApis