From 7e00c8ed10bdd2311c1cac33ebadaa4ec13fe9f0 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 29 Mar 2016 08:39:00 -0400 Subject: [PATCH] core/settings: Move settings into core/ Implements issue #9: Move settings into core/ Signed-off-by: Anna Schumaker --- core/core.c | 3 ++ {gui => core}/settings.c | 14 ++++---- gui/ocarina.c | 3 -- gui/sidebar.c | 6 ++-- gui/view.c | 7 ++-- gui/window.c | 18 +++++----- include/{gui => core}/settings.h | 18 +++++----- tests/core/.gitignore | 1 + tests/core/CMakeLists.txt | 1 + tests/core/idle.c | 1 + tests/core/settings.c | 57 ++++++++++++++++++++++++++++++++ tests/gui/.gitignore | 1 - tests/gui/CMakeLists.txt | 1 - tests/gui/idle.c | 4 +-- tests/gui/sidebar.c | 19 ++++++----- tests/gui/view.c | 15 ++++----- tests/gui/window.c | 22 ++++++------ 17 files changed, 123 insertions(+), 68 deletions(-) rename {gui => core}/settings.c (85%) rename include/{gui => core}/settings.h (58%) create mode 100644 tests/core/settings.c diff --git a/core/core.c b/core/core.c index c1f85a4c..8ac9ee8f 100644 --- a/core/core.c +++ b/core/core.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,7 @@ void core_init(int *argc, char ***argv, struct core_init_data *init) #else idle_init(); #endif /* CONFIG_TESTING */ + settings_init(); tags_init(); playlist_init(init->playlist_ops); tempq_init(init->tempq_ops); @@ -28,5 +30,6 @@ void core_deinit() tempq_deinit(); playlist_deinit(); tags_deinit(); + settings_deinit(); idle_deinit(); } diff --git a/gui/settings.c b/core/settings.c similarity index 85% rename from gui/settings.c rename to core/settings.c index 01d1f259..0d5504d2 100644 --- a/gui/settings.c +++ b/core/settings.c @@ -2,7 +2,7 @@ * Copyright 2015 (c) Anna Schumaker. */ #include -#include +#include static GHashTable *gui_settings = NULL; static struct file gui_settings_file = FILE_INIT("settings", 0, 0); @@ -36,7 +36,7 @@ static void __settings_read() } } -void gui_settings_init() +void settings_init() { gui_settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); @@ -46,13 +46,13 @@ void gui_settings_init() file_close(&gui_settings_file); } -void gui_settings_deinit() +void settings_deinit() { g_hash_table_destroy(gui_settings); gui_settings = NULL; } -void gui_settings_set(const gchar *key, unsigned int value) +void settings_set(const gchar *key, unsigned int value) { if (gui_settings) { g_hash_table_replace(gui_settings, g_strdup(key), @@ -61,14 +61,14 @@ void gui_settings_set(const gchar *key, unsigned int value) } } -unsigned int gui_settings_get(const gchar *key) +unsigned int settings_get(const gchar *key) { if (gui_settings) return GPOINTER_TO_UINT(g_hash_table_lookup(gui_settings, key)); return 0; } -bool gui_settings_has(const gchar *key) +bool settings_has(const gchar *key) { if (gui_settings) return g_hash_table_contains(gui_settings, key); @@ -77,7 +77,7 @@ bool gui_settings_has(const gchar *key) #ifdef CONFIG_TESTING -GHashTable *test_get_gui_settings() +GHashTable *test_get_settings() { return gui_settings; } diff --git a/gui/ocarina.c b/gui/ocarina.c index 7bed720d..0acd4319 100644 --- a/gui/ocarina.c +++ b/gui/ocarina.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -53,7 +52,6 @@ static void __ocarina_startup(GApplication *application, gpointer data) gchar *icon = find_file_path("ocarina.png"); gui_builder_init(ui); - gui_settings_init(); core_init(&startup_argc, &startup_argv, &init_data); gui_queue_model_init(); gui_view_init(); @@ -77,7 +75,6 @@ static void __ocarina_shutdown(GApplication *application, gpointer data) gui_window_deinit(); gui_queue_model_deinit(); - gui_settings_deinit(); gui_builder_deinit(); } diff --git a/gui/sidebar.c b/gui/sidebar.c index 2a5a463f..2570d944 100644 --- a/gui/sidebar.c +++ b/gui/sidebar.c @@ -2,10 +2,10 @@ * Copyright 2015 (c) Anna Schumaker. */ #include +#include #include #include #include -#include #include const gchar *SIDEBAR_SETTING = "gui.sidebar.pos"; @@ -54,7 +54,7 @@ static bool __sidebar_find_queue(struct gui_queue *queue, GtkTreeIter *iter) void __sidebar_resize(GtkPaned *pane, GParamSpec *pspec, gpointer data) { - gui_settings_set(SIDEBAR_SETTING, gtk_paned_get_position(pane)); + settings_set(SIDEBAR_SETTING, gtk_paned_get_position(pane)); } bool __sidebar_keypress(GtkTreeView *treeview, GdkEventKey *event, gpointer data) @@ -114,7 +114,7 @@ void gui_sidebar_init() sb_store = GTK_LIST_STORE(gui_builder_object("o_sidebar_store")); /* Set sidebar width. */ - pos = gui_settings_get(SIDEBAR_SETTING); + pos = settings_get(SIDEBAR_SETTING); if (pos > 0) gtk_paned_set_position(pane, pos); } diff --git a/gui/view.c b/gui/view.c index 265e5452..9c2c9b47 100644 --- a/gui/view.c +++ b/gui/view.c @@ -3,11 +3,11 @@ */ #include #include +#include #include #include #include #include -#include #include #include @@ -144,8 +144,7 @@ void __view_column_resized(GtkTreeViewColumn *col, GParamSpec *pspec, { unsigned int index = __view_get_column_index(col); - gui_settings_set(QUEUE_SETTINGS[index], - gtk_tree_view_column_get_width(col)); + settings_set(QUEUE_SETTINGS[index], gtk_tree_view_column_get_width(col)); } void __view_column_clicked(GtkTreeViewColumn *col, gpointer data) @@ -322,7 +321,7 @@ void gui_view_init() for (i = 0; i < Q_MODEL_N_COLUMNS; i++) { col = gtk_tree_view_get_column(view_treeview, i); - pos = gui_settings_get(QUEUE_SETTINGS[i]); + pos = settings_get(QUEUE_SETTINGS[i]); if (col && pos > 0) gtk_tree_view_column_set_fixed_width(col, pos); } diff --git a/gui/window.c b/gui/window.c index 44442fc7..00a76367 100644 --- a/gui/window.c +++ b/gui/window.c @@ -1,9 +1,9 @@ /* * Copyright 2014 (c) Anna Schumaker. */ +#include #include #include -#include static const gchar *SETTINGS_WIDTH = "gui.window.width"; static const gchar *SETTINGS_HEIGHT = "gui.window.height"; @@ -15,17 +15,17 @@ static int saved_height = 0; gboolean __window_configure(GtkWindow *window, GdkEventConfigure *event, gpointer data) { - int width = gui_settings_get(SETTINGS_WIDTH); - int height = gui_settings_get(SETTINGS_HEIGHT); + int width = settings_get(SETTINGS_WIDTH); + int height = settings_get(SETTINGS_HEIGHT); if (event->width != width) { saved_width = width; - gui_settings_set(SETTINGS_WIDTH, event->width); + settings_set(SETTINGS_WIDTH, event->width); } if (event->height != height) { saved_height = height; - gui_settings_set(SETTINGS_HEIGHT, event->height); + settings_set(SETTINGS_HEIGHT, event->height); } return FALSE; @@ -36,8 +36,8 @@ gboolean __window_state(GtkWindow *window, GdkEventWindowState *event, { if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED && event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) { - gui_settings_set(SETTINGS_WIDTH, saved_width); - gui_settings_set(SETTINGS_HEIGHT, saved_height); + settings_set(SETTINGS_WIDTH, saved_width); + settings_set(SETTINGS_HEIGHT, saved_height); } return FALSE; @@ -52,8 +52,8 @@ void gui_window_init(const gchar *icon) gtk_window_set_title(window, title); gtk_window_set_icon_from_file(window, icon, NULL); - saved_width = gui_settings_get(SETTINGS_WIDTH); - saved_height = gui_settings_get(SETTINGS_HEIGHT); + saved_width = settings_get(SETTINGS_WIDTH); + saved_height = settings_get(SETTINGS_HEIGHT); if (saved_width > 0 && saved_height > 0) gtk_window_resize(window, saved_width, saved_height); diff --git a/include/gui/settings.h b/include/core/settings.h similarity index 58% rename from include/gui/settings.h rename to include/core/settings.h index bb36c826..0ba77a70 100644 --- a/include/gui/settings.h +++ b/include/core/settings.h @@ -3,29 +3,29 @@ * * The settings layer is used to store values configured by the user. */ -#ifndef OCARINA_GUI_SETTINGS_H -#define OCARINA_GUI_SETTINGS_H +#ifndef OCARINA_SETTINGS_H +#define OCARINA_SETTINGS_H #include #include /* Called to initialize GUI settings. */ -void gui_settings_init(); +void settings_init(); /* Called to deinitialize GUI settings. */ -void gui_settings_deinit(); +void settings_deinit(); /* Called to configure a specific setting by key. */ -void gui_settings_set(const gchar *, unsigned int); +void settings_set(const gchar *, unsigned int); /* Called to access a specific setting by key. */ -unsigned int gui_settings_get(const gchar *); +unsigned int settings_get(const gchar *); /* Called to check if a specific settings exists. */ -bool gui_settings_has(const gchar *); +bool settings_has(const gchar *); #ifdef CONFIG_TESTING -GHashTable *test_get_gui_settings(); +GHashTable *test_get_settings(); #endif /* CONFIG_TESTING */ -#endif /* OCARINA_GUI_SETTINGS_H */ +#endif /* OCARINA_SETTINGS_H */ diff --git a/tests/core/.gitignore b/tests/core/.gitignore index babbbc68..288d4f7b 100644 --- a/tests/core/.gitignore +++ b/tests/core/.gitignore @@ -3,6 +3,7 @@ string file date idle +settings database queue tempq diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index e2c34e52..7783178e 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -9,6 +9,7 @@ core_unit_test(String) core_unit_test(File) core_unit_test(Date) core_unit_test(Idle) +core_unit_test(Settings) core_unit_test(Database) add_subdirectory(tags/) diff --git a/tests/core/idle.c b/tests/core/idle.c index 85ddd32d..916336e7 100644 --- a/tests/core/idle.c +++ b/tests/core/idle.c @@ -4,6 +4,7 @@ */ #include #include +#include static unsigned int cur = -1; diff --git a/tests/core/settings.c b/tests/core/settings.c new file mode 100644 index 00000000..9f4d5d49 --- /dev/null +++ b/tests/core/settings.c @@ -0,0 +1,57 @@ +/* + * Copyright 2015 (c) Anna Schumaker. + */ +#include +#include +#include + +static void test_settings() +{ + struct file f = FILE_INIT("settings", 0, 0); + + g_assert_null(test_get_settings()); + settings_set("test.value1", 42); + settings_set("test.value2", 84); + g_assert_false(settings_has("test.value1")); + g_assert_cmpuint(settings_get("test.value1"), ==, 0); + g_assert_cmpuint(settings_get("test.value2"), ==, 0); + g_assert_false(file_exists(&f)); + + settings_init(); + g_assert_nonnull(test_get_settings()); + g_assert_false(settings_has("test.value1")); + settings_set("test.value1", 42); + g_assert_true(settings_has("test.value1")); + g_assert_false(settings_has("test.value2")); + g_assert_true(file_exists(&f)); + settings_set("test.value2", 84); + g_assert_true(settings_has("test.value2")); + g_assert_cmpuint(settings_get("test.value1"), ==, 42); + g_assert_cmpuint(settings_get("test.value2"), ==, 84); + + settings_deinit(); + g_assert_null(test_get_settings()); + settings_set("test.value1", 42); + settings_set("test.value2", 84); + g_assert_cmpuint(settings_get("test.value1"), ==, 0); + g_assert_cmpuint(settings_get("test.value2"), ==, 0); + g_assert_false(settings_has("test.value1")); + g_assert_false(settings_has("test.value2")); + g_assert_true(file_exists(&f)); + + settings_init(); + g_assert_nonnull(test_get_settings()); + g_assert_cmpuint(settings_get("test.value1"), ==, 42); + g_assert_cmpuint(settings_get("test.value2"), ==, 84); + g_assert_true(settings_has("test.value1")); + g_assert_true(settings_has("test.value2")); + + settings_deinit(); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Core/Settings", test_settings); + return g_test_run(); +} diff --git a/tests/gui/.gitignore b/tests/gui/.gitignore index d05fa6ab..d7145bdc 100644 --- a/tests/gui/.gitignore +++ b/tests/gui/.gitignore @@ -1,5 +1,4 @@ builder -settings model view queue diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index 9d71779e..ec6f1113 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -5,7 +5,6 @@ function(gui_unit_test name) endfunction() gui_unit_test(Builder) -gui_unit_test(Settings) gui_unit_test(Model) gui_unit_test(View) gui_unit_test(Queue) diff --git a/tests/gui/idle.c b/tests/gui/idle.c index be14b783..fc7db96a 100644 --- a/tests/gui/idle.c +++ b/tests/gui/idle.c @@ -3,9 +3,9 @@ */ #include #include +#include #include #include -#include #include #include @@ -66,7 +66,6 @@ int main(int argc, char **argv) gtk_init(&argc, NULL); core_init(&argc, NULL, &init_data); gui_builder_init("share/ocarina/ocarina6.glade"); - gui_settings_init(); gui_view_init(); while (idle_run_task()) {} @@ -74,7 +73,6 @@ int main(int argc, char **argv) g_test_add_func("/Gui/Idle", test_idle); ret = g_test_run(); - gui_settings_deinit(); gui_builder_deinit(); core_deinit(); return ret; diff --git a/tests/gui/sidebar.c b/tests/gui/sidebar.c index 16861fe4..082a2bab 100644 --- a/tests/gui/sidebar.c +++ b/tests/gui/sidebar.c @@ -1,8 +1,8 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#include #include -#include #include #include @@ -10,14 +10,15 @@ static void test_sidebar() { GtkPaned *paned = GTK_PANED(gui_builder_widget("o_sidebar")); - g_assert_false(gui_settings_has("gui.sidebar.pos")); - gtk_paned_set_position(paned, 300); - g_assert_true(gui_settings_has("gui.sidebar.pos")); - g_assert_cmpuint(gui_settings_get("gui.sidebar.pos"), ==, 300); + g_assert_false(settings_has("gui.sidebar.pos")); - gui_settings_set("gui.sidebar.pos", 250); + gtk_paned_set_position(paned, 300); + g_assert_true(settings_has("gui.sidebar.pos")); + g_assert_cmpuint(settings_get("gui.sidebar.pos"), ==, 300); + + settings_set("gui.sidebar.pos", 250); gui_sidebar_init(); - g_assert_cmpuint(gui_settings_get("gui.sidebar.pos"), ==, 250); + g_assert_cmpuint(settings_get("gui.sidebar.pos"), ==, 250); g_assert_cmpuint(gtk_paned_get_position(paned), ==, 250); } @@ -26,15 +27,15 @@ int main(int argc, char **argv) int ret; gtk_init(&argc, NULL); + settings_init(); gui_builder_init("share/ocarina/ocarina6.glade"); - gui_settings_init(); gui_sidebar_init(); g_test_init(&argc, &argv, NULL); g_test_add_func("/Gui/Sidebar", test_sidebar); ret = g_test_run(); - gui_settings_deinit(); gui_builder_deinit(); + settings_deinit(); return ret; } diff --git a/tests/gui/view.c b/tests/gui/view.c index 3f59eaf6..ce356e17 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -5,9 +5,9 @@ #include #include #include +#include #include #include -#include #include #include @@ -61,7 +61,6 @@ static void test_treeview() gtk_init(&argc, NULL); core_init(&argc, NULL, &init_data); gui_builder_init("share/ocarina/ocarina6.glade"); - gui_settings_init(); gui_queue_model_init(); gui_view_init(); while (idle_run_task() == true) {} @@ -76,7 +75,7 @@ static void test_treeview() g_idle_add(test_on_idle, NULL); for (i = 0; i < Q_MODEL_N_COLUMNS; i++) - g_assert_false(gui_settings_has(QUEUE_SETTINGS[i])); + g_assert_false(settings_has(QUEUE_SETTINGS[i])); for (i = 0; i < Q_MODEL_N_COLUMNS; i++) { if (i == Q_MODEL_FILE_PATH || i == Q_MODEL_FONT) @@ -88,17 +87,17 @@ static void test_treeview() g_main_loop_run(main_loop); for (i = 0; i < Q_MODEL_N_COLUMNS; i++) { bool has = (i != Q_MODEL_FILE_PATH) && (i != Q_MODEL_FONT); - g_assert(gui_settings_has(QUEUE_SETTINGS[i]) == has); + g_assert(settings_has(QUEUE_SETTINGS[i]) == has); /* The "Played" column gets any remaining space. */ if (has && i != Q_MODEL_LAST_PLAY) - g_assert_cmpuint(gui_settings_get(QUEUE_SETTINGS[i]), + g_assert_cmpuint(settings_get(QUEUE_SETTINGS[i]), ==, (i + 2) * 10); } - g_assert_false(gui_settings_has("gui.queue.filepath")); - g_assert_cmpuint(gui_settings_get("gui.queue.filepath"), ==, 0); + g_assert_false(settings_has("gui.queue.filepath")); + g_assert_cmpuint(settings_get("gui.queue.filepath"), ==, 0); for (i = 0; i < Q_MODEL_LAST_PLAY; i++) - gui_settings_set(QUEUE_SETTINGS[i], 42); + settings_set(QUEUE_SETTINGS[i], 42); gui_view_init(); for (i = 0; i < Q_MODEL_LAST_PLAY; i++) { diff --git a/tests/gui/window.c b/tests/gui/window.c index cb91f370..628cc487 100644 --- a/tests/gui/window.c +++ b/tests/gui/window.c @@ -1,8 +1,8 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#include #include -#include #include #include #include @@ -23,21 +23,21 @@ static void test_window() window = GTK_WINDOW(gui_builder_widget("o_window")); g_idle_add(test_on_idle, window); - g_assert_false(gui_settings_has("gui.window.width")); - g_assert_false(gui_settings_has("gui.window.height")); + g_assert_false(settings_has("gui.window.width")); + g_assert_false(settings_has("gui.window.height")); g_assert_cmpstr(gtk_window_get_title(window), ==, "Ocarina " CONFIG_VERSION); g_main_loop_run(main_loop); - g_assert_true(gui_settings_has("gui.window.width")); - g_assert_true(gui_settings_has("gui.window.height")); + g_assert_true(settings_has("gui.window.width")); + g_assert_true(settings_has("gui.window.height")); - gui_settings_set("gui.window.width", 800); - gui_settings_set("gui.window.height", 600); + settings_set("gui.window.width", 800); + settings_set("gui.window.height", 600); gui_window_init("share/ocarina/ocarina.png"); g_main_loop_run(main_loop); - g_assert_cmpuint(gui_settings_get("gui.window.width"), ==, 800); - g_assert_cmpuint(gui_settings_get("gui.window.height"), ==, 600); + g_assert_cmpuint(settings_get("gui.window.width"), ==, 800); + g_assert_cmpuint(settings_get("gui.window.height"), ==, 600); g_main_loop_unref(main_loop); } @@ -47,8 +47,8 @@ int main(int argc, char **argv) int ret; gtk_init(&argc, NULL); + settings_init(); gui_builder_init("share/ocarina/ocarina6.glade"); - gui_settings_init(); gui_view_init(); gui_window_init("share/ocarina/ocarina.png"); @@ -57,7 +57,7 @@ int main(int argc, char **argv) ret = g_test_run(); gui_window_deinit(); - gui_settings_deinit(); gui_builder_deinit(); + settings_deinit(); return ret; }