summaryrefslogtreecommitdiffstats
path: root/src/config.c
blob: 701dc35fe84ec450478e64ab6fe3002649716a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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
add_type_and_callbacks(MqConfig *config, const gchar *name, Type type)
{
	ConfigItem *item;

	if (!config->types_and_cbs_set) {
		item = g_malloc(sizeof(*item));
		item->type = type;
		item->callbacks = NULL;
		g_hash_table_insert(config->types_and_cbs, g_strdup(name),
			item);
	}
}

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

	add_type_and_callbacks(config, 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;
	}
}