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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* nautilus-icon-info.c
* Copyright (C) 2007 Red Hat, Inc., Alexander Larsson <alexl@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "nautilus-icon-info.h"
#include "nautilus-enums.h"
#include <nautilus-hash-queue.h>
#include <glycin.h>
#include <glycin-gtk4.h>
static GtkIconPaintable *
lookup_themed_icon (GIcon *icon,
int size,
float scale);
GIcon *
nautilus_icon_info_get_default_file_icon (void)
{
static GIcon *fallback_icon = NULL;
if (G_UNLIKELY (fallback_icon == NULL))
{
char *icon_names[3] = {"application-x-generic", "text-x-generic", NULL};
fallback_icon = g_themed_icon_new_from_names (icon_names, -1);
}
return fallback_icon;
}
static GdkPaintable *
nautilus_icon_info_get_fallback (int size,
int scale)
{
static gboolean in_fallback = FALSE, no_fallback = FALSE;
if (in_fallback || no_fallback)
{
/* Already tried to find a fallback, use hard coded fallback paintable */
static GtkSvg *fallback_paintable = NULL;
g_autoptr (GtkSnapshot) snapshot = gtk_snapshot_new ();
int dim = size * scale;
if (fallback_paintable == NULL)
{
const char *resource_path = "/org/gnome/nautilus/image/text-x-preview.svg";
fallback_paintable = gtk_svg_new_from_resource (resource_path);
}
gdk_paintable_snapshot (GDK_PAINTABLE (fallback_paintable), snapshot, dim, dim);
no_fallback = TRUE;
return gtk_snapshot_to_paintable (snapshot, &GRAPHENE_SIZE_INIT (dim, dim));
}
GIcon *icon = nautilus_icon_info_get_default_file_icon ();
GdkPaintable *fallback_paintable;
/* Use existing cache to cache the fallback paintable */
in_fallback = TRUE;
fallback_paintable = nautilus_icon_info_lookup (icon, size, scale);
in_fallback = FALSE;
return fallback_paintable;
}
typedef struct
{
GIcon *icon;
int scale;
int size;
} LoadableIconKey;
typedef struct
{
char *icon_name;
int scale;
int size;
} ThemedIconKey;
static NautilusHashQueue *loadable_icon_cache = NULL;
static NautilusHashQueue *themed_icon_cache = NULL;
#define LOADABLE_ICON_CACHE_COUNT_LIMIT 100
#define THEMED_ICON_CACHE_COUNT_LIMIT 200
void
nautilus_icon_info_clear_caches (void)
{
g_clear_pointer (&loadable_icon_cache, nautilus_hash_queue_destroy);
g_clear_pointer (&themed_icon_cache, nautilus_hash_queue_destroy);
}
static guint
int_hash (int v)
{
return g_direct_hash (GINT_TO_POINTER (v));
}
static guint
loadable_icon_key_hash (LoadableIconKey *key)
{
return g_icon_hash (key->icon) ^ int_hash (key->scale) ^ int_hash (key->size);
}
static gboolean
loadable_icon_key_equal (const LoadableIconKey *a,
const LoadableIconKey *b)
{
return a->size == b->size &&
a->scale == b->scale &&
g_icon_equal (a->icon, b->icon);
}
static LoadableIconKey *
loadable_icon_key_new (GIcon *icon,
int scale,
int size)
{
LoadableIconKey *key;
key = g_slice_new (LoadableIconKey);
key->icon = g_object_ref (icon);
key->scale = scale;
key->size = size;
return key;
}
static void
loadable_icon_key_free (LoadableIconKey *key)
{
g_object_unref (key->icon);
g_slice_free (LoadableIconKey, key);
}
static GdkPaintable *
loadable_icon_cache_get (LoadableIconKey *key)
{
if (loadable_icon_cache == NULL)
{
return NULL;
}
GdkPaintable *paintable = nautilus_hash_queue_find_item (loadable_icon_cache, key);
if (paintable != NULL)
{
nautilus_hash_queue_move_existing_to_tail (loadable_icon_cache, key);
}
return paintable;
}
static void
loadable_icon_cache_add (LoadableIconKey *key,
GdkPaintable *paintable)
{
if (G_UNLIKELY (loadable_icon_cache == NULL))
{
loadable_icon_cache = nautilus_hash_queue_new ((GHashFunc) loadable_icon_key_hash,
(GEqualFunc) loadable_icon_key_equal,
(GDestroyNotify) loadable_icon_key_free,
(GDestroyNotify) g_object_unref);
}
if (nautilus_hash_queue_reenqueue (loadable_icon_cache, key, paintable) &&
nautilus_hash_queue_get_length (loadable_icon_cache) > LOADABLE_ICON_CACHE_COUNT_LIMIT)
{
nautilus_hash_queue_remove_head (loadable_icon_cache);
}
}
static guint
themed_icon_key_hash (ThemedIconKey *key)
{
return g_str_hash (key->icon_name) ^ int_hash (key->scale) ^ int_hash (key->size);
}
static gboolean
themed_icon_key_equal (const ThemedIconKey *a,
const ThemedIconKey *b)
{
return a->size == b->size &&
a->scale == b->scale &&
g_str_equal (a->icon_name, b->icon_name);
}
static ThemedIconKey *
themed_icon_key_new (const char *icon_name,
int scale,
int size)
{
ThemedIconKey *key;
key = g_slice_new (ThemedIconKey);
key->icon_name = g_strdup (icon_name);
key->scale = scale;
key->size = size;
return key;
}
static void
themed_icon_key_free (ThemedIconKey *key)
{
g_free (key->icon_name);
g_slice_free (ThemedIconKey, key);
}
static GdkPaintable *
themed_icon_cache_get (ThemedIconKey *key)
{
if (themed_icon_cache == NULL)
{
return NULL;
}
GdkPaintable *paintable = GDK_PAINTABLE (nautilus_hash_queue_find_item (themed_icon_cache, key));
if (paintable != NULL)
{
nautilus_hash_queue_move_existing_to_tail (themed_icon_cache, key);
}
return paintable;
}
static void
themed_icon_cache_add (ThemedIconKey *key,
GdkPaintable *paintable)
{
if (G_UNLIKELY (themed_icon_cache == NULL))
{
themed_icon_cache = nautilus_hash_queue_new ((GHashFunc) themed_icon_key_hash,
(GEqualFunc) themed_icon_key_equal,
(GDestroyNotify) themed_icon_key_free,
(GDestroyNotify) g_object_unref);
}
if (nautilus_hash_queue_reenqueue (themed_icon_cache, key, paintable) &&
nautilus_hash_queue_get_length (themed_icon_cache) > THEMED_ICON_CACHE_COUNT_LIMIT)
{
nautilus_hash_queue_remove_head (themed_icon_cache);
}
}
static GtkIconTheme *
get_icon_theme (void)
{
GtkIconTheme *theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
if (g_test_initialized ())
{
/* During tests, force Adwaita theme */
static GtkIconTheme *test_theme = NULL;
if (test_theme == NULL)
{
test_theme = gtk_icon_theme_new ();
gtk_icon_theme_set_theme_name (test_theme, "Adwaita");
}
theme = test_theme;
}
return theme;
}
static GtkIconPaintable *
lookup_themed_icon (GIcon *icon,
int size,
float scale)
{
GtkIconTheme *theme = get_icon_theme ();
if (!gtk_icon_theme_has_gicon (theme, icon))
{
return NULL;
}
const gchar *generic_app_icon_name = "application-x-generic";
g_autoptr (GtkIconPaintable) icon_paintable
= gtk_icon_theme_lookup_by_gicon (theme, icon, size, scale,
GTK_TEXT_DIR_NONE,
GTK_ICON_LOOKUP_NONE);
const gchar *icon_name = gtk_icon_paintable_get_icon_name (icon_paintable);
if (G_IS_THEMED_ICON (icon) &&
g_strcmp0 (generic_app_icon_name, icon_name) == 0)
{
/* GTK prefers generic icons in the main theme over exact match in
* other themes even when the match is meaningless like the empty file
* icon 'application-x-generic'. */
const gchar **names = (const gchar **) g_themed_icon_get_names (G_THEMED_ICON (icon));
if (g_strcmp0 (generic_app_icon_name, names[0]) != 0 &&
gtk_icon_theme_has_icon (theme, names[0]))
{
return gtk_icon_theme_lookup_icon (theme, names[0], NULL, size, scale,
GTK_TEXT_DIR_NONE, GTK_ICON_LOOKUP_NONE);
}
}
return g_steal_pointer (&icon_paintable);
}
GdkPaintable *
nautilus_icon_info_lookup (GIcon *icon,
int size,
int scale)
{
GdkPaintable *paintable;
if (G_IS_EMBLEMED_ICON (icon))
{
icon = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
}
else if (G_IS_EMBLEM (icon))
{
icon = g_emblem_get_icon (G_EMBLEM (icon));
}
if (G_IS_LOADABLE_ICON (icon))
{
LoadableIconKey lookup_key;
LoadableIconKey *key;
lookup_key.icon = icon;
lookup_key.scale = scale;
lookup_key.size = size;
paintable = loadable_icon_cache_get (&lookup_key);
if (paintable != NULL)
{
return g_object_ref (paintable);
}
g_autoptr (GInputStream) stream = g_loadable_icon_load (G_LOADABLE_ICON (icon),
size * scale,
NULL, NULL, NULL);
if (stream)
{
g_autoptr (GlyLoader) loader = gly_loader_new_for_stream (stream);
g_autoptr (GlyImage) image = gly_loader_load (loader, NULL);
if (image != NULL)
{
g_autoptr (GlyFrameRequest) frame_request = gly_frame_request_new ();
g_autoptr (GlyFrame) frame = NULL;
gly_frame_request_set_scale (frame_request, size * scale, size * scale);
frame = gly_image_get_specific_frame (image, frame_request, NULL);
if (frame != NULL)
{
double iw = gly_frame_get_width (frame);
double ih = gly_frame_get_height (frame);
double scale_factor = MIN ((double) size / iw, (double) size / ih);
double width = iw * scale_factor;
double height = ih * scale_factor;
g_autoptr (GdkTexture) texture = gly_gtk_frame_get_texture (frame);
g_autoptr (GtkSnapshot) snapshot = gtk_snapshot_new ();
gdk_paintable_snapshot (GDK_PAINTABLE (texture),
GDK_SNAPSHOT (snapshot),
width, height);
paintable = gtk_snapshot_to_paintable (snapshot, NULL);
}
}
}
if (paintable == NULL)
{
return nautilus_icon_info_get_fallback (size, scale);
}
key = loadable_icon_key_new (icon, scale, size);
loadable_icon_cache_add (key, g_object_ref (paintable));
return paintable;
}
else if (G_IS_THEMED_ICON (icon))
{
g_autoptr (GtkIconPaintable) icon_paintable = lookup_themed_icon (icon, size, scale);
if (icon_paintable == NULL)
{
return nautilus_icon_info_get_fallback (size, scale);
}
ThemedIconKey lookup_key;
ThemedIconKey *key;
const char *icon_name;
icon_name = gtk_icon_paintable_get_icon_name (icon_paintable);
lookup_key.icon_name = (char *) icon_name;
lookup_key.scale = scale;
lookup_key.size = size;
paintable = themed_icon_cache_get (&lookup_key);
if (paintable != NULL)
{
return g_object_ref (paintable);
}
paintable = GDK_PAINTABLE (g_steal_pointer (&icon_paintable));
key = themed_icon_key_new (icon_name, scale, size);
themed_icon_cache_add (key, g_object_ref (paintable));
return paintable;
}
else
{
return nautilus_icon_info_get_fallback (size, scale);
}
}
|