Introduce a shared implementation for keyfile loading

Instead of copy&pasting the same code over again, use a common
implementation.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel
2023-08-29 13:46:07 +02:00
parent b7f964fe64
commit ca2df180d8
13 changed files with 200 additions and 314 deletions

View File

@@ -53,7 +53,7 @@
#include "xmpp/xmpp.h"
#include "xmpp/jid.h"
static gchar* accounts_loc;
static prof_keyfile_t accounts_prof_keyfile;
static GKeyFile* accounts;
static Autocomplete all_ac;
@@ -67,14 +67,8 @@ accounts_load(void)
log_info("Loading accounts");
all_ac = autocomplete_new();
enabled_ac = autocomplete_new();
accounts_loc = files_get_data_path(FILE_ACCOUNTS);
if (g_file_test(accounts_loc, G_FILE_TEST_EXISTS)) {
g_chmod(accounts_loc, S_IRUSR | S_IWUSR);
}
accounts = g_key_file_new();
g_key_file_load_from_file(accounts, accounts_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&accounts_prof_keyfile, FILE_ACCOUNTS);
accounts = accounts_prof_keyfile.keyfile;
// create the logins searchable list for autocompletion
gsize naccounts;
@@ -93,7 +87,8 @@ accounts_close(void)
{
autocomplete_free(all_ac);
autocomplete_free(enabled_ac);
g_key_file_free(accounts);
free_keyfile(&accounts_prof_keyfile);
accounts = NULL;
}
char*
@@ -959,11 +954,5 @@ accounts_get_login_status(const char* const account_name)
static void
_save_accounts(void)
{
gsize g_data_size;
auto_gchar gchar* g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(accounts_loc);
auto_gchar gchar* true_loc = get_file_or_linked(accounts_loc, base);
g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL);
g_chmod(accounts_loc, S_IRUSR | S_IWUSR);
save_keyfile(&accounts_prof_keyfile);
}

View File

@@ -161,8 +161,7 @@ gchar*
files_get_data_path(const char* const location)
{
auto_gchar gchar* xdg_data = _files_get_xdg_data_home();
gchar* result = g_strdup_printf("%s/profanity/%s", xdg_data, location);
return result;
return g_strdup_printf("%s/profanity/%s", xdg_data, location);
}
gchar*

View File

@@ -69,7 +69,7 @@
#define INPBLOCK_DEFAULT 1000
static gchar* prefs_loc;
static prof_keyfile_t prefs_prof_keyfile;
static GKeyFile* prefs;
gint log_maxsize = 0;
@@ -234,37 +234,20 @@ _prefs_close(void)
void
prefs_reload(void)
{
/*
* Current function contains copy-paste, but we wanted to avoid config_file
* manipulation from prefs_load/prefs_close
*/
_prefs_close();
g_key_file_free(prefs);
prefs = NULL;
prefs = g_key_file_new();
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
_prefs_load();
auto_gchar gchar* loc = g_strdup(prefs_prof_keyfile.filename);
prefs_close();
prefs_load(loc);
}
void
prefs_load(const char* config_file)
{
if (config_file == NULL) {
prefs_loc = files_get_config_path(FILE_PROFRC);
load_config_keyfile(&prefs_prof_keyfile, FILE_PROFRC);
} else {
prefs_loc = g_strdup(config_file);
load_custom_keyfile(&prefs_prof_keyfile, g_strdup(config_file));
}
if (g_file_test(prefs_loc, G_FILE_TEST_EXISTS)) {
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
}
prefs = g_key_file_new();
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
prefs = prefs_prof_keyfile.keyfile;
_prefs_load();
}
@@ -280,11 +263,8 @@ prefs_close(void)
{
_prefs_close();
g_key_file_free(prefs);
free_keyfile(&prefs_prof_keyfile);
prefs = NULL;
g_free(prefs_loc);
prefs_loc = NULL;
}
gchar*
@@ -1689,13 +1669,7 @@ prefs_free_aliases(GList* aliases)
static void
_save_prefs(void)
{
gsize g_data_size;
auto_gchar gchar* g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(prefs_loc);
auto_gchar gchar* true_loc = get_file_or_linked(prefs_loc, base);
g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL);
g_chmod(prefs_loc, S_IRUSR | S_IWUSR);
save_keyfile(&prefs_prof_keyfile);
}
// get the preference group for a specific preference

View File

@@ -47,7 +47,7 @@
#include "config/tlscerts.h"
#include "tools/autocomplete.h"
static char* tlscerts_loc;
static prof_keyfile_t tlscerts_prof_keyfile;
static GKeyFile* tlscerts;
static void _save_tlscerts(void);
@@ -60,14 +60,8 @@ void
tlscerts_init(void)
{
log_info("Loading TLS certificates");
tlscerts_loc = files_get_data_path(FILE_TLSCERTS);
if (g_file_test(tlscerts_loc, G_FILE_TEST_EXISTS)) {
g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR);
}
tlscerts = g_key_file_new();
g_key_file_load_from_file(tlscerts, tlscerts_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&tlscerts_prof_keyfile, FILE_TLSCERTS);
tlscerts = tlscerts_prof_keyfile.keyfile;
certs_ac = autocomplete_new();
gsize len = 0;
@@ -365,7 +359,7 @@ tlscerts_free(TLSCertificate* cert)
void
tlscerts_close(void)
{
g_key_file_free(tlscerts);
free_keyfile(&tlscerts_prof_keyfile);
tlscerts = NULL;
free(current_fp);
@@ -377,8 +371,5 @@ tlscerts_close(void)
static void
_save_tlscerts(void)
{
gsize g_data_size;
auto_gchar gchar* g_tlscerts_data = g_key_file_to_data(tlscerts, &g_data_size, NULL);
g_file_set_contents(tlscerts_loc, g_tlscerts_data, g_data_size, NULL);
g_chmod(tlscerts_loc, S_IRUSR | S_IWUSR);
save_keyfile(&tlscerts_prof_keyfile);
}