summaryrefslogtreecommitdiff
path: root/saedit/config.c
blob: 1e204115be8234df539499c9b3fda617460342b9 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "config.h"
#include "xml.h"
#include <unistd.h>
#include <sys/stat.h>

static GKeyFile *_config_keyfile = NULL;

gchar *
_config_get_config_folder (void) {
	return g_strjoin (
		"/",
		g_get_user_config_dir (),
		"saedit2",
		NULL
	);
}

gchar *
_config_get_key_file_path (void) {
	gchar *dir = _config_get_config_folder ();
	gchar *result;

	result = g_strjoin (
		"/",
		dir,
		"config.ini",
		NULL
	);

	g_free (dir);
	return result;
}

void
config_keys_load () {
	gchar *filename;

	_config_keyfile = g_key_file_new ();

	filename = _config_get_key_file_path ();

	g_key_file_load_from_file (
		_config_keyfile,
		filename,
		0,
		NULL
	);

	g_free (filename);
}

gchar *
config_keys_get_data_folder_path () {
	gchar *result;

	if (_config_keyfile == NULL)
		config_keys_load ();
	
	result = g_key_file_get_value (
		_config_keyfile,
		"Default",
		"Data Folder",
		NULL
	);

	if (result == NULL)
		result = g_strdup ("");

	return result;
}

void
config_keys_set_data_folder_path (const gchar *filename) {
	if (_config_keyfile == NULL)
		config_keys_load ();

	g_key_file_set_value (
		_config_keyfile,
		"Default",
		"Data Folder",
		filename
	);
}

gint
config_keys_get_tile_size () {
	/* TODO */
	return 32;
}

void
config_keys_save () {
	gchar *filename;

	if (_config_keyfile == NULL)
		return;

	filename = _config_get_config_folder ();
	mkdir (filename, S_IRWXU);
	g_free (filename);

	filename = _config_get_key_file_path ();

	g_key_file_save_to_file (
		_config_keyfile,
		filename,
		NULL
	);

	g_free (filename);
}

static XMLNode *_config_paths_xml_root = NULL;

void
config_data_paths_load () {
	gchar *filename;
	gchar *df_path;

	df_path = config_keys_get_data_folder_path ();

	filename = g_strjoin (
		"/",
		df_path,
		"paths.xml",
		NULL
	);

	_config_paths_xml_root = xml_parse_file (filename);

	g_free (df_path);
	g_free (filename);
}

gchar *
config_data_paths_get_sprites_path () {
	XMLNode *node;
	GList *list;
	gchar *name;

	if (_config_paths_xml_root == NULL)
		config_data_paths_load ();

	node = _config_paths_xml_root;
	
	if (node != NULL) {
		list = node->sub_nodes;
		while (TRUE) {
			list = g_list_find_custom (
					list,
					"option",
					xml_node_compare_with_name_func
			);

			if (list == NULL)
				break;

			name = xml_node_get_attr_value (list->data, "name");
			if (name != NULL && g_strcmp0 (name, "sprites") == 0) {
				g_free (name);
				return xml_node_get_attr_value (list->data, "value");
			}

			g_free(name);
			list = list->next;
		}
	}

	return g_strdup ("");
}

gchar *
config_data_path_get_full_sprite_path (const gchar *rel_path) {
	gchar *data_folder, *sprites_path, *filename;

	data_folder = config_keys_get_data_folder_path ();
	sprites_path = config_data_paths_get_sprites_path ();

	filename = g_strjoin (
		"/",
		data_folder,
		sprites_path,
		rel_path,
		NULL
	);

	g_free (sprites_path);
	g_free (data_folder);

	return filename;
}