Emit an error if image loading fails

Instead of spamming stderr with g_warning, use the new
emit_error method of the GtkStyleProviderPrivate interface
to emit an error if loading an image fails.
This commit is contained in:
Matthias Clasen 2016-01-29 22:47:38 -05:00
parent 2c7fdf6432
commit 0fe468c789
2 changed files with 72 additions and 34 deletions

View File

@ -99,35 +99,46 @@ lookup_symbolic_colors (GtkCssStyle *style,
}
static GtkCssImage *
gtk_css_image_recolor_load (GtkCssImageRecolor *recolor,
GtkCssStyle *style,
GtkCssValue *palette,
gint scale)
gtk_css_image_recolor_load (GtkCssImageRecolor *recolor,
GtkCssStyle *style,
GtkCssValue *palette,
gint scale,
GError **gerror)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (recolor);
GtkIconInfo *info;
GdkRGBA fg, success, warning, error;
GdkPixbuf *pixbuf;
GtkCssImage *image;
GError *gerror = NULL;
GError *local_error = NULL;
lookup_symbolic_colors (style, palette, &fg, &success, &warning, &error);
info = gtk_icon_info_new_for_file (url->file, 0, scale);
pixbuf = gtk_icon_info_load_symbolic (info, &fg, &success, &warning, &error, NULL, &gerror);
pixbuf = gtk_icon_info_load_symbolic (info, &fg, &success, &warning, &error, NULL, &local_error);
g_object_unref (info);
if (pixbuf == NULL)
{
char *uri;
cairo_surface_t *empty;
/* XXX: Get the error somehow back to the CssProvider */
uri = g_file_get_uri (url->file);
g_warning ("Error loading image '%s': %s\n", uri, gerror->message);
g_error_free (gerror);
g_free (uri);
if (gerror)
{
char *uri;
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), "image-missing", 24, 0, NULL);
uri = g_file_get_uri (url->file);
g_set_error (gerror,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Error loading image '%s': %s", uri, local_error->message);
g_error_free (local_error);
g_free (uri);
}
empty = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
image = _gtk_css_image_surface_new (empty);
cairo_surface_destroy (empty);
return image;
}
image = _gtk_css_image_surface_new_for_pixbuf (pixbuf);
@ -147,6 +158,7 @@ gtk_css_image_recolor_compute (GtkCssImage *image,
GtkCssValue *palette;
GtkCssImage *img;
int scale;
GError *error = NULL;
scale = _gtk_style_provider_private_get_scale (provider);
@ -155,7 +167,14 @@ gtk_css_image_recolor_compute (GtkCssImage *image,
else
palette = _gtk_css_value_ref (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_PALETTE));
img = gtk_css_image_recolor_load (recolor, style, palette, scale);
img = gtk_css_image_recolor_load (recolor, style, palette, scale, &error);
if (error)
{
GtkCssSection *section = gtk_css_style_get_section (style, property_id);
_gtk_style_provider_private_emit_error (provider, section, error);
g_error_free (error);
}
_gtk_css_value_unref (palette);

View File

@ -23,14 +23,16 @@
#include "gtkcssimageurlprivate.h"
#include "gtkcssimagesurfaceprivate.h"
#include "gtkstyleproviderprivate.h"
G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
static GtkCssImage *
gtk_css_image_url_load_image (GtkCssImageUrl *url)
gtk_css_image_url_load_image (GtkCssImageUrl *url,
GError **error)
{
GdkPixbuf *pixbuf;
GError *error = NULL;
GError *local_error = NULL;
GFileInputStream *input;
if (url->loaded_image)
@ -44,16 +46,16 @@ gtk_css_image_url_load_image (GtkCssImageUrl *url)
char *uri = g_file_get_uri (url->file);
char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
pixbuf = gdk_pixbuf_new_from_resource (resource_path, &local_error);
g_free (resource_path);
g_free (uri);
}
else
{
input = g_file_read (url->file, NULL, &error);
input = g_file_read (url->file, NULL, &local_error);
if (input != NULL)
{
pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &local_error);
g_object_unref (input);
}
else
@ -64,18 +66,25 @@ gtk_css_image_url_load_image (GtkCssImageUrl *url)
if (pixbuf == NULL)
{
cairo_surface_t *empty = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
char *uri;
cairo_surface_t *empty;
/* XXX: Can we get the error somehow sent to the CssProvider?
* I don't like just dumping it to stderr or losing it completely. */
uri = g_file_get_uri (url->file);
g_warning ("Error loading image '%s': %s", uri, error->message);
g_error_free (error);
g_free (uri);
if (error)
{
char *uri;
uri = g_file_get_uri (url->file);
g_set_error (error,
GTK_CSS_PROVIDER_ERROR,
GTK_CSS_PROVIDER_ERROR_FAILED,
"Error loading image '%s': %s", uri, local_error->message);
g_error_free (local_error);
g_free (uri);
}
empty = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
url->loaded_image = _gtk_css_image_surface_new (empty);
cairo_surface_destroy (empty);
return url->loaded_image;
return url->loaded_image;
}
url->loaded_image = _gtk_css_image_surface_new_for_pixbuf (pixbuf);
@ -89,7 +98,7 @@ gtk_css_image_url_get_width (GtkCssImage *image)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
return _gtk_css_image_get_width (gtk_css_image_url_load_image (url));
return _gtk_css_image_get_width (gtk_css_image_url_load_image (url, NULL));
}
static int
@ -97,7 +106,7 @@ gtk_css_image_url_get_height (GtkCssImage *image)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
return _gtk_css_image_get_height (gtk_css_image_url_load_image (url));
return _gtk_css_image_get_height (gtk_css_image_url_load_image (url, NULL));
}
static double
@ -105,7 +114,7 @@ gtk_css_image_url_get_aspect_ratio (GtkCssImage *image)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
return _gtk_css_image_get_aspect_ratio (gtk_css_image_url_load_image (url));
return _gtk_css_image_get_aspect_ratio (gtk_css_image_url_load_image (url, NULL));
}
static void
@ -116,7 +125,7 @@ gtk_css_image_url_draw (GtkCssImage *image,
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
_gtk_css_image_draw (gtk_css_image_url_load_image (url), cr, width, height);
_gtk_css_image_draw (gtk_css_image_url_load_image (url, NULL), cr, width, height);
}
static GtkCssImage *
@ -127,8 +136,18 @@ gtk_css_image_url_compute (GtkCssImage *image,
GtkCssStyle *parent_style)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
GtkCssImage *copy;
GError *error = NULL;
return g_object_ref (gtk_css_image_url_load_image (url));
copy = gtk_css_image_url_load_image (url, &error);
if (error)
{
GtkCssSection *section = gtk_css_style_get_section (style, property_id);
_gtk_style_provider_private_emit_error (provider, section, error);
g_error_free (error);
}
return g_object_ref (copy);
}
static gboolean
@ -150,7 +169,7 @@ gtk_css_image_url_print (GtkCssImage *image,
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
_gtk_css_image_print (gtk_css_image_url_load_image (url), string);
_gtk_css_image_print (gtk_css_image_url_load_image (url, NULL), string);
}
static void