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