add handle
[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                 // check result
82                 unsigned int size = 0;
83                 ret = *(int*) pims_ipc_data_get(outdata, &size);
84
85                 if (CONTACTS_ERROR_NONE == ret) {
86                         int transaction_ver = 0;
87                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
88                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
89                 }
90
91                 pims_ipc_data_destroy(outdata);
92         }
93
94         return ret;
95 }
96
97 int ctsvc_client_group_remove_contact(contacts_h contact, int group_id, int contact_id)
98 {
99         int ret = CONTACTS_ERROR_NONE;
100
101         pims_ipc_data_h indata = NULL;
102         pims_ipc_data_h outdata = NULL;
103
104         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
105         RETVM_IF(group_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
106
107         /* make indata */
108         indata = pims_ipc_data_create(0);
109         if (indata == NULL) {
110                 CTS_ERR("ipc data created fail!");
111                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
112                 return ret;
113         }
114         ret = ctsvc_ipc_marshal_handle(contact, indata);
115         if (CONTACTS_ERROR_NONE != ret) {
116                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
117                 pims_ipc_data_destroy(indata);
118                 return ret;
119         }
120
121
122         ret = ctsvc_ipc_marshal_int(group_id, indata);
123         if (ret != CONTACTS_ERROR_NONE) {
124                 CTS_ERR("marshal fail");
125                 pims_ipc_data_destroy(indata);
126                 return ret;
127         }
128         ret = ctsvc_ipc_marshal_int(contact_id, indata);
129         if (ret != CONTACTS_ERROR_NONE) {
130                 CTS_ERR("marshal fail");
131                 pims_ipc_data_destroy(indata);
132                 return ret;
133         }
134
135         /* ipc call */
136         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, indata, &outdata) != 0) {
137                 CTS_ERR("ctsvc_ipc_call failed");
138                 pims_ipc_data_destroy(indata);
139                 return CONTACTS_ERROR_IPC;
140         }
141
142         pims_ipc_data_destroy(indata);
143
144         if (outdata) {
145                 // check result
146                 unsigned int size = 0;
147                 ret = *(int*) pims_ipc_data_get(outdata, &size);
148
149                 if (CONTACTS_ERROR_NONE == ret) {
150                         int transaction_ver = 0;
151                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
152                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
153                 }
154
155                 pims_ipc_data_destroy(outdata);
156         }
157
158         return ret;
159 }
160
161 int ctsvc_client_group_set_group_order(contacts_h contact, int group_id, int previous_group_id, int next_group_id)
162 {
163         int ret = CONTACTS_ERROR_NONE;
164
165         pims_ipc_data_h indata = NULL;
166         pims_ipc_data_h outdata = NULL;
167
168         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
169         RETVM_IF(group_id <= 0 || previous_group_id < 0 || next_group_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
170
171         /* make indata */
172         indata = pims_ipc_data_create(0);
173         if (indata == NULL) {
174                 CTS_ERR("ipc data created fail!");
175                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
176                 return ret;
177         }
178         ret = ctsvc_ipc_marshal_handle(contact, indata);
179         if (CONTACTS_ERROR_NONE != ret) {
180                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
181                 pims_ipc_data_destroy(indata);
182                 return ret;
183         }
184
185         ret = ctsvc_ipc_marshal_int(group_id, indata);
186         if (ret != CONTACTS_ERROR_NONE) {
187                 CTS_ERR("marshal fail");
188                 pims_ipc_data_destroy(indata);
189                 return ret;
190         }
191         ret = ctsvc_ipc_marshal_int(previous_group_id, indata);
192         if (ret != CONTACTS_ERROR_NONE) {
193                 CTS_ERR("marshal fail");
194                 pims_ipc_data_destroy(indata);
195                 return ret;
196         }
197         ret = ctsvc_ipc_marshal_int(next_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
204         /* ipc call */
205         if (ctsvc_ipc_call(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, indata, &outdata) != 0) {
206                 CTS_ERR("ctsvc_ipc_call failed");
207                 pims_ipc_data_destroy(indata);
208                 return CONTACTS_ERROR_IPC;
209         }
210
211         pims_ipc_data_destroy(indata);
212
213         if (outdata) {
214                 // check result
215                 unsigned int size = 0;
216                 ret = *(int*) pims_ipc_data_get(outdata, &size);
217
218                 if (CONTACTS_ERROR_NONE == ret) {
219                         int transaction_ver = 0;
220                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
221                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
222                 }
223
224                 pims_ipc_data_destroy(outdata);
225         }
226
227         return ret;
228
229 }
230