Merge remote-tracking branch 'github/merge-requests/451'
[geeqie.git] / src / rcfile.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <glib/gstdio.h>
23 #include <errno.h>
24
25 #include "main.h"
26 #include "rcfile.h"
27
28 #include "bar.h"
29 #include "bar_comment.h"
30 #include "bar_exif.h"
31 #include "bar_histogram.h"
32 #include "bar_keywords.h"
33 #include "bar_sort.h"
34 #include "editors.h"
35 #include "filefilter.h"
36 #include "misc.h"
37 #include "pixbuf-renderer.h"
38 #include "secure_save.h"
39 #include "slideshow.h"
40 #include "ui_fileops.h"
41 #include "layout.h"
42 #include "layout_util.h"
43 #include "bar.h"
44 #include "metadata.h"
45 #include "bar_gps.h"
46
47
48 /*
49  *-----------------------------------------------------------------------------
50  * line write/parse routines (public)
51  *-----------------------------------------------------------------------------
52  */
53
54 void write_indent(GString *str, gint indent)
55 {
56         g_string_append_printf(str, "\n%*s", indent * 4, "");
57 }
58
59 void write_char_option(GString *str, gint indent, const gchar *label, const gchar *text)
60 {
61         /* this is needed for overlay string, because g_markup_escape_text does not handle \n and such,
62            ideas for improvement are welcome
63         */
64         static const unsigned char no_quote_utf[] = {
65                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b,
66                 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
67                 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3,
68                 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
69                 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
70                 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
71                 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3,
72                 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
73                 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
74                 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
75                 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
76                 '"',  0 /* '"' is handled in g_markup_escape_text */
77         };
78
79         gchar *escval1 = g_strescape(text ? text : "", (gchar *) no_quote_utf);
80         gchar *escval2 = g_markup_escape_text(escval1, -1);
81         g_string_append_printf(str, "%s = \"%s\" ", label, escval2);
82         g_free(escval2);
83         g_free(escval1);
84 }
85
86 /* dummy read for old/obsolete/futur/deprecated/unused options */
87 gboolean read_dummy_option(const gchar *option, const gchar *label, const gchar *message)
88 {
89         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
90         log_printf(_("Option %s ignored: %s\n"), option, message);
91         return TRUE;
92 }
93
94
95 gboolean read_char_option(const gchar *option, const gchar *label, const gchar *value, gchar **text)
96 {
97         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
98         if (!text) return FALSE;
99
100         g_free(*text);
101         *text = g_strcompress(value);
102         return TRUE;
103 }
104
105 /* Since gdk_color_to_string() is only available since gtk 2.12
106  * here is an equivalent stub function. */
107 static gchar *color_to_string(GdkColor *color)
108 {
109         return g_strdup_printf("#%04X%04X%04X", color->red, color->green, color->blue);
110 }
111
112 void write_color_option(GString *str, gint indent, gchar *label, GdkColor *color)
113 {
114         if (color)
115                 {
116                 gchar *colorstring = color_to_string(color);
117
118                 write_char_option(str, indent, label, colorstring);
119                 g_free(colorstring);
120                 }
121         else
122                 write_char_option(str, indent, label, "");
123 }
124
125 gboolean read_color_option(const gchar *option, const gchar *label, const gchar *value, GdkColor *color)
126 {
127         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
128         if (!color) return FALSE;
129
130         if (!*value) return FALSE;
131         gdk_color_parse(value, color);
132         return TRUE;
133 }
134
135 void write_int_option(GString *str, gint indent, const gchar *label, gint n)
136 {
137         g_string_append_printf(str, "%s = \"%d\" ", label, n);
138 }
139
140 gboolean read_int_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
141 {
142         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
143         if (!n) return FALSE;
144
145         if (g_ascii_isdigit(value[0]) || (value[0] == '-' && g_ascii_isdigit(value[1])))
146                 {
147                 *n = strtol(value, NULL, 10);
148                 }
149         else
150                 {
151                 if (g_ascii_strcasecmp(value, "true") == 0)
152                         *n = 1;
153                 else
154                         *n = 0;
155                 }
156
157         return TRUE;
158 }
159
160 void write_ushort_option(GString *str, gint indent, const gchar *label, guint16 n)
161 {
162         g_string_append_printf(str, "%s = \"%uh\" ", label, n);
163 }
164
165 gboolean read_ushort_option(const gchar *option, const gchar *label, const gchar *value, guint16 *n)
166 {
167         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
168         if (!n) return FALSE;
169
170         if (g_ascii_isdigit(value[0]))
171                 {
172                 *n = strtoul(value, NULL, 10);
173                 }
174         else
175                 {
176                 if (g_ascii_strcasecmp(value, "true") == 0)
177                         *n = 1;
178                 else
179                         *n = 0;
180                 }
181
182         return TRUE;
183 }
184
185 void write_uint_option(GString *str, gint indent, const gchar *label, guint n)
186 {
187         g_string_append_printf(str, "%s = \"%u\" ", label, n);
188 }
189
190 gboolean read_uint_option(const gchar *option, const gchar *label, const gchar *value, guint *n)
191 {
192         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
193         if (!n) return FALSE;
194
195         if (g_ascii_isdigit(value[0]))
196                 {
197                 *n = strtoul(value, NULL, 10);
198                 }
199         else
200                 {
201                 if (g_ascii_strcasecmp(value, "true") == 0)
202                         *n = 1;
203                 else
204                         *n = 0;
205                 }
206
207         return TRUE;
208 }
209
210 gboolean read_uint_option_clamp(const gchar *option, const gchar *label, const gchar *value, guint *n, guint min, guint max)
211 {
212         gboolean ret;
213
214         ret = read_uint_option(option, label, value, n);
215         if (ret) *n = CLAMP(*n, min, max);
216
217         return ret;
218 }
219
220
221 gboolean read_int_option_clamp(const gchar *option, const gchar *label, const gchar *value, gint *n, gint min, gint max)
222 {
223         gboolean ret;
224
225         ret = read_int_option(option, label, value, n);
226         if (ret) *n = CLAMP(*n, min, max);
227
228         return ret;
229 }
230
231 void write_int_unit_option(GString *str, gint indent, gchar *label, gint n, gint subunits)
232 {
233         gint l, r;
234
235         if (subunits > 0)
236                 {
237                 l = n / subunits;
238                 r = n % subunits;
239                 }
240         else
241                 {
242                 l = n;
243                 r = 0;
244                 }
245
246         g_string_append_printf(str, "%s = \"%d.%d\" ", label, l, r);
247 }
248
249 gboolean read_int_unit_option(const gchar *option, const gchar *label, const gchar *value, gint *n, gint subunits)
250 {
251         gint l, r;
252         gchar *ptr, *buf;
253
254         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
255         if (!n) return FALSE;
256
257         buf = g_strdup(value);
258         ptr = buf;
259         while (*ptr != '\0' && *ptr != '.') ptr++;
260         if (*ptr == '.')
261                 {
262                 *ptr = '\0';
263                 l = strtol(value, NULL, 10);
264                 *ptr = '.';
265                 ptr++;
266                 r = strtol(ptr, NULL, 10);
267                 }
268         else
269                 {
270                 l = strtol(value, NULL, 10);
271                 r = 0;
272                 }
273
274         *n = l * subunits + r;
275         g_free(buf);
276
277         return TRUE;
278 }
279
280 void write_bool_option(GString *str, gint indent, gchar *label, gint n)
281 {
282         g_string_append_printf(str, "%s = \"%s\" ", label, n ? "true" : "false");
283 }
284
285 gboolean read_bool_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
286 {
287         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
288         if (!n) return FALSE;
289
290         if (g_ascii_strcasecmp(value, "true") == 0 || atoi(value) != 0)
291                 *n = TRUE;
292         else
293                 *n = FALSE;
294
295         return TRUE;
296 }
297
298 /*
299  *-----------------------------------------------------------------------------
300  * write fuctions for elements (private)
301  *-----------------------------------------------------------------------------
302  */
303
304 static void write_global_attributes(GString *outstr, gint indent)
305 {
306         /* General Options */
307         WRITE_NL(); WRITE_BOOL(*options, show_icon_names);
308         WRITE_SEPARATOR();
309
310         WRITE_NL(); WRITE_BOOL(*options, tree_descend_subdirs);
311         WRITE_NL(); WRITE_BOOL(*options, view_dir_list_single_click_enter);
312         WRITE_NL(); WRITE_BOOL(*options, lazy_image_sync);
313         WRITE_NL(); WRITE_BOOL(*options, update_on_time_change);
314         WRITE_SEPARATOR();
315
316         WRITE_NL(); WRITE_BOOL(*options, progressive_key_scrolling);
317         WRITE_NL(); WRITE_UINT(*options, keyboard_scroll_step);
318
319         WRITE_NL(); WRITE_UINT(*options, duplicates_similarity_threshold);
320         WRITE_NL(); WRITE_BOOL(*options, rot_invariant_sim);
321         WRITE_SEPARATOR();
322
323         WRITE_NL(); WRITE_BOOL(*options, mousewheel_scrolls);
324         WRITE_NL(); WRITE_BOOL(*options, image_lm_click_nav);
325         WRITE_NL(); WRITE_INT(*options, open_recent_list_maxsize);
326         WRITE_NL(); WRITE_INT(*options, dnd_icon_size);
327         WRITE_NL(); WRITE_BOOL(*options, place_dialogs_under_mouse);
328
329         WRITE_NL(); WRITE_BOOL(*options, save_window_positions);
330         WRITE_NL(); WRITE_BOOL(*options, use_saved_window_positions_for_new_windows);
331         WRITE_NL(); WRITE_BOOL(*options, tools_restore_state);
332
333         /* File operations Options */
334         WRITE_NL(); WRITE_BOOL(*options, file_ops.enable_in_place_rename);
335         WRITE_NL(); WRITE_BOOL(*options, file_ops.confirm_delete);
336         WRITE_NL(); WRITE_BOOL(*options, file_ops.enable_delete_key);
337         WRITE_NL(); WRITE_BOOL(*options, file_ops.safe_delete_enable);
338         WRITE_NL(); WRITE_CHAR(*options, file_ops.safe_delete_path);
339         WRITE_NL(); WRITE_INT(*options, file_ops.safe_delete_folder_maxsize);
340
341         /* Properties dialog Options */
342         WRITE_NL(); WRITE_CHAR(*options, properties.tabs_order);
343
344         /* Image Options */
345         WRITE_NL(); WRITE_UINT(*options, image.zoom_mode);
346
347         WRITE_SEPARATOR();
348         WRITE_NL(); WRITE_BOOL(*options, image.zoom_2pass);
349         WRITE_NL(); WRITE_BOOL(*options, image.zoom_to_fit_allow_expand);
350         WRITE_NL(); WRITE_UINT(*options, image.zoom_quality);
351         WRITE_NL(); WRITE_INT(*options, image.zoom_increment);
352         WRITE_NL(); WRITE_BOOL(*options, image.fit_window_to_image);
353         WRITE_NL(); WRITE_BOOL(*options, image.limit_window_size);
354         WRITE_NL(); WRITE_INT(*options, image.max_window_size);
355         WRITE_NL(); WRITE_BOOL(*options, image.limit_autofit_size);
356         WRITE_NL(); WRITE_INT(*options, image.max_autofit_size);
357         WRITE_NL(); WRITE_UINT(*options, image.scroll_reset_method);
358         WRITE_NL(); WRITE_INT(*options, image.tile_cache_max);
359         WRITE_NL(); WRITE_INT(*options, image.image_cache_max);
360         WRITE_NL(); WRITE_BOOL(*options, image.enable_read_ahead);
361         WRITE_NL(); WRITE_BOOL(*options, image.exif_rotate_enable);
362         WRITE_NL(); WRITE_BOOL(*options, image.use_custom_border_color);
363         WRITE_NL(); WRITE_BOOL(*options, image.use_custom_border_color_in_fullscreen);
364         WRITE_NL(); WRITE_COLOR(*options, image.border_color);
365         WRITE_NL(); WRITE_BOOL(*options, image.use_clutter_renderer);
366
367         /* Thumbnails Options */
368         WRITE_NL(); WRITE_INT(*options, thumbnails.max_width);
369         WRITE_NL(); WRITE_INT(*options, thumbnails.max_height);
370         WRITE_NL(); WRITE_BOOL(*options, thumbnails.enable_caching);
371         WRITE_NL(); WRITE_BOOL(*options, thumbnails.cache_into_dirs);
372         WRITE_NL(); WRITE_BOOL(*options, thumbnails.use_xvpics);
373         WRITE_NL(); WRITE_BOOL(*options, thumbnails.spec_standard);
374         WRITE_NL(); WRITE_UINT(*options, thumbnails.quality);
375         WRITE_NL(); WRITE_BOOL(*options, thumbnails.use_exif);
376
377         /* File sorting Options */
378         WRITE_NL(); WRITE_INT(*options, file_sort.method);
379         WRITE_NL(); WRITE_BOOL(*options, file_sort.ascending);
380         WRITE_NL(); WRITE_BOOL(*options, file_sort.case_sensitive);
381         WRITE_NL(); WRITE_BOOL(*options, file_sort.natural);
382
383         /* Fullscreen Options */
384         WRITE_NL(); WRITE_INT(*options, fullscreen.screen);
385         WRITE_NL(); WRITE_BOOL(*options, fullscreen.clean_flip);
386         WRITE_NL(); WRITE_BOOL(*options, fullscreen.disable_saver);
387         WRITE_NL(); WRITE_BOOL(*options, fullscreen.above);
388
389         WRITE_SEPARATOR();
390
391         /* Image Overlay Options */
392         WRITE_NL(); WRITE_CHAR(*options, image_overlay.template_string);
393
394         WRITE_NL(); WRITE_INT(*options, image_overlay.x);
395         WRITE_NL(); WRITE_INT(*options, image_overlay.y);
396         WRITE_NL(); WRITE_INT(*options, image_overlay.text_red);
397         WRITE_NL(); WRITE_INT(*options, image_overlay.text_green);
398         WRITE_NL(); WRITE_INT(*options, image_overlay.text_blue);
399         WRITE_NL(); WRITE_INT(*options, image_overlay.text_alpha);
400         WRITE_NL(); WRITE_INT(*options, image_overlay.background_red);
401         WRITE_NL(); WRITE_INT(*options, image_overlay.background_green);
402         WRITE_NL(); WRITE_INT(*options, image_overlay.background_blue);
403         WRITE_NL(); WRITE_INT(*options, image_overlay.background_alpha);
404         WRITE_NL(); WRITE_CHAR(*options, image_overlay.font);
405
406         /* Slideshow Options */
407         WRITE_NL(); WRITE_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION);
408         WRITE_NL(); WRITE_BOOL(*options, slideshow.random);
409         WRITE_NL(); WRITE_BOOL(*options, slideshow.repeat);
410
411         /* Collection Options */
412         WRITE_NL(); WRITE_BOOL(*options, collections.rectangular_selection);
413
414         /* Filtering Options */
415         WRITE_NL(); WRITE_BOOL(*options, file_filter.show_hidden_files);
416         WRITE_NL(); WRITE_BOOL(*options, file_filter.show_parent_directory);
417         WRITE_NL(); WRITE_BOOL(*options, file_filter.show_dot_directory);
418         WRITE_NL(); WRITE_BOOL(*options, file_filter.disable_file_extension_checks);
419         WRITE_NL(); WRITE_BOOL(*options, file_filter.disable);
420         WRITE_SEPARATOR();
421
422         /* Sidecars Options */
423         WRITE_NL(); WRITE_CHAR(*options, sidecar.ext);
424
425         /* Shell command */
426         WRITE_NL(); WRITE_CHAR(*options, shell.path);
427         WRITE_NL(); WRITE_CHAR(*options, shell.options);
428
429         /* Helpers */
430         WRITE_NL(); WRITE_CHAR(*options, helpers.html_browser.command_name);
431         WRITE_NL(); WRITE_CHAR(*options, helpers.html_browser.command_line);
432
433         /* Metadata Options */
434         WRITE_NL(); WRITE_BOOL(*options, metadata.enable_metadata_dirs);
435         WRITE_NL(); WRITE_BOOL(*options, metadata.save_in_image_file);
436         WRITE_NL(); WRITE_BOOL(*options, metadata.save_legacy_IPTC);
437         WRITE_NL(); WRITE_BOOL(*options, metadata.warn_on_write_problems);
438         WRITE_NL(); WRITE_BOOL(*options, metadata.save_legacy_format);
439         WRITE_NL(); WRITE_BOOL(*options, metadata.sync_grouped_files);
440         WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_write);
441         WRITE_NL(); WRITE_INT(*options, metadata.confirm_timeout);
442         WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_after_timeout);
443         WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_on_image_change);
444         WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_on_dir_change);
445         WRITE_NL(); WRITE_BOOL(*options, metadata.keywords_case_sensitive);
446         WRITE_NL(); WRITE_BOOL(*options, metadata.write_orientation);
447
448         WRITE_NL(); WRITE_INT(*options, stereo.mode);
449         WRITE_NL(); WRITE_INT(*options, stereo.fsmode);
450         WRITE_NL(); WRITE_BOOL(*options, stereo.enable_fsmode);
451         WRITE_NL(); WRITE_INT(*options, stereo.fixed_w);
452         WRITE_NL(); WRITE_INT(*options, stereo.fixed_h);
453         WRITE_NL(); WRITE_INT(*options, stereo.fixed_x1);
454         WRITE_NL(); WRITE_INT(*options, stereo.fixed_y1);
455         WRITE_NL(); WRITE_INT(*options, stereo.fixed_x2);
456         WRITE_NL(); WRITE_INT(*options, stereo.fixed_y2);
457 }
458
459 static void write_color_profile(GString *outstr, gint indent)
460 {
461         gint i;
462 #ifndef HAVE_LCMS
463         g_string_append_printf(outstr, "<!-- NOTICE: %s was not built with support for color profiles,\n"
464                             "         color profile options will have no effect.\n-->\n", GQ_APPNAME);
465 #endif
466
467         WRITE_NL(); WRITE_STRING("<color_profiles ");
468         WRITE_CHAR(options->color_profile, screen_file);
469         WRITE_BOOL(options->color_profile, enabled);
470         WRITE_BOOL(options->color_profile, use_image);
471         WRITE_INT(options->color_profile, input_type);
472         WRITE_BOOL(options->color_profile, use_x11_screen_profile);
473         WRITE_INT(options->color_profile, render_intent);
474         WRITE_STRING(">");
475
476         indent++;
477         for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
478                 {
479                 WRITE_NL(); WRITE_STRING("<profile ");
480                 write_char_option(outstr, indent, "input_file", options->color_profile.input_file[i]);
481                 write_char_option(outstr, indent, "input_name", options->color_profile.input_name[i]);
482                 WRITE_STRING("/>");
483                 }
484         indent--;
485         WRITE_NL(); WRITE_STRING("</color_profiles>");
486 }
487
488
489 /*
490  *-----------------------------------------------------------------------------
491  * save configuration (public)
492  *-----------------------------------------------------------------------------
493  */
494
495 gboolean save_config_to_file(const gchar *utf8_path, ConfOptions *options)
496 {
497         SecureSaveInfo *ssi;
498         gchar *rc_pathl;
499         GString *outstr;
500         gint indent = 0;
501         GList *work;
502
503         rc_pathl = path_from_utf8(utf8_path);
504         ssi = secure_open(rc_pathl);
505         g_free(rc_pathl);
506         if (!ssi)
507                 {
508                 log_printf(_("error saving config file: %s\n"), utf8_path);
509                 return FALSE;
510                 }
511
512         outstr = g_string_new("");
513         g_string_append_printf(outstr, "<!--\n");
514         g_string_append_printf(outstr, "######################################################################\n");
515         g_string_append_printf(outstr, "# %30s config file      version %-10s #\n", GQ_APPNAME, VERSION);
516         g_string_append_printf(outstr, "######################################################################\n");
517         WRITE_SEPARATOR();
518
519         WRITE_STRING("# Note: This file is autogenerated. Options can be changed here,\n");
520         WRITE_STRING("#       but user comments and formatting will be lost.\n");
521         WRITE_SEPARATOR();
522         WRITE_STRING("-->\n");
523         WRITE_SEPARATOR();
524
525         WRITE_STRING("<gq>\n");
526         indent++;
527
528         WRITE_NL(); WRITE_STRING("<global\n");
529         indent++;
530         write_global_attributes(outstr, indent + 1);
531         indent--;
532         WRITE_STRING(">\n");
533
534         indent++;
535
536         write_color_profile(outstr, indent);
537
538         WRITE_SEPARATOR();
539         filter_write_list(outstr, indent);
540
541         WRITE_SEPARATOR();
542         keyword_tree_write_config(outstr, indent);
543         indent--;
544         WRITE_NL(); WRITE_STRING("</global>\n");
545
546         WRITE_SEPARATOR();
547
548         /* Layout Options */
549         work = layout_window_list;
550         while (work)
551                 {
552                 LayoutWindow *lw = work->data;
553                 layout_write_config(lw, outstr, indent);
554                 work = work->next;
555                 }
556
557         indent--;
558         WRITE_NL(); WRITE_STRING("</gq>\n");
559         WRITE_SEPARATOR();
560
561         secure_fputs(ssi, outstr->str);
562         g_string_free(outstr, TRUE);
563
564         if (secure_close(ssi))
565                 {
566                 log_printf(_("error saving config file: %s\nerror: %s\n"), utf8_path,
567                            secsave_strerror(secsave_errno));
568                 return FALSE;
569                 }
570
571         return TRUE;
572 }
573
574 /*
575  *-----------------------------------------------------------------------------
576  * loading attributes for elements (private)
577  *-----------------------------------------------------------------------------
578  */
579
580
581 static gboolean load_global_params(const gchar **attribute_names, const gchar **attribute_values)
582 {
583         while (*attribute_names)
584                 {
585                 const gchar *option = *attribute_names++;
586                 const gchar *value = *attribute_values++;
587
588                 /* General options */
589                 if (READ_BOOL(*options, show_icon_names)) continue;
590
591                 if (READ_BOOL(*options, tree_descend_subdirs)) continue;
592                 if (READ_BOOL(*options, view_dir_list_single_click_enter)) continue;
593                 if (READ_BOOL(*options, lazy_image_sync)) continue;
594                 if (READ_BOOL(*options, update_on_time_change)) continue;
595
596                 if (READ_UINT_CLAMP(*options, duplicates_similarity_threshold, 0, 100)) continue;
597                 if (READ_BOOL(*options, rot_invariant_sim)) continue;
598
599                 if (READ_BOOL(*options, progressive_key_scrolling)) continue;
600                 if (READ_UINT_CLAMP(*options, keyboard_scroll_step, 1, 32)) continue;
601
602                 if (READ_BOOL(*options, mousewheel_scrolls)) continue;
603                 if (READ_BOOL(*options, image_lm_click_nav)) continue;
604
605                 if (READ_INT(*options, open_recent_list_maxsize)) continue;
606                 if (READ_INT(*options, dnd_icon_size)) continue;
607                 if (READ_BOOL(*options, place_dialogs_under_mouse)) continue;
608
609                 if (READ_BOOL(*options, save_window_positions)) continue;
610                 if (READ_BOOL(*options, use_saved_window_positions_for_new_windows)) continue;
611                 if (READ_BOOL(*options, tools_restore_state)) continue;
612
613                 /* Properties dialog options */
614                 if (READ_CHAR(*options, properties.tabs_order)) continue;
615
616                 /* Image options */
617                 if (READ_UINT_CLAMP(*options, image.zoom_mode, 0, ZOOM_RESET_NONE)) continue;
618                 if (READ_BOOL(*options, image.zoom_2pass)) continue;
619                 if (READ_BOOL(*options, image.zoom_to_fit_allow_expand)) continue;
620                 if (READ_BOOL(*options, image.fit_window_to_image)) continue;
621                 if (READ_BOOL(*options, image.limit_window_size)) continue;
622                 if (READ_INT(*options, image.max_window_size)) continue;
623                 if (READ_BOOL(*options, image.limit_autofit_size)) continue;
624                 if (READ_INT(*options, image.max_autofit_size)) continue;
625                 if (READ_UINT_CLAMP(*options, image.scroll_reset_method, 0, PR_SCROLL_RESET_COUNT - 1)) continue;
626                 if (READ_INT(*options, image.tile_cache_max)) continue;
627                 if (READ_INT(*options, image.image_cache_max)) continue;
628                 if (READ_UINT_CLAMP(*options, image.zoom_quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER)) continue;
629                 if (READ_INT(*options, image.zoom_increment)) continue;
630                 if (READ_BOOL(*options, image.enable_read_ahead)) continue;
631                 if (READ_BOOL(*options, image.exif_rotate_enable)) continue;
632                 if (READ_BOOL(*options, image.use_custom_border_color)) continue;
633                 if (READ_BOOL(*options, image.use_custom_border_color_in_fullscreen)) continue;
634                 if (READ_COLOR(*options, image.border_color)) continue;
635                 if (READ_BOOL(*options, image.use_clutter_renderer)) continue;
636
637                 /* Thumbnails options */
638                 if (READ_INT_CLAMP(*options, thumbnails.max_width, 16, 512)) continue;
639                 if (READ_INT_CLAMP(*options, thumbnails.max_height, 16, 512)) continue;
640
641                 if (READ_BOOL(*options, thumbnails.enable_caching)) continue;
642                 if (READ_BOOL(*options, thumbnails.cache_into_dirs)) continue;
643                 if (READ_BOOL(*options, thumbnails.use_xvpics)) continue;
644                 if (READ_BOOL(*options, thumbnails.spec_standard)) continue;
645                 if (READ_UINT_CLAMP(*options, thumbnails.quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER)) continue;
646                 if (READ_BOOL(*options, thumbnails.use_exif)) continue;
647
648                 /* File sorting options */
649                 if (READ_UINT(*options, file_sort.method)) continue;
650                 if (READ_BOOL(*options, file_sort.ascending)) continue;
651                 if (READ_BOOL(*options, file_sort.case_sensitive)) continue;
652                 if (READ_BOOL(*options, file_sort.natural)) continue;
653
654                 /* File operations *options */
655                 if (READ_BOOL(*options, file_ops.enable_in_place_rename)) continue;
656                 if (READ_BOOL(*options, file_ops.confirm_delete)) continue;
657                 if (READ_BOOL(*options, file_ops.enable_delete_key)) continue;
658                 if (READ_BOOL(*options, file_ops.safe_delete_enable)) continue;
659                 if (READ_CHAR(*options, file_ops.safe_delete_path)) continue;
660                 if (READ_INT(*options, file_ops.safe_delete_folder_maxsize)) continue;
661
662                 /* Fullscreen options */
663                 if (READ_INT(*options, fullscreen.screen)) continue;
664                 if (READ_BOOL(*options, fullscreen.clean_flip)) continue;
665                 if (READ_BOOL(*options, fullscreen.disable_saver)) continue;
666                 if (READ_BOOL(*options, fullscreen.above)) continue;
667
668                 /* Image overlay */
669                 if (READ_CHAR(*options, image_overlay.template_string)) continue;
670                 if (READ_INT(*options, image_overlay.x)) continue;
671                 if (READ_INT(*options, image_overlay.y)) continue;
672                 if (READ_USHORT(*options, image_overlay.text_red)) continue;
673                 if (READ_USHORT(*options, image_overlay.text_green)) continue;
674                 if (READ_USHORT(*options, image_overlay.text_blue)) continue;
675                 if (READ_USHORT(*options, image_overlay.text_alpha)) continue;
676                 if (READ_USHORT(*options, image_overlay.background_red)) continue;
677                 if (READ_USHORT(*options, image_overlay.background_green)) continue;
678                 if (READ_USHORT(*options, image_overlay.background_blue)) continue;
679                 if (READ_USHORT(*options, image_overlay.background_alpha)) continue;
680                 if (READ_CHAR(*options, image_overlay.font)) continue;
681
682                 /* Slideshow options */
683                 if (READ_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION)) continue;
684                 if (READ_BOOL(*options, slideshow.random)) continue;
685                 if (READ_BOOL(*options, slideshow.repeat)) continue;
686
687                 /* Collection options */
688                 if (READ_BOOL(*options, collections.rectangular_selection)) continue;
689
690                 /* Filtering options */
691                 if (READ_BOOL(*options, file_filter.show_hidden_files)) continue;
692                 if (READ_BOOL(*options, file_filter.show_parent_directory)) continue;
693                 if (READ_BOOL(*options, file_filter.show_dot_directory)) continue;
694                 if (READ_BOOL(*options, file_filter.disable_file_extension_checks)) continue;
695                 if (READ_BOOL(*options, file_filter.disable)) continue;
696                 if (READ_CHAR(*options, sidecar.ext)) continue;
697
698                 /* Color Profiles */
699
700                 /* Shell command */
701                 if (READ_CHAR(*options, shell.path)) continue;
702                 if (READ_CHAR(*options, shell.options)) continue;
703
704                 /* Helpers */
705                 if (READ_CHAR(*options, helpers.html_browser.command_name)) continue;
706                 if (READ_CHAR(*options, helpers.html_browser.command_line)) continue;
707
708                 /* Metadata */
709                 if (READ_BOOL(*options, metadata.enable_metadata_dirs)) continue;
710                 if (READ_BOOL(*options, metadata.save_in_image_file)) continue;
711                 if (READ_BOOL(*options, metadata.save_legacy_IPTC)) continue;
712                 if (READ_BOOL(*options, metadata.warn_on_write_problems)) continue;
713                 if (READ_BOOL(*options, metadata.save_legacy_format)) continue;
714                 if (READ_BOOL(*options, metadata.sync_grouped_files)) continue;
715                 if (READ_BOOL(*options, metadata.confirm_write)) continue;
716                 if (READ_BOOL(*options, metadata.confirm_after_timeout)) continue;
717                 if (READ_INT(*options, metadata.confirm_timeout)) continue;
718                 if (READ_BOOL(*options, metadata.confirm_on_image_change)) continue;
719                 if (READ_BOOL(*options, metadata.confirm_on_dir_change)) continue;
720                 if (READ_BOOL(*options, metadata.keywords_case_sensitive)) continue;
721                 if (READ_BOOL(*options, metadata.write_orientation)) continue;
722
723                 if (READ_INT(*options, stereo.mode)) continue;
724                 if (READ_INT(*options, stereo.fsmode)) continue;
725                 if (READ_BOOL(*options, stereo.enable_fsmode)) continue;
726                 if (READ_INT(*options, stereo.fixed_w)) continue;
727                 if (READ_INT(*options, stereo.fixed_h)) continue;
728                 if (READ_INT(*options, stereo.fixed_x1)) continue;
729                 if (READ_INT(*options, stereo.fixed_y1)) continue;
730                 if (READ_INT(*options, stereo.fixed_x2)) continue;
731                 if (READ_INT(*options, stereo.fixed_y2)) continue;
732
733                 /* Dummy options */
734                 if (READ_DUMMY(*options, image.dither_quality, "deprecated since 2012-08-13")) continue;
735
736                 /* Unknown options */
737                 log_printf("unknown attribute %s = %s\n", option, value);
738                 }
739
740         return TRUE;
741 }
742
743 static void options_load_color_profiles(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
744 {
745         while (*attribute_names)
746                 {
747                 const gchar *option = *attribute_names++;
748                 const gchar *value = *attribute_values++;
749
750                 if (READ_BOOL(options->color_profile, enabled)) continue;
751                 if (READ_BOOL(options->color_profile, use_image)) continue;
752                 if (READ_INT(options->color_profile, input_type)) continue;
753                 if (READ_CHAR(options->color_profile, screen_file)) continue;
754                 if (READ_BOOL(options->color_profile, use_x11_screen_profile)) continue;
755                 if (READ_INT(options->color_profile, render_intent)) continue;
756
757                 log_printf("unknown attribute %s = %s\n", option, value);
758                 }
759
760 }
761
762 static void options_load_profile(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
763 {
764         gint i = GPOINTER_TO_INT(data);
765         if (i < 0 || i >= COLOR_PROFILE_INPUTS) return;
766         while (*attribute_names)
767                 {
768                 const gchar *option = *attribute_names++;
769                 const gchar *value = *attribute_values++;
770
771                 if (READ_CHAR_FULL("input_file", options->color_profile.input_file[i])) continue;
772                 if (READ_CHAR_FULL("input_name", options->color_profile.input_name[i])) continue;
773
774                 log_printf("unknown attribute %s = %s\n", option, value);
775                 }
776         i++;
777         options_parse_func_set_data(parser_data, GINT_TO_POINTER(i));
778
779 }
780
781
782
783 /*
784  *-----------------------------------------------------------------------------
785  * xml file structure (private)
786  *-----------------------------------------------------------------------------
787  */
788 struct _GQParserData
789 {
790         GList *parse_func_stack;
791         gboolean startup; /* reading config for the first time - add commandline and defaults */
792 };
793
794 static const gchar *options_get_id(const gchar **attribute_names, const gchar **attribute_values)
795 {
796         while (*attribute_names)
797                 {
798                 const gchar *option = *attribute_names++;
799                 const gchar *value = *attribute_values++;
800
801                 if (strcmp(option, "id") == 0) return value;
802
803                 }
804         return NULL;
805 }
806
807
808 void options_parse_leaf(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
809 {
810         log_printf("unexpected: %s\n", element_name);
811         options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
812 }
813
814 static void options_parse_color_profiles(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
815 {
816         if (g_ascii_strcasecmp(element_name, "profile") == 0)
817                 {
818                 options_load_profile(parser_data, context, element_name, attribute_names, attribute_values, data, error);
819                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
820                 }
821         else
822                 {
823                 log_printf("unexpected in <profile>: <%s>\n", element_name);
824                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
825                 }
826 }
827
828 static void options_parse_filter(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
829 {
830         if (g_ascii_strcasecmp(element_name, "file_type") == 0)
831                 {
832                 filter_load_file_type(attribute_names, attribute_values);
833                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
834                 }
835         else
836                 {
837                 log_printf("unexpected in <filter>: <%s>\n", element_name);
838                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
839                 }
840 }
841
842 static void options_parse_filter_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
843 {
844         if (parser_data->startup) filter_add_defaults();
845         filter_rebuild();
846 }
847
848 static void options_parse_keyword_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
849 {
850         GtkTreeIter *iter_ptr = data;
851         gtk_tree_iter_free(iter_ptr);
852 }
853
854
855 static void options_parse_keyword(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
856 {
857         GtkTreeIter *iter_ptr = data;
858         if (g_ascii_strcasecmp(element_name, "keyword") == 0)
859                 {
860                 GtkTreeIter *child = keyword_add_from_config(keyword_tree, iter_ptr, attribute_names, attribute_values);
861                 options_parse_func_push(parser_data, options_parse_keyword, options_parse_keyword_end, child);
862                 }
863         else
864                 {
865                 log_printf("unexpected in <keyword>: <%s>\n", element_name);
866                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
867                 }
868 }
869
870
871
872 static void options_parse_keyword_tree(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
873 {
874         if (g_ascii_strcasecmp(element_name, "keyword") == 0)
875                 {
876                 GtkTreeIter *iter_ptr = keyword_add_from_config(keyword_tree, NULL, attribute_names, attribute_values);
877                 options_parse_func_push(parser_data, options_parse_keyword, options_parse_keyword_end, iter_ptr);
878                 }
879         else
880                 {
881                 log_printf("unexpected in <keyword_tree>: <%s>\n", element_name);
882                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
883                 }
884 }
885
886
887 static void options_parse_global(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
888 {
889         if (g_ascii_strcasecmp(element_name, "color_profiles") == 0)
890                 {
891                 options_load_color_profiles(parser_data, context, element_name, attribute_names, attribute_values, data, error);
892                 options_parse_func_push(parser_data, options_parse_color_profiles, NULL, GINT_TO_POINTER(0));
893                 }
894         else if (g_ascii_strcasecmp(element_name, "filter") == 0)
895                 {
896                 options_parse_func_push(parser_data, options_parse_filter, options_parse_filter_end, NULL);
897                 }
898         else if (g_ascii_strcasecmp(element_name, "keyword_tree") == 0)
899                 {
900                 if (!keyword_tree) keyword_tree_new();
901                 options_parse_func_push(parser_data, options_parse_keyword_tree, NULL, NULL);
902                 }
903         else
904                 {
905                 log_printf("unexpected in <global>: <%s>\n", element_name);
906                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
907                 }
908 }
909
910 static void options_parse_global_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
911 {
912 #ifndef HAVE_EXIV2
913         /* some options do not work without exiv2 */
914         options->metadata.save_in_image_file = FALSE;
915         options->metadata.save_legacy_format = TRUE;
916         options->metadata.write_orientation = FALSE;
917         DEBUG_1("compiled without Exiv2 - disabling XMP write support");
918 #endif
919 }
920
921 static void options_parse_pane_exif(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
922 {
923         GtkWidget *pane = data;
924         if (g_ascii_strcasecmp(element_name, "entry") == 0)
925                 {
926                 bar_pane_exif_entry_add_from_config(pane, attribute_names, attribute_values);
927                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
928                 }
929         else
930                 {
931                 log_printf("unexpected in <pane_exif>: <%s>\n", element_name);
932                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
933                 }
934 }
935
936 static void options_parse_bar(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
937 {
938         GtkWidget *bar = data;
939         if (g_ascii_strcasecmp(element_name, "pane_comment") == 0)
940                 {
941                 GtkWidget *pane = bar_find_pane_by_id(bar, PANE_COMMENT, options_get_id(attribute_names, attribute_values));
942                 if (pane)
943                         {
944                         bar_pane_comment_update_from_config(pane, attribute_names, attribute_values);
945                         }
946                 else
947                         {
948                         pane = bar_pane_comment_new_from_config(attribute_names, attribute_values);
949                         bar_add(bar, pane);
950                         }
951                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
952                 }
953 #ifdef HAVE_LIBCHAMPLAIN
954 #ifdef HAVE_LIBCHAMPLAIN_GTK
955         else if (g_ascii_strcasecmp(element_name, "pane_gps") == 0)
956                 {
957                 GtkWidget *pane = bar_find_pane_by_id(bar, PANE_GPS, options_get_id(attribute_names, attribute_values));
958                 if (pane)
959                         {
960                         bar_pane_gps_update_from_config(pane, attribute_names, attribute_values);
961                         }
962                 else
963                         {
964                         pane = bar_pane_gps_new_from_config(attribute_names, attribute_values);
965                         bar_add(bar, pane);
966                         }
967                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
968                 }
969 #endif
970 #endif
971         else if (g_ascii_strcasecmp(element_name, "pane_exif") == 0)
972                 {
973                 GtkWidget *pane = bar_find_pane_by_id(bar, PANE_EXIF, options_get_id(attribute_names, attribute_values));
974                 if (pane)
975                         {
976                         bar_pane_exif_update_from_config(pane, attribute_names, attribute_values);
977                         }
978                 else
979                         {
980                         pane = bar_pane_exif_new_from_config(attribute_names, attribute_values);
981                         bar_add(bar, pane);
982                         }
983                 options_parse_func_push(parser_data, options_parse_pane_exif, NULL, pane);
984                 }
985         else if (g_ascii_strcasecmp(element_name, "pane_histogram") == 0)
986                 {
987                 GtkWidget *pane = bar_find_pane_by_id(bar, PANE_HISTOGRAM, options_get_id(attribute_names, attribute_values));
988                 if (pane)
989                         {
990                         bar_pane_histogram_update_from_config(pane, attribute_names, attribute_values);
991                         }
992                 else
993                         {
994                         pane = bar_pane_histogram_new_from_config(attribute_names, attribute_values);
995                         bar_add(bar, pane);
996                         }
997                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
998                 }
999         else if (g_ascii_strcasecmp(element_name, "pane_keywords") == 0)
1000                 {
1001                 GtkWidget *pane = bar_find_pane_by_id(bar, PANE_KEYWORDS, options_get_id(attribute_names, attribute_values));
1002                 if (pane)
1003                         {
1004                         bar_pane_keywords_update_from_config(pane, attribute_names, attribute_values);
1005                         }
1006                 else
1007                         {
1008                         pane = bar_pane_keywords_new_from_config(attribute_names, attribute_values);
1009                         bar_add(bar, pane);
1010                         }
1011                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1012                 }
1013         else if (g_ascii_strcasecmp(element_name, "clear") == 0)
1014                 {
1015                 bar_clear(bar);
1016                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1017                 }
1018         else
1019                 {
1020                 log_printf("unexpected in <bar>: <%s>\n", element_name);
1021                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1022                 }
1023 }
1024
1025 /* Just a dummy function to parse out old leftovers
1026  *
1027  * This function can be cleaned somedays.
1028  */
1029 static void options_parse_toolbar_and_statusbar(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
1030 {
1031         options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1032 }
1033
1034 static void options_parse_layout(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
1035 {
1036         LayoutWindow *lw = data;
1037         if (g_ascii_strcasecmp(element_name, "bar") == 0)
1038                 {
1039                 if (!lw->bar)
1040                         {
1041                         GtkWidget *bar = bar_new_from_config(lw, attribute_names, attribute_values);
1042                         layout_bar_set(lw, bar);
1043                         }
1044                 else
1045                         {
1046                         bar_update_from_config(lw->bar, attribute_names, attribute_values);
1047                         }
1048
1049                 options_parse_func_push(parser_data, options_parse_bar, NULL, lw->bar);
1050                 }
1051         else if (g_ascii_strcasecmp(element_name, "bar_sort") == 0)
1052                 {
1053                 GtkWidget *bar = bar_sort_new_from_config(lw, attribute_names, attribute_values);
1054                 layout_bar_sort_set(lw, bar);
1055                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1056                 }
1057         else if (g_ascii_strcasecmp(element_name, "toolbar") == 0)
1058                 {
1059                 options_parse_func_push(parser_data, options_parse_toolbar_and_statusbar, NULL, NULL);
1060                 }
1061         else if (g_ascii_strcasecmp(element_name, "statusbar") == 0)
1062                 {
1063                 options_parse_func_push(parser_data, options_parse_toolbar_and_statusbar, NULL, NULL);
1064                 }
1065         else
1066                 {
1067                 log_printf("unexpected in <layout>: <%s>\n", element_name);
1068                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1069                 }
1070 }
1071
1072 static void options_parse_layout_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
1073 {
1074         LayoutWindow *lw = data;
1075         layout_util_sync(lw);
1076 }
1077
1078 static void options_parse_toplevel(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
1079 {
1080         if (g_ascii_strcasecmp(element_name, "gq") == 0)
1081                 {
1082                 /* optional top-level node */
1083                 options_parse_func_push(parser_data, options_parse_toplevel, NULL, NULL);
1084                 return;
1085                 }
1086         if (g_ascii_strcasecmp(element_name, "global") == 0)
1087                 {
1088                 load_global_params(attribute_names, attribute_values);
1089                 options_parse_func_push(parser_data, options_parse_global, options_parse_global_end, NULL);
1090                 return;
1091                 }
1092
1093         if (g_ascii_strcasecmp(element_name, "layout") == 0)
1094                 {
1095                 LayoutWindow *lw;
1096                 lw = layout_find_by_layout_id(options_get_id(attribute_names, attribute_values));
1097                 if (lw)
1098                         {
1099                         layout_update_from_config(lw, attribute_names, attribute_values);
1100                         }
1101                 else
1102                         {
1103                         lw = layout_new_from_config(attribute_names, attribute_values, parser_data->startup);
1104                         }
1105                 options_parse_func_push(parser_data, options_parse_layout, options_parse_layout_end, lw);
1106                 }
1107         else
1108                 {
1109                 log_printf("unexpected in <toplevel>: <%s>\n", element_name);
1110                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
1111                 }
1112 }
1113
1114
1115
1116
1117
1118 /*
1119  *-----------------------------------------------------------------------------
1120  * parser
1121  *-----------------------------------------------------------------------------
1122  */
1123
1124
1125 struct _GQParserFuncData
1126 {
1127         GQParserStartFunc start_func;
1128         GQParserEndFunc end_func;
1129         gpointer data;
1130 };
1131
1132 void options_parse_func_push(GQParserData *parser_data, GQParserStartFunc start_func, GQParserEndFunc end_func, gpointer data)
1133 {
1134         GQParserFuncData *func_data = g_new0(GQParserFuncData, 1);
1135         func_data->start_func = start_func;
1136         func_data->end_func = end_func;
1137         func_data->data = data;
1138
1139         parser_data->parse_func_stack = g_list_prepend(parser_data->parse_func_stack, func_data);
1140 }
1141
1142 void options_parse_func_pop(GQParserData *parser_data)
1143 {
1144         g_free(parser_data->parse_func_stack->data);
1145         parser_data->parse_func_stack = g_list_delete_link(parser_data->parse_func_stack, parser_data->parse_func_stack);
1146 }
1147
1148 void options_parse_func_set_data(GQParserData *parser_data, gpointer data)
1149 {
1150         GQParserFuncData *func = parser_data->parse_func_stack->data;
1151         func->data = data;
1152 }
1153
1154
1155 static void start_element(GMarkupParseContext *context,
1156                           const gchar *element_name,
1157                           const gchar **attribute_names,
1158                           const gchar **attribute_values,
1159                           gpointer user_data,
1160                           GError **error)
1161 {
1162         GQParserData *parser_data = user_data;
1163         GQParserFuncData *func = parser_data->parse_func_stack->data;
1164         DEBUG_2("start %s", element_name);
1165
1166         if (func->start_func)
1167                 func->start_func(parser_data, context, element_name, attribute_names, attribute_values, func->data, error);
1168 }
1169
1170 static void end_element(GMarkupParseContext *context,
1171                           const gchar *element_name,
1172                           gpointer user_data,
1173                           GError **error)
1174 {
1175         GQParserData *parser_data = user_data;
1176         GQParserFuncData *func = parser_data->parse_func_stack->data;
1177         DEBUG_2("end %s", element_name);
1178
1179         if (func->end_func)
1180                 func->end_func(parser_data, context, element_name, func->data, error);
1181
1182         options_parse_func_pop(parser_data);
1183 }
1184
1185 static GMarkupParser parser = {
1186         start_element,
1187         end_element,
1188         NULL,
1189         NULL,
1190         NULL
1191 };
1192
1193 /*
1194  *-----------------------------------------------------------------------------
1195  * load configuration (public)
1196  *-----------------------------------------------------------------------------
1197  */
1198
1199 gboolean load_config_from_buf(const gchar *buf, gsize size, gboolean startup)
1200 {
1201         GMarkupParseContext *context;
1202         gboolean ret = TRUE;
1203         GQParserData *parser_data;
1204
1205         parser_data = g_new0(GQParserData, 1);
1206
1207         parser_data->startup = startup;
1208         options_parse_func_push(parser_data, options_parse_toplevel, NULL, NULL);
1209
1210         context = g_markup_parse_context_new(&parser, 0, parser_data, NULL);
1211
1212         if (g_markup_parse_context_parse(context, buf, size, NULL) == FALSE)
1213                 {
1214                 ret = FALSE;
1215                 DEBUG_1("Parse failed");
1216                 }
1217
1218         g_free(parser_data);
1219
1220         g_markup_parse_context_free(context);
1221         return ret;
1222 }
1223
1224 gboolean load_config_from_file(const gchar *utf8_path, gboolean startup)
1225 {
1226         gsize size;
1227         gchar *buf;
1228         gboolean ret = TRUE;
1229
1230         if (g_file_get_contents(utf8_path, &buf, &size, NULL) == FALSE)
1231                 {
1232                 return FALSE;
1233                 }
1234         ret = load_config_from_buf(buf, size, startup);
1235         g_free(buf);
1236         return ret;
1237 }
1238
1239
1240
1241 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */