Commit 4ccd0f0a authored by Michael Natterer's avatar Michael Natterer 😴 Committed by Michael Natterer
Browse files

use GError for reporting load/save errors. Added gimp_data_error_quark()

2002-12-02  Michael Natterer  <mitch@gimp.org>

	* app/core/gimpdata.[ch]: use GError for reporting load/save
	errors. Added gimp_data_error_quark() and a GimpDataError enum.
	Made simple virtual functions out of save(), get_extension()
	and duplicate() because they don't need to be signals.

	* app/core/gimpbrush.[ch]
	* app/core/gimpbrushgenerated.[ch]
	* app/core/gimpbrushpipe.[ch]
	* app/core/gimpgradient.[ch]
	* app/core/gimppalette.[ch]
	* app/core/gimppattern.[ch]: changed accordingly. Set data->dirty
	to FALSE in each _load() function.

	* app/core/gimpdatafactory.[ch]: report GError messages here.
	Changed the ugly factory->path pointer location hack to
	factory->path_property_name and get the path from
	factory->gimp->config. Added gimp_data_factory_data_duplicate()
	implementation which was already declared in the header.

	* app/core/gimp.c: pass the path property name to
	gimp_data_factory_new().

	* app/widgets/gimpdatafactoryview.c: removed the duplicate
	implementation and call gimp_data_factory_data_duplicate().

	* app/core/gimpgradient.[ch]: added gimp_gradient_save_as_pov().

	* app/gui/gradients-commands.c: removed save_as_pov implementation
	and call gimp_gradient_save_as_pov().
parent 558e3a24
2002-12-02 Michael Natterer <mitch@gimp.org>
* app/core/gimpdata.[ch]: use GError for reporting load/save
errors. Added gimp_data_error_quark() and a GimpDataError enum.
Made simple virtual functions out of save(), get_extension()
and duplicate() because they don't need to be signals.
* app/core/gimpbrush.[ch]
* app/core/gimpbrushgenerated.[ch]
* app/core/gimpbrushpipe.[ch]
* app/core/gimpgradient.[ch]
* app/core/gimppalette.[ch]
* app/core/gimppattern.[ch]: changed accordingly. Set data->dirty
to FALSE in each _load() function.
* app/core/gimpdatafactory.[ch]: report GError messages here.
Changed the ugly factory->path pointer location hack to
factory->path_property_name and get the path from
factory->gimp->config. Added gimp_data_factory_data_duplicate()
implementation which was already declared in the header.
* app/core/gimp.c: pass the path property name to
gimp_data_factory_new().
* app/widgets/gimpdatafactoryview.c: removed the duplicate
implementation and call gimp_data_factory_data_duplicate().
* app/core/gimpgradient.[ch]: added gimp_gradient_save_as_pov().
* app/gui/gradients-commands.c: removed save_as_pov implementation
and call gimp_gradient_save_as_pov().
2002-12-01 Sven Neumann <sven@gimp.org>
 
* app/config/gimpconfig-blurbs.h: small cleanup.
......
......@@ -150,75 +150,17 @@ static void
gradients_save_as_pov_ok_callback (GtkWidget *widget,
GimpGradient *gradient)
{
GtkFileSelection *filesel;
const gchar *filename;
FILE *file;
GimpGradientSegment *seg;
gchar buf[3][G_ASCII_DTOSTR_BUF_SIZE];
GtkFileSelection *filesel;
const gchar *filename;
GError *error = NULL;
filesel = GTK_FILE_SELECTION (gtk_widget_get_toplevel (widget));
filename = gtk_file_selection_get_filename (filesel);
file = fopen (filename, "wb");
if (!file)
{
g_message ("Could not open \"%s\"", filename);
}
else
if (! gimp_gradient_save_as_pov (gradient, filename, &error))
{
fprintf (file, "/* color_map file created by the GIMP */\n");
fprintf (file, "/* http://www.gimp.org/ */\n");
fprintf (file, "color_map {\n");
for (seg = gradient->segments; seg; seg = seg->next)
{
/* Left */
g_ascii_formatd (buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->left_color.r);
g_ascii_formatd (buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->left_color.g);
g_ascii_formatd (buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->left_color.b);
g_ascii_formatd (buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
1.0 - seg->left_color.a);
fprintf (file, "\t[%f color rgbt <%s, %s, %s, %s>]\n",
seg->left,
buf[0], buf[1], buf[2], buf[3]);
/* Middle */
g_ascii_formatd (buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
(seg->left_color.r + seg->right_color.r) / 2.0);
g_ascii_formatd (buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
(seg->left_color.g + seg->right_color.g) / 2.0);
g_ascii_formatd (buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
(seg->left_color.b + seg->right_color.b) / 2.0);
g_ascii_formatd (buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
1.0 - (seg->left_color.a + seg->right_color.a) / 2.0);
fprintf (file, "\t[%f color rgbt <%s, %s, %s, %s>]\n",
seg->middle,
buf[0], buf[1], buf[2], buf[3]);
/* Right */
g_ascii_formatd (buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->right_color.r);
g_ascii_formatd (buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->right_color.g);
g_ascii_formatd (buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
seg->right_color.b);
g_ascii_formatd (buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
1.0 - seg->right_color.a);
fprintf (file, "\t[%f color rgbt <%s, %s, %s, %s>]\n",
seg->right,
buf[0], buf[1], buf[2], buf[3]);
}
fprintf (file, "} /* color_map */\n");
fclose (file);
g_message (error->message);
g_clear_error (&error);
}
gtk_widget_destroy (GTK_WIDGET (filesel));
......
......@@ -579,7 +579,7 @@ gimp_initialize (Gimp *gimp,
gimp->brush_factory =
gimp_data_factory_new (gimp,
GIMP_TYPE_BRUSH,
(const gchar **) &gimp->config->brush_path,
"brush_path",
brush_loader_entries,
G_N_ELEMENTS (brush_loader_entries),
gimp_brush_new,
......@@ -589,7 +589,7 @@ gimp_initialize (Gimp *gimp,
gimp->pattern_factory =
gimp_data_factory_new (gimp,
GIMP_TYPE_PATTERN,
(const gchar **) &gimp->config->pattern_path,
"pattern_path",
pattern_loader_entries,
G_N_ELEMENTS (pattern_loader_entries),
gimp_pattern_new,
......@@ -599,7 +599,7 @@ gimp_initialize (Gimp *gimp,
gimp->gradient_factory =
gimp_data_factory_new (gimp,
GIMP_TYPE_GRADIENT,
(const gchar **) &gimp->config->gradient_path,
"gradient_path",
gradient_loader_entries,
G_N_ELEMENTS (gradient_loader_entries),
gimp_gradient_new,
......@@ -609,7 +609,7 @@ gimp_initialize (Gimp *gimp,
gimp->palette_factory =
gimp_data_factory_new (gimp,
GIMP_TYPE_PALETTE,
(const gchar **) &gimp->config->palette_path,
"palette_path",
palette_loader_entries,
G_N_ELEMENTS (palette_loader_entries),
gimp_palette_new,
......
......@@ -18,6 +18,7 @@
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
......@@ -357,19 +358,26 @@ gimp_brush_get_standard (void)
}
GimpData *
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use)
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpBrush *brush;
gint fd;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
fd = open (filename, O_RDONLY | _O_BINARY);
if (fd == -1)
return NULL;
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for reading: %s"),
filename, g_strerror (errno));
return NULL;
}
brush = gimp_brush_load_brush (fd, filename);
brush = gimp_brush_load_brush (fd, filename, error);
close (fd);
......@@ -387,6 +395,8 @@ gimp_brush_load (const gchar *filename,
temp_buf_swap (brush->pixmap);
}
GIMP_DATA (brush)->dirty = FALSE;
return GIMP_DATA (brush);
}
......@@ -483,8 +493,9 @@ gimp_brush_spacing_changed (GimpBrush *brush)
}
GimpBrush *
gimp_brush_load_brush (gint fd,
const gchar *filename)
gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error)
{
GimpBrush *brush;
gint bn_size;
......@@ -494,10 +505,16 @@ gimp_brush_load_brush (gint fd,
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (fd != -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
/* Read in the header size */
if (read (fd, &header, sizeof (header)) != sizeof (header))
return NULL;
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Could not read %d bytes from '%s': %s"),
sizeof (header), filename, g_strerror (errno));
return NULL;
}
/* rearrange the bytes in each unsigned int */
header.header_size = g_ntohl (header.header_size);
......@@ -513,9 +530,10 @@ gimp_brush_load_brush (gint fd,
if (header.version != 1 &&
(header.magic_number != GBRUSH_MAGIC || header.version != 2))
{
g_message (_("Fatal parsing error (unknown version %d):\n"
"Brush file '%s'"),
header.version, filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error (unknown version %d):\n"
"Brush file '%s'"),
header.version, filename);
return NULL;
}
......@@ -534,8 +552,10 @@ gimp_brush_load_brush (gint fd,
name = g_new (gchar, bn_size);
if ((read (fd, name, bn_size)) < bn_size)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
return NULL;
}
......@@ -562,8 +582,10 @@ gimp_brush_load_brush (gint fd,
temp_buf_data (brush->mask), header.width * header.height) <
header.width * header.height)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
g_object_unref (G_OBJECT (brush));
return NULL;
......@@ -572,7 +594,7 @@ gimp_brush_load_brush (gint fd,
case 4:
brush = GIMP_BRUSH (g_object_new (GIMP_TYPE_BRUSH, NULL));
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
brush->pixmap = temp_buf_new (header.width, header.height, 3, 0, 0, NULL);
for (i = 0; i < header.width * header.height; i++)
......@@ -581,8 +603,10 @@ gimp_brush_load_brush (gint fd,
+ i * 3, 3) != 3 ||
read (fd, temp_buf_data (brush->mask) + i, 1) != 1)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
g_object_unref (G_OBJECT (brush));
return NULL;
......@@ -591,16 +615,16 @@ gimp_brush_load_brush (gint fd,
break;
default:
g_message ("Unsupported brush depth %d\n"
"in file '%s'.\n"
"GIMP brushes must be GRAY or RGBA.",
header.bytes, filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Unsupported brush depth %d\n"
"in file '%s'.\n"
"GIMP brushes must be GRAY or RGBA."),
header.bytes, filename);
g_free (name);
return NULL;
}
gimp_object_set_name (GIMP_OBJECT (brush), name);
g_free (name);
brush->spacing = header.spacing;
......
......@@ -18,6 +18,7 @@
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
......@@ -357,19 +358,26 @@ gimp_brush_get_standard (void)
}
GimpData *
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use)
gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpBrush *brush;
gint fd;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
fd = open (filename, O_RDONLY | _O_BINARY);
if (fd == -1)
return NULL;
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for reading: %s"),
filename, g_strerror (errno));
return NULL;
}
brush = gimp_brush_load_brush (fd, filename);
brush = gimp_brush_load_brush (fd, filename, error);
close (fd);
......@@ -387,6 +395,8 @@ gimp_brush_load (const gchar *filename,
temp_buf_swap (brush->pixmap);
}
GIMP_DATA (brush)->dirty = FALSE;
return GIMP_DATA (brush);
}
......@@ -483,8 +493,9 @@ gimp_brush_spacing_changed (GimpBrush *brush)
}
GimpBrush *
gimp_brush_load_brush (gint fd,
const gchar *filename)
gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error)
{
GimpBrush *brush;
gint bn_size;
......@@ -494,10 +505,16 @@ gimp_brush_load_brush (gint fd,
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (fd != -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
/* Read in the header size */
if (read (fd, &header, sizeof (header)) != sizeof (header))
return NULL;
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Could not read %d bytes from '%s': %s"),
sizeof (header), filename, g_strerror (errno));
return NULL;
}
/* rearrange the bytes in each unsigned int */
header.header_size = g_ntohl (header.header_size);
......@@ -513,9 +530,10 @@ gimp_brush_load_brush (gint fd,
if (header.version != 1 &&
(header.magic_number != GBRUSH_MAGIC || header.version != 2))
{
g_message (_("Fatal parsing error (unknown version %d):\n"
"Brush file '%s'"),
header.version, filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error (unknown version %d):\n"
"Brush file '%s'"),
header.version, filename);
return NULL;
}
......@@ -534,8 +552,10 @@ gimp_brush_load_brush (gint fd,
name = g_new (gchar, bn_size);
if ((read (fd, name, bn_size)) < bn_size)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
return NULL;
}
......@@ -562,8 +582,10 @@ gimp_brush_load_brush (gint fd,
temp_buf_data (brush->mask), header.width * header.height) <
header.width * header.height)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
g_object_unref (G_OBJECT (brush));
return NULL;
......@@ -572,7 +594,7 @@ gimp_brush_load_brush (gint fd,
case 4:
brush = GIMP_BRUSH (g_object_new (GIMP_TYPE_BRUSH, NULL));
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
brush->mask = temp_buf_new (header.width, header.height, 1, 0, 0, NULL);
brush->pixmap = temp_buf_new (header.width, header.height, 3, 0, 0, NULL);
for (i = 0; i < header.width * header.height; i++)
......@@ -581,8 +603,10 @@ gimp_brush_load_brush (gint fd,
+ i * 3, 3) != 3 ||
read (fd, temp_buf_data (brush->mask) + i, 1) != 1)
{
g_message (_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."), filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Fatal parsing error:\n"
"Brush file '%s' appears truncated."),
filename);
g_free (name);
g_object_unref (G_OBJECT (brush));
return NULL;
......@@ -591,16 +615,16 @@ gimp_brush_load_brush (gint fd,
break;
default:
g_message ("Unsupported brush depth %d\n"
"in file '%s'.\n"
"GIMP brushes must be GRAY or RGBA.",
header.bytes, filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Unsupported brush depth %d\n"
"in file '%s'.\n"
"GIMP brushes must be GRAY or RGBA."),
header.bytes, filename);
g_free (name);
return NULL;
}
gimp_object_set_name (GIMP_OBJECT (brush), name);
g_free (name);
brush->spacing = header.spacing;
......
......@@ -68,29 +68,31 @@ struct _GimpBrushClass
GType gimp_brush_get_type (void) G_GNUC_CONST;
GimpData * gimp_brush_new (const gchar *name,
gboolean stingy_memory_use);
GimpData * gimp_brush_new (const gchar *name,
gboolean stingy_memory_use);
GimpData * gimp_brush_get_standard (void);
GimpData * gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use);
GimpBrush * gimp_brush_load_brush (gint fd,
const gchar *filename);
GimpBrush * gimp_brush_select_brush (GimpBrush *brush,
GimpCoords *last_coords,
GimpCoords *cur_coords);
gboolean gimp_brush_want_null_motion (GimpBrush *brush,
GimpCoords *last_coords,
GimpCoords *cur_coords);
TempBuf * gimp_brush_get_mask (const GimpBrush *brush);
TempBuf * gimp_brush_get_pixmap (const GimpBrush *brush);
gint gimp_brush_get_spacing (const GimpBrush *brush);
void gimp_brush_set_spacing (GimpBrush *brush,
gint spacing);
void gimp_brush_spacing_changed (GimpBrush *brush);
GimpData * gimp_brush_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error);
GimpBrush * gimp_brush_load_brush (gint fd,
const gchar *filename,
GError **error);
GimpBrush * gimp_brush_select_brush (GimpBrush *brush,
GimpCoords *last_coords,
GimpCoords *cur_coords);
gboolean gimp_brush_want_null_motion (GimpBrush *brush,
GimpCoords *last_coords,
GimpCoords *cur_coords);
TempBuf * gimp_brush_get_mask (const GimpBrush *brush);
TempBuf * gimp_brush_get_pixmap (const GimpBrush *brush);
gint gimp_brush_get_spacing (const GimpBrush *brush);
void gimp_brush_set_spacing (GimpBrush *brush,
gint spacing);
void gimp_brush_spacing_changed (GimpBrush *brush);
#endif /* __GIMP_BRUSH_H__ */
......@@ -20,6 +20,7 @@
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
......@@ -39,6 +40,8 @@
#include "gimpbrushgenerated.h"
#include "libgimp/gimpintl.h"
#define OVERSAMPLING 5
......@@ -47,11 +50,12 @@
static void gimp_brush_generated_class_init (GimpBrushGeneratedClass *klass);
static void gimp_brush_generated_init (GimpBrushGenerated *brush);
static gboolean gimp_brush_generated_save (GimpData *data);
static void gimp_brush_generated_dirty (GimpData *data);
static gchar * gimp_brush_generated_get_extension (GimpData *data);
static GimpData * gimp_brush_generated_duplicate (GimpData *data,
gboolean stingy_memory_use);
static gboolean gimp_brush_generated_save (GimpData *data,
GError **error);
static void gimp_brush_generated_dirty (GimpData *data);
static gchar * gimp_brush_generated_get_extension (GimpData *data);
static GimpData * gimp_brush_generated_duplicate (GimpData *data,
gboolean stingy_memory_use);
static GimpBrushClass *parent_class = NULL;
......@@ -111,21 +115,21 @@ gimp_brush_generated_init (GimpBrushGenerated *brush)
}
static gboolean
gimp_brush_generated_save (GimpData *data)
gimp_brush_generated_save (GimpData *data,
GError **error)
{
GimpBrushGenerated *brush;
FILE *fp;
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
g_return_val_if_fail (data != NULL, FALSE);
g_return_val_if_fail (GIMP_IS_BRUSH_GENERATED (data), FALSE);
brush = GIMP_BRUSH_GENERATED (data);
/* we are (finaly) ready to try to save the generated brush file */
if ((fp = fopen (data->filename, "wb")) == NULL)
{
g_warning ("Unable to save file %s", data->filename);
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for writing: %s"),
data->filename, g_strerror (errno));
return FALSE;
}
......@@ -369,26 +373,45 @@ gimp_brush_generated_new (gfloat radius,
}
GimpData *
gimp_brush_generated_load (const gchar *filename,
gboolean stingy_memory_use)
gimp_brush_generated_load (const gchar *filename,
gboolean stingy_memory_use,
GError **error)
{
GimpBrushGenerated *brush;
FILE *fp;
gchar string[256];
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if ((fp = fopen (filename, "rb")) == NULL)
return NULL;
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for reading: %s"),
filename, g_strerror (errno));
return NULL;