app: port GimpImageMapTool settings import/export to streams

Simplifies and centralizes error checking and reporting.
This commit is contained in:
Michael Natterer 2014-07-04 18:19:45 +02:00
parent 46b619811d
commit 29427fa4f1
9 changed files with 109 additions and 161 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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,

View File

@ -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);

View File

@ -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,

View File

@ -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);
}