summaryrefslogtreecommitdiff
path: root/tp-account-widgets/tpaw-pixbuf-utils.c
blob: f1f8872cda94f5d3669cda2d2bb4e99181e5705b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * 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 <micke@imendio.com>
 *          Richard Hult <richard@imendio.com>
 *          Martyn Russell <martyn@imendio.com>
 *          Xavier Claessens <xclaesse@gmail.com>
 *          Travis Reitter <travis.reitter@collabora.co.uk>
 */

#include "config.h"
#include "tpaw-pixbuf-utils.h"

#define DEBUG_FLAG TPAW_DEBUG_OTHER
#include "tpaw-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;
}