ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION during disconnect_on_thread
[platform/core/pim/contacts-service.git] / client / ctsvc_client_person_helper.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 <pims-ipc-data.h>
21
22 #include "contacts.h"
23 #include "ctsvc_internal.h"
24 #include "ctsvc_ipc_define.h"
25 #include "ctsvc_client_ipc.h"
26 #include "ctsvc_ipc_marshal.h"
27
28 static const char CONTACTS_READ_PRIVILEGE_ID[] =  "http://tizen.org/privilege/contact.read";
29 static const char CONTACTS_WRITE_PRIVILEGE_ID[] =  "http://tizen.org/privilege/contact.write";
30 static const char PHONELOG_READ_PRIVILEGE_ID[] =  "http://tizen.org/privilege/callhistory.read";
31 static const char PHONELOG_WRITE_PRIVILEGE_ID[] =  "http://tizen.org/privilege/callhistory.write";
32
33 int ctsvc_client_person_link_person(contacts_h contact, int base_person_id, int person_id)
34 {
35         int ret = CONTACTS_ERROR_NONE;
36
37         pims_ipc_data_h indata = NULL;
38         pims_ipc_data_h outdata = NULL;
39
40         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
41         RETVM_IF(base_person_id <= 0 || person_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
42
43
44         /* make indata */
45         indata = pims_ipc_data_create(0);
46         if (indata == NULL) {
47                 CTS_ERR("ipc data created fail!");
48                 return CONTACTS_ERROR_OUT_OF_MEMORY;
49         }
50         ret = ctsvc_ipc_marshal_handle(contact, indata);
51         if (ret != CONTACTS_ERROR_NONE) {
52                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
53                 pims_ipc_data_destroy(indata);
54                 return ret;
55         }
56
57         bool success = false;
58         do {
59                 if (ctsvc_ipc_marshal_int(base_person_id, indata) != CONTACTS_ERROR_NONE) break;
60                 if (ctsvc_ipc_marshal_int(person_id, indata) != CONTACTS_ERROR_NONE) break;
61
62                 success = true;
63         } while (0);
64
65         if (success == false) {
66                 CTS_ERR("marshal fail");
67                 pims_ipc_data_destroy(indata);
68                 return CONTACTS_ERROR_IPC;
69         }
70
71 /*
72         ret = ctsvc_ipc_marshal_int(base_person_id, indata);
73         if (ret != CONTACTS_ERROR_NONE) {
74                 CTS_ERR("marshal fail");
75                 return ret;
76         }
77         ret = ctsvc_ipc_marshal_int(person_id, indata);
78         if (ret != CONTACTS_ERROR_NONE) {
79                 CTS_ERR("marshal fail");
80                 return ret;
81         }
82 */
83
84         /* ipc call */
85         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_LINK_PERSON, indata, &outdata) != 0) {
86                 CTS_ERR("ctsvc_ipc_call failed");
87                 pims_ipc_data_destroy(indata);
88                 return CONTACTS_ERROR_IPC;
89         }
90
91         pims_ipc_data_destroy(indata);
92
93         if (outdata) {
94                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
95                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
96                         pims_ipc_data_destroy(outdata);
97                         return CONTACTS_ERROR_IPC;
98                 }
99
100                 if (CONTACTS_ERROR_NONE == ret) {
101                         int transaction_ver = 0;
102                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
103                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
104                                 pims_ipc_data_destroy(outdata);
105                                 return CONTACTS_ERROR_IPC;
106                         }
107                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
108                 }
109
110                 pims_ipc_data_destroy(outdata);
111         }
112
113         return ret;
114 }
115
116 int ctsvc_client_person_unlink_contact(contacts_h contact, int person_id, int contact_id, int* unlinked_person_id)
117 {
118         int ret = CONTACTS_ERROR_NONE;
119
120         pims_ipc_data_h indata = NULL;
121         pims_ipc_data_h outdata = NULL;
122
123         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
124         RETVM_IF(person_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
125
126         if (unlinked_person_id)
127                 *unlinked_person_id = 0;
128
129         /* make indata */
130         indata = pims_ipc_data_create(0);
131         if (indata == NULL) {
132                 CTS_ERR("ipc data created fail!");
133                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
134                 return ret;
135         }
136         ret = ctsvc_ipc_marshal_handle(contact, indata);
137         if (ret != CONTACTS_ERROR_NONE) {
138                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
139                 pims_ipc_data_destroy(indata);
140                 return ret;
141         }
142
143         ret = ctsvc_ipc_marshal_int(person_id, indata);
144         if (ret != CONTACTS_ERROR_NONE) {
145                 CTS_ERR("marshal fail");
146                 pims_ipc_data_destroy(indata);
147                 return ret;
148         }
149         ret = ctsvc_ipc_marshal_int(contact_id, indata);
150         if (ret != CONTACTS_ERROR_NONE) {
151                 CTS_ERR("marshal fail");
152                 pims_ipc_data_destroy(indata);
153                 return ret;
154         }
155
156         /* ipc call */
157         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_UNLINK_CONTACT, indata, &outdata) != 0) {
158                 CTS_ERR("ctsvc_ipc_call failed");
159                 pims_ipc_data_destroy(indata);
160                 return CONTACTS_ERROR_IPC;
161         }
162
163         pims_ipc_data_destroy(indata);
164
165         if (outdata) {
166                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
167                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
168                         pims_ipc_data_destroy(outdata);
169                         return CONTACTS_ERROR_IPC;
170                 }
171
172                 if (CONTACTS_ERROR_NONE == ret) {
173                         int transaction_ver = 0;
174                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
175                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
176                                 pims_ipc_data_destroy(outdata);
177                                 return CONTACTS_ERROR_IPC;
178                         }
179                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
180
181                         if (unlinked_person_id) {
182                                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, unlinked_person_id)) {
183                                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
184                                         pims_ipc_data_destroy(outdata);
185                                         return CONTACTS_ERROR_IPC;
186                                 }
187                         }
188                 }
189                 pims_ipc_data_destroy(outdata);
190         }
191
192         return ret;
193 }
194
195 int ctsvc_client_person_reset_usage(contacts_h contact, int person_id, contacts_usage_type_e type)
196 {
197         int ret = CONTACTS_ERROR_NONE;
198
199         pims_ipc_data_h indata = NULL;
200         pims_ipc_data_h outdata = NULL;
201
202         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
203         RETVM_IF(person_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"contact_id should be greater than 0");
204
205         /* make indata */
206         indata = pims_ipc_data_create(0);
207         if (indata == NULL) {
208                 CTS_ERR("ipc data created fail!");
209                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
210                 return ret;
211         }
212         ret = ctsvc_ipc_marshal_handle(contact, indata);
213         if (ret != CONTACTS_ERROR_NONE) {
214                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
215                 pims_ipc_data_destroy(indata);
216                 return ret;
217         }
218
219
220         ret = ctsvc_ipc_marshal_int(person_id, indata);
221         if (ret != CONTACTS_ERROR_NONE) {
222                 CTS_ERR("marshal fail");
223                 pims_ipc_data_destroy(indata);
224                 return ret;
225         }
226         ret = ctsvc_ipc_marshal_int((int)type, indata);
227         if (ret != CONTACTS_ERROR_NONE) {
228                 CTS_ERR("marshal fail");
229                 pims_ipc_data_destroy(indata);
230                 return ret;
231         }
232
233         /* ipc call */
234         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_RESET_USAGE, indata, &outdata) != 0) {
235                 CTS_ERR("ctsvc_ipc_call failed");
236                 pims_ipc_data_destroy(indata);
237                 return CONTACTS_ERROR_IPC;
238         }
239
240         pims_ipc_data_destroy(indata);
241
242         if (outdata) {
243                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
244                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
245                         pims_ipc_data_destroy(outdata);
246                         return CONTACTS_ERROR_IPC;
247                 }
248
249                 if (CONTACTS_ERROR_NONE == ret) {
250                         int transaction_ver = 0;
251                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
252                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
253                                 pims_ipc_data_destroy(outdata);
254                                 return CONTACTS_ERROR_IPC;
255                         }
256                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
257                 }
258
259                 pims_ipc_data_destroy(outdata);
260         }
261
262         return ret;
263 }
264
265 int ctsvc_client_person_set_favorite_order(contacts_h contact, int person_id, int previous_person_id, int next_person_id)
266 {
267         int ret = CONTACTS_ERROR_NONE;
268
269         pims_ipc_data_h indata = NULL;
270         pims_ipc_data_h outdata = NULL;
271
272         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
273         RETVM_IF(person_id <= 0 || previous_person_id < 0 || next_person_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
274
275         /* make indata */
276         indata = pims_ipc_data_create(0);
277         if (indata == NULL) {
278                 CTS_ERR("ipc data created fail!");
279                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
280                 return ret;
281         }
282         ret = ctsvc_ipc_marshal_handle(contact, indata);
283         if (ret != CONTACTS_ERROR_NONE) {
284                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
285                 pims_ipc_data_destroy(indata);
286                 return ret;
287         }
288
289         ret = ctsvc_ipc_marshal_int(person_id, indata);
290         if (ret != CONTACTS_ERROR_NONE) {
291                 CTS_ERR("marshal fail");
292                 pims_ipc_data_destroy(indata);
293                 return ret;
294         }
295         ret = ctsvc_ipc_marshal_int(previous_person_id, indata);
296         if (ret != CONTACTS_ERROR_NONE) {
297                 CTS_ERR("marshal fail");
298                 pims_ipc_data_destroy(indata);
299                 return ret;
300         }
301         ret = ctsvc_ipc_marshal_int(next_person_id, indata);
302         if (ret != CONTACTS_ERROR_NONE) {
303                 CTS_ERR("marshal fail");
304                 pims_ipc_data_destroy(indata);
305                 return ret;
306         }
307
308         /* ipc call */
309         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_FAVORITE_ORDER, indata, &outdata) != 0) {
310                 CTS_ERR("ctsvc_ipc_call failed");
311                 pims_ipc_data_destroy(indata);
312                 return CONTACTS_ERROR_IPC;
313         }
314
315         pims_ipc_data_destroy(indata);
316
317         if (outdata) {
318                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
319                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
320                         pims_ipc_data_destroy(outdata);
321                         return CONTACTS_ERROR_IPC;
322                 }
323
324                 if (CONTACTS_ERROR_NONE == ret) {
325                         int transaction_ver = 0;
326                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
327                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
328                                 pims_ipc_data_destroy(outdata);
329                                 return CONTACTS_ERROR_IPC;
330                         }
331                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
332                 }
333
334                 pims_ipc_data_destroy(outdata);
335         }
336
337         return ret;
338
339 }
340
341 int ctsvc_client_person_set_default_property(contacts_h contact, contacts_person_property_e property,
342                 int person_id, int id)
343 {
344         int ret = CONTACTS_ERROR_NONE;
345
346         pims_ipc_data_h indata = NULL;
347         pims_ipc_data_h outdata = NULL;
348
349         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
350         RETVM_IF(person_id <= 0 || id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
351
352         /* make indata */
353         indata = pims_ipc_data_create(0);
354         if (indata == NULL) {
355                 CTS_ERR("ipc data created fail!");
356                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
357                 return ret;
358         }
359
360         ret = ctsvc_ipc_marshal_handle(contact, indata);
361         if (ret != CONTACTS_ERROR_NONE) {
362                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
363                 pims_ipc_data_destroy(indata);
364                 return ret;
365         }
366
367         ret = ctsvc_ipc_marshal_int(person_id, indata);
368         if (ret != CONTACTS_ERROR_NONE) {
369                 CTS_ERR("marshal fail");
370                 pims_ipc_data_destroy(indata);
371                 return ret;
372         }
373         ret = ctsvc_ipc_marshal_unsigned_int(property, indata);
374         if (ret != CONTACTS_ERROR_NONE) {
375                 CTS_ERR("marshal fail");
376                 pims_ipc_data_destroy(indata);
377                 return ret;
378         }
379         ret = ctsvc_ipc_marshal_int(id, indata);
380         if (ret != CONTACTS_ERROR_NONE) {
381                 CTS_ERR("marshal fail");
382                 pims_ipc_data_destroy(indata);
383                 return ret;
384         }
385
386         /* ipc call */
387         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_DEFAULT_PROPERTY, indata, &outdata) != 0) {
388                 CTS_ERR("ctsvc_ipc_call failed");
389                 pims_ipc_data_destroy(indata);
390                 return CONTACTS_ERROR_IPC;
391         }
392
393         pims_ipc_data_destroy(indata);
394
395         if (outdata) {
396                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
397                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
398                         pims_ipc_data_destroy(outdata);
399                         return CONTACTS_ERROR_IPC;
400                 }
401                 if (CONTACTS_ERROR_NONE == ret) {
402                         int transaction_ver = 0;
403                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
404                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
405                                 pims_ipc_data_destroy(outdata);
406                                 return CONTACTS_ERROR_IPC;
407                         }
408                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
409                 }
410
411                 pims_ipc_data_destroy(outdata);
412         }
413
414         return ret;
415 }
416
417 int ctsvc_client_person_get_default_property(contacts_h contact, contacts_person_property_e property,
418                 int person_id, int *id)
419 {
420         int ret = CONTACTS_ERROR_NONE;
421         pims_ipc_data_h indata = NULL;
422         pims_ipc_data_h outdata = NULL;
423
424         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
425         RETVM_IF(person_id <= 0 || id == NULL, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
426         *id = 0;
427
428         /* make indata */
429         indata = pims_ipc_data_create(0);
430         if (indata == NULL) {
431                 CTS_ERR("ipc data created fail!");
432                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
433                 return ret;
434         }
435
436         ret = ctsvc_ipc_marshal_handle(contact, indata);
437         if (ret != CONTACTS_ERROR_NONE) {
438                 CTS_ERR("ctsvc_ipc_marshal_handle Fail(%d)", ret);
439                 pims_ipc_data_destroy(indata);
440                 return ret;
441         }
442
443         ret = ctsvc_ipc_marshal_int(person_id, indata);
444         if (ret != CONTACTS_ERROR_NONE) {
445                 CTS_ERR("marshal fail");
446                 pims_ipc_data_destroy(indata);
447                 return ret;
448         }
449
450         ret = ctsvc_ipc_marshal_unsigned_int(property, indata);
451         if (ret != CONTACTS_ERROR_NONE) {
452                 CTS_ERR("marshal fail");
453                 pims_ipc_data_destroy(indata);
454                 return ret;
455         }
456
457         /* ipc call */
458         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_GET_DEFAULT_PROPERTY,
459                                 indata, &outdata) != 0) {
460                 CTS_ERR("ctsvc_ipc_call failed");
461                 pims_ipc_data_destroy(indata);
462                 return CONTACTS_ERROR_IPC;
463         }
464
465         pims_ipc_data_destroy(indata);
466
467         if (outdata) {
468                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
469                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
470                         pims_ipc_data_destroy(outdata);
471                         return CONTACTS_ERROR_IPC;
472                 }
473                 if (ret == CONTACTS_ERROR_NONE && id) {
474                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, id)) {
475                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
476                                 pims_ipc_data_destroy(outdata);
477                                 return CONTACTS_ERROR_IPC;
478                         }
479                 }
480                 pims_ipc_data_destroy(outdata);
481         }
482
483         return ret;
484 }
485