Fix for CompatTC Fail
[framework/osp/social.git] / src / FScl_CalendarbookDbConnector.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_CalendarbookDbConnector.cpp
18  * @brief               This is the implementation for _CalendarbookDbConnector class.
19  *
20  * This file contains definitions of @e _CalendarbookDbConnector class.
21  */
22
23 #include <cstdlib>
24 #include <pthread.h>
25 #include <calendar2.h>
26 #include <FBaseResult.h>
27 #include <FBaseRtMutex.h>
28 #include <FBaseSysLog.h>
29 #include <FSysSystemTime.h>
30 #include "FScl_CalendarbookDbConnector.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Runtime;
34
35 namespace Tizen { namespace Social
36 {
37
38 int _CalendarbookDbConnector::__connectingCount = 0;
39 Mutex _CalendarbookDbConnector::__mutex;
40 bool _CalendarbookDbConnector::__isInit = false;
41
42 result
43 _CalendarbookDbConnector::Connect(void)
44 {
45         result r = E_SUCCESS;
46         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
47
48         if (__isInit == false)
49         {
50                 ClearLastResult();
51
52                 pthread_once(&onceBlock, InitCalendarbookDbConnector);
53                 if (GetLastResult() != E_SUCCESS)
54                 {
55                         onceBlock = PTHREAD_ONCE_INIT;
56                         SysLogException(NID_SCL, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
57                         return E_SYSTEM;
58                 }
59
60                 __isInit = true;
61         }
62
63         r = __mutex.Acquire();
64         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
65
66         if (__connectingCount == 0)
67         {
68                 long long startTicks = 0;
69                 long long endTicks = 0;
70
71                 System::SystemTime::GetTicks(startTicks);
72
73                 int errorCode = calendar_connect_with_flags(CALENDAR_CONNECT_FLAG_RETRY);
74
75                 System::SystemTime::GetTicks(endTicks);
76
77                 SysTryCatch(NID_SCL, errorCode == CALENDAR_ERROR_NONE, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Elapsed time : %lld (milliseconds)", endTicks - startTicks);
78                 __connectingCount = 0;
79         }
80
81         __connectingCount++;
82
83         r = __mutex.Release();
84         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
85
86         return E_SUCCESS;
87
88 CATCH:
89         __mutex.Release();
90
91         return E_SYSTEM;
92 }
93
94 result
95 _CalendarbookDbConnector::Disconnect(void)
96 {
97         SysTryReturnResult(NID_SCL, __isInit == true, E_SYSTEM, "A system error has been occurred.");
98
99         result r = __mutex.Acquire();
100         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
101
102         __connectingCount--;
103
104         if (__connectingCount == 0)
105         {
106                 int errorCode = calendar_disconnect();
107                 SysTryCatch(NID_SCL, errorCode == CALENDAR_ERROR_NONE, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
108         }
109
110         r = __mutex.Release();
111         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
112
113         return E_SUCCESS;
114
115 CATCH:
116         __mutex.Release();
117
118         return E_SYSTEM;
119 }
120
121 _CalendarbookDbConnector::_CalendarbookDbConnector(void)
122 {
123 }
124
125 _CalendarbookDbConnector::~_CalendarbookDbConnector(void)
126 {
127 }
128
129 void
130 _CalendarbookDbConnector::InitCalendarbookDbConnector(void)
131 {
132         result r = __mutex.Create();
133         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
134 }
135
136 }}  // Tizen::Social