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