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 <glib-object.h>
25 #include "cal_internal.h"
26 #include "cal_typedef.h"
27 #include "cal_inotify.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"
36 static int reference_count = 0; /* total connection include on_thread */
38 int cal_client_connect(calendar_h handle, unsigned int id, int *connection_count)
43 RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
45 cal_mutex_lock(CAL_MUTEX_CONNECTION);
46 cal_s *h = (cal_s *)handle;
48 h->connection_count++;
49 DBG("[Connection count:handle] (%d)", h->connection_count);
51 if (0 == *connection_count) { /* total connection */
52 #if !GLIB_CHECK_VERSION(2, 35, 0)
53 g_type_init(); /* for alarmmgr */
55 ret = cal_inotify_init();
56 if (CALENDAR_ERROR_NONE != ret) {
58 ERR("cal_inotify_init() Fail(%d)", ret);
59 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
64 cal_view_initialize();
65 } else if (0 < *connection_count) {
66 DBG("[System] calendar service is already connected");
69 (*connection_count)++;
71 if (0 == reference_count)
75 DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
77 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
78 return CALENDAR_ERROR_NONE;
81 int cal_client_disconnect(calendar_h handle, unsigned int id, int *connection_count)
86 RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
88 cal_mutex_lock(CAL_MUTEX_CONNECTION);
89 cal_s *h = (cal_s *)handle;
91 h->connection_count--;
92 DBG("[Disonnection count:handle] (%d)", h->connection_count);
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);
100 if (1 == *connection_count) {
101 DBG("[System] disconnected successfully");
103 cal_inotify_deinit();
104 } else if (1 < *connection_count) {
105 DBG("[System] connection count(%d)", *connection_count);
107 /* LCOV_EXCL_START */
108 ERR("[System] calendar_connect() is needed");
109 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
110 return CALENDAR_ERROR_INVALID_PARAMETER;
114 (*connection_count)--;
116 if (1 == reference_count)
120 DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
122 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
123 return CALENDAR_ERROR_NONE;
126 int cal_client_connect_with_flags(calendar_h handle, unsigned int id,
127 int *connection_count, unsigned int flags)
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);
135 ret = cal_client_connect(handle, id, connection_count);
136 if (CALENDAR_ERROR_NONE == ret)
139 if (flags & CALENDAR_CONNECT_FLAG_RETRY) {
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)