diff --git a/src/config/files.c b/src/config/files.c index 133a270e..e77bfa30 100644 --- a/src/config/files.c +++ b/src/config/files.c @@ -3,6 +3,7 @@ * vim: expandtab:ts=4:sts=4:sw=4 * * Copyright (C) 2012 - 2019 James Booth + * Copyright (C) 2020 Michael Vetter * * This file is part of Profanity. * @@ -138,6 +139,22 @@ files_get_log_file(char *log_file) return result; } +char* +files_get_chatlog_database_path(void) +{ + gchar *xdg_data = _files_get_xdg_data_home(); + GString *logfile = g_string_new(xdg_data); + + g_string_append(logfile, "/profanity/chatlog.db"); + + char *result = strdup(logfile->str); + + free(xdg_data); + g_string_free(logfile, TRUE); + + return result; +} + char* files_get_config_path(char *config_base) { diff --git a/src/config/files.h b/src/config/files.h index 12c3f03a..124c3ac8 100644 --- a/src/config/files.h +++ b/src/config/files.h @@ -60,6 +60,7 @@ void files_create_directories(void); char* files_get_config_path(char *config_base); char* files_get_data_path(char *data_base); +char* files_get_chatlog_database_path(void); char* files_get_log_file(char *log_file); char* files_get_inputrc_file(void); diff --git a/src/database.c b/src/database.c index c03c2466..2d857d08 100644 --- a/src/database.c +++ b/src/database.c @@ -36,46 +36,51 @@ #include #include "log.h" +#include "config/files.h" -static sqlite3 *g_log_database; +static sqlite3 *g_chatlog_database; bool log_database_init(void) { int ret = sqlite3_initialize(); - char *filename = "test"; + char *filename = files_get_chatlog_database_path(); if (ret != SQLITE_OK) { + free(filename); log_error("Error initializing SQLite database: %d", ret); return FALSE; } - ret = sqlite3_open(filename, &g_log_database); + ret = sqlite3_open(filename, &g_chatlog_database); if (ret != SQLITE_OK) { - const char *err_msg = sqlite3_errmsg(g_log_database); + const char *err_msg = sqlite3_errmsg(g_chatlog_database); log_error("Error opening SQLite database: %s", err_msg); + free(filename); return FALSE; } char *err_msg; char *query = "CREATE TABLE IF NOT EXISTS `ChatLogs` ( `id` INTEGER PRIMARY KEY, `jid` TEXT NOT NULL, `message` TEXT, `timestamp` TEXT)"; - if( SQLITE_OK != sqlite3_exec(g_db, query, NULL, 0, &err_msg)) { + if( SQLITE_OK != sqlite3_exec(g_chatlog_database, query, NULL, 0, &err_msg)) { if (err_msg) { log_error("SQLite error: %s", err_msg); sqlite3_free(err_msg); } else { log_error("Unknown SQLite error"); } + free(filename); return FALSE; } log_debug("Initialized SQLite database: %s", filename); + free(filename); return TRUE; } void log_database_close(void) { - sqlite3_close(g_log_database); + sqlite3_close(g_chatlog_database); sqlite3_shutdown(); }