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