From a5f0da7a601c15346e7da9fea2a4fbb43bdf396d Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 21 Dec 2015 10:49:04 -0500 Subject: [PATCH] gui/settings: Store settings when changed Signed-off-by: Anna Schumaker --- gui/settings.c | 23 ++++++++++++++++++++++- tests/gui/Sconscript | 4 ++-- tests/gui/settings.c | 6 ++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/gui/settings.c b/gui/settings.c index 64e66bad..66be5e02 100644 --- a/gui/settings.c +++ b/gui/settings.c @@ -1,9 +1,28 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#include #include 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) diff --git a/tests/gui/Sconscript b/tests/gui/Sconscript index 7e249e1a..33714512 100644 --- a/tests/gui/Sconscript +++ b/tests/gui/Sconscript @@ -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]) diff --git a/tests/gui/settings.c b/tests/gui/settings.c index 19d0a826..db014d23 100644 --- a/tests/gui/settings.c +++ b/tests/gui/settings.c @@ -1,20 +1,25 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#include #include #include 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(