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) {
57 ERR("cal_inotify_init() Fail(%d)", ret);
58 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
62 cal_view_initialize();
63 } else if (0 < *connection_count) {
64 DBG("[System] calendar service is already connected");
67 (*connection_count)++;
69 if (0 == reference_count)
73 DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
75 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
76 return CALENDAR_ERROR_NONE;
79 int cal_client_disconnect(calendar_h handle, unsigned int id, int *connection_count)
84 RETV_IF(NULL == handle, CALENDAR_ERROR_INVALID_PARAMETER);
86 cal_mutex_lock(CAL_MUTEX_CONNECTION);
87 cal_s *h = (cal_s *)handle;
89 h->connection_count--;
90 DBG("[Disonnection count:handle] (%d)", h->connection_count);
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);
98 if (1 == *connection_count) {
99 DBG("[System] disconnected successfully");
101 cal_inotify_deinit();
102 } else if (1 < *connection_count) {
103 DBG("[System] connection count(%d)", *connection_count);
105 DBG("[System] calendar_connect() is needed");
106 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
107 return CALENDAR_ERROR_INVALID_PARAMETER;
110 (*connection_count)--;
112 if (1 == reference_count)
116 DBG("[Connection count]:total(%d) reference(%d)", *connection_count, reference_count);
118 cal_mutex_unlock(CAL_MUTEX_CONNECTION);
119 return CALENDAR_ERROR_NONE;
122 int cal_client_connect_with_flags(calendar_h handle, unsigned int id,
123 int *connection_count, unsigned int flags)
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);
131 ret = cal_client_connect(handle, id, connection_count);
132 if (CALENDAR_ERROR_NONE == ret)
135 if (flags & CALENDAR_CONNECT_FLAG_RETRY) {
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)