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