core/settings: Move settings into core/

Implements issue #9: Move settings into core/
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-29 08:39:00 -04:00 committed by Anna Schumaker
parent 524d1886f9
commit 7e00c8ed10
17 changed files with 123 additions and 68 deletions

View File

@ -5,6 +5,7 @@
#include <core/core.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/settings.h>
#include <core/tags/tags.h>
#include <core/tempq.h>
@ -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();
}

View File

@ -2,7 +2,7 @@
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <gui/settings.h>
#include <core/settings.h>
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;
}

View File

@ -7,7 +7,6 @@
#include <gui/collection.h>
#include <gui/idle.h>
#include <gui/playlist.h>
#include <gui/settings.h>
#include <gui/sidebar.h>
#include <gui/tempq.h>
#include <gui/view.h>
@ -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();
}

View File

@ -2,10 +2,10 @@
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/queue.h>
#include <core/settings.h>
#include <core/tempq.h>
#include <gui/builder.h>
#include <gui/queue.h>
#include <gui/settings.h>
#include <gui/sidebar.h>
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);
}

View File

@ -3,11 +3,11 @@
*/
#include <core/audio.h>
#include <core/playlist.h>
#include <core/settings.h>
#include <core/tempq.h>
#include <gui/builder.h>
#include <gui/model.h>
#include <gui/queue.h>
#include <gui/settings.h>
#include <gui/view.h>
#include <stdlib.h>
@ -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);
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/settings.h>
#include <core/version.h>
#include <gui/builder.h>
#include <gui/settings.h>
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);

View File

@ -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 <glib.h>
#include <stdbool.h>
/* 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 */

View File

@ -3,6 +3,7 @@ string
file
date
idle
settings
database
queue
tempq

View File

@ -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/)

View File

@ -4,6 +4,7 @@
*/
#include <core/idle.h>
#include <tests/test.h>
#include <glib.h>
static unsigned int cur = -1;

57
tests/core/settings.c Normal file
View File

@ -0,0 +1,57 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <core/settings.h>
#include <tests/test.h>
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();
}

View File

@ -1,5 +1,4 @@
builder
settings
model
view
queue

View File

@ -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)

View File

@ -3,9 +3,9 @@
*/
#include <core/core.h>
#include <core/idle.h>
#include <core/settings.h>
#include <gui/builder.h>
#include <gui/idle.h>
#include <gui/settings.h>
#include <gui/view.h>
#include <tests/test.h>
@ -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;

View File

@ -1,8 +1,8 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/settings.h>
#include <gui/builder.h>
#include <gui/settings.h>
#include <gui/sidebar.h>
#include <tests/test.h>
@ -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;
}

View File

@ -5,9 +5,9 @@
#include <core/core.h>
#include <core/idle.h>
#include <core/playlist.h>
#include <core/settings.h>
#include <gui/builder.h>
#include <gui/model.h>
#include <gui/settings.h>
#include <gui/view.h>
#include <tests/test.h>
@ -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++) {

View File

@ -1,8 +1,8 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/settings.h>
#include <gui/builder.h>
#include <gui/settings.h>
#include <gui/view.h>
#include <gui/window.h>
#include <tests/test.h>
@ -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;
}