1
0
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

db: Have one database per account

This commit is contained in:
Michael Vetter 2020-03-23 12:18:25 +01:00
parent 672f3e22e8
commit 11663625cc
7 changed files with 47 additions and 25 deletions

View File

@ -139,22 +139,6 @@ 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)
{

View File

@ -55,12 +55,12 @@
#define DIR_PGP "pgp"
#define DIR_OMEMO "omemo"
#define DIR_PLUGINS "plugins"
#define DIR_DATABASE "database"
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);

View File

@ -35,28 +35,65 @@
#define _GNU_SOURCE 1
#include <sys/stat.h>
#include <sqlite3.h>
#include <stdio.h>
#include <glib.h>
#include <errno.h>
#include "log.h"
#include "config/files.h"
#include "config/account.h"
#include "xmpp/xmpp.h"
static sqlite3 *g_chatlog_database;
static char*
_get_db_filename(ProfAccount *account)
{
char *databasedir = files_get_data_path(DIR_DATABASE);
GString *basedir = g_string_new(databasedir);
free(databasedir);
g_string_append(basedir, "/");
gchar *account_dir = str_replace(account->jid, "@", "_at_");
g_string_append(basedir, account_dir);
free(account_dir);
int res = g_mkdir_with_parents(basedir->str, S_IRWXU);
if (res == -1) {
char *errmsg = strerror(errno);
if (errmsg) {
log_error("DATABASE: error creating directory: %s, %s", basedir->str, errmsg);
} else {
log_error("DATABASE: creating directory: %s", basedir->str);
}
g_string_free(basedir, TRUE);
return NULL;
}
g_string_append(basedir, "/chatlog.db");
char *result = strdup(basedir->str);
g_string_free(basedir, TRUE);
return result;
}
bool
log_database_init(void)
log_database_init(ProfAccount *account)
{
int ret = sqlite3_initialize();
char *filename = files_get_chatlog_database_path();
if (ret != SQLITE_OK) {
free(filename);
log_error("Error initializing SQLite database: %d", ret);
return FALSE;
}
char *filename = _get_db_filename(account);
if (!filename) {
return FALSE;
}
ret = sqlite3_open(filename, &g_chatlog_database);
if (ret != SQLITE_OK) {
const char *err_msg = sqlite3_errmsg(g_chatlog_database);

View File

@ -36,7 +36,7 @@
#ifndef DATABASE_H
#define DATABASE_H
bool log_database_init(void);
bool log_database_init(ProfAccount *account);
void log_database_close(void);
void log_database_add(ProfMessage *message, gboolean is_muc);

View File

@ -33,6 +33,7 @@
*
*/
#include "database.h"
#include "config/tlscerts.h"
#include "ui/ui.h"
#include "xmpp/chat_session.h"
@ -67,6 +68,7 @@ ev_disconnect_cleanup(void)
#ifdef HAVE_OMEMO
omemo_on_disconnect();
#endif
log_database_close();
}
gboolean

View File

@ -92,6 +92,8 @@ sv_ev_login_account_success(char *account_name, gboolean secured)
omemo_on_connect(account);
#endif
log_database_init(account);
avatar_pep_subscribe();
ui_handle_login_account_success(account, secured);

View File

@ -53,7 +53,6 @@
#include "profanity.h"
#include "common.h"
#include "log.h"
#include "database.h"
#include "config/files.h"
#include "config/tlscerts.h"
#include "config/accounts.h"
@ -188,7 +187,6 @@ _init(char *log_level, char *config_file, char *log_file, char *theme_name)
log_info("Starting Profanity (%s)...", PACKAGE_VERSION);
}
log_database_init();
chat_log_init();
groupchat_log_init();
accounts_load();
@ -257,7 +255,6 @@ _shutdown(void)
#ifdef HAVE_OMEMO
omemo_close();
#endif
log_database_close();
chat_log_close();
theme_close();
accounts_close();