Debian Codegraph @

src/common/icons.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25 #include <libsoup/soup.h>
26
27 #include "icons.h"
28
29 #define PREFERED_DEPTH 32
30 #define PREFERED_WIDTH 22
31 #define PREFERED_HEIGHT 22
32
33 GdkPixbuf *icons[ICON_LAST];
34
35 /* For async downloads of icons */
36 static SoupSession *download_session;
37 static GList *pending_gets;
38
39 typedef struct {
40 gchar *mime_type;
41 gint width;
42 gint height;
43 } GetIconURLData;
44
45 static void
46 get_icon_url_data_free (GetIconURLData *data)
47 {
48 g_free (data->mime_type);
49 g_slice_free (GetIconURLData, data);
50 }
51
52 G_DEFINE_AUTOPTR_CLEANUP_FUNC (GetIconURLData, get_icon_url_data_free)
53
54 static GdkPixbuf *
55 get_icon_from_bytes (GBytes *icon_data,
56 const char *mime,
57 int source_width,
58 int source_height,
59 GError **error)
60 {
61 g_autoptr (GdkPixbufLoader) loader;
62 GdkPixbuf *pixbuf;
63
64 loader = gdk_pixbuf_loader_new_with_mime_type (mime, error);
65 if (loader == NULL)
66 return NULL;
67
68 gdk_pixbuf_loader_write_bytes (loader, icon_data, error);
69 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
70 if (pixbuf) {
71 gfloat aspect_ratio;
72 gint height;
73
74 /* Preserve the aspect-ratio of the original image */
75 aspect_ratio = (gfloat) source_width / source_height;
76 height = (gint) (PREFERED_WIDTH / aspect_ratio);
77 pixbuf = gdk_pixbuf_scale_simple (pixbuf,
78 PREFERED_WIDTH,
79 height,
80 GDK_INTERP_HYPER);
81 }
82
83 gdk_pixbuf_loader_close (loader, NULL);
84
85 return pixbuf;
86 }
87
88 /**
89 * Icon downloaded.
90 **/
91
92 static void
93 on_got_icon (GObject *source, GAsyncResult *res, gpointer user_data)
94 {
95 GError *error = NULL;
96 g_autoptr (GTask) task = G_TASK (user_data);
97 g_autofree char *mime = NULL;
98 int width;
99 int height;
100
101 g_autoptr (GBytes) icon =
102 gupnp_device_info_get_icon_finish (GUPNP_DEVICE_INFO (source),
103 res,
104 &mime,
105 NULL,
106 &width,
107 &height,
108 &error);
109
110 if (error != NULL) {
111 g_task_return_error (task, error);
112
113 return;
114 }
115
116 GdkPixbuf *device_icon = icon == NULL ? NULL
117 : get_icon_from_bytes (icon,
118 mime,
119 width,
120 height,
121 &error);
122
123 if (error != NULL) {
124 g_task_return_error (task, error);
125
126 return;
127 }
128
129 g_task_return_pointer (task, device_icon, g_object_unref);
130 }
131
132 void
133 update_icon_async (GUPnPDeviceInfo *info,
134 GCancellable *cancellable,
135 GAsyncReadyCallback callback,
136 gpointer user_data)
137 {
138 GTask *task = g_task_new (info, cancellable, callback, user_data);
139
140 gupnp_device_info_get_icon_async (info,
141 NULL,
142 PREFERED_DEPTH,
143 PREFERED_WIDTH,
144 PREFERED_HEIGHT,
145 TRUE,
146 cancellable,
147 on_got_icon,
148 task);
149 }
150
151 GdkPixbuf *
152 update_icon_finish (GUPnPDeviceInfo *info, GAsyncResult *res, GError **error)
153 {
154 g_return_val_if_fail (g_task_is_valid (res, info), NULL);
155
156 return g_task_propagate_pointer (G_TASK (res), error);
157 }
158
159 GdkPixbuf *
160 get_icon_by_id (IconID icon_id)
161 {
162 g_return_val_if_fail (icon_id > ICON_FIRST && icon_id < ICON_LAST, NULL);
163
164 return icons[icon_id];
165 }
166
167 static GdkPixbuf *
168 get_pixbuf_from_theme (const char *icon_name)
169 {
170 GdkScreen *screen;
171 GtkIconTheme *theme;
172 GdkPixbuf *pixbuf;
173 GError *error;
174
175 screen = gdk_screen_get_default ();
176 theme = gtk_icon_theme_get_for_screen (screen);
177
178 error = NULL;
179 pixbuf = gtk_icon_theme_load_icon (theme,
180 icon_name,
181 PREFERED_WIDTH,
182 PREFERED_HEIGHT,
183 &error);
184 if (pixbuf == NULL) {
185 g_warning ("Failed to load icon %s: %s",
186 icon_name,
187 error->message);
188 g_error_free (error);
189
190 error = NULL;
191 pixbuf = gtk_icon_theme_load_icon (theme,
192 "image-missing",
193 PREFERED_WIDTH,
194 PREFERED_HEIGHT,
195 &error);
196 if (pixbuf == NULL) {
197 g_critical ("Failed to load fallback icon: %s",
198 error->message);
199 g_error_free (error);
200 }
201 }
202
203 return pixbuf;
204 }
205
206 GdkPixbuf *
207 load_pixbuf_file (const char *file_name)
208 {
209 GdkPixbuf *pixbuf;
210 char *path;
211
212 path = g_build_path ("/", "/org/gupnp/Tools/Common", file_name, NULL);
213 pixbuf = gdk_pixbuf_new_from_resource (path, NULL);
214 if (pixbuf == NULL)
215 g_critical ("failed to get image %s\n", path);
216
217 g_free (path);
218
219 return pixbuf;
220 }
221
222 extern void gupnp_tools_common_unregister_resource (void);
223 extern void gupnp_tools_common_register_resource (void);
224
225 void
226 init_icons (void)
227 {
228 int i, j;
229 const char *file_names[] = {
230 "pixmaps/upnp-device.png", /* ICON_DEVICE */
231 "pixmaps/upnp-service.png", /* ICON_SERVICE */
232 "pixmaps/upnp-state-variable.png", /* ICON_VARIABLE */
233 "pixmaps/upnp-action-arg-in.png", /* ICON_ACTION_ARG_IN */
234 "pixmaps/upnp-action-arg-out.png", /* ICON_ACTION_ARG_OUT */
235 "pixmaps/media-renderer.png" /* ICON_MEDIA_RENDERER */
236 };
237
238 const char *theme_names[] = {
239 "image-missing", /* ICON_MISSING */
240 "network-workgroup", /* ICON_NETWORK */
241 "system-run", /* ICON_ACTION */
242 "folder", /* ICON_VARIABLES */
243 "text-x-generic", /* ICON_FILE */
244 "folder-remote", /* ICON_CONTAINER */
245 "audio-x-generic", /* ICON_AUDIO_ITEM */
246 "video-x-generic", /* ICON_VIDEO_ITEM */
247 "image-x-generic", /* ICON_IMAGE_ITEM */
248 "text-x-generic", /* ICON_TEXT_ITEM */
249 };
250 gupnp_tools_common_register_resource ();
251
252 for (i = 0; i < ICON_MISSING; i++) {
253 icons[i] = load_pixbuf_file (file_names[i]);
254 }
255
256 for (j = 0; i < ICON_LAST; i++, j++) {
257 icons[i] = get_pixbuf_from_theme (theme_names[j]);
258 }
259
260
261 download_session = soup_session_new ();
262 g_assert (download_session != NULL);
263
264 pending_gets = NULL;
265 }
266
267 void
268 deinit_icons (void)
269 {
270 int i;
271
272 g_object_unref (download_session);
273
274 for (i = 0; i < ICON_LAST; i++) {
275 g_object_unref (icons[i]);
276 }
277 gupnp_tools_common_unregister_resource ();
278 }
279