2015-12-21 10:09:05 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-12-21 10:49:04 -05:00
|
|
|
#include <core/file.h>
|
2016-03-29 08:39:00 -04:00
|
|
|
#include <core/settings.h>
|
2015-12-21 10:09:05 -05:00
|
|
|
|
|
|
|
static GHashTable *gui_settings = NULL;
|
2018-02-16 15:50:46 -05:00
|
|
|
static struct file gui_settings_file = FILE_INIT_DATA("", "settings", 0);
|
2015-12-21 10:49:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
static void __settings_save_item(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
2018-02-21 08:19:19 -05:00
|
|
|
file_writef(&gui_settings_file, "%s %u\n", (const gchar *)key,
|
2015-12-21 10:49:04 -05:00
|
|
|
GPOINTER_TO_UINT(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __settings_save()
|
|
|
|
{
|
2018-02-20 15:38:28 -05:00
|
|
|
file_open(&gui_settings_file, OPEN_WRITE);
|
2015-12-21 10:49:04 -05:00
|
|
|
|
2018-02-21 08:19:19 -05:00
|
|
|
file_writef(&gui_settings_file, "%u\n", g_hash_table_size(gui_settings));
|
2015-12-21 10:49:04 -05:00
|
|
|
g_hash_table_foreach(gui_settings, __settings_save_item, NULL);
|
|
|
|
|
2018-02-20 09:53:53 -05:00
|
|
|
file_close(&gui_settings_file);
|
2015-12-21 10:49:04 -05:00
|
|
|
}
|
2015-12-21 10:09:05 -05:00
|
|
|
|
2015-12-21 10:57:23 -05:00
|
|
|
static void __settings_read()
|
|
|
|
{
|
|
|
|
unsigned int num, i, value;
|
|
|
|
gchar *key;
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_readf(&gui_settings_file, "%u\n", &num);
|
2015-12-21 10:57:23 -05:00
|
|
|
for (i = 0; i < num; i++) {
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_readf(&gui_settings_file, "%m[^ ] %u\n", &key, &value);
|
|
|
|
|
2015-12-21 10:57:23 -05:00
|
|
|
g_hash_table_insert(gui_settings, key, GUINT_TO_POINTER(value));
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 10:09:05 -05:00
|
|
|
|
2016-03-29 08:39:00 -04:00
|
|
|
void settings_init()
|
2015-12-21 10:09:05 -05:00
|
|
|
{
|
|
|
|
gui_settings = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
g_free, NULL);
|
2015-12-21 10:57:23 -05:00
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
if (file_open(&gui_settings_file, OPEN_READ))
|
2015-12-21 10:57:23 -05:00
|
|
|
__settings_read();
|
2018-02-20 09:53:53 -05:00
|
|
|
file_close(&gui_settings_file);
|
2015-12-21 10:09:05 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 08:39:00 -04:00
|
|
|
void settings_deinit()
|
2015-12-21 10:09:05 -05:00
|
|
|
{
|
|
|
|
g_hash_table_destroy(gui_settings);
|
|
|
|
gui_settings = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-29 08:39:00 -04:00
|
|
|
void settings_set(const gchar *key, unsigned int value)
|
2015-12-21 10:35:32 -05:00
|
|
|
{
|
2016-09-26 10:52:44 -04:00
|
|
|
if (gui_settings && key) {
|
2015-12-21 10:35:32 -05:00
|
|
|
g_hash_table_replace(gui_settings, g_strdup(key),
|
|
|
|
GUINT_TO_POINTER(value));
|
2015-12-21 10:49:04 -05:00
|
|
|
__settings_save();
|
|
|
|
}
|
2015-12-21 10:35:32 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 08:39:00 -04:00
|
|
|
unsigned int settings_get(const gchar *key)
|
2015-12-21 10:35:32 -05:00
|
|
|
{
|
2016-09-26 10:52:44 -04:00
|
|
|
if (gui_settings && key)
|
2015-12-26 18:36:26 -05:00
|
|
|
return GPOINTER_TO_UINT(g_hash_table_lookup(gui_settings, key));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-29 08:39:00 -04:00
|
|
|
bool settings_has(const gchar *key)
|
2015-12-26 18:36:26 -05:00
|
|
|
{
|
2016-09-26 10:52:44 -04:00
|
|
|
if (gui_settings && key)
|
2015-12-26 18:36:26 -05:00
|
|
|
return g_hash_table_contains(gui_settings, key);
|
|
|
|
return false;
|
2015-12-21 10:35:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-21 10:09:05 -05:00
|
|
|
#ifdef CONFIG_TESTING
|
2016-03-29 08:39:00 -04:00
|
|
|
GHashTable *test_get_settings()
|
2015-12-21 10:09:05 -05:00
|
|
|
{
|
|
|
|
return gui_settings;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_TESTING */
|