add handle
[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                 // check result
74                 unsigned int size = 0;
75                 ret = *(int*) pims_ipc_data_get(outdata, &size);
76
77                 if (CONTACTS_ERROR_NONE == ret) {
78                         int transaction_ver = 0;
79                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
80                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
81                 }
82
83                 pims_ipc_data_destroy(outdata);
84         }
85
86         return ret;
87
88 }
89
90 int ctsvc_client_phone_log_delete(contacts_h contact, contacts_phone_log_delete_e op, va_list args)
91 {
92 #ifndef ENABLE_LOG_FEATURE
93         return CONTACTS_ERROR_NOT_SUPPORTED;
94 #endif /* ENABLE_LOG_FEATURE */
95
96         int ret = CONTACTS_ERROR_NONE;
97
98         pims_ipc_data_h indata = NULL;
99         pims_ipc_data_h outdata = NULL;
100         char *number = NULL;
101         int extra_data1;
102
103         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
104
105         indata = pims_ipc_data_create(0);
106         if (indata == NULL) {
107                 CTS_ERR("ipc data created fail!");
108                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
109                 pims_ipc_data_destroy(indata);
110                 return ret;
111         }
112
113         ret = ctsvc_ipc_marshal_handle(contact, indata);
114         if (CONTACTS_ERROR_NONE != ret) {
115                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
116                 pims_ipc_data_destroy(indata);
117                 return ret;
118         }
119
120         ret = ctsvc_ipc_marshal_int(op, indata);
121         if (ret != CONTACTS_ERROR_NONE) {
122                 CTS_ERR("ctsvc_ipc_marshal_int fail");
123                 pims_ipc_data_destroy(indata);
124                 return ret;
125         }
126
127         switch(op) {
128         case CONTACTS_PHONE_LOG_DELETE_BY_ADDRESS:
129                 number = va_arg(args, char *);
130                 if (NULL == number) {
131                         pims_ipc_data_destroy(indata);
132                         return CONTACTS_ERROR_INVALID_PARAMETER;
133                 }
134                 ret = ctsvc_ipc_marshal_string(number, indata);
135                 if (ret != CONTACTS_ERROR_NONE) {
136                         CTS_ERR("ctsvc_ipc_marshal_string fail");
137                         pims_ipc_data_destroy(indata);
138                         return ret;
139                 }
140                 break;
141         case CONTACTS_PHONE_LOG_DELETE_BY_MESSAGE_EXTRA_DATA1:
142         case CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1:
143                 extra_data1 = va_arg(args, int);
144                 ret = ctsvc_ipc_marshal_int(extra_data1, indata);
145                 if (ret != CONTACTS_ERROR_NONE) {
146                         CTS_ERR("ctsvc_ipc_marshal_int fail");
147                         pims_ipc_data_destroy(indata);
148                         return ret;
149                 }
150                 break;
151         default:
152                 CTS_ERR("Invalid parameter : operation is not proper (%d)", ret);
153                 pims_ipc_data_destroy(indata);
154                 return CONTACTS_ERROR_INVALID_PARAMETER;
155         }
156
157         if (ctsvc_ipc_call(CTSVC_IPC_PHONELOG_MODULE,
158                         CTSVC_IPC_SERVER_PHONELOG_DELETE, indata, &outdata) != 0) {
159                 CTS_ERR("ctsvc_ipc_call failed");
160                 pims_ipc_data_destroy(indata);
161                 return CONTACTS_ERROR_IPC;
162         }
163
164         pims_ipc_data_destroy(indata);
165
166         if (outdata) {
167                 unsigned int size = 0;
168                 ret = *(int*) pims_ipc_data_get(outdata, &size);
169
170                 if (CONTACTS_ERROR_NONE == ret) {
171                         int transaction_ver = 0;
172                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
173                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
174                 }
175
176                 pims_ipc_data_destroy(outdata);
177         }
178
179         return ret;
180 }
181