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