edfbd8eefbc2051a41edeab5f4322551453f0398
[platform/core/system/sync-agent.git] / src / fw-plugins / common-public / contact / src / plugin_interface.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <pthread.h>
23
24 #include <contacts.h>
25 #include <account.h>
26
27 #include "extern_info.h"
28 #include "item_change_info.h"
29 #include "ext_datastore_info_contact.h"
30 #include "in_datastore_info_contact.h"
31
32 /* for log */
33 #include "utility/sync_util.h"
34 #include "utility/fw_async_queue.h"
35
36 #include "plugin/data_connector_interface.h"
37
38 #ifndef EXPORT_API
39 #define EXPORT_API __attribute__ ((visibility("default")))
40 #endif
41
42 #ifndef SYNC_AGENT_LOG
43 #undef LOG_TAG
44 #define LOG_TAG "PLUGIN_DA_CONTACT"
45 #endif
46
47 #define FW_CONTACT              0       /* plugIn-Id */
48
49 static int latest_contact_version;
50
51 static sync_agent_util_async_queue_s *queue;
52
53 static pthread_mutex_t lockx;
54
55 static int is_noti_from_me = 0;
56
57 static int is_storage_changed = 0;
58
59 static sync_agent_add_item_cb_plugin callback_add_item;
60
61 static sync_agent_del_item_cb_plugin callback_del_item;
62
63 static sync_agent_update_item_cb_plugin callback_update_item;
64
65 static sync_agent_get_account_id_list_cb_plugin callback_get_account_id_list;
66
67 static void _set_is_noti_from_me(int set_flag);
68
69 static int _get_is_noti_from_me(void);
70
71 //static void _process_contact_change(void *);
72 static void _process_contact_change(const char *view_uri, void *user_data);
73
74 static void *_rutine_contact_change(void *);
75
76 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err);
77
78 static bool _get_account_id(account_h account, void *user_data);
79
80 static bool __is_contact_id(/*account_capability_type_e*/const char* capability_type, account_capability_state_e capability_value, void *user_data);
81
82 static void _contacts_db_result_cb(int error, void *user_data);
83
84 typedef struct account_id {
85         int account_id;
86         struct account_id *next;
87 } account_id_s;
88
89 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service(void)
90 {
91         _EXTERN_FUNC_ENTER;
92
93 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
94 //      int err = contacts_svc_connect();
95 //      if (err < CTS_SUCCESS) {
96 //              _DEBUG_INFO("[da_contact_plugIn] in contact Fail!\n");
97 //              ret = _convert_service_error_to_common_error(err);
98 //      } else {
99 //              _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
100 //      }
101
102         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
103         contacts_error_e err = contacts_connect2();
104         if (err != CONTACTS_ERROR_NONE) {
105                 _DEBUG_ERROR("[da_contact_plugIn] in contact Fail!\n");
106                 ret = _convert_service_error_to_common_error(err);
107         } else {
108                 _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
109         }
110
111         _EXTERN_FUNC_EXIT;
112
113         return ret;
114 }
115
116 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service(void)
117 {
118         _EXTERN_FUNC_ENTER;
119
120 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
121 //      int err = contacts_svc_disconnect();
122 //      if (err < CTS_SUCCESS) {
123 //              _DEBUG_INFO("[da_contact_plugIn] in contact Fail!\n");
124 //              ret = _convert_service_error_to_common_error(err);
125 //      } else {
126 //              _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
127 //      }
128
129         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
130         contacts_error_e err = contacts_disconnect2();
131         if (err != CONTACTS_ERROR_NONE) {
132                 _DEBUG_ERROR("[da_contact_plugIn] in contact Fail!\n");
133                 ret = _convert_service_error_to_common_error(err);
134         } else {
135                 _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
136         }
137
138         _EXTERN_FUNC_EXIT;
139
140         return ret;
141 }
142
143 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction(void)
144 {
145         _EXTERN_FUNC_ENTER;
146
147         _DEBUG_INFO("[da_contact_plugIn] Start Begin_Transaction\n");
148
149         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
150 //      int err = contacts_svc_begin_trans();
151 //      if (err < CTS_SUCCESS) {
152 //              _DEBUG_ERROR("[da_contact_plugIn] in contact Fail!\n");
153 //              ret = _convert_service_error_to_common_error(err);
154 //      } else {
155 //              _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
156         _set_is_noti_from_me(1);
157 //      }
158
159         _DEBUG_INFO("[da_contact_plugIn] End Begin_Transaction\n");
160
161         _EXTERN_FUNC_EXIT;
162
163         return ret;
164 }
165
166 EXPORT_API sync_agent_da_return_e sync_agent_plugin_end_transaction(int is_success)
167 {
168         _EXTERN_FUNC_ENTER;
169
170         _DEBUG_INFO("[da_contact_plugIn] Start End_Transaction\n");
171
172         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
173 //      int err = contacts_svc_end_trans(is_success);
174 //      if (err < CTS_SUCCESS) {
175 //              _DEBUG_ERROR("[da_contact_plugIn] in contact Fail!");
176 //              ret = _convert_service_error_to_common_error(err);
177 //      } else {
178 //              _DEBUG_INFO("[da_contact_plugIn] in contact Success!");
179         if (is_storage_changed == 0) {
180                 _DEBUG_INFO("contact storaged was not Changed!!");
181                 _set_is_noti_from_me(0);
182         }
183         is_storage_changed = 0;
184 //      }
185
186         _DEBUG_INFO("[da_contact_plugIn] End End_Transaction\n");
187
188         _EXTERN_FUNC_EXIT;
189
190         return ret;
191 }
192
193 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
194 {
195         _EXTERN_FUNC_ENTER;
196
197 //      retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
198 //
199 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
200 //      int err = 0;
201 //      CTSstruct *item = (CTSstruct *)data;
202 //
203 //      /*  add item */
204 //      err = contacts_svc_insert_contact(atoi(folder_id), (CTSstruct *)item);
205 //      if (err < CTS_SUCCESS) {
206 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_insert_contact() Fail!\n");
207 //              ret = _convert_service_error_to_common_error(err);
208 //              *item_id = 0;
209 //      } else {
210 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_insert_contact() Success!\n");
211 //              *item_id = g_strdup_printf("%d", err);
212 //              is_storage_changed = 1;
213 //      }
214 //
215 //      /*  memory free */
216 //      if (item != NULL)
217 //              contacts_svc_struct_free(item);
218
219         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
220
221         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
222         contacts_error_e err = CONTACTS_ERROR_NONE;
223         contacts_record_h record = (contacts_record_h) data;
224
225         int contact_id = 0;
226
227         int int_folder_id = atoi(folder_id);
228         _DEBUG_INFO("[da_contact_plugIn] folder id [%s][%d]", folder_id, int_folder_id);
229
230         /*set folder id */
231         err = contacts_record_set_int(record, _contacts_contact.address_book_id, int_folder_id);
232         if (err != CONTACTS_ERROR_NONE) {
233                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_set_int Fail\n");
234                 ret = _convert_service_error_to_common_error(err);
235                 goto DACI_FINISH;
236         }
237
238         _DEBUG_INFO("[da_contact_plugIn] insert");
239
240         /*  add item */
241         err = contacts_db_insert_record(record, &contact_id);
242         if (err != CONTACTS_ERROR_NONE) {
243                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_record() Fail!\n");
244                 ret = _convert_service_error_to_common_error(err);
245                 *item_id = 0;
246         } else {
247                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_insert_record() Success!\n");
248                 _DEBUG_INFO("contact_id = %d", contact_id);
249                 *item_id = g_strdup_printf("%d", contact_id);
250                 is_storage_changed = 1;
251         }
252
253  DACI_FINISH:
254
255         contacts_record_destroy(record, true);
256
257         _EXTERN_FUNC_EXIT;
258
259         return ret;
260 }
261
262 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_bulk_item(int account_id, char *folder_id, void *data, int **item_id, int *item_id_count)
263 {
264         _EXTERN_FUNC_ENTER;
265
266         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
267
268         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
269         contacts_error_e contacts_err = CONTACTS_ERROR_NONE;
270         contacts_record_h record = NULL;
271         contacts_list_h list = NULL;
272
273         contacts_err = contacts_list_create(&list);
274         if (contacts_err != CONTACTS_ERROR_NONE) {
275                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_create() Fail, contacts_err[%d]", contacts_err);
276                 ret = SYNC_AGENT_DA_ERRORS;
277                 goto DACI_FINISH;
278         }
279
280         int count = g_list_length((GList *)data);
281
282         int i = 0;
283         for (i = 0; i < count; ++i) {
284                 record = (contacts_record_h)g_list_nth_data(data, i);
285                 contacts_err = contacts_list_add(list, record);
286                 if (contacts_err != CONTACTS_ERROR_NONE) {
287                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_add() fail, contacts_err[%d]", contacts_err);
288                         ret = SYNC_AGENT_DA_ERRORS;
289                         goto DACI_FINISH;
290                 }
291         }
292
293         unsigned int id_count = 0;
294         int *ids = NULL;
295         contacts_err = contacts_db_insert_records(list, &ids, &id_count);
296         if (contacts_err != CONTACTS_ERROR_NONE) {
297                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_records() Fail, contacts_err[%d]", contacts_err);
298                 ret = SYNC_AGENT_DA_ERRORS;
299                 goto DACI_FINISH;
300         }
301         _DEBUG_INFO("Inputed data's count = [%d], Saved data's count = [%d]", count, id_count);
302         if (count != id_count) {
303                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_records() API wesn't create all contact data.");
304                 ret = SYNC_AGENT_DA_ERRORS;
305                 goto DACI_FINISH;
306         }
307
308 /*
309         //for test
310         for (i = 0; i < count; ++i) {
311                 _DEBUG_INFO("[da_contact_plugIn] ids[%d] of [%d]", ids[i], id_count);
312         }
313 */
314
315         is_storage_changed = 1;
316
317         *item_id = ids;
318         *item_id_count = id_count;
319
320 DACI_FINISH:
321         _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
322
323         if (list != NULL) {
324                 contacts_err = contacts_list_destroy(list, true);
325                 if (contacts_err != CONTACTS_ERROR_NONE) {
326                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_destroy() fail, contacts_err[%d]", contacts_err);
327                 }
328                 list = NULL;
329         }
330
331         _EXTERN_FUNC_EXIT;
332         return ret;
333 }
334
335 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
336 {
337         _EXTERN_FUNC_ENTER;
338
339         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] folder_id is NULL. FAIL !!!");
340         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
341
342         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
343         contacts_error_e err = CONTACTS_ERROR_NONE;
344         contacts_record_h record = (contacts_record_h) data;
345
346         /*  update item */
347         err = contacts_db_update_record(record);
348         if (err != CONTACTS_ERROR_NONE) {
349                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_update_record() Fail!\n");
350                 ret = _convert_service_error_to_common_error(err);
351         } else {
352                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_update_record() Success!\n");
353                 is_storage_changed = 1;
354         }
355
356         contacts_record_destroy(record, true);
357
358         _EXTERN_FUNC_EXIT;
359         return ret;
360 }
361
362 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_bulk_item(void *data)
363 {
364         _EXTERN_FUNC_ENTER;
365
366         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
367         contacts_error_e contacts_ret = CONTACTS_ERROR_NONE;
368         contacts_record_h record = NULL;
369         contacts_list_h list = NULL;
370
371         contacts_ret = contacts_list_create(&list);
372         if (contacts_ret != CONTACTS_ERROR_NONE) {
373                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_create() fail, contacts_ret[%d]", contacts_ret);
374                 return SYNC_AGENT_DA_ERRORS;
375         }
376
377         int i = 0;
378         for (i = 0; i < g_list_length((GList *)data); ++i) {
379                 record = (contacts_record_h)g_list_nth_data(data, i);
380                 contacts_ret = contacts_list_add(list, record);
381                 if (contacts_ret != CONTACTS_ERROR_NONE) {
382                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_add() fail, contacts_ret[%d]", contacts_ret);
383                         da_ret = SYNC_AGENT_DA_ERRORS;
384                         goto DACI_FINISH;
385                 }
386         }
387
388         contacts_ret = contacts_db_update_records(list);
389         if (contacts_ret != CONTACTS_ERROR_NONE) {
390                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_update_records() fail, contacts_ret[%d]", contacts_ret);
391                 da_ret = SYNC_AGENT_DA_ERRORS;
392                 goto DACI_FINISH;
393         }
394         is_storage_changed = 1;
395
396 DACI_FINISH:
397         _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
398
399         if (list != NULL) {
400                 contacts_ret = contacts_list_destroy(list, true);
401                 if (contacts_ret != CONTACTS_ERROR_NONE) {
402                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_destroy() fail, contacts_ret[%d]", contacts_ret);
403                         da_ret = SYNC_AGENT_DA_ERRORS;
404                 }
405                 list = NULL;
406         }
407
408         _EXTERN_FUNC_EXIT;
409         return da_ret;
410 }
411
412 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
413 {
414         _EXTERN_FUNC_ENTER;
415
416         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
417
418         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
419         contacts_error_e err = CONTACTS_ERROR_NONE;
420
421         err = contacts_db_delete_record(_contacts_contact._uri, atoi(item_id));
422         if (err != CONTACTS_ERROR_NONE) {
423                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_delete_record(%s) Fail!\n", item_id);
424                 ret = _convert_service_error_to_common_error(err);
425         } else {
426                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_record(%s) Success!\n", item_id);
427                 is_storage_changed = 1;
428         }
429
430         _EXTERN_FUNC_EXIT;
431
432         return ret;
433 }
434
435 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_bulk_item(int *item_id, int count)
436 {
437         _EXTERN_FUNC_ENTER;
438
439         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
440         retvm_if(count == 0, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] count is 0. FAIL !!!");
441
442         sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
443         contacts_error_e contacts_err = CONTACTS_ERROR_NONE;
444
445         contacts_err = contacts_db_delete_records(_contacts_contact._uri, item_id, count);
446         if (contacts_err != CONTACTS_ERROR_NONE) {
447                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_delete_records(%s) fail, contacts_err[%d]", *item_id, contacts_err);
448                 da_ret = _convert_service_error_to_common_error(contacts_err);
449         } else {
450                 is_storage_changed = 1;
451         }
452
453         _EXTERN_FUNC_EXIT;
454         return da_ret;
455 }
456
457 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
458 {
459         _EXTERN_FUNC_ENTER;
460
461 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
462 //      int err = 0;
463 //      int folder_id_list_cnt = 0;
464 //      int *folder_type_list = 0;
465 //      char **folder_id_list = 0;
466 //      CTSiter *iter = 0;
467 //      CTSvalue *item = 0;
468 //      int item_id = 0;
469 //
470 //      /* get folder id list for account id */
471 //      folder_id_list = sync_agent_plugin_get_folder_id_list(account_id, &folder_id_list_cnt, &folder_type_list);
472 //      if (folder_id_list == NULL) {
473 //              _DEBUG_INFO("[da_contact_plugIn] folder id list cnt : %d\n", folder_id_list_cnt);
474 //              return SYNC_AGENT_DA_ERR_NO_DATA;
475 //      }
476 //
477 //      int cnt = 0;
478 //      for (; cnt < folder_id_list_cnt; cnt++) {
479 //              /* get item id list (iter) for each folder id */
480 //              err = contacts_svc_get_list_with_int(CTS_LIST_MEMBERS_OF_ADDRESSBOOK_ID, atoi(folder_id_list[cnt]), &iter);
481 //
482 //              if (err == CTS_SUCCESS) {
483 //                      _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_list_with_int Success!! \n");
484 //
485 //                      while (contacts_svc_iter_next(iter) == CTS_SUCCESS) {
486 //                              /* get item */
487 //                              item = contacts_svc_iter_get_info(iter);
488 //                              if (item != NULL) {
489 //                                      item_id = contacts_svc_value_get_int(item, CTS_LIST_CONTACT_ID_INT);
490 //                                      _DEBUG_INFO("[da_contact_plugIn] item id : %d\n", item_id);
491 //
492 //                                      /* delete item */
493 //                                      err = contacts_svc_delete_contact(item_id);
494 //                                      if (err != CTS_SUCCESS) {
495 //                                              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_contact(%d) Fail!\n", item_id);
496 //                                              ret = _convert_service_error_to_common_error(err);
497 //                                              goto DACI_FINISH;
498 //                                      } else {
499 //                                              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_contact(%d) Success!\n", item_id);
500 //                                              is_storage_changed = 1;
501 //                                      }
502 //
503 //                                      /* memory free */
504 //                                      contacts_svc_value_free(item);
505 //                                      item = 0;
506 //                                      item_id = 0;
507 //                              }
508 //                      }       /* end while */
509 //
510 //                      /* memory free */
511 //                      if (iter != NULL) {
512 //                              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
513 //                              contacts_svc_iter_remove(iter);
514 //                              iter = 0;
515 //                      }
516 //
517 //              } else {
518 //                      _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_list_with_int Fail !! \n");
519 //                      ret = _convert_service_error_to_common_error(err);
520 //                      goto DACI_FINISH;
521 //              }
522 //      }
523 //
524 //DACI_FINISH:
525 //
526 //      /* memory free */
527 //      if (item != NULL)
528 //              contacts_svc_value_free(item);
529 //
530 //      if (iter != NULL) {
531 //              contacts_svc_iter_remove(iter);
532 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
533 //      }
534 //      if (folder_id_list != NULL) {
535 //              /*
536 //                 for (cnt = 0; cnt < folder_id_list_cnt; cnt++) {
537 //                 if (folder_id_list[cnt] != NULL) {
538 //                 _DEBUG_INFO("folder_id_list[%d] : %s\n", cnt, folder_id_list[cnt]);
539 //                 free(folder_id_list[cnt]);
540 //                 _DEBUG_INFO("[da_calendar_plugIn] folder id free !! \n");
541 //                 }
542 //                 }
543 //               */
544 //              free(folder_id_list);
545 //              _DEBUG_INFO("[da_contact_plugIn] folder id list free !! \n");
546 //      }
547 //      if (folder_type_list != NULL) {
548 //              free(folder_type_list);
549 //              _DEBUG_INFO("[da_contact_plugIn] folder type list free !! \n");
550 //      }
551
552         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
553         int folder_id_list_cnt = 0;
554         int *folder_type_list = 0;
555         char **folder_id_list = 0;
556         int item_id = 0;
557
558         contacts_error_e err = CONTACTS_ERROR_NONE;
559         contacts_query_h query = NULL;
560         contacts_filter_h filter = NULL;
561         contacts_list_h list = NULL;
562         contacts_record_h record = NULL;
563
564         int count = 0;
565         int index = 0;
566         int *contact_id_list = NULL;
567
568         /* get folder id list for account id */
569         folder_id_list = sync_agent_plugin_get_folder_id_list(account_id, &folder_id_list_cnt, &folder_type_list);
570         if (folder_id_list == NULL) {
571                 _DEBUG_ERROR("[da_contact_plugIn] folder id list cnt : %d\n", folder_id_list_cnt);
572                 return SYNC_AGENT_DA_ERR_NO_DATA;
573         }
574
575         int cnt = 0;
576         for (; cnt < folder_id_list_cnt; cnt++) {
577                 err = contacts_query_create(_contacts_contact_grouprel._uri, &query);
578                 if (err != CONTACTS_ERROR_NONE) {
579                         _DEBUG_ERROR("[da_contact_plugIn] contacts_query_create Fail\n");
580                         ret = _convert_service_error_to_common_error(err);
581                         goto DACI_FINISH;
582                 }
583
584                 err = contacts_filter_create(_contacts_contact_grouprel._uri, &filter);
585                 if (err != CONTACTS_ERROR_NONE) {
586                         _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_create Fail\n");
587                         ret = _convert_service_error_to_common_error(err);
588                         goto DACI_FINISH;
589                 }
590
591                 err = contacts_filter_add_int(filter, _contacts_contact_grouprel.address_book_id, CONTACTS_MATCH_EQUAL, atoi(folder_id_list[cnt]));
592                 if (err != CONTACTS_ERROR_NONE) {
593                         _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_set_int Fail\n");
594                         ret = _convert_service_error_to_common_error(err);
595                         goto DACI_FINISH;
596                 }
597
598                 err = contacts_query_set_filter(query, filter);
599                 if (err != CONTACTS_ERROR_NONE) {
600                         _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_filter Fail\n");
601                         ret = _convert_service_error_to_common_error(err);
602                         goto DACI_FINISH;
603                 }
604
605                 err = contacts_db_get_count_with_query(query, &count);
606                 if (err != CONTACTS_ERROR_NONE) {
607                         _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_count_with_query Fail\n");
608                         ret = _convert_service_error_to_common_error(err);
609                         goto DACI_FINISH;
610                 }
611                 _DEBUG_INFO("count = %d", count);
612
613                 if (count > 0) {
614
615                         err = contacts_db_get_records_with_query(query, 0, 0, &list);
616                         if (err == CONTACTS_ERROR_NONE) {
617                                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_records_with_query Success!! \n");
618
619                                 index = 0;
620                                 contact_id_list = (int *)calloc(count, sizeof(int));
621
622                                 err = contacts_list_first(list);
623                                 while (err == CONTACTS_ERROR_NONE) {
624
625                                         err = contacts_list_get_current_record_p(list, &record);
626                                         if (err != CONTACTS_ERROR_NONE) {
627                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
628                                                 ret = _convert_service_error_to_common_error(err);
629                                                 goto DACI_FINISH;
630                                         }
631
632                                         err = contacts_record_get_int(record, _contacts_contact_grouprel.contact_id, &item_id);
633                                         if (err != CONTACTS_ERROR_NONE) {
634                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
635                                                 ret = _convert_service_error_to_common_error(err);
636                                                 goto DACI_FINISH;
637                                         }
638
639                                         contact_id_list[index] = item_id;
640                                         _DEBUG_INFO("[da_contact_plugIn] contact_id_list[%d]: %d\n", index, contact_id_list[index]);
641
642                                         index++;
643                                         item_id = 0;
644
645                                         err = contacts_list_next(list);
646                                 }       /* end while */
647
648                                 int i = 0;
649                                 for (; i < count; i++) {
650                                         _DEBUG_INFO("%d = %d", i, contact_id_list[i]);
651                                 }
652
653                                 /* delete items */
654                                 err = contacts_db_delete_records_async(_contacts_contact._uri, contact_id_list, count, _contacts_db_result_cb, NULL);
655                                 if (err != CONTACTS_ERROR_NONE) {
656                                         _DEBUG_ERROR("[da_contact_plugIn] contacts_db_delete_records_async() Fail");
657                                         ret = _convert_service_error_to_common_error(err);
658                                         goto DACI_FINISH;
659                                 } else {
660                                         _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_records_async() Success");
661                                         is_storage_changed = 1;
662                                 }
663
664                                 if (contact_id_list != NULL) {
665                                         free(contact_id_list);
666                                         contact_id_list = NULL;
667                                 }
668
669                         } else {
670                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_records_with_query Fail !! \n");
671                                 ret = _convert_service_error_to_common_error(err);
672                                 goto DACI_FINISH;
673                         }
674
675                         count = 0;
676                         contacts_list_destroy(list, true);
677                         list = NULL;
678                 }
679
680                 contacts_filter_destroy(filter);
681                 filter = NULL;
682
683                 contacts_query_destroy(query);
684                 query = NULL;
685         }
686
687  DACI_FINISH:
688
689         contacts_filter_destroy(filter);
690         contacts_query_destroy(query);
691         contacts_list_destroy(list, true);
692
693         if (folder_id_list != NULL) {
694                 free(folder_id_list);
695                 _DEBUG_INFO("[da_contact_plugIn] folder id list free !! \n");
696         }
697         if (folder_type_list != NULL) {
698                 free(folder_type_list);
699                 _DEBUG_INFO("[da_contact_plugIn] folder type list free !! \n");
700         }
701         if (contact_id_list != NULL) {
702                 free(contact_id_list);
703                 contact_id_list = NULL;
704         }
705
706         _EXTERN_FUNC_EXIT;
707
708         return ret;
709 }
710
711 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
712 {
713         _EXTERN_FUNC_ENTER;
714
715 //      retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
716 //
717 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
718 //      int err = 0;
719 //      CTSstruct *item = 0;
720 //
721 //      /* get item */
722 //      int contact_id = atoi(item_id);
723 //      _DEBUG_INFO("[da_contact_plugIn] contact_id : %d\n", contact_id);
724 //      err = contacts_svc_get_contact(contact_id, &item);
725 //      if (err < CTS_SUCCESS) {
726 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_contact() Fail!\n");
727 //              ret = _convert_service_error_to_common_error(err);
728 //              *data = 0;
729 //      } else {
730 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_contact() Success!\n");
731 //              *data = (void *)item;
732 //      }
733
734         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
735
736         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
737         contacts_error_e err = CONTACTS_ERROR_NONE;
738         contacts_record_h record = NULL;
739
740         /* get item */
741         int contact_id = atoi(item_id);
742         _DEBUG_INFO("[da_contact_plugIn] contact_id : %d\n", contact_id);
743         err = contacts_db_get_record(_contacts_contact._uri, contact_id, &record);
744         if (err != CONTACTS_ERROR_NONE) {
745                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_record() Fail!\n");
746                 ret = _convert_service_error_to_common_error(err);
747                 *data = 0;
748         } else {
749                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_record() Success!\n");
750                 *data = (void *)record;
751         }
752
753         _EXTERN_FUNC_EXIT;
754
755         return ret;
756 }
757
758 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_folder(int account_id, char *folder_name, int folder_type, char **folder_id)
759 {
760         _EXTERN_FUNC_ENTER;
761
762         retvm_if(folder_name == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_name is NULL. FAIL !!!");
763
764 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
765 //      int err = 0;
766 //      CTSvalue *folder = 0;
767 //
768 //      /* new service struct */
769 //      folder = contacts_svc_value_new(CTS_VALUE_ADDRESSBOOK);
770 //
771 //      /* setting folder data */
772 //      contacts_svc_value_set_int(folder, CTS_ADDRESSBOOK_VAL_ACC_ID_INT, account_id);
773 //      /*              change value in contacts service :   CTS_ADDRESSBOOK_TYPE_NONE --> CTS_ADDRESSBOOK_TYPE_OTHER
774 //       *
775 //       contacts_svc_value_set_int(folder, CTS_ADDRESSBOOK_VAL_ACC_TYPE_INT, CTS_ADDRESSBOOK_TYPE_NONE);
776 //       */
777 //      //contacts_svc_value_set_int(folder, CTS_ADDRESSBOOK_VAL_ACC_TYPE_INT, CTS_ADDRESSBOOK_TYPE_OTHER);
778 //      contacts_svc_value_set_int(folder, CTS_ADDRESSBOOK_VAL_MODE_INT, CTS_ADDRESSBOOK_MODE_NONE);
779 //      contacts_svc_value_set_str(folder, CTS_ADDRESSBOOK_VAL_NAME_STR, folder_name);
780 //
781 //      /* add folder */
782 //      err = contacts_svc_insert_addressbook(folder);
783 //      if (err < CTS_SUCCESS) {
784 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_insert_addressbook() Fail!\n");
785 //              ret = _convert_service_error_to_common_error(err);
786 //              *folder_id = 0;
787 //      } else {
788 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_insert_addressbook() Success!\n");
789 //              *folder_id = g_strdup_printf("%d", err);
790 //              is_storage_changed = 1;
791 //      }
792 //
793 //      /*  memory free */
794 //      if (folder != NULL)
795 //              contacts_svc_value_free(folder);
796
797         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
798         contacts_error_e err = CONTACTS_ERROR_NONE;
799         contacts_record_h record = NULL;
800         int contact_id = 0;
801
802         err = contacts_record_create(_contacts_address_book._uri, &record);
803         if (err != CONTACTS_ERROR_NONE) {
804                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_create Fail\n");
805                 ret = _convert_service_error_to_common_error(err);
806                 folder_id = 0;
807                 return ret;
808         }
809
810         err = contacts_record_set_int(record, _contacts_address_book.account_id, account_id);
811         if (err != CONTACTS_ERROR_NONE) {
812                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_set_int Fail\n");
813                 ret = _convert_service_error_to_common_error(err);
814                 goto DACI_FINISH;
815         }
816
817         err = contacts_record_set_int(record, _contacts_address_book.mode, CONTACTS_ADDRESS_BOOK_MODE_NONE);
818         if (err != CONTACTS_ERROR_NONE) {
819                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_set_int Fail\n");
820                 ret = _convert_service_error_to_common_error(err);
821                 goto DACI_FINISH;
822         }
823
824         err = contacts_record_set_str(record, _contacts_address_book.name, folder_name);
825         if (err != CONTACTS_ERROR_NONE) {
826                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_set_str Fail\n");
827                 ret = _convert_service_error_to_common_error(err);
828                 goto DACI_FINISH;
829         }
830
831         err = contacts_db_insert_record(record, &contact_id);
832         if (err != CONTACTS_ERROR_NONE) {
833                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_record() Fail!\n");
834                 ret = _convert_service_error_to_common_error(err);
835                 *folder_id = 0;
836         } else {
837                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_insert_record() Success!\n");
838                 _DEBUG_INFO("[da_contact_plugIn] contact_id = %d", contact_id);
839                 int folder = 0;
840                 err = contacts_record_get_int(record, _contacts_address_book.id, &folder);
841                 if (err != CONTACTS_ERROR_NONE) {
842                         _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
843                         ret = _convert_service_error_to_common_error(err);
844                         goto DACI_FINISH;
845                 }
846
847                 *folder_id = g_strdup_printf("%d", folder);
848                 is_storage_changed = 1;
849         }
850
851  DACI_FINISH:
852
853         contacts_record_destroy(record, true);
854
855         _EXTERN_FUNC_EXIT;
856
857         return ret;
858 }
859
860 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
861 {
862         _EXTERN_FUNC_ENTER;
863
864         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
865
866 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
867 //      int err = 0;
868 //
869 //      /* delete folder */
870 //      err = contacts_svc_delete_addressbook(atoi(folder_id));
871 //      if (err < CTS_SUCCESS) {
872 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_addressbook(%s) Fail!\n", folder_id);
873 //              ret = _convert_service_error_to_common_error(err);
874 //      } else {
875 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_addressbook(%s) Success!\n", folder_id);
876 //              is_storage_changed = 1;
877 //      }
878
879         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
880         contacts_error_e err = CONTACTS_ERROR_NONE;
881
882         /* delete folder */
883         err = contacts_db_delete_record(_contacts_address_book._uri, atoi(folder_id));
884         if (err != CONTACTS_ERROR_NONE) {
885                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_delete_record(%s) Fail!\n", folder_id);
886                 ret = _convert_service_error_to_common_error(err);
887         } else {
888                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_record(%s) Success!\n", folder_id);
889                 is_storage_changed = 1;
890         }
891
892         _EXTERN_FUNC_EXIT;
893
894         return ret;
895 }
896
897 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_folder(int account_id, char *folder_id, char **out_folder_name, int *out_folder_type)
898 {
899         _EXTERN_FUNC_ENTER;
900
901         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
902
903 //      sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
904 //      int err = 0;
905 //      CTSvalue *folder = 0;
906 //
907 //      /* get item */
908 //      int contact_folder_id = atoi(folder_id);
909 //      _DEBUG_INFO("[da_contact_plugIn] contact_folder_id : %d\n", contact_folder_id);
910 //      err = contacts_svc_get_addressbook(contact_folder_id, &folder);
911 //      if (err < CTS_SUCCESS) {
912 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_addressbook() Fail!\n");
913 //              ret = _convert_service_error_to_common_error(err);
914 //              *out_folder_name = 0;
915 //              *out_folder_type = -1;
916 //      } else {
917 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_addressbook() Success!\n");
918 //              const char *folder_name = contacts_svc_value_get_str(folder, CTS_ADDRESSBOOK_VAL_NAME_STR);
919 //              _DEBUG_INFO("out_folder_name : %s", folder_name);
920 //              *out_folder_name = strdup(folder_name);
921 //              *out_folder_type = DEFAULT_CONTACT_FOLDER;
922 //      }
923 //
924 //      /*  memory free */
925 //      if (folder != NULL)
926 //              contacts_svc_value_free(folder);
927
928         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
929         contacts_error_e err = CONTACTS_ERROR_NONE;
930         contacts_record_h record = NULL;
931
932         /* get item */
933         int contact_folder_id = atoi(folder_id);
934         _DEBUG_INFO("[da_contact_plugIn] contact_folder_id : %d\n", contact_folder_id);
935         err = contacts_db_get_record(_contacts_address_book._uri, contact_folder_id, &record);
936         if (err != CONTACTS_ERROR_NONE) {
937                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_record() Fail!\n");
938                 ret = _convert_service_error_to_common_error(err);
939                 *out_folder_name = 0;
940                 *out_folder_type = -1;
941         } else {
942                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_record() Success!\n");
943                 char *folder_name = NULL;
944                 err = contacts_record_get_str(record, _contacts_address_book.name, &folder_name);
945                 if (err != CONTACTS_ERROR_NONE) {
946                         _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_str Fail\n");
947                         ret = _convert_service_error_to_common_error(err);
948                         goto DACI_FINISH;
949                 }
950
951                 _DEBUG_INFO("out_folder_name : %s", folder_name);
952                 *out_folder_name = folder_name;
953                 *out_folder_type = DEFAULT_CONTACT_FOLDER;
954         }
955
956  DACI_FINISH:
957
958         contacts_record_destroy(record, true);
959
960         _EXTERN_FUNC_EXIT;
961
962         return ret;
963 }
964
965 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
966 {
967         _EXTERN_FUNC_ENTER;
968
969         _DEBUG_INFO("[da_contact_plugIn] service is not supported execute. \n");
970         *result = 0;
971
972         _EXTERN_FUNC_EXIT;
973
974         return SYNC_AGENT_DA_SUCCESS;
975 }
976
977 EXPORT_API int sync_agent_plugin_get_used_item_count(void)
978 {
979         _EXTERN_FUNC_ENTER;
980
981         _DEBUG_INFO("[da_contact_plugIn] Start\n");
982
983 //      int ret = SYNC_AGENT_DA_SUCCESS;
984 //      int used_cnt = contacts_svc_count(CTS_GET_ALL_CONTACT);
985 //      if (used_cnt < 0) {
986 //              _DEBUG_INFO("[da_contact_plugIn] contact_svc_count() Fail\n");
987 //              ret = _convert_service_error_to_common_error(used_cnt);
988 //      } else {
989 //              _DEBUG_INFO("[da_contact_plugIn] contact_svc_count() Success\n");
990 //              _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
991 //              ret = used_cnt;
992 //      }
993
994         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
995         contacts_error_e err = CONTACTS_ERROR_NONE;
996         int used_cnt = 0;
997
998         err = contacts_db_get_count(_contacts_contact._uri, &used_cnt);
999         if (err != CONTACTS_ERROR_NONE) {
1000                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_count() Fail\n");
1001                 ret = _convert_service_error_to_common_error(err);
1002         } else {
1003                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_count() Success\n");
1004                 _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
1005         }
1006
1007         _EXTERN_FUNC_EXIT;
1008
1009         return used_cnt;
1010 }
1011
1012 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
1013 {
1014         _EXTERN_FUNC_ENTER;
1015
1016         retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
1017
1018         _DEBUG_INFO("[da_contact_plugIn] Start\n");
1019
1020 //      int ret = SYNC_AGENT_DA_SUCCESS;
1021 //      int used_cnt = contacts_svc_count_with_int(CTS_GET_COUNT_CONTACTS_IN_ADDRESSBOOK, atoi(folder_id));
1022 //      if (used_cnt < 0) {
1023 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_count_with_int(%s) Fail\n", folder_id);
1024 //              ret = _convert_service_error_to_common_error(used_cnt);
1025 //      } else {
1026 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_count_with_int(%s) Success\n", folder_id);
1027 //              _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
1028 //              ret = used_cnt;
1029 //      }
1030
1031         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1032         contacts_error_e err = CONTACTS_ERROR_NONE;
1033         int used_cnt = 0;
1034
1035         contacts_query_h query = NULL;
1036         contacts_filter_h filter = NULL;
1037
1038         err = contacts_query_create(_contacts_contact._uri, &query);
1039         if (err != CONTACTS_ERROR_NONE) {
1040                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_create Fail\n");
1041                 ret = _convert_service_error_to_common_error(err);
1042                 goto DACI_FINISH;
1043         }
1044
1045         err = contacts_filter_create(_contacts_address_book._uri, &filter);
1046         if (err != CONTACTS_ERROR_NONE) {
1047                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_create Fail\n");
1048                 ret = _convert_service_error_to_common_error(err);
1049                 goto DACI_FINISH;
1050         }
1051
1052         err = contacts_filter_add_int(filter, _contacts_address_book.id, CONTACTS_MATCH_EQUAL, atoi(folder_id));
1053         if (err != CONTACTS_ERROR_NONE) {
1054                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_add_int Fail\n");
1055                 ret = _convert_service_error_to_common_error(err);
1056                 goto DACI_FINISH;
1057         }
1058
1059         err = contacts_query_set_filter(query, filter);
1060         if (err != CONTACTS_ERROR_NONE) {
1061                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_filter Fail\n");
1062                 ret = _convert_service_error_to_common_error(err);
1063                 goto DACI_FINISH;
1064         }
1065
1066         err = contacts_db_get_count_with_query(query, &used_cnt);
1067         if (err != CONTACTS_ERROR_NONE) {
1068                 _DEBUG_ERROR("[da_contact_plugIn] contacts_svc_count_with_int(%s) Fail\n", folder_id);
1069                 ret = _convert_service_error_to_common_error(used_cnt);
1070         } else {
1071                 _DEBUG_INFO("[da_contact_plugIn] contacts_svc_count_with_int(%s) Success\n", folder_id);
1072                 _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
1073         }
1074
1075  DACI_FINISH:
1076
1077         contacts_filter_destroy(filter);
1078         contacts_query_destroy(query);
1079
1080         _EXTERN_FUNC_EXIT;
1081
1082         return ret;
1083 }
1084
1085 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
1086 {
1087         _EXTERN_FUNC_ENTER;
1088
1089 //      char **folder_id_list = 0;
1090 //      *folder_count = 0;
1091 //      CTSiter *iter_addressbook = 0;
1092 //      CTSvalue *addressbook = 0;
1093 //
1094 //      if (account_id == -1) {
1095 //              folder_id_list = (char **)calloc(1, sizeof(char *));
1096 //              if( folder_id_list == NULL ) {
1097 //                      _DEBUG_ERROR("CALLOC failed !!!");
1098 //                      return NULL;
1099 //              }
1100 //              *folder_type_list = (int *)calloc(1, sizeof(int));
1101 //              if( *folder_type_list == NULL ) {
1102 //                      _DEBUG_ERROR("CALLOC failed !!!");
1103 //                      return NULL;
1104 //              }
1105 //
1106 //              folder_id_list[0] = "0";
1107 //              *folder_count = 1;
1108 //              (*folder_type_list)[0] = DEFAULT_CONTACT_FOLDER;
1109 //
1110 //              return folder_id_list;
1111 //      }
1112 //
1113 //      folder_id_list = (char **)calloc(50, sizeof(char *));
1114 //      if( folder_id_list == NULL ) {
1115 //              _DEBUG_ERROR("CALLOC failed !!!");
1116 //              return NULL;
1117 //      }
1118 //      *folder_type_list = (int *)calloc(50, sizeof(int));
1119 //      if( *folder_type_list == NULL ) {
1120 //              _DEBUG_ERROR("CALLOC failed !!!");
1121 //              return NULL;
1122 //      }
1123 //
1124 //      int err = contacts_svc_get_list_with_int(CTS_LIST_ADDRESSBOOKS_OF_ACCOUNT_ID, account_id, &iter_addressbook);
1125 //      err = contacts_svc_iter_next(iter_addressbook);
1126 //      while (err == CTS_SUCCESS) {
1127 //              addressbook = contacts_svc_iter_get_info(iter_addressbook);
1128 //              if (addressbook != NULL) {
1129 //                      int addressbook_id = 0;
1130 //                      addressbook_id = contacts_svc_value_get_int(addressbook, CTS_LIST_ADDRESSBOOK_ID_INT);
1131 //                      char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
1132 //                      _DEBUG_INFO("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
1133 //
1134 //                      folder_id_list[*folder_count] = str_addressbook_id;
1135 //                      (*folder_type_list)[*folder_count] = DEFAULT_CONTACT_FOLDER;
1136 //                      *folder_count = *folder_count + 1;
1137 //
1138 //                      if (addressbook != NULL)
1139 //                              contacts_svc_value_free(addressbook);
1140 //
1141 //                      err = contacts_svc_iter_next(iter_addressbook);
1142 //              } else {
1143 //                      _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_get_info() Fail!\n");
1144 //                      break;
1145 //              }
1146 //      }
1147 //
1148 //      /* memory free */
1149 //      if (iter_addressbook != NULL) {
1150 //              contacts_svc_iter_remove(iter_addressbook);
1151 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
1152 //      }
1153
1154         char **folder_id_list = 0;
1155         *folder_count = 0;
1156
1157         contacts_error_e err = CONTACTS_ERROR_NONE;
1158         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1159
1160         contacts_query_h query = NULL;
1161         contacts_filter_h filter = NULL;
1162         contacts_list_h list = NULL;
1163         contacts_record_h record = NULL;
1164
1165         if (account_id == -1) {
1166                 folder_id_list = (char **)calloc(1, sizeof(char *));
1167                 if (folder_id_list == NULL) {
1168                         _DEBUG_ERROR("CALLOC failed !!!");
1169                         return NULL;
1170                 }
1171                 *folder_type_list = (int *)calloc(1, sizeof(int));
1172                 if (*folder_type_list == NULL) {
1173                         _DEBUG_ERROR("CALLOC failed !!!");
1174                         return NULL;
1175                 }
1176
1177                 folder_id_list[0] = "0";
1178                 *folder_count = 1;
1179                 (*folder_type_list)[0] = DEFAULT_CONTACT_FOLDER;
1180
1181                 return folder_id_list;
1182         }
1183
1184         folder_id_list = (char **)calloc(50, sizeof(char *));
1185         if (folder_id_list == NULL) {
1186                 _DEBUG_ERROR("CALLOC failed !!!");
1187                 return NULL;
1188         }
1189         *folder_type_list = (int *)calloc(50, sizeof(int));
1190         if (*folder_type_list == NULL) {
1191                 _DEBUG_ERROR("CALLOC failed !!!");
1192
1193                 if (folder_id_list != NULL)
1194                         free(folder_id_list);
1195
1196                 return NULL;
1197         }
1198
1199         err = contacts_query_create(_contacts_address_book._uri, &query);
1200         if (err != CONTACTS_ERROR_NONE) {
1201                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_create Fail\n");
1202                 ret = _convert_service_error_to_common_error(err);
1203                 goto DACI_FINISH;
1204         }
1205
1206         err = contacts_filter_create(_contacts_address_book._uri, &filter);
1207         if (err != CONTACTS_ERROR_NONE) {
1208                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_create Fail\n");
1209                 ret = _convert_service_error_to_common_error(err);
1210                 goto DACI_FINISH;
1211         }
1212
1213         err = contacts_filter_add_int(filter, _contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, account_id);
1214         if (err != CONTACTS_ERROR_NONE) {
1215                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_set_int Fail\n");
1216                 ret = _convert_service_error_to_common_error(err);
1217                 goto DACI_FINISH;
1218         }
1219
1220         err = contacts_query_set_filter(query, filter);
1221         if (err != CONTACTS_ERROR_NONE) {
1222                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_filter Fail\n");
1223                 ret = _convert_service_error_to_common_error(err);
1224                 goto DACI_FINISH;
1225         }
1226
1227         err = contacts_db_get_records_with_query(query, 0, 0, &list);
1228         if (err == CONTACTS_ERROR_NONE) {
1229                 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_records_with_query Success!! \n");
1230
1231                 err = contacts_list_first(list);
1232                 while (err == CONTACTS_ERROR_NONE) {
1233                         int addressbook_id = 0;
1234
1235                         err = contacts_list_get_current_record_p(list, &record);
1236                         if (err != CONTACTS_ERROR_NONE) {
1237                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
1238                                 ret = _convert_service_error_to_common_error(err);
1239                                 goto DACI_FINISH;
1240                         }
1241
1242                         err = contacts_record_get_int(record, _contacts_address_book.id, &addressbook_id);
1243                         if (err != CONTACTS_ERROR_NONE) {
1244                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
1245                                 ret = _convert_service_error_to_common_error(err);
1246                                 goto DACI_FINISH;
1247                         }
1248
1249                         char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
1250                         _DEBUG_INFO("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
1251
1252                         folder_id_list[*folder_count] = str_addressbook_id;
1253                         (*folder_type_list)[*folder_count] = DEFAULT_CONTACT_FOLDER;
1254                         *folder_count = *folder_count + 1;
1255
1256                         err = contacts_list_next(list);
1257                 }
1258         }
1259
1260  DACI_FINISH:
1261
1262         contacts_filter_destroy(filter);
1263         contacts_query_destroy(query);
1264
1265         contacts_list_destroy(list, true);
1266
1267         _EXTERN_FUNC_EXIT;
1268
1269         return folder_id_list;
1270 }
1271
1272 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
1273 {
1274         _EXTERN_FUNC_ENTER;
1275
1276         _DEBUG_INFO("[da_contact_plugIn] start!!\n");
1277
1278         int error_code = account_connect();
1279
1280         account_id_s *account_info_list = (account_id_s *) calloc(1, sizeof(account_id_s));
1281         if (account_info_list == NULL) {
1282                 _DEBUG_ERROR("CALLOC failed !!!");
1283                 return NULL;
1284         }
1285         account_info_list->account_id = -1;
1286         account_info_list->next = 0;
1287
1288         account_foreach_account_from_db(_get_account_id, account_info_list);
1289
1290         *count = 0;
1291         account_id_s *cursor_ptr = account_info_list;
1292         while (cursor_ptr != 0) {
1293                 cursor_ptr = cursor_ptr->next;
1294                 (*count)++;
1295         }
1296
1297         int *id_list = 0;
1298         if ((*count) > 0) {
1299                 id_list = (int *)calloc(*count, sizeof(int));
1300                 if (id_list == NULL) {
1301                         _DEBUG_ERROR("CALLOC failed !!!");
1302                         if (account_info_list) {
1303                                 free(account_info_list);
1304                                 account_info_list = NULL;
1305                         }
1306                         return NULL;
1307                 }
1308                 cursor_ptr = account_info_list;
1309                 int i = 0;
1310                 while (cursor_ptr != 0) {
1311                         id_list[i] = cursor_ptr->account_id;
1312                         cursor_ptr = cursor_ptr->next;
1313                         i++;
1314                 }
1315         }
1316
1317         free(account_info_list);
1318
1319         error_code = account_disconnect();
1320
1321         _DEBUG_INFO("[da_contact_plugIn] end!!\n");
1322
1323         _EXTERN_FUNC_EXIT;
1324
1325         return id_list;
1326 }
1327
1328 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_add(int account_id, const char *folder_id, int change_point, int *change_count)
1329 {
1330         _EXTERN_FUNC_ENTER;
1331
1332         _DEBUG_INFO("[da_contact_plugIn] account_id = [%d]", account_id);
1333         _DEBUG_INFO("[da_contact_plugIn] folder_id = [%s]", folder_id);
1334         _DEBUG_INFO("[da_contact_plugIn] changepoint = [%d]", change_point);
1335
1336         sync_agent_plugin_item_node_s *root_ptr = 0;
1337         sync_agent_plugin_item_node_s *cursor_ptr = 0;
1338         int chagned_count = 0;
1339         int contact_version = change_point;
1340         int addressbook_id = atoi(folder_id);
1341
1342         contacts_list_h list = NULL;
1343         contacts_record_h record = NULL;
1344         contacts_error_e err = CONTACTS_ERROR_NONE;
1345         int current_contact_version;
1346
1347         err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, &current_contact_version);
1348         if (err != CONTACTS_ERROR_NONE) {
1349                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_changes_by_version() Fail, err[%d]", err);
1350                 goto DACI_FINISH;
1351         }
1352
1353         err = contacts_list_first(list);
1354         if (err != CONTACTS_ERROR_NONE) {
1355                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_first() fail, err[%d]", err);
1356                 goto DACI_FINISH;
1357         }
1358
1359         while (CONTACTS_ERROR_NONE == err) {
1360                 err = contacts_list_get_current_record_p(list, &record);
1361                 if (err != CONTACTS_ERROR_NONE) {
1362                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p() Fail, err[%d]", err);
1363                 } else {
1364                         int type;
1365                         err = contacts_record_get_int(record, _contacts_contact_updated_info.type, &type);
1366                         if (err != CONTACTS_ERROR_NONE) {
1367                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int() Fail, err[%d]", err);
1368                                 goto DACI_FINISH;
1369                         }
1370
1371                         switch (type) {
1372                         case CONTACTS_CHANGE_INSERTED:
1373                                 {
1374                                         int contact_id;
1375                                         err = contacts_record_get_int(record, _contacts_contact_updated_info.contact_id, &contact_id);
1376                                         if (err != CONTACTS_ERROR_NONE) {
1377                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int() Fail, err[%d]", err);
1378                                                 goto DACI_FINISH;
1379                                         }
1380
1381                                         char *contact_id_str = g_strdup_printf("%d", contact_id);
1382                                         _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_INSERTED = [%s]", contact_id_str);
1383
1384                                         if (root_ptr == NULL) {
1385                                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1386                                                 if (root_ptr == NULL) {
1387                                                         _DEBUG_ERROR("[da_contact_plugIn] Calloc failed, root_ptr is NULL");
1388                                                         goto DACI_FINISH;
1389                                                 }
1390                                                 root_ptr->item_id = contact_id_str;
1391                                                 root_ptr->next = 0;
1392                                                 cursor_ptr = root_ptr;
1393                                         } else {
1394                                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1395                                                 if (cursor_ptr->next == NULL) {
1396                                                         _DEBUG_ERROR("[da_contact_plugIn] Calloc failed, cursor_ptr->next is NULL");
1397                                                         root_ptr = NULL;
1398                                                         goto DACI_FINISH;
1399                                                 }
1400                                                 cursor_ptr->next->item_id = contact_id_str;
1401                                                 cursor_ptr->next->next = 0;
1402                                                 cursor_ptr = cursor_ptr->next;
1403                                         }
1404
1405                                         chagned_count++;
1406                                 }
1407                                 break;
1408                         default:
1409                                 /*_DEBUG_INFO("[da_contact_plugIn] Another Contact Change Noti");*/
1410                                 break;
1411                         }
1412                         err = contacts_list_next(list);
1413                         if (err != CONTACTS_ERROR_NONE) {
1414                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_next() fail, err[%d]", err);
1415                                 goto DACI_FINISH;
1416                         }
1417                 }
1418         }
1419
1420 DACI_FINISH:
1421         _DEBUG_INFO("DACI_FINISH");
1422
1423         *change_count = chagned_count;
1424
1425         if (list != NULL) {
1426                 err = contacts_list_destroy(list, true);
1427                 if (err != CONTACTS_ERROR_NONE) {
1428                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_destroy() fail, err[%d]", err);
1429                         goto DACI_FINISH;
1430                 }
1431         }
1432
1433         _EXTERN_FUNC_EXIT;
1434         return root_ptr;
1435 }
1436
1437 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_delete(int account_id, const char *folder_id, int change_point, int *change_count)
1438 {
1439         _EXTERN_FUNC_ENTER;
1440
1441 //      _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
1442 //      _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
1443 //      _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
1444 //
1445 //      sync_agent_plugin_item_node_s *root_ptr = 0;
1446 //      sync_agent_plugin_item_node_s *cursor_ptr = 0;
1447 //      int chagned_count = 0;
1448 //
1449 //      int contact_version = change_point;
1450 //
1451 //      int addressbook_id = atoi(folder_id);
1452 //      CTSiter *iter = 0;
1453 //      contacts_svc_get_updated_contacts(addressbook_id, contact_version, &iter);
1454 //
1455 //      int contact_ret = contacts_svc_iter_next(iter);
1456 //      while (CTS_SUCCESS == contact_ret) {
1457 //              CTSvalue *row_info = 0;
1458 //
1459 //              if ((row_info = contacts_svc_iter_get_info(iter)) == CTS_SUCCESS) {
1460 //                      _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_get_info() failed.\n");
1461 //              } else {
1462 //                      int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
1463 //
1464 //                      switch (type) {
1465 //                      case CTS_OPERATION_DELETED:
1466 //                              {
1467 //                                      int contact_id = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
1468 //                                      char *contact_id_str = g_strdup_printf("%d", contact_id);
1469 //                                      _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_DELETED : [%s]\n", contact_id_str);
1470 //
1471 //                                      if (root_ptr == NULL) {
1472 //                                              root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1473 //                                              if( root_ptr == NULL ) {
1474 //                                                      _DEBUG_ERROR("CALLOC failed !!!");
1475 //                                                      return NULL;
1476 //                                              }
1477 //                                              root_ptr->item_id = contact_id_str;
1478 //                                              root_ptr->next = 0;
1479 //                                              cursor_ptr = root_ptr;
1480 //                                      } else {
1481 //                                              cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1482 //                                              if( cursor_ptr->next == NULL ) {
1483 //                                                      _DEBUG_ERROR("CALLOC failed !!!");
1484 //                                                      return NULL;
1485 //                                              }
1486 //                                              cursor_ptr->next->item_id = contact_id_str;
1487 //                                              cursor_ptr->next->next = 0;
1488 //                                              cursor_ptr = cursor_ptr->next;
1489 //                                      }
1490 //
1491 //                                      chagned_count++;
1492 //                              }
1493 //                              break;
1494 //                      default:
1495 //                              break;
1496 //                      }
1497 //              }
1498 //
1499 //              contacts_svc_value_free(row_info);      /* next changing */
1500 //              row_info = 0;
1501 //              contact_ret = contacts_svc_iter_next(iter);
1502 //      }
1503 //
1504 //      /* memory free */
1505 //      if (iter != NULL) {
1506 //              contacts_svc_iter_remove(iter);
1507 //              iter = 0;
1508 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
1509 //      }
1510 //
1511 //      *change_count = chagned_count;
1512
1513         _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
1514         _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
1515         _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
1516
1517         sync_agent_plugin_item_node_s *root_ptr = 0;
1518         sync_agent_plugin_item_node_s *cursor_ptr = 0;
1519
1520         int chagned_count = 0;
1521         int contact_version = change_point;
1522         int addressbook_id = atoi(folder_id);
1523
1524         contacts_list_h list = NULL;
1525         contacts_record_h record = NULL;
1526         contacts_error_e err = CONTACTS_ERROR_NONE;
1527         int current_contact_version;
1528
1529         err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, &current_contact_version);
1530         if (err != CONTACTS_ERROR_NONE) {
1531                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1532                 goto DACI_FINISH;
1533         }
1534
1535         err = contacts_list_first(list);
1536         while (err == CONTACTS_ERROR_NONE) {
1537
1538                 err = contacts_list_get_current_record_p(list, &record);
1539                 if (err != CONTACTS_ERROR_NONE) {
1540                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
1541                 } else {
1542                         int type;
1543                         err = contacts_record_get_int(record, _contacts_contact_updated_info.type, &type);
1544                         if (err != CONTACTS_ERROR_NONE) {
1545                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1546                                 goto DACI_FINISH;
1547                         }
1548
1549                         switch (type) {
1550                         case CONTACTS_CHANGE_DELETED:
1551                                 {
1552                                         int contact_id;
1553                                         err = contacts_record_get_int(record, _contacts_contact_updated_info.contact_id, &contact_id);
1554                                         if (err != CONTACTS_ERROR_NONE) {
1555                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1556                                                 goto DACI_FINISH;
1557                                         }
1558
1559                                         char *contact_id_str = g_strdup_printf("%d", contact_id);
1560                                         _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_DELETED : [%s]\n", contact_id_str);
1561
1562                                         if (root_ptr == NULL) {
1563                                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1564                                                 if (root_ptr == NULL) {
1565                                                         _DEBUG_ERROR("CALLOC failed !!!");
1566                                                         return NULL;
1567                                                 }
1568                                                 root_ptr->item_id = contact_id_str;
1569                                                 root_ptr->next = 0;
1570                                                 cursor_ptr = root_ptr;
1571                                         } else {
1572                                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1573                                                 if (cursor_ptr->next == NULL) {
1574                                                         _DEBUG_ERROR("CALLOC failed !!!");
1575                                                         return NULL;
1576                                                 }
1577                                                 cursor_ptr->next->item_id = contact_id_str;
1578                                                 cursor_ptr->next->next = 0;
1579                                                 cursor_ptr = cursor_ptr->next;
1580                                         }
1581
1582                                         chagned_count++;
1583                                 }
1584                                 break;
1585                         default:
1586                                 break;
1587                         }
1588                 }
1589
1590                 err = contacts_list_next(list);
1591         }
1592
1593  DACI_FINISH:
1594
1595         *change_count = chagned_count;
1596
1597         contacts_list_destroy(list, true);
1598
1599         _EXTERN_FUNC_EXIT;
1600
1601         return root_ptr;
1602 }
1603
1604 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_update(int account_id, const char *folder_id, int change_point, int *change_count)
1605 {
1606         _EXTERN_FUNC_ENTER;
1607
1608 //      _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
1609 //      _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
1610 //      _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
1611 //
1612 //      sync_agent_plugin_item_node_s *root_ptr = 0;
1613 //      sync_agent_plugin_item_node_s *cursor_ptr = 0;
1614 //      int chagned_count = 0;
1615 //
1616 //      int contact_version = change_point;
1617 //
1618 //      int addressbook_id = atoi(folder_id);
1619 //      CTSiter *iter = 0;
1620 //      contacts_svc_get_updated_contacts(addressbook_id, contact_version, &iter);
1621 //
1622 //      int contact_ret = contacts_svc_iter_next(iter);
1623 //      while (CTS_SUCCESS == contact_ret) {
1624 //              CTSvalue *row_info = 0;
1625 //
1626 //              if ((row_info = contacts_svc_iter_get_info(iter)) == CTS_SUCCESS) {
1627 //                      _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_get_info() failed.\n");
1628 //              } else {
1629 //                      int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
1630 //
1631 //                      switch (type) {
1632 //                      case CTS_OPERATION_UPDATED:
1633 //                              {
1634 //                                      int contact_id = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
1635 //                                      char *contact_id_str = g_strdup_printf("%d", contact_id);
1636 //                                      _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_UPDATED : [%s]\n", contact_id_str);
1637 //
1638 //                                      if (root_ptr == NULL) {
1639 //                                              root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1640 //                                              if( root_ptr == NULL ) {
1641 //                                                      _DEBUG_ERROR("CALLOC failed !!!");
1642 //                                                      return NULL;
1643 //                                              }
1644 //                                              root_ptr->item_id = contact_id_str;
1645 //                                              root_ptr->next = 0;
1646 //                                              cursor_ptr = root_ptr;
1647 //                                      } else {
1648 //                                              cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1649 //                                              if( cursor_ptr->next == NULL ) {
1650 //                                                      _DEBUG_ERROR("CALLOC failed !!!");
1651 //                                                      return NULL;
1652 //                                              }
1653 //                                              cursor_ptr->next->item_id = contact_id_str;
1654 //                                              cursor_ptr->next->next = 0;
1655 //                                              cursor_ptr = cursor_ptr->next;
1656 //                                      }
1657 //
1658 //                                      chagned_count++;
1659 //                              }
1660 //                              break;
1661 //                      default:
1662 //                              break;
1663 //                      }
1664 //              }
1665 //
1666 //              contacts_svc_value_free(row_info);      /* next changing */
1667 //              row_info = 0;
1668 //              contact_ret = contacts_svc_iter_next(iter);
1669 //      }
1670 //
1671 //      /* memory free */
1672 //      if (iter != NULL) {
1673 //              contacts_svc_iter_remove(iter);
1674 //              iter = 0;
1675 //              _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
1676 //      }
1677 //
1678 //      *change_count = chagned_count;
1679
1680         _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
1681         _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
1682         _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
1683
1684         sync_agent_plugin_item_node_s *root_ptr = 0;
1685         sync_agent_plugin_item_node_s *cursor_ptr = 0;
1686
1687         int chagned_count = 0;
1688         int contact_version = change_point;
1689         int addressbook_id = atoi(folder_id);
1690
1691         contacts_list_h list = NULL;
1692         contacts_record_h record = NULL;
1693         contacts_error_e err = CONTACTS_ERROR_NONE;
1694         int current_contact_version;
1695
1696         err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, &current_contact_version);
1697         if (err != CONTACTS_ERROR_NONE) {
1698                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_changes_by_version Fail\n");
1699                 goto DACI_FINISH;
1700         }
1701
1702         err = contacts_list_first(list);
1703         while (err == CONTACTS_ERROR_NONE) {
1704
1705                 err = contacts_list_get_current_record_p(list, &record);
1706                 if (err != CONTACTS_ERROR_NONE) {
1707                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
1708                 } else {
1709                         int type;
1710                         err = contacts_record_get_int(record, _contacts_contact_updated_info.type, &type);
1711                         if (err != CONTACTS_ERROR_NONE) {
1712                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1713                                 goto DACI_FINISH;
1714                         }
1715
1716                         switch (type) {
1717                         case CONTACTS_CHANGE_UPDATED:
1718                                 {
1719                                         int contact_id;
1720                                         err = contacts_record_get_int(record, _contacts_contact_updated_info.contact_id, &contact_id);
1721                                         if (err != CONTACTS_ERROR_NONE) {
1722                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1723                                                 goto DACI_FINISH;
1724                                         }
1725
1726                                         char *contact_id_str = g_strdup_printf("%d", contact_id);
1727                                         _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_UPDATED : [%s]\n", contact_id_str);
1728
1729                                         if (root_ptr == NULL) {
1730                                                 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1731                                                 if (root_ptr == NULL) {
1732                                                         _DEBUG_ERROR("CALLOC failed !!!");
1733                                                         return NULL;
1734                                                 }
1735                                                 root_ptr->item_id = contact_id_str;
1736                                                 root_ptr->next = 0;
1737                                                 cursor_ptr = root_ptr;
1738                                         } else {
1739                                                 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
1740                                                 if (cursor_ptr->next == NULL) {
1741                                                         _DEBUG_ERROR("CALLOC failed !!!");
1742                                                         return NULL;
1743                                                 }
1744                                                 cursor_ptr->next->item_id = contact_id_str;
1745                                                 cursor_ptr->next->next = 0;
1746                                                 cursor_ptr = cursor_ptr->next;
1747                                         }
1748
1749                                         chagned_count++;
1750                                 }
1751                                 break;
1752                         default:
1753                                 break;
1754                         }
1755                 }
1756
1757                 err = contacts_list_next(list);
1758         }
1759
1760  DACI_FINISH:
1761
1762         *change_count = chagned_count;
1763
1764         contacts_list_destroy(list, true);
1765
1766         _EXTERN_FUNC_EXIT;
1767
1768         return root_ptr;
1769 }
1770
1771 EXPORT_API int sync_agent_plugin_get_last_change_point(void)
1772 {
1773         _EXTERN_FUNC_ENTER;
1774
1775 //      contacts_svc_begin_trans();
1776 //      int contactVersion = contacts_svc_end_trans(1);
1777
1778         int contactVersion;
1779         contacts_db_get_current_version(&contactVersion);
1780
1781         _EXTERN_FUNC_EXIT;
1782
1783         return contactVersion;
1784 }
1785
1786 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
1787 {
1788         _EXTERN_FUNC_ENTER;
1789
1790 //      contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, _process_contact_change, 0);
1791 //
1792 //      latest_contact_version = get_contact_version(1);
1793 //      _DEBUG_INFO("Current Latest Contact Version : %d", latest_contact_version);
1794 //
1795 //      pthread_t thread_process_change_id;
1796 //      if (pthread_create(&thread_process_change_id, 0, _rutine_contact_change, 0) < 0) {
1797 //              _DEBUG_ERROR("Contact Change Noti Handling Thread Create Error");
1798 //      }
1799
1800         contacts_error_e err = contacts_db_add_changed_cb(_contacts_contact._uri, _process_contact_change, 0);
1801         sync_agent_da_return_e ret = _convert_service_error_to_common_error(err);
1802         _DEBUG_INFO("ret = %d", ret);
1803
1804         latest_contact_version = get_contact_version(1);
1805         _DEBUG_INFO("Current Latest Contact Version : %d", latest_contact_version);
1806
1807         pthread_t thread_process_change_id;
1808         if (pthread_create(&thread_process_change_id, 0, _rutine_contact_change, 0) < 0) {
1809                 _DEBUG_ERROR("Contact Change Noti Handling Thread Create Error");
1810         }
1811
1812         _EXTERN_FUNC_EXIT;
1813 }
1814
1815 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
1816 {
1817         _EXTERN_FUNC_ENTER;
1818
1819         callback_add_item = callback;
1820
1821         _EXTERN_FUNC_EXIT;
1822 }
1823
1824 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
1825 {
1826         _EXTERN_FUNC_ENTER;
1827
1828         callback_del_item = callback;
1829
1830         _EXTERN_FUNC_EXIT;
1831 }
1832
1833 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
1834 {
1835         _EXTERN_FUNC_ENTER;
1836
1837         callback_update_item = callback;
1838
1839         _EXTERN_FUNC_EXIT;
1840 }
1841
1842 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
1843 {
1844         _EXTERN_FUNC_ENTER;
1845
1846         callback_get_account_id_list = callback;
1847
1848         _EXTERN_FUNC_EXIT;
1849 }
1850
1851 EXPORT_API int sync_agent_plugin_get_max_item_count(int folder_type)
1852 {
1853         _EXTERN_FUNC_ENTER;
1854
1855         _EXTERN_FUNC_EXIT;
1856
1857         return MAX_ITEM_COUNT_CONTACT;
1858 }
1859
1860 EXPORT_API int sync_agent_plugin_get_max_field_length(int field_name, int child_field_name)
1861 {
1862         _EXTERN_FUNC_ENTER;
1863
1864         switch (field_name) {
1865         case CONTACT_FIELD_NAME:
1866                 switch (child_field_name) {
1867                 case CONTACT_CHILD_FIELD_FAMILY:
1868                         _EXTERN_FUNC_EXIT;
1869                         return MAX_LEN_FAMILY;
1870                 case CONTACT_CHILD_FIELD_GIVEN:
1871                         _EXTERN_FUNC_EXIT;
1872                         return MAX_LEN_GIVEN;
1873                 case CONTACT_CHILD_FIELD_MIDDLE:
1874                         _EXTERN_FUNC_EXIT;
1875                         return MAX_LEN_MIDDLE;
1876                 case CONTACT_CHILD_FIELD_PREFIX:
1877                         _EXTERN_FUNC_EXIT;
1878                         return MAX_LEN_PREFIX;
1879                 case CONTACT_CHILD_FIELD_SUFFIX:
1880                         _EXTERN_FUNC_EXIT;
1881                         return MAX_LEN_SUFFIX;
1882                 case CONTACT_CHILD_FIELD_FORMATTED:
1883                         _EXTERN_FUNC_EXIT;
1884                         return MAX_LEN_FORMATTED;
1885                 case CONTACT_CHILD_FIELD_NICKNAME:
1886                         _EXTERN_FUNC_EXIT;
1887                         return MAX_LEN_NICKNAME;
1888                 case CONTACT_CHILD_FIELD_READING_NAME:
1889                         _EXTERN_FUNC_EXIT;
1890                         return MAX_LEN_READING_NAME;
1891                 case CONTACT_CHILD_FIELD_READING_LAST_NAME:
1892                         _EXTERN_FUNC_EXIT;
1893                         return MAX_LEN_READING_LAST_NAME;
1894                 default:
1895                         return -1;
1896                 }
1897         case CONTACT_FIELD_TEL:
1898                 switch (child_field_name) {
1899                 case CONTACT_CHILD_FIELD_TEL:
1900                         _EXTERN_FUNC_EXIT;
1901                         return MAX_LEN_TEL;
1902                 default:
1903                         return -1;
1904                 }
1905         case CONTACT_FIELD_EMAIL:
1906                 switch (child_field_name) {
1907                 case CONTACT_CHILD_FIELD_EMAIL:
1908                         _EXTERN_FUNC_EXIT;
1909                         return MAX_LEN_EMAIL;
1910                 default:
1911                         return -1;
1912                 }
1913         case CONTACT_FIELD_URL:
1914                 switch (child_field_name) {
1915                 case CONTACT_CHILD_FIELD_URL:
1916                         _EXTERN_FUNC_EXIT;
1917                         return MAX_LEN_URL;
1918                 default:
1919                         return -1;
1920                 }
1921         case CONTACT_FIELD_ADDR:
1922                 switch (child_field_name) {
1923                 case CONTACT_CHILD_FIELD_POBOX:
1924                         _EXTERN_FUNC_EXIT;
1925                         return MAX_LEN_POBOX;
1926                 case CONTACT_CHILD_FIELD_EXT_ADDR:
1927                         _EXTERN_FUNC_EXIT;
1928                         return MAX_LEN_EXT_ADDR;
1929                 case CONTACT_CHILD_FIELD_STREET:
1930                         _EXTERN_FUNC_EXIT;
1931                         return MAX_LEN_STREET;
1932                 case CONTACT_CHILD_FIELD_LOCALITY:
1933                         _EXTERN_FUNC_EXIT;
1934                         return MAX_LEN_LOCALITY;
1935                 case CONTACT_CHILD_FIELD_REGION:
1936                         _EXTERN_FUNC_EXIT;
1937                         return MAX_LEN_REGION;
1938                 case CONTACT_CHILD_FIELD_POSTAL_CODE:
1939                         _EXTERN_FUNC_EXIT;
1940                         return MAX_LEN_POSTAL_CODE;
1941                 case CONTACT_CHILD_FIELD_COUNTRY:
1942                         _EXTERN_FUNC_EXIT;
1943                         return MAX_LEN_COUNTRY;
1944                 default:
1945                         return -1;
1946                 }
1947         case CONTACT_FIELD_NOTE:
1948                 _EXTERN_FUNC_EXIT;
1949                 return MAX_LEN_NOTE;
1950         case CONTACT_FIELD_COMPANY:
1951                 _EXTERN_FUNC_EXIT;
1952                 return MAX_LEN_COMPANY;
1953         case CONTACT_FIELD_DEPARTMENT:
1954                 _EXTERN_FUNC_EXIT;
1955                 return MAX_LEN_DEPARTMENT;
1956         case CONTACT_FIELD_TITLE:
1957                 _EXTERN_FUNC_EXIT;
1958                 return MAX_LEN_TITLE;
1959         case CONTACT_FIELD_HOBBY:
1960                 _EXTERN_FUNC_EXIT;
1961                 return MAX_LEN_HOBBY;
1962         case CONTACT_FIELD_PHOTO:
1963                 _EXTERN_FUNC_EXIT;
1964                 return MAX_LEN_PHOTO;
1965         default:
1966                 return -1;
1967         }
1968 }
1969
1970 EXPORT_API int sync_agent_plugin_get_max_field_count(int field_name, int child_field_name)
1971 {
1972         _EXTERN_FUNC_ENTER;
1973
1974         switch (field_name) {
1975         case CONTACT_FIELD_TEL:
1976                 _EXTERN_FUNC_EXIT;
1977                 return MAX_FIELD_COUNT_TEL;
1978         case CONTACT_FIELD_EMAIL:
1979                 _EXTERN_FUNC_EXIT;
1980                 return MAX_FIELD_COUNT_EMAIL;
1981         case CONTACT_FIELD_URL:
1982                 _EXTERN_FUNC_EXIT;
1983                 return MAX_FIELD_COUNT_URL;
1984         case CONTACT_FIELD_ADDR:
1985                 _EXTERN_FUNC_EXIT;
1986                 return MAX_FIELD_COUNT_ADDR;
1987         default:
1988                 return -1;
1989         }
1990 }
1991
1992 EXPORT_API int sync_agent_plugin_get_field_value(int field_name, int child_field_name, char **str_val, int *num_val1, int *num_val2)
1993 {
1994         _EXTERN_FUNC_ENTER;
1995
1996         switch (field_name) {
1997         case CONTACT_FIELD_YEAR:
1998                 *num_val1 = MIN_PERIOD_YEAR;
1999                 *num_val2 = MAX_PERIOD_YEAR;
2000                 _EXTERN_FUNC_EXIT;
2001                 return 1;
2002         default:
2003                 return -1;
2004         }
2005 }
2006
2007 EXPORT_API int sync_agent_plugin_get_is_support_feature(int feature)
2008 {
2009         _EXTERN_FUNC_ENTER;
2010
2011         switch (feature) {
2012         case CONTACT_FEATURE_GENDER:
2013                 _EXTERN_FUNC_EXIT;
2014                 return IF_SUPPORT_GENDER;
2015         default:
2016                 return -1;
2017         }
2018 }
2019
2020 /********************************** static function *******************************************/
2021
2022 static void _set_is_noti_from_me(int set_flag)
2023 {
2024         _INNER_FUNC_ENTER;
2025
2026         if (pthread_mutex_lock(&lockx)) {
2027                 _DEBUG_TRACE("[da_contact_plugIn] pthread_mutex_lock error\n");
2028         }
2029
2030         is_noti_from_me = set_flag;
2031         _DEBUG_TRACE("[da_contact_plugIn] is_noti_from_me set [%d]", set_flag);
2032
2033         if (pthread_mutex_unlock(&lockx)) {
2034                 _DEBUG_TRACE("[da_contact_plugIn] pthread_mutex_unlock error\n");
2035         }
2036
2037         _INNER_FUNC_EXIT;
2038 }
2039
2040 static int _get_is_noti_from_me(void)
2041 {
2042         _INNER_FUNC_ENTER;
2043
2044         _DEBUG_TRACE("[da_contact_plugIn] is_noti_from_me get [%d]", is_noti_from_me);
2045
2046         _INNER_FUNC_EXIT;
2047
2048         return is_noti_from_me;
2049 }
2050
2051 //static void _process_contact_change(void *data)
2052 //{
2053 //      _INNER_FUNC_ENTER;
2054 //
2055 //      _DEBUG_TRACE("[da_contact_plugIn] detected contact storage changed!!\n");
2056 //
2057 //      int from_Me = _get_is_noti_from_me();
2058 //
2059 //      if (from_Me == 1) {
2060 //              _DEBUG_TRACE("[da_contact_plugIn] This noti has been sended from Me!! so will be ignored!!\n");
2061 //              _set_is_noti_from_me(0);
2062 //              latest_contact_version++;
2063 //              return;
2064 //      }
2065 //
2066 //      int *contactVersion = (int *)calloc(1, sizeof(int));
2067 //      if( contactVersion == NULL ) {
2068 //              _DEBUG_ERROR("CALLOC failed !!!");
2069 //              return;
2070 //      }
2071 //      *contactVersion = latest_contact_version;
2072 //      latest_contact_version++;
2073 //
2074 //
2075 //      _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2076 //      if( sync_agent_async_queue_length(queue) < 1 ) {
2077 //              sync_agent_send_msg_async_queue(queue, (void *)contactVersion);
2078 //              _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2079 //      }
2080 //
2081 //      _INNER_FUNC_EXIT;
2082 //}
2083
2084 static void _process_contact_change(const char *view_uri, void *user_data)
2085 {
2086         _INNER_FUNC_ENTER;
2087
2088         int from_Me = _get_is_noti_from_me();
2089         if (from_Me == 1) {
2090                 _DEBUG_TRACE("[da_contact_plugIn] This noti has been sended from Me!! so will be ignored!!\n");
2091                 _set_is_noti_from_me(0);
2092                 latest_contact_version++;
2093                 _DEBUG_INFO("latest_contact_version = %d", latest_contact_version);
2094                 return;
2095         }
2096
2097         int *contactVersion = (int *)calloc(1, sizeof(int));
2098         if (contactVersion == NULL) {
2099                 _DEBUG_ERROR("CALLOC failed !!!");
2100                 return;
2101         }
2102
2103         *contactVersion = latest_contact_version;
2104         latest_contact_version++;
2105         _DEBUG_INFO("latest_contact_version = %d", latest_contact_version);
2106
2107         _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2108         if (sync_agent_async_queue_length(queue) < 1) {
2109                 sync_agent_send_msg_async_queue(queue, (void *)contactVersion);
2110                 _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2111         }
2112
2113         _INNER_FUNC_EXIT;
2114 }
2115
2116 static void *_rutine_contact_change(void *data)
2117 {
2118         _INNER_FUNC_ENTER;
2119
2120 //      _DEBUG_TRACE("[da_contact_plugIn] Start __rutine_Contact_Change (create thread)\n");
2121 //
2122 //      /*
2123 //       * Create Queue
2124 //       */
2125 //      queue = sync_agent_alloc_async_queue();
2126 //      if (queue == NULL) {
2127 //              _DEBUG_ERROR("Failed to call fw_async_quecontacts_svc_get_updated_contactsue_alloc()");
2128 //              return 0;
2129 //      }
2130 //
2131 //      while (1) {
2132 //              _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2133 //              int *temp_contact_version = sync_agent_receive_msg_async_queue(queue);
2134 //              _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2135 //              int contact_version = *temp_contact_version;
2136 //              free(temp_contact_version);
2137 //
2138 //              _DEBUG_TRACE("Detected Change ContactVersion : %d", contact_version);
2139 //
2140 //              int count;
2141 //              int *accountList = callback_get_account_id_list(FW_CONTACT, &count);
2142 //              if (accountList == NULL)
2143 //                      continue;
2144 //
2145 //              int i = 0;
2146 //              for (; i < count; i++) {
2147 //                      int contact_account_id = accountList[i];
2148 //
2149 //                      _DEBUG_TRACE("[da_contact_plugIn] contact_account_id : %d, count : %d\n", contact_account_id, count);
2150 //                      _DEBUG_TRACE("[da_contact_plugIn] Before contacts_svc_get_updated_contacts : %d\n", contact_version);
2151 //
2152 //                      CTSiter *iter_addressbook = 0;
2153 //                      CTSvalue *addressbook = 0;
2154 //
2155 //                      int err = contacts_svc_get_list_with_int(CTS_LIST_ADDRESSBOOKS_OF_ACCOUNT_ID, contact_account_id, &iter_addressbook);
2156 //                      err = contacts_svc_iter_next(iter_addressbook);
2157 //                      _DEBUG_TRACE("[da_contact_plugIn] err : %d\n", err);
2158 //
2159 //                      if (err != CTS_SUCCESS && contact_account_id == -1) {
2160 //                              err = CTS_SUCCESS;
2161 //                      }
2162 //
2163 //                      while (err == CTS_SUCCESS) {
2164 //                              if (contact_account_id != -1)
2165 //                                      addressbook = contacts_svc_iter_get_info(iter_addressbook);
2166 //
2167 //                              if (addressbook != NULL || contact_account_id == -1) {
2168 //                                      int addressbook_id = 0;
2169 //                                      if (contact_account_id != -1)
2170 //                                              addressbook_id = contacts_svc_value_get_int(addressbook, CTS_LIST_ADDRESSBOOK_ID_INT);
2171 //
2172 //                                      char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
2173 //                                      _DEBUG_TRACE("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
2174 //
2175 //                                      CTSiter *iter = 0;
2176 //                                      int temp_err = contacts_svc_get_updated_contacts(addressbook_id, contact_version, &iter);
2177 //                                      _DEBUG_TRACE("temp_err =%d", temp_err);
2178 //
2179 //                                      int contact_ret = contacts_svc_iter_next(iter);
2180 //                                      while (CTS_SUCCESS == contact_ret) {
2181 //                                              CTSvalue *row_info = 0;
2182 //
2183 //                                              if ((row_info = contacts_svc_iter_get_info(iter)) == CTS_SUCCESS) {
2184 //                                                      _DEBUG_TRACE("[da_contact_plugIn] contacts_svc_iter_get_info() failed.\n");
2185 //                                              } else {
2186 //                                                      int contact_id = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
2187 //                                                      char contact_id_str[12];
2188 //                                                      snprintf(contact_id_str, sizeof(contact_id_str), "%d", contact_id);
2189 //                                                      int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
2190 //                                                      _DEBUG_TRACE("[da_contact_plugIn] $$$$$$$ contact_id :%d\n", contact_id);
2191 //
2192 //                                                      switch (type) {
2193 //                                                      case CTS_OPERATION_INSERTED:
2194 //                                                              {
2195 //                                                                      _DEBUG_TRACE("[da_contact_plugIn]  CTS_OPERATION_INSERTED\n");
2196 //
2197 //                                                                      int result = callback_add_item(accountList[i], i, contact_id_str, FW_CONTACT, str_addressbook_id, 0);
2198 //                                                                      _DEBUG_TRACE("[da_contact_plugIn] ===1\n");
2199 //                                                                      if (!result)
2200 //                                                                              _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_add_item() \n");
2201 //                                                              }
2202 //                                                              break;
2203 //                                                      case CTS_OPERATION_UPDATED:
2204 //                                                              {
2205 //                                                                      _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_UPDATED\n");
2206 //                                                                      _DEBUG_TRACE("[da_contact_plugIn] contact_id : %s\n", contact_id_str);
2207 //
2208 //                                                                      int result = callback_update_item(accountList[i], i, contact_id_str, FW_CONTACT);
2209 //                                                                      if (!result)
2210 //                                                                              _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_update_item() \n");
2211 //                                                              }
2212 //                                                              break;
2213 //                                                      case CTS_OPERATION_DELETED:
2214 //                                                              {
2215 //                                                                      _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_DELETED\n");
2216 //                                                                      int result = callback_del_item(accountList[i], i, contact_id_str, FW_CONTACT);
2217 //                                                                      if (!result)
2218 //                                                                              _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_del_item() \n");
2219 //                                                              }
2220 //                                                              break;
2221 //                                                      default:
2222 //                                                              _DEBUG_TRACE("[da_contact_plugIn] Another Contact Change Noti\n");
2223 //                                                              break;
2224 //                                                      }
2225 //                                              }
2226 //
2227 //                                              contacts_svc_value_free(row_info);      /* next changing */
2228 //                                              row_info = NULL;
2229 //                                              contact_ret = contacts_svc_iter_next(iter);
2230 //                                      }
2231 //
2232 //                                      if (contact_account_id == -1)
2233 //                                              err = CTS_ERR_FINISH_ITER;
2234 //                                      else
2235 //                                              err = contacts_svc_iter_next(iter_addressbook);
2236 //
2237 //                                      if (addressbook != NULL)
2238 //                                              contacts_svc_value_free(addressbook);
2239 //
2240 //                                      if (iter != NULL)
2241 //                                              contacts_svc_iter_remove(iter);
2242 //
2243 //                                      if (str_addressbook_id != NULL) {
2244 //                                              free(str_addressbook_id);
2245 //                                      }
2246 //
2247 //                              } else {
2248 //                                      _DEBUG_TRACE("[da_contact_plugIn] account : %d, contact folder does not exist \n", contact_account_id);
2249 //                                      break;
2250 //                              }
2251 //                      }
2252 //
2253 //                      if (contact_account_id != -1) {
2254 //                              contacts_svc_iter_remove(iter_addressbook);
2255 //                      }
2256 //              }
2257 //
2258 //              /* memory free */
2259 //              if (accountList != NULL)
2260 //                      free(accountList);
2261 //      }
2262 //
2263 //      _DEBUG_TRACE("[da_contact_plugIn] End __rutine_Contact_Change (create thread)\n");
2264
2265         _DEBUG_TRACE("[da_contact_plugIn] Start __rutine_Contact_Change (create thread)\n");
2266
2267         /*
2268          * Create Queue
2269          */
2270         queue = sync_agent_alloc_async_queue();
2271         if (queue == NULL) {
2272                 _DEBUG_ERROR("Failed to call sync_agent_alloc_async_queue()");
2273                 return 0;
2274         }
2275         // open contact service
2276         sync_agent_plugin_open_service();
2277
2278         while (1) {
2279                 _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2280                 int *temp_contact_version = sync_agent_receive_msg_async_queue(queue);
2281                 _DEBUG_TRACE("fw_async_queue_length = %d", sync_agent_async_queue_length(queue));
2282                 int contact_version = *temp_contact_version;
2283                 free(temp_contact_version);
2284
2285                 _DEBUG_TRACE("Detected Change ContactVersion : %d", contact_version);
2286
2287                 int count;
2288                 int *accountList = callback_get_account_id_list(FW_CONTACT, &count);
2289                 if (accountList == NULL)
2290                         continue;
2291
2292                 int i = 0;
2293                 for (; i < count; i++) {
2294                         int contact_account_id = accountList[i];
2295
2296                         _DEBUG_TRACE("[da_contact_plugIn] contact_account_id : %d, count : %d\n", contact_account_id, count);
2297                         _DEBUG_TRACE("[da_contact_plugIn] Before contacts_db_get_changes_by_version : %d\n", contact_version);
2298
2299                         contacts_error_e err = CONTACTS_ERROR_NONE;
2300                         contacts_query_h query = NULL;
2301                         contacts_filter_h filter = NULL;
2302                         contacts_list_h address_book_list = NULL;
2303                         contacts_list_h contact_list = NULL;
2304                         contacts_record_h address_book_record = NULL;
2305                         contacts_record_h contact_record = NULL;
2306
2307                         unsigned int projections[] = {
2308                                 _contacts_address_book.id
2309                         };
2310
2311                         err = contacts_query_create(_contacts_address_book._uri, &query);
2312                         if (err != CONTACTS_ERROR_NONE)
2313                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_create Fail\n");
2314
2315                         err = contacts_filter_create(_contacts_address_book._uri, &filter);
2316                         if (err != CONTACTS_ERROR_NONE)
2317                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_create Fail\n");
2318
2319                         err = contacts_filter_add_int(filter, _contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, contact_account_id);
2320                         if (err != CONTACTS_ERROR_NONE)
2321                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_filter_set_int Fail\n");
2322
2323                         err = contacts_query_set_filter(query, filter);
2324                         if (err != CONTACTS_ERROR_NONE)
2325                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_filter Fail\n");
2326
2327                         err = contacts_query_set_projection(query, projections, 1);
2328                         if (err != CONTACTS_ERROR_NONE)
2329                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
2330
2331                         err = contacts_db_get_records_with_query(query, 0, 0, &address_book_list);
2332                         if (err != CONTACTS_ERROR_NONE) {
2333                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_records_with_query Fail\n");
2334                                 _DEBUG_ERROR("err = %d", err);
2335                         }
2336 //                      err = contacts_list_first(address_book_list);
2337 //                      _DEBUG_TRACE("[da_contact_plugIn] err : %d\n", err);
2338 //                      if (err != CONTACTS_ERROR_NONE && contact_account_id == -1)
2339 //                              err = CONTACTS_ERROR_NONE;
2340
2341                         while (err == CONTACTS_ERROR_NONE) {
2342                                 if (contact_account_id != -1) {
2343                                         err = contacts_list_get_current_record_p(address_book_list, &address_book_record);
2344                                         if (err != CONTACTS_ERROR_NONE)
2345                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
2346                                 }
2347
2348                                 if (address_book_record != NULL || contact_account_id == -1) {
2349                                         int addressbook_id = 0;
2350                                         if (contact_account_id != -1) {
2351                                                 err = contacts_record_get_int(address_book_record, _contacts_address_book.id, &addressbook_id);
2352                                                 if (err != CONTACTS_ERROR_NONE)
2353                                                         _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
2354                                         }
2355
2356                                         char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
2357                                         _DEBUG_TRACE("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
2358
2359                                         int current_contact_version;
2360                                         err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &contact_list, &current_contact_version);
2361                                         _DEBUG_TRACE("contact_version = %d, current_contact_version = %d\n", contact_version, current_contact_version);
2362                                         if (err != CONTACTS_ERROR_NONE)
2363                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_changes_by_version Fail\n");
2364
2365                                         err = contacts_list_first(contact_list);
2366                                         unsigned int contact_count = 0;
2367                                         contacts_list_get_count(contact_list, &contact_count);
2368                                         _DEBUG_INFO("err = %d, contact_count = %d", err, contact_count);
2369                                         while (err == CONTACTS_ERROR_NONE) {
2370
2371                                                 err = contacts_list_get_current_record_p(contact_list, &contact_record);
2372                                                 if (err != CONTACTS_ERROR_NONE) {
2373                                                         _DEBUG_ERROR("[da_contact_plugIn] contacts_list_get_current_record_p Fail\n");
2374                                                 } else {
2375                                                         int contact_id;
2376                                                         char contact_id_str[12];
2377                                                         int type;
2378
2379                                                         err = contacts_record_get_int(contact_record, _contacts_contact_updated_info.contact_id, &contact_id);
2380                                                         if (err != CONTACTS_ERROR_NONE)
2381                                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
2382
2383                                                         snprintf(contact_id_str, sizeof(contact_id_str), "%d", contact_id);
2384
2385                                                         err = contacts_record_get_int(contact_record, _contacts_contact_updated_info.type, &type);
2386                                                         if (err != CONTACTS_ERROR_NONE)
2387                                                                 _DEBUG_ERROR("[da_contact_plugIn] contacts_record_get_int Fail\n");
2388
2389                                                         _DEBUG_TRACE("[da_contact_plugIn] $$$$$$$ contact_id :%d\n", contact_id);
2390
2391                                                         switch (type) {
2392                                                         case CONTACTS_CHANGE_INSERTED:
2393                                                                 {
2394                                                                         _DEBUG_TRACE("[da_contact_plugIn]  CTS_OPERATION_INSERTED\n");
2395
2396                                                                         int result = callback_add_item(accountList[i], i, contact_id_str, FW_CONTACT, str_addressbook_id, 0);
2397                                                                         _DEBUG_TRACE("[da_contact_plugIn] ===1\n");
2398                                                                         if (!result)
2399                                                                                 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_add_item() \n");
2400                                                                 }
2401                                                                 break;
2402                                                         case CONTACTS_CHANGE_UPDATED:
2403                                                                 {
2404                                                                         _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_UPDATED\n");
2405                                                                         _DEBUG_TRACE("[da_contact_plugIn] contact_id : %s\n", contact_id_str);
2406
2407                                                                         int result = callback_update_item(accountList[i], i, contact_id_str, FW_CONTACT);
2408                                                                         if (!result)
2409                                                                                 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_update_item() \n");
2410                                                                 }
2411                                                                 break;
2412                                                         case CONTACTS_CHANGE_DELETED:
2413                                                                 {
2414                                                                         _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_DELETED\n");
2415                                                                         int result = callback_del_item(accountList[i], i, contact_id_str, FW_CONTACT);
2416                                                                         if (!result)
2417                                                                                 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_del_item() \n");
2418                                                                 }
2419                                                                 break;
2420                                                         default:
2421                                                                 _DEBUG_TRACE("[da_contact_plugIn] Another Contact Change Noti\n");
2422                                                                 break;
2423                                                         }
2424
2425                                                 }
2426
2427                                                 err = contacts_list_next(contact_list);
2428                                         }
2429
2430                                         if (str_addressbook_id != NULL) {
2431                                                 free(str_addressbook_id);
2432                                         }
2433
2434                                         contacts_list_destroy(contact_list, true);
2435                                 } else {
2436                                         _DEBUG_TRACE("[da_contact_plugIn] account : %d, contact folder does not exist \n", contact_account_id);
2437                                         break;
2438                                 }
2439
2440                                 err = contacts_list_next(address_book_list);
2441                                 _DEBUG_TRACE("contacts_list_next = %d", err);
2442                         }
2443
2444                         contacts_query_destroy(query);
2445                         contacts_filter_destroy(filter);
2446                         contacts_list_destroy(address_book_list, true);
2447                 }
2448
2449                 /* memory free */
2450                 if (accountList != NULL)
2451                         free(accountList);
2452
2453         }
2454
2455         //  close contact service
2456         sync_agent_plugin_close_service();
2457
2458         _DEBUG_TRACE("[da_contact_plugIn] End __rutine_Contact_Change (create thread)\n");
2459
2460         _INNER_FUNC_EXIT;
2461
2462         return 0;
2463 }
2464
2465 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err)
2466 {
2467         _INNER_FUNC_ENTER;
2468
2469         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
2470         _DEBUG_TRACE("[da_contact_plugIn] Error Code : %d\n", err);
2471
2472         switch (err) {
2473         case CONTACTS_ERROR_NONE:
2474                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_NONE");
2475                 ret = SYNC_AGENT_DA_SUCCESS;
2476                 break;
2477         case CONTACTS_ERROR_OUT_OF_MEMORY:
2478                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_OUT_OF_MEMORY");
2479                 ret = SYNC_AGENT_DA_ERR_MEMORY_FULL;
2480                 break;
2481         case CONTACTS_ERROR_INVALID_PARAMETER:
2482                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_INVALID_PARAMETER");
2483                 ret = SYNC_AGENT_DA_ERR_INVALID_PARAMETER;
2484                 break;
2485         case CONTACTS_ERROR_NO_DATA:
2486                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_NO_DATA");
2487                 ret = SYNC_AGENT_DA_ERR_NO_DATA;
2488                 break;
2489         case CONTACTS_ERROR_DB:
2490                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_DB");
2491                 ret = SYNC_AGENT_DA_ERR_SERVICE_DB;
2492                 break;
2493         case CONTACTS_ERROR_IPC:
2494                 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_IPC");
2495                 ret = SYNC_AGENT_DA_ERR_SERVICE_IPC;
2496                 break;
2497         default:
2498                 ret = SYNC_AGENT_DA_ERRORS;
2499                 break;
2500         }
2501
2502         _INNER_FUNC_EXIT;
2503
2504         return ret;
2505 }
2506
2507 static bool _get_account_id(account_h account, void *user_data)
2508 {
2509         _INNER_FUNC_ENTER;
2510
2511         account_id_s *account_info_list = user_data;
2512
2513         bool has_contact = FALSE;
2514
2515         account_get_capability_all(account, __is_contact_id, &has_contact);
2516
2517         if (has_contact == FALSE) {
2518                 /* DO Nothing */
2519         } else {
2520                 char *domain_name = NULL;
2521                 account_error_e error_code = account_get_domain_name(account, &domain_name);
2522                 if (error_code != ACCOUNT_ERROR_NONE) {
2523                         _DEBUG_ERROR("Failed to call account_get_domain_name()");
2524                         return has_contact;
2525                 }
2526
2527                 /*local service id already in id list so pass */
2528                 if (strcmp(domain_name, "Gmail") && strcmp(domain_name, "exchange")) {
2529                         _DEBUG_TRACE("domain is [%s] is not gmail&exchange");
2530                         return has_contact;
2531                 }
2532
2533                 account_id_s *account_info = (account_id_s *) calloc(1, sizeof(account_id_s));
2534                 if (account_info == NULL) {
2535                         _DEBUG_ERROR("CALLOC failed !!!");
2536                         return FALSE;
2537                 }
2538                 account_get_account_id(account, &(account_info->account_id));
2539                 _DEBUG_TRACE("Getted accont id : %d", account_info->account_id);
2540                 account_info->next = 0;
2541
2542                 account_id_s *cursor_ptr = account_info_list;
2543                 while (cursor_ptr->next != 0) {
2544                         cursor_ptr = cursor_ptr->next;
2545                 }
2546
2547                 cursor_ptr->next = account_info;
2548         }
2549
2550         _INNER_FUNC_EXIT;
2551
2552         return has_contact;
2553 }
2554
2555 static bool __is_contact_id(/*account_capability_type_e*/ const char* capability_type, account_capability_state_e capability_value, void *user_data)
2556 {
2557         _INNER_FUNC_ENTER;
2558         retvm_if(capability_type == NULL, FALSE, "capability_type is NULL !!");
2559
2560         bool *has_contact = user_data;
2561         //if (capability_type == ACCOUNT_CAPABILITY_CONTACT) {
2562         if (strncmp(capability_type, ACCOUNT_SUPPORTS_CAPABILITY_CONTACT, strlen(capability_type)) ==0) {
2563                 *has_contact = TRUE;
2564                 _INNER_FUNC_EXIT;
2565                 return FALSE;
2566         }
2567         _INNER_FUNC_EXIT;
2568         return TRUE;
2569 }
2570
2571 static void _contacts_db_result_cb(int error, void *user_data)
2572 {
2573         _INNER_FUNC_ENTER;
2574         _DEBUG_TRACE("error = %d", error);
2575         _INNER_FUNC_EXIT;
2576 }