Fix code for TDIS-5396
[framework/osp/social.git] / src / FScl_ContactDbConnector.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FScl_AddressbookUtil.cpp
19  * @brief               This is the implementation for _AddressbookUtil class.
20  *
21  * This file contains definitions of @e _AddressbookUtil class.
22  */
23
24 #include <unique_ptr.h>
25 #include <pthread.h>
26 #include <FBaseSysLog.h>
27 #include <FSysSystemTime.h>
28 #include "FScl_ContactDbConnector.h"
29
30 namespace Tizen { namespace Social
31 {
32
33 bool _ContactDbConnector::__isConnected = false;
34 _ContactDbConnector* _ContactDbConnector::__pInstance = null;
35
36 _ContactDbConnector::_ContactDbConnector(void)
37 {
38 }
39
40 _ContactDbConnector::~_ContactDbConnector(void)
41 {
42 }
43
44 result
45 _ContactDbConnector::Connect(void)
46 {
47         long long startTicks = 0;
48         long long endTicks = 0;
49
50         System::SystemTime::GetTicks(startTicks);
51
52         int ret = contacts_connect_with_flags(CONTACTS_CONNECT_FLAG_RETRY);
53
54         System::SystemTime::GetTicks(endTicks);
55
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));
59
60         return E_SUCCESS;
61 }
62
63 result
64 _ContactDbConnector::Disconnect(void)
65 {
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));
69
70         return E_SUCCESS;
71 }
72
73 result
74 _ContactDbConnector::EnsureDbConnection(void)
75 {
76         if (!__isConnected)
77         {
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));
80
81                 result r = pInstance->Connect();
82                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
83
84                 __isConnected = true;
85         }
86
87         return E_SUCCESS;
88 }
89
90 void
91 _ContactDbConnector::InitContactDbConnector(void)
92 {
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));
95
96         __pInstance = pInstance.release();
97
98         std::atexit(DestroyContactDbConnector);
99 }
100
101 void
102 _ContactDbConnector::DestroyContactDbConnector(void)
103 {
104         delete __pInstance;
105         __pInstance = null;
106 }
107
108 _ContactDbConnector*
109 _ContactDbConnector::GetInstance(void)
110 {
111         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
112
113         if (__pInstance == null)
114         {
115                 ClearLastResult();
116
117                 pthread_once(&onceBlock, InitContactDbConnector);
118
119                 result r = GetLastResult();
120
121                 if (IsFailed(r))
122                 {
123                         onceBlock = PTHREAD_ONCE_INIT;
124                 }
125         }
126
127         return __pInstance;
128 }
129
130 }}  // Tizen::Social