Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_AccountDbConnector.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_AccountDbConnector.cpp
18  * @brief               This is the implementation for _AccountDbConnector class.
19  *
20  * This file contains definitions of @e _AccountDbConnector class.
21  */
22
23 #include <unique_ptr.h>
24 #include <pthread.h>
25 #include <FBaseSysLog.h>
26 #include "FScl_AccountDbConnector.h"
27
28 namespace Tizen { namespace Social
29 {
30
31 bool _AccountDbConnector::__isAccountDbConnected = false;
32 _AccountDbConnector* _AccountDbConnector::__pInstance = null;
33
34 _AccountDbConnector::_AccountDbConnector(void)
35 {
36 }
37
38 _AccountDbConnector::~_AccountDbConnector(void)
39 {
40 }
41
42 result
43 _AccountDbConnector::Connect(void)
44 {
45         int ret = account_connect();
46         SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
47
48         return E_SUCCESS;
49 }
50
51 result
52 _AccountDbConnector::Disconnect(void)
53 {
54         int ret = account_disconnect();
55         SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
56
57         return E_SUCCESS;
58 }
59
60 result
61 _AccountDbConnector::EnsureDbConnection(void)
62 {
63         if (!__isAccountDbConnected)
64         {
65                 _AccountDbConnector* pInstance = GetInstance();
66                 SysTryReturn(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67
68                 result r = pInstance->Connect();
69                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
70
71                 __isAccountDbConnected = true;
72         }
73
74         return E_SUCCESS;
75 }
76
77 void
78 _AccountDbConnector::InitAccountDbConnector(void)
79 {
80         std::unique_ptr<_AccountDbConnector> pInstance(new (std::nothrow) _AccountDbConnector());
81         SysTryReturnVoidResult(NID_SCL, pInstance, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
82
83         __pInstance = pInstance.release();
84
85         std::atexit(DestroyAccountDbConnector);
86 }
87
88 void
89 _AccountDbConnector::DestroyAccountDbConnector(void)
90 {
91         delete __pInstance;
92         __pInstance = null;
93 }
94
95 _AccountDbConnector*
96 _AccountDbConnector::GetInstance(void)
97 {
98         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
99
100         if (__pInstance == null)
101         {
102                 ClearLastResult();
103
104                 pthread_once(&onceBlock, InitAccountDbConnector);
105
106                 result r = GetLastResult();
107
108                 if (IsFailed(r))
109                 {
110                         onceBlock = PTHREAD_ONCE_INIT;
111                 }
112         }
113
114         return __pInstance;
115 }
116
117 }}  // Tizen::Social