ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION
[platform/core/pim/contacts-service.git] / client / ctsvc_client_setting.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 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 <glib.h>
21 #include <pims-ipc-data.h>
22
23 #include "contacts.h"
24 #include "ctsvc_internal.h"
25 #include "ctsvc_notify.h"
26 #include "ctsvc_ipc_define.h"
27 #include "ctsvc_ipc_marshal.h"
28 #include "ctsvc_client_ipc.h"
29 #include "ctsvc_mutex.h"
30
31 API int contacts_setting_get_name_display_order(contacts_name_display_order_e *name_display_order)
32 {
33         int ret = CONTACTS_ERROR_NONE;
34         pims_ipc_data_h outdata = NULL;
35
36         RETVM_IF(name_display_order == NULL, CONTACTS_ERROR_INVALID_PARAMETER,"The out param is NULL");
37         *name_display_order = 0;
38
39         if (ctsvc_ipc_call(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_DISPLAY_ORDER, NULL, &outdata) != 0) {
40                 CTS_ERR("ctsvc_ipc_call Fail");
41                 return CONTACTS_ERROR_IPC;
42         }
43
44         if (outdata) {
45                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
46                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
47                         pims_ipc_data_destroy(outdata);
48                         return CONTACTS_ERROR_IPC;
49                 }
50
51                 if (CONTACTS_ERROR_NONE == ret) {
52                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, (int *)name_display_order)) {
53                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
54                                 pims_ipc_data_destroy(outdata);
55                                 return CONTACTS_ERROR_IPC;
56                         }
57                 }
58                 pims_ipc_data_destroy(outdata);
59         }
60
61         return ret;
62 }
63
64 API int contacts_setting_get_name_sorting_order(contacts_name_sorting_order_e *name_sorting_order)
65 {
66         int ret = CONTACTS_ERROR_NONE;
67         pims_ipc_data_h outdata = NULL;
68
69         RETVM_IF(name_sorting_order == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "The out param is NULL");
70         *name_sorting_order = 0;
71
72         if (ctsvc_ipc_call(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_SORTING_ORDER, NULL, &outdata) != 0) {
73                 CTS_ERR("ctsvc_ipc_call Fail");
74                 return CONTACTS_ERROR_IPC;
75         }
76
77         if (outdata) {
78                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
79                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
80                         pims_ipc_data_destroy(outdata);
81                         return CONTACTS_ERROR_IPC;
82                 }
83                 if (CONTACTS_ERROR_NONE == ret) {
84                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, (int *)name_sorting_order)) {
85                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
86                                 pims_ipc_data_destroy(outdata);
87                                 return CONTACTS_ERROR_IPC;
88                         }
89                 }
90                 pims_ipc_data_destroy(outdata);
91         }
92
93         return ret;
94 }
95
96 API int contacts_setting_set_name_display_order(contacts_name_display_order_e name_display_order)
97 {
98         int ret = CONTACTS_ERROR_NONE;
99         pims_ipc_data_h indata = NULL;
100         pims_ipc_data_h outdata = NULL;
101
102         RETVM_IF(name_display_order != CONTACTS_NAME_DISPLAY_ORDER_FIRSTLAST
103                         && name_display_order != CONTACTS_NAME_DISPLAY_ORDER_LASTFIRST,
104                         CONTACTS_ERROR_INVALID_PARAMETER, "name display order is invalid : %d", name_display_order);
105
106         indata = pims_ipc_data_create(0);
107         if (indata == NULL) {
108                 CTS_ERR("ipc data created fail!");
109                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
110                 return ret;
111         }
112
113         ret = ctsvc_ipc_marshal_int(name_display_order, indata);
114         if (ret != CONTACTS_ERROR_NONE) {
115                 CTS_ERR("marshal fail");
116                 pims_ipc_data_destroy(indata);
117                 return ret;
118         }
119
120         if (ctsvc_ipc_call(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_DISPLAY_ORDER, indata, &outdata) != 0) {
121                 CTS_ERR("ctsvc_ipc_call Fail");
122                 pims_ipc_data_destroy(indata);
123                 return CONTACTS_ERROR_IPC;
124         }
125         pims_ipc_data_destroy(indata);
126
127         if (outdata) {
128                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
129                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
130                         pims_ipc_data_destroy(outdata);
131                         return CONTACTS_ERROR_IPC;
132                 }
133                 pims_ipc_data_destroy(outdata);
134         }
135
136         return ret;
137 }
138
139 API int contacts_setting_set_name_sorting_order(contacts_name_sorting_order_e name_sorint_order)
140 {
141         int ret = CONTACTS_ERROR_NONE;
142         pims_ipc_data_h indata = NULL;
143         pims_ipc_data_h outdata = NULL;
144
145         RETVM_IF(name_sorint_order != CONTACTS_NAME_SORTING_ORDER_FIRSTLAST
146                         && name_sorint_order != CONTACTS_NAME_SORTING_ORDER_LASTFIRST,
147                         CONTACTS_ERROR_INVALID_PARAMETER, "name sorting order is invalid : %d", name_sorint_order);
148
149         indata = pims_ipc_data_create(0);
150         if (indata == NULL) {
151                 CTS_ERR("ipc data created fail!");
152                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
153                 return ret;
154         }
155
156         ret = ctsvc_ipc_marshal_int(name_sorint_order, indata);
157         if (ret != CONTACTS_ERROR_NONE) {
158                 CTS_ERR("marshal fail");
159                 pims_ipc_data_destroy(indata);
160                 return ret;
161         }
162
163         if (ctsvc_ipc_call(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_SORTING_ORDER, indata, &outdata) != 0) {
164                 CTS_ERR("ctsvc_ipc_call Fail");
165                 pims_ipc_data_destroy(indata);
166                 return CONTACTS_ERROR_IPC;
167         }
168         pims_ipc_data_destroy(indata);
169
170         if (outdata) {
171                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
172                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
173                         pims_ipc_data_destroy(outdata);
174                         return CONTACTS_ERROR_IPC;
175                 }
176                 pims_ipc_data_destroy(outdata);
177         }
178
179         return ret;
180 }
181
182 typedef struct
183 {
184         contacts_setting_name_display_order_changed_cb cb;
185         void *user_data;
186 }ctsvc_name_display_order_changed_cb_info_s;
187
188 static GSList *__setting_name_display_order_subscribe_list = NULL;
189
190 static void __ctsvc_setting_name_display_order_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
191 {
192         int ret;
193         int value = -1;
194
195         if (data) {
196                 ret = ctsvc_ipc_unmarshal_int(data, &value);
197                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
198         }
199         if (__setting_name_display_order_subscribe_list) {
200                 GSList *l;
201                 for (l = __setting_name_display_order_subscribe_list;l;l=l->next) {
202                         ctsvc_name_display_order_changed_cb_info_s *cb_info = l->data;
203                         if (cb_info->cb)
204                                 cb_info->cb((contacts_name_display_order_e)value, cb_info->user_data);
205                 }
206         }
207 }
208
209 API int contacts_setting_add_name_display_order_changed_cb(
210         contacts_setting_name_display_order_changed_cb cb, void* user_data)
211 {
212         GSList *l;
213         int ret;
214         bool result = false;
215         ctsvc_name_display_order_changed_cb_info_s *cb_info;
216
217         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
218
219         ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
220         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
221         RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
222
223         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
224
225         if (NULL == __setting_name_display_order_subscribe_list) {
226                 if (pims_ipc_subscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
227                                         CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_SETTING_DISPLAY_ORDER_CHANGED,
228                                         __ctsvc_setting_name_display_order_subscriber_callback, NULL) != 0) {
229                         CTS_ERR("pims_ipc_subscribe error\n");
230                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
231                         return CONTACTS_ERROR_IPC;
232                 }
233         }
234
235         for (l = __setting_name_display_order_subscribe_list;l;l=l->next) {
236                 ctsvc_name_display_order_changed_cb_info_s *cb_info = l->data;
237                 if (cb_info->cb == cb && cb_info->user_data == user_data) {
238                         CTS_ERR("The same callback(%s) is already exist");
239                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
240                         return CONTACTS_ERROR_INVALID_PARAMETER;
241                 }
242         }
243
244         cb_info = calloc(1, sizeof(ctsvc_name_display_order_changed_cb_info_s));
245         if (NULL == cb_info) {
246                 CTS_ERR("calloc() Failed");
247                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
248                 return CONTACTS_ERROR_OUT_OF_MEMORY;
249         }
250         cb_info->user_data = user_data;
251         cb_info->cb = cb;
252         __setting_name_display_order_subscribe_list = g_slist_append(__setting_name_display_order_subscribe_list, cb_info);
253
254         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
255         return CONTACTS_ERROR_NONE;
256 }
257
258 API int contacts_setting_remove_name_display_order_changed_cb(
259         contacts_setting_name_display_order_changed_cb cb, void* user_data)
260 {
261         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
262
263         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
264
265         if (__setting_name_display_order_subscribe_list) {
266                 GSList *l;
267                 for (l = __setting_name_display_order_subscribe_list;l;l=l->next) {
268                         ctsvc_name_display_order_changed_cb_info_s *cb_info = l->data;
269                         if (cb == cb_info->cb && user_data == cb_info->user_data) {
270                                 __setting_name_display_order_subscribe_list = g_slist_remove(__setting_name_display_order_subscribe_list, cb_info);
271                                 free(cb_info);
272                                 break;
273                         }
274                 }
275                 if (g_slist_length(__setting_name_display_order_subscribe_list) == 0) {
276                         pims_ipc_unsubscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
277                                         CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_SETTING_DISPLAY_ORDER_CHANGED);
278                         g_slist_free(__setting_name_display_order_subscribe_list);
279                         __setting_name_display_order_subscribe_list = NULL;
280                 }
281         }
282
283         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
284         return CONTACTS_ERROR_NONE;
285 }
286
287 typedef struct
288 {
289         contacts_setting_name_sorting_order_changed_cb cb;
290         void *user_data;
291 }ctsvc_name_sorting_order_changed_cb_info_s;
292
293 static GSList *__setting_name_sorting_order_subscribe_list = NULL;
294
295 static void __ctsvc_setting_name_sorting_order_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
296 {
297         int ret;
298         int value = -1;
299
300         if (data) {
301                 ret = ctsvc_ipc_unmarshal_int(data, &value);
302                 WARN_IF(CONTACTS_ERROR_NONE != ret, "() Fail(%d)", ret);
303         }
304
305         if (__setting_name_sorting_order_subscribe_list) {
306                 GSList *l;
307                 for (l = __setting_name_sorting_order_subscribe_list;l;l=l->next) {
308                         ctsvc_name_sorting_order_changed_cb_info_s *cb_info = l->data;
309                         if (cb_info->cb)
310                                 cb_info->cb((contacts_name_sorting_order_e)value, cb_info->user_data);
311                 }
312         }
313 }
314
315 API int contacts_setting_add_name_sorting_order_changed_cb(
316         contacts_setting_name_sorting_order_changed_cb cb, void* user_data)
317 {
318         GSList *l;
319         int ret;
320         bool result = false;
321         ctsvc_name_sorting_order_changed_cb_info_s *cb_info;
322
323         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
324
325         ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
326         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
327         RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
328
329         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
330
331         if (NULL == __setting_name_sorting_order_subscribe_list) {
332                 if (pims_ipc_subscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
333                                         CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_SETTING_SORTING_ORDER_CHANGED,
334                                         __ctsvc_setting_name_sorting_order_subscriber_callback, NULL) != 0) {
335                         CTS_ERR("pims_ipc_subscribe error\n");
336                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
337                         return CONTACTS_ERROR_IPC;
338                 }
339         }
340
341         for (l = __setting_name_sorting_order_subscribe_list;l;l=l->next) {
342                 ctsvc_name_sorting_order_changed_cb_info_s *cb_info = l->data;
343                 if (cb_info->cb == cb && cb_info->user_data == user_data) {
344                         CTS_ERR("The same callback(%s) is already exist");
345                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
346                         return CONTACTS_ERROR_INVALID_PARAMETER;
347                 }
348         }
349
350         cb_info = calloc(1, sizeof(ctsvc_name_sorting_order_changed_cb_info_s));
351         if (NULL == cb_info) {
352                 CTS_ERR("calloc() Failed");
353                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
354                 return CONTACTS_ERROR_OUT_OF_MEMORY;
355         }
356         cb_info->user_data = user_data;
357         cb_info->cb = cb;
358         __setting_name_sorting_order_subscribe_list = g_slist_append(__setting_name_sorting_order_subscribe_list, cb_info);
359
360         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
361         return CONTACTS_ERROR_NONE;
362 }
363
364 API int contacts_setting_remove_name_sorting_order_changed_cb(
365         contacts_setting_name_sorting_order_changed_cb cb, void* user_data)
366 {
367         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
368
369         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
370
371         if (__setting_name_sorting_order_subscribe_list) {
372                 GSList *l;
373                 for (l = __setting_name_sorting_order_subscribe_list;l;l=l->next) {
374                         ctsvc_name_sorting_order_changed_cb_info_s *cb_info = l->data;
375                         if (cb == cb_info->cb && user_data == cb_info->user_data) {
376                                 __setting_name_sorting_order_subscribe_list = g_slist_remove(__setting_name_sorting_order_subscribe_list, cb_info);
377                                 free(cb_info);
378                                 break;
379                         }
380                 }
381                 if (g_slist_length(__setting_name_sorting_order_subscribe_list) == 0) {
382                         pims_ipc_unsubscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
383                                         CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_SETTING_SORTING_ORDER_CHANGED);
384                         g_slist_free(__setting_name_sorting_order_subscribe_list);
385                         __setting_name_sorting_order_subscribe_list = NULL;
386                 }
387         }
388
389         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
390         return CONTACTS_ERROR_NONE;
391 }
392