2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
17 * @file FScl_AddressbookUtil.cpp
18 * @brief This is the implementation for _AddressbookUtil class.
20 * This file contains definitions of @e _AddressbookUtil class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FSysSystemTime.h>
27 #include "FScl_ContactDbConnector.h"
29 namespace Tizen { namespace Social
32 bool _ContactDbConnector::__isConnected = false;
33 _ContactDbConnector* _ContactDbConnector::__pInstance = null;
35 _ContactDbConnector::_ContactDbConnector(void)
39 _ContactDbConnector::~_ContactDbConnector(void)
44 _ContactDbConnector::Connect(void)
46 long long startTicks = 0;
47 long long endTicks = 0;
49 System::SystemTime::GetTicks(startTicks);
51 int ret = contacts_connect_with_flags(CONTACTS_CONNECT_FLAG_RETRY);
53 System::SystemTime::GetTicks(endTicks);
55 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));
56 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);
57 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
63 _ContactDbConnector::Disconnect(void)
65 int ret = contacts_disconnect2();
66 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));
67 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
73 _ContactDbConnector::EnsureDbConnection(void)
77 _ContactDbConnector* pInstance = GetInstance();
78 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
80 result r = pInstance->Connect();
81 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
90 _ContactDbConnector::InitContactDbConnector(void)
92 std::unique_ptr<_ContactDbConnector> pInstance(new (std::nothrow) _ContactDbConnector());
93 SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
95 __pInstance = pInstance.release();
97 std::atexit(DestroyContactDbConnector);
101 _ContactDbConnector::DestroyContactDbConnector(void)
108 _ContactDbConnector::GetInstance(void)
110 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
112 if (__pInstance == null)
116 pthread_once(&onceBlock, InitContactDbConnector);
118 result r = GetLastResult();
122 onceBlock = PTHREAD_ONCE_INIT;