File: gm-options.c

package info (click to toggle)
gnoemoe 2.2.0%2Bdfsg-2.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,024 kB
  • sloc: ansic: 23,956; sh: 8,882; ruby: 300; makefile: 174; xml: 86
file content (330 lines) | stat: -rw-r--r-- 8,030 bytes parent folder | download | duplicates (5)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <glib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml/parser.h>
#include <unistd.h>

#include "gm-options.h"
#include "gm-string.h"
#include "gm-debug.h"

extern int errno;

#define GM_OPTIONS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), \
		GM_TYPE_OPTIONS, GmOptionsPrivate))

struct _GmOptionsPrivate {
	GHashTable *options;
	gchar *filepath;
};

/* Signals */

enum {
	OPTION_CHANGED,
	NUM_SIGNALS
};

#define XML_ROOT_NAME "options"
#define XML_OPTION_NAME "option"

static guint options_signals[NUM_SIGNALS] = {0};

G_DEFINE_TYPE(GmOptions, gm_options, G_TYPE_OBJECT)

static void
gm_options_finalize(GObject *object) {
	GmOptions *options = GM_OPTIONS(object);
	
	g_hash_table_destroy(options->priv->options);
	g_free(options->priv->filepath);
	
	G_OBJECT_CLASS(gm_options_parent_class)->finalize(object);
}

static void
gm_options_class_init(GmOptionsClass *klass) {
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	
	object_class->finalize = gm_options_finalize;

	options_signals[OPTION_CHANGED] = 
		g_signal_new("option_changed",
			G_OBJECT_CLASS_TYPE(object_class),
			G_SIGNAL_RUN_LAST,
			G_STRUCT_OFFSET(GmOptionsClass, option_changed),
			NULL, NULL,
			g_cclosure_marshal_VOID__STRING,
			G_TYPE_NONE,
			1,
			G_TYPE_STRING);
			
	g_type_class_add_private(object_class, sizeof(GmOptionsPrivate));
}

static void
gm_options_init(GmOptions *options) {
	options->priv = GM_OPTIONS_GET_PRIVATE(options);
	options->priv->options = g_hash_table_new_full(g_str_hash, g_str_equal,
			g_free, g_free);
	options->priv->filepath = NULL;
}

static void
gm_options_load_option(GmOptions *options, xmlDocPtr doc, xmlNodePtr ptr) {
	xmlChar *key, *value;

	key = xmlGetProp(ptr, (const xmlChar *)"key");
	value = xmlGetProp(ptr, (const xmlChar *)"value");
	
	if (key == NULL) {
		gm_debug_msg(DEBUG_DEFAULT, 
				"GmOptions.load_option: key not present in option");
	} else if (value == NULL) {
		gm_debug_msg(DEBUG_DEFAULT, 
				"GmOptions.load_option: value not present in option");
	} else {	
		gm_debug_msg(DEBUG_DEFAULT,
				"GmOptions.load_option: adding option %s: %s", key, value);
		gm_options_set(options, (gchar const *)key, (gchar const *)value);
	}
	
	xmlFree(key);
	xmlFree(value);
}

static void
gm_options_save_option(gchar *key, gchar *value, xmlNodePtr root) {
	xmlNodePtr option;

	gm_debug_msg(DEBUG_DEFAULT, "GmOptions.SaveValue: saving %s, %s", key, 
			value);
	
	option = xmlNewChild(root, NULL, (const xmlChar *)(XML_OPTION_NAME), NULL);
	xmlNewProp(option, (const xmlChar *)("key"), (const xmlChar *)(key));
	xmlNewProp(option, (const xmlChar *)("value"), (const xmlChar *)(value));
}

static void
gm_options_dup_option(gchar *key, gchar *value, GmOptions *copy) {
	gm_options_set(copy, key, value);
}

/* Public functions */

GmOptions *
gm_options_new(void) {
	GmOptions *options = GM_OPTIONS(g_object_new(GM_TYPE_OPTIONS, NULL));
	
	return options;
}

GmOptions *
gm_options_dup(GmOptions *source) {
	GmOptions *copy = gm_options_new();
	
	g_hash_table_foreach(source->priv->options, (GHFunc)gm_options_dup_option,
			copy);
	
	return copy;
}

// Adds an option to opt, even if key already exists
void
gm_options_set(GmOptions * options, gchar const *key, gchar const *value) {
	gchar *trimmed = gm_string_trim(key);
	gchar *lookup = g_hash_table_lookup(options->priv->options, trimmed);
	gboolean changed = lookup != NULL && lookup != value;
	
	if (lookup != value) {
		g_hash_table_insert(options->priv->options, g_strdup(trimmed), 
				g_strdup(value));
	}
	
	if (changed) {
		g_signal_emit(options, options_signals[OPTION_CHANGED], 0, trimmed);
	}
	
	g_free(trimmed);
}

gchar const *
gm_options_get(GmOptions *options, gchar const *key) {
	return g_hash_table_lookup(options->priv->options, key);
}

void
gm_options_set_int(GmOptions *options, gchar const *key, int value) {
	gchar val[15];

	g_snprintf((gchar *) &val, 15, "%d", value);
	gm_options_set(options, key, (gchar const *)val);
}

int
gm_options_get_int(GmOptions *options, gchar const *key) {
	gchar const *val = gm_options_get(options, key);
	int ret;

	if (val && gm_string_to_int(val, &ret)) {
		return ret;
	} else {
		return 0;
	}
}

void
gm_options_remove(GmOptions *options, gchar const *key) {
	g_hash_table_remove(options->priv->options, key);
}

void
gm_options_save(GmOptions *options) {
	xmlDocPtr doc;
	xmlNodePtr root;

	if (options->priv->filepath == NULL) {
		return;
	}

	gm_debug_msg(DEBUG_DEFAULT, "GmOptions.save: saving options (%s)!", 
			options->priv->filepath);

	doc = xmlNewDoc((const xmlChar *)("1.0"));
	root = xmlNewNode(NULL, (const xmlChar *)(XML_ROOT_NAME));
	xmlDocSetRootElement(doc, root);

	g_hash_table_foreach(options->priv->options, 
			(GHFunc)gm_options_save_option, root);

	xmlSaveFormatFileEnc(options->priv->filepath, doc, "UTF-8", 1);
	xmlFreeDoc(doc);

	// Make sure to make this only readable for the user and the group
	chmod(options->priv->filepath, 0660);
}

void
gm_options_save_as(GmOptions *options, gchar const *filename) {
	g_free(options->priv->filepath);
	options->priv->filepath = g_strdup(filename);
	
	gm_options_save(options);
}

gboolean
gm_options_load(GmOptions *options, gchar const *filename) {
	xmlDocPtr doc;
	xmlNodePtr root;
	
	gm_debug_msg(DEBUG_DEFAULT, "GmOptions.load: loading options (%s)!", 
			filename);

	g_free(options->priv->filepath);
	options->priv->filepath = g_strdup(filename);

	if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
		gm_debug_msg(DEBUG_DEFAULT, "GmOptions.load: file does not exist");
		return FALSE;
	}
	
	doc = xmlParseFile(filename);
	
	if (doc == NULL) {
		gm_debug_msg(DEBUG_DEFAULT, 
				"GmOptions.load: error on parsing options file");
		return FALSE;
	}
	
	root = xmlDocGetRootElement(doc);

	if (root == NULL) {
		xmlFreeDoc(doc);
		return FALSE;
	}
	
	if (xmlStrcmp(root->name, (const xmlChar *)(XML_ROOT_NAME))) {
		gm_debug_msg(DEBUG_DEFAULT, "GmOptions.load: invalid root node");
		xmlFreeDoc(doc);
		return FALSE;
	}

	for (root = root->xmlChildrenNode; root; root = root->next) {
		if (!xmlStrcmp(root->name, (const xmlChar *)(XML_OPTION_NAME))) {
			gm_options_load_option(options, doc, root);
		}
	}

	xmlFreeDoc(doc);
	return TRUE;
}

void 
_gm_options_check_old_options(gchar const *xmlname) {
	gchar *filename;
	xmlDocPtr doc;
	xmlNodePtr root;
	FILE *f;
	gchar **keyvalue, line[1024];
	gint i;

	filename = g_strdup(xmlname);
	filename[strlen(xmlname) - 4] = '\0';

	if (g_file_test(xmlname, G_FILE_TEST_EXISTS) || 
			!g_file_test(filename, G_FILE_TEST_EXISTS)) {
		g_free(filename);
		return;
	}

	doc = xmlNewDoc((const xmlChar *)("1.0"));
	root = xmlNewNode(NULL, (const xmlChar *)(XML_ROOT_NAME));
	xmlDocSetRootElement(doc, root);

	if ((f = fopen(filename, "r")) != NULL) {
		i = 0;
		while (fgets((char *) &line, 1024 - 1, f) != NULL) {
			line[strlen((char *) &line) - 1] = '\0';
			i++;
			
			if (strlen(line) != 0) {
				// Empty lines, we don't need to process those
				keyvalue = g_strsplit(line, "=", 2);
				// This will return at least 1 element, at most 2, we need 2
				if (strncmp(keyvalue[0], "#", 1) != 0) {
					// Commented lines, well ignore them too
					if (keyvalue[1] != NULL) {
						gm_debug_msg(DEBUG_DEFAULT, 
								"GmOptions.check_old_options: converting %s, %s", 
								keyvalue[0], keyvalue[1]);
						gm_options_save_option(keyvalue[0], keyvalue[1], root);
					} else {
						gm_debug_msg(DEBUG_DEFAULT, "GmOptions.load: wrong "
								"syntax of options line in %s line %d", 
								filename, i);
					}
				}
				
				g_strfreev(keyvalue);
			}
		}

		fclose(f);
	} else {
		gm_debug_msg(DEBUG_DEFAULT, "GmOptions.check_old_options: could not "
				"retrieve contents of file %s (%s)", filename, strerror(errno));
	}

	xmlSaveFormatFileEnc(xmlname, doc, "UTF-8", 1);
	xmlFreeDoc(doc);

	if (g_file_test(xmlname, G_FILE_TEST_EXISTS)) {
		// Make sure to make this only readable for the user and the group
		chmod(xmlname, 0660);
		unlink(filename);
	}
	
	g_free(filename);
}