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.
26 #include "utility/sync_util.h"
28 #include "plugin/data_connector_interface.h"
30 #include <tzplatform_config.h>
32 #define BACKUP_PATH tzplatform_mkpath(TZ_USER_CONTENT,"Downloads/Kies/.BnR/CallLog.bk")
35 #define EXPORT_API __attribute__ ((visibility("default")))
38 #ifndef SYNC_AGENT_LOG
40 #define LOG_TAG "PLUGIN_DA_CALL_LOG"
43 #define CALLOG_SYNC_PERIODIC 60*60*24*8 /*8days */
45 /* call log Internal Struct */
59 int extra_data1; /* duration, message_id */
60 const char *extra_data2; /*short message */
61 int related_id; /* contact id */
64 static void _del_all_calllogs(void);
66 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err);
68 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service(void)
72 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
73 contacts_error_e err = contacts_connect2();
74 if (err != CONTACTS_ERROR_NONE) {
75 _DEBUG_ERROR("[da_calllog_plugIn] in calllog Fail!\n");
76 ret = _convert_service_error_to_common_error(err);
78 _DEBUG_INFO("[da_calllog_plugIn] in calllog Success!\n");
86 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service(void)
90 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
91 contacts_error_e err = contacts_disconnect2();
92 if (err != CONTACTS_ERROR_NONE) {
93 _DEBUG_ERROR("[da_calllog_plugIn] in calllog Fail!\n");
94 ret = _convert_service_error_to_common_error(err);
96 _DEBUG_INFO("[da_calllog_plugIn] in calllog Success!\n");
104 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
108 _DEBUG_INFO("[da_calllog_plugIn] calllog Start !!");
109 int *account_id_list = (int *)calloc(1, sizeof(int));
110 if (account_id_list == NULL) {
111 _DEBUG_ERROR("CALLOC failed !!!");
114 account_id_list[0] = -1;
117 _DEBUG_INFO("[da_calllog_plugIn] calllog End !!");
121 return account_id_list;
124 EXPORT_API sync_agent_da_return_e sync_agent_plugin_backup_service_items_to_file(int account_ID, char **file_path)
128 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
131 // CTSiter *iter = 0;
132 // CTSvalue *plog = 0; /*plog_list */
134 // file = fopen(BACKUP_PATH, "w");
135 // if (file == NULL) {
136 // _DEBUG_INFO("[da_call_log_plugIn] fopen fail\n");
137 // return SYNC_AGENT_DA_ERRORS;
140 // contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter);
141 // while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
142 // dc_plog_list_s plog_list;
144 // /* get call log */
145 // plog = contacts_svc_iter_get_info(iter);
149 // plog_list.number = contacts_svc_value_get_str(plog, CTS_LIST_PLOG_NUMBER_STR);
150 // plog_list.log_time = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_LOG_TIME_INT);
151 // plog_list.log_type = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_LOG_TYPE_INT);
152 // plog_list.extra_data1 = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_DURATION_INT);
153 // plog_list.extra_data2 = contacts_svc_value_get_str(plog, CTS_LIST_PLOG_SHORTMSG_STR);
154 // plog_list.related_id = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_RELATED_ID_INT);
156 // _DEBUG_INFO("plog_list.number : %s\n", plog_list.number);
157 // _DEBUG_INFO("plog_list.related_id : %d\n", plog_list.related_id);
158 // _DEBUG_INFO("plog_list.log_time : %d\n", plog_list.log_time);
159 // _DEBUG_INFO("plog_list.log_type : %d\n", plog_list.log_type);
160 // _DEBUG_INFO("plog_list.extra_data1 : %d\n", plog_list.extra_data1);
161 // _DEBUG_INFO("plog_list.extra_data2 : %s\n", plog_list.extra_data2);
163 // /* save call log : to file text */
164 // cur_size = fprintf(file, "%s %d %d %d %d %s ", plog_list.number, plog_list.related_id, plog_list.log_time, plog_list.log_type, plog_list.extra_data1, plog_list.extra_data2);
166 // if (cur_size == -1) {
167 // _DEBUG_INFO("[da_call_log_plugIn] fprintf fail\n");
168 // ret = SYNC_AGENT_DA_ERRORS;
173 // if (plog != NULL) {
174 // contacts_svc_value_free(plog);
181 // if (file != NULL) {
187 // if (plog != NULL) {
188 // contacts_svc_value_free(plog);
192 // if (iter != NULL) {
193 // contacts_svc_iter_remove(iter);
197 // *file_path = strdup(BACKUP_PATH);
199 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
203 contacts_error_e err = CONTACTS_ERROR_NONE;
204 contacts_list_h list = NULL;
211 char *short_msg = NULL;
213 file = fopen(BACKUP_PATH, "w");
215 _DEBUG_ERROR("[da_call_log_plugIn] fopen fail\n");
216 return SYNC_AGENT_DA_ERRORS;
219 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
220 if (err != CONTACTS_ERROR_NONE) {
221 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records fail\n");
222 ret = _convert_service_error_to_common_error(err);
226 err = contacts_list_first(list);
227 while (err == CONTACTS_ERROR_NONE) {
228 contacts_record_h record = NULL;
231 err = contacts_list_get_current_record_p(list, &record);
232 if (err != CONTACTS_ERROR_NONE) {
233 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p fail\n");
237 contacts_record_get_str_p(record, _contacts_phone_log.address, &number);
238 contacts_record_get_int(record, _contacts_phone_log.person_id, &related_id);
239 contacts_record_get_int(record, _contacts_phone_log.log_time, &log_time);
240 contacts_record_get_int(record, _contacts_phone_log.log_type, &log_type);
241 contacts_record_get_int(record, _contacts_phone_log.extra_data1, &duration);
242 contacts_record_get_str_p(record, _contacts_phone_log.extra_data2, &short_msg);
244 _DEBUG_INFO("plog_list.number : %s\n", number);
245 _DEBUG_INFO("plog_list.related_id : %d\n", related_id);
246 _DEBUG_INFO("plog_list.log_time : %d\n", log_time);
247 _DEBUG_INFO("plog_list.log_type : %d\n", log_type);
248 _DEBUG_INFO("plog_list.extra_data1 : %d\n", duration);
249 _DEBUG_INFO("plog_list.extra_data2 : %s\n", short_msg);
251 /* save call log : to file text */
252 cur_size = fprintf(file, "%s %d %d %d %d %s ", number, related_id, log_time, log_type, duration, short_msg);
253 if (cur_size == -1) {
254 _DEBUG_ERROR("[da_call_log_plugIn] fprintf fail\n");
255 ret = SYNC_AGENT_DA_ERRORS;
259 err = contacts_list_next(list);
269 contacts_list_destroy(list, true);
271 *file_path = strdup(BACKUP_PATH);
278 EXPORT_API sync_agent_da_return_e sync_agent_plugin_restore_service_items_from_file(int account_ID, const char *file_path)
282 retvm_if(file_path == NULL, SYNC_AGENT_DA_ERRORS, "[da_call_log_plugIn] file_path is NULL. FAIL !!!");
284 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
289 contacts_record_h record = NULL;
291 /* open call log : from file text */
292 file = fopen(BACKUP_PATH, "r");
294 _DEBUG_ERROR("[da_call_log_plugIn] fopen() fail");
295 return SYNC_AGENT_DA_ERRORS;
298 /* delete call log from contact service database */
302 dc_plog_list_s plog_list;
303 char temp_number[100] = { 0 };
304 char temp_extra_data2[1024] = { 0 };
306 /* read call log : from file text */
307 cur_size = fscanf(file, "%100s %d %d %d %d %1024s ", temp_number, &(plog_list.related_id), &(plog_list.log_time), &(plog_list.log_type), &(plog_list.extra_data1), temp_extra_data2);
308 if (cur_size == -1) {
309 _DEBUG_INFO("[da_call_log_plugIn] The end of file");
310 ret = SYNC_AGENT_DA_SUCCESS;
314 err = contacts_record_create(_contacts_phone_log._uri, &record);
315 if (err != CONTACTS_ERROR_NONE) {
316 _DEBUG_ERROR("[da_call_log_plugIn] contacts_record_create() Fail, err[%d]", err);
317 ret = _convert_service_error_to_common_error(err);
321 /* add call log to contact service database */
322 contacts_record_set_str(record, _contacts_phone_log.address, temp_number);
323 contacts_record_set_int(record, _contacts_phone_log.person_id, plog_list.related_id);
324 contacts_record_set_int(record, _contacts_phone_log.log_time, plog_list.log_time);
325 contacts_record_set_int(record, _contacts_phone_log.log_type, plog_list.log_type);
326 contacts_record_set_int(record, _contacts_phone_log.extra_data1, plog_list.extra_data1);
327 contacts_record_set_str(record, _contacts_phone_log.extra_data2, temp_extra_data2);
329 _DEBUG_INFO("[da_contact_plugIn] plog_list.number : [%s]", temp_number);
330 _DEBUG_INFO("[da_contact_plugIn] plog_list.related_id : [%d]", plog_list.related_id);
331 _DEBUG_INFO("[da_contact_plugIn] plog_list.log_time : [%d]", plog_list.log_time);
332 _DEBUG_INFO("[da_contact_plugIn] plog_list.log_type : [%d]", plog_list.log_type);
333 _DEBUG_INFO("[da_contact_plugIn] plog_list.extra_data1 : [%d]", plog_list.extra_data1);
334 _DEBUG_INFO("[da_contact_plugIn] plog_list.extra_data2 : [%s]", temp_extra_data2);
336 err = contacts_db_insert_record(record, &contact_id);
337 if (err != CONTACTS_ERROR_NONE) {
338 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_record() Fail, err[%d]", err);
339 ret = _convert_service_error_to_common_error(err);
342 _DEBUG_INFO("[da_contact_plugIn] contacts_db_insert_record() Success, contact_id = [%d]", contact_id);
347 _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
349 contacts_record_destroy(record, true);
360 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
364 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
366 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
367 // cts_error err = 0;
368 // CTSvalue *item = 0;
371 // int colllog_id = atoi(item_id);
372 // _DEBUG_INFO("[da_call-log_plugIn] colllog_id : %d\n", colllog_id);
373 // err = contacts_svc_get_phonelog(colllog_id, &item);
374 // if (err < CTS_SUCCESS) {
375 // _DEBUG_INFO("[da_call-log_plugIn] contacts_svc_get_phonelog() Fail!\n");
376 // ret = _convert_service_error_to_common_error(err);
379 // _DEBUG_INFO("[da_call-log_plugIn] contacts_svc_get_phonelog() Success!\n");
380 // *data = (void *)item;
383 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
385 contacts_record_h record = NULL;
388 int colllog_id = atoi(item_id);
389 _DEBUG_INFO("[da_call-log_plugIn] colllog_id : %d\n", colllog_id);
390 err = contacts_db_get_record(_contacts_phone_log._uri, colllog_id, &record);
391 if (err != CONTACTS_ERROR_NONE) {
392 _DEBUG_ERROR("[da_call-log_plugIn] contacts_db_get_record() Fail!\n");
393 ret = _convert_service_error_to_common_error(err);
396 _DEBUG_INFO("[da_call-log_plugIn] contacts_db_get_record() Success!\n");
397 *data = (void *)record;
405 static void _del_all_calllogs(void)
409 contacts_error_e err = CONTACTS_ERROR_NONE;
410 contacts_list_h list = NULL;
412 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
413 if (err != CONTACTS_ERROR_NONE) {
414 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records() fail, err[%d]", err);
415 err = contacts_list_destroy(list, true);
416 if (err != CONTACTS_ERROR_NONE) {
417 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
422 err = contacts_list_first(list);
423 while (err == CONTACTS_ERROR_NONE) {
424 contacts_record_h record = NULL;
427 err = contacts_list_get_current_record_p(list, &record);
428 if (err != CONTACTS_ERROR_NONE) {
429 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p() fail, err[%d]", err);
434 err = contacts_record_get_int(record, _contacts_phone_log.id, &id);
435 if (err != CONTACTS_ERROR_NONE) {
436 _DEBUG_ERROR("[da_call_log_plugIn] contacts_record_get_int() fail, err[%d]", err);
440 _DEBUG_TRACE("[da_call_log_plugIn] id : [%d]", id);
442 /* delete call log */
443 contacts_db_delete_record(_contacts_phone_log._uri, id);
445 err = contacts_list_next(list);
449 err = contacts_list_destroy(list, true);
450 if (err != CONTACTS_ERROR_NONE) {
451 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
457 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 changepoint, int *changeCount)
461 _DEBUG_INFO("[da_call-log_plugIn] account_id : [%d]", account_id);
462 _DEBUG_INFO("[da_call-log_plugIn] folder_id : [%s]", folder_id);
463 _DEBUG_INFO("[da_call-log_plugIn] changepoint : [%d]", changepoint);
465 sync_agent_plugin_item_node_s *root_ptr = 0;
466 sync_agent_plugin_item_node_s *cursor_ptr = 0;
467 int chagned_count = 0;
468 int current_time = time(NULL);
470 char *calllog_id_str = NULL;
472 contacts_error_e err = CONTACTS_ERROR_NONE;
473 contacts_list_h list = NULL;
475 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
476 if (err != CONTACTS_ERROR_NONE) {
477 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records() fail, err[%d]", err);
478 err = contacts_list_destroy(list, true);
479 if (err != CONTACTS_ERROR_NONE) {
480 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
485 err = contacts_list_first(list);
486 while (err == CONTACTS_ERROR_NONE) {
487 contacts_record_h record = NULL;
490 err = contacts_list_get_current_record_p(list, &record);
491 if (err != CONTACTS_ERROR_NONE) {
492 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p() fail, err[%d]", err);
496 /*check that item is within 7 days before since at the time */
498 contacts_record_get_int(record, _contacts_phone_log.log_time, &log_time);
500 if (current_time - CALLOG_SYNC_PERIODIC <= log_time) {
502 contacts_record_get_int(record, _contacts_phone_log.id, &calllog_id);
504 calllog_id_str = g_strdup_printf("%d", calllog_id);
505 _DEBUG_INFO("[da_call_log_plugIn] Calllog_id_str = [%s]", calllog_id_str);
507 if (root_ptr == NULL) {
508 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
509 if (root_ptr == NULL) {
510 _DEBUG_ERROR("[da_call_log_plugIn] Calloc failed, root_ptr is NULL");
511 err = contacts_list_destroy(list, true);
512 if (err != CONTACTS_ERROR_NONE) {
513 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
517 root_ptr->item_id = calllog_id_str;
519 cursor_ptr = root_ptr;
521 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
522 if (cursor_ptr->next == NULL) {
523 _DEBUG_ERROR("[da_call_log_plugIn] Calloc failed, cursor_ptr->next is NULL");
524 err = contacts_list_destroy(list, true);
525 if (err != CONTACTS_ERROR_NONE) {
526 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
528 if (root_ptr != NULL) {
534 cursor_ptr->next->item_id = calllog_id_str;
535 cursor_ptr->next->next = 0;
536 cursor_ptr = cursor_ptr->next;
542 err = contacts_list_next(list);
545 err = contacts_list_destroy(list, true);
546 if (err != CONTACTS_ERROR_NONE) {
547 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
550 *changeCount = chagned_count;
556 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)
560 _DEBUG_INFO("[da_call-log_plugIn] account_id : %d\n", account_id);
561 _DEBUG_INFO("[da_call-log_plugIn] folder_id : %s\n", folder_id);
562 _DEBUG_INFO("[da_call-log_plugIn] changepoint : %d\n", change_point);
571 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)
575 _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
576 _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
577 _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
586 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err)
590 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
591 _DEBUG_TRACE("[da_contact_plugIn] Error Code : %d\n", err);
594 case CONTACTS_ERROR_NONE:
595 ret = SYNC_AGENT_DA_SUCCESS;
597 case CONTACTS_ERROR_OUT_OF_MEMORY:
598 ret = SYNC_AGENT_DA_ERR_MEMORY_FULL;
600 case CONTACTS_ERROR_INVALID_PARAMETER:
601 ret = SYNC_AGENT_DA_ERR_INVALID_PARAMETER;
603 case CONTACTS_ERROR_NO_DATA:
604 ret = SYNC_AGENT_DA_ERR_NO_DATA;
606 case CONTACTS_ERROR_DB:
607 ret = SYNC_AGENT_DA_ERR_SERVICE_DB;
609 case CONTACTS_ERROR_IPC:
610 ret = SYNC_AGENT_DA_ERR_SERVICE_IPC;
613 ret = SYNC_AGENT_DA_ERRORS;
622 EXPORT_API int sync_agent_plugin_get_last_change_point(void)
626 // contacts_svc_begin_trans();
627 // int contact_version = contacts_svc_end_trans(1);
630 contacts_db_get_current_version(&contact_version);
634 return contact_version;