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 --- libempathy-gtk/empathy-account-chooser.c | 5 +- libempathy-gtk/empathy-avatar-image.c | 7 +- libempathy-gtk/empathy-individual-store-channel.c | 4 +- libempathy-gtk/empathy-individual-view.c | 5 +- .../empathy-local-xmpp-assistant-widget.c | 3 +- libempathy-gtk/empathy-notify-manager.c | 3 +- libempathy-gtk/empathy-protocol-chooser.c | 7 +- libempathy-gtk/empathy-roster-contact.c | 3 +- libempathy-gtk/empathy-smiley-manager.c | 5 +- libempathy-gtk/empathy-theme-adium.c | 3 +- libempathy-gtk/empathy-ui-utils.c | 156 +----------------- libempathy-gtk/empathy-ui-utils.h | 13 -- src/empathy-accounts-dialog.c | 3 +- src/empathy-call-window.c | 3 +- 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 ++++++ 18 files changed, 272 insertions(+), 190 deletions(-) create mode 100644 tp-account-widgets/tpaw-pixbuf-utils.c create mode 100644 tp-account-widgets/tpaw-pixbuf-utils.h diff --git a/libempathy-gtk/empathy-account-chooser.c b/libempathy-gtk/empathy-account-chooser.c index e6f4367c9..69fc54fe2 100644 --- a/libempathy-gtk/empathy-account-chooser.c +++ b/libempathy-gtk/empathy-account-chooser.c @@ -25,8 +25,7 @@ #include "empathy-account-chooser.h" #include - -#include "empathy-ui-utils.h" +#include #define DEBUG_FLAG EMPATHY_DEBUG_OTHER #include "empathy-debug.h" @@ -840,7 +839,7 @@ account_chooser_filter_ready_cb (gboolean is_enabled, store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox)); icon_name = tp_account_get_icon_name (account); - pixbuf = empathy_pixbuf_from_icon_name (icon_name, + pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON); gtk_list_store_set (store, iter, diff --git a/libempathy-gtk/empathy-avatar-image.c b/libempathy-gtk/empathy-avatar-image.c index f61c1b236..78a3e5ff3 100644 --- a/libempathy-gtk/empathy-avatar-image.c +++ b/libempathy-gtk/empathy-avatar-image.c @@ -26,6 +26,7 @@ #include #include +#include #include "empathy-ui-utils.h" #include "empathy-utils.h" @@ -211,7 +212,7 @@ avatar_image_button_press_event (GtkWidget *widget, GdkEventButton *event) return TRUE; } - pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_LARGE); + pixbuf = tpaw_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_LARGE); popup_width = gdk_pixbuf_get_width (pixbuf); popup_height = gdk_pixbuf_get_height (pixbuf); @@ -302,7 +303,7 @@ empathy_avatar_image_set (EmpathyAvatarImage *avatar_image, } if (avatar) { - priv->pixbuf = empathy_pixbuf_from_data ((gchar *) avatar->data, + priv->pixbuf = tpaw_pixbuf_from_data ((gchar *) avatar->data, avatar->len); } @@ -311,7 +312,7 @@ empathy_avatar_image_set (EmpathyAvatarImage *avatar_image, return; } - scaled_pixbuf = empathy_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_SMALL); + scaled_pixbuf = tpaw_pixbuf_scale_down_if_necessary (priv->pixbuf, MAX_SMALL); gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled_pixbuf); if (scaled_pixbuf != priv->pixbuf) { diff --git a/libempathy-gtk/empathy-individual-store-channel.c b/libempathy-gtk/empathy-individual-store-channel.c index a1a1161f9..eb0f3f593 100644 --- a/libempathy-gtk/empathy-individual-store-channel.c +++ b/libempathy-gtk/empathy-individual-store-channel.c @@ -26,6 +26,8 @@ #include "config.h" #include "empathy-individual-store-channel.h" +#include + #include "empathy-utils.h" #include "empathy-ui-utils.h" #include "empathy-images.h" @@ -159,7 +161,7 @@ individual_store_channel_contact_chat_state_changed (TpTextChannel *channel, if (state == TP_CHANNEL_CHAT_STATE_COMPOSING) { gchar *icon_filename = - empathy_filename_from_icon_name (EMPATHY_IMAGE_TYPING, + tpaw_filename_from_icon_name (EMPATHY_IMAGE_TYPING, GTK_ICON_SIZE_MENU); pixbuf = gdk_pixbuf_new_from_file (icon_filename, NULL); diff --git a/libempathy-gtk/empathy-individual-view.c b/libempathy-gtk/empathy-individual-view.c index 22e65a0ec..b901848e1 100644 --- a/libempathy-gtk/empathy-individual-view.c +++ b/libempathy-gtk/empathy-individual-view.c @@ -28,6 +28,7 @@ #include "empathy-individual-view.h" #include +#include #include #include "empathy-cell-renderer-activatable.h" @@ -1161,12 +1162,12 @@ individual_view_group_icon_cell_data_func (GtkTreeViewColumn *tree_column, if (!tp_strdiff (name, EMPATHY_INDIVIDUAL_STORE_FAVORITE)) { - pixbuf = empathy_pixbuf_from_icon_name ("emblem-favorite", + pixbuf = tpaw_pixbuf_from_icon_name ("emblem-favorite", GTK_ICON_SIZE_MENU); } else if (!tp_strdiff (name, EMPATHY_INDIVIDUAL_STORE_PEOPLE_NEARBY)) { - pixbuf = empathy_pixbuf_from_icon_name ("im-local-xmpp", + pixbuf = tpaw_pixbuf_from_icon_name ("im-local-xmpp", GTK_ICON_SIZE_MENU); } diff --git a/libempathy-gtk/empathy-local-xmpp-assistant-widget.c b/libempathy-gtk/empathy-local-xmpp-assistant-widget.c index 971ad5e96..d5f044eba 100644 --- a/libempathy-gtk/empathy-local-xmpp-assistant-widget.c +++ b/libempathy-gtk/empathy-local-xmpp-assistant-widget.c @@ -23,6 +23,7 @@ #include #include +#include #include #include "empathy-ui-utils.h" @@ -87,7 +88,7 @@ empathy_local_xmpp_assistant_widget_constructed (GObject *object) gtk_grid_attach (GTK_GRID (self), w, 0, 0, 1, 1); gtk_widget_show (w); - pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 48); + pix = tpaw_pixbuf_from_icon_name_sized ("im-local-xmpp", 48); if (pix != NULL) { w = gtk_image_new_from_pixbuf (pix); diff --git a/libempathy-gtk/empathy-notify-manager.c b/libempathy-gtk/empathy-notify-manager.c index 033557e81..8ca97950e 100644 --- a/libempathy-gtk/empathy-notify-manager.c +++ b/libempathy-gtk/empathy-notify-manager.c @@ -21,6 +21,7 @@ #include "empathy-notify-manager.h" #include +#include #include "empathy-gsettings.h" #include "empathy-ui-utils.h" @@ -174,7 +175,7 @@ empathy_notify_manager_get_pixbuf_for_notification (EmpathyNotifyManager *self, pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48); if (pixbuf == NULL) - pixbuf = empathy_pixbuf_from_icon_name_sized (icon_name, 48); + pixbuf = tpaw_pixbuf_from_icon_name_sized (icon_name, 48); return pixbuf; } diff --git a/libempathy-gtk/empathy-protocol-chooser.c b/libempathy-gtk/empathy-protocol-chooser.c index f2414ad10..3966809f7 100644 --- a/libempathy-gtk/empathy-protocol-chooser.c +++ b/libempathy-gtk/empathy-protocol-chooser.c @@ -25,6 +25,7 @@ #include #include +#include #include #include "empathy-ui-utils.h" @@ -231,7 +232,7 @@ protocol_choosers_add_cm (EmpathyProtocolChooser *chooser, g_strdup (proto_name), g_strdup (cm_name)); icon_name = tpaw_protocol_icon_name (proto_name); - pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON); + pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON); display_name = tpaw_protocol_name_to_display_name (proto_name); @@ -249,7 +250,7 @@ protocol_choosers_add_cm (EmpathyProtocolChooser *chooser, !tp_strdiff (cm_name, "gabble")) { display_name = tpaw_service_name_to_display_name ("google-talk"); - pixbuf = empathy_pixbuf_from_icon_name ("im-google-talk", + pixbuf = tpaw_pixbuf_from_icon_name ("im-google-talk", GTK_ICON_SIZE_BUTTON); gtk_list_store_insert_with_values (priv->store, @@ -264,7 +265,7 @@ protocol_choosers_add_cm (EmpathyProtocolChooser *chooser, g_clear_object (&pixbuf); display_name = tpaw_service_name_to_display_name ("facebook"); - pixbuf = empathy_pixbuf_from_icon_name ("im-facebook", + pixbuf = tpaw_pixbuf_from_icon_name ("im-facebook", GTK_ICON_SIZE_BUTTON); gtk_list_store_insert_with_values (priv->store, diff --git a/libempathy-gtk/empathy-roster-contact.c b/libempathy-gtk/empathy-roster-contact.c index 22b360075..328b51240 100644 --- a/libempathy-gtk/empathy-roster-contact.c +++ b/libempathy-gtk/empathy-roster-contact.c @@ -3,6 +3,7 @@ #include #include +#include #include "empathy-ui-utils.h" #include "empathy-utils.h" @@ -124,7 +125,7 @@ avatar_loaded_cb (GObject *source, if (pixbuf == NULL) { - pixbuf = empathy_pixbuf_from_icon_name_sized ( + pixbuf = tpaw_pixbuf_from_icon_name_sized ( TPAW_IMAGE_AVATAR_DEFAULT, AVATAR_SIZE); } diff --git a/libempathy-gtk/empathy-smiley-manager.c b/libempathy-gtk/empathy-smiley-manager.c index fcbe56024..44ea817be 100644 --- a/libempathy-gtk/empathy-smiley-manager.c +++ b/libempathy-gtk/empathy-smiley-manager.c @@ -23,6 +23,7 @@ #include "config.h" #include "empathy-smiley-manager.h" +#include #include #include "empathy-ui-utils.h" @@ -247,12 +248,12 @@ empathy_smiley_manager_add (EmpathySmileyManager *manager, g_return_if_fail (!TPAW_STR_EMPTY (icon_name)); g_return_if_fail (!TPAW_STR_EMPTY (first_str)); - pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); + pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); if (pixbuf) { gchar *path; va_start (var_args, first_str); - path = empathy_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); + path = tpaw_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); smiley_manager_add_valist (manager, pixbuf, path, first_str, var_args); va_end (var_args); g_object_unref (pixbuf); diff --git a/libempathy-gtk/empathy-theme-adium.c b/libempathy-gtk/empathy-theme-adium.c index 5fd61d339..0cf74e396 100644 --- a/libempathy-gtk/empathy-theme-adium.c +++ b/libempathy-gtk/empathy-theme-adium.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "empathy-gsettings.h" @@ -954,7 +955,7 @@ theme_adium_add_message (EmpathyThemeAdium *self, { if (!self->priv->data->default_avatar_filename) self->priv->data->default_avatar_filename = - empathy_filename_from_icon_name (TPAW_IMAGE_AVATAR_DEFAULT, + tpaw_filename_from_icon_name (TPAW_IMAGE_AVATAR_DEFAULT, GTK_ICON_SIZE_DIALOG); avatar_filename = self->priv->data->default_avatar_filename; diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c index 174eceac4..5e01d9fcc 100644 --- a/libempathy-gtk/empathy-ui-utils.c +++ b/libempathy-gtk/empathy-ui-utils.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include "empathy-ft-factory.h" @@ -155,69 +156,6 @@ empathy_protocol_name_for_contact (EmpathyContact *contact) return tp_account_get_icon_name (account); } -GdkPixbuf * -empathy_pixbuf_from_data (gchar *data, - gsize data_size) -{ - return empathy_pixbuf_from_data_and_mime (data, data_size, NULL); -} - -GdkPixbuf * -empathy_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; -} - struct SizeData { gint width; @@ -654,7 +592,7 @@ empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact, numerator = 3; denominator = 4; - icon_filename = empathy_filename_from_icon_name (icon_name, + icon_filename = tpaw_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU); if (icon_filename == NULL) @@ -711,7 +649,7 @@ empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact, g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL); account = empathy_contact_get_account (contact); - filename = empathy_filename_from_icon_name ( + filename = tpaw_filename_from_icon_name ( tp_account_get_icon_name (account), GTK_ICON_SIZE_MENU); if (filename != NULL) @@ -723,94 +661,6 @@ empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact, return pixbuf; } -GdkPixbuf * -empathy_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 * -empathy_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 * -empathy_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 empathy_pixbuf_from_icon_name_sized (icon_name, size); -} - -gchar * -empathy_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; -} - void empathy_url_show (GtkWidget *parent, const char *url) diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h index 3478e8605..22c0899cb 100644 --- a/libempathy-gtk/empathy-ui-utils.h +++ b/libempathy-gtk/empathy-ui-utils.h @@ -60,11 +60,6 @@ const gchar * empathy_icon_name_for_presence ( const gchar * empathy_icon_name_for_contact (EmpathyContact *contact); const gchar * empathy_icon_name_for_individual (FolksIndividual *individual); const gchar * empathy_protocol_name_for_contact (EmpathyContact *contact); -GdkPixbuf * empathy_pixbuf_from_data (gchar *data, - gsize data_size); -GdkPixbuf * empathy_pixbuf_from_data_and_mime (gchar *data, - gsize data_size, - gchar **mime_type); void empathy_pixbuf_avatar_from_individual_scaled_async ( FolksIndividual *individual, gint width, @@ -85,14 +80,6 @@ GdkPixbuf * empathy_pixbuf_contact_status_icon_with_icon_name ( EmpathyContact *contact, const gchar *icon_name, gboolean show_protocol); -GdkPixbuf * empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, - gint max_size); -GdkPixbuf * empathy_pixbuf_from_icon_name (const gchar *icon_name, - GtkIconSize icon_size); -GdkPixbuf * empathy_pixbuf_from_icon_name_sized (const gchar *icon_name, - gint size); -gchar * empathy_filename_from_icon_name (const gchar *icon_name, - GtkIconSize icon_size); void empathy_move_to_window_desktop (GtkWindow *window, guint32 timestamp); diff --git a/src/empathy-accounts-dialog.c b/src/empathy-accounts-dialog.c index ca5d57f5c..aa1f9ab31 100644 --- a/src/empathy-accounts-dialog.c +++ b/src/empathy-accounts-dialog.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "empathy-accounts-common.h" @@ -1023,7 +1024,7 @@ ensure_icon (EmpathyAccountsDialog *self, pixbuf = g_hash_table_lookup (priv->icons_cache, icon_name); if (pixbuf == NULL) { - pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON); + pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON); if (pixbuf == NULL) return NULL; diff --git a/src/empathy-call-window.c b/src/empathy-call-window.c index 6c215b53b..39c70c17b 100644 --- a/src/empathy-call-window.c +++ b/src/empathy-call-window.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "empathy-about-dialog.h" @@ -1893,7 +1894,7 @@ init_contact_avatar_with_size (EmpathyContact *contact, if (pixbuf_avatar == NULL) { - pixbuf_avatar = empathy_pixbuf_from_icon_name_sized ( + pixbuf_avatar = tpaw_pixbuf_from_icon_name_sized ( TPAW_IMAGE_AVATAR_DEFAULT, size); } diff --git a/tp-account-widgets/Makefile.am b/tp-account-widgets/Makefile.am index fa79032f6..968e62617 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 6a5b83b42..77d4a0b72 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 000000000..1b226e92d --- /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 000000000..0521f71aa --- /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.3