From 39c6d0512e3605aaafb1285d1f3627e3f92ec104 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Tue, 11 Nov 2008 15:25:40 +0000 Subject: Don't automatically resize and png-ify avatars. svn path=/trunk/; revision=1669 --- libempathy-gtk/empathy-avatar-chooser.c | 71 +++++++++++++++++---------------- libempathy-gtk/empathy-avatar-image.c | 2 +- libempathy-gtk/empathy-ui-utils.c | 30 ++++++++++---- libempathy-gtk/empathy-ui-utils.h | 3 +- 4 files changed, 62 insertions(+), 44 deletions(-) diff --git a/libempathy-gtk/empathy-avatar-chooser.c b/libempathy-gtk/empathy-avatar-chooser.c index 08dbfad18..4a181d3f2 100644 --- a/libempathy-gtk/empathy-avatar-chooser.c +++ b/libempathy-gtk/empathy-avatar-chooser.c @@ -166,39 +166,30 @@ avatar_chooser_finalize (GObject *object) } static void -avatar_chooser_set_pixbuf (EmpathyAvatarChooser *chooser, - GdkPixbuf *pixbuf) +avatar_chooser_set_image (EmpathyAvatarChooser *chooser, + GdkPixbuf *pixbuf, + gchar *image_data, + gsize image_data_size, + gchar *mime_type) { EmpathyAvatarChooserPriv *priv = GET_PRIV (chooser); GtkWidget *image; GdkPixbuf *pixbuf_view = NULL; - GdkPixbuf *pixbuf_save = NULL; - GError *error = NULL; g_free (priv->image_data); + g_free (priv->mime_type); + priv->image_data = NULL; priv->image_data_size = 0; - g_free (priv->mime_type); priv->mime_type = NULL; if (pixbuf) { + priv->image_data = image_data; + priv->image_data_size = image_data_size; + priv->mime_type = mime_type; + pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW); - pixbuf_save = empathy_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE); - - if (!gdk_pixbuf_save_to_buffer (pixbuf_save, - &priv->image_data, - &priv->image_data_size, - "png", - &error, NULL)) { - DEBUG ("Failed to save pixbuf: %s", - error ? error->message : "No error given"); - g_clear_error (&error); - } else { - priv->mime_type = "image/png"; - } image = gtk_image_new_from_pixbuf (pixbuf_view); - - g_object_unref (pixbuf_save); g_object_unref (pixbuf_view); } else { image = gtk_image_new_from_icon_name ("stock_person", @@ -209,23 +200,31 @@ avatar_chooser_set_pixbuf (EmpathyAvatarChooser *chooser, g_signal_emit (chooser, signals[CHANGED], 0); } +static void +avatar_chooser_clear_image (EmpathyAvatarChooser *chooser) +{ + avatar_chooser_set_image (chooser, NULL, NULL, 0, NULL); +} + static void avatar_chooser_set_image_from_file (EmpathyAvatarChooser *chooser, const gchar *filename) { - GdkPixbuf *pixbuf; - GError *error = NULL; + gchar *image_data = NULL; + gsize image_size = 0; + GError *error = NULL; - if (!(pixbuf = gdk_pixbuf_new_from_file (filename, &error))) { - DEBUG ("Failed to load pixbuf from file: %s", + if (!g_file_get_contents (filename, &image_data, &image_size, &error)) { + DEBUG ("Failed to load image from '%s': %s", filename, error ? error->message : "No error given"); + + avatar_chooser_clear_image (chooser); + g_clear_error (&error); + return; } - avatar_chooser_set_pixbuf (chooser, pixbuf); - if (pixbuf) { - g_object_unref (pixbuf); - } + avatar_chooser_set_image_from_data (chooser, image_data, image_size); } static void @@ -233,10 +232,11 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *chooser, gchar *data, gsize size) { - GdkPixbuf *pixbuf; + GdkPixbuf *pixbuf; + gchar *mime_type = NULL; - pixbuf = empathy_pixbuf_from_data (data, size); - avatar_chooser_set_pixbuf (chooser, pixbuf); + pixbuf = empathy_pixbuf_from_data (data, size, &mime_type); + avatar_chooser_set_image (chooser, pixbuf, data, size, mime_type); if (pixbuf) { g_object_unref (pixbuf); } @@ -553,9 +553,12 @@ empathy_avatar_chooser_set (EmpathyAvatarChooser *chooser, { g_return_if_fail (EMPATHY_IS_AVATAR_CHOOSER (chooser)); - avatar_chooser_set_image_from_data (chooser, - avatar ? avatar->data : NULL, - avatar ? avatar->len : 0); + if (avatar != NULL) { + gchar *data = g_memdup (avatar->data, avatar->len); + avatar_chooser_set_image_from_data (chooser, data, avatar->len); + } else { + avatar_chooser_clear_image (chooser); + } } void diff --git a/libempathy-gtk/empathy-avatar-image.c b/libempathy-gtk/empathy-avatar-image.c index 7dd93aad9..6b378b606 100644 --- a/libempathy-gtk/empathy-avatar-image.c +++ b/libempathy-gtk/empathy-avatar-image.c @@ -276,7 +276,7 @@ empathy_avatar_image_set (EmpathyAvatarImage *avatar_image, } if (avatar) { - priv->pixbuf = empathy_pixbuf_from_data (avatar->data, avatar->len); + priv->pixbuf = empathy_pixbuf_from_data (avatar->data, avatar->len, NULL); } if (!priv->pixbuf) { diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c index ae3675145..bf391ccc2 100644 --- a/libempathy-gtk/empathy-ui-utils.c +++ b/libempathy-gtk/empathy-ui-utils.c @@ -245,11 +245,14 @@ empathy_icon_name_for_contact (EmpathyContact *contact) } GdkPixbuf * -empathy_pixbuf_from_data (gchar *data, - gsize data_size) +empathy_pixbuf_from_data (gchar *data, + gsize data_size, + gchar **mime_type) { GdkPixbufLoader *loader; + GdkPixbufFormat *format; GdkPixbuf *pixbuf = NULL; + gchar **mime_types; GError *error = NULL; if (!data) { @@ -260,23 +263,34 @@ empathy_pixbuf_from_data (gchar *data, if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) { DEBUG ("Failed to write to pixbuf loader: %s", error ? error->message : "No error given"); - g_clear_error (&error); - g_object_unref (loader); - return NULL; + goto out; } if (!gdk_pixbuf_loader_close (loader, &error)) { DEBUG ("Failed to close pixbuf loader: %s", error ? error->message : "No error given"); - g_clear_error (&error); - g_object_unref (loader); - return NULL; + goto out; } pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (pixbuf) { g_object_ref (pixbuf); + + if (mime_type != NULL) { + format = gdk_pixbuf_loader_get_format (loader); + mime_types = gdk_pixbuf_format_get_mime_types (format); + + *mime_type = g_strdup (*mime_types); + if (*(mime_types + 1) != NULL) { + DEBUG ("Loader supports more than one mime " + "type! Picking the first one, %s", + *mime_type); + } + g_strfreev (mime_types); + } } +out: + g_clear_error (&error); g_object_unref (loader); return pixbuf; diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h index 24ab01fb4..be87b94c7 100644 --- a/libempathy-gtk/empathy-ui-utils.h +++ b/libempathy-gtk/empathy-ui-utils.h @@ -69,7 +69,8 @@ const gchar * empathy_icon_name_from_account (McAccount *accou const gchar * empathy_icon_name_for_presence (McPresence presence); const gchar * empathy_icon_name_for_contact (EmpathyContact *contact); GdkPixbuf * empathy_pixbuf_from_data (gchar *data, - gsize data_size); + gsize data_size, + gchar **mime_type); GdkPixbuf * empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar, gint width, gint height); -- cgit v1.2.3