Tizen 2.1 base
[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("pims_ipc_call failed");
81                 return CONTACTS_ERROR_IPC;
82         }
83
84         if (indata)
85         {
86                 pims_ipc_data_destroy(indata);
87         }
88
89         if (outdata)
90         {
91                 // check result
92                 unsigned int size = 0;
93                 ret = *(int*) pims_ipc_data_get(outdata, &size);
94
95                 pims_ipc_data_destroy(outdata);
96         }
97
98         return ret;
99 }
100
101 API int contacts_person_unlink_contact(int person_id, int contact_id, int* unlinked_person_id)
102 {
103         int ret = CONTACTS_ERROR_NONE;
104
105         pims_ipc_data_h indata = NULL;
106         pims_ipc_data_h outdata = NULL;
107
108         RETVM_IF(person_id <= 0 || contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
109         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
110
111         if (unlinked_person_id)
112                 *unlinked_person_id = 0;
113
114         // make indata
115         indata = pims_ipc_data_create(0);
116         if (indata == NULL)
117         {
118                 CTS_ERR("ipc data created fail!");
119                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
120                 return ret;
121         }
122
123         ret = ctsvc_ipc_marshal_int( person_id, indata);
124         if (ret != CONTACTS_ERROR_NONE)
125         {
126                 CTS_ERR("marshal fail");
127                 pims_ipc_data_destroy(indata);
128                 return ret;
129         }
130         ret = ctsvc_ipc_marshal_int( contact_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
138         // ipc call
139         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_UNLINK_CONTACT, indata, &outdata) != 0)
140         {
141                 CTS_ERR("pims_ipc_call failed");
142                 pims_ipc_data_destroy(indata);
143                 return CONTACTS_ERROR_IPC;
144         }
145
146         if (indata)
147         {
148                 pims_ipc_data_destroy(indata);
149         }
150
151         if (outdata)
152         {
153                 // check result
154                 unsigned int size = 0;
155                 ret = *(int*) pims_ipc_data_get(outdata, &size);
156
157                 if (CONTACTS_ERROR_NONE == ret) {
158                         if (unlinked_person_id)
159                                 *unlinked_person_id = *(int*)pims_ipc_data_get(outdata,&size);
160                 }
161
162                 pims_ipc_data_destroy(outdata);
163         }
164
165         return ret;
166 }
167
168 API int contacts_person_reset_usage(int person_id, contacts_usage_type_e type)
169 {
170         int ret = CONTACTS_ERROR_NONE;
171
172         pims_ipc_data_h indata = NULL;
173         pims_ipc_data_h outdata = NULL;
174
175         RETVM_IF(person_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"contact_id should be greater than 0");
176         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
177
178         // make indata
179         indata = pims_ipc_data_create(0);
180         if (indata == NULL)
181         {
182                 CTS_ERR("ipc data created fail!");
183                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
184                 return ret;
185         }
186
187         ret = ctsvc_ipc_marshal_int( person_id, indata);
188         if (ret != CONTACTS_ERROR_NONE)
189         {
190                 CTS_ERR("marshal fail");
191                 pims_ipc_data_destroy(indata);
192                 return ret;
193         }
194         ret = ctsvc_ipc_marshal_int( (int)type, indata);
195         if (ret != CONTACTS_ERROR_NONE)
196         {
197                 CTS_ERR("marshal fail");
198                 pims_ipc_data_destroy(indata);
199                 return ret;
200         }
201
202         // ipc call
203         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_RESET_USAGE, indata, &outdata) != 0)
204         {
205                 CTS_ERR("pims_ipc_call failed");
206                 pims_ipc_data_destroy(indata);
207                 return CONTACTS_ERROR_IPC;
208         }
209
210         if (indata)
211         {
212                 pims_ipc_data_destroy(indata);
213         }
214
215         if (outdata)
216         {
217                 // check result
218                 unsigned int size = 0;
219                 ret = *(int*) pims_ipc_data_get(outdata, &size);
220
221                 pims_ipc_data_destroy(outdata);
222         }
223
224         return ret;
225 }
226
227 API int contacts_person_set_favorite_order(int person_id, int previous_person_id, int next_person_id)
228 {
229         int ret = CONTACTS_ERROR_NONE;
230
231         pims_ipc_data_h indata = NULL;
232         pims_ipc_data_h outdata = NULL;
233
234         RETVM_IF(person_id <= 0 || previous_person_id < 0 || next_person_id < 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
235         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
236
237         // make indata
238         indata = pims_ipc_data_create(0);
239         if (indata == NULL)
240         {
241                 CTS_ERR("ipc data created fail!");
242                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
243                 return ret;
244         }
245
246         ret = ctsvc_ipc_marshal_int( person_id, indata);
247         if (ret != CONTACTS_ERROR_NONE)
248         {
249                 CTS_ERR("marshal fail");
250                 pims_ipc_data_destroy(indata);
251                 return ret;
252         }
253         ret = ctsvc_ipc_marshal_int( previous_person_id, indata);
254         if (ret != CONTACTS_ERROR_NONE)
255         {
256                 CTS_ERR("marshal fail");
257                 pims_ipc_data_destroy(indata);
258                 return ret;
259         }
260         ret = ctsvc_ipc_marshal_int( next_person_id, indata);
261         if (ret != CONTACTS_ERROR_NONE)
262         {
263                 CTS_ERR("marshal fail");
264                 pims_ipc_data_destroy(indata);
265                 return ret;
266         }
267
268         // ipc call
269         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_FAVORITE_ORDER, indata, &outdata) != 0)
270         {
271                 CTS_ERR("pims_ipc_call failed");
272                 return CONTACTS_ERROR_IPC;
273         }
274
275         if (indata)
276         {
277                 pims_ipc_data_destroy(indata);
278         }
279
280         if (outdata)
281         {
282                 // check result
283                 unsigned int size = 0;
284                 ret = *(int*) pims_ipc_data_get(outdata, &size);
285
286                 pims_ipc_data_destroy(outdata);
287         }
288
289         return ret;
290
291 }
292
293 API int contacts_person_set_default_property(contacts_person_property_e property,
294                 int person_id, int id)
295 {
296         int ret = CONTACTS_ERROR_NONE;
297
298         pims_ipc_data_h indata = NULL;
299         pims_ipc_data_h outdata = NULL;
300
301         RETVM_IF(person_id <= 0 || id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
302         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
303
304         // make indata
305         indata = pims_ipc_data_create(0);
306         if (indata == NULL)
307         {
308                 CTS_ERR("ipc data created fail!");
309                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
310                 return ret;
311         }
312
313         ret = ctsvc_ipc_marshal_int( person_id, indata);
314         if (ret != CONTACTS_ERROR_NONE)
315         {
316                 CTS_ERR("marshal fail");
317                 pims_ipc_data_destroy(indata);
318                 return ret;
319         }
320         ret = ctsvc_ipc_marshal_unsigned_int( property, indata);
321         if (ret != CONTACTS_ERROR_NONE)
322         {
323                 CTS_ERR("marshal fail");
324                 pims_ipc_data_destroy(indata);
325                 return ret;
326         }
327         ret = ctsvc_ipc_marshal_int( id, indata);
328         if (ret != CONTACTS_ERROR_NONE)
329         {
330                 CTS_ERR("marshal fail");
331                 pims_ipc_data_destroy(indata);
332                 return ret;
333         }
334
335         // ipc call
336         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_DEFAULT_PROPERTY, indata, &outdata) != 0)
337         {
338                 CTS_ERR("pims_ipc_call failed");
339                 pims_ipc_data_destroy(indata);
340                 return CONTACTS_ERROR_IPC;
341         }
342
343         if (indata)
344         {
345                 pims_ipc_data_destroy(indata);
346         }
347
348         if (outdata)
349         {
350                 // check result
351                 unsigned int size = 0;
352                 ret = *(int*) pims_ipc_data_get(outdata, &size);
353
354                 pims_ipc_data_destroy(outdata);
355         }
356
357         return ret;
358 }
359
360 API int contacts_person_get_default_property(contacts_person_property_e property,
361                 int person_id, int *id)
362 {
363         int ret = CONTACTS_ERROR_NONE;
364         pims_ipc_data_h indata = NULL;
365         pims_ipc_data_h outdata = NULL;
366
367         RETVM_IF(person_id <= 0 || id == NULL, CONTACTS_ERROR_INVALID_PARAMETER,"id should be greater than 0");
368         *id = 0;
369         RETVM_IF(ctsvc_get_ipc_handle() == NULL,CONTACTS_ERROR_IPC,"contacts not connected");
370
371         // make indata
372         indata = pims_ipc_data_create(0);
373         if (indata == NULL) {
374                 CTS_ERR("ipc data created fail!");
375                 ret = CONTACTS_ERROR_OUT_OF_MEMORY;
376                 return ret;
377         }
378
379         ret = ctsvc_ipc_marshal_int(person_id, indata);
380         if (ret != CONTACTS_ERROR_NONE) {
381                 CTS_ERR("marshal fail");
382                 pims_ipc_data_destroy(indata);
383                 return ret;
384         }
385
386         ret = ctsvc_ipc_marshal_unsigned_int(property, indata);
387         if (ret != CONTACTS_ERROR_NONE) {
388                 CTS_ERR("marshal fail");
389                 pims_ipc_data_destroy(indata);
390                 return ret;
391         }
392
393         // ipc call
394         if (ctsvc_ipc_call(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_GET_DEFAULT_PROPERTY,
395                                 indata, &outdata) != 0) {
396                 CTS_ERR("pims_ipc_call failed");
397                 pims_ipc_data_destroy(indata);
398                 return CONTACTS_ERROR_IPC;
399         }
400
401         if (indata) {
402                 pims_ipc_data_destroy(indata);
403         }
404
405         if (outdata) {
406                 // check result
407                 unsigned int size = 0;
408                 ret = *(int*) pims_ipc_data_get(outdata, &size);
409                 if (ret == CONTACTS_ERROR_NONE) {
410                         if (id)
411                                 *id = *(int*)pims_ipc_data_get(outdata,&size);
412                 }
413                 pims_ipc_data_destroy(outdata);
414         }
415
416         return ret;
417 }
418