From a56fa2a95e93ad68ca08f336178b26baf3b3f29d Mon Sep 17 00:00:00 2001 From: Marco Barisione Date: Tue, 30 Jul 2013 16:08:34 +0100 Subject: pixbuf-utils: copy the generic pixbuf-related utily functions from Empathy This commit also changes the licence of the moved code from GPL to LGPL. See GOSSIP-RELICENSING.txt for details. https://bugzilla.gnome.org/show_bug.cgi?id=699492 --- tp-account-widgets/Makefile.am | 2 + tp-account-widgets/tpaw-avatar-chooser.c | 9 +- tp-account-widgets/tpaw-pixbuf-utils.c | 183 +++++++++++++++++++++++++++++++ tp-account-widgets/tpaw-pixbuf-utils.h | 48 ++++++++ 4 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 tp-account-widgets/tpaw-pixbuf-utils.c create mode 100644 tp-account-widgets/tpaw-pixbuf-utils.h (limited to 'tp-account-widgets') diff --git a/tp-account-widgets/Makefile.am b/tp-account-widgets/Makefile.am index fa79032f..968e6261 100644 --- a/tp-account-widgets/Makefile.am +++ b/tp-account-widgets/Makefile.am @@ -42,6 +42,7 @@ libtp_account_widgets_sources = \ tpaw-irc-network.c \ tpaw-irc-server.c \ tpaw-live-search.c \ + tpaw-pixbuf-utils.c \ tpaw-string-parser.c \ tpaw-time.c \ tpaw-user-info.c \ @@ -70,6 +71,7 @@ libtp_account_widgets_headers = \ tpaw-irc-network.h \ tpaw-irc-server.h \ tpaw-live-search.h \ + tpaw-pixbuf-utils.h \ tpaw-string-parser.h \ tpaw-time.h \ tpaw-user-info.h \ diff --git a/tp-account-widgets/tpaw-avatar-chooser.c b/tp-account-widgets/tpaw-avatar-chooser.c index 6a5b83b4..77d4a0b7 100644 --- a/tp-account-widgets/tpaw-avatar-chooser.c +++ b/tp-account-widgets/tpaw-avatar-chooser.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #ifdef HAVE_CHEESE @@ -145,7 +146,7 @@ get_avatar_cb (GObject *source, goto out; } - pixbuf = empathy_pixbuf_from_data_and_mime ((gchar *) avatar->data, + pixbuf = tpaw_pixbuf_from_data_and_mime ((gchar *) avatar->data, avatar->len, &mime_type); if (pixbuf == NULL) { @@ -725,7 +726,7 @@ avatar_chooser_set_image (TpawAvatarChooser *self, self->priv->changed = TRUE; - pixbuf_view = empathy_pixbuf_scale_down_if_necessary (pixbuf, + pixbuf_view = tpaw_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_VIEW); image = gtk_image_new_from_pixbuf (pixbuf_view); @@ -751,7 +752,7 @@ avatar_chooser_set_image_from_data (TpawAvatarChooser *self, return; } - pixbuf = empathy_pixbuf_from_data_and_mime (data, size, &mime_type); + pixbuf = tpaw_pixbuf_from_data_and_mime (data, size, &mime_type); if (pixbuf == NULL) { g_free (data); @@ -844,7 +845,7 @@ avatar_chooser_update_preview_cb (GtkFileChooser *file_chooser, if (pixbuf != NULL) { - scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (pixbuf, + scaled_pixbuf = tpaw_pixbuf_scale_down_if_necessary (pixbuf, AVATAR_SIZE_SAVE); gtk_image_set_from_pixbuf (GTK_IMAGE (image), scaled_pixbuf); diff --git a/tp-account-widgets/tpaw-pixbuf-utils.c b/tp-account-widgets/tpaw-pixbuf-utils.c new file mode 100644 index 00000000..1b226e92 --- /dev/null +++ b/tp-account-widgets/tpaw-pixbuf-utils.c @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2002-2007 Imendio AB + * Copyright (C) 2007-2013 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Mikael Hallendal + * Richard Hult + * Martyn Russell + * Xavier Claessens + * Travis Reitter + */ + +#include "config.h" +#include "tpaw-pixbuf-utils.h" + +#include + +#define DEBUG_FLAG EMPATHY_DEBUG_OTHER +#include "empathy-debug.h" + +GdkPixbuf * +tpaw_pixbuf_from_data (gchar *data, + gsize data_size) +{ + return tpaw_pixbuf_from_data_and_mime (data, data_size, NULL); +} + +GdkPixbuf * +tpaw_pixbuf_from_data_and_mime (gchar *data, + gsize data_size, + gchar **mime_type) +{ + GdkPixbufLoader *loader; + GdkPixbufFormat *format; + GdkPixbuf *pixbuf = NULL; + gchar **mime_types; + GError *error = NULL; + + if (!data) + return NULL; + + loader = gdk_pixbuf_loader_new (); + if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) + { + DEBUG ("Failed to write to pixbuf loader: %s", + error ? error->message : "No error given"); + goto out; + } + + if (!gdk_pixbuf_loader_close (loader, &error)) + { + DEBUG ("Failed to close pixbuf loader: %s", + error ? error->message : "No error given"); + 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; +} + +GdkPixbuf * +tpaw_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, + gint max_size) +{ + gint width, height; + gdouble factor; + + width = gdk_pixbuf_get_width (pixbuf); + height = gdk_pixbuf_get_height (pixbuf); + + if (width > 0 && (width > max_size || height > max_size)) + { + factor = (gdouble) max_size / MAX (width, height); + + width = width * factor; + height = height * factor; + + return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_HYPER); + } + + return g_object_ref (pixbuf); +} + +GdkPixbuf * +tpaw_pixbuf_from_icon_name_sized (const gchar *icon_name, + gint size) +{ + GtkIconTheme *theme; + GdkPixbuf *pixbuf; + GError *error = NULL; + + if (!icon_name) + return NULL; + + theme = gtk_icon_theme_get_default (); + + pixbuf = gtk_icon_theme_load_icon (theme, icon_name, size, 0, &error); + + if (error) + { + DEBUG ("Error loading icon: %s", error->message); + g_clear_error (&error); + } + + return pixbuf; +} + +GdkPixbuf * +tpaw_pixbuf_from_icon_name (const gchar *icon_name, + GtkIconSize icon_size) +{ + gint w, h; + gint size = 48; + + if (!icon_name) + return NULL; + + if (gtk_icon_size_lookup (icon_size, &w, &h)) + size = (w + h) / 2; + + return tpaw_pixbuf_from_icon_name_sized (icon_name, size); +} + +gchar * +tpaw_filename_from_icon_name (const gchar *icon_name, + GtkIconSize icon_size) +{ + GtkIconTheme *icon_theme; + GtkIconInfo *icon_info; + gint w, h; + gint size = 48; + gchar *ret; + + icon_theme = gtk_icon_theme_get_default (); + + if (gtk_icon_size_lookup (icon_size, &w, &h)) + size = (w + h) / 2; + + icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0); + if (icon_info == NULL) + return NULL; + + ret = g_strdup (gtk_icon_info_get_filename (icon_info)); + gtk_icon_info_free (icon_info); + + return ret; +} diff --git a/tp-account-widgets/tpaw-pixbuf-utils.h b/tp-account-widgets/tpaw-pixbuf-utils.h new file mode 100644 index 00000000..0521f71a --- /dev/null +++ b/tp-account-widgets/tpaw-pixbuf-utils.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2002-2007 Imendio AB + * Copyright (C) 2007-2013 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * Authors: Mikael Hallendal + * Richard Hult + * Martyn Russell + * Xavier Claessens + * Travis Reitter + */ + +#ifndef __TPAW_UI_UTILS_H__ +#define __TPAW_UI_UTILS_H__ + +#include + +G_BEGIN_DECLS + +GdkPixbuf * tpaw_pixbuf_from_data (gchar *data, + gsize data_size); +GdkPixbuf * tpaw_pixbuf_from_data_and_mime (gchar *data, + gsize data_size, + gchar **mime_type); +GdkPixbuf * tpaw_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, + gint max_size); +GdkPixbuf * tpaw_pixbuf_from_icon_name (const gchar *icon_name, + GtkIconSize icon_size); +GdkPixbuf * tpaw_pixbuf_from_icon_name_sized (const gchar *icon_name, + gint size); +gchar * tpaw_filename_from_icon_name (const gchar *icon_name, + GtkIconSize icon_size); + +G_END_DECLS + +#endif /* __TPAW_UI_UTILS_H__ */ -- cgit v1.2.1