Blame telepathy-account-widgets/tp-account-widgets/tpaw-pixbuf-utils.c

Packit 79f644
/*
Packit 79f644
 * Copyright (C) 2002-2007 Imendio AB
Packit 79f644
 * Copyright (C) 2007-2013 Collabora Ltd.
Packit 79f644
 *
Packit 79f644
 * This library is free software; you can redistribute it and/or
Packit 79f644
 * modify it under the terms of the GNU Lesser General Public
Packit 79f644
 * License as published by the Free Software Foundation; either
Packit 79f644
 * version 2.1 of the License, or (at your option) any later version.
Packit 79f644
 *
Packit 79f644
 * This library is distributed in the hope that it will be useful,
Packit 79f644
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 79f644
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 79f644
 * Lesser General Public License for more details.
Packit 79f644
 *
Packit 79f644
 * You should have received a copy of the GNU Lesser General Public
Packit 79f644
 * License along with this library; if not, write to the Free Software
Packit 79f644
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 79f644
 *
Packit 79f644
 * Authors: Mikael Hallendal <micke@imendio.com>
Packit 79f644
 *          Richard Hult <richard@imendio.com>
Packit 79f644
 *          Martyn Russell <martyn@imendio.com>
Packit 79f644
 *          Xavier Claessens <xclaesse@gmail.com>
Packit 79f644
 *          Travis Reitter <travis.reitter@collabora.co.uk>
Packit 79f644
 */
Packit 79f644
Packit 79f644
#include "config.h"
Packit 79f644
#include "tpaw-pixbuf-utils.h"
Packit 79f644
Packit 79f644
#define DEBUG_FLAG TPAW_DEBUG_OTHER
Packit 79f644
#include "tpaw-debug.h"
Packit 79f644
Packit 79f644
GdkPixbuf *
Packit 79f644
tpaw_pixbuf_from_data (gchar *data,
Packit 79f644
    gsize data_size)
Packit 79f644
{
Packit 79f644
  return tpaw_pixbuf_from_data_and_mime (data, data_size, NULL);
Packit 79f644
}
Packit 79f644
Packit 79f644
GdkPixbuf *
Packit 79f644
tpaw_pixbuf_from_data_and_mime (gchar *data,
Packit 79f644
           gsize data_size,
Packit 79f644
           gchar **mime_type)
Packit 79f644
{
Packit 79f644
  GdkPixbufLoader *loader;
Packit 79f644
  GdkPixbufFormat *format;
Packit 79f644
  GdkPixbuf *pixbuf = NULL;
Packit 79f644
  gchar **mime_types;
Packit 79f644
  GError *error = NULL;
Packit 79f644
Packit 79f644
  if (!data)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  loader = gdk_pixbuf_loader_new ();
Packit 79f644
  if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error))
Packit 79f644
    {
Packit 79f644
      DEBUG ("Failed to write to pixbuf loader: %s",
Packit 79f644
        error ? error->message : "No error given");
Packit 79f644
      goto out;
Packit 79f644
    }
Packit 79f644
Packit 79f644
  if (!gdk_pixbuf_loader_close (loader, &error))
Packit 79f644
    {
Packit 79f644
      DEBUG ("Failed to close pixbuf loader: %s",
Packit 79f644
        error ? error->message : "No error given");
Packit 79f644
      goto out;
Packit 79f644
    }
Packit 79f644
Packit 79f644
  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
Packit 79f644
  if (pixbuf)
Packit 79f644
    {
Packit 79f644
      g_object_ref (pixbuf);
Packit 79f644
Packit 79f644
      if (mime_type != NULL)
Packit 79f644
        {
Packit 79f644
          format = gdk_pixbuf_loader_get_format (loader);
Packit 79f644
          mime_types = gdk_pixbuf_format_get_mime_types (format);
Packit 79f644
Packit 79f644
          *mime_type = g_strdup (*mime_types);
Packit 79f644
          if (mime_types[1] != NULL)
Packit 79f644
            DEBUG ("Loader supports more than one mime "
Packit 79f644
              "type! Picking the first one, %s",
Packit 79f644
              *mime_type);
Packit 79f644
Packit 79f644
          g_strfreev (mime_types);
Packit 79f644
        }
Packit 79f644
    }
Packit 79f644
Packit 79f644
out:
Packit 79f644
  g_clear_error (&error);
Packit 79f644
  g_object_unref (loader);
Packit 79f644
Packit 79f644
  return pixbuf;
Packit 79f644
}
Packit 79f644
Packit 79f644
GdkPixbuf *
Packit 79f644
tpaw_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf,
Packit 79f644
    gint max_size)
Packit 79f644
{
Packit 79f644
  gint width, height;
Packit 79f644
  gdouble factor;
Packit 79f644
Packit 79f644
  width = gdk_pixbuf_get_width (pixbuf);
Packit 79f644
  height = gdk_pixbuf_get_height (pixbuf);
Packit 79f644
Packit 79f644
  if (width > 0 && (width > max_size || height > max_size))
Packit 79f644
    {
Packit 79f644
      factor = (gdouble) max_size / MAX (width, height);
Packit 79f644
Packit 79f644
      width = width * factor;
Packit 79f644
      height = height * factor;
Packit 79f644
Packit 79f644
      return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_HYPER);
Packit 79f644
    }
Packit 79f644
Packit 79f644
  return g_object_ref (pixbuf);
Packit 79f644
}
Packit 79f644
Packit 79f644
GdkPixbuf *
Packit 79f644
tpaw_pixbuf_from_icon_name_sized (const gchar *icon_name,
Packit 79f644
    gint size)
Packit 79f644
{
Packit 79f644
  GtkIconTheme *theme;
Packit 79f644
  GdkPixbuf *pixbuf;
Packit 79f644
  GError *error = NULL;
Packit 79f644
Packit 79f644
  if (!icon_name)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  theme = gtk_icon_theme_get_default ();
Packit 79f644
Packit 79f644
  pixbuf = gtk_icon_theme_load_icon (theme, icon_name, size, 0, &error);
Packit 79f644
Packit 79f644
  if (error)
Packit 79f644
    {
Packit 79f644
      DEBUG ("Error loading icon: %s", error->message);
Packit 79f644
      g_clear_error (&error);
Packit 79f644
    }
Packit 79f644
Packit 79f644
  return pixbuf;
Packit 79f644
}
Packit 79f644
Packit 79f644
GdkPixbuf *
Packit 79f644
tpaw_pixbuf_from_icon_name (const gchar *icon_name,
Packit 79f644
    GtkIconSize  icon_size)
Packit 79f644
{
Packit 79f644
  gint w, h;
Packit 79f644
  gint size = 48;
Packit 79f644
Packit 79f644
  if (!icon_name)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  if (gtk_icon_size_lookup (icon_size, &w, &h))
Packit 79f644
    size = (w + h) / 2;
Packit 79f644
Packit 79f644
  return tpaw_pixbuf_from_icon_name_sized (icon_name, size);
Packit 79f644
}
Packit 79f644
Packit 79f644
gchar *
Packit 79f644
tpaw_filename_from_icon_name (const gchar *icon_name,
Packit 79f644
    GtkIconSize icon_size)
Packit 79f644
{
Packit 79f644
  GtkIconTheme *icon_theme;
Packit 79f644
  GtkIconInfo *icon_info;
Packit 79f644
  gint w, h;
Packit 79f644
  gint size = 48;
Packit 79f644
  gchar *ret;
Packit 79f644
Packit 79f644
  icon_theme = gtk_icon_theme_get_default ();
Packit 79f644
Packit 79f644
  if (gtk_icon_size_lookup (icon_size, &w, &h))
Packit 79f644
    size = (w + h) / 2;
Packit 79f644
Packit 79f644
  icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
Packit 79f644
  if (icon_info == NULL)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  ret = g_strdup (gtk_icon_info_get_filename (icon_info));
Packit 79f644
  gtk_icon_info_free (icon_info);
Packit 79f644
Packit 79f644
  return ret;
Packit 79f644
}