Revise marshal/unmarshal codes
[platform/core/pim/contacts-service.git] / client / ctsvc_client_activity_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 <glib.h>
21 #include <pims-ipc-data.h>
22
23 #include "contacts.h"
24 #include "ctsvc_internal.h"
25 #include "ctsvc_ipc_define.h"
26 #include "ctsvc_ipc_marshal.h"
27 #include "ctsvc_client_ipc.h"
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
35 int ctsvc_client_activity_delete_by_contact_id(contacts_h contact, int contact_id)
36 {
37         int ret = CONTACTS_ERROR_NONE;
38         pims_ipc_data_h indata = NULL;
39         pims_ipc_data_h outdata = NULL;
40
41         RETVM_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER, "contact is NULL");
42         RETVM_IF(contact_id <= 0,CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
43
44         /* make indata */
45         indata = pims_ipc_data_create(0);
46         if (indata == NULL) {
47                 CTS_ERR("ipc data created fail!");
48                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
49                 return ret;
50         }
51
52         ret = ctsvc_ipc_marshal_handle(contact, indata);
53         if (CONTACTS_ERROR_NONE != ret) {
54                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
55                 pims_ipc_data_destroy(indata);
56                 return ret;
57         }
58
59         ret = ctsvc_ipc_marshal_int(contact_id, indata);
60         if (ret != CONTACTS_ERROR_NONE) {
61                 CTS_ERR("marshal fail");
62                 pims_ipc_data_destroy(indata);
63                 return ret;
64         }
65
66         /* ipc call */
67         if (ctsvc_ipc_call(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_CONTACT_ID, indata, &outdata) != 0) {
68                 CTS_ERR("ctsvc_ipc_call failed");
69                 pims_ipc_data_destroy(indata);
70                 return CONTACTS_ERROR_IPC;
71         }
72
73         pims_ipc_data_destroy(indata);
74
75         if (outdata) {
76                 int transaction_ver = 0;
77
78                 // check result
79                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
80                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
81                         pims_ipc_data_destroy(outdata);
82                         return CONTACTS_ERROR_IPC;
83                 }
84                 if (CONTACTS_ERROR_NONE == ret) {
85                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
86                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
87                                 pims_ipc_data_destroy(outdata);
88                                 return CONTACTS_ERROR_IPC;
89                         }
90                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
91                 }
92                 pims_ipc_data_destroy(outdata);
93         }
94
95         return ret;
96 }
97
98 int ctsvc_client_activity_delete_by_account_id(contacts_h contact, int account_id)
99 {
100         int ret = CONTACTS_ERROR_NONE;
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(account_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
115         ret = ctsvc_ipc_marshal_handle(contact, indata);
116         if (CONTACTS_ERROR_NONE != ret) {
117                 CTS_ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
118                 pims_ipc_data_destroy(indata);
119                 return ret;
120         }
121
122         ret = ctsvc_ipc_marshal_int(account_id, indata);
123         if (ret != CONTACTS_ERROR_NONE) {
124                 CTS_ERR("marshal fail");
125                 pims_ipc_data_destroy(indata);
126                 return ret;
127         }
128
129         /* ipc call */
130         if (ctsvc_ipc_call(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_ACCOUNT_ID, indata, &outdata) != 0) {
131                 CTS_ERR("ctsvc_ipc_call failed");
132                 pims_ipc_data_destroy(indata);
133                 return CONTACTS_ERROR_IPC;
134         }
135
136         pims_ipc_data_destroy(indata);
137
138         if (outdata) {
139                 int transaction_ver = 0;
140                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
141                         CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
142                         pims_ipc_data_destroy(outdata);
143                         return CONTACTS_ERROR_IPC;
144                 }
145
146                 if (CONTACTS_ERROR_NONE == ret) {
147                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
148                                 CTS_ERR("ctsvc_ipc_unmarshal_int() Fail");
149                                 pims_ipc_data_destroy(outdata);
150                                 return CONTACTS_ERROR_IPC;
151                         }
152                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
153                 }
154                 pims_ipc_data_destroy(outdata);
155         }
156
157         return ret;
158 }
159