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