add comment LCOV_EXCL
[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                         /* LCOV_EXCL_START */
47                         ERR("cal_inotify_init() Fail(%d)", ret);
48                         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
49                         return ret;
50                         /* LCOV_EXCL_STOP */
51                 }
52         } else {
53                 DBG("[System] calendar service has been already connected");
54         }
55         cal_total_connection++;
56
57         if (0 == cal_thread_connection) {
58                 ret = cal_db_util_open();
59                 if (CALENDAR_ERROR_NONE != ret) {
60                         /* LCOV_EXCL_START */
61                         ERR("_cal_db_open() Fail(%d)", ret);
62                         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
63                         return ret;
64                         /* LCOV_EXCL_STOP */
65                 }
66         }
67         cal_thread_connection++;
68         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
69
70         return CALENDAR_ERROR_NONE;
71 }
72
73 int cal_disconnect(void)
74 {
75         CAL_FN_CALL();
76
77         cal_mutex_lock(CAL_MUTEX_CONNECTION);
78         if (1 == cal_thread_connection) {
79                 cal_db_util_close();
80         } else if (cal_thread_connection <= 0) {
81                 /* LCOV_EXCL_START */
82                 DBG("[System] not connected, count(%d)", cal_thread_connection);
83                 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
84                 return CALENDAR_ERROR_INVALID_PARAMETER;
85                 /* LCOV_EXCL_STOP */
86         }
87         cal_thread_connection--;
88
89         if (1 == cal_total_connection) {
90                 cal_inotify_deinit();
91                 cal_view_finalize();
92         } else if (1 < cal_total_connection) {
93                 DBG("[System] connection count(%d)", cal_total_connection);
94         } else {
95                 /* LCOV_EXCL_START */
96                 DBG("[System] Not connected, count(%d)", cal_total_connection);
97                 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
98                 return CALENDAR_ERROR_INVALID_PARAMETER;
99                 /* LCOV_EXCL_STOP */
100         }
101         cal_total_connection--;
102
103         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
104         return CALENDAR_ERROR_NONE;
105 }
106
107 void cal_service_internal_disconnect(void)
108 {
109         cal_mutex_lock(CAL_MUTEX_CONNECTION);
110
111         if (1 == cal_thread_connection) {
112                 cal_db_util_close();
113                 cal_thread_connection--;
114
115                 if (1 <= cal_total_connection)
116                         cal_total_connection--;
117         }
118         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
119         return;
120 }