add APIs for getting SIM init status and importing SIM contacts by sim slot no
[platform/core/pim/contacts-service.git] / client / ctsvc_client_db_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.h>
22 #include <pims-ipc-data.h>
23
24 #include "contacts.h"
25
26 #include "ctsvc_internal.h"
27 #include "ctsvc_list.h"
28 #include "ctsvc_record.h"
29 #include "ctsvc_inotify.h"
30 #include "ctsvc_ipc_define.h"
31 #include "ctsvc_ipc_marshal.h"
32 #include "ctsvc_view.h"
33 #include "ctsvc_client_ipc.h"
34 #include "ctsvc_mutex.h"
35 #include "ctsvc_handle.h"
36 #include "ctsvc_client_db_helper.h"
37
38 #define CTSVC_DEFAULT_START_MATCH "["
39 #define CTSVC_DEFAULT_END_MATCH "]"
40
41 int ctsvc_client_db_insert_record(contacts_h contact, contacts_record_h record, int *id)
42 {
43         int ret = CONTACTS_ERROR_NONE;
44         pims_ipc_data_h indata = NULL;
45         pims_ipc_data_h outdata = NULL;
46
47         if (id)
48                 *id = 0;
49
50         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
51         RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
52
53         /* make indata */
54         indata = pims_ipc_data_create(0);
55         if (indata == NULL) {
56                 ERR("pims_ipc_data_create() Fail");
57                 return CONTACTS_ERROR_OUT_OF_MEMORY;
58         }
59
60         ret = ctsvc_ipc_marshal_handle(contact, indata);
61         if (ret != CONTACTS_ERROR_NONE) {
62                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
63                 pims_ipc_data_destroy(indata);
64                 return ret;
65         }
66
67         ret = ctsvc_ipc_marshal_record(record, indata);
68         if (ret != CONTACTS_ERROR_NONE) {
69                 ERR("ctsvc_ipc_marshal_record() Fail(%d)", ret);
70                 pims_ipc_data_destroy(indata);
71                 return ret;
72         }
73
74         /* ipc call */
75         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORD, indata, &outdata) != 0) {
76                 ERR("ctsvc_ipc_call() Fail");
77                 pims_ipc_data_destroy(indata);
78                 return CONTACTS_ERROR_IPC;
79         }
80
81         pims_ipc_data_destroy(indata);
82
83         if (outdata) {
84                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
85                         ERR("ctsvc_ipc_unmarshal_int() Fail");
86                         pims_ipc_data_destroy(outdata);
87                         return CONTACTS_ERROR_IPC;
88                 }
89
90                 if (CONTACTS_ERROR_NONE == ret) {
91                         int transaction_ver = 0;
92                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
93                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
94                                 pims_ipc_data_destroy(outdata);
95                                 return CONTACTS_ERROR_IPC;
96                         }
97                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
98
99                         if (id) {
100                                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, id)) {
101                                         ERR("ctsvc_ipc_unmarshal_int() Fail");
102                                         pims_ipc_data_destroy(outdata);
103                                         return CONTACTS_ERROR_IPC;
104                                 }
105                         }
106                 }
107                 pims_ipc_data_destroy(outdata);
108         }
109
110         return ret;
111 }
112
113 int ctsvc_client_db_get_record(contacts_h contact, const char *view_uri, int id, contacts_record_h *out_record)
114 {
115         int ret = CONTACTS_ERROR_NONE;
116         pims_ipc_data_h indata = NULL;
117         pims_ipc_data_h outdata = NULL;
118
119         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
120         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
121         RETVM_IF(id < 0, CONTACTS_ERROR_INVALID_PARAMETER, "id < 0");
122         RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
123         *out_record = NULL;
124
125         /* make indata */
126         indata = pims_ipc_data_create(0);
127         if (indata == NULL) {
128                 ERR("pims_ipc_data_create() Fail");
129                 return CONTACTS_ERROR_OUT_OF_MEMORY;
130         }
131
132         ret = ctsvc_ipc_marshal_handle(contact, indata);
133         if (ret != CONTACTS_ERROR_NONE) {
134                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
135                 pims_ipc_data_destroy(indata);
136                 return ret;
137         }
138
139         ret = ctsvc_ipc_marshal_string(view_uri, indata);
140         if (ret != CONTACTS_ERROR_NONE) {
141                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
142                 pims_ipc_data_destroy(indata);
143                 return ret;
144         }
145         ret = ctsvc_ipc_marshal_int(id, indata);
146         if (ret != CONTACTS_ERROR_NONE) {
147                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
148                 pims_ipc_data_destroy(indata);
149                 return ret;
150         }
151
152         /* ipc call */
153         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORD, indata, &outdata) != 0) {
154                 ERR("ctsvc_ipc_call() Fail");
155                 pims_ipc_data_destroy(indata);
156                 return CONTACTS_ERROR_IPC;
157         }
158
159         pims_ipc_data_destroy(indata);
160
161         if (outdata) {
162                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
163                         ERR("ctsvc_ipc_unmarshal_int() Fail");
164                         pims_ipc_data_destroy(outdata);
165                         return CONTACTS_ERROR_IPC;
166                 }
167                 if (CONTACTS_ERROR_NONE == ret) {
168                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_record(outdata, out_record)) {
169                                 ERR("ctsvc_ipc_unmarshal_record() Fail");
170                                 pims_ipc_data_destroy(outdata);
171                                 return CONTACTS_ERROR_IPC;
172                         }
173                 }
174                 pims_ipc_data_destroy(outdata);
175         }
176         return ret;
177 }
178
179 int ctsvc_client_db_update_record(contacts_h contact, contacts_record_h record)
180 {
181         int ret = CONTACTS_ERROR_NONE;
182         pims_ipc_data_h indata = NULL;
183         pims_ipc_data_h outdata = NULL;
184
185         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
186         RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
187
188         /* make indata */
189         indata = pims_ipc_data_create(0);
190         if (indata == NULL) {
191                 ERR("pims_ipc_data_create() Fail");
192                 return CONTACTS_ERROR_OUT_OF_MEMORY;
193         }
194
195         ret = ctsvc_ipc_marshal_handle(contact, indata);
196         if (ret != CONTACTS_ERROR_NONE) {
197                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
198                 pims_ipc_data_destroy(indata);
199                 return ret;
200         }
201
202         ret = ctsvc_ipc_marshal_record(record, indata);
203         if (ret != CONTACTS_ERROR_NONE) {
204                 ERR("ctsvc_ipc_marshal_record() Fail(%d)", ret);
205                 pims_ipc_data_destroy(indata);
206                 return ret;
207         }
208
209         /* ipc call */
210         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORD, indata, &outdata) != 0) {
211                 ERR("ctsvc_ipc_call() Fail");
212                 pims_ipc_data_destroy(indata);
213                 return CONTACTS_ERROR_IPC;
214         }
215
216         pims_ipc_data_destroy(indata);
217
218         if (outdata) {
219                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
220                         ERR("ctsvc_ipc_unmarshal_int() Fail");
221                         pims_ipc_data_destroy(outdata);
222                         return CONTACTS_ERROR_IPC;
223                 }
224                 if (CONTACTS_ERROR_NONE == ret) {
225                         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
226                         int transaction_ver = 0;
227                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
228                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
229                                 pims_ipc_data_destroy(outdata);
230                                 return CONTACTS_ERROR_IPC;
231                         }
232                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
233                 }
234                 pims_ipc_data_destroy(outdata);
235         }
236         return ret;
237 }
238
239 int ctsvc_client_db_delete_record(contacts_h contact, const char *view_uri, int id)
240 {
241         int ret = CONTACTS_ERROR_NONE;
242         pims_ipc_data_h indata = NULL;
243         pims_ipc_data_h outdata = NULL;
244
245         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
246         RETVM_IF(id <= 0, CONTACTS_ERROR_INVALID_PARAMETER, "id <= 0");
247
248         /* make indata */
249         indata = pims_ipc_data_create(0);
250         if (indata == NULL) {
251                 ERR("pims_ipc_data_create() Fail");
252                 return CONTACTS_ERROR_OUT_OF_MEMORY;
253         }
254
255         ret = ctsvc_ipc_marshal_handle(contact, indata);
256         if (CONTACTS_ERROR_NONE != ret) {
257                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
258                 pims_ipc_data_destroy(indata);
259                 return ret;
260         }
261
262         ret = ctsvc_ipc_marshal_string(view_uri, indata);
263         if (ret != CONTACTS_ERROR_NONE) {
264                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
265                 pims_ipc_data_destroy(indata);
266                 return ret;
267         }
268         ret = ctsvc_ipc_marshal_int(id, indata);
269         if (ret != CONTACTS_ERROR_NONE) {
270                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
271                 pims_ipc_data_destroy(indata);
272                 return ret;
273         }
274
275         /* ipc call */
276         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORD, indata, &outdata) != 0) {
277                 ERR("ctsvc_ipc_call() Fail");
278                 pims_ipc_data_destroy(indata);
279                 return CONTACTS_ERROR_IPC;
280         }
281
282         pims_ipc_data_destroy(indata);
283
284         if (outdata) {
285                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
286                         ERR("ctsvc_ipc_unmarshal_int() Fail");
287                         pims_ipc_data_destroy(outdata);
288                         return CONTACTS_ERROR_IPC;
289                 }
290                 if (CONTACTS_ERROR_NONE == ret) {
291                         int transaction_ver = 0;
292                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
293                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
294                                 pims_ipc_data_destroy(outdata);
295                                 return CONTACTS_ERROR_IPC;
296                         }
297                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
298                 }
299
300                 pims_ipc_data_destroy(outdata);
301         }
302
303         return ret;
304 }
305
306 int ctsvc_client_db_replace_record(contacts_h contact, contacts_record_h record, int id)
307 {
308         int ret = CONTACTS_ERROR_NONE;
309         pims_ipc_data_h indata = NULL;
310         pims_ipc_data_h outdata = NULL;
311
312         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
313         RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
314
315         /* make indata */
316         indata = pims_ipc_data_create(0);
317         if (indata == NULL) {
318                 ERR("pims_ipc_data_create() Fail");
319                 return CONTACTS_ERROR_OUT_OF_MEMORY;
320         }
321
322         ret = ctsvc_ipc_marshal_handle(contact, indata);
323         if (CONTACTS_ERROR_NONE != ret) {
324                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
325                 pims_ipc_data_destroy(indata);
326                 return ret;
327         }
328
329         ret = ctsvc_ipc_marshal_record(record, indata);
330         if (ret != CONTACTS_ERROR_NONE) {
331                 ERR("ctsvc_ipc_marshal_record() Fail(%d)", ret);
332                 pims_ipc_data_destroy(indata);
333                 return ret;
334         }
335         ret = ctsvc_ipc_marshal_int(id, indata);
336         if (ret != CONTACTS_ERROR_NONE) {
337                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
338                 pims_ipc_data_destroy(indata);
339                 return ret;
340         }
341
342         /* ipc call */
343         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE,
344                                 CTSVC_IPC_SERVER_DB_REPLACE_RECORD, indata, &outdata) != 0) {
345                 ERR("ctsvc_ipc_call() Fail");
346                 pims_ipc_data_destroy(indata);
347                 return CONTACTS_ERROR_IPC;
348         }
349
350         pims_ipc_data_destroy(indata);
351
352         if (outdata) {
353                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
354                         ERR("ctsvc_ipc_unmarshal_int() Fail");
355                         pims_ipc_data_destroy(outdata);
356                         return CONTACTS_ERROR_IPC;
357                 }
358                 if (CONTACTS_ERROR_NONE == ret) {
359                         int transaction_ver = 0;
360                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
361                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
362                                 pims_ipc_data_destroy(outdata);
363                                 return CONTACTS_ERROR_IPC;
364                         }
365                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
366                 }
367                 pims_ipc_data_destroy(outdata);
368         }
369
370         return ret;
371 }
372
373 int ctsvc_client_db_get_all_records(contacts_h contact, const char *view_uri, int offset, int limit, contacts_list_h *out_list)
374 {
375         int ret = CONTACTS_ERROR_NONE;
376         pims_ipc_data_h indata = NULL;
377         pims_ipc_data_h outdata = NULL;
378
379         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
380         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
381         *out_list = NULL;
382         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
383
384         /* make indata */
385         indata = pims_ipc_data_create(0);
386         if (indata == NULL) {
387                 ERR("pims_ipc_data_create() Fail");
388                 return CONTACTS_ERROR_OUT_OF_MEMORY;
389         }
390
391         ret = ctsvc_ipc_marshal_handle(contact, indata);
392         if (CONTACTS_ERROR_NONE != ret) {
393                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
394                 pims_ipc_data_destroy(indata);
395                 return ret;
396         }
397
398         ret = ctsvc_ipc_marshal_string(view_uri, indata);
399         if (ret != CONTACTS_ERROR_NONE) {
400                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
401                 pims_ipc_data_destroy(indata);
402                 return ret;
403         }
404         ret = ctsvc_ipc_marshal_int(offset, indata);
405         if (ret != CONTACTS_ERROR_NONE) {
406                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
407                 pims_ipc_data_destroy(indata);
408                 return ret;
409         }
410         ret = ctsvc_ipc_marshal_int(limit, indata);
411         if (ret != CONTACTS_ERROR_NONE) {
412                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
413                 pims_ipc_data_destroy(indata);
414                 return ret;
415         }
416
417         /* ipc call */
418         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_ALL_RECORDS, indata, &outdata) != 0) {
419                 ERR("ctsvc_ipc_call() Fail");
420                 pims_ipc_data_destroy(indata);
421                 return CONTACTS_ERROR_IPC;
422         }
423
424         pims_ipc_data_destroy(indata);
425
426         if (outdata) {
427                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
428                         ERR("ctsvc_ipc_unmarshal_int() Fail");
429                         pims_ipc_data_destroy(outdata);
430                         return CONTACTS_ERROR_IPC;
431                 }
432
433                 if (ret == CONTACTS_ERROR_NONE) {
434                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
435                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
436                                 pims_ipc_data_destroy(outdata);
437                                 return CONTACTS_ERROR_IPC;
438                         }
439                 }
440                 pims_ipc_data_destroy(outdata);
441         }
442         return ret;
443 }
444
445 int ctsvc_client_db_get_records_with_query(contacts_h contact, contacts_query_h query, int offset, int limit, contacts_list_h *out_list)
446 {
447         int ret = CONTACTS_ERROR_NONE;
448         pims_ipc_data_h indata = NULL;
449         pims_ipc_data_h outdata = NULL;
450
451         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
452         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
453         *out_list = NULL;
454         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
455
456         /* make indata */
457         indata = pims_ipc_data_create(0);
458         if (indata == NULL) {
459                 ERR("pims_ipc_data_create() Fail");
460                 return CONTACTS_ERROR_OUT_OF_MEMORY;
461         }
462
463         ret = ctsvc_ipc_marshal_handle(contact, indata);
464         if (CONTACTS_ERROR_NONE != ret) {
465                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
466                 pims_ipc_data_destroy(indata);
467                 return ret;
468         }
469
470         ret = ctsvc_ipc_marshal_query(query, indata);
471         if (ret != CONTACTS_ERROR_NONE) {
472                 ERR("ctsvc_ipc_marshal_query() Fail(%d)", ret);
473                 pims_ipc_data_destroy(indata);
474                 return ret;
475         }
476
477         ret = ctsvc_ipc_marshal_int(offset, indata);
478         if (ret != CONTACTS_ERROR_NONE) {
479                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
480                 pims_ipc_data_destroy(indata);
481                 return ret;
482         }
483         ret = ctsvc_ipc_marshal_int(limit, indata);
484         if (ret != CONTACTS_ERROR_NONE) {
485                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
486                 return ret;
487         }
488
489         /* ipc call */
490         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORDS_WITH_QUERY, indata, &outdata) != 0) {
491                 ERR("ctsvc_ipc_call() Fail");
492                 pims_ipc_data_destroy(indata);
493                 return CONTACTS_ERROR_IPC;
494         }
495
496         pims_ipc_data_destroy(indata);
497
498         if (outdata) {
499                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
500                         ERR("ctsvc_ipc_unmarshal_int() Fail");
501                         pims_ipc_data_destroy(outdata);
502                         return CONTACTS_ERROR_IPC;
503                 }
504
505                 if (CONTACTS_ERROR_NONE == ret) {
506                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
507                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
508                                 pims_ipc_data_destroy(outdata);
509                                 return CONTACTS_ERROR_IPC;
510                         }
511                 }
512
513                 pims_ipc_data_destroy(outdata);
514         }
515
516         return ret;
517 }
518
519
520 int ctsvc_client_db_get_count(contacts_h contact, const char *view_uri, int *out_count)
521 {
522         int ret = CONTACTS_ERROR_NONE;
523         pims_ipc_data_h indata = NULL;
524         pims_ipc_data_h outdata = NULL;
525
526         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
527         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
528         RETV_IF(NULL == out_count, CONTACTS_ERROR_INVALID_PARAMETER);
529         *out_count = 0;
530
531         /* make indata */
532         indata = pims_ipc_data_create(0);
533         if (indata == NULL) {
534                 ERR("pims_ipc_data_create() Fail");
535                 return CONTACTS_ERROR_OUT_OF_MEMORY;
536         }
537
538         ret = ctsvc_ipc_marshal_handle(contact, indata);
539         if (CONTACTS_ERROR_NONE != ret) {
540                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
541                 pims_ipc_data_destroy(indata);
542                 return ret;
543         }
544
545         ret = ctsvc_ipc_marshal_string(view_uri, indata);
546         if (ret != CONTACTS_ERROR_NONE) {
547                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
548                 pims_ipc_data_destroy(indata);
549                 return ret;
550         }
551
552         /* ipc call */
553         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT, indata, &outdata) != 0) {
554                 ERR("ctsvc_ipc_call() Fail");
555                 pims_ipc_data_destroy(indata);
556                 return CONTACTS_ERROR_IPC;
557         }
558
559         pims_ipc_data_destroy(indata);
560
561         if (outdata) {
562                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
563                         ERR("ctsvc_ipc_unmarshal_int() Fail");
564                         pims_ipc_data_destroy(outdata);
565                         return CONTACTS_ERROR_IPC;
566                 }
567
568                 if (CONTACTS_ERROR_NONE == ret) {
569                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, out_count)) {
570                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
571                                 pims_ipc_data_destroy(outdata);
572                                 return CONTACTS_ERROR_IPC;
573                         }
574                 }
575
576                 pims_ipc_data_destroy(outdata);
577         }
578
579         return ret;
580 }
581
582 int ctsvc_client_db_get_count_with_query(contacts_h contact, contacts_query_h query, int *out_count)
583 {
584         int ret = CONTACTS_ERROR_NONE;
585         pims_ipc_data_h indata = NULL;
586         pims_ipc_data_h outdata = NULL;
587
588         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
589         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
590         RETV_IF(NULL == out_count, CONTACTS_ERROR_INVALID_PARAMETER);
591         *out_count = 0;
592
593         /* make indata */
594         indata = pims_ipc_data_create(0);
595         if (indata == NULL) {
596                 ERR("pims_ipc_data_create() Fail");
597                 return CONTACTS_ERROR_OUT_OF_MEMORY;
598         }
599
600         ret = ctsvc_ipc_marshal_handle(contact, indata);
601         if (CONTACTS_ERROR_NONE != ret) {
602                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
603                 pims_ipc_data_destroy(indata);
604                 return ret;
605         }
606
607         ret = ctsvc_ipc_marshal_query(query, indata);
608         if (ret != CONTACTS_ERROR_NONE) {
609                 ERR("ctsvc_ipc_marshal_query() Fail(%d)", ret);
610                 pims_ipc_data_destroy(indata);
611                 return ret;
612         }
613
614         /* ipc call */
615         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT_WITH_QUERY, indata, &outdata) != 0) {
616                 ERR("ctsvc_ipc_call() Fail");
617                 pims_ipc_data_destroy(indata);
618                 return CONTACTS_ERROR_IPC;
619         }
620
621         pims_ipc_data_destroy(indata);
622
623         if (outdata) {
624                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
625                         ERR("ctsvc_ipc_unmarshal_int() Fail");
626                         pims_ipc_data_destroy(outdata);
627                         return CONTACTS_ERROR_IPC;
628                 }
629
630                 if (CONTACTS_ERROR_NONE == ret) {
631                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, out_count)) {
632                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
633                                 pims_ipc_data_destroy(outdata);
634                                 return CONTACTS_ERROR_IPC;
635                         }
636                 }
637                 pims_ipc_data_destroy(outdata);
638         }
639
640         return ret;
641 }
642
643 int ctsvc_client_db_insert_records(contacts_h contact, contacts_list_h list, int **ids, int *count)
644 {
645         int ret = CONTACTS_ERROR_NONE;
646         pims_ipc_data_h indata = NULL;
647         pims_ipc_data_h outdata = NULL;
648
649         if (ids)
650                 *ids = NULL;
651         if (count)
652                 *count = 0;
653
654         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
655         RETV_IF(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER);
656
657         indata = pims_ipc_data_create(0);
658         if (indata == NULL) {
659                 ERR("pims_ipc_data_create() Fail");
660                 return CONTACTS_ERROR_OUT_OF_MEMORY;
661         }
662
663         ret = ctsvc_ipc_marshal_handle(contact, indata);
664         if (CONTACTS_ERROR_NONE != ret) {
665                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
666                 pims_ipc_data_destroy(indata);
667                 return ret;
668         }
669
670         ret = ctsvc_ipc_marshal_list(list, indata);
671         if (ret != CONTACTS_ERROR_NONE) {
672                 ERR("ctsvc_ipc_marshal_list() Fail(%d)", ret);
673                 pims_ipc_data_destroy(indata);
674                 return ret;
675         }
676
677         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORDS,
678                                 indata, &outdata) != 0) {
679                 ERR("ctsvc_ipc_call() Fail");
680                 pims_ipc_data_destroy(indata);
681                 return CONTACTS_ERROR_IPC;
682         }
683
684         pims_ipc_data_destroy(indata);
685
686         if (outdata) {
687                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
688                         ERR("ctsvc_ipc_unmarshal_int() Fail");
689                         pims_ipc_data_destroy(outdata);
690                         return CONTACTS_ERROR_IPC;
691                 }
692
693                 if (CONTACTS_ERROR_NONE == ret) {
694                         int transaction_ver = 0;
695                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
696                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
697                                 pims_ipc_data_destroy(outdata);
698                                 return CONTACTS_ERROR_IPC;
699                         }
700                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
701
702                         if (ids && count) {
703                                 int i = 0;
704                                 int *id = NULL;
705                                 int c;
706
707                                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &c)) {
708                                         ERR("ctsvc_ipc_unmarshal_int() Fail");
709                                         pims_ipc_data_destroy(outdata);
710                                         return CONTACTS_ERROR_IPC;
711                                 }
712                                 id = calloc(c, sizeof(int));
713                                 if (NULL == id) {
714                                         ERR("calloc() Fail");
715                                         pims_ipc_data_destroy(outdata);
716                                         return CONTACTS_ERROR_OUT_OF_MEMORY;
717                                 }
718
719                                 for (i = 0; i < c; i++) {
720                                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &(id[i]))) {
721                                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
722                                                 pims_ipc_data_destroy(outdata);
723                                                 free(id);
724                                                 return CONTACTS_ERROR_IPC;
725                                         }
726                                 }
727                                 *ids = id;
728                                 *count = c;
729                         }
730                 }
731                 pims_ipc_data_destroy(outdata);
732         }
733
734         return ret;
735 }
736
737 int ctsvc_client_db_update_records(contacts_h contact, contacts_list_h list)
738 {
739         int ret = CONTACTS_ERROR_NONE;
740         pims_ipc_data_h indata = NULL;
741         pims_ipc_data_h outdata = NULL;
742
743         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
744         RETV_IF(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER);
745
746         indata = pims_ipc_data_create(0);
747         if (indata == NULL) {
748                 ERR("pims_ipc_data_create() Fail");
749                 return CONTACTS_ERROR_OUT_OF_MEMORY;
750         }
751
752         ret = ctsvc_ipc_marshal_handle(contact, indata);
753         if (CONTACTS_ERROR_NONE != ret) {
754                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
755                 pims_ipc_data_destroy(indata);
756                 return ret;
757         }
758
759         ret = ctsvc_ipc_marshal_list(list, indata);
760         if (ret != CONTACTS_ERROR_NONE) {
761                 ERR("ctsvc_ipc_marshal_list() Fail(%d)", ret);
762                 pims_ipc_data_destroy(indata);
763                 return ret;
764         }
765
766         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORDS,
767                                 indata, &outdata) != 0) {
768                 ERR("ctsvc_ipc_call() Fail");
769                 pims_ipc_data_destroy(indata);
770                 return CONTACTS_ERROR_IPC;
771         }
772
773         pims_ipc_data_destroy(indata);
774
775         if (outdata) {
776                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
777                         ERR("ctsvc_ipc_unmarshal_int() Fail");
778                         pims_ipc_data_destroy(outdata);
779                         return CONTACTS_ERROR_IPC;
780                 }
781
782                 if (CONTACTS_ERROR_NONE == ret) {
783                         int transaction_ver = 0;
784                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
785                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
786                                 pims_ipc_data_destroy(outdata);
787                                 return CONTACTS_ERROR_IPC;
788                         }
789                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
790                 }
791
792                 pims_ipc_data_destroy(outdata);
793         }
794
795         return ret;
796 }
797
798 int ctsvc_client_db_delete_records(contacts_h contact, const char *view_uri, int ids[], int count)
799 {
800         int i;
801         int ret = CONTACTS_ERROR_NONE;
802         pims_ipc_data_h indata = NULL;
803         pims_ipc_data_h outdata = NULL;
804
805         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
806         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
807
808         indata = pims_ipc_data_create(0);
809         if (indata == NULL) {
810                 ERR("pims_ipc_data_create() Fail");
811                 return CONTACTS_ERROR_OUT_OF_MEMORY;
812         }
813
814         ret = ctsvc_ipc_marshal_handle(contact, indata);
815         if (CONTACTS_ERROR_NONE != ret) {
816                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
817                 pims_ipc_data_destroy(indata);
818                 return ret;
819         }
820
821         ret = ctsvc_ipc_marshal_string(view_uri, indata);
822         if (ret != CONTACTS_ERROR_NONE) {
823                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
824                 pims_ipc_data_destroy(indata);
825                 return ret;
826         }
827
828         ret = ctsvc_ipc_marshal_int(count, indata);
829         if (ret != CONTACTS_ERROR_NONE) {
830                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
831                 pims_ipc_data_destroy(indata);
832                 return ret;
833         }
834
835         for (i = 0; i < count; i++) {
836                 ret = ctsvc_ipc_marshal_int(ids[i], indata);
837                 if (ret != CONTACTS_ERROR_NONE) {
838                         ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
839                         pims_ipc_data_destroy(indata);
840                         return ret;
841                 }
842         }
843
844         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORDS,
845                                 indata, &outdata) != 0) {
846                 ERR("ctsvc_ipc_call() Fail");
847                 pims_ipc_data_destroy(indata);
848                 return CONTACTS_ERROR_IPC;
849         }
850
851         pims_ipc_data_destroy(indata);
852
853         if (outdata) {
854                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
855                         ERR("ctsvc_ipc_unmarshal_int() Fail");
856                         pims_ipc_data_destroy(outdata);
857                         return CONTACTS_ERROR_IPC;
858                 }
859
860                 if (CONTACTS_ERROR_NONE == ret) {
861                         int transaction_ver = 0;
862                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
863                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
864                                 pims_ipc_data_destroy(outdata);
865                                 return CONTACTS_ERROR_IPC;
866                         }
867                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
868                 }
869
870                 pims_ipc_data_destroy(outdata);
871         }
872
873         return ret;
874 }
875
876 int ctsvc_client_db_replace_records(contacts_h contact, contacts_list_h list, int ids[], int count)
877 {
878         int i;
879         int ret = CONTACTS_ERROR_NONE;
880         pims_ipc_data_h indata = NULL;
881         pims_ipc_data_h outdata = NULL;
882
883         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
884         RETV_IF(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER);
885         RETV_IF(NULL == ids, CONTACTS_ERROR_INVALID_PARAMETER);
886         RETV_IF(0 == count, CONTACTS_ERROR_INVALID_PARAMETER);
887
888         /* make indata */
889         indata = pims_ipc_data_create(0);
890         if (indata == NULL) {
891                 ERR("pims_ipc_data_create() Fail");
892                 return CONTACTS_ERROR_OUT_OF_MEMORY;
893         }
894
895         ret = ctsvc_ipc_marshal_handle(contact, indata);
896         if (CONTACTS_ERROR_NONE != ret) {
897                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
898                 pims_ipc_data_destroy(indata);
899                 return ret;
900         }
901
902         ret = ctsvc_ipc_marshal_list(list, indata);
903         if (ret != CONTACTS_ERROR_NONE) {
904                 ERR("ctsvc_ipc_marshal_list() Fail(%d)", ret);
905                 pims_ipc_data_destroy(indata);
906                 return ret;
907         }
908
909         ret = ctsvc_ipc_marshal_int(count, indata);
910         if (ret != CONTACTS_ERROR_NONE) {
911                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
912                 pims_ipc_data_destroy(indata);
913                 return ret;
914         }
915
916         for (i = 0; i < count; i++) {
917                 ret = ctsvc_ipc_marshal_int(ids[i], indata);
918                 if (ret != CONTACTS_ERROR_NONE) {
919                         ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
920                         pims_ipc_data_destroy(indata);
921                         return ret;
922                 }
923         }
924
925         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_REPLACE_RECORDS,
926                                 indata, &outdata) != 0) {
927                 ERR("ctsvc_ipc_call() Fail");
928                 pims_ipc_data_destroy(indata);
929                 return CONTACTS_ERROR_IPC;
930         }
931
932         pims_ipc_data_destroy(indata);
933
934         if (outdata) {
935                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
936                         ERR("ctsvc_ipc_unmarshal_int() Fail");
937                         pims_ipc_data_destroy(outdata);
938                         return CONTACTS_ERROR_IPC;
939                 }
940                 if (CONTACTS_ERROR_NONE == ret) {
941                         int transaction_ver = 0;
942                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &transaction_ver)) {
943                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
944                                 pims_ipc_data_destroy(outdata);
945                                 return CONTACTS_ERROR_IPC;
946                         }
947                         ctsvc_client_ipc_set_change_version(contact, transaction_ver);
948                 }
949                 pims_ipc_data_destroy(outdata);
950         }
951
952         return ret;
953 }
954
955 int ctsvc_client_db_get_changes_by_version(contacts_h contact, const char *view_uri, int addressbook_id, int contacts_db_version, contacts_list_h *record_list, int *current_contacts_db_version)
956 {
957         int ret = CONTACTS_ERROR_NONE;
958         pims_ipc_data_h indata = NULL;
959         pims_ipc_data_h outdata = NULL;
960
961         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
962         RETV_IF(NULL == record_list, CONTACTS_ERROR_INVALID_PARAMETER);
963
964         *record_list = NULL;
965         RETV_IF(NULL == view_uri, CONTACTS_ERROR_INVALID_PARAMETER);
966         RETV_IF(NULL == current_contacts_db_version, CONTACTS_ERROR_INVALID_PARAMETER);
967
968         /* make indata */
969         indata = pims_ipc_data_create(0);
970         if (indata == NULL) {
971                 ERR("pims_ipc_data_create() Fail");
972                 return CONTACTS_ERROR_OUT_OF_MEMORY;
973         }
974
975         ret = ctsvc_ipc_marshal_handle(contact, indata);
976         if (CONTACTS_ERROR_NONE != ret) {
977                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
978                 pims_ipc_data_destroy(indata);
979                 return ret;
980         }
981
982         ret = ctsvc_ipc_marshal_string(view_uri, indata);
983         if (ret != CONTACTS_ERROR_NONE) {
984                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
985                 pims_ipc_data_destroy(indata);
986                 return ret;
987         }
988         ret = ctsvc_ipc_marshal_int(addressbook_id, indata);
989         if (ret != CONTACTS_ERROR_NONE) {
990                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
991                 pims_ipc_data_destroy(indata);
992                 return ret;
993         }
994         ret = ctsvc_ipc_marshal_int(contacts_db_version, indata);
995         if (ret != CONTACTS_ERROR_NONE) {
996                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
997                 pims_ipc_data_destroy(indata);
998                 return ret;
999         }
1000
1001         /* ipc call */
1002         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_CHANGES_BY_VERSION, indata, &outdata) != 0) {
1003                 ERR("ctsvc_ipc_call() Fail");
1004                 pims_ipc_data_destroy(indata);
1005                 return CONTACTS_ERROR_IPC;
1006         }
1007
1008         pims_ipc_data_destroy(indata);
1009
1010         if (outdata) {
1011                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1012                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1013                         pims_ipc_data_destroy(outdata);
1014                         return CONTACTS_ERROR_IPC;
1015                 }
1016
1017                 if (ret == CONTACTS_ERROR_NONE) {
1018                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, record_list)) {
1019                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1020                                 pims_ipc_data_destroy(outdata);
1021                                 return CONTACTS_ERROR_IPC;
1022                         }
1023                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, current_contacts_db_version)) {
1024                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1025                                 pims_ipc_data_destroy(outdata);
1026                                 return CONTACTS_ERROR_IPC;
1027                         }
1028                 }
1029                 pims_ipc_data_destroy(outdata);
1030         }
1031
1032         return ret;
1033 }
1034
1035 int ctsvc_client_db_get_current_version(contacts_h contact, int *contacts_db_version)
1036 {
1037         int ret = CONTACTS_ERROR_NONE;
1038         pims_ipc_data_h indata = NULL;
1039         pims_ipc_data_h outdata = NULL;
1040
1041         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1042         RETV_IF(NULL == contacts_db_version, CONTACTS_ERROR_INVALID_PARAMETER);
1043
1044         *contacts_db_version = 0;
1045
1046         /* make indata */
1047         indata = pims_ipc_data_create(0);
1048         if (indata == NULL) {
1049                 ERR("pims_ipc_data_create() Fail");
1050                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1051         }
1052
1053         ret = ctsvc_ipc_marshal_handle(contact, indata);
1054         if (CONTACTS_ERROR_NONE != ret) {
1055                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1056                 pims_ipc_data_destroy(indata);
1057                 return ret;
1058         }
1059
1060         /* ipc call */
1061         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_CURRENT_VERSION, indata, &outdata) != 0) {
1062                 ERR("ctsvc_ipc_call() Fail");
1063                 pims_ipc_data_destroy(indata);
1064                 return CONTACTS_ERROR_IPC;
1065         }
1066
1067         pims_ipc_data_destroy(indata);
1068
1069         if (outdata) {
1070                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1071                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1072                         pims_ipc_data_destroy(outdata);
1073                         return CONTACTS_ERROR_IPC;
1074                 }
1075
1076                 if (CONTACTS_ERROR_NONE == ret) {
1077                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, contacts_db_version)) {
1078                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1079                                 pims_ipc_data_destroy(outdata);
1080                                 return CONTACTS_ERROR_IPC;
1081                         }
1082                 }
1083                 pims_ipc_data_destroy(outdata);
1084         }
1085
1086         return ret;
1087 }
1088
1089 int ctsvc_client_db_search_records_for_snippet(contacts_h contact,
1090                 const char *view_uri,
1091                 const char *keyword,
1092                 int offset,
1093                 int limit,
1094                 const char *start_match,
1095                 const char *end_match,
1096                 int token_number,
1097                 contacts_list_h *out_list)
1098 {
1099         int ret = CONTACTS_ERROR_NONE;
1100         pims_ipc_data_h indata = NULL;
1101         pims_ipc_data_h outdata = NULL;
1102
1103         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1104         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1105         *out_list = NULL;
1106
1107         /* make indata */
1108         indata = pims_ipc_data_create(0);
1109         if (indata == NULL) {
1110                 ERR("pims_ipc_data_create() Fail");
1111                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1112         }
1113
1114         ret = ctsvc_ipc_marshal_handle(contact, indata);
1115         if (CONTACTS_ERROR_NONE != ret) {
1116                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1117                 pims_ipc_data_destroy(indata);
1118                 return ret;
1119         }
1120         ret = ctsvc_ipc_marshal_string(view_uri, indata);
1121         if (ret != CONTACTS_ERROR_NONE) {
1122                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1123                 pims_ipc_data_destroy(indata);
1124                 return ret;
1125         }
1126         ret = ctsvc_ipc_marshal_string(keyword, indata);
1127         if (ret != CONTACTS_ERROR_NONE) {
1128                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1129                 pims_ipc_data_destroy(indata);
1130                 return ret;
1131         }
1132         ret = ctsvc_ipc_marshal_int(offset, indata);
1133         if (ret != CONTACTS_ERROR_NONE) {
1134                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1135                 pims_ipc_data_destroy(indata);
1136                 return ret;
1137         }
1138         ret = ctsvc_ipc_marshal_int(limit, indata);
1139         if (ret != CONTACTS_ERROR_NONE) {
1140                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1141                 pims_ipc_data_destroy(indata);
1142                 return ret;
1143         }
1144         ret = ctsvc_ipc_marshal_string(start_match ? start_match : CTSVC_DEFAULT_START_MATCH, indata);
1145         if (ret != CONTACTS_ERROR_NONE) {
1146                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1147                 pims_ipc_data_destroy(indata);
1148                 return ret;
1149         }
1150         ret = ctsvc_ipc_marshal_string(end_match ? end_match : CTSVC_DEFAULT_END_MATCH, indata);
1151         if (ret != CONTACTS_ERROR_NONE) {
1152                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1153                 pims_ipc_data_destroy(indata);
1154                 return ret;
1155         }
1156         ret = ctsvc_ipc_marshal_int(token_number, indata);
1157         if (ret != CONTACTS_ERROR_NONE) {
1158                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1159                 pims_ipc_data_destroy(indata);
1160                 return ret;
1161         }
1162
1163         /* ipc call */
1164         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE,
1165                                 CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_FOR_SNIPPET,
1166                                 indata, &outdata) != 0) {
1167                 ERR("ctsvc_ipc_call() Fail");
1168                 pims_ipc_data_destroy(indata);
1169                 return CONTACTS_ERROR_IPC;
1170         }
1171
1172         pims_ipc_data_destroy(indata);
1173
1174         if (outdata) {
1175                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1176                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1177                         pims_ipc_data_destroy(outdata);
1178                         return CONTACTS_ERROR_IPC;
1179                 }
1180
1181                 if (CONTACTS_ERROR_NONE == ret) {
1182                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1183                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1184                                 pims_ipc_data_destroy(outdata);
1185                                 return CONTACTS_ERROR_IPC;
1186                         }
1187                 }
1188
1189                 pims_ipc_data_destroy(outdata);
1190         }
1191
1192         return ret;
1193 }
1194
1195 int ctsvc_client_db_search_records_with_range_for_snippet(contacts_h contact,
1196                 const char *view_uri,
1197                 const char *keyword,
1198                 int offset,
1199                 int limit,
1200                 int range,
1201                 const char *start_match,
1202                 const char *end_match,
1203                 int token_number,
1204                 contacts_list_h *out_list)
1205 {
1206         int ret = CONTACTS_ERROR_NONE;
1207         pims_ipc_data_h indata = NULL;
1208         pims_ipc_data_h outdata = NULL;
1209
1210         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1211         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1212         RETV_IF(0 == range, CONTACTS_ERROR_INVALID_PARAMETER);
1213         *out_list = NULL;
1214
1215         indata = pims_ipc_data_create(0);
1216         if (indata == NULL) {
1217                 ERR("pims_ipc_data_create() Fail");
1218                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1219         }
1220         ret = ctsvc_ipc_marshal_handle(contact, indata);
1221         if (CONTACTS_ERROR_NONE != ret) {
1222                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1223                 pims_ipc_data_destroy(indata);
1224                 return ret;
1225         }
1226
1227         ret = ctsvc_ipc_marshal_string(view_uri, indata);
1228         if (ret != CONTACTS_ERROR_NONE) {
1229                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1230                 pims_ipc_data_destroy(indata);
1231                 return ret;
1232         }
1233         ret = ctsvc_ipc_marshal_string(keyword, indata);
1234         if (ret != CONTACTS_ERROR_NONE) {
1235                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1236                 pims_ipc_data_destroy(indata);
1237                 return ret;
1238         }
1239         ret = ctsvc_ipc_marshal_int(offset, indata);
1240         if (ret != CONTACTS_ERROR_NONE) {
1241                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1242                 pims_ipc_data_destroy(indata);
1243                 return ret;
1244         }
1245         ret = ctsvc_ipc_marshal_int(limit, indata);
1246         if (ret != CONTACTS_ERROR_NONE) {
1247                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1248                 pims_ipc_data_destroy(indata);
1249                 return ret;
1250         }
1251         ret = ctsvc_ipc_marshal_int(range, indata);
1252         if (ret != CONTACTS_ERROR_NONE) {
1253                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1254                 pims_ipc_data_destroy(indata);
1255                 return ret;
1256         }
1257         ret = ctsvc_ipc_marshal_string(start_match ? start_match : CTSVC_DEFAULT_START_MATCH, indata);
1258         if (ret != CONTACTS_ERROR_NONE) {
1259                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1260                 pims_ipc_data_destroy(indata);
1261                 return ret;
1262         }
1263         ret = ctsvc_ipc_marshal_string(end_match ? end_match : CTSVC_DEFAULT_END_MATCH, indata);
1264         if (ret != CONTACTS_ERROR_NONE) {
1265                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1266                 pims_ipc_data_destroy(indata);
1267                 return ret;
1268         }
1269         ret = ctsvc_ipc_marshal_int(token_number, indata);
1270         if (ret != CONTACTS_ERROR_NONE) {
1271                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1272                 pims_ipc_data_destroy(indata);
1273                 return ret;
1274         }
1275
1276         if (0 != ctsvc_ipc_call(CTSVC_IPC_DB_MODULE,
1277                                 CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_RANGE_FOR_SNIPPET,
1278                                 indata, &outdata)) {
1279                 ERR("ctsvc_ipc_call() Fail");
1280                 pims_ipc_data_destroy(indata);
1281                 return CONTACTS_ERROR_IPC;
1282         }
1283
1284         pims_ipc_data_destroy(indata);
1285
1286         if (outdata) {
1287                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1288                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1289                         pims_ipc_data_destroy(outdata);
1290                         return CONTACTS_ERROR_IPC;
1291                 }
1292                 if (CONTACTS_ERROR_NONE == ret) {
1293                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1294                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1295                                 pims_ipc_data_destroy(outdata);
1296                                 return CONTACTS_ERROR_IPC;
1297                         }
1298                 }
1299                 pims_ipc_data_destroy(outdata);
1300         }
1301
1302         return ret;
1303 }
1304
1305 int ctsvc_client_db_search_records_with_query_for_snippet(contacts_h contact,
1306                 contacts_query_h query,
1307                 const char *keyword,
1308                 int offset,
1309                 int limit,
1310                 const char *start_match,
1311                 const char *end_match,
1312                 int token_number,
1313                 contacts_list_h *out_list)
1314 {
1315         int ret = CONTACTS_ERROR_NONE;
1316         pims_ipc_data_h indata = NULL;
1317         pims_ipc_data_h outdata = NULL;
1318
1319         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1320         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1321         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
1322         *out_list = NULL;
1323
1324         /* make indata */
1325         indata = pims_ipc_data_create(0);
1326         if (indata == NULL) {
1327                 ERR("pims_ipc_data_create() Fail");
1328                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1329         }
1330
1331         ret = ctsvc_ipc_marshal_handle(contact, indata);
1332         if (CONTACTS_ERROR_NONE != ret) {
1333                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1334                 pims_ipc_data_destroy(indata);
1335                 return ret;
1336         }
1337         ret = ctsvc_ipc_marshal_query(query, indata);
1338         if (ret != CONTACTS_ERROR_NONE) {
1339                 ERR("ctsvc_ipc_marshal_query() Fail(%d)", ret);
1340                 pims_ipc_data_destroy(indata);
1341                 return ret;
1342         }
1343         ret = ctsvc_ipc_marshal_string(keyword, indata);
1344         if (ret != CONTACTS_ERROR_NONE) {
1345                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1346                 pims_ipc_data_destroy(indata);
1347                 return ret;
1348         }
1349         ret = ctsvc_ipc_marshal_int(offset, indata);
1350         if (ret != CONTACTS_ERROR_NONE) {
1351                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1352                 return ret;
1353         }
1354         ret = ctsvc_ipc_marshal_int(limit, indata);
1355         if (ret != CONTACTS_ERROR_NONE) {
1356                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1357                 pims_ipc_data_destroy(indata);
1358                 return ret;
1359         }
1360         ret = ctsvc_ipc_marshal_string(start_match ? start_match : CTSVC_DEFAULT_START_MATCH, indata);
1361         if (ret != CONTACTS_ERROR_NONE) {
1362                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1363                 pims_ipc_data_destroy(indata);
1364                 return ret;
1365         }
1366         ret = ctsvc_ipc_marshal_string(end_match ? end_match : CTSVC_DEFAULT_END_MATCH, indata);
1367         if (ret != CONTACTS_ERROR_NONE) {
1368                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1369                 pims_ipc_data_destroy(indata);
1370                 return ret;
1371         }
1372         ret = ctsvc_ipc_marshal_int(token_number, indata);
1373         if (ret != CONTACTS_ERROR_NONE) {
1374                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1375                 pims_ipc_data_destroy(indata);
1376                 return ret;
1377         }
1378
1379         /* ipc call */
1380         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE,
1381                                 CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_QUERY_FOR_SNIPPET,
1382                                 indata, &outdata) != 0) {
1383                 ERR("ctsvc_ipc_call() Fail");
1384                 pims_ipc_data_destroy(indata);
1385                 return CONTACTS_ERROR_IPC;
1386         }
1387
1388         pims_ipc_data_destroy(indata);
1389
1390         if (outdata) {
1391                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1392                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1393                         pims_ipc_data_destroy(outdata);
1394                         return CONTACTS_ERROR_IPC;
1395                 }
1396
1397                 if (CONTACTS_ERROR_NONE == ret) {
1398                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1399                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1400                                 pims_ipc_data_destroy(outdata);
1401                                 return CONTACTS_ERROR_IPC;
1402                         }
1403                 }
1404
1405                 pims_ipc_data_destroy(outdata);
1406         }
1407
1408         return ret;
1409 }
1410
1411 int ctsvc_client_db_search_records(contacts_h contact, const char *view_uri, const char *keyword,
1412                 int offset, int limit, contacts_list_h *out_list)
1413 {
1414         int ret = CONTACTS_ERROR_NONE;
1415         pims_ipc_data_h indata = NULL;
1416         pims_ipc_data_h outdata = NULL;
1417
1418         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1419         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1420         *out_list = NULL;
1421
1422         /* make indata */
1423         indata = pims_ipc_data_create(0);
1424         if (indata == NULL) {
1425                 ERR("pims_ipc_data_create() Fail");
1426                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1427         }
1428
1429         ret = ctsvc_ipc_marshal_handle(contact, indata);
1430         if (CONTACTS_ERROR_NONE != ret) {
1431                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1432                 pims_ipc_data_destroy(indata);
1433                 return ret;
1434         }
1435         ret = ctsvc_ipc_marshal_string(view_uri, indata);
1436         if (ret != CONTACTS_ERROR_NONE) {
1437                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1438                 pims_ipc_data_destroy(indata);
1439                 return ret;
1440         }
1441         ret = ctsvc_ipc_marshal_string(keyword, indata);
1442         if (ret != CONTACTS_ERROR_NONE) {
1443                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1444                 pims_ipc_data_destroy(indata);
1445                 return ret;
1446         }
1447         ret = ctsvc_ipc_marshal_int(offset, indata);
1448         if (ret != CONTACTS_ERROR_NONE) {
1449                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1450                 pims_ipc_data_destroy(indata);
1451                 return ret;
1452         }
1453         ret = ctsvc_ipc_marshal_int(limit, indata);
1454         if (ret != CONTACTS_ERROR_NONE) {
1455                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1456                 pims_ipc_data_destroy(indata);
1457                 return ret;
1458         }
1459
1460         /* ipc call */
1461         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS, indata, &outdata) != 0) {
1462                 ERR("ctsvc_ipc_call() Fail");
1463                 pims_ipc_data_destroy(indata);
1464                 return CONTACTS_ERROR_IPC;
1465         }
1466
1467         pims_ipc_data_destroy(indata);
1468
1469         if (outdata) {
1470                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1471                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1472                         pims_ipc_data_destroy(outdata);
1473                         return CONTACTS_ERROR_IPC;
1474                 }
1475
1476                 if (CONTACTS_ERROR_NONE == ret) {
1477                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1478                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1479                                 pims_ipc_data_destroy(outdata);
1480                                 return CONTACTS_ERROR_IPC;
1481                         }
1482                 }
1483
1484                 pims_ipc_data_destroy(outdata);
1485         }
1486
1487         return ret;
1488 }
1489
1490 int ctsvc_client_db_search_records_with_range(contacts_h contact, const char *view_uri, const char *keyword,
1491                 int offset, int limit, int range, contacts_list_h *out_list)
1492 {
1493         int ret = CONTACTS_ERROR_NONE;
1494         pims_ipc_data_h indata = NULL;
1495         pims_ipc_data_h outdata = NULL;
1496
1497         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1498         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1499         RETV_IF(0 == range, CONTACTS_ERROR_INVALID_PARAMETER);
1500         *out_list = NULL;
1501
1502         indata = pims_ipc_data_create(0);
1503         if (indata == NULL) {
1504                 ERR("pims_ipc_data_create() Fail");
1505                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1506         }
1507         ret = ctsvc_ipc_marshal_handle(contact, indata);
1508         if (CONTACTS_ERROR_NONE != ret) {
1509                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1510                 pims_ipc_data_destroy(indata);
1511                 return ret;
1512         }
1513
1514         ret = ctsvc_ipc_marshal_string(view_uri, indata);
1515         if (ret != CONTACTS_ERROR_NONE) {
1516                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1517                 pims_ipc_data_destroy(indata);
1518                 return ret;
1519         }
1520         ret = ctsvc_ipc_marshal_string(keyword, indata);
1521         if (ret != CONTACTS_ERROR_NONE) {
1522                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1523                 pims_ipc_data_destroy(indata);
1524                 return ret;
1525         }
1526         ret = ctsvc_ipc_marshal_int(offset, indata);
1527         if (ret != CONTACTS_ERROR_NONE) {
1528                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1529                 pims_ipc_data_destroy(indata);
1530                 return ret;
1531         }
1532         ret = ctsvc_ipc_marshal_int(limit, indata);
1533         if (ret != CONTACTS_ERROR_NONE) {
1534                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1535                 pims_ipc_data_destroy(indata);
1536                 return ret;
1537         }
1538         ret = ctsvc_ipc_marshal_int(range, indata);
1539         if (ret != CONTACTS_ERROR_NONE) {
1540                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1541                 pims_ipc_data_destroy(indata);
1542                 return ret;
1543         }
1544
1545         if (0 != ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_RANGE, indata, &outdata)) {
1546                 ERR("ctsvc_ipc_call() Fail");
1547                 pims_ipc_data_destroy(indata);
1548                 return CONTACTS_ERROR_IPC;
1549         }
1550
1551         pims_ipc_data_destroy(indata);
1552
1553         if (outdata) {
1554                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1555                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1556                         pims_ipc_data_destroy(outdata);
1557                         return CONTACTS_ERROR_IPC;
1558                 }
1559                 if (CONTACTS_ERROR_NONE == ret) {
1560                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1561                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1562                                 pims_ipc_data_destroy(outdata);
1563                                 return CONTACTS_ERROR_IPC;
1564                         }
1565                 }
1566                 pims_ipc_data_destroy(outdata);
1567         }
1568
1569         return ret;
1570 }
1571
1572 int ctsvc_client_db_search_records_with_query(contacts_h contact, contacts_query_h query, const char *keyword,
1573                 int offset, int limit, contacts_list_h *out_list)
1574 {
1575         int ret = CONTACTS_ERROR_NONE;
1576         pims_ipc_data_h indata = NULL;
1577         pims_ipc_data_h outdata = NULL;
1578
1579         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1580         RETV_IF(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER);
1581         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
1582         *out_list = NULL;
1583
1584         /* make indata */
1585         indata = pims_ipc_data_create(0);
1586         if (indata == NULL) {
1587                 ERR("pims_ipc_data_create() Fail");
1588                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1589         }
1590
1591         ret = ctsvc_ipc_marshal_handle(contact, indata);
1592         if (CONTACTS_ERROR_NONE != ret) {
1593                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1594                 pims_ipc_data_destroy(indata);
1595                 return ret;
1596         }
1597         ret = ctsvc_ipc_marshal_query(query, indata);
1598         if (ret != CONTACTS_ERROR_NONE) {
1599                 ERR("ctsvc_ipc_marshal_query() Fail(%d)", ret);
1600                 pims_ipc_data_destroy(indata);
1601                 return ret;
1602         }
1603         ret = ctsvc_ipc_marshal_string(keyword, indata);
1604         if (ret != CONTACTS_ERROR_NONE) {
1605                 ERR("ctsvc_ipc_marshal_string() Fail(%d)", ret);
1606                 pims_ipc_data_destroy(indata);
1607                 return ret;
1608         }
1609         ret = ctsvc_ipc_marshal_int(offset, indata);
1610         if (ret != CONTACTS_ERROR_NONE) {
1611                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1612                 return ret;
1613         }
1614         ret = ctsvc_ipc_marshal_int(limit, indata);
1615         if (ret != CONTACTS_ERROR_NONE) {
1616                 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
1617                 pims_ipc_data_destroy(indata);
1618                 return ret;
1619         }
1620
1621         /* ipc call */
1622         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_QUERY, indata, &outdata) != 0) {
1623                 ERR("ctsvc_ipc_call() Fail");
1624                 pims_ipc_data_destroy(indata);
1625                 return CONTACTS_ERROR_IPC;
1626         }
1627
1628         pims_ipc_data_destroy(indata);
1629
1630         if (outdata) {
1631                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1632                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1633                         pims_ipc_data_destroy(outdata);
1634                         return CONTACTS_ERROR_IPC;
1635                 }
1636
1637                 if (CONTACTS_ERROR_NONE == ret) {
1638                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_list(outdata, out_list)) {
1639                                 ERR("ctsvc_ipc_unmarshal_list() Fail");
1640                                 pims_ipc_data_destroy(outdata);
1641                                 return CONTACTS_ERROR_IPC;
1642                         }
1643                 }
1644
1645                 pims_ipc_data_destroy(outdata);
1646         }
1647
1648         return ret;
1649 }
1650
1651 int ctsvc_client_db_get_last_change_version(contacts_h contact, int *last_version)
1652 {
1653         int ret = CONTACTS_ERROR_NONE;
1654         bool result = false;
1655
1656         RETV_IF(NULL == last_version, CONTACTS_ERROR_INVALID_PARAMETER);
1657         *last_version = 0;
1658
1659         ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
1660         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_ipc_client_check_permission Fail(%d)", ret);
1661         if (result == false) {
1662                 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_PHONELOG_READ, &result);
1663                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_ipc_client_check_permission Fail(%d)", ret);
1664                 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied");
1665         }
1666
1667         *last_version = ctsvc_client_ipc_get_change_version(contact);
1668         return ret;
1669 }
1670
1671 typedef struct {
1672         contacts_db_status_changed_cb cb;
1673         void *user_data;
1674 } status_callback_info_s;
1675
1676 static GSList *__status_change_subscribe_list = NULL;
1677
1678 static void __ctsvc_client_db_free_cb_info(status_callback_info_s *cb_info)
1679 {
1680         if (NULL == cb_info)
1681                 return;
1682         free(cb_info);
1683 }
1684
1685 static void __ctsvc_db_status_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data,
1686                 void *user_data)
1687 {
1688         int ret;
1689         int status = -1;
1690         GSList *l;
1691
1692         if (data) {
1693                 ret = ctsvc_ipc_unmarshal_int(data, &status);
1694                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
1695         }
1696
1697         for (l = __status_change_subscribe_list; l; l = l->next) {
1698                 status_callback_info_s *cb_info = l->data;
1699                 /* TODO: Fixme - check zone_name */
1700                 if (cb_info->cb)
1701                         cb_info->cb(status, cb_info->user_data);
1702         }
1703 }
1704
1705 int ctsvc_client_db_get_status(contacts_h contact, contacts_db_status_e *status)
1706 {
1707         int ret = CONTACTS_ERROR_NONE;
1708         pims_ipc_data_h outdata = NULL;
1709         pims_ipc_data_h indata = NULL;
1710
1711         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1712         RETV_IF(NULL == status, CONTACTS_ERROR_INVALID_PARAMETER);
1713         *status = 0;
1714
1715         /* make indata */
1716         indata = pims_ipc_data_create(0);
1717         if (indata == NULL) {
1718                 ERR("pims_ipc_data_create() Fail");
1719                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1720         }
1721
1722         ret = ctsvc_ipc_marshal_handle(contact, indata);
1723         if (CONTACTS_ERROR_NONE != ret) {
1724                 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
1725                 pims_ipc_data_destroy(indata);
1726                 return ret;
1727         }
1728
1729         if (ctsvc_ipc_call(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_STATUS, indata, &outdata) != 0) {
1730                 pims_ipc_data_destroy(indata);
1731                 ERR("ctsvc_ipc_call() Fail");
1732                 return CONTACTS_ERROR_IPC;
1733         }
1734         pims_ipc_data_destroy(indata);
1735
1736         if (outdata) {
1737                 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
1738                         ERR("ctsvc_ipc_unmarshal_int() Fail");
1739                         pims_ipc_data_destroy(outdata);
1740                         return CONTACTS_ERROR_IPC;
1741                 }
1742                 if (CONTACTS_ERROR_NONE == ret) {
1743                         if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, (int*)status)) {
1744                                 ERR("ctsvc_ipc_unmarshal_int() Fail");
1745                                 pims_ipc_data_destroy(outdata);
1746                                 return CONTACTS_ERROR_IPC;
1747                         }
1748                 }
1749                 pims_ipc_data_destroy(outdata);
1750         }
1751
1752         return ret;
1753 }
1754
1755 int ctsvc_client_db_add_status_changed_cb(contacts_h contact,
1756                 contacts_db_status_changed_cb cb, void *user_data)
1757 {
1758         int ret;
1759         status_callback_info_s *cb_info = NULL;
1760
1761         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1762         RETV_IF(NULL == cb, CONTACTS_ERROR_INVALID_PARAMETER);
1763
1764         ret = ctsvc_ipc_create_for_change_subscription();
1765         if (CONTACTS_ERROR_NONE != ret) {
1766                 ERR("ctsvc_ipc_create_for_change_subscription() Fail(%d)", ret);
1767                 return ret;
1768         }
1769
1770         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1771
1772         if (pims_ipc_subscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
1773                                 CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_IPC_SERVER_DB_STATUS_CHANGED,
1774                                 __ctsvc_db_status_subscriber_callback, NULL) != 0) {
1775                 ERR("pims_ipc_subscribe error\n");
1776                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1777                 return CONTACTS_ERROR_IPC;
1778         }
1779
1780         cb_info = calloc(1, sizeof(status_callback_info_s));
1781         if (NULL == cb_info) {
1782                 ERR("calloc() Fail");
1783                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1784                 return CONTACTS_ERROR_OUT_OF_MEMORY;
1785         }
1786         cb_info->user_data = user_data;
1787         cb_info->cb = cb;
1788         __status_change_subscribe_list = g_slist_append(__status_change_subscribe_list, cb_info);
1789
1790         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1791         return CONTACTS_ERROR_NONE;
1792 }
1793
1794 int ctsvc_client_db_remove_status_changed_cb(contacts_h contact,
1795                 contacts_db_status_changed_cb cb, void *user_data)
1796 {
1797         int ret;
1798         GSList *l;
1799
1800         RETV_IF(NULL == contact, CONTACTS_ERROR_INVALID_PARAMETER);
1801         RETV_IF(NULL == cb, CONTACTS_ERROR_INVALID_PARAMETER);
1802
1803         ret = ctsvc_ipc_destroy_for_change_subscription(false);
1804         if (CONTACTS_ERROR_NONE != ret) {
1805                 ERR("ctsvc_ipc_destroy_for_change_subscription() Fail(%d)", ret);
1806                 return ret;
1807         }
1808
1809         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1810         for (l = __status_change_subscribe_list; l; l = l->next) {
1811                 status_callback_info_s *cb_info = l->data;
1812                 if (cb == cb_info->cb && user_data == cb_info->user_data) {
1813                         __status_change_subscribe_list = g_slist_remove(__status_change_subscribe_list, cb_info);
1814                         __ctsvc_client_db_free_cb_info(cb_info);
1815                         break;
1816                 }
1817         }
1818
1819         if (g_slist_length(__status_change_subscribe_list) == 0) {
1820                 pims_ipc_unsubscribe(ctsvc_ipc_get_handle_for_change_subsciption(),
1821                                 CTSVC_IPC_SUBSCRIBE_MODULE, CTSVC_IPC_SERVER_DB_STATUS_CHANGED);
1822                 g_slist_free(__status_change_subscribe_list);
1823                 __status_change_subscribe_list = NULL;
1824         }
1825
1826         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
1827         return CONTACTS_ERROR_NONE;
1828 }