gui/settings: Store settings when changed

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-21 10:49:04 -05:00
parent 92c1b64b1a
commit a5f0da7a60
3 changed files with 30 additions and 3 deletions

View File

@ -1,9 +1,28 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <gui/settings.h>
static GHashTable *gui_settings = NULL;
static struct file gui_settings_file = FILE_INIT("settings", 0);
static void __settings_save_item(gpointer key, gpointer value, gpointer data)
{
file_writef(&gui_settings_file, "%s %u\n", (const char *)key,
GPOINTER_TO_UINT(value));
}
static void __settings_save()
{
file_open(&gui_settings_file, OPEN_WRITE);
file_writef(&gui_settings_file, "%u\n", g_hash_table_size(gui_settings));
g_hash_table_foreach(gui_settings, __settings_save_item, NULL);
file_close(&gui_settings_file);
}
void gui_settings_init()
@ -20,9 +39,11 @@ void gui_settings_deinit()
void gui_settings_set(const gchar *key, unsigned int value)
{
if (gui_settings)
if (gui_settings) {
g_hash_table_replace(gui_settings, g_strdup(key),
GUINT_TO_POINTER(value));
__settings_save();
}
}
unsigned int gui_settings_get(const gchar *key)

View File

@ -1,6 +1,6 @@
#!/usr/bin/python
import os
Import("env", "UnitTest", "testing_group")
Import("env", "UnitTest", "testing_group", "core_objs")
res = []
gui_objs = []
@ -16,7 +16,7 @@ def GuiTest(name):
if os.path.exists(gui):
gui_objs += [ env.Object(gui) ]
run = UnitTest("gui/%s" % name, [ test ] + gui_objs)
run = UnitTest("gui/%s" % name, [ test ] + gui_objs + core_objs)
Alias("tests/gui", run)
if len(res) > 0 and testing_group(["tests/gui"]):
Depends(run, res[-1])

View File

@ -1,20 +1,25 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <gui/settings.h>
#include <tests/test.h>
static void test_settings()
{
struct file f = FILE_INIT("settings", 0);
test_equal((void *)test_get_gui_settings(), NULL);
gui_settings_set("test.value1", 42);
gui_settings_set("test.value2", 84);
test_equal(gui_settings_get("test.value1"), 0);
test_equal(gui_settings_get("test.value2"), 0);
test_equal(file_exists(&f), (bool)false);
gui_settings_init();
test_not_equal((void *)test_get_gui_settings(), NULL);
gui_settings_set("test.value1", 42);
test_equal(file_exists(&f), (bool)true);
gui_settings_set("test.value2", 84);
test_equal(gui_settings_get("test.value1"), 42);
test_equal(gui_settings_get("test.value2"), 84);
@ -25,6 +30,7 @@ static void test_settings()
gui_settings_set("test.value2", 84);
test_equal(gui_settings_get("test.value1"), 0);
test_equal(gui_settings_get("test.value2"), 0);
test_equal(file_exists(&f), (bool)true);
}
DECLARE_UNIT_TESTS(