ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION during disconnect_on_thread
[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         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
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                 CTS_ERR("ipc data created fail!");
47                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
48                 return ret;
49         }
50
51         ret = ctsvc_ipc_marshal_handle(contact, indata);
52         if (CONTACTS_ERROR_NONE != ret) {
53                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
54                 pims_ipc_data_destroy(indata);
55                 return ret;
56         }
57
58         ret = ctsvc_ipc_marshal_int(group_id, indata);
59         if (ret != CONTACTS_ERROR_NONE) {
60                 CTS_ERR("marshal fail");
61                 pims_ipc_data_destroy(indata);
62                 return ret;
63         }
64         ret = ctsvc_ipc_marshal_int(contact_id, indata);
65         if (ret != CONTACTS_ERROR_NONE) {
66                 CTS_ERR("marshal fail");
67                 pims_ipc_data_destroy(indata);
68                 return ret;
69         }
70
71         /* ipc call */
72         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_ADD_CONTACT, indata, &outdata) != 0) {
73                 CTS_ERR("ctsvc_ipc_call failed");
74                 pims_ipc_data_destroy(indata);
75                 return CONTACTS_ERROR_IPC;
76         }
77
78         pims_ipc_data_destroy(indata);
79
80         if (outdata) {
81                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
82                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
83                         pims_ipc_data_destroy(outdata);
84                         return CONTACTS_ERROR_IPC;
85                 }
86
87                 if (CONTACTS_ERROR_NONE == ret) {
88                         int transaction_ver = 0;
89                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
90                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
91                                 pims_ipc_data_destroy(outdata);
92                                 return CONTACTS_ERROR_IPC;
93                         }
94                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
95                 }
96
97                 pims_ipc_data_destroy(outdata);
98         }
99
100         return ret;
101 }
102
103 int ctsvc_client_group_remove_contact(contacts_h contact, int group_id, int contact_id)
104 {
105         int ret = CONTACTS_ERROR_NONE;
106
107         pims_ipc_data_h indata = NULL;
108         pims_ipc_data_h outdata = NULL;
109
110         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
111         RETVM_IF(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
112
113         /* make indata */
114         indata = pims_ipc_data_create(0);
115         if (indata == NULL) {
116                 CTS_ERR("ipc data created fail!");
117                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
118                 return ret;
119         }
120         ret = ctsvc_ipc_marshal_handle(contact, indata);
121         if (CONTACTS_ERROR_NONE != ret) {
122                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
123                 pims_ipc_data_destroy(indata);
124                 return ret;
125         }
126
127
128         ret = ctsvc_ipc_marshal_int(group_id, indata);
129         if (ret != CONTACTS_ERROR_NONE) {
130                 CTS_ERR("marshal fail");
131                 pims_ipc_data_destroy(indata);
132                 return ret;
133         }
134         ret = ctsvc_ipc_marshal_int(contact_id, indata);
135         if (ret != CONTACTS_ERROR_NONE) {
136                 CTS_ERR("marshal fail");
137                 pims_ipc_data_destroy(indata);
138                 return ret;
139         }
140
141         /* ipc call */
142         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, indata, &outdata) != 0) {
143                 CTS_ERR("ctsvc_ipc_call failed");
144                 pims_ipc_data_destroy(indata);
145                 return CONTACTS_ERROR_IPC;
146         }
147
148         pims_ipc_data_destroy(indata);
149
150         if (outdata) {
151                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
152                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
153                         pims_ipc_data_destroy(outdata);
154                         return CONTACTS_ERROR_IPC;
155                 }
156
157                 if (CONTACTS_ERROR_NONE == ret) {
158                         int transaction_ver = 0;
159                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
160                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
161                                 pims_ipc_data_destroy(outdata);
162                                 return CONTACTS_ERROR_IPC;
163                         }
164                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
165                 }
166
167                 pims_ipc_data_destroy(outdata);
168         }
169
170         return ret;
171 }
172
173 int ctsvc_client_group_set_group_order(contacts_h contact, int group_id, int previous_group_id, int next_group_id)
174 {
175         int ret = CONTACTS_ERROR_NONE;
176
177         pims_ipc_data_h indata = NULL;
178         pims_ipc_data_h outdata = NULL;
179
180         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
181         RETVM_IF(group_id <= 0 || previous_group_id < 0 || next_group_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
182
183         /* make indata */
184         indata = pims_ipc_data_create(0);
185         if (indata == NULL) {
186                 CTS_ERR("ipc data created fail!");
187                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
188                 return ret;
189         }
190         ret = ctsvc_ipc_marshal_handle(contact, indata);
191         if (CONTACTS_ERROR_NONE != ret) {
192                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
193                 pims_ipc_data_destroy(indata);
194                 return ret;
195         }
196
197         ret = ctsvc_ipc_marshal_int(group_id, indata);
198         if (ret != CONTACTS_ERROR_NONE) {
199                 CTS_ERR("marshal fail");
200                 pims_ipc_data_destroy(indata);
201                 return ret;
202         }
203         ret = ctsvc_ipc_marshal_int(previous_group_id, indata);
204         if (ret != CONTACTS_ERROR_NONE) {
205                 CTS_ERR("marshal fail");
206                 pims_ipc_data_destroy(indata);
207                 return ret;
208         }
209         ret = ctsvc_ipc_marshal_int(next_group_id, indata);
210         if (ret != CONTACTS_ERROR_NONE) {
211                 CTS_ERR("marshal fail");
212                 pims_ipc_data_destroy(indata);
213                 return ret;
214         }
215
216         /* ipc call */
217         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, indata, &outdata) != 0) {
218                 CTS_ERR("ctsvc_ipc_call failed");
219                 pims_ipc_data_destroy(indata);
220                 return CONTACTS_ERROR_IPC;
221         }
222
223         pims_ipc_data_destroy(indata);
224
225         if (outdata) {
226                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
227                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
228                         pims_ipc_data_destroy(outdata);
229                         return CONTACTS_ERROR_IPC;
230                 }
231
232                 if (CONTACTS_ERROR_NONE == ret) {
233                         int transaction_ver = 0;
234                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
235                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
236                                 pims_ipc_data_destroy(outdata);
237                                 return CONTACTS_ERROR_IPC;
238                         }
239                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
240                 }
241
242                 pims_ipc_data_destroy(outdata);
243         }
244
245         return ret;
246
247 }
248