add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / client / cal_client_service_helper.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 <unistd.h>
21 #include <stdlib.h>
22 #include <glib-object.h>
23
24 #include "calendar.h"
25 #include "cal_internal.h"
26 #include "cal_typedef.h"
27 #include "cal_inotify.h"
28 #include "cal_view.h"
29 #include "cal_mutex.h"
30 #include "cal_client_reminder.h"
31 #include "cal_client_handle.h"
32 #include "cal_client_dbus.h"
33 #include "cal_client_utils.h"
34 #include "cal_client_reminder.h"
35
36 static int reference_count = 0; /* total connection include on_thread */
37
38 int cal_client_connect(calendar_h handle, unsigned int id, int *connection_count)
39 {
40         CAL_FN_CALL();
41         int ret = 0;
42
43         RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
44
45         cal_mutex_lock(CAL_MUTEX_CONNECTION);
46         cal_s *h = (cal_s *)handle;
47
48         h->connection_count++;
49         DBG("[Connection count:handle] (%d)", h->connection_count);
50
51         if (0 == *connection_count) { /* total connection */
52 #if !GLIB_CHECK_VERSION(2, 35, 0)
53                 g_type_init(); /* for alarmmgr */
54 #endif
55                 ret = cal_inotify_init();
56                 if (CALENDAR_ERROR_NONE != ret) {
57                         /* LCOV_EXCL_START */
58                         ERR("cal_inotify_init() Fail(%d)", ret);
59                         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
60                         return ret;
61                         /* LCOV_EXCL_STOP */
62                 }
63
64                 cal_view_initialize();
65         } else if (0 < *connection_count) {
66                 DBG("[System] calendar service is already connected");
67         }
68
69         (*connection_count)++;
70
71         if (0 == reference_count)
72                 cal_dbus_start();
73         reference_count++;
74
75         DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
76
77         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
78         return CALENDAR_ERROR_NONE;
79 }
80
81 int cal_client_disconnect(calendar_h handle, unsigned int id, int *connection_count)
82 {
83         CAL_FN_CALL();
84         int ret = 0;
85
86         RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
87
88         cal_mutex_lock(CAL_MUTEX_CONNECTION);
89         cal_s *h = (cal_s *)handle;
90
91         h->connection_count--;
92         DBG("[Disonnection count:handle] (%d)", h->connection_count);
93
94         if (0 == h->connection_count) {
95                 ret = cal_client_handle_remove(id, handle);
96                 if (CALENDAR_ERROR_NONE != ret)
97                         WARN("cal_client_handle_remove() Fail(%d)", ret);
98         }
99
100         if (1 == *connection_count) {
101                 DBG("[System] disconnected successfully");
102                 cal_view_finalize();
103                 cal_inotify_deinit();
104         } else if (1 < *connection_count) {
105                 DBG("[System] connection count(%d)", *connection_count);
106         } else {
107                 /* LCOV_EXCL_START */
108                 ERR("[System] calendar_connect() is needed");
109                 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
110                 return CALENDAR_ERROR_INVALID_PARAMETER;
111                 /* LCOV_EXCL_STOP */
112         }
113
114         (*connection_count)--;
115
116         if (1 == reference_count)
117                 cal_dbus_stop();
118         reference_count--;
119
120         DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
121
122         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
123         return CALENDAR_ERROR_NONE;
124 }
125
126 int cal_client_connect_with_flags(calendar_h handle, unsigned int id,
127                 int *connection_count, unsigned int flags)
128 {
129         CAL_FN_CALL();
130         int ret = 0;
131
132         /* If new flag is defined, error check should be updated. */
133         RETVM_IF(flags & 0x11111110, CALENDAR_ERROR_INVALID_PARAMETER, "flag is invalid(%x)", flags);
134
135         ret = cal_client_connect(handle, id, connection_count);
136         if (CALENDAR_ERROR_NONE == ret)
137                 return ret;
138
139         if (flags & CALENDAR_CONNECT_FLAG_RETRY) {
140                 int i = 0;
141                 int retry_time = 500;
142                 for (i = 0; i < 9; i++) {
143                         usleep(retry_time*1000);
144                         ret = cal_client_connect(handle, id, connection_count);
145                         DBG("retry count(%d), ret(%x)", (i+1), ret);
146                         if (CALENDAR_ERROR_NONE == ret)
147                                 break;
148                         if (6 < i)
149                                 retry_time += 30000;
150                         else
151                                 retry_time *= 2;
152                 }
153         }
154         return ret;
155 }