summaryrefslogtreecommitdiff
path: root/src/common/icons.c
blob: 381157df1c6c94ac59361fc56f1fc7d8403fd429 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2007 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *
 * Authors: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <libsoup/soup.h>

#include "icons.h"

#define PREFERED_DEPTH  32
#define PREFERED_WIDTH  22
#define PREFERED_HEIGHT 22

GdkPixbuf *icons[ICON_LAST];

/* For async downloads of icons */
static SoupSession *download_session;
static GList       *pending_gets;

typedef struct {
        gchar           *mime_type;
        gint             width;
        gint             height;
} GetIconURLData;

static void
get_icon_url_data_free (GetIconURLData *data)
{
        g_free (data->mime_type);
        g_slice_free (GetIconURLData, data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (GetIconURLData, get_icon_url_data_free)

static GdkPixbuf *
get_icon_from_bytes (GBytes *icon_data, GetIconURLData *data, GError **error)
{
        g_autoptr (GdkPixbufLoader) loader;
        GdkPixbuf       *pixbuf;

        loader = gdk_pixbuf_loader_new_with_mime_type (data->mime_type, error);
        if (loader == NULL)
                return NULL;

        gdk_pixbuf_loader_write_bytes (loader, icon_data, error);
        pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
        if (pixbuf) {
                gfloat aspect_ratio;
                gint   height;

                /* Preserve the aspect-ratio of the original image */
                aspect_ratio = (gfloat) data->width / data->height;
                height = (gint) (PREFERED_WIDTH / aspect_ratio);
                pixbuf = gdk_pixbuf_scale_simple (pixbuf,
                                                  PREFERED_WIDTH,
                                                  height,
                                                  GDK_INTERP_HYPER);
        }

        gdk_pixbuf_loader_close (loader, NULL);

        return pixbuf;
}

/**
 * Icon downloaded.
 **/

static void
on_got_icon (GObject *source, GAsyncResult *res, gpointer user_data)
{
        GError *error = NULL;
        g_autoptr (GTask) task = G_TASK (user_data);

        g_autoptr (GBytes) body =
                soup_session_send_and_read_finish (SOUP_SESSION (source),
                                                   res,
                                                   &error);

        if (error != NULL) {
                g_task_return_error (task, error);

                return;
        }

        SoupMessage *message =
                soup_session_get_async_result_message (SOUP_SESSION (source),
                                                       res);

        if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (message))) {
                g_task_return_error (
                        task,
                        g_error_new (G_IO_ERROR,
                                     G_IO_ERROR_FAILED,
                                     "Unable to  download icon: %s",
                                     soup_message_get_reason_phrase (message)));
                return;
        }

        GdkPixbuf *device_icon =
                get_icon_from_bytes (body, g_task_get_task_data (task), &error);
        if (error != NULL) {
                g_task_return_error (task, error);

                return;
        }

        g_task_return_pointer (task, device_icon, g_object_unref);
}

void
update_icon_async (GUPnPDeviceInfo *info,
                   GCancellable *cancellable,
                   GAsyncReadyCallback callback,
                   gpointer user_data)
{
        g_autoptr (GTask) task =
                g_task_new (info, cancellable, callback, user_data);

        g_autoptr (GetIconURLData) data;
        g_autofree char *icon_url;

        data = g_slice_new0 (GetIconURLData);

        icon_url = gupnp_device_info_get_icon_url (info,
                                                   NULL,
                                                   PREFERED_DEPTH,
                                                   PREFERED_WIDTH,
                                                   PREFERED_HEIGHT,
                                                   TRUE,
                                                   &data->mime_type,
                                                   NULL,
                                                   &data->width,
                                                   &data->height);

        if (icon_url == NULL) {
                g_task_return_pointer (task, NULL, NULL);

                return;
        }

        g_autofree char *new_url =
                gupnp_context_rewrite_uri (gupnp_device_info_get_context (info),
                                           icon_url);
        g_autoptr (SoupMessage) message =
                soup_message_new (SOUP_METHOD_GET, new_url);

        if (message == NULL) {
                g_task_return_error (task,
                                     g_error_new (G_URI_ERROR,
                                                  G_URI_ERROR_FAILED,
                                                  "Could not parse icon url %s",
                                                  new_url));

                return;
        }

        g_task_set_task_data (task,
                              g_steal_pointer (&data),
                              (GDestroyNotify) get_icon_url_data_free);

        soup_session_send_and_read_async (download_session,
                                          message,
                                          G_PRIORITY_LOW,
                                          cancellable,
                                          on_got_icon,
                                          task);

        g_steal_pointer (&task);
}

GdkPixbuf *
update_icon_finish (GUPnPDeviceInfo *info, GAsyncResult *res, GError **error)
{
        g_return_val_if_fail (g_task_is_valid (res, info), NULL);

        return g_task_propagate_pointer (G_TASK (res), error);
}

GdkPixbuf *
get_icon_by_id (IconID icon_id)
{
        g_return_val_if_fail (icon_id > ICON_FIRST && icon_id < ICON_LAST, NULL);

        return icons[icon_id];
}

static GdkPixbuf *
get_pixbuf_from_theme (const char *icon_name)
{
        GdkScreen    *screen;
        GtkIconTheme *theme;
        GdkPixbuf    *pixbuf;
        GError       *error;

        screen = gdk_screen_get_default ();
        theme = gtk_icon_theme_get_for_screen (screen);

        error = NULL;
        pixbuf = gtk_icon_theme_load_icon (theme,
                                           icon_name,
                                           PREFERED_WIDTH,
                                           PREFERED_HEIGHT,
                                           &error);
        if (pixbuf == NULL) {
                g_warning ("Failed to load icon %s: %s",
                           icon_name,
                           error->message);
                g_error_free (error);

                error = NULL;
                pixbuf = gtk_icon_theme_load_icon (theme,
                                                   "image-missing",
                                                   PREFERED_WIDTH,
                                                   PREFERED_HEIGHT,
                                                   &error);
                if (pixbuf == NULL) {
                    g_critical ("Failed to load fallback icon: %s",
                                error->message);
                    g_error_free (error);
                }
        }

        return pixbuf;
}

GdkPixbuf *
load_pixbuf_file (const char *file_name)
{
        GdkPixbuf *pixbuf;
        char *path;

        path = g_build_path ("/", "/org/gupnp/Tools/Common", file_name, NULL);
        pixbuf = gdk_pixbuf_new_from_resource (path, NULL);
        if (pixbuf == NULL)
                g_critical ("failed to get image %s\n", path);

        g_free (path);

        return pixbuf;
}

extern void gupnp_tools_common_unregister_resource (void);
extern void gupnp_tools_common_register_resource (void);

void
init_icons (void)
{
        int   i, j;
        const char *file_names[] = {
                "pixmaps/upnp-device.png",          /* ICON_DEVICE         */
                "pixmaps/upnp-service.png",         /* ICON_SERVICE        */
                "pixmaps/upnp-state-variable.png",  /* ICON_VARIABLE       */
                "pixmaps/upnp-action-arg-in.png",   /* ICON_ACTION_ARG_IN  */
                "pixmaps/upnp-action-arg-out.png",  /* ICON_ACTION_ARG_OUT */
                "pixmaps/media-renderer.png"        /* ICON_MEDIA_RENDERER */
        };

        const char *theme_names[] = {
                "image-missing",               /* ICON_MISSING    */
                "network-workgroup",           /* ICON_NETWORK    */
                "system-run",                  /* ICON_ACTION     */
                "folder",                      /* ICON_VARIABLES  */
                "text-x-generic",              /* ICON_FILE       */
                "folder-remote",               /* ICON_CONTAINER  */
                "audio-x-generic",             /* ICON_AUDIO_ITEM */
                "video-x-generic",             /* ICON_VIDEO_ITEM */
                "image-x-generic",             /* ICON_IMAGE_ITEM */
                "text-x-generic",              /* ICON_TEXT_ITEM */
        };
        gupnp_tools_common_register_resource ();

        for (i = 0; i < ICON_MISSING; i++) {
                icons[i] = load_pixbuf_file (file_names[i]);
        }

        for (j = 0; i < ICON_LAST; i++, j++) {
                icons[i] = get_pixbuf_from_theme (theme_names[j]);
        }


        download_session = soup_session_new ();
        g_assert (download_session != NULL);

        pending_gets = NULL;
}

void
deinit_icons (void)
{
        int i;

        g_object_unref (download_session);

        for (i = 0; i < ICON_LAST; i++) {
                g_object_unref (icons[i]);
        }
        gupnp_tools_common_unregister_resource ();
}