4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_db_util.h"
26 #include "cal_utils.h"
27 #include "cal_mutex.h"
28 #include "cal_inotify.h"
30 static int cal_total_connection = 0;
31 static TLS int cal_thread_connection = 0;
38 cal_mutex_lock(CAL_MUTEX_CONNECTION);
39 if (0 == cal_total_connection) {
40 #if !GLIB_CHECK_VERSION(2, 35, 0)
41 g_type_init(); /* added for alarmmgr */
43 cal_view_initialize();
44 ret = cal_inotify_init();
45 if (CALENDAR_ERROR_NONE != ret) {
47 ERR("cal_inotify_init() Fail(%d)", ret);
48 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
53 DBG("[System] calendar service has been already connected");
55 cal_total_connection++;
57 if (0 == cal_thread_connection) {
58 ret = cal_db_util_open();
59 if (CALENDAR_ERROR_NONE != ret) {
61 ERR("_cal_db_open() Fail(%d)", ret);
62 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
67 cal_thread_connection++;
68 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
70 return CALENDAR_ERROR_NONE;
73 int cal_disconnect(void)
77 cal_mutex_lock(CAL_MUTEX_CONNECTION);
78 if (1 == cal_thread_connection) {
80 } else if (cal_thread_connection <= 0) {
82 DBG("[System] not connected, count(%d)", cal_thread_connection);
83 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
84 return CALENDAR_ERROR_INVALID_PARAMETER;
87 cal_thread_connection--;
89 if (1 == cal_total_connection) {
92 } else if (1 < cal_total_connection) {
93 DBG("[System] connection count(%d)", cal_total_connection);
96 DBG("[System] Not connected, count(%d)", cal_total_connection);
97 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
98 return CALENDAR_ERROR_INVALID_PARAMETER;
101 cal_total_connection--;
103 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
104 return CALENDAR_ERROR_NONE;
107 void cal_service_internal_disconnect(void)
109 cal_mutex_lock(CAL_MUTEX_CONNECTION);
111 if (1 == cal_thread_connection) {
113 cal_thread_connection--;
115 if (1 <= cal_total_connection)
116 cal_total_connection--;
118 cal_mutex_unlock(CAL_MUTEX_CONNECTION);