ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION
[platform/core/pim/contacts-service.git] / client / ctsvc_client_phonelog_helper.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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 <pims-ipc-data.h>
21
22 #include "contacts.h"
23 #include "contacts_phone_log_internal.h"
24 #include "ctsvc_internal.h"
25 #include "ctsvc_ipc_define.h"
26 #include "ctsvc_client_ipc.h"
27 #include "ctsvc_ipc_marshal.h"
28
29
30 static const char CONTACTS_READ_PRIVILEGE_ID[] =  "http://tizen.org/privilege/contact.read";
31 static const char CONTACTS_WRITE_PRIVILEGE_ID[] =  "http://tizen.org/privilege/contact.write";
32 static const char PHONELOG_READ_PRIVILEGE_ID[] =  "http://tizen.org/privilege/callhistory.read";
33 static const char PHONELOG_WRITE_PRIVILEGE_ID[] =  "http://tizen.org/privilege/callhistory.write";
34
35 int ctsvc_client_phone_log_reset_statistics(contacts_h contact)
36 {
37 #ifndef ENABLE_LOG_FEATURE
38         return CONTACTS_ERROR_NOT_SUPPORTED;
39 #endif /* ENABLE_LOG_FEATURE */
40
41         int ret = CONTACTS_ERROR_NONE;
42
43         pims_ipc_data_h indata = NULL;
44         pims_ipc_data_h outdata = NULL;
45
46         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
47
48         /* make indata */
49         indata = pims_ipc_data_create(0);
50         if (indata == NULL) {
51                 CTS_ERR("ipc data created fail!");
52                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
53                 return ret;
54         }
55
56         ret = ctsvc_ipc_marshal_handle(contact, indata);
57         if (CONTACTS_ERROR_NONE != ret) {
58                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
59                 pims_ipc_data_destroy(indata);
60                 return ret;
61         }
62
63         /* ipc call */
64         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_RESET_STATISTICS, indata, &outdata) != 0) {
65                 CTS_ERR("ctsvc_ipc_call failed");
66                 pims_ipc_data_destroy(indata);
67                 return CONTACTS_ERROR_IPC;
68         }
69
70         pims_ipc_data_destroy(indata);
71
72         if (outdata) {
73                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
74                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
75                         pims_ipc_data_destroy(outdata);
76                         return CONTACTS_ERROR_IPC;
77                 }
78
79                 if (CONTACTS_ERROR_NONE == ret) {
80                         int transaction_ver = 0;
81                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
82                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
83                                 pims_ipc_data_destroy(outdata);
84                                 return CONTACTS_ERROR_IPC;
85                         }
86                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
87                 }
88
89                 pims_ipc_data_destroy(outdata);
90         }
91
92         return ret;
93
94 }
95
96 int ctsvc_client_phone_log_delete(contacts_h contact, contacts_phone_log_delete_e op, va_list args)
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         char *number = NULL;
107         int extra_data1;
108
109         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
110
111         indata = pims_ipc_data_create(0);
112         if (indata == NULL) {
113                 CTS_ERR("ipc data created fail!");
114                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
115                 pims_ipc_data_destroy(indata);
116                 return ret;
117         }
118
119         ret = ctsvc_ipc_marshal_handle(contact, indata);
120         if (CONTACTS_ERROR_NONE != ret) {
121                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
122                 pims_ipc_data_destroy(indata);
123                 return ret;
124         }
125
126         ret = ctsvc_ipc_marshal_int(op, indata);
127         if (ret != CONTACTS_ERROR_NONE) {
128                 CTS_ERR("ctsvc_ipc_marshal_int fail");
129                 pims_ipc_data_destroy(indata);
130                 return ret;
131         }
132
133         switch(op) {
134         case CONTACTS_PHONE_LOG_DELETE_BY_ADDRESS:
135                 number = va_arg(args, char *);
136                 if (NULL == number) {
137                         pims_ipc_data_destroy(indata);
138                         return CONTACTS_ERROR_INVALID_PARAMETER;
139                 }
140                 ret = ctsvc_ipc_marshal_string(number, indata);
141                 if (ret != CONTACTS_ERROR_NONE) {
142                         CTS_ERR("ctsvc_ipc_marshal_string fail");
143                         pims_ipc_data_destroy(indata);
144                         return ret;
145                 }
146                 break;
147         case CONTACTS_PHONE_LOG_DELETE_BY_MESSAGE_EXTRA_DATA1:
148         case CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1:
149                 extra_data1 = va_arg(args, int);
150                 ret = ctsvc_ipc_marshal_int(extra_data1, indata);
151                 if (ret != CONTACTS_ERROR_NONE) {
152                         CTS_ERR("ctsvc_ipc_marshal_int fail");
153                         pims_ipc_data_destroy(indata);
154                         return ret;
155                 }
156                 break;
157         default:
158                 CTS_ERR("Invalid parameter : operation is not proper (%d)", ret);
159                 pims_ipc_data_destroy(indata);
160                 return CONTACTS_ERROR_INVALID_PARAMETER;
161         }
162
163         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE,
164                         CTSVC_IPC_SERVER_PHONELOG_DELETE, indata, &outdata) != 0) {
165                 CTS_ERR("ctsvc_ipc_call failed");
166                 pims_ipc_data_destroy(indata);
167                 return CONTACTS_ERROR_IPC;
168         }
169
170         pims_ipc_data_destroy(indata);
171
172         if (outdata) {
173                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
174                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
175                         pims_ipc_data_destroy(outdata);
176                         return CONTACTS_ERROR_IPC;
177                 }
178                 if (CONTACTS_ERROR_NONE == ret) {
179                         int transaction_ver = 0;
180                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
181                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
182                                 pims_ipc_data_destroy(outdata);
183                                 return CONTACTS_ERROR_IPC;
184                         }
185                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
186                 }
187
188                 pims_ipc_data_destroy(outdata);
189         }
190
191         return ret;
192 }
193