4 * Copyright (C) 2008 - 2009 The Geeqie Team
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!
13 #include <glib/gstdio.h>
20 #include "bar_comment.h"
22 #include "bar_histogram.h"
23 #include "bar_keywords.h"
25 #include "filefilter.h"
27 #include "pixbuf-renderer.h"
28 #include "secure_save.h"
29 #include "slideshow.h"
30 #include "ui_fileops.h"
32 #include "layout_util.h"
37 *-----------------------------------------------------------------------------
38 * line write/parse routines (public)
39 *-----------------------------------------------------------------------------
42 void write_indent(GString *str, gint indent)
44 g_string_append_printf(str, "%*s", indent * 4, "");
47 void write_char_option(GString *str, gint indent, const gchar *label, const gchar *text)
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);
55 gboolean read_char_option(const gchar *option, const gchar *label, const gchar *value, gchar **text)
57 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
58 if (!text) return FALSE;
61 *text = g_strdup(value);
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)
69 return g_strdup_printf("#%04X%04X%04X", color->red, color->green, color->blue);
72 void write_color_option(GString *str, gint indent, gchar *label, GdkColor *color)
76 gchar *colorstring = color_to_string(color);
78 write_char_option(str, indent, label, colorstring);
82 write_char_option(str, indent, label, "");
85 gboolean read_color_option(const gchar *option, const gchar *label, const gchar *value, GdkColor *color)
87 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
88 if (!color) return FALSE;
90 if (!*value) return FALSE;
91 gdk_color_parse(value, color);
95 void write_int_option(GString *str, gint indent, const gchar *label, gint n)
97 write_indent(str, indent);
98 g_string_append_printf(str, "%s = \"%d\"\n", label, n);
101 gboolean read_int_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
103 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
104 if (!n) return FALSE;
106 if (g_ascii_isdigit(value[0]) || (value[0] == '-' && g_ascii_isdigit(value[1])))
108 *n = strtol(value, NULL, 10);
112 if (g_ascii_strcasecmp(value, "true") == 0)
121 void write_uint_option(GString *str, gint indent, const gchar *label, guint n)
123 write_indent(str, indent);
124 g_string_append_printf(str, "%s = \"%u\"\n", label, n);
127 gboolean read_uint_option(const gchar *option, const gchar *label, const gchar *value, guint *n)
129 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
130 if (!n) return FALSE;
132 if (g_ascii_isdigit(value[0]))
134 *n = strtoul(value, NULL, 10);
138 if (g_ascii_strcasecmp(value, "true") == 0)
147 gboolean read_uint_option_clamp(const gchar *option, const gchar *label, const gchar *value, guint *n, guint min, guint max)
151 ret = read_uint_option(option, label, value, n);
152 if (ret) *n = CLAMP(*n, min, max);
158 gboolean read_int_option_clamp(const gchar *option, const gchar *label, const gchar *value, gint *n, gint min, gint max)
162 ret = read_int_option(option, label, value, n);
163 if (ret) *n = CLAMP(*n, min, max);
168 void write_int_unit_option(GString *str, gint indent, gchar *label, gint n, gint subunits)
183 write_indent(str, indent);
184 g_string_append_printf(str, "%s = \"%d.%d\"\n", label, l, r);
187 gboolean read_int_unit_option(const gchar *option, const gchar *label, const gchar *value, gint *n, gint subunits)
192 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
193 if (!n) return FALSE;
195 buf = g_strdup(value);
197 while (*ptr != '\0' && *ptr != '.') ptr++;
201 l = strtol(value, NULL, 10);
204 r = strtol(ptr, NULL, 10);
208 l = strtol(value, NULL, 10);
212 *n = l * subunits + r;
218 void write_bool_option(GString *str, gint indent, gchar *label, gint n)
220 write_indent(str, indent);
221 g_string_append_printf(str, "%s = \"%s\"\n", label, n ? "true" : "false");
224 gboolean read_bool_option(const gchar *option, const gchar *label, const gchar *value, gint *n)
226 if (g_ascii_strcasecmp(option, label) != 0) return FALSE;
227 if (!n) return FALSE;
229 if (g_ascii_strcasecmp(value, "true") == 0 || atoi(value) != 0)
238 *-----------------------------------------------------------------------------
239 * write fuctions for elements (private)
240 *-----------------------------------------------------------------------------
243 static void write_global_attributes(GString *outstr, gint indent)
245 // WRITE_SUBTITLE("General Options");
247 WRITE_BOOL(*options, show_icon_names);
248 WRITE_BOOL(*options, show_copy_path);
251 WRITE_BOOL(*options, tree_descend_subdirs);
252 WRITE_BOOL(*options, lazy_image_sync);
253 WRITE_BOOL(*options, update_on_time_change);
256 WRITE_BOOL(*options, progressive_key_scrolling);
258 WRITE_UINT(*options, duplicates_similarity_threshold);
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);
267 // WRITE_SUBTITLE("Startup Options");
269 WRITE_BOOL(*options, startup.restore_path);
270 WRITE_BOOL(*options, startup.use_last_path);
271 WRITE_CHAR(*options, startup.path);
274 // WRITE_SUBTITLE("File operations Options");
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);
286 // WRITE_SUBTITLE("Properties dialog Options");
287 WRITE_CHAR(*options, properties.tabs_order);
289 // WRITE_SUBTITLE("Image Options");
291 WRITE_UINT(*options, image.zoom_mode);
293 // g_string_append_printf(outstr, "# image.zoom_mode possible values are:\n"
296 // "# dont_change\n");
297 // g_string_append_printf(outstr, "image.zoom_mode: ");
298 // switch (options->image.zoom_mode)
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;
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);
325 // WRITE_SUBTITLE("Thumbnails Options");
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);
338 // WRITE_SUBTITLE("File sorting Options");
340 WRITE_INT(*options, file_sort.method);
341 WRITE_BOOL(*options, file_sort.ascending);
342 WRITE_BOOL(*options, file_sort.case_sensitive);
345 // WRITE_SUBTITLE("Fullscreen Options");
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);
353 // WRITE_SUBTITLE("Histogram Options");
354 WRITE_UINT(*options, histogram.last_channel_mode);
355 WRITE_UINT(*options, histogram.last_log_mode);
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);
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);
373 // WRITE_SUBTITLE("Slideshow Options");
375 WRITE_INT_UNIT(*options, slideshow.delay, SLIDESHOW_SUBSECOND_PRECISION);
376 WRITE_BOOL(*options, slideshow.random);
377 WRITE_BOOL(*options, slideshow.repeat);
380 // WRITE_SUBTITLE("Collection Options");
382 WRITE_BOOL(*options, collections.rectangular_selection);
385 // WRITE_SUBTITLE("Filtering Options");
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);
393 // WRITE_SUBTITLE("Sidecars Options");
395 WRITE_CHAR(*options, sidecar.ext);
399 // WRITE_SUBTITLE("Shell command");
400 WRITE_CHAR(*options, shell.path);
401 WRITE_CHAR(*options, shell.options);
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);
416 WRITE_SUBTITLE("Exif Options");
417 g_string_append_printf(outstr, "# Display: 0: never\n"
420 for (i = 0; ExifUIList[i].key; i++)
422 g_string_append_printf(outstr, "exif.display.");
423 write_int_option(outstr, 2, (gchar *)ExifUIList[i].key, ExifUIList[i].current);
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);
442 static void write_color_profile(GString *outstr, gint indent)
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);
450 write_indent(outstr, indent);
451 g_string_append_printf(outstr, "<color_profiles\n");
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);
459 write_indent(outstr, indent);
460 g_string_append_printf(outstr, ">\n");
463 for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
465 write_indent(outstr, indent);
466 g_string_append_printf(outstr, "<profile\n");
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]);
471 write_indent(outstr, indent);
472 g_string_append_printf(outstr, "/>\n");
475 write_indent(outstr, indent);
476 g_string_append_printf(outstr, "</color_profiles>\n");
481 *-----------------------------------------------------------------------------
482 * save configuration (public)
483 *-----------------------------------------------------------------------------
486 gboolean save_options_to(const gchar *utf8_path, ConfOptions *options)
494 rc_pathl = path_from_utf8(utf8_path);
495 ssi = secure_open(rc_pathl);
499 log_printf(_("error saving config file: %s\n"), utf8_path);
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");
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");
513 g_string_append_printf(outstr, "-->\n");
515 g_string_append_printf(outstr, "<global\n");
517 write_global_attributes(outstr, indent);
518 write_indent(outstr, indent);
519 g_string_append_printf(outstr, ">\n");
521 write_color_profile(outstr, indent);
524 filter_write_list(outstr, indent);
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");
535 g_string_append_printf(outstr, "</global>\n");
538 WRITE_SUBTITLE("Layout Options");
540 work = layout_window_list;
543 LayoutWindow *lw = work->data;
544 layout_write_config(lw, outstr, indent);
551 secure_fputs(ssi, outstr->str);
552 g_string_free(outstr, TRUE);
554 if (secure_close(ssi))
556 log_printf(_("error saving config file: %s\nerror: %s\n"), utf8_path,
557 secsave_strerror(secsave_errno));
565 *-----------------------------------------------------------------------------
566 * loading attributes for elements (private)
567 *-----------------------------------------------------------------------------
571 static gboolean load_global_params(const gchar **attribute_names, const gchar **attribute_values)
573 while (*attribute_names)
575 const gchar *option = *attribute_names++;
576 const gchar *value = *attribute_values++;
579 /* general options */
580 READ_BOOL(*options, show_icon_names);
581 READ_BOOL(*options, show_copy_path);
583 READ_BOOL(*options, tree_descend_subdirs);
584 READ_BOOL(*options, lazy_image_sync);
585 READ_BOOL(*options, update_on_time_change);
587 READ_UINT_CLAMP(*options, duplicates_similarity_threshold, 0, 100);
589 READ_BOOL(*options, progressive_key_scrolling);
591 READ_BOOL(*options, mousewheel_scrolls);
593 READ_INT(*options, open_recent_list_maxsize);
594 READ_INT(*options, dnd_icon_size);
595 READ_BOOL(*options, place_dialogs_under_mouse);
597 /* startup options */
599 READ_BOOL(*options, startup.restore_path);
601 READ_BOOL(*options, startup.use_last_path);
603 READ_CHAR(*options, startup.path);
606 /* properties dialog options */
607 READ_CHAR(*options, properties.tabs_order);
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);
632 /* thumbnails options */
633 READ_INT_CLAMP(*options, thumbnails.max_width, 16, 512);
634 READ_INT_CLAMP(*options, thumbnails.max_height, 16, 512);
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);
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);
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);
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);
664 READ_UINT(*options, histogram.last_channel_mode);
665 READ_UINT(*options, histogram.last_log_mode);
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);
672 READ_INT(*options, image_overlay.common.x);
673 READ_INT(*options, image_overlay.common.y);
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);
681 /* collection options */
683 READ_BOOL(*options, collections.rectangular_selection);
685 /* filtering options */
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);
695 READ_CHAR(*options, shell.path);
696 READ_CHAR(*options, shell.options);
699 READ_CHAR(*options, helpers.html_browser.command_name);
700 READ_CHAR(*options, helpers.html_browser.command_line);
703 if (0 == g_ascii_strncasecmp(option, "exif.display.", 13))
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);
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);
724 DEBUG_1("unknown attribute %s = %s", option, value);
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)
732 while (*attribute_names)
734 const gchar *option = *attribute_names++;
735 const gchar *value = *attribute_values++;
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);
744 DEBUG_1("unknown attribute %s = %s", option, value);
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)
751 gint i = GPOINTER_TO_INT(data);
752 if (i < 0 || i >= COLOR_PROFILE_INPUTS) return;
753 while (*attribute_names)
755 const gchar *option = *attribute_names++;
756 const gchar *value = *attribute_values++;
758 READ_CHAR_FULL("input_file", options->color_profile.input_file[i]);
759 READ_CHAR_FULL("input_name", options->color_profile.input_name[i]);
762 DEBUG_1("unknown attribute %s = %s", option, value);
765 options_parse_func_set_data(parser_data, GINT_TO_POINTER(i));
772 *-----------------------------------------------------------------------------
773 * xml file structure (private)
774 *-----------------------------------------------------------------------------
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)
780 DEBUG_1("unexpected: %s", element_name);
781 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
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)
786 if (g_ascii_strcasecmp(element_name, "profile") == 0)
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);
793 DEBUG_1("unexpected profile: %s", element_name);
794 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
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)
800 if (g_ascii_strcasecmp(element_name, "file_type") == 0)
802 filter_load_file_type(attribute_names, attribute_values);
803 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
807 DEBUG_1("unexpected filter: %s", element_name);
808 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
812 static void options_parse_filter_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
814 DEBUG_1(" filter end");
815 filter_add_defaults();
819 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)
821 if (g_ascii_strcasecmp(element_name, "color_profiles") == 0)
823 options_load_color_profiles(parser_data, context, element_name, attribute_names, attribute_values, data, error);
824 options_parse_func_push(parser_data, options_parse_color_profiles, NULL, GINT_TO_POINTER(0));
826 else if (g_ascii_strcasecmp(element_name, "filter") == 0)
828 options_parse_func_push(parser_data, options_parse_filter, options_parse_filter_end, NULL);
830 else if (g_ascii_strcasecmp(element_name, "layout") == 0)
832 layout_load_attributes(&options->layout, attribute_names, attribute_values);
833 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
837 DEBUG_1("unexpected global: %s", element_name);
838 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
842 static void options_parse_global_end(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, gpointer data, GError **error)
844 DEBUG_1(" global end");
845 init_after_global_options();
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)
850 GtkWidget *bar = data;
851 if (g_ascii_strcasecmp(element_name, "pane_comment") == 0)
853 GtkWidget *pane = bar_pane_comment_new_from_config(attribute_names, attribute_values);
855 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
857 else if (g_ascii_strcasecmp(element_name, "pane_exif") == 0)
859 GtkWidget *pane = bar_pane_exif_new_from_config(attribute_names, attribute_values);
861 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
863 else if (g_ascii_strcasecmp(element_name, "pane_histogram") == 0)
865 GtkWidget *pane = bar_pane_histogram_new_from_config(attribute_names, attribute_values);
867 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
869 else if (g_ascii_strcasecmp(element_name, "pane_keywords") == 0)
871 GtkWidget *pane = bar_pane_keywords_new_from_config(attribute_names, attribute_values);
873 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
877 DEBUG_1("unexpected in <bar>: <%s>", element_name);
878 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
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)
884 LayoutWindow *lw = data;
885 if (g_ascii_strcasecmp(element_name, "bar") == 0)
888 layout_bar_close(lw);
889 layout_bar_new(lw, FALSE);
890 options_parse_func_push(parser_data, options_parse_bar, NULL, lw->bar);
894 DEBUG_1("unexpected in <layout>: <%s>", element_name);
895 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
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)
901 if (g_ascii_strcasecmp(element_name, "global") == 0)
903 load_global_params(attribute_names, attribute_values);
904 options_parse_func_push(parser_data, options_parse_global, options_parse_global_end, NULL);
906 else if (g_ascii_strcasecmp(element_name, "layout") == 0)
909 lw = layout_new_from_config(attribute_names, attribute_values);
910 options_parse_func_push(parser_data, options_parse_layout, NULL, lw);
914 DEBUG_1("unexpected in <toplevel>: <%s>", element_name);
915 options_parse_func_push(parser_data, options_parse_leaf, NULL, NULL);
924 *-----------------------------------------------------------------------------
926 *-----------------------------------------------------------------------------
930 struct _GQParserFuncData
932 GQParserStartFunc start_func;
933 GQParserEndFunc end_func;
934 // GQParserTextFunc text_func;
940 GList *parse_func_stack;
943 void options_parse_func_push(GQParserData *parser_data, GQParserStartFunc start_func, GQParserEndFunc end_func, gpointer data)
945 GQParserFuncData *func_data = g_new0(GQParserFuncData, 1);
946 func_data->start_func = start_func;
947 func_data->end_func = end_func;
948 func_data->data = data;
950 parser_data->parse_func_stack = g_list_prepend(parser_data->parse_func_stack, func_data);
953 void options_parse_func_pop(GQParserData *parser_data)
955 g_free(parser_data->parse_func_stack->data);
956 parser_data->parse_func_stack = g_list_delete_link(parser_data->parse_func_stack, parser_data->parse_func_stack);
959 void options_parse_func_set_data(GQParserData *parser_data, gpointer data)
961 GQParserFuncData *func = parser_data->parse_func_stack->data;
966 static void start_element(GMarkupParseContext *context,
967 const gchar *element_name,
968 const gchar **attribute_names,
969 const gchar **attribute_values,
973 GQParserData *parser_data = user_data;
974 GQParserFuncData *func = parser_data->parse_func_stack->data;
975 DEBUG_1("start %s", element_name);
977 if (func->start_func)
978 func->start_func(parser_data, context, element_name, attribute_names, attribute_values, func->data, error);
981 static void end_element(GMarkupParseContext *context,
982 const gchar *element_name,
986 GQParserData *parser_data = user_data;
987 GQParserFuncData *func = parser_data->parse_func_stack->data;
988 DEBUG_1("end %s", element_name);
991 func->end_func(parser_data, context, element_name, func->data, error);
993 options_parse_func_pop(parser_data);
996 static GMarkupParser parser = {
1005 *-----------------------------------------------------------------------------
1006 * load configuration (public)
1007 *-----------------------------------------------------------------------------
1010 gboolean load_options_from(const gchar *utf8_path, ConfOptions *options)
1014 GMarkupParseContext *context;
1015 gboolean ret = TRUE;
1016 GQParserData *parser_data;
1018 if (g_file_get_contents (utf8_path, &buf, &size, NULL) == FALSE)
1023 parser_data = g_new0(GQParserData, 1);
1024 options_parse_func_push(parser_data, options_parse_toplevel, NULL, NULL);
1026 context = g_markup_parse_context_new(&parser, 0, parser_data, NULL);
1028 if (g_markup_parse_context_parse (context, buf, size, NULL) == FALSE)
1031 DEBUG_1("Parse failed");
1034 g_free(parser_data);
1037 g_markup_parse_context_free (context);
1043 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */