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