enabled commandline again
[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_indent(outstr, indent);
451         g_string_append_printf(outstr, "<color_profiles\n");
452         indent++;
453         WRITE_INT(options->color_profile, screen_type);
454         WRITE_CHAR(options->color_profile, screen_file);
455         WRITE_BOOL(options->color_profile, enabled);
456         WRITE_BOOL(options->color_profile, use_image);
457         WRITE_INT(options->color_profile, input_type);
458         indent--;
459         write_indent(outstr, indent);
460         g_string_append_printf(outstr, ">\n");
461
462         indent++;
463         for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
464                 {
465                 write_indent(outstr, indent);
466                 g_string_append_printf(outstr, "<profile\n");
467                 indent++;
468                 write_char_option(outstr, indent, "input_file", options->color_profile.input_file[i]);
469                 write_char_option(outstr, indent, "input_name", options->color_profile.input_name[i]);
470                 indent--;
471                 write_indent(outstr, indent);
472                 g_string_append_printf(outstr, "/>\n");
473                 }
474         indent--;
475         write_indent(outstr, indent);
476         g_string_append_printf(outstr, "</color_profiles>\n");
477 }
478
479
480 /*
481  *-----------------------------------------------------------------------------
482  * save configuration (public)
483  *-----------------------------------------------------------------------------
484  */
485
486 gboolean save_options_to(const gchar *utf8_path, ConfOptions *options)
487 {
488         SecureSaveInfo *ssi;
489         gchar *rc_pathl;
490         GString *outstr;
491         gint indent = 0;
492         GList *work;
493         
494         rc_pathl = path_from_utf8(utf8_path);
495         ssi = secure_open(rc_pathl);
496         g_free(rc_pathl);
497         if (!ssi)
498                 {
499                 log_printf(_("error saving config file: %s\n"), utf8_path);
500                 return FALSE;
501                 }
502
503         outstr = g_string_new("");
504         g_string_append_printf(outstr, "<!--\n");
505         g_string_append_printf(outstr, "######################################################################\n");
506         g_string_append_printf(outstr, "# %30s config file      version %-10s #\n", GQ_APPNAME, VERSION);
507         g_string_append_printf(outstr, "######################################################################\n");
508         WRITE_SEPARATOR();
509
510         g_string_append_printf(outstr, "# Note: This file is autogenerated. Options can be changed here,\n");
511         g_string_append_printf(outstr, "#       but user comments and formatting will be lost.\n");
512         WRITE_SEPARATOR();
513         g_string_append_printf(outstr, "-->\n");
514         WRITE_SEPARATOR();
515         g_string_append_printf(outstr, "<global\n");
516         indent++;
517         write_global_attributes(outstr, indent);
518         write_indent(outstr, indent);
519         g_string_append_printf(outstr, ">\n");
520
521         write_color_profile(outstr, indent);
522
523         WRITE_SEPARATOR();
524         filter_write_list(outstr, indent);
525
526         WRITE_SEPARATOR();
527         WRITE_SUBTITLE("Layout Options - defaults");
528         write_indent(outstr, indent);
529         g_string_append_printf(outstr, "<layout\n");
530         layout_write_attributes(&options->layout, outstr, indent + 1);
531         write_indent(outstr, indent);
532         g_string_append_printf(outstr, "/>\n");
533
534         indent--;
535         g_string_append_printf(outstr, "</global>\n");
536
537         WRITE_SEPARATOR();
538         WRITE_SUBTITLE("Layout Options");
539
540         work = layout_window_list;
541         while (work)
542                 {
543                 LayoutWindow *lw = work->data;
544                 layout_write_config(lw, outstr, indent);
545                 work = work->next;
546
547                 }
548
549
550
551         secure_fputs(ssi, outstr->str);
552         g_string_free(outstr, TRUE);
553
554         if (secure_close(ssi))
555                 {
556                 log_printf(_("error saving config file: %s\nerror: %s\n"), utf8_path,
557                            secsave_strerror(secsave_errno));
558                 return FALSE;
559                 }
560
561         return TRUE;
562 }
563
564 /*
565  *-----------------------------------------------------------------------------
566  * loading attributes for elements (private)
567  *-----------------------------------------------------------------------------
568  */
569
570
571 static gboolean load_global_params(const gchar **attribute_names, const gchar **attribute_values)
572 {
573         while (*attribute_names)
574                 {
575                 const gchar *option = *attribute_names++;
576                 const gchar *value = *attribute_values++;
577
578
579                 /* general options */
580                 READ_BOOL(*options, show_icon_names);
581                 READ_BOOL(*options, show_copy_path);
582
583                 READ_BOOL(*options, tree_descend_subdirs);
584                 READ_BOOL(*options, lazy_image_sync);
585                 READ_BOOL(*options, update_on_time_change);
586
587                 READ_UINT_CLAMP(*options, duplicates_similarity_threshold, 0, 100);
588
589                 READ_BOOL(*options, progressive_key_scrolling);
590
591                 READ_BOOL(*options, mousewheel_scrolls);
592
593                 READ_INT(*options, open_recent_list_maxsize);
594                 READ_INT(*options, dnd_icon_size);
595                 READ_BOOL(*options, place_dialogs_under_mouse);
596
597                 /* startup options */
598                 
599                 READ_BOOL(*options, startup.restore_path);
600
601                 READ_BOOL(*options, startup.use_last_path);
602
603                 READ_CHAR(*options, startup.path);
604         
605
606                 /* properties dialog options */
607                 READ_CHAR(*options, properties.tabs_order);
608
609                 /* image options */
610                 READ_UINT_CLAMP(*options, image.zoom_mode, 0, ZOOM_RESET_NONE);
611                 READ_BOOL(*options, image.zoom_2pass);
612                 READ_BOOL(*options, image.zoom_to_fit_allow_expand);
613                 READ_BOOL(*options, image.fit_window_to_image);
614                 READ_BOOL(*options, image.limit_window_size);
615                 READ_INT(*options, image.max_window_size);
616                 READ_BOOL(*options, image.limit_autofit_size);
617                 READ_INT(*options, image.max_autofit_size);
618                 READ_UINT_CLAMP(*options, image.scroll_reset_method, 0, PR_SCROLL_RESET_COUNT - 1);
619                 READ_INT(*options, image.tile_cache_max);
620                 READ_INT(*options, image.image_cache_max);
621                 READ_UINT_CLAMP(*options, image.zoom_quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER);
622                 READ_UINT_CLAMP(*options, image.dither_quality, GDK_RGB_DITHER_NONE, GDK_RGB_DITHER_MAX);
623                 READ_INT(*options, image.zoom_increment);
624                 READ_BOOL(*options, image.enable_read_ahead);
625                 READ_BOOL(*options, image.exif_rotate_enable);
626                 READ_BOOL(*options, image.use_custom_border_color);
627                 READ_COLOR(*options, image.border_color);
628                 READ_INT_CLAMP(*options, image.read_buffer_size, IMAGE_LOADER_READ_BUFFER_SIZE_MIN, IMAGE_LOADER_READ_BUFFER_SIZE_MAX);
629                 READ_INT_CLAMP(*options, image.idle_read_loop_count, IMAGE_LOADER_IDLE_READ_LOOP_COUNT_MIN, IMAGE_LOADER_IDLE_READ_LOOP_COUNT_MAX);
630
631
632                 /* thumbnails options */
633                 READ_INT_CLAMP(*options, thumbnails.max_width, 16, 512);
634                 READ_INT_CLAMP(*options, thumbnails.max_height, 16, 512);
635
636                 READ_BOOL(*options, thumbnails.enable_caching);
637                 READ_BOOL(*options, thumbnails.cache_into_dirs);
638                 READ_BOOL(*options, thumbnails.fast);
639                 READ_BOOL(*options, thumbnails.use_xvpics);
640                 READ_BOOL(*options, thumbnails.spec_standard);
641                 READ_UINT_CLAMP(*options, thumbnails.quality, GDK_INTERP_NEAREST, GDK_INTERP_HYPER);
642                 READ_BOOL(*options, thumbnails.use_exif);
643
644                 /* file sorting options */
645                 READ_UINT(*options, file_sort.method);
646                 READ_BOOL(*options, file_sort.ascending);
647                 READ_BOOL(*options, file_sort.case_sensitive);
648
649                 /* file operations *options */
650                 READ_BOOL(*options, file_ops.enable_in_place_rename);
651                 READ_BOOL(*options, file_ops.confirm_delete);
652                 READ_BOOL(*options, file_ops.enable_delete_key);
653                 READ_BOOL(*options, file_ops.safe_delete_enable);
654                 READ_CHAR(*options, file_ops.safe_delete_path);
655                 READ_INT(*options, file_ops.safe_delete_folder_maxsize);
656
657                 /* fullscreen options */
658                 READ_INT(*options, fullscreen.screen);
659                 READ_BOOL(*options, fullscreen.clean_flip);
660                 READ_BOOL(*options, fullscreen.disable_saver);
661                 READ_BOOL(*options, fullscreen.above);
662
663                 /* histogram */
664                 READ_UINT(*options, histogram.last_channel_mode);
665                 READ_UINT(*options, histogram.last_log_mode);
666
667                 /* image overlay */
668                 READ_UINT(*options, image_overlay.common.state);
669                 READ_BOOL(*options, image_overlay.common.show_at_startup);
670                 READ_CHAR(*options, image_overlay.common.template_string);
671
672                 READ_INT(*options, image_overlay.common.x);
673                 READ_INT(*options, image_overlay.common.y);
674
675
676                 /* slideshow options */
677                 READ_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION);
678                 READ_BOOL(*options, slideshow.random);
679                 READ_BOOL(*options, slideshow.repeat);
680
681                 /* collection options */
682
683                 READ_BOOL(*options, collections.rectangular_selection);
684
685                 /* filtering options */
686
687                 READ_BOOL(*options, file_filter.show_hidden_files);
688                 READ_BOOL(*options, file_filter.show_dot_directory);
689                 READ_BOOL(*options, file_filter.disable);
690                 READ_CHAR(*options, sidecar.ext);
691
692                 /* Color Profiles */
693
694                 /* Shell command */
695                 READ_CHAR(*options, shell.path);
696                 READ_CHAR(*options, shell.options);
697
698                 /* Helpers */
699                 READ_CHAR(*options, helpers.html_browser.command_name);
700                 READ_CHAR(*options, helpers.html_browser.command_line);
701                 /* Exif */
702 /*
703                 if (0 == g_ascii_strncasecmp(option, "exif.display.", 13))
704                         {
705                         for (i = 0; ExifUIList[i].key; i++)
706                                 if (0 == g_ascii_strcasecmp(option + 13, ExifUIList[i].key))
707                                         ExifUIList[i].current = strtol(value, NULL, 10);
708                         continue;
709                         }
710 */
711                 /* metadata */          
712                 READ_BOOL(*options, metadata.enable_metadata_dirs);
713                 READ_BOOL(*options, metadata.save_in_image_file);
714                 READ_BOOL(*options, metadata.save_legacy_IPTC);
715                 READ_BOOL(*options, metadata.warn_on_write_problems);
716                 READ_BOOL(*options, metadata.save_legacy_format);
717                 READ_BOOL(*options, metadata.sync_grouped_files);
718                 READ_BOOL(*options, metadata.confirm_write);
719                 READ_BOOL(*options, metadata.confirm_after_timeout);
720                 READ_INT(*options, metadata.confirm_timeout);
721                 READ_BOOL(*options, metadata.confirm_on_image_change);
722                 READ_BOOL(*options, metadata.confirm_on_dir_change);
723
724                 DEBUG_1("unknown attribute %s = %s", option, value);
725                 }
726
727         return TRUE;
728 }
729
730 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)
731 {
732         while (*attribute_names)
733                 {
734                 const gchar *option = *attribute_names++;
735                 const gchar *value = *attribute_values++;
736
737
738                 READ_BOOL(options->color_profile, enabled);
739                 READ_BOOL(options->color_profile, use_image);
740                 READ_INT(options->color_profile, input_type);
741                 READ_INT(options->color_profile, screen_type);
742                 READ_CHAR(options->color_profile, screen_file);
743
744                 DEBUG_1("unknown attribute %s = %s", option, value);
745                 }
746
747 }
748
749 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)
750 {
751         gint i = GPOINTER_TO_INT(data);
752         if (i < 0 || i >= COLOR_PROFILE_INPUTS) return;
753         while (*attribute_names)
754                 {
755                 const gchar *option = *attribute_names++;
756                 const gchar *value = *attribute_values++;
757
758                 READ_CHAR_FULL("input_file", options->color_profile.input_file[i]);
759                 READ_CHAR_FULL("input_name", options->color_profile.input_name[i]);
760                 
761
762                 DEBUG_1("unknown attribute %s = %s", option, value);
763                 }
764         i++;
765         options_parse_func_set_data(parser_data, GINT_TO_POINTER(i));
766
767 }
768
769
770
771 /*
772  *-----------------------------------------------------------------------------
773  * xml file structure (private)
774  *-----------------------------------------------------------------------------
775  */
776 struct _GQParserData
777 {
778         GList *parse_func_stack;
779         gboolean startup; /* reading config for the first time - add commandline and call init_after_global_options() */
780         gboolean global_found;
781 };
782
783
784 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)
785 {
786         DEBUG_1("unexpected: %s", element_name);
787         options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
788 }
789
790 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)
791 {
792         if (g_ascii_strcasecmp(element_name, "profile") == 0)
793                 {
794                 options_load_profile(parser_data, context, element_name, attribute_names, attribute_values, data, error);
795                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
796                 }
797         else
798                 {
799                 DEBUG_1("unexpected profile: %s", element_name);
800                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
801                 }
802 }
803
804 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)
805 {
806         if (g_ascii_strcasecmp(element_name, "file_type") == 0)
807                 {
808                 filter_load_file_type(attribute_names, attribute_values);
809                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
810                 }
811         else
812                 {
813                 DEBUG_1("unexpected filter: %s", element_name);
814                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
815                 }
816 }
817
818 static void options_parse_filter_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
819 {
820         DEBUG_1(" filter end");
821         filter_add_defaults();
822         filter_rebuild();
823 }
824
825 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)
826 {
827         if (g_ascii_strcasecmp(element_name, "color_profiles") == 0)
828                 {
829                 options_load_color_profiles(parser_data, context, element_name, attribute_names, attribute_values, data, error);
830                 options_parse_func_push(parser_data, options_parse_color_profiles, NULL, GINT_TO_POINTER(0));
831                 }
832         else if (g_ascii_strcasecmp(element_name, "filter") == 0)
833                 {
834                 options_parse_func_push(parser_data, options_parse_filter, options_parse_filter_end, NULL);
835                 }
836         else if (g_ascii_strcasecmp(element_name, "layout") == 0)
837                 {
838                 layout_load_attributes(&options->layout, attribute_names, attribute_values);
839                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
840                 }
841         else
842                 {
843                 DEBUG_1("unexpected global: %s", element_name);
844                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
845                 }
846 }
847
848 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)
849 {
850         GtkWidget *bar = data;
851         if (g_ascii_strcasecmp(element_name, "pane_comment") == 0)
852                 {
853                 GtkWidget *pane = bar_pane_comment_new_from_config(attribute_names, attribute_values);
854                 bar_add(bar, pane);
855                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
856                 }
857         else if (g_ascii_strcasecmp(element_name, "pane_exif") == 0)
858                 {
859                 GtkWidget *pane = bar_pane_exif_new_from_config(attribute_names, attribute_values);
860                 bar_add(bar, pane);
861                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
862                 }
863         else if (g_ascii_strcasecmp(element_name, "pane_histogram") == 0)
864                 {
865                 GtkWidget *pane = bar_pane_histogram_new_from_config(attribute_names, attribute_values);
866                 bar_add(bar, pane);
867                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
868                 }
869         else if (g_ascii_strcasecmp(element_name, "pane_keywords") == 0)
870                 {
871                 GtkWidget *pane = bar_pane_keywords_new_from_config(attribute_names, attribute_values);
872                 bar_add(bar, pane);
873                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
874                 }
875         else
876                 {
877                 DEBUG_1("unexpected in <bar>: <%s>", element_name);
878                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
879                 }
880 }
881
882 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)
883 {
884         LayoutWindow *lw = data;
885         if (g_ascii_strcasecmp(element_name, "bar") == 0)
886                 {
887                 if (lw->bar) 
888                         layout_bar_close(lw);
889                 layout_bar_new(lw, FALSE);
890                 options_parse_func_push(parser_data, options_parse_bar, NULL, lw->bar);
891                 }
892         else
893                 {
894                 DEBUG_1("unexpected in <layout>: <%s>", element_name);
895                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
896                 }
897 }
898
899 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)
900 {
901         if (g_ascii_strcasecmp(element_name, "global") == 0)
902                 {
903                 load_global_params(attribute_names, attribute_values);
904                 options_parse_func_push(parser_data, options_parse_global, NULL, NULL);
905                 return;
906                 }
907         
908         if (parser_data->startup && !parser_data->global_found)
909                 {
910                 DEBUG_1(" global end");
911                 parser_data->global_found = TRUE;
912                 init_after_global_options();
913                 }
914         
915         if (g_ascii_strcasecmp(element_name, "layout") == 0)
916                 {
917                 LayoutWindow *lw;
918                 lw = layout_new_from_config(attribute_names, attribute_values, parser_data->startup);
919                 options_parse_func_push(parser_data, options_parse_layout, NULL, lw);
920                 }
921         else
922                 {
923                 DEBUG_1("unexpected in <toplevel>: <%s>", element_name);
924                 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
925                 }
926 }
927
928
929
930
931
932 /*
933  *-----------------------------------------------------------------------------
934  * parser
935  *-----------------------------------------------------------------------------
936  */
937
938
939 struct _GQParserFuncData
940 {
941         GQParserStartFunc start_func;
942         GQParserEndFunc end_func;
943 //      GQParserTextFunc text_func;
944         gpointer data;
945 };
946
947 void options_parse_func_push(GQParserData *parser_data, GQParserStartFunc start_func, GQParserEndFunc end_func, gpointer data)
948 {
949         GQParserFuncData *func_data = g_new0(GQParserFuncData, 1);
950         func_data->start_func = start_func;
951         func_data->end_func = end_func;
952         func_data->data = data;
953         
954         parser_data->parse_func_stack = g_list_prepend(parser_data->parse_func_stack, func_data);
955 }
956
957 void options_parse_func_pop(GQParserData *parser_data)
958 {
959         g_free(parser_data->parse_func_stack->data);
960         parser_data->parse_func_stack = g_list_delete_link(parser_data->parse_func_stack, parser_data->parse_func_stack);
961 }
962
963 void options_parse_func_set_data(GQParserData *parser_data, gpointer data)
964 {
965         GQParserFuncData *func = parser_data->parse_func_stack->data;
966         func->data = data;
967 }
968
969
970 static void start_element(GMarkupParseContext *context,
971                           const gchar *element_name,
972                           const gchar **attribute_names,
973                           const gchar **attribute_values,
974                           gpointer user_data,
975                           GError **error) 
976 {
977         GQParserData *parser_data = user_data;
978         GQParserFuncData *func = parser_data->parse_func_stack->data; 
979         DEBUG_1("start %s", element_name);
980         
981         if (func->start_func)
982                 func->start_func(parser_data, context, element_name, attribute_names, attribute_values, func->data, error);
983 }
984
985 static void end_element(GMarkupParseContext *context,
986                           const gchar *element_name,
987                           gpointer user_data,
988                           GError **error) 
989 {
990         GQParserData *parser_data = user_data;
991         GQParserFuncData *func = parser_data->parse_func_stack->data; 
992         DEBUG_1("end %s", element_name);
993
994         if (func->end_func)
995                 func->end_func(parser_data, context, element_name, func->data, error);
996
997         options_parse_func_pop(parser_data);
998 }
999
1000 static GMarkupParser parser = {
1001         start_element,
1002         end_element,
1003         NULL,
1004         NULL,
1005         NULL
1006 };
1007
1008 /*
1009  *-----------------------------------------------------------------------------
1010  * load configuration (public)
1011  *-----------------------------------------------------------------------------
1012  */
1013
1014 gboolean load_options_from(const gchar *utf8_path, ConfOptions *options, gboolean startup)
1015 {
1016         gsize size;
1017         gchar *buf;
1018         GMarkupParseContext *context;
1019         gboolean ret = TRUE;
1020         GQParserData *parser_data;
1021
1022         if (g_file_get_contents (utf8_path, &buf, &size, NULL) == FALSE) 
1023                 {
1024                 return FALSE;
1025                 }
1026         
1027         parser_data = g_new0(GQParserData, 1);
1028         
1029         parser_data->startup = startup;
1030         options_parse_func_push(parser_data, options_parse_toplevel, NULL, NULL);
1031         
1032         context = g_markup_parse_context_new(&parser, 0, parser_data, NULL);
1033
1034         if (g_markup_parse_context_parse (context, buf, size, NULL) == FALSE)
1035                 {
1036                 ret = FALSE;
1037                 DEBUG_1("Parse failed");
1038                 }
1039                 
1040         g_free(parser_data);
1041
1042         g_free(buf);
1043         g_markup_parse_context_free (context);
1044         return ret;
1045 }
1046         
1047
1048
1049 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */