summaryrefslogtreecommitdiff
path: root/walls_conf.c
blob: 7b31d13026c8d9f504f84597a7e2fcaf94030cdb (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
#include <unistd.h>

#include <glib/gstdio.h>
#include "walls_conf.h"

GKeyFile *keyfile = NULL;

void conf_open() {
	gchar *filename;
	gchar *confdir;
	GError *error = NULL;

	keyfile = g_key_file_new();
	if(!keyfile) {
		g_warning("Could not create key");
		return;
	}

	filename = g_strdup_printf("%s/walls/config", g_get_user_config_dir());
	confdir = g_path_get_dirname(filename);
	if(g_access(confdir, F_OK) == -1) {
		g_mkdir_with_parents(confdir, 0700);
	}
	g_free(confdir);

	if(!g_key_file_load_from_file(keyfile, filename, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) {
		g_warning("Can't read config: %s", error->message);
		g_error_free(error);
	}

	g_free(filename);
}

gint conf_get_int(const gchar *group_name, const gchar *key, gint _default) {
	GError *error = NULL;
	gint ret;

	ret = g_key_file_get_integer(keyfile, group_name, key, &error);

	if(ret == 0 && error) {
		g_warning("Can't get key from config: %s", error->message);
		g_error_free(error);
		return _default;
	}
	return ret;
}

void conf_close() {
	gchar *s, *filename;
	GError *error = NULL;

	if(keyfile) {
		filename = g_strdup_printf("%s/walls/config", g_get_user_config_dir());
		s = g_key_file_to_data(keyfile, NULL, NULL);
		if(!g_file_set_contents(filename, s, -1, &error)) {
			g_warning("%s", error->message);
			g_error_free(error);
		}
		g_free(filename);
		g_free(s);

		g_key_file_free(keyfile);
		keyfile = NULL;
	}
}