Add db integrity check
[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 int ctsvc_client_group_add_contact(contacts_h contact, int group_id, int contact_id)
29 {
30         int ret = CONTACTS_ERROR_NONE;
31
32         pims_ipc_data_h indata = NULL;
33         pims_ipc_data_h outdata = NULL;
34
35         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
36         RETVM_IF(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
37
38         /* make indata */
39         indata = pims_ipc_data_create(0);
40         if (indata == NULL) {
41                 /* LCOV_EXCL_START */
42                 ERR("pims_ipc_data_create() Fail");
43                 return CONTACTS_ERROR_OUT_OF_MEMORY;
44                 /* LCOV_EXCL_STOP */
45         }
46
47         ret = ctsvc_ipc_marshal_handle(contact, indata);
48         if (CONTACTS_ERROR_NONE != ret) {
49                 /* LCOV_EXCL_START */
50                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
51                 pims_ipc_data_destroy(indata);
52                 return ret;
53                 /* LCOV_EXCL_STOP */
54         }
55
56         ret = ctsvc_ipc_marshal_int(group_id, indata);
57         if (ret != CONTACTS_ERROR_NONE) {
58                 /* LCOV_EXCL_START */
59                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
60                 pims_ipc_data_destroy(indata);
61                 return ret;
62                 /* LCOV_EXCL_STOP */
63         }
64         ret = ctsvc_ipc_marshal_int(contact_id, indata);
65         if (ret != CONTACTS_ERROR_NONE) {
66                 /* LCOV_EXCL_START */
67                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
68                 pims_ipc_data_destroy(indata);
69                 return ret;
70                 /* LCOV_EXCL_STOP */
71         }
72
73         /* ipc call */
74         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_ADD_CONTACT, indata, &outdata) != 0) {
75                 /* LCOV_EXCL_START */
76                 ERR("ctsvc_ipc_call() Fail");
77                 pims_ipc_data_destroy(indata);
78                 return CONTACTS_ERROR_IPC;
79                 /* LCOV_EXCL_STOP */
80         }
81
82         pims_ipc_data_destroy(indata);
83
84         if (outdata) {
85                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
86                         /* LCOV_EXCL_START */
87                         ERR("ctsvc_ipc_unmarshal_int() Fail");
88                         pims_ipc_data_destroy(outdata);
89                         return CONTACTS_ERROR_IPC;
90                         /* LCOV_EXCL_STOP */
91                 }
92
93                 if (CONTACTS_ERROR_NONE == ret) {
94                         int transaction_ver = 0;
95                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
96                                 /* LCOV_EXCL_START */
97                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
98                                 pims_ipc_data_destroy(outdata);
99                                 return CONTACTS_ERROR_IPC;
100                                 /* LCOV_EXCL_STOP */
101                         }
102                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
103                 }
104
105                 pims_ipc_data_destroy(outdata);
106         }
107
108         return ret;
109 }
110
111 int ctsvc_client_group_remove_contact(contacts_h contact, int group_id, int contact_id)
112 {
113         int ret = CONTACTS_ERROR_NONE;
114
115         pims_ipc_data_h indata = NULL;
116         pims_ipc_data_h outdata = NULL;
117
118         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
119         RETVM_IF(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
120
121         /* make indata */
122         indata = pims_ipc_data_create(0);
123         if (indata == NULL) {
124                 /* LCOV_EXCL_START */
125                 ERR("pims_ipc_data_create() Fail");
126                 return CONTACTS_ERROR_OUT_OF_MEMORY;
127                 /* LCOV_EXCL_STOP */
128         }
129
130         ret = ctsvc_ipc_marshal_handle(contact, indata);
131         if (CONTACTS_ERROR_NONE != ret) {
132                 /* LCOV_EXCL_START */
133                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
134                 pims_ipc_data_destroy(indata);
135                 return ret;
136                 /* LCOV_EXCL_STOP */
137         }
138
139
140         ret = ctsvc_ipc_marshal_int(group_id, indata);
141         if (ret != CONTACTS_ERROR_NONE) {
142                 /* LCOV_EXCL_START */
143                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
144                 pims_ipc_data_destroy(indata);
145                 return ret;
146                 /* LCOV_EXCL_STOP */
147         }
148         ret = ctsvc_ipc_marshal_int(contact_id, indata);
149         if (ret != CONTACTS_ERROR_NONE) {
150                 /* LCOV_EXCL_START */
151                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
152                 pims_ipc_data_destroy(indata);
153                 return ret;
154                 /* LCOV_EXCL_STOP */
155         }
156
157         /* ipc call */
158         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, indata, &outdata) != 0) {
159                 /* LCOV_EXCL_START */
160                 ERR("ctsvc_ipc_call() Fail");
161                 pims_ipc_data_destroy(indata);
162                 return CONTACTS_ERROR_IPC;
163                 /* LCOV_EXCL_STOP */
164         }
165
166         pims_ipc_data_destroy(indata);
167
168         if (outdata) {
169                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
170                         /* LCOV_EXCL_START */
171                         ERR("ctsvc_ipc_unmarshal_int() Fail");
172                         pims_ipc_data_destroy(outdata);
173                         return CONTACTS_ERROR_IPC;
174                         /* LCOV_EXCL_STOP */
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                                 /* LCOV_EXCL_START */
181                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
182                                 pims_ipc_data_destroy(outdata);
183                                 return CONTACTS_ERROR_IPC;
184                                 /* LCOV_EXCL_STOP */
185                         }
186                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
187                 }
188
189                 pims_ipc_data_destroy(outdata);
190         }
191
192         return ret;
193 }
194
195 int ctsvc_client_group_set_group_order(contacts_h contact, int group_id, int previous_group_id, int next_group_id)
196 {
197         int ret = CONTACTS_ERROR_NONE;
198
199         pims_ipc_data_h indata = NULL;
200         pims_ipc_data_h outdata = NULL;
201
202         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
203         RETVM_IF(group_id <= 0 || previous_group_id < 0 || next_group_id < 0, CONTACTS_ERROR_INVALID_PARAMETER, "id should be greater than 0");
204
205         /* make indata */
206         indata = pims_ipc_data_create(0);
207         if (indata == NULL) {
208                 /* LCOV_EXCL_START */
209                 ERR("pims_ipc_data_create() Fail");
210                 return CONTACTS_ERROR_OUT_OF_MEMORY;
211                 /* LCOV_EXCL_STOP */
212         }
213
214         ret = ctsvc_ipc_marshal_handle(contact, indata);
215         if (CONTACTS_ERROR_NONE != ret) {
216                 /* LCOV_EXCL_START */
217                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
218                 pims_ipc_data_destroy(indata);
219                 return ret;
220                 /* LCOV_EXCL_STOP */
221         }
222
223         ret = ctsvc_ipc_marshal_int(group_id, indata);
224         if (ret != CONTACTS_ERROR_NONE) {
225                 /* LCOV_EXCL_START */
226                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
227                 pims_ipc_data_destroy(indata);
228                 return ret;
229                 /* LCOV_EXCL_STOP */
230         }
231         ret = ctsvc_ipc_marshal_int(previous_group_id, indata);
232         if (ret != CONTACTS_ERROR_NONE) {
233                 /* LCOV_EXCL_START */
234                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
235                 pims_ipc_data_destroy(indata);
236                 return ret;
237                 /* LCOV_EXCL_STOP */
238         }
239         ret = ctsvc_ipc_marshal_int(next_group_id, indata);
240         if (ret != CONTACTS_ERROR_NONE) {
241                 /* LCOV_EXCL_START */
242                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
243                 pims_ipc_data_destroy(indata);
244                 return ret;
245                 /* LCOV_EXCL_STOP */
246         }
247
248         /* ipc call */
249         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, indata, &outdata) != 0) {
250                 /* LCOV_EXCL_START */
251                 ERR("ctsvc_ipc_call() Fail");
252                 pims_ipc_data_destroy(indata);
253                 return CONTACTS_ERROR_IPC;
254                 /* LCOV_EXCL_STOP */
255         }
256
257         pims_ipc_data_destroy(indata);
258
259         if (outdata) {
260                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
261                         /* LCOV_EXCL_START */
262                         ERR("ctsvc_ipc_unmarshal_int() Fail");
263                         pims_ipc_data_destroy(outdata);
264                         return CONTACTS_ERROR_IPC;
265                         /* LCOV_EXCL_STOP */
266                 }
267
268                 if (CONTACTS_ERROR_NONE == ret) {
269                         int transaction_ver = 0;
270                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
271                                 /* LCOV_EXCL_START */
272                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
273                                 pims_ipc_data_destroy(outdata);
274                                 return CONTACTS_ERROR_IPC;
275                                 /* LCOV_EXCL_STOP */
276                         }
277                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
278                 }
279
280                 pims_ipc_data_destroy(outdata);
281         }
282
283         return ret;
284
285 }
286