e0292810ba528740823a0be7cc985b7a1ff23815
[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                         ERR("cal_inotify_init() Fail(%d)", ret);
58                         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
59                         return ret;
60                 }
61
62                 cal_view_initialize();
63         } else if (0 < *connection_count) {
64                 DBG("[System] calendar service is already connected");
65         }
66
67         (*connection_count)++;
68
69         if (0 == reference_count)
70                 cal_dbus_start();
71         reference_count++;
72
73         DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
74
75         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
76         return CALENDAR_ERROR_NONE;
77 }
78
79 int cal_client_disconnect(calendar_h handle, unsigned int id, int *connection_count)
80 {
81         CAL_FN_CALL();
82         int ret = 0;
83
84         RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
85
86         cal_mutex_lock(CAL_MUTEX_CONNECTION);
87         cal_s *h = (cal_s *)handle;
88
89         h->connection_count--;
90         DBG("[Disonnection count:handle] (%d)", h->connection_count);
91
92         if (0 == h->connection_count) {
93                 ret = cal_client_handle_remove(id, handle);
94                 if (CALENDAR_ERROR_NONE != ret)
95                         WARN("cal_client_handle_remove() Fail(%d)", ret);
96         }
97
98         if (1 == *connection_count) {
99                 DBG("[System] disconnected successfully");
100                 cal_view_finalize();
101                 cal_inotify_deinit();
102         } else if (1 < *connection_count) {
103                 DBG("[System] connection count(%d)", *connection_count);
104         } else {
105                 DBG("[System] calendar_connect() is needed");
106                 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
107                 return CALENDAR_ERROR_INVALID_PARAMETER;
108         }
109
110         (*connection_count)--;
111
112         if (1 == reference_count)
113                 cal_dbus_stop();
114         reference_count--;
115
116         DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
117
118         cal_mutex_unlock(CAL_MUTEX_CONNECTION);
119         return CALENDAR_ERROR_NONE;
120 }
121
122 int cal_client_connect_with_flags(calendar_h handle, unsigned int id,
123                 int *connection_count, unsigned int flags)
124 {
125         CAL_FN_CALL();
126         int ret = 0;
127
128         /* If new flag is defined, error check should be updated. */
129         RETVM_IF(flags & 0x11111110, CALENDAR_ERROR_INVALID_PARAMETER, "flag is invalid(%x)", flags);
130
131         ret = cal_client_connect(handle, id, connection_count);
132         if (CALENDAR_ERROR_NONE == ret)
133                 return ret;
134
135         if (flags & CALENDAR_CONNECT_FLAG_RETRY) {
136                 int i = 0;
137                 int retry_time = 500;
138                 for (i = 0; i < 9; i++) {
139                         usleep(retry_time*1000);
140                         ret = cal_client_connect(handle, id, connection_count);
141                         DBG("retry count(%d), ret(%x)", (i+1), ret);
142                         if (CALENDAR_ERROR_NONE == ret)
143                                 break;
144                         if (6 < i)
145                                 retry_time += 30000;
146                         else
147                                 retry_time *= 2;
148                 }
149         }
150         return ret;
151 }