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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-icon-factory.c: Class for obtaining icons for files and other objects.
Copyright (C) 1999, 2000 Red Hat Inc.
Copyright (C) 1999, 2000, 2001 Eazel, Inc.
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., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: John Sullivan <sullivan@eazel.com>,
Darin Adler <darin@bentspoon.com>,
Andy Hertzfeld <andy@eazel.com>
*/
#include <config.h>
#include "nautilus-icon-factory.h"
#include "nautilus-default-file-icon.h"
#include "nautilus-file-attributes.h"
#include "nautilus-file-private.h"
#include "nautilus-file-utilities.h"
#include "nautilus-global-preferences.h"
#include "nautilus-icon-factory-private.h"
#include "nautilus-lib-self-check-functions.h"
#include "nautilus-link.h"
#include "nautilus-theme.h"
#include "nautilus-thumbnails.h"
#include "nautilus-trash-monitor.h"
#include <eel/eel-debug.h>
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-gdk-pixbuf-extensions.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-macros.h>
#include <eel/eel-pango-extensions.h>
#include <eel/eel-string.h>
#include <eel/eel-vfs-extensions.h>
#include <gtk/gtksettings.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkicontheme.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include <libgnome/gnome-macros.h>
#include <libgnomeui/gnome-icon-lookup.h>
#include <libgnomevfs/gnome-vfs-file-info.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnomevfs/gnome-vfs-mime-monitor.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-types.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <librsvg/rsvg.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define CACHE_SELF_CHECKS 0
#define ICON_NAME_THUMBNAIL_LOADING "gnome-fs-loading-icon"
#define ICON_NAME_TRASH_EMPTY "gnome-fs-trash-empty"
#define ICON_NAME_TRASH_FULL "gnome-fs-trash-full"
#define ICON_NAME_HOME "gnome-fs-home"
#define NAUTILUS_EMBLEM_NAME_PREFIX "emblem-"
/* This used to be called ICON_CACHE_MAX_ENTRIES, but it's misleading
* to call it that, since we can have any number of entries in the
* cache if the caller keeps the pixbuf around (we only get rid of
* items from the cache after the caller unref's them).
*/
#define ICON_CACHE_COUNT 128
/* This is the number of milliseconds we wait before sweeping out
* items from the cache.
*/
#define ICON_CACHE_SWEEP_TIMEOUT (10 * 1000)
/* This circular doubly-linked list structure is used to keep a list
* of the most recently used items in the cache.
*/
typedef struct CircularList CircularList;
struct CircularList {
CircularList *next;
CircularList *prev;
};
/* The key to a hash table that holds CacheIcons. */
typedef struct {
char *name; /* Icon name or absolute filename */
char *modifier;
guint nominal_size;
gboolean force_nominal;
} CacheKey;
/* The value in the same table. */
typedef struct {
guint ref_count;
GdkPixbuf *pixbuf;
GdkRectangle *embedded_text_rect;
GdkPoint *attach_points;
int n_attach_points;
char *display_name;
time_t mtime; /* Only used for absolute filenames */
CircularList recently_used_node;
} CacheIcon;
/* The icon factory.
* These are just globals, but they're in an object so we can
* connect signals and have multiple icon factories some day
* if we want to.
*/
typedef struct {
GObject object;
/* A hash table that contains the icons. A circular list of
* the most recently used icons is kept around, and we don't
* let them go when we sweep the cache.
*/
GHashTable *icon_cache;
/* frames to use for thumbnail icons */
GdkPixbuf *thumbnail_frame;
/* Used for icon themes according to the freedesktop icon spec. */
GtkIconTheme *icon_theme;
GnomeThumbnailFactory *thumbnail_factory;
CircularList recently_used_dummy_head;
guint recently_used_count;
guint sweep_timer;
CacheIcon *fallback_icon;
GHashTable *image_mime_types;
} NautilusIconFactory;
#define NAUTILUS_ICON_FACTORY(obj) \
GTK_CHECK_CAST (obj, nautilus_icon_factory_get_type (), NautilusIconFactory)
typedef struct {
GObjectClass parent_class;
} NautilusIconFactoryClass;
enum {
ICONS_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
static int cached_thumbnail_limit;
static int show_image_thumbs;
/* forward declarations */
static GType nautilus_icon_factory_get_type (void);
static void nautilus_icon_factory_class_init (NautilusIconFactoryClass *class);
static void nautilus_icon_factory_instance_init (NautilusIconFactory *factory);
static void nautilus_icon_factory_finalize (GObject *object);
static void thumbnail_limit_changed_callback (gpointer user_data);
static void show_thumbnails_changed_callback (gpointer user_data);
static void mime_type_data_changed_callback (GnomeVFSMIMEMonitor *monitor,
gpointer user_data);
static guint cache_key_hash (gconstpointer p);
static gboolean cache_key_equal (gconstpointer a,
gconstpointer b);
static void cache_key_destroy (CacheKey *key);
static void cache_icon_unref (CacheIcon *icon);
static CacheIcon *cache_icon_new (GdkPixbuf *pixbuf,
GtkIconInfo *icon_info,
double scale_x,
double scale_y);
static CacheIcon *get_icon_from_cache (const char *icon,
const char *modifier,
guint nominal_size,
gboolean force_nominal);
static void nautilus_icon_factory_clear (void);
GNOME_CLASS_BOILERPLATE (NautilusIconFactory,
nautilus_icon_factory,
GObject, G_TYPE_OBJECT);
static NautilusIconFactory *global_icon_factory = NULL;
static void
destroy_icon_factory (void)
{
eel_preferences_remove_callback (NAUTILUS_PREFERENCES_IMAGE_FILE_THUMBNAIL_LIMIT,
thumbnail_limit_changed_callback,
NULL);
eel_preferences_remove_callback (NAUTILUS_PREFERENCES_SHOW_IMAGE_FILE_THUMBNAILS,
show_thumbnails_changed_callback,
NULL);
g_object_unref (global_icon_factory);
}
/* Return a pointer to the single global icon factory. */
static NautilusIconFactory *
get_icon_factory (void)
{
if (global_icon_factory == NULL) {
nautilus_global_preferences_init ();
global_icon_factory = NAUTILUS_ICON_FACTORY
(g_object_new (nautilus_icon_factory_get_type (), NULL));
thumbnail_limit_changed_callback (NULL);
eel_preferences_add_callback (NAUTILUS_PREFERENCES_IMAGE_FILE_THUMBNAIL_LIMIT,
thumbnail_limit_changed_callback,
NULL);
show_thumbnails_changed_callback (NULL);
eel_preferences_add_callback (NAUTILUS_PREFERENCES_SHOW_IMAGE_FILE_THUMBNAILS,
show_thumbnails_changed_callback,
NULL);
g_signal_connect (gnome_vfs_mime_monitor_get (),
"data_changed",
G_CALLBACK (mime_type_data_changed_callback),
NULL);
eel_debug_call_at_shutdown (destroy_icon_factory);
}
return global_icon_factory;
}
GObject *
nautilus_icon_factory_get (void)
{
return G_OBJECT (get_icon_factory ());
}
static void
icon_theme_changed_callback (GnomeIconTheme *icon_theme,
gpointer user_data)
{
NautilusIconFactory *factory;
nautilus_icon_factory_clear ();
factory = user_data;
g_signal_emit (factory,
signals[ICONS_CHANGED], 0);
}
GtkIconTheme *
nautilus_icon_factory_get_icon_theme (void)
{
NautilusIconFactory *factory;
factory = get_icon_factory ();
return g_object_ref (factory->icon_theme);
}
GnomeThumbnailFactory *
nautilus_icon_factory_get_thumbnail_factory ()
{
NautilusIconFactory *factory;
factory = get_icon_factory ();
return g_object_ref (factory->thumbnail_factory);
}
static void
check_recently_used_list (void)
{
#if CACHE_SELF_CHECKS
NautilusIconFactory *factory;
CircularList *head, *node, *next;
guint count;
factory = get_icon_factory ();
head = &factory->recently_used_dummy_head;
count = 0;
node = head;
while (1) {
next = node->next;
g_assert (next != NULL);
g_assert (next->prev == node);
if (next == head) {
break;
}
count += 1;
node = next;
}
g_assert (count == factory->recently_used_count);
#endif
}
/* load the thumbnail frame */
static void
load_thumbnail_frame (NautilusIconFactory *factory)
{
char *image_path;
image_path = nautilus_theme_get_image_path ("thumbnail_frame.png");
if (factory->thumbnail_frame != NULL) {
g_object_unref (factory->thumbnail_frame);
}
if (image_path != NULL) {
factory->thumbnail_frame = gdk_pixbuf_new_from_file (image_path, NULL);
}
g_free (image_path);
}
static void
nautilus_icon_factory_instance_init (NautilusIconFactory *factory)
{
GdkPixbuf *pixbuf;
guint i;
static const char *types [] = {
"image/x-bmp", "image/x-ico", "image/jpeg", "image/gif",
"image/png", "image/pnm", "image/ras", "image/tga",
"image/tiff", "image/wbmp", "image/bmp", "image/x-xbitmap",
"image/x-xpixmap"
};
factory->icon_cache = g_hash_table_new_full (cache_key_hash,
cache_key_equal,
(GDestroyNotify)cache_key_destroy,
(GDestroyNotify)cache_icon_unref);
factory->icon_theme = gtk_icon_theme_get_default ();
g_signal_connect_object (factory->icon_theme,
"changed",
G_CALLBACK (icon_theme_changed_callback),
factory, 0);
factory->thumbnail_factory = gnome_thumbnail_factory_new (GNOME_THUMBNAIL_SIZE_NORMAL);
load_thumbnail_frame (factory);
/* Empty out the recently-used list. */
factory->recently_used_dummy_head.next = &factory->recently_used_dummy_head;
factory->recently_used_dummy_head.prev = &factory->recently_used_dummy_head;
pixbuf = gdk_pixbuf_new_from_data (nautilus_default_file_icon,
GDK_COLORSPACE_RGB,
TRUE,
8,
nautilus_default_file_icon_width,
nautilus_default_file_icon_height,
nautilus_default_file_icon_width * 4, /* stride */
NULL, /* don't destroy data */
NULL);
factory->fallback_icon = cache_icon_new (pixbuf, NULL, 1.0, 1.0);
g_object_unref(pixbuf);
factory->image_mime_types = g_hash_table_new (g_str_hash, g_str_equal);
for (i = 0; i < G_N_ELEMENTS (types); i++) {
g_hash_table_insert (factory->image_mime_types,
(gpointer) types [i],
GUINT_TO_POINTER (1));
}
}
static void
nautilus_icon_factory_class_init (NautilusIconFactoryClass *class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (class);
signals[ICONS_CHANGED]
= g_signal_new ("icons_changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
object_class->finalize = nautilus_icon_factory_finalize;
}
static void
cache_key_destroy (CacheKey *key)
{
g_free (key->name);
g_free (key->modifier);
g_free (key);
}
static CacheIcon *
cache_icon_new (GdkPixbuf *pixbuf,
GtkIconInfo *info,
double scale_x, double scale_y)
{
CacheIcon *icon;
GdkRectangle rect;
int i;
/* Grab the pixbuf since we are keeping it. */
g_object_ref (pixbuf);
/* Make the icon. */
icon = g_new0 (CacheIcon, 1);
icon->ref_count = 1;
icon->pixbuf = pixbuf;
icon->mtime = 0;
if (info) {
icon->display_name = g_strdup (gtk_icon_info_get_display_name (info));
if (gtk_icon_info_get_embedded_rect (info, &rect)) {
rect.x *= scale_x;
rect.width *= scale_x;
rect.y *= scale_y;
rect.height *= scale_y;
icon->embedded_text_rect = g_memdup (&rect, sizeof (rect));
}
if (gtk_icon_info_get_attach_points (info,
&icon->attach_points,
&icon->n_attach_points)) {
for (i = 0; i < icon->n_attach_points; i++) {
icon->attach_points[i].x *= scale_x;
icon->attach_points[i].y *= scale_x;
}
}
}
return icon;
}
static void
cache_icon_ref (CacheIcon *icon)
{
g_assert (icon != NULL);
g_assert (icon->ref_count >= 1);
icon->ref_count++;
}
static void
cache_icon_unref (CacheIcon *icon)
{
CircularList *node;
NautilusIconFactory *factory;
g_assert (icon != NULL);
g_assert (icon->ref_count >= 1);
if (icon->ref_count > 1) {
icon->ref_count--;
return;
}
icon->ref_count = 0;
factory = get_icon_factory ();
check_recently_used_list ();
/* If it's in the recently used list, free it from there */
node = &icon->recently_used_node;
if (node->next != NULL) {
#if CACHE_SELF_CHECKS
g_assert (factory->recently_used_count >= 1);
g_assert (node->next->prev == node);
g_assert (node->prev->next == node);
g_assert (node->next != node);
g_assert (node->prev != node);
#endif
node->next->prev = node->prev;
node->prev->next = node->next;
node->next = NULL;
node->prev = NULL;
factory->recently_used_count -= 1;
}
check_recently_used_list ();
g_object_unref (icon->pixbuf);
g_free (icon->display_name);
g_free (icon->embedded_text_rect);
g_free (icon->attach_points);
g_free (icon);
}
static gboolean
nautilus_icon_factory_possibly_free_cached_icon (gpointer key,
gpointer value,
gpointer user_data)
{
CacheIcon *icon;
icon = value;
/* Don't free a cache entry that is in the recently used list. */
if (icon->recently_used_node.next != NULL) {
return FALSE;
}
/* Don't free a cache entry if the pixbuf is still in use. */
if (G_OBJECT (icon->pixbuf)->ref_count > 1) {
return FALSE;
}
/* Free the item. */
return TRUE;
}
/* Sweep the cache, freeing any icons that are not in use and are
* also not recently used.
*/
static gboolean
nautilus_icon_factory_sweep (gpointer user_data)
{
NautilusIconFactory *factory;
factory = user_data;
g_hash_table_foreach_remove (factory->icon_cache,
nautilus_icon_factory_possibly_free_cached_icon,
NULL);
factory->sweep_timer = 0;
return FALSE;
}
/* Schedule a timer to do a sweep. */
static void
nautilus_icon_factory_schedule_sweep (NautilusIconFactory *factory)
{
if (factory->sweep_timer != 0) {
return;
}
factory->sweep_timer = g_timeout_add (ICON_CACHE_SWEEP_TIMEOUT,
nautilus_icon_factory_sweep,
factory);
}
/* Move this item to the head of the recently-used list,
* bumping the last item off that list if necessary.
*/
static void
mark_recently_used (CircularList *node)
{
NautilusIconFactory *factory;
CircularList *head, *last_node;
check_recently_used_list ();
factory = get_icon_factory ();
head = &factory->recently_used_dummy_head;
/* Move the node to the start of the list. */
if (node->prev != head) {
if (node->next != NULL) {
/* Remove the node from its current position in the list. */
node->next->prev = node->prev;
node->prev->next = node->next;
} else {
/* Node was not already in the list, so add it.
* If the list is already full, remove the last node.
*/
if (factory->recently_used_count < ICON_CACHE_COUNT) {
factory->recently_used_count += 1;
} else {
/* Remove the last node. */
last_node = head->prev;
#if CACHE_SELF_CHECKS
g_assert (last_node != head);
g_assert (last_node != node);
#endif
head->prev = last_node->prev;
last_node->prev->next = head;
last_node->prev = NULL;
last_node->next = NULL;
}
}
/* Insert the node at the head of the list. */
node->prev = head;
node->next = head->next;
node->next->prev = node;
head->next = node;
}
check_recently_used_list ();
}
static gboolean
remove_all (gpointer key, gpointer value, gpointer user_data)
{
/* Tell the caller to remove the hash table entry. */
return TRUE;
}
/* Reset the cache to the default state. */
static void
nautilus_icon_factory_clear (void)
{
NautilusIconFactory *factory;
CircularList *head;
factory = get_icon_factory ();
g_hash_table_foreach_remove (factory->icon_cache,
remove_all,
NULL);
/* Empty out the recently-used list. */
head = &factory->recently_used_dummy_head;
/* fallback_icon hangs around, but we don't know if it
* was ever inserted in the list
*/
g_assert (factory->recently_used_count == 0 ||
factory->recently_used_count == 1);
if (factory->recently_used_count == 1) {
/* make sure this one is the fallback_icon */
g_assert (head->next == head->prev);
g_assert (&factory->fallback_icon->recently_used_node == head->next);
}
}
static void
nautilus_icon_factory_finalize (GObject *object)
{
NautilusIconFactory *factory;
factory = NAUTILUS_ICON_FACTORY (object);
if (factory->icon_cache) {
g_hash_table_destroy (factory->icon_cache);
factory->icon_cache = NULL;
}
if (factory->thumbnail_frame != NULL) {
g_object_unref (factory->thumbnail_frame);
factory->thumbnail_frame = NULL;
}
if (factory->fallback_icon) {
g_assert (factory->fallback_icon->ref_count == 1);
cache_icon_unref (factory->fallback_icon);
}
if (factory->image_mime_types) {
g_hash_table_destroy (factory->image_mime_types);
factory->image_mime_types = NULL;
}
EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}
static void
thumbnail_limit_changed_callback (gpointer user_data)
{
cached_thumbnail_limit = eel_preferences_get_integer (NAUTILUS_PREFERENCES_IMAGE_FILE_THUMBNAIL_LIMIT);
/* Tell the world that icons might have changed. We could invent a narrower-scope
* signal to mean only "thumbnails might have changed" if this ends up being slow
* for some reason.
*/
nautilus_icon_factory_clear ();
g_signal_emit (global_icon_factory,
signals[ICONS_CHANGED], 0);
}
static void
show_thumbnails_changed_callback (gpointer user_data)
{
show_image_thumbs = eel_preferences_get_enum (NAUTILUS_PREFERENCES_SHOW_IMAGE_FILE_THUMBNAILS);
nautilus_icon_factory_clear ();
/* If the user disabled thumbnailing, remove all outstanding thumbnails */
if (show_image_thumbs == NAUTILUS_SPEED_TRADEOFF_NEVER) {
nautilus_thumbnail_remove_all_from_queue ();
}
g_signal_emit (global_icon_factory,
signals[ICONS_CHANGED], 0);
}
static void
mime_type_data_changed_callback (GnomeVFSMIMEMonitor *monitor, gpointer user_data)
{
g_assert (monitor != NULL);
g_assert (user_data == NULL);
/* We don't know which data changed, so we have to assume that
* any or all icons might have changed.
*/
nautilus_icon_factory_clear ();
g_signal_emit (get_icon_factory (),
signals[ICONS_CHANGED], 0);
}
static char *
nautilus_remove_icon_file_name_suffix (const char *icon_name)
{
guint i;
const char *suffix;
static const char *icon_file_name_suffixes[] = { ".svg", ".svgz", ".png", ".jpg", ".xpm" };
for (i = 0; i < G_N_ELEMENTS (icon_file_name_suffixes); i++) {
suffix = icon_file_name_suffixes[i];
if (eel_str_has_suffix (icon_name, suffix)) {
return eel_str_strip_trailing_str (icon_name, suffix);
}
}
return g_strdup (icon_name);
}
static char *
image_uri_to_name_or_uri (const char *image_uri)
{
char *icon_path;
icon_path = gnome_vfs_get_local_path_from_uri (image_uri);
if (icon_path == NULL && image_uri[0] == '/') {
icon_path = g_strdup (image_uri);
}
if (icon_path != NULL) {
return icon_path;
} else if (strpbrk (image_uri, ":/") == NULL) {
return nautilus_remove_icon_file_name_suffix (image_uri);
}
return NULL;
}
static gboolean
mimetype_limited_by_size (const char *mime_type)
{
NautilusIconFactory *factory;
factory = get_icon_factory();
if (g_hash_table_lookup (factory->image_mime_types, mime_type)) {
return TRUE;
}
return FALSE;
}
static gboolean
should_show_thumbnail (NautilusFile *file, const char *mime_type)
{
if (mimetype_limited_by_size (mime_type) &&
nautilus_file_get_size (file) > (unsigned int)cached_thumbnail_limit) {
return FALSE;
}
if (show_image_thumbs == NAUTILUS_SPEED_TRADEOFF_ALWAYS) {
return TRUE;
} else if (show_image_thumbs == NAUTILUS_SPEED_TRADEOFF_NEVER) {
return FALSE;
} else {
/* only local files */
return nautilus_file_is_local (file);
}
return FALSE;
}
/* key routine to get the icon for a file */
char *
nautilus_icon_factory_get_icon_for_file (NautilusFile *file, gboolean embedd_text)
{
char *custom_uri, *file_uri, *icon_name, *mime_type, *custom_icon;
NautilusIconFactory *factory;
GnomeIconLookupResultFlags lookup_result;
GnomeVFSFileInfo *file_info;
GnomeThumbnailFactory *thumb_factory;
gboolean show_thumb;
GnomeIconLookupFlags lookup_flags;
if (file == NULL) {
return NULL;
}
factory = get_icon_factory ();
custom_icon = NULL;
/* if there is a custom image in the metadata or link info, use that. */
custom_uri = nautilus_file_get_custom_icon (file);
if (custom_uri) {
custom_icon = image_uri_to_name_or_uri (custom_uri);
}
g_free (custom_uri);
file_uri = nautilus_file_get_uri (file);
if (strcmp (file_uri, "burn:///") == 0) {
g_free (file_uri);
return g_strdup ("gnome-dev-cdrom");
}
if (strcmp (file_uri, "computer:///") == 0) {
g_free (file_uri);
return g_strdup ("gnome-fs-client");
}
if (strcmp (file_uri, EEL_TRASH_URI) == 0) {
g_free (file_uri);
return g_strdup (nautilus_trash_monitor_is_empty ()
? ICON_NAME_TRASH_EMPTY : ICON_NAME_TRASH_FULL);
}
if (nautilus_file_is_home (file)) {
g_free (file_uri);
return g_strdup (ICON_NAME_HOME);
}
mime_type = nautilus_file_get_mime_type (file);
file_info = nautilus_file_peek_vfs_file_info (file);
show_thumb = should_show_thumbnail (file, mime_type);
if (show_thumb) {
thumb_factory = factory->thumbnail_factory;
} else {
thumb_factory = NULL;
}
lookup_flags = GNOME_ICON_LOOKUP_FLAGS_SHOW_SMALL_IMAGES_AS_THEMSELVES;
if (embedd_text) {
lookup_flags |= GNOME_ICON_LOOKUP_FLAGS_EMBEDDING_TEXT;
}
icon_name = gnome_icon_lookup (factory->icon_theme,
thumb_factory,
file_uri,
custom_icon,
nautilus_file_peek_vfs_file_info (file),
mime_type,
lookup_flags,
&lookup_result);
/* Create thumbnails if we can, and if the looked up icon isn't a thumbnail
or an absolute pathname (custom icon or image as itself) */
if (show_thumb &&
!(lookup_result & GNOME_ICON_LOOKUP_RESULT_FLAGS_THUMBNAIL) &&
icon_name[0] != '/' && file_info &&
gnome_thumbnail_factory_can_thumbnail (factory->thumbnail_factory,
file_uri,
mime_type,
file_info->mtime)) {
nautilus_create_thumbnail (file);
g_free (icon_name);
icon_name = g_strdup (ICON_NAME_THUMBNAIL_LOADING);
}
g_free (file_uri);
g_free (custom_icon);
g_free (mime_type);
return icon_name;
}
/**
* nautilus_icon_factory_get_required_file_attributes
*
* Get the file attributes required to obtain a file's icon.
*/
NautilusFileAttributes
nautilus_icon_factory_get_required_file_attributes (void)
{
return NAUTILUS_FILE_ATTRIBUTE_CUSTOM_ICON |
NAUTILUS_FILE_ATTRIBUTE_MIME_TYPE;
}
/**
* nautilus_icon_factory_is_icon_ready_for_file
*
* Check whether a NautilusFile has enough information to report
* what its icon should be.
*
* @file: The NautilusFile in question.
*/
gboolean
nautilus_icon_factory_is_icon_ready_for_file (NautilusFile *file)
{
NautilusFileAttributes attributes;
gboolean result;
attributes = nautilus_icon_factory_get_required_file_attributes ();
result = nautilus_file_check_if_ready (file, attributes);
return result;
}
char *
nautilus_icon_factory_get_emblem_icon_by_name (const char *emblem_name)
{
char *name_with_prefix;
name_with_prefix = g_strconcat (NAUTILUS_EMBLEM_NAME_PREFIX, emblem_name, NULL);
return name_with_prefix;
}
GList *
nautilus_icon_factory_get_emblem_icons_for_file (NautilusFile *file,
EelStringList *exclude)
{
GList *icons, *emblem_names, *node;
char *uri, *name;
char *icon;
gboolean file_is_trash;
icons = NULL;
emblem_names = nautilus_file_get_emblem_names (file);
for (node = emblem_names; node != NULL; node = node->next) {
name = node->data;
if (strcmp (name, NAUTILUS_FILE_EMBLEM_NAME_TRASH) == 0) {
/* Leave out the trash emblem for the trash itself, since
* putting a trash emblem on a trash icon is gilding the
* lily.
*/
uri = nautilus_file_get_uri (file);
file_is_trash = strcmp (uri, EEL_TRASH_URI) == 0;
g_free (uri);
if (file_is_trash) {
continue;
}
}
if (eel_string_list_contains (exclude, name)) {
continue;
}
icon = nautilus_icon_factory_get_emblem_icon_by_name (name);
icons = g_list_prepend (icons, icon);
}
eel_g_list_free_deep (emblem_names);
return g_list_reverse (icons);
}
static guint
get_larger_icon_size (guint size)
{
if (size < NAUTILUS_ICON_SIZE_SMALLEST) {
return NAUTILUS_ICON_SIZE_SMALLEST;
}
if (size < NAUTILUS_ICON_SIZE_FOR_MENUS) {
return NAUTILUS_ICON_SIZE_FOR_MENUS;
}
if (size < NAUTILUS_ICON_SIZE_SMALLER) {
return NAUTILUS_ICON_SIZE_SMALLER;
}
if (size < NAUTILUS_ICON_SIZE_SMALL) {
return NAUTILUS_ICON_SIZE_SMALL;
}
if (size < NAUTILUS_ICON_SIZE_STANDARD) {
return NAUTILUS_ICON_SIZE_STANDARD;
}
if (size < NAUTILUS_ICON_SIZE_LARGE) {
return NAUTILUS_ICON_SIZE_LARGE;
}
if (size < NAUTILUS_ICON_SIZE_LARGER) {
return NAUTILUS_ICON_SIZE_LARGER;
}
return NAUTILUS_ICON_SIZE_LARGEST;
}
static guint
get_smaller_icon_size (guint size)
{
if (size > NAUTILUS_ICON_SIZE_LARGEST) {
return NAUTILUS_ICON_SIZE_LARGEST;
}
if (size > NAUTILUS_ICON_SIZE_LARGER) {
return NAUTILUS_ICON_SIZE_LARGER;
}
if (size > NAUTILUS_ICON_SIZE_LARGE) {
return NAUTILUS_ICON_SIZE_LARGE;
}
if (size > NAUTILUS_ICON_SIZE_STANDARD) {
return NAUTILUS_ICON_SIZE_STANDARD;
}
if (size > NAUTILUS_ICON_SIZE_SMALL) {
return NAUTILUS_ICON_SIZE_SMALL;
}
if (size > NAUTILUS_ICON_SIZE_SMALLER) {
return NAUTILUS_ICON_SIZE_SMALLER;
}
if (size > NAUTILUS_ICON_SIZE_FOR_MENUS) {
return NAUTILUS_ICON_SIZE_FOR_MENUS;
}
return NAUTILUS_ICON_SIZE_SMALLEST;
}
/* This loads an SVG image, scaling it to the appropriate size. */
static GdkPixbuf *
load_pixbuf_svg (const char *path,
guint size_in_pixels,
guint base_size,
double *scale_x,
double *scale_y)
{
double zoom;
int width, height;
GdkPixbuf *pixbuf;
if (base_size != 0) {
zoom = (double)size_in_pixels / base_size;
pixbuf = rsvg_pixbuf_from_file_at_zoom_with_max (path, zoom, zoom, NAUTILUS_ICON_MAXIMUM_SIZE, NAUTILUS_ICON_MAXIMUM_SIZE, NULL);
} else {
pixbuf = rsvg_pixbuf_from_file_at_max_size (path,
size_in_pixels,
size_in_pixels,
NULL);
}
if (pixbuf == NULL) {
return NULL;
}
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
*scale_x = width / 1000.0;
*scale_y = height / 1000.0;
return pixbuf;
}
static gboolean
path_represents_svg_image (const char *path)
{
/* Synchronous mime sniffing is a really bad idea here
* since it's only useful for people adding custom icons,
* and if they're doing that, they can behave themselves
* and use a .svg extension.
*/
return path != NULL && (strstr (path, ".svg") != NULL || strstr (path, ".svgz") != NULL);
}
static GdkPixbuf *
scale_icon (GdkPixbuf *pixbuf,
double *scale)
{
guint width, height;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
if ((int) (width * *scale) > NAUTILUS_ICON_MAXIMUM_SIZE ||
(int) (height * *scale) > NAUTILUS_ICON_MAXIMUM_SIZE) {
*scale = MIN ((double) NAUTILUS_ICON_MAXIMUM_SIZE / width,
(double) NAUTILUS_ICON_MAXIMUM_SIZE / height);
}
width = floor (width * *scale + 0.5);
height = floor (height * *scale + 0.5);
return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
}
static GdkPixbuf *
load_icon_file (const char *filename,
guint base_size,
guint nominal_size,
gboolean force_nominal,
double *scale_x,
double *scale_y)
{
GdkPixbuf *pixbuf, *scaled_pixbuf;
int width, height, size;
double scale;
gboolean is_thumbnail;
gboolean add_frame = FALSE;
*scale_x = 1.0;
*scale_y = 1.0;
if (path_represents_svg_image (filename)) {
pixbuf = load_pixbuf_svg (filename,
nominal_size,
force_nominal ? 0 : base_size,
scale_x, scale_y);
} else {
is_thumbnail = strstr (filename, "/.thumbnails/") != NULL;
/* FIXME: Maybe we shouldn't have to load the file each time
* Not sure if that is important */
if (is_thumbnail) {
pixbuf = nautilus_thumbnail_load_framed_image (filename);
} else {
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
}
if (pixbuf == NULL) {
return NULL;
}
if (force_nominal) {
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
base_size = MAX (width, height);
} else if (base_size == 0) {
if (is_thumbnail) {
base_size = 128 * NAUTILUS_ICON_SIZE_STANDARD / NAUTILUS_ICON_SIZE_THUMBNAIL;
} else {
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
size = MAX (width, height);
if (size > NAUTILUS_ICON_SIZE_THUMBNAIL && !gdk_pixbuf_get_has_alpha(pixbuf)) {
add_frame=TRUE;
}
if (size > nominal_size * NAUTILUS_ICON_SIZE_THUMBNAIL / NAUTILUS_ICON_SIZE_STANDARD) {
base_size = size * NAUTILUS_ICON_SIZE_STANDARD / NAUTILUS_ICON_SIZE_THUMBNAIL;
}
else if (size > NAUTILUS_ICON_SIZE_STANDARD) {
base_size = nominal_size;
} else {
/* Don't scale up small icons */
base_size = NAUTILUS_ICON_SIZE_STANDARD;
}
}
}
if (base_size != nominal_size) {
scale = (double)nominal_size/base_size;
scaled_pixbuf = scale_icon (pixbuf, &scale);
*scale_x = scale;
*scale_y = scale;
g_object_unref (pixbuf);
pixbuf = scaled_pixbuf;
}
}
if (add_frame){
nautilus_thumbnail_frame_image(&pixbuf);
}
return pixbuf;
}
static CacheIcon *
create_normal_cache_icon (const char *icon,
const char *modifier,
guint nominal_size,
gboolean force_nominal)
{
NautilusIconFactory *factory;
const char *filename;
char *name_with_modifier;
GtkIconInfo *info;
CacheIcon *cache_icon;
GdkPixbuf *pixbuf;
int base_size;
struct stat statbuf;
time_t mtime;
double scale_x, scale_y;
factory = get_icon_factory ();
info = NULL;
filename = NULL;
mtime = 0;
base_size = 0;
if (icon[0] == '/') {
/* FIXME: maybe we should add modifier to the filename
* before the extension */
if (stat (icon, &statbuf) == 0 &&
S_ISREG (statbuf.st_mode)) {
filename = icon;
mtime = statbuf.st_mtime;
}
} else {
if (modifier) {
name_with_modifier = g_strconcat (icon, "-", modifier, NULL);
} else {
name_with_modifier = (char *)icon;
}
info = gtk_icon_theme_lookup_icon (factory->icon_theme,
name_with_modifier,
nominal_size,
GTK_ICON_LOOKUP_FORCE_SVG);
if (name_with_modifier != icon) {
g_free (name_with_modifier);
}
if (info == NULL) {
return NULL;
}
gtk_icon_info_set_raw_coordinates (info, TRUE);
base_size = gtk_icon_info_get_base_size (info);
filename = gtk_icon_info_get_filename (info);
}
/* If e.g. the absolute filename doesn't exist */
if (filename == NULL) {
return NULL;
}
pixbuf = load_icon_file (filename,
base_size,
nominal_size,
force_nominal,
&scale_x, &scale_y);
if (pixbuf == NULL) {
if (info) {
gtk_icon_info_free (info);
}
return NULL;
}
cache_icon = cache_icon_new (pixbuf, info, scale_x, scale_y);
cache_icon->mtime = mtime;
if (info) {
gtk_icon_info_free (info);
}
g_object_unref (pixbuf);
return cache_icon;
}
/* Get the icon, handling the caching.
* If @picky is true, then only an unscaled icon is acceptable.
* Also, if @picky is true, the icon must be a custom icon if
* @custom is true or a standard icon is @custom is false.
* If @force_nominal is #TRUE, the returned icon will be guaranteed
* to be smaller than the nominal size
*/
static CacheIcon *
get_icon_from_cache (const char *icon,
const char *modifier,
guint nominal_size,
gboolean force_nominal)
{
NautilusIconFactory *factory;
GHashTable *hash_table;
CacheKey lookup_key;
CacheKey *key;
CacheIcon *cached_icon;
gpointer key_in_table, value;
struct stat statbuf;
g_return_val_if_fail (icon != NULL, NULL);
key = NULL;
cached_icon = NULL;
factory = get_icon_factory ();
hash_table = factory->icon_cache;
/* Check to see if it's already in the table. */
lookup_key.name = (char *)icon;
lookup_key.modifier = (char *)modifier;
lookup_key.nominal_size = nominal_size;
lookup_key.force_nominal = force_nominal;
if (g_hash_table_lookup_extended (hash_table, &lookup_key,
&key_in_table, &value)) {
/* Found it in the table. */
g_assert (key_in_table != NULL);
g_assert (value != NULL);
key = key_in_table;
cached_icon = value;
}
/* Make sure that thumbnails and image-as-itself icons gets
reloaded when they change: */
if (cached_icon && icon[0] == '/') {
if (stat (icon, &statbuf) != 0 ||
!S_ISREG (statbuf.st_mode) ||
statbuf.st_mtime != cached_icon->mtime) {
cached_icon = NULL;
}
}
if (cached_icon == NULL) {
/* Not in the table, so load the image. */
/*
g_print ("cache miss for %s:%s:%s:%d\n",
icon, modifier?modifier:"", embedded_text?"<tl>":"", nominal_size);
*/
cached_icon = create_normal_cache_icon (icon,
modifier,
nominal_size,
force_nominal);
/* Try to fallback without modifier */
if (cached_icon == NULL && modifier != NULL) {
cached_icon = create_normal_cache_icon (icon,
NULL,
nominal_size,
force_nominal);
}
if (cached_icon == NULL) {
cached_icon = factory->fallback_icon;
cache_icon_ref (cached_icon);
}
/* Create the key and icon for the hash table. */
key = g_new (CacheKey, 1);
key->name = g_strdup (icon);
key->modifier = g_strdup (modifier);
key->nominal_size = nominal_size;
key->force_nominal = force_nominal;
g_hash_table_insert (hash_table, key, cached_icon);
}
/* Hand back a ref to the caller. */
cache_icon_ref (cached_icon);
/* Since this item was used, keep it in the cache longer. */
mark_recently_used (&cached_icon->recently_used_node);
/* Come back later and sweep the cache. */
nautilus_icon_factory_schedule_sweep (factory);
return cached_icon;
}
static GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_icon_internal (const char *icon,
const char *modifier,
guint nominal_size,
gboolean force_size,
NautilusEmblemAttachPoints *attach_points,
GdkRectangle *embedded_text_rect,
gboolean wants_default,
char **display_name)
{
NautilusIconFactory *factory;
CacheIcon *cached_icon;
GdkPixbuf *pixbuf;
int i;
factory = get_icon_factory ();
cached_icon = get_icon_from_cache (icon,
modifier,
nominal_size,
force_size);
if (attach_points != NULL) {
if (cached_icon->attach_points != NULL) {
attach_points->num_points = MIN (cached_icon->n_attach_points,
MAX_ATTACH_POINTS);
for (i = 0; i < attach_points->num_points; i++) {
attach_points->points[i].x = cached_icon->attach_points[i].x;
attach_points->points[i].y = cached_icon->attach_points[i].y;
}
} else {
attach_points->num_points = 0;
}
}
if (embedded_text_rect) {
if (cached_icon->embedded_text_rect != NULL) {
*embedded_text_rect = *cached_icon->embedded_text_rect;
} else {
embedded_text_rect->x = 0;
embedded_text_rect->y = 0;
embedded_text_rect->width = 0;
embedded_text_rect->height = 0;
}
}
if (display_name != NULL) {
*display_name = g_strdup (cached_icon->display_name);
}
/* if we don't want a default icon and one is returned, return NULL instead */
if (!wants_default && cached_icon == factory->fallback_icon) {
cache_icon_unref (cached_icon);
return NULL;
}
pixbuf = cached_icon->pixbuf;
g_object_ref (pixbuf);
cache_icon_unref (cached_icon);
return pixbuf;
}
GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_icon (const char *icon,
const char *modifier,
guint nominal_size,
NautilusEmblemAttachPoints *attach_points,
GdkRectangle *embedded_text_rect,
gboolean wants_default,
char **display_name)
{
return nautilus_icon_factory_get_pixbuf_for_icon_internal (icon, modifier, nominal_size, FALSE,
attach_points, embedded_text_rect,
wants_default, display_name);
}
GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_icon_force_size (const char *icon,
const char *modifier,
guint nominal_size,
NautilusEmblemAttachPoints *attach_points,
GdkRectangle *embedded_text_rect,
gboolean wants_default,
char **display_name)
{
return nautilus_icon_factory_get_pixbuf_for_icon_internal (icon, modifier, nominal_size, TRUE,
attach_points, embedded_text_rect,
wants_default, display_name);
}
static guint
cache_key_hash (gconstpointer p)
{
const CacheKey *key;
guint hash;
key = p;
hash = g_str_hash (key->name) ^
((key->nominal_size << 4) + (gint)key->force_nominal);
if (key->modifier) {
hash ^= g_str_hash (key->modifier);
}
return hash;
}
static gboolean
cache_key_equal (gconstpointer a, gconstpointer b)
{
const CacheKey *key_a, *key_b;
key_a = a;
key_b = b;
return eel_strcmp (key_a->name, key_b->name) == 0 &&
key_a->nominal_size == key_b->nominal_size &&
key_a->force_nominal == key_b->force_nominal &&
eel_strcmp (key_a->modifier, key_b->modifier) == 0;
}
/* Return nominal icon size for given zoom level.
* @zoom_level: zoom level for which to find matching icon size.
*
* Return value: icon size between NAUTILUS_ICON_SIZE_SMALLEST and
* NAUTILUS_ICON_SIZE_LARGEST, inclusive.
*/
guint
nautilus_get_icon_size_for_zoom_level (NautilusZoomLevel zoom_level)
{
switch (zoom_level) {
case NAUTILUS_ZOOM_LEVEL_SMALLEST:
return NAUTILUS_ICON_SIZE_SMALLEST;
case NAUTILUS_ZOOM_LEVEL_SMALLER:
return NAUTILUS_ICON_SIZE_SMALLER;
case NAUTILUS_ZOOM_LEVEL_SMALL:
return NAUTILUS_ICON_SIZE_SMALL;
case NAUTILUS_ZOOM_LEVEL_STANDARD:
return NAUTILUS_ICON_SIZE_STANDARD;
case NAUTILUS_ZOOM_LEVEL_LARGE:
return NAUTILUS_ICON_SIZE_LARGE;
case NAUTILUS_ZOOM_LEVEL_LARGER:
return NAUTILUS_ICON_SIZE_LARGER;
case NAUTILUS_ZOOM_LEVEL_LARGEST:
return NAUTILUS_ICON_SIZE_LARGEST;
}
g_return_val_if_reached (NAUTILUS_ICON_SIZE_STANDARD);
}
float
nautilus_get_relative_icon_size_for_zoom_level (NautilusZoomLevel zoom_level)
{
return (float)nautilus_get_icon_size_for_zoom_level (zoom_level) / NAUTILUS_ICON_SIZE_STANDARD;
}
/* Convenience cover for nautilus_icon_factory_get_icon_for_file
* and nautilus_icon_factory_get_pixbuf_for_icon.
*/
static GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_file_internal (NautilusFile *file,
const char *modifier,
guint size_in_pixels,
gboolean force_size)
{
char *icon;
GdkPixbuf *pixbuf;
/* Get the pixbuf for this file. */
icon = nautilus_icon_factory_get_icon_for_file (file, FALSE);
if (icon == NULL) {
return NULL;
}
pixbuf = nautilus_icon_factory_get_pixbuf_for_icon_internal (icon, modifier,
size_in_pixels,
force_size,
NULL, NULL,
TRUE, NULL);
g_free (icon);
return pixbuf;
}
GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_file (NautilusFile *file,
const char *modifier,
guint size_in_pixels)
{
return nautilus_icon_factory_get_pixbuf_for_file_internal (file,
modifier,
size_in_pixels,
FALSE);
}
GdkPixbuf *
nautilus_icon_factory_get_pixbuf_for_file_force_size (NautilusFile *file,
const char *modifier,
guint size_in_pixels)
{
return nautilus_icon_factory_get_pixbuf_for_file_internal (file,
modifier,
size_in_pixels,
TRUE);
}
/* Convenience routine for getting a pixbuf from an icon name. */
GdkPixbuf *
nautilus_icon_factory_get_pixbuf_from_name (const char *icon_name,
const char *modifier,
guint size_in_pixels,
gboolean force_size,
char **display_name)
{
return nautilus_icon_factory_get_pixbuf_for_icon_internal (icon_name, modifier,
size_in_pixels, force_size,
NULL, NULL,
TRUE, display_name);
}
GdkPixbuf *
nautilus_icon_factory_get_thumbnail_frame (void)
{
return get_icon_factory ()->thumbnail_frame;
}
gboolean
nautilus_icon_factory_remove_from_cache (const char *icon_name,
const char *modifier,
guint size)
{
GHashTable *hash_table;
NautilusIconFactory *factory;
CacheKey lookup_key;
factory = get_icon_factory ();
hash_table = factory->icon_cache;
/* Check to see if it's already in the table. */
lookup_key.name = (char *)icon_name;
lookup_key.modifier = (char *)modifier;
lookup_key.nominal_size = size;
return g_hash_table_remove (hash_table, &lookup_key);
}
#if ! defined (NAUTILUS_OMIT_SELF_CHECK)
void
nautilus_self_check_icon_factory (void)
{
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (0), 12);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (1), 24);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (2), 36);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (3), 48);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (4), 72);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (5), 96);
EEL_CHECK_INTEGER_RESULT (nautilus_get_icon_size_for_zoom_level (6), 192);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (0), 12);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (1), 12);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (11), 12);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (12), 20);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (19), 20);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (20), 24);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (23), 24);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (24), 36);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (35), 36);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (36), 48);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (47), 48);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (48), 72);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (71), 72);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (72), 96);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (95), 96);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (96), 192);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (191), 192);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (192), 192);
EEL_CHECK_INTEGER_RESULT (get_larger_icon_size (0xFFFFFFFF), 192);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (0), 12);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (1), 12);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (11), 12);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (12), 12);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (20), 12);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (21), 20);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (24), 20);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (25), 24);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (36), 24);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (37), 36);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (48), 36);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (49), 48);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (72), 48);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (73), 72);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (96), 72);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (97), 96);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (192), 96);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (193), 192);
EEL_CHECK_INTEGER_RESULT (get_smaller_icon_size (0xFFFFFFFF), 192);
}
#endif /* ! NAUTILUS_OMIT_SELF_CHECK */
|