4 * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <pims-ipc-data.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"
33 contacts_setting_name_display_order_changed_cb cb;
35 }ctsvc_name_display_order_changed_cb_info_s;
39 contacts_setting_name_sorting_order_changed_cb cb;
41 }ctsvc_name_sorting_order_changed_cb_info_s;
43 static GSList *__setting_name_display_order_subscribe_list = NULL;
44 static GSList *__setting_name_sorting_order_subscribe_list = NULL;
46 API int contacts_setting_get_name_display_order(contacts_name_display_order_e *name_display_order)
48 int ret = CONTACTS_ERROR_NONE;
49 pims_ipc_data_h outdata = NULL;
51 RETVM_IF(name_display_order == NULL, CONTACTS_ERROR_INVALID_PARAMETER,"The out param is NULL");
52 *name_display_order = 0;
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;
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;
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;
73 pims_ipc_data_destroy(outdata);
79 API int contacts_setting_get_name_sorting_order(contacts_name_sorting_order_e *name_sorting_order)
81 int ret = CONTACTS_ERROR_NONE;
82 pims_ipc_data_h outdata = NULL;
84 RETVM_IF(name_sorting_order == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "The out param is NULL");
85 *name_sorting_order = 0;
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;
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;
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;
105 pims_ipc_data_destroy(outdata);
111 API int contacts_setting_set_name_display_order(contacts_name_display_order_e name_display_order)
113 int ret = CONTACTS_ERROR_NONE;
114 pims_ipc_data_h indata = NULL;
115 pims_ipc_data_h outdata = NULL;
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);
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;
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);
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;
140 pims_ipc_data_destroy(indata);
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;
148 pims_ipc_data_destroy(outdata);
154 API int contacts_setting_set_name_sorting_order(contacts_name_sorting_order_e name_sorint_order)
156 int ret = CONTACTS_ERROR_NONE;
157 pims_ipc_data_h indata = NULL;
158 pims_ipc_data_h outdata = NULL;
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);
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;
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);
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;
183 pims_ipc_data_destroy(indata);
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;
191 pims_ipc_data_destroy(outdata);
197 static void __ctsvc_setting_name_display_order_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
203 ret = ctsvc_ipc_unmarshal_int(data, &value);
204 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
206 if (__setting_name_display_order_subscribe_list) {
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;
211 cb_info->cb((contacts_name_display_order_e)value, cb_info->user_data);
216 static void __ctsvc_setting_name_sorting_order_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
222 ret = ctsvc_ipc_unmarshal_int(data, &value);
223 WARN_IF(CONTACTS_ERROR_NONE != ret, "() Fail(%d)", ret);
226 if (__setting_name_sorting_order_subscribe_list) {
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;
231 cb_info->cb((contacts_name_sorting_order_e)value, cb_info->user_data);
236 int ctsvc_setting_recover_for_change_subscription()
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;
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;
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;
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;
266 return CONTACTS_ERROR_NONE;
269 API int contacts_setting_add_name_display_order_changed_cb(
270 contacts_setting_name_display_order_changed_cb cb, void* user_data)
275 ctsvc_name_display_order_changed_cb_info_s *cb_info;
277 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
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)");
283 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
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;
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;
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;
310 cb_info->user_data = user_data;
312 __setting_name_display_order_subscribe_list = g_slist_append(__setting_name_display_order_subscribe_list, cb_info);
314 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
315 return CONTACTS_ERROR_NONE;
318 API int contacts_setting_remove_name_display_order_changed_cb(
319 contacts_setting_name_display_order_changed_cb cb, void* user_data)
321 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
323 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
325 if (__setting_name_display_order_subscribe_list) {
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);
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;
343 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
344 return CONTACTS_ERROR_NONE;
347 API int contacts_setting_add_name_sorting_order_changed_cb(
348 contacts_setting_name_sorting_order_changed_cb cb, void* user_data)
353 ctsvc_name_sorting_order_changed_cb_info_s *cb_info;
355 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
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)");
361 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
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;
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;
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;
388 cb_info->user_data = user_data;
390 __setting_name_sorting_order_subscribe_list = g_slist_append(__setting_name_sorting_order_subscribe_list, cb_info);
392 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
393 return CONTACTS_ERROR_NONE;
396 API int contacts_setting_remove_name_sorting_order_changed_cb(
397 contacts_setting_name_sorting_order_changed_cb cb, void* user_data)
399 RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "Invalid parameter : callback is NULL");
401 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
403 if (__setting_name_sorting_order_subscribe_list) {
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);
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;
421 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
422 return CONTACTS_ERROR_NONE;