forked from vain/pdfPres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.c
403 lines (338 loc) · 9.75 KB
/
prefs.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright 2010-2012 Peter Hofmann
This file is part of pdfpres.
pdfpres 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.
pdfpres 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 pdfpres. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <gtk/gtk.h>
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include "prefs.h"
struct _prefs prefs;
void loadPreferences(void)
{
char *prefspath = NULL;
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *cur_node = NULL;
xmlChar *tmp = NULL;
int tmp_i = 0;
/* Init default settings. */
prefs.initial_fit_mode = FIT_PAGE;
prefs.slide_context = 1;
prefs.do_wrapping = FALSE;
prefs.do_notectrl = FALSE;
prefs.cache_max = 32;
prefs.font_notes = g_strdup("Sans 12");
prefs.font_timer = g_strdup("Sans 35");
prefs.q_exits_fullscreen = FALSE;
prefs.timer_is_clock = FALSE;
prefs.stop_timer_on_fs = FALSE;
/* We're using g_strdup() here so we can use g_free() all the time.
*/
/* NOTE / FIXME: This is specific to unix. */
prefspath = g_strdup_printf("%s/.config/pdfpres/config.xml",
getenv("HOME"));
/* Try to read the file. */
doc = xmlReadFile(prefspath, NULL, 0);
g_free(prefspath);
if (doc == NULL)
{
fprintf(stderr, "[prefs] Config could not be read.\n");
xmlCleanupParser();
return;
}
/* Get the root element. */
root_element = xmlDocGetRootElement(doc);
if (root_element == NULL)
{
fprintf(stderr, "[prefs] Config has no root element.\n");
xmlFreeDoc(doc);
xmlCleanupParser();
return;
}
/* Traverse elements. */
/* TODO: Isn't there something like "get child with name XYZ"? */
for (cur_node = root_element->children; cur_node;
cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "initial_fit_mode")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Fit mode? Restrict to valid values. */
tmp_i = atoi((char *)tmp);
if (tmp_i == FIT_WIDTH
|| tmp_i == FIT_HEIGHT
|| tmp_i == FIT_PAGE)
{
prefs.initial_fit_mode = tmp_i;
}
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "slide_context")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Slide context? Restrict to sane values. */
tmp_i = atoi((char *)tmp);
if (tmp_i > 0 && tmp_i < 30)
{
prefs.slide_context = tmp_i;
}
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "do_wrapping")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Do wrapping? */
tmp_i = atoi((char *)tmp);
prefs.do_wrapping = (tmp_i == 1 ? TRUE : FALSE);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "do_notectrl")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Do note control? */
tmp_i = atoi((char *)tmp);
prefs.do_notectrl = (tmp_i == 1 ? TRUE : FALSE);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "cache_max")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Cache maximum? */
tmp_i = atoi((char *)tmp);
if (tmp_i > 0)
{
prefs.cache_max = tmp_i;
}
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "font_notes")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Font for notes? */
if (prefs.font_notes != NULL)
g_free(prefs.font_notes);
prefs.font_notes = g_strdup((char *)tmp);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "font_timer")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* Font for timer? */
if (prefs.font_timer != NULL)
g_free(prefs.font_timer);
prefs.font_timer = g_strdup((char *)tmp);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "q_exits_fullscreen")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* When in fullscreen, does Q/Esc quit the program (0) or
* does it exit fullscreen mode (1)? */
tmp_i = atoi((char *)tmp);
prefs.q_exits_fullscreen = (tmp_i == 1 ? TRUE : FALSE);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "timer_is_clock")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* If set to 1, show the current time instead of a timer.
* Furthermore, no buttons to control the timer shall be
* visible. */
tmp_i = atoi((char *)tmp);
prefs.timer_is_clock = (tmp_i == 1 ? TRUE : FALSE);
xmlFree(tmp);
}
if (cur_node->type == XML_ELEMENT_NODE
&& !xmlStrcmp(cur_node->name, BAD_CAST "stop_timer_on_fs")
&& (tmp = xmlGetProp(cur_node, BAD_CAST "v")) != NULL)
{
/* When in fullscreen, does Q/Esc quit the program (0) or
* does it exit fullscreen mode (1)? */
tmp_i = atoi((char *)tmp);
prefs.stop_timer_on_fs = (tmp_i == 1 ? TRUE : FALSE);
xmlFree(tmp);
}
}
xmlFreeDoc(doc);
xmlCleanupParser();
return;
}
static gboolean checkdir(char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) == -1)
{
if (mkdir(path, S_IRWXU) == -1)
{
fprintf(stderr, "[prefs] Could not create directory:"
"\n\t`%s'\n", path);
return FALSE;
}
}
return TRUE;
}
static gboolean writeRootElement(xmlTextWriterPtr writer)
{
int rc = 0;
/* Start of element. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "config");
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not start element `config'.\n");
xmlFreeTextWriter(writer);
xmlCleanupParser();
return FALSE;
}
return TRUE;
}
static gboolean writeElement(xmlTextWriterPtr writer, char *ele, char *v)
{
int rc = 0;
/* Start of element. */
rc = xmlTextWriterStartElement(writer, BAD_CAST ele);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not start element `%s'.\n", ele);
xmlFreeTextWriter(writer);
xmlCleanupParser();
return FALSE;
}
/* Write page number as attribute. */
rc = xmlTextWriterWriteFormatAttribute(writer,
BAD_CAST "v", "%s", v);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not write value `%s' "
"for element `%s'.\n", v, ele);
xmlFreeTextWriter(writer);
xmlCleanupParser();
return FALSE;
}
/* End of "slide" element. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not end element `%s'.\n", ele);
xmlFreeTextWriter(writer);
xmlCleanupParser();
return FALSE;
}
return TRUE;
}
static gboolean writeElementInt(xmlTextWriterPtr writer, char *ele, int v)
{
char *v_str = NULL;
gboolean result = FALSE;
v_str = g_strdup_printf("%d", v);
result = writeElement(writer, ele, v_str);
g_free(v_str);
return result;
}
static gboolean writeElementBoolean(xmlTextWriterPtr writer, char *ele,
gboolean v)
{
return writeElement(writer, ele, (v ? "1" : "0"));
}
void savePreferences(void)
{
char *prefspath = NULL;
xmlTextWriterPtr writer;
gboolean result = FALSE;
int rc = 0;
/* Ready up dirs. */
/* NOTE / FIXME: This is specific to unix. */
prefspath = g_strdup_printf("%s/.config", getenv("HOME"));
result = checkdir(prefspath);
g_free(prefspath);
if (!result)
return;
prefspath = g_strdup_printf("%s/.config/pdfpres", getenv("HOME"));
result = checkdir(prefspath);
g_free(prefspath);
if (!result)
return;
/* Directories ready. */
prefspath = g_strdup_printf("%s/.config/pdfpres/config.xml",
getenv("HOME"));
/* Create a new XmlWriter for uri, with no compression. */
writer = xmlNewTextWriterFilename(prefspath, 0);
g_free(prefspath);
if (writer == NULL)
{
fprintf(stderr, "[prefs] Can't write to config.xml.\n");
xmlCleanupParser();
return;
}
/* Activate indentation. An error is not fatal at this point. */
rc = xmlTextWriterSetIndent(writer, 1);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not activate xml indentation.\n");
}
/* Start the document with the xml default for the version, encoding
* UTF-8 and the default for the standalone declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not start document.\n");
xmlFreeTextWriter(writer);
xmlCleanupParser();
return;
}
/* Write all preferences. Note: On failure, these functions clean up
* the xml stuff by themselves. */
if (!writeRootElement(writer))
return;
if (!writeElementInt(writer, "initial_fit_mode",
prefs.initial_fit_mode))
return;
if (!writeElementInt(writer, "slide_context", prefs.slide_context))
return;
if (!writeElementBoolean(writer, "do_wrapping", prefs.do_wrapping))
return;
if (!writeElementBoolean(writer, "do_notectrl", prefs.do_notectrl))
return;
if (!writeElementInt(writer, "cache_max", prefs.cache_max))
return;
if (!writeElement(writer, "font_notes", prefs.font_notes))
return;
if (!writeElement(writer, "font_timer", prefs.font_timer))
return;
if (!writeElementBoolean(writer, "q_exits_fullscreen",
prefs.q_exits_fullscreen))
return;
if (!writeElementBoolean(writer, "stop_timer_on_fs",
prefs.stop_timer_on_fs))
return;
if (!writeElementBoolean(writer, "timer_is_clock", prefs.timer_is_clock))
return;
/* Finish. */
rc = xmlTextWriterEndDocument(writer);
if (rc < 0)
{
fprintf(stderr, "[prefs] Could not end document.\n");
}
xmlFreeTextWriter(writer);
xmlCleanupParser();
}