Add user space access control
[platform/core/pim/contacts-service.git] / client / ctsvc_client_person.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 <glib.h>
21
22 #include "contacts.h"
23 #include "ctsvc_internal.h"
24 #include "ctsvc_ipc_define.h"
25 #include "ctsvc_client_ipc.h"
26 #include <pims-ipc-data.h>
27 #include "ctsvc_ipc_marshal.h"
28
29 API int contacts_person_link_person(int base_person_id, int person_id)
30 {
31
32         int ret = CONTACTS_ERROR_NONE;
33
34         pims_ipc_data_h indata = NULL;
35         pims_ipc_data_h outdata = NULL;
36
37         RETVM_IF(base_person_id <= 0 || person_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
38         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
39
40         // make indata
41         indata = pims_ipc_data_create(0);
42         if (indata == NULL)
43         {
44                 CTS_ERR("ipc data created fail!");
45                 return CONTACTS_ERROR_OUT_OF_MEMORY;
46         }
47
48         bool success = false;
49         do {
50                 if( ctsvc_ipc_marshal_int( base_person_id, indata) != CONTACTS_ERROR_NONE ) break;
51                 if( ctsvc_ipc_marshal_int( person_id, indata) != CONTACTS_ERROR_NONE ) break;
52
53                 success = true;
54         } while(0);
55
56         if( success == false ) {
57                 CTS_ERR("marshal fail");
58                 pims_ipc_data_destroy(indata);
59                 return CONTACTS_ERROR_IPC;
60         }
61
62 /*
63         ret = ctsvc_ipc_marshal_int( base_person_id, indata);
64         if (ret != CONTACTS_ERROR_NONE)
65         {
66                 CTS_ERR("marshal fail");
67                 return ret;
68         }
69         ret = ctsvc_ipc_marshal_int( person_id, indata);
70         if (ret != CONTACTS_ERROR_NONE)
71         {
72                 CTS_ERR("marshal fail");
73                 return ret;
74         }
75 */
76
77         // ipc call
78         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_LINK_PERSON, indata, &outdata) != 0)
79         {
80                 CTS_ERR("ctsvc_ipc_call failed");
81                 pims_ipc_data_destroy(indata);
82                 return CONTACTS_ERROR_IPC;
83         }
84
85         if (indata)
86         {
87                 pims_ipc_data_destroy(indata);
88         }
89
90         if (outdata)
91         {
92                 // check result
93                 unsigned int size = 0;
94                 ret = *(int*) pims_ipc_data_get(outdata, &size);
95
96                 if (CONTACTS_ERROR_NONE == ret) {
97                         int transaction_ver = 0;
98                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
99                         ctsvc_client_ipc_set_change_version(transaction_ver);
100                 }
101
102                 pims_ipc_data_destroy(outdata);
103         }
104
105         return ret;
106 }
107
108 API int contacts_person_unlink_contact(int person_id, int contact_id, int* unlinked_person_id)
109 {
110         int ret = CONTACTS_ERROR_NONE;
111
112         pims_ipc_data_h indata = NULL;
113         pims_ipc_data_h outdata = NULL;
114
115         RETVM_IF(person_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
116         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
117
118         if (unlinked_person_id)
119                 *unlinked_person_id = 0;
120
121         // make indata
122         indata = pims_ipc_data_create(0);
123         if (indata == NULL)
124         {
125                 CTS_ERR("ipc data created fail!");
126                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
127                 return ret;
128         }
129
130         ret = ctsvc_ipc_marshal_int( person_id, indata);
131         if (ret != CONTACTS_ERROR_NONE)
132         {
133                 CTS_ERR("marshal fail");
134                 pims_ipc_data_destroy(indata);
135                 return ret;
136         }
137         ret = ctsvc_ipc_marshal_int( contact_id, indata);
138         if (ret != CONTACTS_ERROR_NONE)
139         {
140                 CTS_ERR("marshal fail");
141                 pims_ipc_data_destroy(indata);
142                 return ret;
143         }
144
145         // ipc call
146         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_UNLINK_CONTACT, indata, &outdata) != 0)
147         {
148                 CTS_ERR("ctsvc_ipc_call failed");
149                 pims_ipc_data_destroy(indata);
150                 return CONTACTS_ERROR_IPC;
151         }
152
153         if (indata)
154         {
155                 pims_ipc_data_destroy(indata);
156         }
157
158         if (outdata)
159         {
160                 // check result
161                 unsigned int size = 0;
162                 ret = *(int*) pims_ipc_data_get(outdata, &size);
163
164                 if (CONTACTS_ERROR_NONE == ret) {
165                         int transaction_ver = 0;
166                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
167                         ctsvc_client_ipc_set_change_version(transaction_ver);
168
169                         if (unlinked_person_id)
170                                 *unlinked_person_id = *(int*)pims_ipc_data_get(outdata,&size);
171                 }
172
173                 pims_ipc_data_destroy(outdata);
174         }
175
176         return ret;
177 }
178
179 API int contacts_person_reset_usage(int person_id, contacts_usage_type_e type)
180 {
181         int ret = CONTACTS_ERROR_NONE;
182
183         pims_ipc_data_h indata = NULL;
184         pims_ipc_data_h outdata = NULL;
185
186         RETVM_IF(person_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"contact_id should be greater than 0");
187         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
188
189         // make indata
190         indata = pims_ipc_data_create(0);
191         if (indata == NULL)
192         {
193                 CTS_ERR("ipc data created fail!");
194                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
195                 return ret;
196         }
197
198         ret = ctsvc_ipc_marshal_int( person_id, indata);
199         if (ret != CONTACTS_ERROR_NONE)
200         {
201                 CTS_ERR("marshal fail");
202                 pims_ipc_data_destroy(indata);
203                 return ret;
204         }
205         ret = ctsvc_ipc_marshal_int( (int)type, indata);
206         if (ret != CONTACTS_ERROR_NONE)
207         {
208                 CTS_ERR("marshal fail");
209                 pims_ipc_data_destroy(indata);
210                 return ret;
211         }
212
213         // ipc call
214         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_RESET_USAGE, indata, &outdata) != 0)
215         {
216                 CTS_ERR("ctsvc_ipc_call failed");
217                 pims_ipc_data_destroy(indata);
218                 return CONTACTS_ERROR_IPC;
219         }
220
221         if (indata)
222         {
223                 pims_ipc_data_destroy(indata);
224         }
225
226         if (outdata)
227         {
228                 // check result
229                 unsigned int size = 0;
230                 ret = *(int*) pims_ipc_data_get(outdata, &size);
231
232                 if (CONTACTS_ERROR_NONE == ret) {
233                         int transaction_ver = 0;
234                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
235                         ctsvc_client_ipc_set_change_version(transaction_ver);
236                 }
237
238                 pims_ipc_data_destroy(outdata);
239         }
240
241         return ret;
242 }
243
244 API int contacts_person_set_favorite_order(int person_id, int previous_person_id, int next_person_id)
245 {
246         int ret = CONTACTS_ERROR_NONE;
247
248         pims_ipc_data_h indata = NULL;
249         pims_ipc_data_h outdata = NULL;
250
251         RETVM_IF(person_id <= 0 || previous_person_id < 0 || next_person_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
252         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
253
254         // make indata
255         indata = pims_ipc_data_create(0);
256         if (indata == NULL)
257         {
258                 CTS_ERR("ipc data created fail!");
259                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
260                 return ret;
261         }
262
263         ret = ctsvc_ipc_marshal_int( person_id, indata);
264         if (ret != CONTACTS_ERROR_NONE)
265         {
266                 CTS_ERR("marshal fail");
267                 pims_ipc_data_destroy(indata);
268                 return ret;
269         }
270         ret = ctsvc_ipc_marshal_int( previous_person_id, indata);
271         if (ret != CONTACTS_ERROR_NONE)
272         {
273                 CTS_ERR("marshal fail");
274                 pims_ipc_data_destroy(indata);
275                 return ret;
276         }
277         ret = ctsvc_ipc_marshal_int( next_person_id, indata);
278         if (ret != CONTACTS_ERROR_NONE)
279         {
280                 CTS_ERR("marshal fail");
281                 pims_ipc_data_destroy(indata);
282                 return ret;
283         }
284
285         // ipc call
286         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_FAVORITE_ORDER, indata, &outdata) != 0)
287         {
288                 CTS_ERR("ctsvc_ipc_call failed");
289                 pims_ipc_data_destroy(indata);
290                 return CONTACTS_ERROR_IPC;
291         }
292
293         if (indata)
294         {
295                 pims_ipc_data_destroy(indata);
296         }
297
298         if (outdata)
299         {
300                 // check result
301                 unsigned int size = 0;
302                 ret = *(int*) pims_ipc_data_get(outdata, &size);
303
304                 if (CONTACTS_ERROR_NONE == ret) {
305                         int transaction_ver = 0;
306                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
307                         ctsvc_client_ipc_set_change_version(transaction_ver);
308                 }
309
310                 pims_ipc_data_destroy(outdata);
311         }
312
313         return ret;
314
315 }
316
317 API int contacts_person_set_default_property(contacts_person_property_e property,
318                 int person_id, int id)
319 {
320         int ret = CONTACTS_ERROR_NONE;
321
322         pims_ipc_data_h indata = NULL;
323         pims_ipc_data_h outdata = NULL;
324
325         RETVM_IF(person_id <= 0 || id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
326         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
327
328         // make indata
329         indata = pims_ipc_data_create(0);
330         if (indata == NULL)
331         {
332                 CTS_ERR("ipc data created fail!");
333                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
334                 return ret;
335         }
336
337         ret = ctsvc_ipc_marshal_int( person_id, indata);
338         if (ret != CONTACTS_ERROR_NONE)
339         {
340                 CTS_ERR("marshal fail");
341                 pims_ipc_data_destroy(indata);
342                 return ret;
343         }
344         ret = ctsvc_ipc_marshal_unsigned_int( property, indata);
345         if (ret != CONTACTS_ERROR_NONE)
346         {
347                 CTS_ERR("marshal fail");
348                 pims_ipc_data_destroy(indata);
349                 return ret;
350         }
351         ret = ctsvc_ipc_marshal_int( id, indata);
352         if (ret != CONTACTS_ERROR_NONE)
353         {
354                 CTS_ERR("marshal fail");
355                 pims_ipc_data_destroy(indata);
356                 return ret;
357         }
358
359         // ipc call
360         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_DEFAULT_PROPERTY, indata, &outdata) != 0)
361         {
362                 CTS_ERR("ctsvc_ipc_call failed");
363                 pims_ipc_data_destroy(indata);
364                 return CONTACTS_ERROR_IPC;
365         }
366
367         if (indata)
368         {
369                 pims_ipc_data_destroy(indata);
370         }
371
372         if (outdata)
373         {
374                 // check result
375                 unsigned int size = 0;
376                 ret = *(int*) pims_ipc_data_get(outdata, &size);
377                 if (CONTACTS_ERROR_NONE == ret) {
378                         int transaction_ver = 0;
379                         transaction_ver = *(int*)pims_ipc_data_get(outdata,&size);
380                         ctsvc_client_ipc_set_change_version(transaction_ver);
381                 }
382
383                 pims_ipc_data_destroy(outdata);
384         }
385
386         return ret;
387 }
388
389 API int contacts_person_get_default_property(contacts_person_property_e property,
390                 int person_id, int *id)
391 {
392         int ret = CONTACTS_ERROR_NONE;
393         pims_ipc_data_h indata = NULL;
394         pims_ipc_data_h outdata = NULL;
395
396         RETVM_IF(person_id <= 0 || id == NULL, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
397         *id = 0;
398         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
399
400         // make indata
401         indata = pims_ipc_data_create(0);
402         if (indata == NULL) {
403                 CTS_ERR("ipc data created fail!");
404                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
405                 return ret;
406         }
407
408         ret = ctsvc_ipc_marshal_int(person_id, indata);
409         if (ret != CONTACTS_ERROR_NONE) {
410                 CTS_ERR("marshal fail");
411                 pims_ipc_data_destroy(indata);
412                 return ret;
413         }
414
415         ret = ctsvc_ipc_marshal_unsigned_int(property, indata);
416         if (ret != CONTACTS_ERROR_NONE) {
417                 CTS_ERR("marshal fail");
418                 pims_ipc_data_destroy(indata);
419                 return ret;
420         }
421
422         // ipc call
423         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_GET_DEFAULT_PROPERTY,
424                                 indata, &outdata) != 0) {
425                 CTS_ERR("ctsvc_ipc_call failed");
426                 pims_ipc_data_destroy(indata);
427                 return CONTACTS_ERROR_IPC;
428         }
429
430         if (indata) {
431                 pims_ipc_data_destroy(indata);
432         }
433
434         if (outdata) {
435                 // check result
436                 unsigned int size = 0;
437                 ret = *(int*) pims_ipc_data_get(outdata, &size);
438                 if (ret == CONTACTS_ERROR_NONE) {
439                         if (id)
440                                 *id = *(int*)pims_ipc_data_get(outdata,&size);
441                 }
442                 pims_ipc_data_destroy(outdata);
443         }
444
445         return ret;
446 }
447