2 // Open Service Platform
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.
18 * @file FScl_AddressbookUtil.cpp
19 * @brief This is the implementation for _AddressbookUtil class.
21 * This file contains definitions of @e _AddressbookUtil class.
24 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FSysSystemTime.h>
28 #include "FScl_ContactDbConnector.h"
30 namespace Tizen { namespace Social
33 bool _ContactDbConnector::__isConnected = false;
34 _ContactDbConnector* _ContactDbConnector::__pInstance = null;
36 _ContactDbConnector::_ContactDbConnector(void)
40 _ContactDbConnector::~_ContactDbConnector(void)
45 _ContactDbConnector::Connect(void)
47 long long startTicks = 0;
48 long long endTicks = 0;
50 System::SystemTime::GetTicks(startTicks);
52 int ret = contacts_connect_with_flags(CONTACTS_CONNECT_FLAG_RETRY);
54 System::SystemTime::GetTicks(endTicks);
56 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
57 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_IPC_NOT_AVALIABLE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Contact server is not available. Elapsed time : %lld", GetErrorMessage(E_SYSTEM), endTicks - startTicks);
58 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
64 _ContactDbConnector::Disconnect(void)
66 int ret = contacts_disconnect2();
67 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
74 _ContactDbConnector::EnsureDbConnection(void)
78 _ContactDbConnector* pInstance = GetInstance();
79 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
81 result r = pInstance->Connect();
82 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
91 _ContactDbConnector::InitContactDbConnector(void)
93 std::unique_ptr<_ContactDbConnector> pInstance(new (std::nothrow) _ContactDbConnector());
94 SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
96 __pInstance = pInstance.release();
98 std::atexit(DestroyContactDbConnector);
102 _ContactDbConnector::DestroyContactDbConnector(void)
109 _ContactDbConnector::GetInstance(void)
111 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
113 if (__pInstance == null)
117 pthread_once(&onceBlock, InitContactDbConnector);
119 result r = GetLastResult();
123 onceBlock = PTHREAD_ONCE_INIT;