bd9712130026b5fd51580243cc7c6a785a435187
[platform/core/pim/contacts-service.git] / client / ctsvc_client_group_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 "contacts.h"
21 #include "ctsvc_internal.h"
22 #include "ctsvc_ipc_define.h"
23 #include "ctsvc_client_ipc.h"
24 #include <pims-ipc-data.h>
25 #include "ctsvc_ipc_marshal.h"
26
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_group_add_contact(contacts_h contact, int group_id, int contact_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(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
42
43         /* make indata */
44         indata = pims_ipc_data_create(0);
45         if (indata == NULL) {
46                 /* LCOV_EXCL_START */
47                 ERR("pims_ipc_data_create() Fail");
48                 return CONTACTS_ERROR_OUT_OF_MEMORY;
49                 /* LCOV_EXCL_STOP */
50         }
51
52         ret = ctsvc_ipc_marshal_handle(contact, indata);
53         if (CONTACTS_ERROR_NONE != ret) {
54                 /* LCOV_EXCL_START */
55                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
56                 pims_ipc_data_destroy(indata);
57                 return ret;
58                 /* LCOV_EXCL_STOP */
59         }
60
61         ret = ctsvc_ipc_marshal_int(group_id, indata);
62         if (ret != CONTACTS_ERROR_NONE) {
63                 /* LCOV_EXCL_START */
64                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
65                 pims_ipc_data_destroy(indata);
66                 return ret;
67                 /* LCOV_EXCL_STOP */
68         }
69         ret = ctsvc_ipc_marshal_int(contact_id, indata);
70         if (ret != CONTACTS_ERROR_NONE) {
71                 /* LCOV_EXCL_START */
72                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
73                 pims_ipc_data_destroy(indata);
74                 return ret;
75                 /* LCOV_EXCL_STOP */
76         }
77
78         /* ipc call */
79         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_ADD_CONTACT, indata, &outdata) != 0) {
80                 /* LCOV_EXCL_START */
81                 ERR("ctsvc_ipc_call() Fail");
82                 pims_ipc_data_destroy(indata);
83                 return CONTACTS_ERROR_IPC;
84                 /* LCOV_EXCL_STOP */
85         }
86
87         pims_ipc_data_destroy(indata);
88
89         if (outdata) {
90                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
91                         /* LCOV_EXCL_START */
92                         ERR("ctsvc_ipc_unmarshal_int() Fail");
93                         pims_ipc_data_destroy(outdata);
94                         return CONTACTS_ERROR_IPC;
95                         /* LCOV_EXCL_STOP */
96                 }
97
98                 if (CONTACTS_ERROR_NONE == ret) {
99                         int transaction_ver = 0;
100                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
101                                 /* LCOV_EXCL_START */
102                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
103                                 pims_ipc_data_destroy(outdata);
104                                 return CONTACTS_ERROR_IPC;
105                                 /* LCOV_EXCL_STOP */
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_group_remove_contact(contacts_h contact, int group_id, int contact_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         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
124         RETVM_IF(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
125
126         /* make indata */
127         indata = pims_ipc_data_create(0);
128         if (indata == NULL) {
129                 /* LCOV_EXCL_START */
130                 ERR("pims_ipc_data_create() Fail");
131                 return CONTACTS_ERROR_OUT_OF_MEMORY;
132                 /* LCOV_EXCL_STOP */
133         }
134
135         ret = ctsvc_ipc_marshal_handle(contact, indata);
136         if (CONTACTS_ERROR_NONE != ret) {
137                 /* LCOV_EXCL_START */
138                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
139                 pims_ipc_data_destroy(indata);
140                 return ret;
141                 /* LCOV_EXCL_STOP */
142         }
143
144
145         ret = ctsvc_ipc_marshal_int(group_id, indata);
146         if (ret != CONTACTS_ERROR_NONE) {
147                 /* LCOV_EXCL_START */
148                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
149                 pims_ipc_data_destroy(indata);
150                 return ret;
151                 /* LCOV_EXCL_STOP */
152         }
153         ret = ctsvc_ipc_marshal_int(contact_id, indata);
154         if (ret != CONTACTS_ERROR_NONE) {
155                 /* LCOV_EXCL_START */
156                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
157                 pims_ipc_data_destroy(indata);
158                 return ret;
159                 /* LCOV_EXCL_STOP */
160         }
161
162         /* ipc call */
163         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, indata, &outdata) != 0) {
164                 /* LCOV_EXCL_START */
165                 ERR("ctsvc_ipc_call() Fail");
166                 pims_ipc_data_destroy(indata);
167                 return CONTACTS_ERROR_IPC;
168                 /* LCOV_EXCL_STOP */
169         }
170
171         pims_ipc_data_destroy(indata);
172
173         if (outdata) {
174                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
175                         /* LCOV_EXCL_START */
176                         ERR("ctsvc_ipc_unmarshal_int() Fail");
177                         pims_ipc_data_destroy(outdata);
178                         return CONTACTS_ERROR_IPC;
179                         /* LCOV_EXCL_STOP */
180                 }
181
182                 if (CONTACTS_ERROR_NONE == ret) {
183                         int transaction_ver = 0;
184                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
185                                 /* LCOV_EXCL_START */
186                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
187                                 pims_ipc_data_destroy(outdata);
188                                 return CONTACTS_ERROR_IPC;
189                                 /* LCOV_EXCL_STOP */
190                         }
191                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
192                 }
193
194                 pims_ipc_data_destroy(outdata);
195         }
196
197         return ret;
198 }
199
200 int ctsvc_client_group_set_group_order(contacts_h contact, int group_id, int previous_group_id, int next_group_id)
201 {
202         int ret = CONTACTS_ERROR_NONE;
203
204         pims_ipc_data_h indata = NULL;
205         pims_ipc_data_h outdata = NULL;
206
207         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
208         RETVM_IF(group_id <= 0 || previous_group_id < 0 || next_group_id < 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
209
210         /* make indata */
211         indata = pims_ipc_data_create(0);
212         if (indata == NULL) {
213                 /* LCOV_EXCL_START */
214                 ERR("pims_ipc_data_create() Fail");
215                 return CONTACTS_ERROR_OUT_OF_MEMORY;
216                 /* LCOV_EXCL_STOP */
217         }
218
219         ret = ctsvc_ipc_marshal_handle(contact, indata);
220         if (CONTACTS_ERROR_NONE != ret) {
221                 /* LCOV_EXCL_START */
222                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
223                 pims_ipc_data_destroy(indata);
224                 return ret;
225                 /* LCOV_EXCL_STOP */
226         }
227
228         ret = ctsvc_ipc_marshal_int(group_id, indata);
229         if (ret != CONTACTS_ERROR_NONE) {
230                 /* LCOV_EXCL_START */
231                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
232                 pims_ipc_data_destroy(indata);
233                 return ret;
234                 /* LCOV_EXCL_STOP */
235         }
236         ret = ctsvc_ipc_marshal_int(previous_group_id, indata);
237         if (ret != CONTACTS_ERROR_NONE) {
238                 /* LCOV_EXCL_START */
239                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
240                 pims_ipc_data_destroy(indata);
241                 return ret;
242                 /* LCOV_EXCL_STOP */
243         }
244         ret = ctsvc_ipc_marshal_int(next_group_id, indata);
245         if (ret != CONTACTS_ERROR_NONE) {
246                 /* LCOV_EXCL_START */
247                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
248                 pims_ipc_data_destroy(indata);
249                 return ret;
250                 /* LCOV_EXCL_STOP */
251         }
252
253         /* ipc call */
254         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, indata, &outdata) != 0) {
255                 /* LCOV_EXCL_START */
256                 ERR("ctsvc_ipc_call() Fail");
257                 pims_ipc_data_destroy(indata);
258                 return CONTACTS_ERROR_IPC;
259                 /* LCOV_EXCL_STOP */
260         }
261
262         pims_ipc_data_destroy(indata);
263
264         if (outdata) {
265                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
266                         /* LCOV_EXCL_START */
267                         ERR("ctsvc_ipc_unmarshal_int() Fail");
268                         pims_ipc_data_destroy(outdata);
269                         return CONTACTS_ERROR_IPC;
270                         /* LCOV_EXCL_STOP */
271                 }
272
273                 if (CONTACTS_ERROR_NONE == ret) {
274                         int transaction_ver = 0;
275                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
276                                 /* LCOV_EXCL_START */
277                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
278                                 pims_ipc_data_destroy(outdata);
279                                 return CONTACTS_ERROR_IPC;
280                                 /* LCOV_EXCL_STOP */
281                         }
282                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
283                 }
284
285                 pims_ipc_data_destroy(outdata);
286         }
287
288         return ret;
289
290 }
291