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