From 29427fa4f184ab09e5107b90a301d281476c25c5 Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Fri, 4 Jul 2014 18:19:45 +0200 Subject: [PATCH] app: port GimpImageMapTool settings import/export to streams Simplifies and centralizes error checking and reporting. --- app/operations/gimpcurvesconfig.c | 35 ++-------- app/operations/gimpcurvesconfig.h | 2 +- app/operations/gimplevelsconfig.c | 35 ++-------- app/operations/gimplevelsconfig.h | 2 +- app/tools/gimpcurvestool.c | 45 +++--------- app/tools/gimpimagemaptool-settings.c | 98 ++++++++++++++++++--------- app/tools/gimpimagemaptool-settings.h | 4 +- app/tools/gimpimagemaptool.h | 4 +- app/tools/gimplevelstool.c | 45 +++--------- 9 files changed, 109 insertions(+), 161 deletions(-) diff --git a/app/operations/gimpcurvesconfig.c b/app/operations/gimpcurvesconfig.c index 3f1e670d04..c79d102c17 100644 --- a/app/operations/gimpcurvesconfig.c +++ b/app/operations/gimpcurvesconfig.c @@ -599,32 +599,17 @@ gimp_curves_config_load_cruft (GimpCurvesConfig *config, gboolean gimp_curves_config_save_cruft (GimpCurvesConfig *config, - GFile *file, + GOutputStream *output, GError **error) { - GOutputStream *output; - GString *string; - gsize bytes_written; - gint i; - GError *my_error = NULL; + GString *string; + gsize bytes_written; + gint i; g_return_val_if_fail (GIMP_IS_CURVES_CONFIG (config), FALSE); - g_return_val_if_fail (G_IS_FILE (file), FALSE); + g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - output = G_OUTPUT_STREAM (g_file_replace (file, - NULL, FALSE, G_FILE_CREATE_NONE, - NULL, &my_error)); - if (! output) - { - g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, - _("Could not open '%s' for writing: %s"), - gimp_file_get_utf8_name (file), - my_error->message); - g_clear_error (&my_error); - return FALSE; - } - string = g_string_new ("# GIMP Curves File\n"); for (i = 0; i < 5; i++) @@ -682,21 +667,15 @@ gimp_curves_config_save_cruft (GimpCurvesConfig *config, } if (! g_output_stream_write_all (output, string->str, string->len, - &bytes_written, NULL, &my_error) || + &bytes_written, NULL, error) || bytes_written != string->len) { - g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_WRITE, - _("Writing curves file '%s' failed: %s"), - gimp_file_get_utf8_name (file), - my_error->message); - g_clear_error (&my_error); + g_prefix_error (error, _("Writing curves file failed: ")); g_string_free (string, TRUE); - g_object_unref (output); return FALSE; } g_string_free (string, TRUE); - g_object_unref (output); return TRUE; } diff --git a/app/operations/gimpcurvesconfig.h b/app/operations/gimpcurvesconfig.h index 990a8e930d..c73afec208 100644 --- a/app/operations/gimpcurvesconfig.h +++ b/app/operations/gimpcurvesconfig.h @@ -72,7 +72,7 @@ gboolean gimp_curves_config_load_cruft (GimpCurvesConfig *config, GInputStream *input, GError **error); gboolean gimp_curves_config_save_cruft (GimpCurvesConfig *config, - GFile *file, + GOutputStream *output, GError **error); diff --git a/app/operations/gimplevelsconfig.c b/app/operations/gimplevelsconfig.c index ead15ce560..cc5702f86c 100644 --- a/app/operations/gimplevelsconfig.c +++ b/app/operations/gimplevelsconfig.c @@ -840,32 +840,17 @@ gimp_levels_config_load_cruft (GimpLevelsConfig *config, gboolean gimp_levels_config_save_cruft (GimpLevelsConfig *config, - GFile *file, + GOutputStream *output, GError **error) { - GOutputStream *output; - GString *string; - gsize bytes_written; - gint i; - GError *my_error = NULL; + GString *string; + gsize bytes_written; + gint i; g_return_val_if_fail (GIMP_IS_LEVELS_CONFIG (config), FALSE); - g_return_val_if_fail (G_IS_FILE (file), FALSE); + g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - output = G_OUTPUT_STREAM (g_file_replace (file, - NULL, FALSE, G_FILE_CREATE_NONE, - NULL, &my_error)); - if (! output) - { - g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, - _("Could not open '%s' for writing: %s"), - gimp_file_get_utf8_name (file), - my_error->message); - g_clear_error (&my_error); - return FALSE; - } - string = g_string_new ("# GIMP Levels File\n"); for (i = 0; i < 5; i++) @@ -883,21 +868,15 @@ gimp_levels_config_save_cruft (GimpLevelsConfig *config, } if (! g_output_stream_write_all (output, string->str, string->len, - &bytes_written, NULL, &my_error) || + &bytes_written, NULL, error) || bytes_written != string->len) { - g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_WRITE, - _("Writing levels file '%s' failed: %s"), - gimp_file_get_utf8_name (file), - my_error->message); - g_clear_error (&my_error); + g_prefix_error (error, _("Writing levels file failed: ")); g_string_free (string, TRUE); - g_object_unref (output); return FALSE; } g_string_free (string, TRUE); - g_object_unref (output); return TRUE; } diff --git a/app/operations/gimplevelsconfig.h b/app/operations/gimplevelsconfig.h index 71ac0370cb..bac413732a 100644 --- a/app/operations/gimplevelsconfig.h +++ b/app/operations/gimplevelsconfig.h @@ -79,7 +79,7 @@ gboolean gimp_levels_config_load_cruft (GimpLevelsConfig *config, GInputStream *input, GError **error); gboolean gimp_levels_config_save_cruft (GimpLevelsConfig *config, - GFile *file, + GOutputStream *output, GError **error); diff --git a/app/tools/gimpcurvestool.c b/app/tools/gimpcurvestool.c index 07392fff9a..fecdf8b75c 100644 --- a/app/tools/gimpcurvestool.c +++ b/app/tools/gimpcurvestool.c @@ -93,10 +93,10 @@ static GeglNode * gimp_curves_tool_get_operation (GimpImageMapTool *image_m static void gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool); static void gimp_curves_tool_reset (GimpImageMapTool *image_map_tool); static gboolean gimp_curves_tool_settings_import(GimpImageMapTool *image_map_tool, - GFile *file, + GInputStream *input, GError **error); static gboolean gimp_curves_tool_settings_export(GimpImageMapTool *image_map_tool, - GFile *file, + GOutputStream *output, GError **error); static void gimp_curves_tool_export_setup (GimpSettingsBox *settings_box, @@ -594,66 +594,43 @@ gimp_curves_tool_reset (GimpImageMapTool *image_map_tool) static gboolean gimp_curves_tool_settings_import (GimpImageMapTool *image_map_tool, - GFile *file, + GInputStream *input, GError **error) { GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool); - GInputStream *input; gchar header[64]; gsize bytes_read; - input = G_INPUT_STREAM (g_file_read (file, NULL, error)); - if (! input) - { - g_prefix_error (error, - _("Could not open '%s' for reading: "), - gimp_file_get_utf8_name (file)); - return FALSE; - } - if (! g_input_stream_read_all (input, header, sizeof (header), &bytes_read, NULL, error) || bytes_read != sizeof (header)) { - g_prefix_error (error, - _("Could not read header from '%s': "), - gimp_file_get_utf8_name (file)); - g_object_unref (input); + g_prefix_error (error, _("Could not read header: ")); return FALSE; } + g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); + if (g_str_has_prefix (header, "# GIMP Curves File\n")) - { - gboolean success; - - g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); - - success = gimp_curves_config_load_cruft (tool->config, input, error); - - g_object_unref (input); - - return success; - } - - g_object_unref (input); + return gimp_curves_config_load_cruft (tool->config, input, error); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool, - file, + input, error); } static gboolean gimp_curves_tool_settings_export (GimpImageMapTool *image_map_tool, - GFile *file, + GOutputStream *output, GError **error) { GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool); if (tool->export_old_format) - return gimp_curves_config_save_cruft (tool->config, file, error); + return gimp_curves_config_save_cruft (tool->config, output, error); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool, - file, + output, error); } diff --git a/app/tools/gimpimagemaptool-settings.c b/app/tools/gimpimagemaptool-settings.c index d8ac0f0dad..5f10fc7538 100644 --- a/app/tools/gimpimagemaptool-settings.c +++ b/app/tools/gimpimagemaptool-settings.c @@ -114,24 +114,17 @@ gimp_image_map_tool_real_get_settings_ui (GimpImageMapTool *image_map_tool, gboolean gimp_image_map_tool_real_settings_import (GimpImageMapTool *tool, - GFile *file, + GInputStream *input, GError **error) { - gboolean success; - - if (GIMP_TOOL (tool)->tool_info->gimp->be_verbose) - g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); - - success = gimp_config_deserialize_gfile (GIMP_CONFIG (tool->config), - file, - NULL, error); - - return success; + return gimp_config_deserialize_stream (GIMP_CONFIG (tool->config), + input, + NULL, error); } gboolean gimp_image_map_tool_real_settings_export (GimpImageMapTool *tool, - GFile *file, + GOutputStream *output, GError **error) { GimpImageMapToolClass *klass = GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool); @@ -142,13 +135,10 @@ gimp_image_map_tool_real_settings_export (GimpImageMapTool *tool, header = g_strdup_printf ("GIMP %s tool settings", klass->settings_name); footer = g_strdup_printf ("end of %s tool settings", klass->settings_name); - if (GIMP_TOOL (tool)->tool_info->gimp->be_verbose) - g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file)); - - success = gimp_config_serialize_to_gfile (GIMP_CONFIG (tool->config), - file, - header, footer, - NULL, error); + success = gimp_config_serialize_to_stream (GIMP_CONFIG (tool->config), + output, + header, footer, + NULL, error); g_free (header); g_free (footer); @@ -165,20 +155,42 @@ gimp_image_map_tool_settings_import (GimpSettingsBox *box, GimpImageMapTool *tool) { GimpImageMapToolClass *tool_class = GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool); + GInputStream *input; GError *error = NULL; g_return_val_if_fail (tool_class->settings_import != NULL, FALSE); - if (! tool_class->settings_import (tool, file, &error)) - { - gimp_message_literal (GIMP_TOOL (tool)->tool_info->gimp, - G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), - GIMP_MESSAGE_ERROR, error->message); - g_clear_error (&error); + if (GIMP_TOOL (tool)->tool_info->gimp->be_verbose) + g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file)); + input = G_INPUT_STREAM (g_file_read (file, NULL, &error)); + if (! input) + { + gimp_message (GIMP_TOOL (tool)->tool_info->gimp, + G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), + GIMP_MESSAGE_ERROR, + _("Could not open '%s' for reading: %s"), + gimp_file_get_utf8_name (file), + error->message); + g_clear_error (&error); return FALSE; } + if (! tool_class->settings_import (tool, input, &error)) + { + gimp_message (GIMP_TOOL (tool)->tool_info->gimp, + G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), + GIMP_MESSAGE_ERROR, + _("Error reading '%s': %s"), + gimp_file_get_utf8_name (file), + error->message); + g_clear_error (&error); + g_object_unref (input); + return FALSE; + } + + g_object_unref (input); + gimp_image_map_tool_preview (tool); g_object_set (GIMP_TOOL_GET_OPTIONS (tool), @@ -194,20 +206,44 @@ gimp_image_map_tool_settings_export (GimpSettingsBox *box, GimpImageMapTool *tool) { GimpImageMapToolClass *tool_class = GIMP_IMAGE_MAP_TOOL_GET_CLASS (tool); + GOutputStream *output; GError *error = NULL; g_return_val_if_fail (tool_class->settings_export != NULL, FALSE); - if (! tool_class->settings_export (tool, file, &error)) - { - gimp_message_literal (GIMP_TOOL (tool)->tool_info->gimp, - G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), - GIMP_MESSAGE_ERROR, error->message); - g_clear_error (&error); + if (GIMP_TOOL (tool)->tool_info->gimp->be_verbose) + g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file)); + output = G_OUTPUT_STREAM (g_file_replace (file, + NULL, FALSE, G_FILE_CREATE_NONE, + NULL, &error)); + if (! output) + { + gimp_message (GIMP_TOOL (tool)->tool_info->gimp, + G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), + GIMP_MESSAGE_ERROR, + _("Could not open '%s' for writing: %s"), + gimp_file_get_utf8_name (file), + error->message); + g_clear_error (&error); return FALSE; } + if (! tool_class->settings_export (tool, output, &error)) + { + gimp_message (GIMP_TOOL (tool)->tool_info->gimp, + G_OBJECT (gimp_tool_gui_get_dialog (tool->gui)), + GIMP_MESSAGE_ERROR, + _("Error writing '%s': %s"), + gimp_file_get_utf8_name (file), + error->message); + g_clear_error (&error); + g_object_unref (output); + return FALSE; + } + + g_object_unref (output); + gimp_message (GIMP_TOOL (tool)->tool_info->gimp, G_OBJECT (GIMP_TOOL (tool)->display), GIMP_MESSAGE_INFO, diff --git a/app/tools/gimpimagemaptool-settings.h b/app/tools/gimpimagemaptool-settings.h index 0ed225fab3..1fddf867c6 100644 --- a/app/tools/gimpimagemaptool-settings.h +++ b/app/tools/gimpimagemaptool-settings.h @@ -30,10 +30,10 @@ GtkWidget * gimp_image_map_tool_real_get_settings_ui (GimpImageMapTool *tool, const gchar *default_folder, GtkWidget **settings_box); gboolean gimp_image_map_tool_real_settings_import (GimpImageMapTool *tool, - GFile *file, + GInputStream *input, GError **error); gboolean gimp_image_map_tool_real_settings_export (GimpImageMapTool *tool, - GFile *file, + GOutputStream *output, GError **error); diff --git a/app/tools/gimpimagemaptool.h b/app/tools/gimpimagemaptool.h index 6fda8a54a4..23722ad9df 100644 --- a/app/tools/gimpimagemaptool.h +++ b/app/tools/gimpimagemaptool.h @@ -85,10 +85,10 @@ struct _GimpImageMapToolClass GtkWidget **settings_box); gboolean (* settings_import) (GimpImageMapTool *image_map_tool, - GFile *file, + GInputStream *input, GError **error); gboolean (* settings_export) (GimpImageMapTool *image_map_tool, - GFile *file, + GOutputStream *output, GError **error); void (* color_picked) (GimpImageMapTool *image_map_tool, diff --git a/app/tools/gimplevelstool.c b/app/tools/gimplevelstool.c index b4ff5c1844..5c22ad1a6d 100644 --- a/app/tools/gimplevelstool.c +++ b/app/tools/gimplevelstool.c @@ -77,10 +77,10 @@ static GeglNode * gimp_levels_tool_get_operation (GimpImageMapTool *im_tool, static void gimp_levels_tool_dialog (GimpImageMapTool *im_tool); static void gimp_levels_tool_reset (GimpImageMapTool *im_tool); static gboolean gimp_levels_tool_settings_import(GimpImageMapTool *im_tool, - GFile *file, + GInputStream *input, GError **error); static gboolean gimp_levels_tool_settings_export(GimpImageMapTool *im_tool, - GFile *file, + GOutputStream *output, GError **error); static void gimp_levels_tool_color_picked (GimpImageMapTool *im_tool, gpointer identifier, @@ -611,66 +611,43 @@ gimp_levels_tool_reset (GimpImageMapTool *image_map_tool) static gboolean gimp_levels_tool_settings_import (GimpImageMapTool *image_map_tool, - GFile *file, + GInputStream *input, GError **error) { GimpLevelsTool *tool = GIMP_LEVELS_TOOL (image_map_tool); - GInputStream *input; gchar header[64]; gsize bytes_read; - input = G_INPUT_STREAM (g_file_read (file, NULL, error)); - if (! input) - { - g_prefix_error (error, - _("Could not open '%s' for reading: "), - gimp_file_get_utf8_name (file)); - return FALSE; - } - if (! g_input_stream_read_all (input, header, sizeof (header), &bytes_read, NULL, error) || bytes_read != sizeof (header)) { - g_prefix_error (error, - _("Could not read header from '%s': "), - gimp_file_get_utf8_name (file)); - g_object_unref (input); + g_prefix_error (error, _("Could not read header: ")); return FALSE; } + g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); + if (g_str_has_prefix (header, "# GIMP Levels File\n")) - { - gboolean success; - - g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL); - - success = gimp_levels_config_load_cruft (tool->config, input, error); - - g_object_unref (input); - - return success; - } - - g_object_unref (input); + return gimp_levels_config_load_cruft (tool->config, input, error); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool, - file, + input, error); } static gboolean gimp_levels_tool_settings_export (GimpImageMapTool *image_map_tool, - GFile *file, + GOutputStream *output, GError **error) { GimpLevelsTool *tool = GIMP_LEVELS_TOOL (image_map_tool); if (tool->export_old_format) - return gimp_levels_config_save_cruft (tool->config, file, error); + return gimp_levels_config_save_cruft (tool->config, output, error); return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool, - file, + output, error); }