add APIs for getting SIM init status and importing SIM contacts by sim slot no
[platform/core/pim/contacts-service.git] / client / ctsvc_client_phonelog_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 #include <pims-ipc-data.h>
20
21 #include "contacts.h"
22 #include "contacts_phone_log_internal.h"
23 #include "ctsvc_internal.h"
24 #include "ctsvc_ipc_define.h"
25 #include "ctsvc_client_ipc.h"
26 #include "ctsvc_ipc_marshal.h"
27
28
29 static const char CONTACTS_READ_PRIVILEGE_ID[] = "http://tizen.org/privilege/contact.read";
30 static const char CONTACTS_WRITE_PRIVILEGE_ID[] = "http://tizen.org/privilege/contact.write";
31 static const char PHONELOG_READ_PRIVILEGE_ID[] = "http://tizen.org/privilege/callhistory.read";
32 static const char PHONELOG_WRITE_PRIVILEGE_ID[] = "http://tizen.org/privilege/callhistory.write";
33
34 int ctsvc_client_phone_log_reset_statistics(contacts_h contact)
35 {
36 #ifndef ENABLE_LOG_FEATURE
37         return CONTACTS_ERROR_NOT_SUPPORTED;
38 #endif /* ENABLE_LOG_FEATURE */
39
40         int ret = CONTACTS_ERROR_NONE;
41
42         pims_ipc_data_h indata = NULL;
43         pims_ipc_data_h outdata = NULL;
44
45         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
46
47         /* make indata */
48         indata = pims_ipc_data_create(0);
49         if (indata == NULL) {
50                 ERR("pims_ipc_data_create() Fail");
51                 return CONTACTS_ERROR_OUT_OF_MEMORY;
52         }
53
54         ret = ctsvc_ipc_marshal_handle(contact, indata);
55         if (CONTACTS_ERROR_NONE != ret) {
56                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
57                 pims_ipc_data_destroy(indata);
58                 return ret;
59         }
60
61         /* ipc call */
62         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_RESET_STATISTICS,
63                                 indata, &outdata) != 0) {
64                 ERR("ctsvc_ipc_call() Fail");
65                 pims_ipc_data_destroy(indata);
66                 return CONTACTS_ERROR_IPC;
67         }
68
69         pims_ipc_data_destroy(indata);
70
71         if (outdata) {
72                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
73                         ERR("ctsvc_ipc_unmarshal_int() Fail");
74                         pims_ipc_data_destroy(outdata);
75                         return CONTACTS_ERROR_IPC;
76                 }
77
78                 if (CONTACTS_ERROR_NONE == ret) {
79                         int transaction_ver = 0;
80                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
81                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
82                                 pims_ipc_data_destroy(outdata);
83                                 return CONTACTS_ERROR_IPC;
84                         }
85                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
86                 }
87
88                 pims_ipc_data_destroy(outdata);
89         }
90
91         return ret;
92
93 }
94
95 int ctsvc_client_phone_log_reset_statistics_by_sim(contacts_h contact,
96                 int sim_slot_no)
97 {
98 #ifndef ENABLE_LOG_FEATURE
99         return CONTACTS_ERROR_NOT_SUPPORTED;
100 #endif /* ENABLE_LOG_FEATURE */
101
102         int ret = CONTACTS_ERROR_NONE;
103
104         pims_ipc_data_h indata = NULL;
105         pims_ipc_data_h outdata = NULL;
106
107         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
108         RETV_IF(sim_slot_no < 0, CONTACTS_ERROR_INVALID_PARAMETER);
109
110         /* make indata */
111         indata = pims_ipc_data_create(0);
112         if (indata == NULL) {
113                 ERR("pims_ipc_data_create() Fail");
114                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
115                 return ret;
116         }
117
118         ret = ctsvc_ipc_marshal_handle(contact, indata);
119         if (CONTACTS_ERROR_NONE != ret) {
120                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
121                 pims_ipc_data_destroy(indata);
122                 return ret;
123         }
124
125         ret = ctsvc_ipc_marshal_int(sim_slot_no, indata);
126         if (CONTACTS_ERROR_NONE != ret) {
127                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
128                 pims_ipc_data_destroy(indata);
129                 return ret;
130         }
131
132         /* ipc call */
133         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE,
134                         CTSVC_IPC_SERVER_PHONELOG_RESET_STATISTICS_BY_SIM,
135                         indata, &outdata) != 0) {
136                 ERR("ctsvc_ipc_call() Fail");
137                 pims_ipc_data_destroy(indata);
138                 return CONTACTS_ERROR_IPC;
139         }
140
141         pims_ipc_data_destroy(indata);
142
143         if (outdata) {
144                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
145                         ERR("ctsvc_ipc_unmarshal_int() Fail");
146                         pims_ipc_data_destroy(outdata);
147                         return CONTACTS_ERROR_IPC;
148                 }
149
150                 if (CONTACTS_ERROR_NONE == ret) {
151                         int transaction_ver = 0;
152                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
153                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
154                                 pims_ipc_data_destroy(outdata);
155                                 return CONTACTS_ERROR_IPC;
156                         }
157                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
158                 }
159
160                 pims_ipc_data_destroy(outdata);
161         }
162
163         return ret;
164 }
165
166 int ctsvc_client_phone_log_delete(contacts_h contact, contacts_phone_log_delete_e op,
167                 va_list args)
168 {
169 #ifndef ENABLE_LOG_FEATURE
170         return CONTACTS_ERROR_NOT_SUPPORTED;
171 #endif /* ENABLE_LOG_FEATURE */
172
173         int ret = CONTACTS_ERROR_NONE;
174
175         pims_ipc_data_h indata = NULL;
176         pims_ipc_data_h outdata = NULL;
177         char *number = NULL;
178         int extra_data1;
179
180         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
181
182         indata = pims_ipc_data_create(0);
183         if (indata == NULL) {
184                 ERR("pims_ipc_data_create() Fail");
185                 return CONTACTS_ERROR_OUT_OF_MEMORY;
186         }
187
188         ret = ctsvc_ipc_marshal_handle(contact, indata);
189         if (CONTACTS_ERROR_NONE != ret) {
190                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
191                 pims_ipc_data_destroy(indata);
192                 return ret;
193         }
194
195         ret = ctsvc_ipc_marshal_int(op, indata);
196         if (ret != CONTACTS_ERROR_NONE) {
197                 ERR("ctsvc_ipc_marshal_int fail");
198                 pims_ipc_data_destroy(indata);
199                 return ret;
200         }
201
202         switch (op) {
203         case CONTACTS_PHONE_LOG_DELETE_BY_ADDRESS:
204                 number = va_arg(args, char *);
205                 if (NULL == number) {
206                         pims_ipc_data_destroy(indata);
207                         return CONTACTS_ERROR_INVALID_PARAMETER;
208                 }
209                 ret = ctsvc_ipc_marshal_string(number, indata);
210                 if (ret != CONTACTS_ERROR_NONE) {
211                         ERR("ctsvc_ipc_marshal_string fail");
212                         pims_ipc_data_destroy(indata);
213                         return ret;
214                 }
215                 break;
216         case CONTACTS_PHONE_LOG_DELETE_BY_MESSAGE_EXTRA_DATA1:
217         case CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1:
218                 extra_data1 = va_arg(args, int);
219                 ret = ctsvc_ipc_marshal_int(extra_data1, indata);
220                 if (ret != CONTACTS_ERROR_NONE) {
221                         ERR("ctsvc_ipc_marshal_int fail");
222                         pims_ipc_data_destroy(indata);
223                         return ret;
224                 }
225                 break;
226         default:
227                 ERR("operation is not proper (%d)", ret);
228                 pims_ipc_data_destroy(indata);
229                 return CONTACTS_ERROR_INVALID_PARAMETER;
230         }
231
232         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE,
233                                 CTSVC_IPC_SERVER_PHONELOG_DELETE, indata, &outdata) != 0) {
234                 ERR("ctsvc_ipc_call() Fail");
235                 pims_ipc_data_destroy(indata);
236                 return CONTACTS_ERROR_IPC;
237         }
238
239         pims_ipc_data_destroy(indata);
240
241         if (outdata) {
242                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
243                         ERR("ctsvc_ipc_unmarshal_int() Fail");
244                         pims_ipc_data_destroy(outdata);
245                         return CONTACTS_ERROR_IPC;
246                 }
247                 if (CONTACTS_ERROR_NONE == ret) {
248                         int transaction_ver = 0;
249                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
250                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
251                                 pims_ipc_data_destroy(outdata);
252                                 return CONTACTS_ERROR_IPC;
253                         }
254                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
255                 }
256
257                 pims_ipc_data_destroy(outdata);
258         }
259
260         return ret;
261 }
262