Remove async APIs same as 2.4
[platform/core/pim/contacts-service.git] / client / ctsvc_client_noti.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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 <pims-ipc.h>
21 #include <pims-ipc-svc.h>
22 #include <pims-ipc-data.h>
23
24 #include "contacts.h"
25 #include "ctsvc_internal.h"
26 #include "ctsvc_ipc_define.h"
27 #include "ctsvc_mutex.h"
28 #include "ctsvc_client_ipc.h"
29 #include "contacts_extension.h"
30
31 typedef struct
32 {
33         contacts_db_change_cb_with_info cb;
34         void *user_data;
35 }db_callback_info_s;
36
37 typedef struct
38 {
39         char *view_uri;
40         GSList *callbacks;
41 }subscribe_info_s;
42
43 static int __ipc_pubsub_ref = 0;
44 static pims_ipc_h __ipc = NULL;
45 static GSList *__db_change_subscribe_list = NULL;
46
47 static void __ctsvc_db_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
48 {
49         unsigned int size = 0;
50         char *str = NULL;
51         subscribe_info_s *info = user_data;
52
53         if (data)
54                 str = (char*)pims_ipc_data_get(data, &size);
55
56         if (!str) {
57                 CTS_ERR("There is no changed data");
58                 return;
59         }
60
61         if (info) {
62                 GSList *l;
63                 for (l = info->callbacks;l;l=l->next) {
64                         db_callback_info_s *cb_info = l->data;
65                         if (cb_info->cb) {
66                                 cb_info->cb(info->view_uri, str, cb_info->user_data);
67                         }
68                 }
69         }
70 }
71
72 // This API should be called in CTS_MUTEX_PIMS_IPC_PUBSUB mutex
73 pims_ipc_h ctsvc_ipc_get_handle_for_change_subsciption()
74 {
75         return __ipc;
76 }
77
78 int ctsvc_ipc_create_for_change_subscription()
79 {
80         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
81
82         if (0 < __ipc_pubsub_ref) {
83                 __ipc_pubsub_ref++;
84                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
85                 return CONTACTS_ERROR_NONE;
86         }
87
88         if (!__ipc) {
89                 __ipc = pims_ipc_create_for_subscribe(CTSVC_IPC_SOCKET_PATH_FOR_CHANGE_SUBSCRIPTION);
90                 if (!__ipc) {
91                         CTS_ERR("pims_ipc_create_for_subscribe error\n");
92                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
93                         return CONTACTS_ERROR_IPC;
94                 }
95         }
96         __ipc_pubsub_ref++;
97         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
98         return CONTACTS_ERROR_NONE;
99 }
100
101 int ctsvc_ipc_destroy_for_change_subscription()
102 {
103         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
104
105         if (1 == __ipc_pubsub_ref) {
106                 pims_ipc_destroy_for_subscribe(__ipc);
107                 __ipc = NULL;
108         }
109         else if (1 < __ipc_pubsub_ref) {
110                 CTS_DBG("ctsvc pubsub ipc ref count : %d", __ipc_pubsub_ref);
111         }
112         else {
113                 CTS_DBG("System : please call connection APIs, connection count is (%d)", __ipc_pubsub_ref);
114                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
115                 return CONTACTS_ERROR_INVALID_PARAMETER;
116         }
117
118         __ipc_pubsub_ref--;
119
120         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
121         return CONTACTS_ERROR_NONE;
122 }
123
124 API int contacts_db_add_changed_cb_with_info(const char* view_uri,
125                 contacts_db_change_cb_with_info cb, void* user_data)
126 {
127         GSList *it = NULL;
128         subscribe_info_s *info = NULL;
129         db_callback_info_s *cb_info;
130         bool result = false;
131         int ret;
132
133         RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
134         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
135
136         if (strncmp(view_uri, CTSVC_VIEW_URI_PHONELOG, strlen(CTSVC_VIEW_URI_PHONELOG)) == 0) {
137                 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_PHONELOG_READ, &result);
138                 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
139                 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (phonelog read)");
140         }
141         else if (strncmp(view_uri, CTSVC_VIEW_URI_PERSON, strlen(CTSVC_VIEW_URI_PERSON)) == 0) {
142                 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
143                 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
144                 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
145         }
146         else {
147                 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
148                 return CONTACTS_ERROR_INVALID_PARAMETER;
149         }
150
151         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
152
153         for (it=__db_change_subscribe_list;it;it=it->next) {
154                 if (!it->data) continue;
155
156                 info = it->data;
157                 if (strcmp(info->view_uri, view_uri) == 0)
158                         break;
159                 else
160                         info = NULL;
161         }
162
163         if (!info) {
164                 info = calloc(1, sizeof(subscribe_info_s));
165                 if (NULL == info) {
166                         CTS_ERR("calloc() Failed");
167                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
168                         return CONTACTS_ERROR_OUT_OF_MEMORY;
169                 }
170
171                 if (pims_ipc_subscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, (char*)view_uri,
172                                         __ctsvc_db_subscriber_callback, (void*)info) != 0) {
173                         CTS_ERR("pims_ipc_subscribe error\n");
174                         free(info);
175                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
176                         return CONTACTS_ERROR_IPC;
177                 }
178                 info->view_uri = strdup(view_uri);
179                 __db_change_subscribe_list = g_slist_append(__db_change_subscribe_list, info);
180         }
181         else {
182                 GSList *l;
183                 for (l = info->callbacks;l;l=l->next) {
184                         db_callback_info_s *cb_info = l->data;
185                         if (cb_info->cb == cb && cb_info->user_data == user_data) {
186                                 CTS_ERR("The same callback(%s) is already exist", view_uri);
187                                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
188                                 return CONTACTS_ERROR_INVALID_PARAMETER;
189                         }
190                 }
191         }
192
193         cb_info = calloc(1, sizeof(db_callback_info_s));
194         cb_info->user_data = user_data;
195         cb_info->cb = cb;
196         info->callbacks = g_slist_append(info->callbacks, cb_info);
197
198         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
199         return CONTACTS_ERROR_NONE;
200 }
201
202 API int contacts_db_remove_changed_cb_with_info(const char* view_uri,
203                 contacts_db_change_cb_with_info cb, void* user_data)
204 {
205         GSList *it = NULL;
206         subscribe_info_s *info = NULL;
207
208         RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
209         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
210
211         if (strcmp(view_uri, CTSVC_VIEW_URI_PHONELOG) != 0 &&
212                         strcmp(view_uri, CTSVC_VIEW_URI_PERSON) != 0) {
213                 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
214                 return CONTACTS_ERROR_INVALID_PARAMETER;
215         }
216
217         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
218
219         for (it=__db_change_subscribe_list;it;it=it->next) {
220                 if (!it->data) continue;
221
222                 info = it->data;
223                 if (strcmp(info->view_uri, view_uri) == 0)
224                         break;
225                 else
226                         info = NULL;
227         }
228
229         if (info) {
230                 GSList *l;
231                 for(l = info->callbacks;l;l=l->next) {
232                         db_callback_info_s *cb_info = l->data;
233                         if (cb == cb_info->cb && user_data == cb_info->user_data) {
234                                 info->callbacks = g_slist_remove(info->callbacks, cb_info);
235                                 free(cb_info);
236                                 break;
237                         }
238                 }
239                 if (g_slist_length(info->callbacks) == 0) {
240                         pims_ipc_unsubscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, info->view_uri);
241                         __db_change_subscribe_list = g_slist_remove(__db_change_subscribe_list, info);
242                         free(info->view_uri);
243                         free(info);
244                 }
245         }
246
247         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
248         return CONTACTS_ERROR_NONE;
249 }