From 1617069f6a5fea9e974a5414dc80508908871455 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 8 Sep 2015 21:44:50 +0200 Subject: [PATCH] Few fixes + added migrate method from rc store logs --- coreapi/call_log.c | 13 ++++----- coreapi/linphonecore.c | 66 ++++++++++++++++++++++++++++++++++++++++-- coreapi/linphonecore.h | 7 +++++ gtk/main.c | 4 +++ 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/coreapi/call_log.c b/coreapi/call_log.c index cc378d879..b3d9cbb75 100644 --- a/coreapi/call_log.c +++ b/coreapi/call_log.c @@ -376,7 +376,7 @@ void linphone_core_call_log_storage_init(LinphoneCore *lc) { const char *errmsg; sqlite3 *db; - linphone_core_message_storage_close(lc); + linphone_core_call_log_storage_close(lc); ret=_linphone_sqlite3_open(lc->logs_db_file, &db); if(ret != SQLITE_OK) { @@ -433,7 +433,7 @@ static int create_call_log(void *data, int argc, char **argv, char **colName) { log->video_enabled = atoi(argv[8]) == 1; log->quality = atof(argv[9]); - *list = ms_list_prepend(*list, log); + *list = ms_list_append(*list, log); return 0; } @@ -443,7 +443,7 @@ void linphone_sql_request_call_log(sqlite3 *db, const char *stmt, MSList **list) int ret; ret = sqlite3_exec(db, stmt, create_call_log, list, &errmsg); if (ret != SQLITE_OK) { - ms_error("Error in creation: %s.", errmsg); + ms_error("linphone_sql_request: statement %s -> error sqlite3_exec(): %s.", stmt, errmsg); sqlite3_free(errmsg); } } @@ -497,7 +497,7 @@ const MSList *linphone_core_get_call_history(LinphoneCore *lc) { lc->call_logs = ms_list_free_with_data(lc->call_logs, (void (*)(void*))linphone_call_log_unref); - buf = sqlite3_mprintf("SELECT * FROM call_history ORDER BY id ASC LIMIT %i", lc->max_call_logs); + buf = sqlite3_mprintf("SELECT * FROM call_history ORDER BY id DESC LIMIT %i", lc->max_call_logs); begin = ortp_get_cur_time_ms(); linphone_sql_request_call_log(lc->logs_db, buf, &lc->call_logs); @@ -526,9 +526,6 @@ void linphone_core_delete_call_log(LinphoneCore *lc, LinphoneCallLog *log) { buf = sqlite3_mprintf("DELETE FROM call_history WHERE id = %i", log->storage_id); linphone_sql_request_generic(lc->logs_db, buf); sqlite3_free(buf); - - lc->call_logs = ms_list_remove(lc->call_logs, log); - linphone_call_log_unref(log); } int linphone_core_get_call_history_size(LinphoneCore *lc) { @@ -562,7 +559,7 @@ MSList * linphone_core_get_call_history_for_address(LinphoneCore *lc, const Linp /*since we want to append query parameters depending on arguments given, we use malloc instead of sqlite3_mprintf*/ sipAddress = linphone_address_as_string_uri_only(addr); - buf = sqlite3_mprintf("SELECT * FROM call_history WHERE caller LIKE '%%%q%%' OR callee LIKE '%%%q%%' ORDER BY id ASC", sipAddress, sipAddress); // The '%%%q%%' takes care of the eventual presence of a display name + buf = sqlite3_mprintf("SELECT * FROM call_history WHERE caller LIKE '%%%q%%' OR callee LIKE '%%%q%%' ORDER BY id DESC", sipAddress, sipAddress); // The '%%%q%%' takes care of the eventual presence of a display name begin = ortp_get_cur_time_ms(); linphone_sql_request_call_log(lc->logs_db, buf, &result); diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 89634e511..a82f98f00 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -4932,7 +4932,7 @@ LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc) * Call log related functions * ******************************************************************************/ -void linphone_core_set_call_logs_database_path(LinphoneCore *lc, const char *path){ +void linphone_core_set_call_logs_database_path(LinphoneCore *lc, const char *path) { if (lc->logs_db_file){ ms_free(lc->logs_db_file); lc->logs_db_file = NULL; @@ -4940,6 +4940,8 @@ void linphone_core_set_call_logs_database_path(LinphoneCore *lc, const char *pat if (path) { lc->logs_db_file = ms_strdup(path); linphone_core_call_log_storage_init(lc); + + linphone_core_migrate_logs_from_rc_to_db(lc); } } @@ -4969,7 +4971,7 @@ void linphone_core_reset_missed_calls_count(LinphoneCore *lc) { lc->missed_calls=0; } -void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCallLog *cl){ +void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCallLog *cl) { #ifdef CALL_LOGS_STORAGE_ENABLED linphone_core_delete_call_log(lc, cl); #else @@ -4979,6 +4981,66 @@ void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCallLog *cl){ #endif } +void linphone_core_migrate_logs_from_rc_to_db(LinphoneCore *lc) { + MSList *logs_to_migrate = NULL; + LpConfig *lpc = NULL; + int original_logs_count, migrated_logs_count; + int i; + +#ifndef CALL_LOGS_STORAGE_ENABLED + ms_warning("linphone has been compiled without sqlite, can't migrate call logs"); + return; +#endif + if (!lc) { + return; + } + + lpc = linphone_core_get_config(lc); + if (!lpc) { + ms_warning("this core has been started without a rc file, nothing to migrate"); + return; + } + if (lp_config_get_int(lpc, "misc", "call_logs_migration_done", 0) == 1) { + ms_warning("the call logs migration has already been done, skipping..."); + return; + } + + // This is because there must have been a call previously to linphone_core_call_log_storage_init + lc->call_logs = ms_list_free_with_data(lc->call_logs, (void (*)(void*))linphone_call_log_unref); + + call_logs_read_from_config_file(lc); + if (!lc->call_logs) { + ms_warning("nothing to migrate, skipping..."); + return; + } + + logs_to_migrate = lc->call_logs; + lc->call_logs = NULL; + // We can't use ms_list_for_each because logs_to_migrate are listed in the wrong order (latest first), and we want to store the logs latest last + for (i = ms_list_size(logs_to_migrate) - 1; i >= 0; i--) { + LinphoneCallLog *log = (LinphoneCallLog *) ms_list_nth_data(logs_to_migrate, i); + linphone_core_store_call_log(lc, log); + } + + original_logs_count = ms_list_size(logs_to_migrate); + migrated_logs_count = ms_list_size(lc->call_logs); + if (original_logs_count == migrated_logs_count) { + int i = 0; + ms_debug("call logs migration successful: %i logs migrated", ms_list_size(lc->call_logs)); + lp_config_set_int(lpc, "misc", "call_logs_migration_done", 1); + + for (; i < original_logs_count; i++) { + char logsection[32]; + snprintf(logsection, sizeof(logsection), "call_log_%i", i); + lp_config_clean_section(lpc, logsection); + } + } else { + ms_error("not as many logs saved in db has logs read from rc (%i in rc against %i in db)!", original_logs_count, migrated_logs_count); + } + + ms_list_free_with_data(logs_to_migrate, (void (*)(void*))linphone_call_log_unref); +} + /******************************************************************************* * Video related functions * diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index f7ef79394..b31909941 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -3215,6 +3215,13 @@ LINPHONE_PUBLIC void linphone_core_remove_call_log(LinphoneCore *lc, LinphoneCal **/ LINPHONE_PUBLIC void linphone_core_set_call_logs_database_path(LinphoneCore *lc, const char *path); +/** + * Migrates the call logs from the linphonerc to the database if not done yet + * @ingroup initializing + * @param lc the linphone core +**/ +LINPHONE_PUBLIC void linphone_core_migrate_logs_from_rc_to_db(LinphoneCore *lc); + /** * @} **/ diff --git a/gtk/main.c b/gtk/main.c index c4c332bb3..8c9ebac24 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -1900,7 +1900,9 @@ static void linphone_gtk_init_main_window(){ linphone_gtk_show_friends(); linphone_core_reset_missed_calls_count(linphone_gtk_get_core()); main_window=linphone_gtk_get_main_window(); +#ifndef CALL_LOGS_STORAGE_ENABLED linphone_gtk_call_log_update(main_window); +#endif linphone_gtk_update_call_buttons (NULL); g_object_set_data(G_OBJECT(main_window),"keypad",NULL); @@ -2220,7 +2222,9 @@ core_start: g_free(chat_messages_db_file); g_free(call_logs_db_file); +#ifdef CALL_LOGS_STORAGE_ENABLED linphone_gtk_call_log_update(the_ui); +#endif /* do not lower timeouts under 30 ms because it exhibits a bug on gtk+/win32, with cpu running 20% all the time...*/ gtk_timeout_add(30,(GtkFunction)linphone_gtk_iterate,(gpointer)linphone_gtk_get_core());