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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2015 Kalev Lember <klember@redhat.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <config.h>
#include <string.h>
#include <gnome-software.h>
/*
* SECTION:
* Loads remote icons and converts them into local cached ones.
*
* It is provided so that each plugin handling icons does not
* have to handle the download and caching functionality.
*/
struct GsPluginData {
GtkIconTheme *icon_theme;
GMutex icon_theme_lock;
GHashTable *icon_theme_paths;
};
static void gs_plugin_icons_add_theme_path (GsPlugin *plugin, const gchar *path);
void
gs_plugin_initialize (GsPlugin *plugin)
{
GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
const gchar *test_search_path;
priv->icon_theme = gtk_icon_theme_new ();
gtk_icon_theme_set_screen (priv->icon_theme, gdk_screen_get_default ());
priv->icon_theme_paths = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
g_mutex_init (&priv->icon_theme_lock);
test_search_path = g_getenv ("GS_SELF_TEST_ICON_THEME_PATH");
if (test_search_path != NULL) {
g_auto(GStrv) dirs = g_strsplit (test_search_path, ":", -1);
/* add_theme_path() prepends, so we have to iterate in reverse to preserve order */
for (gsize i = g_strv_length (dirs); i > 0; i--)
gs_plugin_icons_add_theme_path (plugin, dirs[i - 1]);
}
/* needs remote icons downloaded */
gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "epiphany");
}
void
gs_plugin_destroy (GsPlugin *plugin)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
g_object_unref (priv->icon_theme);
g_hash_table_unref (priv->icon_theme_paths);
g_mutex_clear (&priv->icon_theme_lock);
}
static gboolean
gs_plugin_icons_download (GsPlugin *plugin,
const gchar *uri,
const gchar *filename,
GError **error)
{
guint status_code;
g_autoptr(GdkPixbuf) pixbuf_new = NULL;
g_autoptr(GdkPixbuf) pixbuf = NULL;
g_autoptr(GInputStream) stream = NULL;
g_autoptr(SoupMessage) msg = NULL;
/* create the GET data */
msg = soup_message_new (SOUP_METHOD_GET, uri);
if (msg == NULL) {
g_set_error (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"%s is not a valid URL", uri);
return FALSE;
}
/* set sync request */
status_code = soup_session_send_message (gs_plugin_get_soup_session (plugin), msg);
if (status_code != SOUP_STATUS_OK) {
g_set_error (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_DOWNLOAD_FAILED,
"Failed to download icon %s: %s",
uri, soup_status_get_phrase (status_code));
return FALSE;
}
/* we're assuming this is a 64x64 png file, resize if not */
stream = g_memory_input_stream_new_from_data (msg->response_body->data,
msg->response_body->length,
NULL);
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
if (pixbuf == NULL) {
gs_utils_error_convert_gdk_pixbuf (error);
return FALSE;
}
if (gdk_pixbuf_get_height (pixbuf) == 64 &&
gdk_pixbuf_get_width (pixbuf) == 64) {
pixbuf_new = g_object_ref (pixbuf);
} else {
pixbuf_new = gdk_pixbuf_scale_simple (pixbuf, 64, 64,
GDK_INTERP_BILINEAR);
}
/* write file */
if (!gdk_pixbuf_save (pixbuf_new, filename, "png", error, NULL)) {
gs_utils_error_convert_gdk_pixbuf (error);
return FALSE;
}
return TRUE;
}
static GdkPixbuf *
gs_plugin_icons_load_local (GsPlugin *plugin, AsIcon *icon, GError **error)
{
GdkPixbuf *pixbuf;
gint size;
if (as_icon_get_filename (icon) == NULL) {
g_set_error_literal (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"icon has no filename");
return NULL;
}
size = (gint) (64 * gs_plugin_get_scale (plugin));
pixbuf = gdk_pixbuf_new_from_file_at_size (as_icon_get_filename (icon),
size, size, error);
if (pixbuf == NULL) {
gs_utils_error_convert_gdk_pixbuf (error);
return NULL;
}
return pixbuf;
}
static gchar *
gs_plugin_icons_get_cache_fn (AsIcon *icon)
{
g_autofree gchar *basename = NULL;
g_autofree gchar *checksum = NULL;
checksum = g_compute_checksum_for_string (G_CHECKSUM_SHA1,
as_icon_get_url (icon),
-1);
basename = g_path_get_basename (as_icon_get_url (icon));
return g_strdup_printf ("%s-%s", checksum, basename);
}
static GdkPixbuf *
gs_plugin_icons_load_remote (GsPlugin *plugin, AsIcon *icon, GError **error)
{
const gchar *fn;
gchar *found;
/* not applicable for remote */
if (as_icon_get_url (icon) == NULL) {
g_set_error_literal (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"icon has no URL");
return NULL;
}
/* set cache filename if not already set */
if (as_icon_get_filename (icon) == NULL) {
g_autofree gchar *fn_cache = NULL;
g_autofree gchar *fn_basename = NULL;
/* use a hash-prefixed filename to avoid cache clashes */
fn_basename = gs_plugin_icons_get_cache_fn (icon);
fn_cache = gs_utils_get_cache_filename ("icons",
fn_basename,
GS_UTILS_CACHE_FLAG_WRITEABLE,
error);
if (fn_cache == NULL)
return NULL;
as_icon_set_filename (icon, fn_cache);
}
/* already in cache */
if (g_file_test (as_icon_get_filename (icon), G_FILE_TEST_EXISTS))
return gs_plugin_icons_load_local (plugin, icon, error);
/* a REMOTE that's really LOCAL */
if (g_str_has_prefix (as_icon_get_url (icon), "file://")) {
as_icon_set_filename (icon, as_icon_get_url (icon) + 7);
as_icon_set_kind (icon, AS_ICON_KIND_LOCAL);
return gs_plugin_icons_load_local (plugin, icon, error);
}
/* convert filename from jpg to png */
fn = as_icon_get_filename (icon);
found = g_strstr_len (fn, -1, ".jpg");
if (found != NULL)
memcpy (found, ".png", 4);
/* create runtime dir and download */
if (!gs_mkdir_parent (fn, error))
return NULL;
if (!gs_plugin_icons_download (plugin, as_icon_get_url (icon), fn, error))
return NULL;
as_icon_set_kind (icon, AS_ICON_KIND_LOCAL);
return gs_plugin_icons_load_local (plugin, icon, error);
}
static void
gs_plugin_icons_add_theme_path (GsPlugin *plugin, const gchar *path)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
if (path == NULL)
return;
if (!g_hash_table_contains (priv->icon_theme_paths, path)) {
gtk_icon_theme_prepend_search_path (priv->icon_theme, path);
g_hash_table_add (priv->icon_theme_paths, g_strdup (path));
}
}
static GdkPixbuf *
gs_plugin_icons_load_stock (GsPlugin *plugin, AsIcon *icon, GError **error)
{
GsPluginData *priv = gs_plugin_get_data (plugin);
GdkPixbuf *pixbuf;
gint size;
g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->icon_theme_lock);
/* required */
if (as_icon_get_name (icon) == NULL) {
g_set_error_literal (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"icon has no name");
return NULL;
}
gs_plugin_icons_add_theme_path (plugin, as_icon_get_prefix (icon));
size = (gint) (64 * gs_plugin_get_scale (plugin));
pixbuf = gtk_icon_theme_load_icon (priv->icon_theme,
as_icon_get_name (icon),
size,
GTK_ICON_LOOKUP_USE_BUILTIN |
GTK_ICON_LOOKUP_FORCE_SIZE,
error);
if (pixbuf == NULL) {
gs_utils_error_convert_gdk_pixbuf (error);
return NULL;
}
return pixbuf;
}
static GdkPixbuf *
gs_plugin_icons_load_cached (GsPlugin *plugin, AsIcon *icon, GError **error)
{
if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, error)) {
gs_utils_error_convert_gdk_pixbuf (error);
gs_utils_error_convert_appstream (error);
return NULL;
}
return g_object_ref (as_icon_get_pixbuf (icon));
}
static gboolean
refine_app (GsPlugin *plugin,
GsApp *app,
GsPluginRefineFlags flags,
GCancellable *cancellable,
GError **error)
{
GPtrArray *icons;
guint i;
/* not required */
if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0)
return TRUE;
/* invalid */
if (gs_app_get_pixbuf (app) != NULL)
return TRUE;
/* process all icons */
icons = gs_app_get_icons (app);
for (i = 0; i < icons->len; i++) {
AsIcon *icon = g_ptr_array_index (icons, i);
g_autoptr(GdkPixbuf) pixbuf = NULL;
g_autoptr(GError) error_local = NULL;
/* handle different icon types */
switch (as_icon_get_kind (icon)) {
case AS_ICON_KIND_LOCAL:
pixbuf = gs_plugin_icons_load_local (plugin, icon, &error_local);
break;
case AS_ICON_KIND_STOCK:
pixbuf = gs_plugin_icons_load_stock (plugin, icon, &error_local);
break;
case AS_ICON_KIND_REMOTE:
pixbuf = gs_plugin_icons_load_remote (plugin, icon, &error_local);
break;
case AS_ICON_KIND_CACHED:
pixbuf = gs_plugin_icons_load_cached (plugin, icon, &error_local);
break;
default:
g_set_error (&error_local,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_NOT_SUPPORTED,
"icon kind '%s' unknown",
as_icon_kind_to_string (as_icon_get_kind (icon)));
break;
}
if (pixbuf != NULL) {
gs_app_set_pixbuf (app, pixbuf);
break;
}
/* we failed, but keep going */
g_debug ("failed to load icon for %s: %s",
gs_app_get_id (app),
error_local->message);
}
return TRUE;
}
gboolean
gs_plugin_refine (GsPlugin *plugin,
GsAppList *list,
GsPluginRefineFlags flags,
GCancellable *cancellable,
GError **error)
{
/* nothing to do here */
if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0)
return TRUE;
for (guint i = 0; i < gs_app_list_length (list); i++) {
GsApp *app = gs_app_list_index (list, i);
if (!refine_app (plugin, app, flags, cancellable, error))
return FALSE;
}
return TRUE;
}
|