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) {
46 ERR("cal_inotify_init() Fail(%d)", ret);
47 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
51 DBG("[System] calendar service has been already connected");
53 cal_total_connection++;
55 if (0 == cal_thread_connection) {
56 ret = cal_db_util_open();
57 if (CALENDAR_ERROR_NONE != ret) {
58 ERR("_cal_db_open() Fail(%d)", ret);
59 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
63 cal_thread_connection++;
64 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
66 return CALENDAR_ERROR_NONE;
69 int cal_disconnect(void)
73 cal_mutex_lock(CAL_MUTEX_CONNECTION);
74 if (1 == cal_thread_connection) {
76 } else if (cal_thread_connection <= 0) {
77 DBG("[System] not connected, count(%d)", cal_thread_connection);
78 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
79 return CALENDAR_ERROR_INVALID_PARAMETER;
81 cal_thread_connection--;
83 if (1 == cal_total_connection) {
86 } else if (1 < cal_total_connection) {
87 DBG("[System] connection count(%d)", cal_total_connection);
89 DBG("[System] Not connected, count(%d)", cal_total_connection);
90 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
91 return CALENDAR_ERROR_INVALID_PARAMETER;
93 cal_total_connection--;
95 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
96 return CALENDAR_ERROR_NONE;
99 void cal_service_internal_disconnect(void)
101 cal_mutex_lock(CAL_MUTEX_CONNECTION);
103 if (1 == cal_thread_connection) {
105 cal_thread_connection--;
107 if (1 <= cal_total_connection)
108 cal_total_connection--;
110 cal_mutex_unlock(CAL_MUTEX_CONNECTION);