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 #define BACKUP_PATH "/opt/usr/media/Downloads/Kies/.BnR/CallLog.bk"
33 #define EXPORT_API __attribute__ ((visibility("default")))
36 #ifndef SYNC_AGENT_LOG
38 #define LOG_TAG "PLUGIN_DA_CALL_LOG"
41 #define CALLOG_SYNC_PERIODIC 60*60*24*8 /*8days */
43 /* call log Internal Struct */
57 int extra_data1; /* duration, message_id */
58 const char *extra_data2; /*short message */
59 int related_id; /* contact id */
62 static void _del_all_calllogs(void);
64 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err);
66 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service(void)
70 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
71 contacts_error_e err = contacts_connect2();
72 if (err != CONTACTS_ERROR_NONE) {
73 _DEBUG_ERROR("[da_calllog_plugIn] in calllog Fail!\n");
74 ret = _convert_service_error_to_common_error(err);
76 _DEBUG_INFO("[da_calllog_plugIn] in calllog Success!\n");
84 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service(void)
88 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
89 contacts_error_e err = contacts_disconnect2();
90 if (err != CONTACTS_ERROR_NONE) {
91 _DEBUG_ERROR("[da_calllog_plugIn] in calllog Fail!\n");
92 ret = _convert_service_error_to_common_error(err);
94 _DEBUG_INFO("[da_calllog_plugIn] in calllog Success!\n");
102 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
106 _DEBUG_INFO("[da_calllog_plugIn] calllog Start !!");
107 int *account_id_list = (int *)calloc(1, sizeof(int));
108 if (account_id_list == NULL) {
109 _DEBUG_ERROR("CALLOC failed !!!");
112 account_id_list[0] = -1;
115 _DEBUG_INFO("[da_calllog_plugIn] calllog End !!");
119 return account_id_list;
122 EXPORT_API sync_agent_da_return_e sync_agent_plugin_backup_service_items_to_file(int account_ID, char **file_path)
126 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
129 // CTSiter *iter = 0;
130 // CTSvalue *plog = 0; /*plog_list */
132 // file = fopen(BACKUP_PATH, "w");
133 // if (file == NULL) {
134 // _DEBUG_INFO("[da_call_log_plugIn] fopen fail\n");
135 // return SYNC_AGENT_DA_ERRORS;
138 // contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter);
139 // while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
140 // dc_plog_list_s plog_list;
142 // /* get call log */
143 // plog = contacts_svc_iter_get_info(iter);
147 // plog_list.number = contacts_svc_value_get_str(plog, CTS_LIST_PLOG_NUMBER_STR);
148 // plog_list.log_time = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_LOG_TIME_INT);
149 // plog_list.log_type = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_LOG_TYPE_INT);
150 // plog_list.extra_data1 = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_DURATION_INT);
151 // plog_list.extra_data2 = contacts_svc_value_get_str(plog, CTS_LIST_PLOG_SHORTMSG_STR);
152 // plog_list.related_id = contacts_svc_value_get_int(plog, CTS_LIST_PLOG_RELATED_ID_INT);
154 // _DEBUG_INFO("plog_list.number : %s\n", plog_list.number);
155 // _DEBUG_INFO("plog_list.related_id : %d\n", plog_list.related_id);
156 // _DEBUG_INFO("plog_list.log_time : %d\n", plog_list.log_time);
157 // _DEBUG_INFO("plog_list.log_type : %d\n", plog_list.log_type);
158 // _DEBUG_INFO("plog_list.extra_data1 : %d\n", plog_list.extra_data1);
159 // _DEBUG_INFO("plog_list.extra_data2 : %s\n", plog_list.extra_data2);
161 // /* save call log : to file text */
162 // 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);
164 // if (cur_size == -1) {
165 // _DEBUG_INFO("[da_call_log_plugIn] fprintf fail\n");
166 // ret = SYNC_AGENT_DA_ERRORS;
171 // if (plog != NULL) {
172 // contacts_svc_value_free(plog);
179 // if (file != NULL) {
185 // if (plog != NULL) {
186 // contacts_svc_value_free(plog);
190 // if (iter != NULL) {
191 // contacts_svc_iter_remove(iter);
195 // *file_path = strdup(BACKUP_PATH);
197 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
201 contacts_error_e err = CONTACTS_ERROR_NONE;
202 contacts_list_h list = NULL;
209 char *short_msg = NULL;
211 file = fopen(BACKUP_PATH, "w");
213 _DEBUG_ERROR("[da_call_log_plugIn] fopen fail\n");
214 return SYNC_AGENT_DA_ERRORS;
217 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
218 if (err != CONTACTS_ERROR_NONE) {
219 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records fail\n");
220 ret = _convert_service_error_to_common_error(err);
224 err = contacts_list_first(list);
225 while (err == CONTACTS_ERROR_NONE) {
226 contacts_record_h record = NULL;
229 err = contacts_list_get_current_record_p(list, &record);
230 if (err != CONTACTS_ERROR_NONE) {
231 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p fail\n");
235 contacts_record_get_str_p(record, _contacts_phone_log.address, &number);
236 contacts_record_get_int(record, _contacts_phone_log.person_id, &related_id);
237 contacts_record_get_int(record, _contacts_phone_log.log_time, &log_time);
238 contacts_record_get_int(record, _contacts_phone_log.log_type, &log_type);
239 contacts_record_get_int(record, _contacts_phone_log.extra_data1, &duration);
240 contacts_record_get_str_p(record, _contacts_phone_log.extra_data2, &short_msg);
242 _DEBUG_INFO("plog_list.number : %s\n", number);
243 _DEBUG_INFO("plog_list.related_id : %d\n", related_id);
244 _DEBUG_INFO("plog_list.log_time : %d\n", log_time);
245 _DEBUG_INFO("plog_list.log_type : %d\n", log_type);
246 _DEBUG_INFO("plog_list.extra_data1 : %d\n", duration);
247 _DEBUG_INFO("plog_list.extra_data2 : %s\n", short_msg);
249 /* save call log : to file text */
250 cur_size = fprintf(file, "%s %d %d %d %d %s ", number, related_id, log_time, log_type, duration, short_msg);
251 if (cur_size == -1) {
252 _DEBUG_ERROR("[da_call_log_plugIn] fprintf fail\n");
253 ret = SYNC_AGENT_DA_ERRORS;
257 err = contacts_list_next(list);
267 contacts_list_destroy(list, true);
269 *file_path = strdup(BACKUP_PATH);
276 EXPORT_API sync_agent_da_return_e sync_agent_plugin_restore_service_items_from_file(int account_ID, const char *file_path)
280 retvm_if(file_path == NULL, SYNC_AGENT_DA_ERRORS, "[da_call_log_plugIn] file_path is NULL. FAIL !!!");
282 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
287 contacts_record_h record = NULL;
289 /* open call log : from file text */
290 file = fopen(BACKUP_PATH, "r");
292 _DEBUG_ERROR("[da_call_log_plugIn] fopen() fail");
293 return SYNC_AGENT_DA_ERRORS;
296 /* delete call log from contact service database */
300 dc_plog_list_s plog_list;
301 char temp_number[100] = { 0 };
302 char temp_extra_data2[1024] = { 0 };
304 /* read call log : from file text */
305 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);
306 if (cur_size == -1) {
307 _DEBUG_INFO("[da_call_log_plugIn] The end of file");
308 ret = SYNC_AGENT_DA_SUCCESS;
312 err = contacts_record_create(_contacts_phone_log._uri, &record);
313 if (err != CONTACTS_ERROR_NONE) {
314 _DEBUG_ERROR("[da_call_log_plugIn] contacts_record_create() Fail, err[%d]", err);
315 ret = _convert_service_error_to_common_error(err);
319 /* add call log to contact service database */
320 contacts_record_set_str(record, _contacts_phone_log.address, temp_number);
321 contacts_record_set_int(record, _contacts_phone_log.person_id, plog_list.related_id);
322 contacts_record_set_int(record, _contacts_phone_log.log_time, plog_list.log_time);
323 contacts_record_set_int(record, _contacts_phone_log.log_type, plog_list.log_type);
324 contacts_record_set_int(record, _contacts_phone_log.extra_data1, plog_list.extra_data1);
325 contacts_record_set_str(record, _contacts_phone_log.extra_data2, temp_extra_data2);
327 _DEBUG_INFO("[da_contact_plugIn] plog_list.number : [%s]", temp_number);
328 _DEBUG_INFO("[da_contact_plugIn] plog_list.related_id : [%d]", plog_list.related_id);
329 _DEBUG_INFO("[da_contact_plugIn] plog_list.log_time : [%d]", plog_list.log_time);
330 _DEBUG_INFO("[da_contact_plugIn] plog_list.log_type : [%d]", plog_list.log_type);
331 _DEBUG_INFO("[da_contact_plugIn] plog_list.extra_data1 : [%d]", plog_list.extra_data1);
332 _DEBUG_INFO("[da_contact_plugIn] plog_list.extra_data2 : [%s]", temp_extra_data2);
334 err = contacts_db_insert_record(record, &contact_id);
335 if (err != CONTACTS_ERROR_NONE) {
336 _DEBUG_ERROR("[da_contact_plugIn] contacts_db_insert_record() Fail, err[%d]", err);
337 ret = _convert_service_error_to_common_error(err);
340 _DEBUG_INFO("[da_contact_plugIn] contacts_db_insert_record() Success, contact_id = [%d]", contact_id);
345 _DEBUG_INFO("[da_contact_plugIn] DACI_FINISH");
347 contacts_record_destroy(record, true);
358 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
362 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
364 // sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
365 // cts_error err = 0;
366 // CTSvalue *item = 0;
369 // int colllog_id = atoi(item_id);
370 // _DEBUG_INFO("[da_call-log_plugIn] colllog_id : %d\n", colllog_id);
371 // err = contacts_svc_get_phonelog(colllog_id, &item);
372 // if (err < CTS_SUCCESS) {
373 // _DEBUG_INFO("[da_call-log_plugIn] contacts_svc_get_phonelog() Fail!\n");
374 // ret = _convert_service_error_to_common_error(err);
377 // _DEBUG_INFO("[da_call-log_plugIn] contacts_svc_get_phonelog() Success!\n");
378 // *data = (void *)item;
381 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
383 contacts_record_h record = NULL;
386 int colllog_id = atoi(item_id);
387 _DEBUG_INFO("[da_call-log_plugIn] colllog_id : %d\n", colllog_id);
388 err = contacts_db_get_record(_contacts_phone_log._uri, colllog_id, &record);
389 if (err != CONTACTS_ERROR_NONE) {
390 _DEBUG_ERROR("[da_call-log_plugIn] contacts_db_get_record() Fail!\n");
391 ret = _convert_service_error_to_common_error(err);
394 _DEBUG_INFO("[da_call-log_plugIn] contacts_db_get_record() Success!\n");
395 *data = (void *)record;
403 static void _del_all_calllogs(void)
407 contacts_error_e err = CONTACTS_ERROR_NONE;
408 contacts_list_h list = NULL;
410 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
411 if (err != CONTACTS_ERROR_NONE) {
412 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records() fail, err[%d]", err);
413 err = contacts_list_destroy(list, true);
414 if (err != CONTACTS_ERROR_NONE) {
415 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
420 err = contacts_list_first(list);
421 while (err == CONTACTS_ERROR_NONE) {
422 contacts_record_h record = NULL;
425 err = contacts_list_get_current_record_p(list, &record);
426 if (err != CONTACTS_ERROR_NONE) {
427 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p() fail, err[%d]", err);
432 err = contacts_record_get_int(record, _contacts_phone_log.id, &id);
433 if (err != CONTACTS_ERROR_NONE) {
434 _DEBUG_ERROR("[da_call_log_plugIn] contacts_record_get_int() fail, err[%d]", err);
438 _DEBUG_TRACE("[da_call_log_plugIn] id : [%d]", id);
440 /* delete call log */
441 contacts_db_delete_record(_contacts_phone_log._uri, id);
443 err = contacts_list_next(list);
447 err = contacts_list_destroy(list, true);
448 if (err != CONTACTS_ERROR_NONE) {
449 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
455 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)
459 _DEBUG_INFO("[da_call-log_plugIn] account_id : [%d]", account_id);
460 _DEBUG_INFO("[da_call-log_plugIn] folder_id : [%s]", folder_id);
461 _DEBUG_INFO("[da_call-log_plugIn] changepoint : [%d]", changepoint);
463 sync_agent_plugin_item_node_s *root_ptr = 0;
464 sync_agent_plugin_item_node_s *cursor_ptr = 0;
465 int chagned_count = 0;
466 int current_time = time(NULL);
468 char *calllog_id_str = NULL;
470 contacts_error_e err = CONTACTS_ERROR_NONE;
471 contacts_list_h list = NULL;
473 err = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &list);
474 if (err != CONTACTS_ERROR_NONE) {
475 _DEBUG_ERROR("[da_call_log_plugIn] contacts_db_get_all_records() fail, err[%d]", err);
476 err = contacts_list_destroy(list, true);
477 if (err != CONTACTS_ERROR_NONE) {
478 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
483 err = contacts_list_first(list);
484 while (err == CONTACTS_ERROR_NONE) {
485 contacts_record_h record = NULL;
488 err = contacts_list_get_current_record_p(list, &record);
489 if (err != CONTACTS_ERROR_NONE) {
490 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_get_current_record_p() fail, err[%d]", err);
494 /*check that item is within 7 days before since at the time */
496 contacts_record_get_int(record, _contacts_phone_log.log_time, &log_time);
498 if (current_time - CALLOG_SYNC_PERIODIC <= log_time) {
500 contacts_record_get_int(record, _contacts_phone_log.id, &calllog_id);
502 calllog_id_str = g_strdup_printf("%d", calllog_id);
503 _DEBUG_INFO("[da_call_log_plugIn] Calllog_id_str = [%s]", calllog_id_str);
505 if (root_ptr == NULL) {
506 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
507 if (root_ptr == NULL) {
508 _DEBUG_ERROR("[da_call_log_plugIn] Calloc failed, root_ptr is NULL");
509 err = contacts_list_destroy(list, true);
510 if (err != CONTACTS_ERROR_NONE) {
511 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
515 root_ptr->item_id = calllog_id_str;
517 cursor_ptr = root_ptr;
519 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
520 if (cursor_ptr->next == NULL) {
521 _DEBUG_ERROR("[da_call_log_plugIn] Calloc failed, cursor_ptr->next is NULL");
522 err = contacts_list_destroy(list, true);
523 if (err != CONTACTS_ERROR_NONE) {
524 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
526 if (root_ptr != NULL) {
532 cursor_ptr->next->item_id = calllog_id_str;
533 cursor_ptr->next->next = 0;
534 cursor_ptr = cursor_ptr->next;
540 err = contacts_list_next(list);
543 err = contacts_list_destroy(list, true);
544 if (err != CONTACTS_ERROR_NONE) {
545 _DEBUG_ERROR("[da_call_log_plugIn] contacts_list_destroy() fail, err[%d]", err);
548 *changeCount = chagned_count;
554 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)
558 _DEBUG_INFO("[da_call-log_plugIn] account_id : %d\n", account_id);
559 _DEBUG_INFO("[da_call-log_plugIn] folder_id : %s\n", folder_id);
560 _DEBUG_INFO("[da_call-log_plugIn] changepoint : %d\n", change_point);
569 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)
573 _DEBUG_INFO("\n[da_contact_plugIn] account_id : %d\n", account_id);
574 _DEBUG_INFO("[da_contact_plugIn] folder_id : %s\n", folder_id);
575 _DEBUG_INFO("[da_contact_plugIn] changepoint : %d\n", change_point);
584 static sync_agent_da_return_e _convert_service_error_to_common_error(contacts_error_e err)
588 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
589 _DEBUG_TRACE("[da_contact_plugIn] Error Code : %d\n", err);
592 case CONTACTS_ERROR_NONE:
593 ret = SYNC_AGENT_DA_SUCCESS;
595 case CONTACTS_ERROR_OUT_OF_MEMORY:
596 ret = SYNC_AGENT_DA_ERR_MEMORY_FULL;
598 case CONTACTS_ERROR_INVALID_PARAMETER:
599 ret = SYNC_AGENT_DA_ERR_INVALID_PARAMETER;
601 case CONTACTS_ERROR_NO_DATA:
602 ret = SYNC_AGENT_DA_ERR_NO_DATA;
604 case CONTACTS_ERROR_DB:
605 ret = SYNC_AGENT_DA_ERR_SERVICE_DB;
607 case CONTACTS_ERROR_IPC:
608 ret = SYNC_AGENT_DA_ERR_SERVICE_IPC;
611 ret = SYNC_AGENT_DA_ERRORS;
620 EXPORT_API int sync_agent_plugin_get_last_change_point(void)
624 // contacts_svc_begin_trans();
625 // int contact_version = contacts_svc_end_trans(1);
628 contacts_db_get_current_version(&contact_version);
632 return contact_version;