Since READ_*() macros are now exported, do not hide
[geeqie.git] / src / rcfile.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include <glib/gstdio.h>
14 #include <errno.h>
15
16 #include "main.h"
17 #include "rcfile.h"
18
19 #include "bar.h"
20 #include "bar_comment.h"
21 #include "bar_exif.h"
22 #include "bar_histogram.h"
23 #include "bar_keywords.h"
24 #include "editors.h"
25 #include "filefilter.h"
26 #include "misc.h"
27 #include "pixbuf-renderer.h"
28 #include "secure_save.h"
29 #include "slideshow.h"
30 #include "ui_fileops.h"
31 #include "layout.h"
32 #include "layout_util.h"
33 #include "bar.h"
34
35
36 /*
37  *-----------------------------------------------------------------------------
38  * line write/parse routines (public)
39  *-----------------------------------------------------------------------------
40  */
41
42 void write_indent(GString *str, gint indent)
43 {
44         g_string_append_printf(str, "%*s", indent * 4, "");
45 }
46
47 void write_char_option(GString *str, gint indent, const gchar *label, const gchar *text)
48 {
49         gchar *escval = g_markup_escape_text(text ? text : "", -1);
50         write_indent(str, indent);
51         g_string_append_printf(str, "%s = \"%s\"\n", label, escval);
52         g_free(escval);
53 }
54
55 gboolean read_char_option(const gchar *option, const gchar *label, const gchar *value, gchar **text)
56 {
57         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
58         if (!text) return FALSE;
59
60         g_free(*text);
61         *text = g_strdup(value);
62         return TRUE;
63 }
64
65 /* Since gdk_color_to_string() is only available since gtk 2.12
66  * here is an equivalent stub function. */
67 static gchar *color_to_string(GdkColor *color)
68 {
69         return g_strdup_printf("#%04X%04X%04X", color->red, color->green, color->blue);
70 }
71
72 void write_color_option(GString *str, gint indent, gchar *label, GdkColor *color)
73 {
74         if (color)
75                 {
76                 gchar *colorstring = color_to_string(color);
77
78                 write_char_option(str, indent, label, colorstring);
79                 g_free(colorstring);
80                 }
81         else
82                 write_char_option(str, indent, label, "");
83 }
84
85 gboolean read_color_option(const gchar *option, const gchar *label, const gchar *value, GdkColor *color)
86 {
87         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
88         if (!color) return FALSE;
89
90         if (!*value) return FALSE;
91         gdk_color_parse(value, color);
92         return TRUE;
93 }
94
95 void write_int_option(GString *str, gint indent, const gchar *label, gint n)
96 {
97         write_indent(str, indent);
98         g_string_append_printf(str, "%s = \"%d\"\n", label, n);
99 }
100
101 gboolean read_int_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
102 {
103         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
104         if (!n) return FALSE;
105
106         if (g_ascii_isdigit(value[0]) || (value[0] == '-' && g_ascii_isdigit(value[1])))
107                 {
108                 *n = strtol(value, NULL, 10);
109                 }
110         else
111                 {
112                 if (g_ascii_strcasecmp(value, "true") == 0)
113                         *n = 1;
114                 else
115                         *n = 0;
116                 }
117
118         return TRUE;
119 }
120
121 void write_uint_option(GString *str, gint indent, const gchar *label, guint n)
122 {
123         write_indent(str, indent);
124         g_string_append_printf(str, "%s = \"%u\"\n", label, n);
125 }
126
127 gboolean read_uint_option(const gchar *option, const gchar *label, const gchar *value, guint *n)
128 {
129         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
130         if (!n) return FALSE;
131
132         if (g_ascii_isdigit(value[0]))
133                 {
134                 *n = strtoul(value, NULL, 10);
135                 }
136         else
137                 {
138                 if (g_ascii_strcasecmp(value, "true") == 0)
139                         *n = 1;
140                 else
141                         *n = 0;
142                 }
143         
144         return TRUE;
145 }
146
147 gboolean read_uint_option_clamp(const gchar *option, const gchar *label, const gchar *value, guint *n, guint min, guint max)
148 {
149         gboolean ret;
150
151         ret = read_uint_option(option, label, value, n);
152         if (ret) *n = CLAMP(*n, min, max);
153
154         return ret;
155 }
156
157
158 gboolean read_int_option_clamp(const gchar *option, const gchar *label, const gchar *value, gint *n, gint min, gint max)
159 {
160         gboolean ret;
161
162         ret = read_int_option(option, label, value, n);
163         if (ret) *n = CLAMP(*n, min, max);
164
165         return ret;
166 }
167
168 void write_int_unit_option(GString *str, gint indent, gchar *label, gint n, gint subunits)
169 {
170         gint l, r;
171
172         if (subunits > 0)
173                 {
174                 l = n / subunits;
175                 r = n % subunits;
176                 }
177         else
178                 {
179                 l = n;
180                 r = 0;
181                 }
182
183         write_indent(str, indent);
184         g_string_append_printf(str, "%s = \"%d.%d\"\n", label, l, r);
185 }
186
187 gboolean read_int_unit_option(const gchar *option, const gchar *label, const gchar *value, gint *n, gint subunits)
188 {
189         gint l, r;
190         gchar *ptr, *buf;
191
192         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
193         if (!n) return FALSE;
194
195         buf = g_strdup(value);
196         ptr = buf;
197         while (*ptr != '\0' && *ptr != '.') ptr++;
198         if (*ptr == '.')
199                 {
200                 *ptr = '\0';
201                 l = strtol(value, NULL, 10);
202                 *ptr = '.';
203                 ptr++;
204                 r = strtol(ptr, NULL, 10);
205                 }
206         else
207                 {
208                 l = strtol(value, NULL, 10);
209                 r = 0;
210                 }
211
212         *n = l * subunits + r;
213         g_free(buf);
214         
215         return TRUE;
216 }
217
218 void write_bool_option(GString *str, gint indent, gchar *label, gint n)
219 {
220         write_indent(str, indent);
221         g_string_append_printf(str, "%s = \"%s\"\n", label, n ? "true" : "false");
222 }
223
224 gboolean read_bool_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
225 {
226         if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
227         if (!n) return FALSE;
228
229         if (g_ascii_strcasecmp(value, "true") == 0 || atoi(value) != 0)
230                 *n = TRUE;
231         else
232                 *n = FALSE;
233
234         return TRUE;
235 }
236
237 /*
238  *-----------------------------------------------------------------------------
239  * write fuctions for elements (private)
240  *-----------------------------------------------------------------------------
241  */
242
243 static void write_global_attributes(GString *outstr, gint indent)
244 {
245 //      WRITE_SUBTITLE("General Options");
246
247         WRITE_BOOL(*options, show_icon_names);
248         WRITE_BOOL(*options, show_copy_path);
249         WRITE_SEPARATOR();
250
251         WRITE_BOOL(*options, tree_descend_subdirs);
252         WRITE_BOOL(*options, lazy_image_sync);
253         WRITE_BOOL(*options, update_on_time_change);
254         WRITE_SEPARATOR();
255
256         WRITE_BOOL(*options, progressive_key_scrolling);
257
258         WRITE_UINT(*options, duplicates_similarity_threshold);
259         WRITE_SEPARATOR();
260
261         WRITE_BOOL(*options, mousewheel_scrolls);
262         WRITE_INT(*options, open_recent_list_maxsize);
263         WRITE_INT(*options, dnd_icon_size);
264         WRITE_BOOL(*options, place_dialogs_under_mouse);
265
266
267 //      WRITE_SUBTITLE("Startup Options");
268
269         WRITE_BOOL(*options, startup.restore_path);
270         WRITE_BOOL(*options, startup.use_last_path);
271         WRITE_CHAR(*options, startup.path);
272
273
274 //      WRITE_SUBTITLE("File operations Options");
275
276         WRITE_BOOL(*options, file_ops.enable_in_place_rename);
277         WRITE_BOOL(*options, file_ops.confirm_delete);
278         WRITE_BOOL(*options, file_ops.enable_delete_key);
279         WRITE_BOOL(*options, file_ops.safe_delete_enable);
280         WRITE_CHAR(*options, file_ops.safe_delete_path);
281         WRITE_INT(*options, file_ops.safe_delete_folder_maxsize);
282
283
284
285
286 //      WRITE_SUBTITLE("Properties dialog Options");
287         WRITE_CHAR(*options, properties.tabs_order);
288
289 //      WRITE_SUBTITLE("Image Options");
290
291         WRITE_UINT(*options, image.zoom_mode);
292
293 //      g_string_append_printf(outstr, "# image.zoom_mode possible values are:\n"
294 //                          "#   original\n"
295 //                          "#   fit\n"
296 //                          "#   dont_change\n");
297 //      g_string_append_printf(outstr, "image.zoom_mode: ");
298 //      switch (options->image.zoom_mode)
299 //      {
300 //      case ZOOM_RESET_ORIGINAL: g_string_append_printf(outstr, "original\n"); break;
301 //      case ZOOM_RESET_FIT_WINDOW: g_string_append_printf(outstr, "fit\n"); break;
302 //      case ZOOM_RESET_NONE: g_string_append_printf(outstr, "dont_change\n"); break;
303 //      }
304         WRITE_SEPARATOR();
305         WRITE_BOOL(*options, image.zoom_2pass);
306         WRITE_BOOL(*options, image.zoom_to_fit_allow_expand);
307         WRITE_UINT(*options, image.zoom_quality);
308         WRITE_INT(*options, image.zoom_increment);
309         WRITE_BOOL(*options, image.fit_window_to_image);
310         WRITE_BOOL(*options, image.limit_window_size);
311         WRITE_INT(*options, image.max_window_size);
312         WRITE_BOOL(*options, image.limit_autofit_size);
313         WRITE_INT(*options, image.max_autofit_size);
314         WRITE_UINT(*options, image.scroll_reset_method);
315         WRITE_INT(*options, image.tile_cache_max);
316         WRITE_INT(*options, image.image_cache_max);
317         WRITE_UINT(*options, image.dither_quality);
318         WRITE_BOOL(*options, image.enable_read_ahead);
319         WRITE_BOOL(*options, image.exif_rotate_enable);
320         WRITE_BOOL(*options, image.use_custom_border_color);
321         WRITE_COLOR(*options, image.border_color);
322         WRITE_INT(*options, image.read_buffer_size);
323         WRITE_INT(*options, image.idle_read_loop_count);
324
325 //      WRITE_SUBTITLE("Thumbnails Options");
326
327         WRITE_INT(*options, thumbnails.max_width);
328         WRITE_INT(*options, thumbnails.max_height);
329         WRITE_BOOL(*options, thumbnails.enable_caching);
330         WRITE_BOOL(*options, thumbnails.cache_into_dirs);
331         WRITE_BOOL(*options, thumbnails.fast);
332         WRITE_BOOL(*options, thumbnails.use_xvpics);
333         WRITE_BOOL(*options, thumbnails.spec_standard);
334         WRITE_UINT(*options, thumbnails.quality);
335         WRITE_BOOL(*options, thumbnails.use_exif);
336
337
338 //      WRITE_SUBTITLE("File sorting Options");
339
340         WRITE_INT(*options, file_sort.method);
341         WRITE_BOOL(*options, file_sort.ascending);
342         WRITE_BOOL(*options, file_sort.case_sensitive);
343
344
345 //      WRITE_SUBTITLE("Fullscreen Options");
346
347         WRITE_INT(*options, fullscreen.screen);
348         WRITE_BOOL(*options, fullscreen.clean_flip);
349         WRITE_BOOL(*options, fullscreen.disable_saver);
350         WRITE_BOOL(*options, fullscreen.above);
351
352
353 //      WRITE_SUBTITLE("Histogram Options");
354         WRITE_UINT(*options, histogram.last_channel_mode);
355         WRITE_UINT(*options, histogram.last_log_mode);
356
357
358 //      WRITE_SUBTITLE("Image Overlay Options");
359         WRITE_UINT(*options, image_overlay.common.state);
360         WRITE_BOOL(*options, image_overlay.common.show_at_startup);
361         WRITE_CHAR(*options, image_overlay.common.template_string);
362         WRITE_SEPARATOR();
363
364 //      g_string_append_printf(outstr, "# these are relative positions:\n");
365 //      g_string_append_printf(outstr, "# x >= 0: |x| pixels from left border\n");
366 //      g_string_append_printf(outstr, "# x < 0 : |x| pixels from right border\n");
367 //      g_string_append_printf(outstr, "# y >= 0: |y| pixels from top border\n");
368 //      g_string_append_printf(outstr, "# y < 0 : |y| pixels from bottom border\n");
369         WRITE_INT(*options, image_overlay.common.x);
370         WRITE_INT(*options, image_overlay.common.y);
371
372
373 //      WRITE_SUBTITLE("Slideshow Options");
374
375         WRITE_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION);
376         WRITE_BOOL(*options, slideshow.random);
377         WRITE_BOOL(*options, slideshow.repeat);
378
379
380 //      WRITE_SUBTITLE("Collection Options");
381
382         WRITE_BOOL(*options, collections.rectangular_selection);
383
384
385 //      WRITE_SUBTITLE("Filtering Options");
386
387         WRITE_BOOL(*options, file_filter.show_hidden_files);
388         WRITE_BOOL(*options, file_filter.show_dot_directory);
389         WRITE_BOOL(*options, file_filter.disable);
390         WRITE_SEPARATOR();
391
392
393 //      WRITE_SUBTITLE("Sidecars Options");
394
395         WRITE_CHAR(*options, sidecar.ext);
396
397
398
399 //      WRITE_SUBTITLE("Shell command");
400         WRITE_CHAR(*options, shell.path);
401         WRITE_CHAR(*options, shell.options);
402
403
404 //      WRITE_SUBTITLE("Helpers");
405 //      g_string_append_printf(outstr, "# Html browser\n");
406 //      g_string_append_printf(outstr, "# command_name is: the binary's name to look for in the path\n");
407 //      g_string_append_printf(outstr, "# If command_name is empty, the program will try various common html browsers\n");
408 //      g_string_append_printf(outstr, "# command_line is:\n");
409 //      g_string_append_printf(outstr, "# \"\" (empty string)  = execute binary with html file path as command line\n");
410 //      g_string_append_printf(outstr, "# \"string\"           = execute string and use results for command line\n");
411 //      g_string_append_printf(outstr, "# \"!string\"          = use text following ! as command line, replacing optional %%s with html file path\n");
412         WRITE_CHAR(*options, helpers.html_browser.command_name);
413         WRITE_CHAR(*options, helpers.html_browser.command_line);
414
415 /* FIXME:
416         WRITE_SUBTITLE("Exif Options");
417         g_string_append_printf(outstr, "# Display: 0: never\n"
418                             "#          1: if set\n"
419                             "#          2: always\n\n");
420         for (i = 0; ExifUIList[i].key; i++)
421                 {
422                 g_string_append_printf(outstr, "exif.display.");
423                 write_int_option(outstr, 2, (gchar *)ExifUIList[i].key, ExifUIList[i].current);
424                 }
425 */
426
427 //      WRITE_SUBTITLE("Metadata Options");
428         WRITE_BOOL(*options, metadata.enable_metadata_dirs);
429         WRITE_BOOL(*options, metadata.save_in_image_file); 
430         WRITE_BOOL(*options, metadata.save_legacy_IPTC);
431         WRITE_BOOL(*options, metadata.warn_on_write_problems);
432         WRITE_BOOL(*options, metadata.save_legacy_format);
433         WRITE_BOOL(*options, metadata.sync_grouped_files);
434         WRITE_BOOL(*options, metadata.confirm_write);
435         WRITE_INT(*options, metadata.confirm_timeout);
436         WRITE_BOOL(*options, metadata.confirm_after_timeout);
437         WRITE_BOOL(*options, metadata.confirm_on_image_change);
438         WRITE_BOOL(*options, metadata.confirm_on_dir_change);
439
440 }
441
442 static void write_color_profile(GString *outstr, gint indent)
443 {
444         gint i;
445 #ifndef HAVE_LCMS
446         g_string_append_printf(outstr, "<!-- NOTICE: %s was not built with support for color profiles,\n"
447                             "         color profile options will have no effect.\n-->\n", GQ_APPNAME);
448 #endif
449
450         WRITE_STRING("<color_profiles\n");
451         indent++;
452         WRITE_INT(options->color_profile, screen_type);
453         WRITE_CHAR(options->color_profile, screen_file);
454         WRITE_BOOL(options->color_profile, enabled);
455         WRITE_BOOL(options->color_profile, use_image);
456         WRITE_INT(options->color_profile, input_type);
457         indent--;
458         WRITE_STRING(">\n");
459
460         indent++;
461         for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
462                 {
463                 WRITE_STRING("<profile\n");
464                 indent++;
465                 write_char_option(outstr, indent, "input_file", options->color_profile.input_file[i]);
466                 write_char_option(outstr, indent, "input_name", options->color_profile.input_name[i]);
467                 indent--;
468                 WRITE_STRING("/>\n");
469                 }
470         indent--;
471         WRITE_STRING("</color_profiles>\n");
472 }
473
474
475 /*
476  *-----------------------------------------------------------------------------
477  * save configuration (public)
478  *-----------------------------------------------------------------------------
479  */
480
481 gboolean save_options_to(const gchar *utf8_path, ConfOptions *options)
482 {
483         SecureSaveInfo *ssi;
484         gchar *rc_pathl;
485         GString *outstr;
486         gint indent = 0;
487         GList *work;
488         
489         rc_pathl = path_from_utf8(utf8_path);
490         ssi = secure_open(rc_pathl);
491         g_free(rc_pathl);
492         if (!ssi)
493                 {
494                 log_printf(_("error saving config file: %s\n"), utf8_path);
495                 return FALSE;
496                 }
497
498         outstr = g_string_new("");
499         g_string_append_printf(outstr, "<!--\n");
500         g_string_append_printf(outstr, "######################################################################\n");
501         g_string_append_printf(outstr, "# %30s config file      version %-10s #\n", GQ_APPNAME, VERSION);
502         g_string_append_printf(outstr, "######################################################################\n");
503         WRITE_SEPARATOR();
504
505         WRITE_STRING("# Note: This file is autogenerated. Options can be changed here,\n");
506         WRITE_STRING("#       but user comments and formatting will be lost.\n");
507         WRITE_SEPARATOR();
508         WRITE_STRING("-->\n");
509         WRITE_SEPARATOR();
510         WRITE_STRING("<global\n");
511         write_global_attributes(outstr, indent + 1);
512         WRITE_STRING(">\n");
513
514         indent++;
515
516         write_color_profile(outstr, indent);
517
518         WRITE_SEPARATOR();
519         filter_write_list(outstr, indent);
520
521         WRITE_SEPARATOR();
522         WRITE_SUBTITLE("Layout Options - defaults");
523         WRITE_STRING("<layout\n");
524         layout_write_attributes(&options->layout, outstr, indent + 1);
525         WRITE_STRING("/>\n");
526
527         indent--;
528         WRITE_STRING("</global>\n");
529
530         WRITE_SEPARATOR();
531         WRITE_SUBTITLE("Layout Options");
532
533         work = layout_window_list;
534         while (work)
535                 {
536                 LayoutWindow *lw = work->data;
537                 layout_write_config(lw, outstr, indent);
538                 work = work->next;
539
540                 }
541
542
543
544         secure_fputs(ssi, outstr->str);
545         g_string_free(outstr, TRUE);
546
547         if (secure_close(ssi))
548                 {
549                 log_printf(_("error saving config file: %s\nerror: %s\n"), utf8_path,
550                            secsave_strerror(secsave_errno));
551                 return FALSE;
552                 }
553
554         return TRUE;
555 }
556
557 /*
558  *-----------------------------------------------------------------------------
559  * loading attributes for elements (private)
560  *-----------------------------------------------------------------------------
561  */
562
563
564 static gboolean load_global_params(const gchar **attribute_names, const gchar **attribute_values)
565 {
566         while (*attribute_names)
567                 {
568                 const gchar *option = *attribute_names++;
569                 const gchar *value = *attribute_values++;
570
571
572                 /* general options */
573                 if (READ_BOOL(*options, show_icon_names)) continue;
574                 if (READ_BOOL(*options, show_copy_path)) continue;
575
576                 if (READ_BOOL(*options, tree_descend_subdirs)) continue;
577                 if (READ_BOOL(*options, lazy_image_sync)) continue;
578                 if (READ_BOOL(*options, update_on_time_change)) continue;
579
580                 if (READ_UINT_CLAMP(*options, duplicates_similarity_threshold, 0, 100)) continue;
581
582                 if (READ_BOOL(*options, progressive_key_scrolling)) continue;
583
584                 if (READ_BOOL(*options, mousewheel_scrolls)) continue;
585
586                 if (READ_INT(*options, open_recent_list_maxsize)) continue;
587                 if (READ_INT(*options, dnd_icon_size)) continue;
588                 if (READ_BOOL(*options, place_dialogs_under_mouse)) continue;
589
590                 /* startup options */
591                 
592                 if (READ_BOOL(*options, startup.restore_path)) continue;
593
594                 if (READ_BOOL(*options, startup.use_last_path)) continue;
595
596                 if (READ_CHAR(*options, startup.path)) continue;
597         
598
599                 /* properties dialog options */
600                 if (READ_CHAR(*options, properties.tabs_order)) continue;
601
602                 /* image options */
603                 if (READ_UINT_CLAMP(*options, image.zoom_mode, 0, ZOOM_RESET_NONE)) continue;
604                 if (READ_BOOL(*options, image.zoom_2pass)) continue;
605                 if (READ_BOOL(*options, image.zoom_to_fit_allow_expand)) continue;
606                 if (READ_BOOL(*options, image.fit_window_to_image)) continue;
607                 if (READ_BOOL(*options, image.limit_window_size)) continue;
608                 if (READ_INT(*options, image.max_window_size)) continue;
609                 if (READ_BOOL(*options, image.limit_autofit_size)) continue;
610                 if (READ_INT(*options, image.max_autofit_size)) continue;
611                 if (READ_UINT_CLAMP(*options, image.scroll_reset_method, 0, PR_SCROLL_RESET_COUNT - 1)) continue;
612                 if (READ_INT(*options, image.tile_cache_max)) continue;
613                 if (READ_INT(*options, image.image_cache_max)) continue;
614                 if (READ_UINT_CLAMP(*options, image.zoom_quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER)) continue;
615                 if (READ_UINT_CLAMP(*options, image.dither_quality, GDK_RGB_DITHER_NONE, GDK_RGB_DITHER_MAX)) continue;
616                 if (READ_INT(*options, image.zoom_increment)) continue;
617                 if (READ_BOOL(*options, image.enable_read_ahead)) continue;
618                 if (READ_BOOL(*options, image.exif_rotate_enable)) continue;
619                 if (READ_BOOL(*options, image.use_custom_border_color)) continue;
620                 if (READ_COLOR(*options, image.border_color)) continue;
621                 if (READ_INT_CLAMP(*options, image.read_buffer_size, IMAGE_LOADER_READ_BUFFER_SIZE_MIN, IMAGE_LOADER_READ_BUFFER_SIZE_MAX)) continue;
622                 if (READ_INT_CLAMP(*options, image.idle_read_loop_count, IMAGE_LOADER_IDLE_READ_LOOP_COUNT_MIN, IMAGE_LOADER_IDLE_READ_LOOP_COUNT_MAX)) continue;
623
624
625                 /* thumbnails options */
626                 if (READ_INT_CLAMP(*options, thumbnails.max_width, 16, 512)) continue;
627                 if (READ_INT_CLAMP(*options, thumbnails.max_height, 16, 512)) continue;
628
629                 if (READ_BOOL(*options, thumbnails.enable_caching)) continue;
630                 if (READ_BOOL(*options, thumbnails.cache_into_dirs)) continue;
631                 if (READ_BOOL(*options, thumbnails.fast)) continue;
632                 if (READ_BOOL(*options, thumbnails.use_xvpics)) continue;
633                 if (READ_BOOL(*options, thumbnails.spec_standard)) continue;
634                 if (READ_UINT_CLAMP(*options, thumbnails.quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER)) continue;
635                 if (READ_BOOL(*options, thumbnails.use_exif)) continue;
636
637                 /* file sorting options */
638                 if (READ_UINT(*options, file_sort.method)) continue;
639                 if (READ_BOOL(*options, file_sort.ascending)) continue;
640                 if (READ_BOOL(*options, file_sort.case_sensitive)) continue;
641
642                 /* file operations *options */
643                 if (READ_BOOL(*options, file_ops.enable_in_place_rename)) continue;
644                 if (READ_BOOL(*options, file_ops.confirm_delete)) continue;
645                 if (READ_BOOL(*options, file_ops.enable_delete_key)) continue;
646                 if (READ_BOOL(*options, file_ops.safe_delete_enable)) continue;
647                 if (READ_CHAR(*options, file_ops.safe_delete_path)) continue;
648                 if (READ_INT(*options, file_ops.safe_delete_folder_maxsize)) continue;
649
650                 /* fullscreen options */
651                 if (READ_INT(*options, fullscreen.screen)) continue;
652                 if (READ_BOOL(*options, fullscreen.clean_flip)) continue;
653                 if (READ_BOOL(*options, fullscreen.disable_saver)) continue;
654                 if (READ_BOOL(*options, fullscreen.above)) continue;
655
656                 /* histogram */
657                 if (READ_UINT(*options, histogram.last_channel_mode)) continue;
658                 if (READ_UINT(*options, histogram.last_log_mode)) continue;
659
660                 /* image overlay */
661                 if (READ_UINT(*options, image_overlay.common.state)) continue;
662                 if (READ_BOOL(*options, image_overlay.common.show_at_startup)) continue;
663                 if (READ_CHAR(*options, image_overlay.common.template_string)) continue;
664
665                 if (READ_INT(*options, image_overlay.common.x)) continue;
666                 if (READ_INT(*options, image_overlay.common.y)) continue;
667
668
669                 /* slideshow options */
670                 if (READ_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION)) continue;
671                 if (READ_BOOL(*options, slideshow.random)) continue;
672                 if (READ_BOOL(*options, slideshow.repeat)) continue;
673
674                 /* collection options */
675
676                 if (READ_BOOL(*options, collections.rectangular_selection)) continue;
677
678                 /* filtering options */
679
680                 if (READ_BOOL(*options, file_filter.show_hidden_files)) continue;
681                 if (READ_BOOL(*options, file_filter.show_dot_directory)) continue;
682                 if (READ_BOOL(*options, file_filter.disable)) continue;
683                 if (READ_CHAR(*options, sidecar.ext)) continue;
684
685                 /* Color Profiles */
686
687                 /* Shell command */
688                 if (READ_CHAR(*options, shell.path)) continue;
689                 if (READ_CHAR(*options, shell.options)) continue;
690
691                 /* Helpers */
692                 if (READ_CHAR(*options, helpers.html_browser.command_name)) continue;
693                 if (READ_CHAR(*options, helpers.html_browser.command_line)) continue;
694                 /* Exif */
695 /*
696                 if (0 == g_ascii_strncasecmp(option, "exif.display.", 13))
697                         {
698                         for (i = 0; ExifUIList[i].key; i++)
699                                 if (0 == g_ascii_strcasecmp(option + 13, ExifUIList[i].key))
700                                         ExifUIList[i].current = strtol(value, NULL, 10);
701                         continue;
702                         }
703 */
704                 /* metadata */          
705                 if (READ_BOOL(*options, metadata.enable_metadata_dirs)) continue;
706                 if (READ_BOOL(*options, metadata.save_in_image_file)) continue;
707                 if (READ_BOOL(*options, metadata.save_legacy_IPTC)) continue;
708                 if (READ_BOOL(*options, metadata.warn_on_write_problems)) continue;
709                 if (READ_BOOL(*options, metadata.save_legacy_format)) continue;
710                 if (READ_BOOL(*options, metadata.sync_grouped_files)) continue;
711                 if (READ_BOOL(*options, metadata.confirm_write)) continue;
712                 if (READ_BOOL(*options, metadata.confirm_after_timeout)) continue;
713                 if (READ_INT(*options, metadata.confirm_timeout)) continue;
714                 if (READ_BOOL(*options, metadata.confirm_on_image_change)) continue;
715                 if (READ_BOOL(*options, metadata.confirm_on_dir_change)) continue;
716
717                 DEBUG_1("unknown attribute %s = %s", option, value);
718                 }
719
720         return TRUE;
721 }
722
723 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)
724 {
725         while (*attribute_names)
726                 {
727                 const gchar *option = *attribute_names++;
728                 const gchar *value = *attribute_values++;
729
730
731                 if (READ_BOOL(options->color_profile, enabled)) continue;
732                 if (READ_BOOL(options->color_profile, use_image)) continue;
733                 if (READ_INT(options->color_profile, input_type)) continue;
734                 if (READ_INT(options->color_profile, screen_type)) continue;
735                 if (READ_CHAR(options->color_profile, screen_file)) continue;
736
737                 DEBUG_1("unknown attribute %s = %s", option, value);
738                 }
739
740 }
741
742 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)
743 {
744         gint i = GPOINTER_TO_INT(data);
745         if (i < 0 || i >= COLOR_PROFILE_INPUTS) return;
746         while (*attribute_names)
747                 {
748                 const gchar *option = *attribute_names++;
749                 const gchar *value = *attribute_values++;
750
751                 if (READ_CHAR_FULL("input_file", options->color_profile.input_file[i])) continue;
752                 if (READ_CHAR_FULL("input_name", options->color_profile.input_name[i])) continue;
753                 
754
755                 DEBUG_1("unknown attribute %s = %s", option, value);
756                 }
757         i++;
758         options_parse_func_set_data(parser_data, GINT_TO_POINTER(i));
759
760 }
761
762
763
764 /*
765  *-----------------------------------------------------------------------------
766  * xml file structure (private)
767  *-----------------------------------------------------------------------------
768  */
769 struct _GQParserData
770 {
771         GList *parse_func_stack;
772         gboolean startup; /* reading config for the first time - add commandline and call init_after_global_options() */
773         gboolean global_found;
774 };
775
776
777 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)
778 {
779         DEBUG_1("unexpected: %s", element_name);
780         options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
781 }
782
783 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)
784 {
785         if (g_ascii_strcasecmp(element_name, "profile") == 0)
786                 {
787                 options_load_profile(parser_data, context, element_name, attribute_names, attribute_values, data, error);
788                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
789                 }
790         else
791                 {
792                 DEBUG_1("unexpected profile: %s", element_name);
793                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
794                 }
795 }
796
797 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)
798 {
799         if (g_ascii_strcasecmp(element_name, "file_type") == 0)
800                 {
801                 filter_load_file_type(attribute_names, attribute_values);
802                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
803                 }
804         else
805                 {
806                 DEBUG_1("unexpected filter: %s", element_name);
807                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
808                 }
809 }
810
811 static void options_parse_filter_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
812 {
813         DEBUG_1(" filter end");
814         filter_add_defaults();
815         filter_rebuild();
816 }
817
818 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)
819 {
820         if (g_ascii_strcasecmp(element_name, "color_profiles") == 0)
821                 {
822                 options_load_color_profiles(parser_data, context, element_name, attribute_names, attribute_values, data, error);
823                 options_parse_func_push(parser_data, options_parse_color_profiles, NULL, GINT_TO_POINTER(0));
824                 }
825         else if (g_ascii_strcasecmp(element_name, "filter") == 0)
826                 {
827                 options_parse_func_push(parser_data, options_parse_filter, options_parse_filter_end, NULL);
828                 }
829         else if (g_ascii_strcasecmp(element_name, "layout") == 0)
830                 {
831                 layout_load_attributes(&options->layout, attribute_names, attribute_values);
832                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
833                 }
834         else
835                 {
836                 DEBUG_1("unexpected global: %s", element_name);
837                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
838                 }
839 }
840
841 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)
842 {
843         GtkWidget *bar = data;
844         if (g_ascii_strcasecmp(element_name, "pane_comment") == 0)
845                 {
846                 GtkWidget *pane = bar_pane_comment_new_from_config(attribute_names, attribute_values);
847                 bar_add(bar, pane);
848                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
849                 }
850         else if (g_ascii_strcasecmp(element_name, "pane_exif") == 0)
851                 {
852                 GtkWidget *pane = bar_pane_exif_new_from_config(attribute_names, attribute_values);
853                 bar_add(bar, pane);
854                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
855                 }
856         else if (g_ascii_strcasecmp(element_name, "pane_histogram") == 0)
857                 {
858                 GtkWidget *pane = bar_pane_histogram_new_from_config(attribute_names, attribute_values);
859                 bar_add(bar, pane);
860                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
861                 }
862         else if (g_ascii_strcasecmp(element_name, "pane_keywords") == 0)
863                 {
864                 GtkWidget *pane = bar_pane_keywords_new_from_config(attribute_names, attribute_values);
865                 bar_add(bar, pane);
866                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
867                 }
868         else
869                 {
870                 DEBUG_1("unexpected in <bar>: <%s>", element_name);
871                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
872                 }
873 }
874
875 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)
876 {
877         LayoutWindow *lw = data;
878         if (g_ascii_strcasecmp(element_name, "bar") == 0)
879                 {
880                 if (lw->bar) 
881                         layout_bar_close(lw);
882                 layout_bar_new(lw, FALSE);
883                 options_parse_func_push(parser_data, options_parse_bar, NULL, lw->bar);
884                 }
885         else
886                 {
887                 DEBUG_1("unexpected in <layout>: <%s>", element_name);
888                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
889                 }
890 }
891
892 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)
893 {
894         if (g_ascii_strcasecmp(element_name, "global") == 0)
895                 {
896                 load_global_params(attribute_names, attribute_values);
897                 options_parse_func_push(parser_data, options_parse_global, NULL, NULL);
898                 return;
899                 }
900         
901         if (parser_data->startup && !parser_data->global_found)
902                 {
903                 DEBUG_1(" global end");
904                 parser_data->global_found = TRUE;
905                 init_after_global_options();
906                 }
907         
908         if (g_ascii_strcasecmp(element_name, "layout") == 0)
909                 {
910                 LayoutWindow *lw;
911                 lw = layout_new_from_config(attribute_names, attribute_values, parser_data->startup);
912                 options_parse_func_push(parser_data, options_parse_layout, NULL, lw);
913                 }
914         else
915                 {
916                 DEBUG_1("unexpected in <toplevel>: <%s>", element_name);
917                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
918                 }
919 }
920
921
922
923
924
925 /*
926  *-----------------------------------------------------------------------------
927  * parser
928  *-----------------------------------------------------------------------------
929  */
930
931
932 struct _GQParserFuncData
933 {
934         GQParserStartFunc start_func;
935         GQParserEndFunc end_func;
936 //      GQParserTextFunc text_func;
937         gpointer data;
938 };
939
940 void options_parse_func_push(GQParserData *parser_data, GQParserStartFunc start_func, GQParserEndFunc end_func, gpointer data)
941 {
942         GQParserFuncData *func_data = g_new0(GQParserFuncData, 1);
943         func_data->start_func = start_func;
944         func_data->end_func = end_func;
945         func_data->data = data;
946         
947         parser_data->parse_func_stack = g_list_prepend(parser_data->parse_func_stack, func_data);
948 }
949
950 void options_parse_func_pop(GQParserData *parser_data)
951 {
952         g_free(parser_data->parse_func_stack->data);
953         parser_data->parse_func_stack = g_list_delete_link(parser_data->parse_func_stack, parser_data->parse_func_stack);
954 }
955
956 void options_parse_func_set_data(GQParserData *parser_data, gpointer data)
957 {
958         GQParserFuncData *func = parser_data->parse_func_stack->data;
959         func->data = data;
960 }
961
962
963 static void start_element(GMarkupParseContext *context,
964                           const gchar *element_name,
965                           const gchar **attribute_names,
966                           const gchar **attribute_values,
967                           gpointer user_data,
968                           GError **error) 
969 {
970         GQParserData *parser_data = user_data;
971         GQParserFuncData *func = parser_data->parse_func_stack->data; 
972         DEBUG_1("start %s", element_name);
973         
974         if (func->start_func)
975                 func->start_func(parser_data, context, element_name, attribute_names, attribute_values, func->data, error);
976 }
977
978 static void end_element(GMarkupParseContext *context,
979                           const gchar *element_name,
980                           gpointer user_data,
981                           GError **error) 
982 {
983         GQParserData *parser_data = user_data;
984         GQParserFuncData *func = parser_data->parse_func_stack->data; 
985         DEBUG_1("end %s", element_name);
986
987         if (func->end_func)
988                 func->end_func(parser_data, context, element_name, func->data, error);
989
990         options_parse_func_pop(parser_data);
991 }
992
993 static GMarkupParser parser = {
994         start_element,
995         end_element,
996         NULL,
997         NULL,
998         NULL
999 };
1000
1001 /*
1002  *-----------------------------------------------------------------------------
1003  * load configuration (public)
1004  *-----------------------------------------------------------------------------
1005  */
1006
1007 gboolean load_options_from(const gchar *utf8_path, ConfOptions *options, gboolean startup)
1008 {
1009         gsize size;
1010         gchar *buf;
1011         GMarkupParseContext *context;
1012         gboolean ret = TRUE;
1013         GQParserData *parser_data;
1014
1015         if (g_file_get_contents (utf8_path, &buf, &size, NULL) == FALSE) 
1016                 {
1017                 return FALSE;
1018                 }
1019         
1020         parser_data = g_new0(GQParserData, 1);
1021         
1022         parser_data->startup = startup;
1023         options_parse_func_push(parser_data, options_parse_toplevel, NULL, NULL);
1024         
1025         context = g_markup_parse_context_new(&parser, 0, parser_data, NULL);
1026
1027         if (g_markup_parse_context_parse (context, buf, size, NULL) == FALSE)
1028                 {
1029                 ret = FALSE;
1030                 DEBUG_1("Parse failed");
1031                 }
1032                 
1033         g_free(parser_data);
1034
1035         g_free(buf);
1036         g_markup_parse_context_free (context);
1037         return ret;
1038 }
1039         
1040
1041
1042 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */