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 __thread bool isConnected = false;
34 pthread_key_t tlsKey = 0;
35 _ContactDbConnector* _ContactDbConnector::__pInstance = null;
37 _ContactDbConnector::_ContactDbConnector(void)
41 _ContactDbConnector::~_ContactDbConnector(void)
46 _ContactDbConnector::OnThreadExit(void *pValue)
48 _ContactDbConnector* pInstance = static_cast<_ContactDbConnector*>(pValue);
50 if (pInstance != null)
52 pInstance->Disconnect();
55 pthread_setspecific(tlsKey, NULL);
59 _ContactDbConnector::Connect(void)
61 long long startTicks = 0;
62 long long endTicks = 0;
64 System::SystemTime::GetTicks(startTicks);
66 int ret = contacts_connect_with_flags(CONTACTS_CONNECT_FLAG_RETRY);
68 System::SystemTime::GetTicks(endTicks);
70 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));
71 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);
72 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
74 pthread_setspecific(tlsKey, this);
80 _ContactDbConnector::Disconnect(void)
82 int ret = contacts_disconnect2();
83 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));
84 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
90 _ContactDbConnector::EnsureDbConnection(void)
94 _ContactDbConnector* pInstance = GetInstance();
95 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
97 result r = pInstance->Connect();
98 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
107 _ContactDbConnector::InitContactDbConnector(void)
109 std::unique_ptr<_ContactDbConnector> pInstance(new (std::nothrow) _ContactDbConnector());
110 SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
112 __pInstance = pInstance.release();
114 pthread_key_create(&tlsKey, OnThreadExit);
116 std::atexit(DestroyContactDbConnector);
120 _ContactDbConnector::DestroyContactDbConnector(void)
127 _ContactDbConnector::GetInstance(void)
129 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
131 if (__pInstance == null)
135 pthread_once(&onceBlock, InitContactDbConnector);
137 result r = GetLastResult();
141 onceBlock = PTHREAD_ONCE_INIT;