3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
33 #include "utility/sync_util.h"
34 #include "utility/fw_async_queue.h"
36 #include "plugin/data_connector_interface.h"
39 #define EXPORT_API __attribute__ ((visibility("default")))
42 #ifndef SYNC_AGENT_LOG
44 #define LOG_TAG "PLUGIN_DA_CONTACT"
47 #define FW_CONTACT 0 /* plugIn-Id */
49 static int latest_contact_version;
51 static sync_agent_util_async_queue_s *queue;
53 static pthread_mutex_t lockx;
55 static int is_noti_from_me = 0;
57 static int is_storage_changed = 0;
59 static sync_agent_add_item_cb_plugin callback_add_item;
61 static sync_agent_del_item_cb_plugin callback_del_item;
63 static sync_agent_update_item_cb_plugin callback_update_item;
65 static sync_agent_get_account_id_list_cb_plugin callback_get_account_id_list;
67 static void _set_is_noti_from_me(int set_flag);
69 static int _get_is_noti_from_me(void);
71 //static void _process_contact_change(void *);
72 static void _process_contact_change(const char *view_uri, void *user_data);
74 static void *_rutine_contact_change(void *);
76 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err);
78 static bool _get_account_id(account_h account, void *user_data);
80 static bool __is_contact_id(/*account_capability_type_e*/const char* capability_type, account_capability_state_e capability_value, void *user_data);
82 static void _contacts_db_result_cb(int error, void *user_data);
84 typedef struct account_id {
86 struct account_id *next;
89 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service(void)
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);
99 // _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
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);
108 _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
116 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service(void)
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);
126 // _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
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);
135 _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
143 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction(void)
147 _DEBUG_INFO("[da_contact_plugIn] Start Begin_Transaction\n");
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);
155 // _DEBUG_INFO("[da_contact_plugIn] in contact Success!\n");
156 _set_is_noti_from_me(1);
159 _DEBUG_INFO("[da_contact_plugIn] End Begin_Transaction\n");
166 EXPORT_API sync_agent_da_return_e sync_agent_plugin_end_transaction(int is_success)
170 _DEBUG_INFO("[da_contact_plugIn] Start End_Transaction\n");
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);
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);
183 is_storage_changed = 0;
186 _DEBUG_INFO("[da_contact_plugIn] End End_Transaction\n");
193 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
197 // retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
199 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
201 // CTSstruct *item = (CTSstruct *)data;
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);
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;
217 // contacts_svc_struct_free(item);
219 retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
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;
227 int int_folder_id = atoi(folder_id);
228 _DEBUG_INFO("[da_contact_plugIn] folder id [%s][%d]", folder_id, int_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);
238 _DEBUG_INFO("[da_contact_plugIn] insert");
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);
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;
255 contacts_record_destroy(record, true);
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)
266 retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
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;
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;
280 int count = g_list_length((GList *)data);
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;
293 unsigned int id_count = 0;
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;
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;
310 for (i = 0; i < count; ++i) {
311 _DEBUG_INFO("[da_contact_plugIn] ids[%d] of [%d]", ids[i], id_count);
315 is_storage_changed = 1;
318 *item_id_count = id_count;
321 _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
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);
335 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
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 !!!");
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;
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);
352 _DEBUG_INFO("[da_contact_plugIn] contacts_db_update_record() Success!\n");
353 is_storage_changed = 1;
356 contacts_record_destroy(record, true);
362 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_bulk_item(void *data)
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;
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;
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;
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;
394 is_storage_changed = 1;
397 _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
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;
412 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
416 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "[da_contact_plugIn] item_id is NULL. FAIL !!!");
418 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
419 contacts_error_e err = CONTACTS_ERROR_NONE;
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);
426 _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_record(%s) Success!\n", item_id);
427 is_storage_changed = 1;
435 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_bulk_item(int *item_id, int count)
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 !!!");
442 sync_agent_da_return_e da_ret = SYNC_AGENT_DA_SUCCESS;
443 contacts_error_e contacts_err = CONTACTS_ERROR_NONE;
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);
450 is_storage_changed = 1;
457 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
461 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
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;
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;
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);
482 // if (err == CTS_SUCCESS) {
483 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_list_with_int Success!! \n");
485 // while (contacts_svc_iter_next(iter) == CTS_SUCCESS) {
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);
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);
499 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_contact(%d) Success!\n", item_id);
500 // is_storage_changed = 1;
504 // contacts_svc_value_free(item);
511 // if (iter != NULL) {
512 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
513 // contacts_svc_iter_remove(iter);
518 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_list_with_int Fail !! \n");
519 // ret = _convert_service_error_to_common_error(err);
528 // contacts_svc_value_free(item);
530 // if (iter != NULL) {
531 // contacts_svc_iter_remove(iter);
532 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
534 // if (folder_id_list != NULL) {
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");
544 // free(folder_id_list);
545 // _DEBUG_INFO("[da_contact_plugIn] folder id list free !! \n");
547 // if (folder_type_list != NULL) {
548 // free(folder_type_list);
549 // _DEBUG_INFO("[da_contact_plugIn] folder type list free !! \n");
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;
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;
566 int *contact_id_list = NULL;
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;
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);
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);
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);
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);
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);
611 _DEBUG_INFO("count = %d", count);
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");
620 contact_id_list = (int *)calloc(count, sizeof(int));
622 err = contacts_list_first(list);
623 while (err == CONTACTS_ERROR_NONE) {
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);
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);
639 contact_id_list[index] = item_id;
640 _DEBUG_INFO("[da_contact_plugIn] contact_id_list[%d]: %d\n", index, contact_id_list[index]);
645 err = contacts_list_next(list);
649 for (; i < count; i++) {
650 _DEBUG_INFO("%d = %d", i, contact_id_list[i]);
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);
660 _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_records_async() Success");
661 is_storage_changed = 1;
664 if (contact_id_list != NULL) {
665 free(contact_id_list);
666 contact_id_list = NULL;
670 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_records_with_query Fail !! \n");
671 ret = _convert_service_error_to_common_error(err);
676 contacts_list_destroy(list, true);
680 contacts_filter_destroy(filter);
683 contacts_query_destroy(query);
689 contacts_filter_destroy(filter);
690 contacts_query_destroy(query);
691 contacts_list_destroy(list, true);
693 if (folder_id_list != NULL) {
694 free(folder_id_list);
695 _DEBUG_INFO("[da_contact_plugIn] folder id list free !! \n");
697 if (folder_type_list != NULL) {
698 free(folder_type_list);
699 _DEBUG_INFO("[da_contact_plugIn] folder type list free !! \n");
701 if (contact_id_list != NULL) {
702 free(contact_id_list);
703 contact_id_list = NULL;
711 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
715 // retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
717 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
719 // CTSstruct *item = 0;
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);
730 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_get_contact() Success!\n");
731 // *data = (void *)item;
734 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
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;
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);
749 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_record() Success!\n");
750 *data = (void *)record;
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)
762 retvm_if(folder_name == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_name is NULL. FAIL !!!");
764 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
766 // CTSvalue *folder = 0;
768 // /* new service struct */
769 // folder = contacts_svc_value_new(CTS_VALUE_ADDRESSBOOK);
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
775 // contacts_svc_value_set_int(folder, CTS_ADDRESSBOOK_VAL_ACC_TYPE_INT, CTS_ADDRESSBOOK_TYPE_NONE);
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);
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);
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;
794 // if (folder != NULL)
795 // contacts_svc_value_free(folder);
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;
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);
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);
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);
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);
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);
837 _DEBUG_INFO("[da_contact_plugIn] contacts_db_insert_record() Success!\n");
838 _DEBUG_INFO("[da_contact_plugIn] contact_id = %d", contact_id);
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);
847 *folder_id = g_strdup_printf("%d", folder);
848 is_storage_changed = 1;
853 contacts_record_destroy(record, true);
860 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
864 retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
866 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
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);
875 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_delete_addressbook(%s) Success!\n", folder_id);
876 // is_storage_changed = 1;
879 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
880 contacts_error_e err = CONTACTS_ERROR_NONE;
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);
888 _DEBUG_INFO("[da_contact_plugIn] contacts_db_delete_record(%s) Success!\n", folder_id);
889 is_storage_changed = 1;
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)
901 retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
903 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
905 // CTSvalue *folder = 0;
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;
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;
925 // if (folder != NULL)
926 // contacts_svc_value_free(folder);
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;
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;
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);
951 _DEBUG_INFO("out_folder_name : %s", folder_name);
952 *out_folder_name = folder_name;
953 *out_folder_type = DEFAULT_CONTACT_FOLDER;
958 contacts_record_destroy(record, true);
965 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
969 _DEBUG_INFO("[da_contact_plugIn] service is not supported execute. \n");
974 return SYNC_AGENT_DA_SUCCESS;
977 EXPORT_API int sync_agent_plugin_get_used_item_count(void)
981 _DEBUG_INFO("[da_contact_plugIn] Start\n");
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);
989 // _DEBUG_INFO("[da_contact_plugIn] contact_svc_count() Success\n");
990 // _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
994 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
995 contacts_error_e err = CONTACTS_ERROR_NONE;
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);
1003 _DEBUG_INFO("[da_contact_plugIn] contacts_db_get_count() Success\n");
1004 _DEBUG_INFO("[da_contact_plugIn] used_count = %d\n", used_cnt);
1012 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
1016 retvm_if(folder_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "folder_id is NULL. FAIL !!!");
1018 _DEBUG_INFO("[da_contact_plugIn] Start\n");
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);
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);
1031 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1032 contacts_error_e err = CONTACTS_ERROR_NONE;
1035 contacts_query_h query = NULL;
1036 contacts_filter_h filter = NULL;
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);
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);
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);
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);
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);
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);
1077 contacts_filter_destroy(filter);
1078 contacts_query_destroy(query);
1085 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
1089 // char **folder_id_list = 0;
1090 // *folder_count = 0;
1091 // CTSiter *iter_addressbook = 0;
1092 // CTSvalue *addressbook = 0;
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 !!!");
1100 // *folder_type_list = (int *)calloc(1, sizeof(int));
1101 // if( *folder_type_list == NULL ) {
1102 // _DEBUG_ERROR("CALLOC failed !!!");
1106 // folder_id_list[0] = "0";
1107 // *folder_count = 1;
1108 // (*folder_type_list)[0] = DEFAULT_CONTACT_FOLDER;
1110 // return folder_id_list;
1113 // folder_id_list = (char **)calloc(50, sizeof(char *));
1114 // if( folder_id_list == NULL ) {
1115 // _DEBUG_ERROR("CALLOC failed !!!");
1118 // *folder_type_list = (int *)calloc(50, sizeof(int));
1119 // if( *folder_type_list == NULL ) {
1120 // _DEBUG_ERROR("CALLOC failed !!!");
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);
1134 // folder_id_list[*folder_count] = str_addressbook_id;
1135 // (*folder_type_list)[*folder_count] = DEFAULT_CONTACT_FOLDER;
1136 // *folder_count = *folder_count + 1;
1138 // if (addressbook != NULL)
1139 // contacts_svc_value_free(addressbook);
1141 // err = contacts_svc_iter_next(iter_addressbook);
1143 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_get_info() Fail!\n");
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");
1154 char **folder_id_list = 0;
1157 contacts_error_e err = CONTACTS_ERROR_NONE;
1158 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
1160 contacts_query_h query = NULL;
1161 contacts_filter_h filter = NULL;
1162 contacts_list_h list = NULL;
1163 contacts_record_h record = NULL;
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 !!!");
1171 *folder_type_list = (int *)calloc(1, sizeof(int));
1172 if (*folder_type_list == NULL) {
1173 _DEBUG_ERROR("CALLOC failed !!!");
1177 folder_id_list[0] = "0";
1179 (*folder_type_list)[0] = DEFAULT_CONTACT_FOLDER;
1181 return folder_id_list;
1184 folder_id_list = (char **)calloc(50, sizeof(char *));
1185 if (folder_id_list == NULL) {
1186 _DEBUG_ERROR("CALLOC failed !!!");
1189 *folder_type_list = (int *)calloc(50, sizeof(int));
1190 if (*folder_type_list == NULL) {
1191 _DEBUG_ERROR("CALLOC failed !!!");
1193 if (folder_id_list != NULL)
1194 free(folder_id_list);
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);
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);
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);
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);
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");
1231 err = contacts_list_first(list);
1232 while (err == CONTACTS_ERROR_NONE) {
1233 int addressbook_id = 0;
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);
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);
1249 char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
1250 _DEBUG_INFO("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
1252 folder_id_list[*folder_count] = str_addressbook_id;
1253 (*folder_type_list)[*folder_count] = DEFAULT_CONTACT_FOLDER;
1254 *folder_count = *folder_count + 1;
1256 err = contacts_list_next(list);
1262 contacts_filter_destroy(filter);
1263 contacts_query_destroy(query);
1265 contacts_list_destroy(list, true);
1269 return folder_id_list;
1272 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
1276 _DEBUG_INFO("[da_contact_plugIn] start!!\n");
1278 int error_code = account_connect();
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 !!!");
1285 account_info_list->account_id = -1;
1286 account_info_list->next = 0;
1288 account_foreach_account_from_db(_get_account_id, account_info_list);
1291 account_id_s *cursor_ptr = account_info_list;
1292 while (cursor_ptr != 0) {
1293 cursor_ptr = cursor_ptr->next;
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;
1308 cursor_ptr = account_info_list;
1310 while (cursor_ptr != 0) {
1311 id_list[i] = cursor_ptr->account_id;
1312 cursor_ptr = cursor_ptr->next;
1317 free(account_info_list);
1319 error_code = account_disconnect();
1321 _DEBUG_INFO("[da_contact_plugIn] end!!\n");
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)
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);
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);
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;
1347 err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, ¤t_contact_version);
1348 if (err != CONTACTS_ERROR_NONE) {
1349 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_changes_by_version() Fail, err[%d]", err);
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);
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);
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);
1372 case CONTACTS_CHANGE_INSERTED:
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);
1381 char *contact_id_str = g_strdup_printf("%d", contact_id);
1382 _DEBUG_INFO("[da_contact_plugIn] CTS_OPERATION_INSERTED = [%s]", contact_id_str);
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");
1390 root_ptr->item_id = contact_id_str;
1392 cursor_ptr = root_ptr;
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");
1400 cursor_ptr->next->item_id = contact_id_str;
1401 cursor_ptr->next->next = 0;
1402 cursor_ptr = cursor_ptr->next;
1409 /*_DEBUG_INFO("[da_contact_plugIn] Another Contact Change Noti");*/
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);
1421 _DEBUG_INFO("DACI_FINISH");
1423 *change_count = chagned_count;
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);
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)
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);
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;
1449 // int contact_version = change_point;
1451 // int addressbook_id = atoi(folder_id);
1452 // CTSiter *iter = 0;
1453 // contacts_svc_get_updated_contacts(addressbook_id, contact_version, &iter);
1455 // int contact_ret = contacts_svc_iter_next(iter);
1456 // while (CTS_SUCCESS == contact_ret) {
1457 // CTSvalue *row_info = 0;
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");
1462 // int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
1465 // case CTS_OPERATION_DELETED:
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);
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 !!!");
1477 // root_ptr->item_id = contact_id_str;
1478 // root_ptr->next = 0;
1479 // cursor_ptr = root_ptr;
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 !!!");
1486 // cursor_ptr->next->item_id = contact_id_str;
1487 // cursor_ptr->next->next = 0;
1488 // cursor_ptr = cursor_ptr->next;
1499 // contacts_svc_value_free(row_info); /* next changing */
1501 // contact_ret = contacts_svc_iter_next(iter);
1504 // /* memory free */
1505 // if (iter != NULL) {
1506 // contacts_svc_iter_remove(iter);
1508 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
1511 // *change_count = chagned_count;
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);
1517 sync_agent_plugin_item_node_s *root_ptr = 0;
1518 sync_agent_plugin_item_node_s *cursor_ptr = 0;
1520 int chagned_count = 0;
1521 int contact_version = change_point;
1522 int addressbook_id = atoi(folder_id);
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;
1529 err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, ¤t_contact_version);
1530 if (err != CONTACTS_ERROR_NONE) {
1531 _DEBUG_ERROR("[da_contact_plugIn] contacts_query_set_projection Fail\n");
1535 err = contacts_list_first(list);
1536 while (err == CONTACTS_ERROR_NONE) {
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");
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");
1550 case CONTACTS_CHANGE_DELETED:
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");
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);
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 !!!");
1568 root_ptr->item_id = contact_id_str;
1570 cursor_ptr = root_ptr;
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 !!!");
1577 cursor_ptr->next->item_id = contact_id_str;
1578 cursor_ptr->next->next = 0;
1579 cursor_ptr = cursor_ptr->next;
1590 err = contacts_list_next(list);
1595 *change_count = chagned_count;
1597 contacts_list_destroy(list, true);
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)
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);
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;
1616 // int contact_version = change_point;
1618 // int addressbook_id = atoi(folder_id);
1619 // CTSiter *iter = 0;
1620 // contacts_svc_get_updated_contacts(addressbook_id, contact_version, &iter);
1622 // int contact_ret = contacts_svc_iter_next(iter);
1623 // while (CTS_SUCCESS == contact_ret) {
1624 // CTSvalue *row_info = 0;
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");
1629 // int type = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
1632 // case CTS_OPERATION_UPDATED:
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);
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 !!!");
1644 // root_ptr->item_id = contact_id_str;
1645 // root_ptr->next = 0;
1646 // cursor_ptr = root_ptr;
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 !!!");
1653 // cursor_ptr->next->item_id = contact_id_str;
1654 // cursor_ptr->next->next = 0;
1655 // cursor_ptr = cursor_ptr->next;
1666 // contacts_svc_value_free(row_info); /* next changing */
1668 // contact_ret = contacts_svc_iter_next(iter);
1671 // /* memory free */
1672 // if (iter != NULL) {
1673 // contacts_svc_iter_remove(iter);
1675 // _DEBUG_INFO("[da_contact_plugIn] contacts_svc_iter_remove !! \n");
1678 // *change_count = chagned_count;
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);
1684 sync_agent_plugin_item_node_s *root_ptr = 0;
1685 sync_agent_plugin_item_node_s *cursor_ptr = 0;
1687 int chagned_count = 0;
1688 int contact_version = change_point;
1689 int addressbook_id = atoi(folder_id);
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;
1696 err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &list, ¤t_contact_version);
1697 if (err != CONTACTS_ERROR_NONE) {
1698 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_get_changes_by_version Fail\n");
1702 err = contacts_list_first(list);
1703 while (err == CONTACTS_ERROR_NONE) {
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");
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");
1717 case CONTACTS_CHANGE_UPDATED:
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");
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);
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 !!!");
1735 root_ptr->item_id = contact_id_str;
1737 cursor_ptr = root_ptr;
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 !!!");
1744 cursor_ptr->next->item_id = contact_id_str;
1745 cursor_ptr->next->next = 0;
1746 cursor_ptr = cursor_ptr->next;
1757 err = contacts_list_next(list);
1762 *change_count = chagned_count;
1764 contacts_list_destroy(list, true);
1771 EXPORT_API int sync_agent_plugin_get_last_change_point(void)
1775 // contacts_svc_begin_trans();
1776 // int contactVersion = contacts_svc_end_trans(1);
1779 contacts_db_get_current_version(&contactVersion);
1783 return contactVersion;
1786 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
1790 // contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, _process_contact_change, 0);
1792 // latest_contact_version = get_contact_version(1);
1793 // _DEBUG_INFO("Current Latest Contact Version : %d", latest_contact_version);
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");
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);
1804 latest_contact_version = get_contact_version(1);
1805 _DEBUG_INFO("Current Latest Contact Version : %d", latest_contact_version);
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");
1815 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
1819 callback_add_item = callback;
1824 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
1828 callback_del_item = callback;
1833 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
1837 callback_update_item = callback;
1842 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
1846 callback_get_account_id_list = callback;
1851 EXPORT_API int sync_agent_plugin_get_max_item_count(int folder_type)
1857 return MAX_ITEM_COUNT_CONTACT;
1860 EXPORT_API int sync_agent_plugin_get_max_field_length(int field_name, int child_field_name)
1864 switch (field_name) {
1865 case CONTACT_FIELD_NAME:
1866 switch (child_field_name) {
1867 case CONTACT_CHILD_FIELD_FAMILY:
1869 return MAX_LEN_FAMILY;
1870 case CONTACT_CHILD_FIELD_GIVEN:
1872 return MAX_LEN_GIVEN;
1873 case CONTACT_CHILD_FIELD_MIDDLE:
1875 return MAX_LEN_MIDDLE;
1876 case CONTACT_CHILD_FIELD_PREFIX:
1878 return MAX_LEN_PREFIX;
1879 case CONTACT_CHILD_FIELD_SUFFIX:
1881 return MAX_LEN_SUFFIX;
1882 case CONTACT_CHILD_FIELD_FORMATTED:
1884 return MAX_LEN_FORMATTED;
1885 case CONTACT_CHILD_FIELD_NICKNAME:
1887 return MAX_LEN_NICKNAME;
1888 case CONTACT_CHILD_FIELD_READING_NAME:
1890 return MAX_LEN_READING_NAME;
1891 case CONTACT_CHILD_FIELD_READING_LAST_NAME:
1893 return MAX_LEN_READING_LAST_NAME;
1897 case CONTACT_FIELD_TEL:
1898 switch (child_field_name) {
1899 case CONTACT_CHILD_FIELD_TEL:
1905 case CONTACT_FIELD_EMAIL:
1906 switch (child_field_name) {
1907 case CONTACT_CHILD_FIELD_EMAIL:
1909 return MAX_LEN_EMAIL;
1913 case CONTACT_FIELD_URL:
1914 switch (child_field_name) {
1915 case CONTACT_CHILD_FIELD_URL:
1921 case CONTACT_FIELD_ADDR:
1922 switch (child_field_name) {
1923 case CONTACT_CHILD_FIELD_POBOX:
1925 return MAX_LEN_POBOX;
1926 case CONTACT_CHILD_FIELD_EXT_ADDR:
1928 return MAX_LEN_EXT_ADDR;
1929 case CONTACT_CHILD_FIELD_STREET:
1931 return MAX_LEN_STREET;
1932 case CONTACT_CHILD_FIELD_LOCALITY:
1934 return MAX_LEN_LOCALITY;
1935 case CONTACT_CHILD_FIELD_REGION:
1937 return MAX_LEN_REGION;
1938 case CONTACT_CHILD_FIELD_POSTAL_CODE:
1940 return MAX_LEN_POSTAL_CODE;
1941 case CONTACT_CHILD_FIELD_COUNTRY:
1943 return MAX_LEN_COUNTRY;
1947 case CONTACT_FIELD_NOTE:
1949 return MAX_LEN_NOTE;
1950 case CONTACT_FIELD_COMPANY:
1952 return MAX_LEN_COMPANY;
1953 case CONTACT_FIELD_DEPARTMENT:
1955 return MAX_LEN_DEPARTMENT;
1956 case CONTACT_FIELD_TITLE:
1958 return MAX_LEN_TITLE;
1959 case CONTACT_FIELD_HOBBY:
1961 return MAX_LEN_HOBBY;
1962 case CONTACT_FIELD_PHOTO:
1964 return MAX_LEN_PHOTO;
1970 EXPORT_API int sync_agent_plugin_get_max_field_count(int field_name, int child_field_name)
1974 switch (field_name) {
1975 case CONTACT_FIELD_TEL:
1977 return MAX_FIELD_COUNT_TEL;
1978 case CONTACT_FIELD_EMAIL:
1980 return MAX_FIELD_COUNT_EMAIL;
1981 case CONTACT_FIELD_URL:
1983 return MAX_FIELD_COUNT_URL;
1984 case CONTACT_FIELD_ADDR:
1986 return MAX_FIELD_COUNT_ADDR;
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)
1996 switch (field_name) {
1997 case CONTACT_FIELD_YEAR:
1998 *num_val1 = MIN_PERIOD_YEAR;
1999 *num_val2 = MAX_PERIOD_YEAR;
2007 EXPORT_API int sync_agent_plugin_get_is_support_feature(int feature)
2012 case CONTACT_FEATURE_GENDER:
2014 return IF_SUPPORT_GENDER;
2020 /********************************** static function *******************************************/
2022 static void _set_is_noti_from_me(int set_flag)
2026 if (pthread_mutex_lock(&lockx)) {
2027 _DEBUG_TRACE("[da_contact_plugIn] pthread_mutex_lock error\n");
2030 is_noti_from_me = set_flag;
2031 _DEBUG_TRACE("[da_contact_plugIn] is_noti_from_me set [%d]", set_flag);
2033 if (pthread_mutex_unlock(&lockx)) {
2034 _DEBUG_TRACE("[da_contact_plugIn] pthread_mutex_unlock error\n");
2040 static int _get_is_noti_from_me(void)
2044 _DEBUG_TRACE("[da_contact_plugIn] is_noti_from_me get [%d]", is_noti_from_me);
2048 return is_noti_from_me;
2051 //static void _process_contact_change(void *data)
2053 // _INNER_FUNC_ENTER;
2055 // _DEBUG_TRACE("[da_contact_plugIn] detected contact storage changed!!\n");
2057 // int from_Me = _get_is_noti_from_me();
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++;
2066 // int *contactVersion = (int *)calloc(1, sizeof(int));
2067 // if( contactVersion == NULL ) {
2068 // _DEBUG_ERROR("CALLOC failed !!!");
2071 // *contactVersion = latest_contact_version;
2072 // latest_contact_version++;
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));
2081 // _INNER_FUNC_EXIT;
2084 static void _process_contact_change(const char *view_uri, void *user_data)
2088 int from_Me = _get_is_noti_from_me();
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);
2097 int *contactVersion = (int *)calloc(1, sizeof(int));
2098 if (contactVersion == NULL) {
2099 _DEBUG_ERROR("CALLOC failed !!!");
2103 *contactVersion = latest_contact_version;
2104 latest_contact_version++;
2105 _DEBUG_INFO("latest_contact_version = %d", latest_contact_version);
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));
2116 static void *_rutine_contact_change(void *data)
2120 // _DEBUG_TRACE("[da_contact_plugIn] Start __rutine_Contact_Change (create thread)\n");
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()");
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);
2138 // _DEBUG_TRACE("Detected Change ContactVersion : %d", contact_version);
2141 // int *accountList = callback_get_account_id_list(FW_CONTACT, &count);
2142 // if (accountList == NULL)
2146 // for (; i < count; i++) {
2147 // int contact_account_id = accountList[i];
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);
2152 // CTSiter *iter_addressbook = 0;
2153 // CTSvalue *addressbook = 0;
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);
2159 // if (err != CTS_SUCCESS && contact_account_id == -1) {
2160 // err = CTS_SUCCESS;
2163 // while (err == CTS_SUCCESS) {
2164 // if (contact_account_id != -1)
2165 // addressbook = contacts_svc_iter_get_info(iter_addressbook);
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);
2172 // char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
2173 // _DEBUG_TRACE("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
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);
2179 // int contact_ret = contacts_svc_iter_next(iter);
2180 // while (CTS_SUCCESS == contact_ret) {
2181 // CTSvalue *row_info = 0;
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");
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);
2193 // case CTS_OPERATION_INSERTED:
2195 // _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_INSERTED\n");
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");
2200 // _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_add_item() \n");
2203 // case CTS_OPERATION_UPDATED:
2205 // _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_UPDATED\n");
2206 // _DEBUG_TRACE("[da_contact_plugIn] contact_id : %s\n", contact_id_str);
2208 // int result = callback_update_item(accountList[i], i, contact_id_str, FW_CONTACT);
2210 // _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_update_item() \n");
2213 // case CTS_OPERATION_DELETED:
2215 // _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_DELETED\n");
2216 // int result = callback_del_item(accountList[i], i, contact_id_str, FW_CONTACT);
2218 // _DEBUG_TRACE("[da_contact_plugIn] Failed to call callback_del_item() \n");
2222 // _DEBUG_TRACE("[da_contact_plugIn] Another Contact Change Noti\n");
2227 // contacts_svc_value_free(row_info); /* next changing */
2229 // contact_ret = contacts_svc_iter_next(iter);
2232 // if (contact_account_id == -1)
2233 // err = CTS_ERR_FINISH_ITER;
2235 // err = contacts_svc_iter_next(iter_addressbook);
2237 // if (addressbook != NULL)
2238 // contacts_svc_value_free(addressbook);
2240 // if (iter != NULL)
2241 // contacts_svc_iter_remove(iter);
2243 // if (str_addressbook_id != NULL) {
2244 // free(str_addressbook_id);
2248 // _DEBUG_TRACE("[da_contact_plugIn] account : %d, contact folder does not exist \n", contact_account_id);
2253 // if (contact_account_id != -1) {
2254 // contacts_svc_iter_remove(iter_addressbook);
2258 // /* memory free */
2259 // if (accountList != NULL)
2260 // free(accountList);
2263 // _DEBUG_TRACE("[da_contact_plugIn] End __rutine_Contact_Change (create thread)\n");
2265 _DEBUG_TRACE("[da_contact_plugIn] Start __rutine_Contact_Change (create thread)\n");
2270 queue = sync_agent_alloc_async_queue();
2271 if (queue == NULL) {
2272 _DEBUG_ERROR("Failed to call sync_agent_alloc_async_queue()");
2275 // open contact service
2276 sync_agent_plugin_open_service();
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);
2285 _DEBUG_TRACE("Detected Change ContactVersion : %d", contact_version);
2288 int *accountList = callback_get_account_id_list(FW_CONTACT, &count);
2289 if (accountList == NULL)
2293 for (; i < count; i++) {
2294 int contact_account_id = accountList[i];
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);
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;
2307 unsigned int projections[] = {
2308 _contacts_address_book.id
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");
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");
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");
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");
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");
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);
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;
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");
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");
2356 char *str_addressbook_id = g_strdup_printf("%d", addressbook_id);
2357 _DEBUG_TRACE("[da_contact_plugIn] addressbook_id : %d\n", addressbook_id);
2359 int current_contact_version;
2360 err = contacts_db_get_changes_by_version(_contacts_contact_updated_info._uri, addressbook_id, contact_version, &contact_list, ¤t_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");
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) {
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");
2376 char contact_id_str[12];
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");
2383 snprintf(contact_id_str, sizeof(contact_id_str), "%d", contact_id);
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");
2389 _DEBUG_TRACE("[da_contact_plugIn] $$$$$$$ contact_id :%d\n", contact_id);
2392 case CONTACTS_CHANGE_INSERTED:
2394 _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_INSERTED\n");
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");
2399 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_add_item() \n");
2402 case CONTACTS_CHANGE_UPDATED:
2404 _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_UPDATED\n");
2405 _DEBUG_TRACE("[da_contact_plugIn] contact_id : %s\n", contact_id_str);
2407 int result = callback_update_item(accountList[i], i, contact_id_str, FW_CONTACT);
2409 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_update_item() \n");
2412 case CONTACTS_CHANGE_DELETED:
2414 _DEBUG_TRACE("[da_contact_plugIn] CTS_OPERATION_DELETED\n");
2415 int result = callback_del_item(accountList[i], i, contact_id_str, FW_CONTACT);
2417 _DEBUG_ERROR("[da_contact_plugIn] Failed to call callback_del_item() \n");
2421 _DEBUG_TRACE("[da_contact_plugIn] Another Contact Change Noti\n");
2427 err = contacts_list_next(contact_list);
2430 if (str_addressbook_id != NULL) {
2431 free(str_addressbook_id);
2434 contacts_list_destroy(contact_list, true);
2436 _DEBUG_TRACE("[da_contact_plugIn] account : %d, contact folder does not exist \n", contact_account_id);
2440 err = contacts_list_next(address_book_list);
2441 _DEBUG_TRACE("contacts_list_next = %d", err);
2444 contacts_query_destroy(query);
2445 contacts_filter_destroy(filter);
2446 contacts_list_destroy(address_book_list, true);
2450 if (accountList != NULL)
2455 // close contact service
2456 sync_agent_plugin_close_service();
2458 _DEBUG_TRACE("[da_contact_plugIn] End __rutine_Contact_Change (create thread)\n");
2465 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err)
2469 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
2470 _DEBUG_TRACE("[da_contact_plugIn] Error Code : %d\n", err);
2473 case CONTACTS_ERROR_NONE:
2474 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_NONE");
2475 ret = SYNC_AGENT_DA_SUCCESS;
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;
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;
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;
2489 case CONTACTS_ERROR_DB:
2490 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_DB");
2491 ret = SYNC_AGENT_DA_ERR_SERVICE_DB;
2493 case CONTACTS_ERROR_IPC:
2494 _DEBUG_TRACE("[da_contact_plugIn] Error Code CONTACTS_ERROR_IPC");
2495 ret = SYNC_AGENT_DA_ERR_SERVICE_IPC;
2498 ret = SYNC_AGENT_DA_ERRORS;
2507 static bool _get_account_id(account_h account, void *user_data)
2511 account_id_s *account_info_list = user_data;
2513 bool has_contact = FALSE;
2515 account_get_capability_all(account, __is_contact_id, &has_contact);
2517 if (has_contact == FALSE) {
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()");
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");
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 !!!");
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;
2542 account_id_s *cursor_ptr = account_info_list;
2543 while (cursor_ptr->next != 0) {
2544 cursor_ptr = cursor_ptr->next;
2547 cursor_ptr->next = account_info;
2555 static bool __is_contact_id(/*account_capability_type_e*/ const char* capability_type, account_capability_state_e capability_value, void *user_data)
2558 retvm_if(capability_type == NULL, FALSE, "capability_type is NULL !!");
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;
2571 static void _contacts_db_result_cb(int error, void *user_data)
2574 _DEBUG_TRACE("error = %d", error);