36b2d91f7eefd1011edecc83ab555587c7d14221
[platform/core/pim/calendar-service.git] / server / cal_server_service.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_db_util.h"
26 #include "cal_utils.h"
27 #include "cal_mutex.h"
28 #include "cal_inotify.h"
29
30 static int cal_total_connection = 0;
31 static TLS int cal_thread_connection = 0;
32
33 int cal_connect(void)
34 {
35         CAL_FN_CALL();
36         int ret = 0;
37
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 */
42 #endif
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);
48                         return ret;
49                 }
50         } else {
51                 DBG("[System] calendar service has been already connected");
52         }
53         cal_total_connection++;
54
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);
60                         return ret;
61                 }
62         }
63         cal_thread_connection++;
64         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
65
66         return CALENDAR_ERROR_NONE;
67 }
68
69 int cal_disconnect(void)
70 {
71         CAL_FN_CALL();
72
73         cal_mutex_lock(CAL_MUTEX_CONNECTION);
74         if (1 == cal_thread_connection) {
75                 cal_db_util_close();
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;
80         }
81         cal_thread_connection--;
82
83         if (1 == cal_total_connection) {
84                 cal_inotify_deinit();
85                 cal_view_finalize();
86         } else if (1 < cal_total_connection) {
87                 DBG("[System] connection count(%d)", cal_total_connection);
88         } else {
89                 DBG("[System] Not connected, count(%d)", cal_total_connection);
90                 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
91                 return CALENDAR_ERROR_INVALID_PARAMETER;
92         }
93         cal_total_connection--;
94
95         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
96         return CALENDAR_ERROR_NONE;
97 }
98
99 void cal_service_internal_disconnect(void)
100 {
101         cal_mutex_lock(CAL_MUTEX_CONNECTION);
102
103         if (1 == cal_thread_connection) {
104                 cal_db_util_close();
105                 cal_thread_connection--;
106
107                 if (1 <= cal_total_connection)
108                         cal_total_connection--;
109         }
110         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
111         return;
112 }