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