summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <pj@pehjota.net>2017-10-04 13:24:57 (EDT)
committer Patrick McDermott <pj@pehjota.net>2017-10-04 13:24:57 (EDT)
commit90598c17e5c215d26f36f078f3a64050cbb0f7e1 (patch)
treed0c61e92a0ad812858f6f496d49c213c3314463a
parent3547a37fbf25cd775e12fc55a8692f94b358128f (diff)
downloadmarquee-90598c17e5c215d26f36f078f3a64050cbb0f7e1.zip
marquee-90598c17e5c215d26f36f078f3a64050cbb0f7e1.tar.gz
marquee-90598c17e5c215d26f36f078f3a64050cbb0f7e1.tar.bz2
src/config.[ch]: New files
-rw-r--r--src/config.c120
-rw-r--r--src/config.h36
-rw-r--r--src/local.mk1
3 files changed, 157 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..ef9a4a7
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,120 @@
+/*
+ * Application configuration
+ *
+ * Copyright (C) 2017 Patrick McDermott
+ *
+ * This file is part of Marquee.
+ *
+ * Marquee is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Marquee is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Marquee. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <glib.h>
+
+#include "config.h"
+
+typedef enum {
+ TYPE_BOOLEAN,
+} Type;
+
+typedef struct {
+ Type type;
+ GList *callbacks;
+} ConfigItem;
+
+static void
+split_name(const gchar *name, gchar **group, gchar **key)
+{
+ *group = g_strdup(name);
+ *key = strchr(*group, '.');
+ *(key[0]) = '\0';
+ ++*key;
+}
+
+static void
+set_boolean(MqConfig *config, const gchar *name, gboolean value)
+{
+ gchar *group;
+ gchar *key;
+
+ split_name(name, &group, &key);
+
+ g_key_file_set_boolean(config->key_file, group, name, value);
+
+ if (!config->types_and_cbs_set) {
+ g_hash_table_insert(config->types_and_cbs, g_strdup(name),
+ TYPE_BOOLEAN);
+ }
+}
+
+static void
+set_defaults(MqConfig *config)
+{
+ config->types_and_cbs_set = FALSE;
+
+
+ config->types_and_cbs_set = TRUE;
+}
+
+MqConfig *
+mq_config_new(const gchar *profile)
+{
+ MqConfig *config;
+
+ config = malloc(sizeof(*config));
+
+ config->file_name = g_strdup_printf("%s/%s/config",
+ g_get_user_config_dir(), profile);
+ config->key_file = g_key_file_new();
+ config->types_and_cbs = g_hash_table_new(g_str_hash, g_int_equal);
+ set_defaults(config);
+
+ return config;
+}
+
+gboolean
+mq_config_load(MqConfig *config)
+{
+ /* TODO: Handle parsing and ENOENT errors differently? */
+ return g_key_file_load_from_file(config->key_file, config->file_name,
+ G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
+}
+
+gboolean
+mq_config_save(MqConfig *config)
+{
+ /* TODO: Handle GFileError? */
+ return g_key_file_save_to_file(config->key_file, config->file_name,
+ NULL);
+}
+
+void
+mq_config_set(MqConfig *config, const gchar *name, const gchar *value)
+{
+ ConfigItem *item;
+
+ item = g_hash_table_lookup(config->types_and_cbs, name);
+ g_assert(item);
+
+ switch (item->type) {
+ case TYPE_BOOLEAN:
+ /* value is "on" or "off" (as implemented in
+ * mq_html_input_checkbox()). */
+ set_boolean(config, name, value[1] == 'n' ? TRUE :
+ FALSE);
+ break;
+ }
+}
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..281319f
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,36 @@
+/*
+ * Application configuration
+ *
+ * Copyright (C) 2017 Patrick McDermott
+ *
+ * This file is part of Marquee.
+ *
+ * Marquee is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Marquee is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Marquee. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+typedef struct MqConfig MqConfig;
+
+#ifndef MQ_CONFIG_H
+#define MQ_CONFIG_H
+
+#include <glib.h>
+
+struct MqConfig {
+ gchar *file_name;
+ GKeyFile *key_file;
+ gboolean types_and_cbs_set;
+ GHashTable *types_and_cbs;
+};
+
+#endif
diff --git a/src/local.mk b/src/local.mk
index 0ffd545..bee2a35 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -1,6 +1,7 @@
marquee_SOURCES += \
%reldir%/main.c \
%reldir%/application.c \
+ %reldir%/config.c \
%reldir%/window.c \
%reldir%/tab.c \
%reldir%/tab-chrome.c \