summaryrefslogtreecommitdiff
path: root/src/FScl_CalendarbookDbConnector.cpp
blob: a835d502cb8a3f638fc748abcb50e55c739f5c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
 * @file		FScl_CalendarbookDbConnector.cpp
 * @brief		This is the implementation for _CalendarbookDbConnector class.
 *
 * This file contains definitions of @e _CalendarbookDbConnector class.
 */

#include <cstdlib>
#include <pthread.h>
#include <calendar2.h>
#include <FBaseResult.h>
#include <FBaseRtMutex.h>
#include <FBaseSysLog.h>
#include <FSysSystemTime.h>
#include "FScl_CalendarbookDbConnector.h"

using namespace Tizen::Base;
using namespace Tizen::Base::Runtime;

namespace Tizen { namespace Social
{

int _CalendarbookDbConnector::__connectingCount = 0;
Mutex _CalendarbookDbConnector::__mutex;
bool _CalendarbookDbConnector::__isInit = false;

result
_CalendarbookDbConnector::Connect(void)
{
	result r = E_SUCCESS;
	static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;

	if (__isInit == false)
	{
		ClearLastResult();

		pthread_once(&onceBlock, InitCalendarbookDbConnector);
		if (GetLastResult() != E_SUCCESS)
		{
			onceBlock = PTHREAD_ONCE_INIT;
			SysLogException(NID_SCL, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
			return E_SYSTEM;
		}

		__isInit = true;
	}

	r = __mutex.Acquire();
	SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");

	if (__connectingCount == 0)
	{
		long long startTicks = 0;
		long long endTicks = 0;

		System::SystemTime::GetTicks(startTicks);

		int errorCode = calendar_connect_with_flags(CALENDAR_CONNECT_FLAG_RETRY);

		System::SystemTime::GetTicks(endTicks);

		SysTryCatch(NID_SCL, errorCode == CALENDAR_ERROR_NONE, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Elapsed time : %lld (milliseconds)", endTicks - startTicks);
		__connectingCount = 0;
	}

	__connectingCount++;

	r = __mutex.Release();
	SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");

	return E_SUCCESS;

CATCH:
	__mutex.Release();

	return E_SYSTEM;
}

result
_CalendarbookDbConnector::Disconnect(void)
{
	SysTryReturnResult(NID_SCL, __isInit == true, E_SYSTEM, "A system error has been occurred.");

	result r = __mutex.Acquire();
	SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");

	__connectingCount--;

	if (__connectingCount == 0)
	{
		int errorCode = calendar_disconnect();
		SysTryCatch(NID_SCL, errorCode == CALENDAR_ERROR_NONE, , E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
	}

	r = __mutex.Release();
	SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");

	return E_SUCCESS;

CATCH:
	__mutex.Release();

	return E_SYSTEM;
}

_CalendarbookDbConnector::_CalendarbookDbConnector(void)
{
}

_CalendarbookDbConnector::~_CalendarbookDbConnector(void)
{
}

void
_CalendarbookDbConnector::InitCalendarbookDbConnector(void)
{
	result r = __mutex.Create();
	SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
}

}}  // Tizen::Social