-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
IconCache.cpp
1999 lines (1596 loc) · 48.7 KB
/
IconCache.cpp
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
/*
Open Tracker License
Terms and Conditions
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice applies to all licensees
and shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Be Incorporated shall not be
used in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from Be Incorporated.
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders.
All rights reserved.
*/
// Icon cache is used for drawing node icons; it caches icons
// and reuses them for successive draws
//
// Possible performance improvements:
// - Mime API requires BBitmaps to retrieve bits and successive
// SetBits that cause app server contention
// Consider having special purpose "give me just the bits" calls
// to deal with that.
// - Related to this, node cache entries would only store the raw bits
// to cut down on number of BBitmaps and related overhead
// - Make the cache miss and fill case for the shared cache reuse the
// already calculated hash value
//
// Other ToDo items:
// Use lazily allocated bitmap arrays for every view for node icon cache
// drawing
// Have an overflow list for deleting shared icons, delete from the list
// every now and then
// Actual icon lookup sequence:
// icon from node
// preferred app for node -> icon for type
// preferred app for type -> icon for type
// metamime -> icon for type
// preferred app for supertype -> icon for type
// supertype metamime -> icon for type
// generic icon
#include <Debug.h>
#include <Screen.h>
#include <Volume.h>
#include <fs_info.h>
#include "Bitmaps.h"
#include "FSUtils.h"
#include "IconCache.h"
#include "MimeTypes.h"
#include "Model.h"
#if DEBUG
// #define LOG_DISK_HITS
// the LOG_DISK_HITS define is used to check that the disk is not hit more
// than needed - enable it, open a window with a bunch of poses, force
// it to redraw, shouldn't recache
// #define LOG_ADD_ITEM
#endif
// set up a few printing macros to get rid of a ton of debugging ifdefs in the code
#ifdef LOG_DISK_HITS
#define PRINT_DISK_HITS(ARGS) _debugPrintf ARGS
#else
#define PRINT_DISK_HITS(ARGS) (void)0
#endif
#ifdef LOG_ADD_ITEM
#define PRINT_ADD_ITEM(ARGS) _debugPrintf ARGS
#else
#define PRINT_ADD_ITEM(ARGS) (void)0
#endif
#undef NODE_CACHE_ASYNC_DRAWS
IconCacheEntry::IconCacheEntry()
: fLargeIcon(NULL),
fMiniIcon(NULL),
fHilitedLargeIcon(NULL),
fHilitedMiniIcon(NULL),
fAliasForIndex(-1)
{
}
IconCacheEntry::~IconCacheEntry()
{
if (fAliasForIndex < 0) {
delete fLargeIcon;
delete fMiniIcon;
delete fHilitedLargeIcon;
delete fHilitedMiniIcon;
// clean up a bit to leave the hash table entry in an initialized state
fLargeIcon = NULL;
fMiniIcon = NULL;
fHilitedLargeIcon = NULL;
fHilitedMiniIcon = NULL;
}
fAliasForIndex = -1;
}
void
IconCacheEntry::SetAliasFor(const SharedIconCache* sharedCache,
const SharedCacheEntry* entry)
{
sharedCache->SetAliasFor(this, entry);
ASSERT(fAliasForIndex >= 0);
}
IconCacheEntry*
IconCacheEntry::ResolveIfAlias(const SharedIconCache* sharedCache)
{
return sharedCache->ResolveIfAlias(this);
}
IconCacheEntry*
IconCacheEntry::ResolveIfAlias(const SharedIconCache* sharedCache,
IconCacheEntry* entry)
{
if (!entry)
return NULL;
return sharedCache->ResolveIfAlias(entry);
}
bool
IconCacheEntry::CanConstructBitmap(IconDrawMode mode, icon_size) const
{
if (mode == kSelected)
// for now only
return true;
return false;
}
bool
IconCacheEntry::HaveIconBitmap(IconDrawMode mode, icon_size size) const
{
ASSERT(mode == kSelected || mode == kNormalIcon);
// for now only
if (mode == kNormalIcon) {
if (size == B_MINI_ICON)
return fMiniIcon != NULL;
else
return fLargeIcon != NULL
&& fLargeIcon->Bounds().IntegerWidth() + 1 == size;
} else if (mode == kSelected) {
if (size == B_MINI_ICON)
return fHilitedMiniIcon != NULL;
else
return fHilitedLargeIcon != NULL
&& fHilitedLargeIcon->Bounds().IntegerWidth() + 1 == size;
}
return false;
}
BBitmap*
IconCacheEntry::IconForMode(IconDrawMode mode, icon_size size) const
{
ASSERT(mode == kSelected || mode == kNormalIcon);
// for now only
if (mode == kNormalIcon) {
if (size == B_MINI_ICON)
return fMiniIcon;
else
return fLargeIcon;
} else if (mode == kSelected) {
if (size == B_MINI_ICON)
return fHilitedMiniIcon;
else
return fHilitedLargeIcon;
}
return NULL;
}
bool
IconCacheEntry::IconHitTest(BPoint where, IconDrawMode mode, icon_size size) const
{
ASSERT(where.x < size && where.y < size);
BBitmap* bitmap = IconForMode(mode, size);
if (!bitmap)
return false;
uchar* bits = (uchar*)bitmap->Bits();
ASSERT(bits);
BRect bounds(bitmap->Bounds());
bounds.InsetBy((bounds.Width() + 1.0) / 8.0, (bounds.Height() + 1.0) / 8.0);
if (bounds.Contains(where))
return true;
switch (bitmap->ColorSpace()) {
case B_RGBA32:
// test alpha channel
return *(bits + (int32)(floorf(where.y) * bitmap->BytesPerRow()
+ floorf(where.x) * 4 + 3)) > 20;
case B_CMAP8:
return *(bits + (int32)(floorf(where.y) * size + where.x))
!= B_TRANSPARENT_8_BIT;
default:
return true;
}
}
BBitmap*
IconCacheEntry::ConstructBitmap(BBitmap* constructFrom,
IconDrawMode requestedMode, IconDrawMode constructFromMode,
icon_size size, LazyBitmapAllocator* lazyBitmap)
{
ASSERT(requestedMode == kSelected && constructFromMode == kNormalIcon);
// for now
if (requestedMode == kSelected && constructFromMode == kNormalIcon) {
return IconCache::sIconCache->MakeSelectedIcon(constructFrom, size,
lazyBitmap);
}
return NULL;
}
BBitmap*
IconCacheEntry::ConstructBitmap(IconDrawMode requestedMode, icon_size size,
LazyBitmapAllocator* lazyBitmap)
{
BBitmap* source = (size == B_MINI_ICON) ? fMiniIcon : fLargeIcon;
ASSERT(source);
return ConstructBitmap(source, requestedMode, kNormalIcon, size,
lazyBitmap);
}
bool
IconCacheEntry::AlternateModeForIconConstructing(IconDrawMode requestedMode,
IconDrawMode &alternate, icon_size)
{
if (requestedMode & kSelected) {
// for now
alternate = kNormalIcon;
return true;
}
return false;
}
void
IconCacheEntry::SetIcon(BBitmap* bitmap, IconDrawMode mode, icon_size size,
bool /*create*/)
{
if (mode == kNormalIcon) {
if (size == B_MINI_ICON)
fMiniIcon = bitmap;
else
fLargeIcon = bitmap;
} else if (mode == kSelectedIcon) {
if (size == B_MINI_ICON)
fHilitedMiniIcon = bitmap;
else
fHilitedLargeIcon = bitmap;
} else
TRESPASS();
}
IconCache::IconCache()
: fInitHiliteTable(true)
{
InitHiliteTable();
}
// The following calls use the icon lookup sequence node-prefered app for
// node-metamime-preferred app for metamime to find an icon;
// if we are trying to get a specialized icon, we will first look for a normal
// icon in each of the locations, if we get a hit, we look for the
// specialized, if we don't find one, we try to auto-construct one, if we
// can't we assume the icon is not available for now the code only looks for
// normal icons, selected icons are auto-generated
IconCacheEntry*
IconCache::GetIconForPreferredApp(const char* fileTypeSignature,
const char* preferredApp, IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry)
{
ASSERT(fSharedCache.IsLocked());
if (!preferredApp[0])
return NULL;
if (!entry) {
entry = fSharedCache.FindItem(fileTypeSignature, preferredApp);
if (entry) {
entry = entry->ResolveIfAlias(&fSharedCache, entry);
#if xDEBUG
PRINT(("File %s; Line %d # looking for %s, type %s, found %x\n",
__FILE__, __LINE__, preferredApp, fileTypeSignature, entry));
#endif
if (entry->HaveIconBitmap(mode, size))
return entry;
}
}
if (!entry || !entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
PRINT_DISK_HITS(
("File %s; Line %d # hitting disk for preferredApp %s, type %s\n",
__FILE__, __LINE__, preferredApp, fileTypeSignature));
BMimeType preferredAppType(preferredApp);
BString signature(fileTypeSignature);
signature.ToLower();
if (preferredAppType.GetIconForType(signature.String(),
lazyBitmap->Get(), size) != B_OK) {
return NULL;
}
BBitmap* bitmap = lazyBitmap->Adopt();
if (!entry) {
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry for preferredApp %s, "
"type %s\n", __FILE__, __LINE__, preferredApp,
fileTypeSignature));
entry = fSharedCache.AddItem(fileTypeSignature, preferredApp);
}
entry->SetIcon(bitmap, kNormalIcon, size);
}
if (mode != kNormalIcon
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
return entry;
}
IconCacheEntry*
IconCache::GetIconFromMetaMime(const char* fileType, IconDrawMode mode,
icon_size size, LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry)
{
ASSERT(fSharedCache.IsLocked());
if (!entry)
entry = fSharedCache.FindItem(fileType);
if (entry) {
entry = entry->ResolveIfAlias(&fSharedCache, entry);
// metamime defines an icon and we have it cached
if (entry->HaveIconBitmap(mode, size))
return entry;
}
if (!entry || !entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
PRINT_DISK_HITS(("File %s; Line %d # hitting disk for metamime %s\n",
__FILE__, __LINE__, fileType));
BMimeType mime(fileType);
// try getting the icon directly from the metamime
if (mime.GetIcon(lazyBitmap->Get(), size) != B_OK) {
// try getting it from the preferred app of this type
char preferredAppSig[B_MIME_TYPE_LENGTH];
if (mime.GetPreferredApp(preferredAppSig) != B_OK)
return NULL;
SharedCacheEntry* aliasTo = NULL;
if (entry) {
aliasTo
= (SharedCacheEntry*)entry->ResolveIfAlias(&fSharedCache);
}
// look for icon defined by preferred app from metamime
aliasTo = (SharedCacheEntry*)GetIconForPreferredApp(fileType,
preferredAppSig, mode, size, lazyBitmap, aliasTo);
if (aliasTo == NULL)
return NULL;
// make an aliased entry so that the next time we get a
// hit on the first FindItem in here
if (!entry) {
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry as alias for type %s\n",
__FILE__, __LINE__, fileType));
entry = fSharedCache.AddItem(&aliasTo, fileType);
entry->SetAliasFor(&fSharedCache, aliasTo);
}
ASSERT(aliasTo->HaveIconBitmap(mode, size));
return aliasTo;
}
// at this point, we've found an icon for the MIME type
BBitmap* bitmap = lazyBitmap->Adopt();
if (!entry) {
PRINT_ADD_ITEM(("File %s; Line %d # adding entry for type %s\n",
__FILE__, __LINE__, fileType));
entry = fSharedCache.AddItem(fileType);
}
entry->SetIcon(bitmap, kNormalIcon, size);
}
ASSERT(entry);
if (mode != kNormalIcon
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
#if xDEBUG
if (!entry->HaveIconBitmap(mode, size))
PRINT(("failing on %s, mode %ld, size %ld\n", fileType, mode, size));
#endif
ASSERT(entry->HaveIconBitmap(mode, size));
return entry;
}
IconCacheEntry*
IconCache::GetIconFromFileTypes(ModelNodeLazyOpener* modelOpener,
IconSource &source, IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry)
{
ASSERT(fSharedCache.IsLocked());
// use file types to get the icon
Model* model = modelOpener->TargetModel();
const char* fileType = model->MimeType();
const char* nodePreferredApp = model->PreferredAppSignature();
if (source == kUnknownSource || source == kUnknownNotFromNode
|| source == kPreferredAppForNode) {
if (nodePreferredApp[0]) {
// file has a locally set preferred app, try getting an icon from
// there
entry = GetIconForPreferredApp(fileType, nodePreferredApp, mode,
size, lazyBitmap, entry);
#if xDEBUG
PRINT(("File %s; Line %d # looking for %s, type %s, found %x\n",
__FILE__, __LINE__, nodePreferredApp, fileType, entry));
#endif
if (entry) {
source = kPreferredAppForNode;
ASSERT(entry->HaveIconBitmap(mode, size));
return entry;
}
}
if (source == kPreferredAppForNode)
source = kUnknownSource;
}
entry = GetIconFromMetaMime(fileType, mode, size, lazyBitmap, entry);
if (!entry) {
// Try getting a supertype handler icon
BMimeType mime(fileType);
if (!mime.IsSupertypeOnly()) {
BMimeType superType;
mime.GetSupertype(&superType);
const char* superTypeFileType = superType.Type();
if (superTypeFileType)
entry = GetIconFromMetaMime(superTypeFileType, mode, size,
lazyBitmap, entry);
#if DEBUG
else
PRINT(
("File %s; Line %d # failed to get supertype for "
"type %s\n", __FILE__, __LINE__, fileType));
#endif
}
}
ASSERT(!entry || entry->HaveIconBitmap(mode, size));
if (entry) {
if (nodePreferredApp[0]) {
// we got a miss using GetIconForPreferredApp before, cache this
// fileType/preferredApp combo with an aliased entry
// make an aliased entry so that the next time we get a
// hit and substitute a generic icon right away
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry as alias for "
"preferredApp %s, type %s\n",
__FILE__, __LINE__, nodePreferredApp, fileType));
IconCacheEntry* aliasedEntry
= fSharedCache.AddItem((SharedCacheEntry**)&entry, fileType,
nodePreferredApp);
aliasedEntry->SetAliasFor(&fSharedCache,
(SharedCacheEntry*)entry);
// OK to cast here, have a runtime check
source = kPreferredAppForNode;
// set source as preferred for node, so that next time we
// get a hit in the initial find that uses
// GetIconForPreferredApp
} else
source = kMetaMime;
#if DEBUG
if (!entry->HaveIconBitmap(mode, size))
model->PrintToStream();
#endif
ASSERT(entry->HaveIconBitmap(mode, size));
}
return entry;
}
IconCacheEntry*
IconCache::GetVolumeIcon(AutoLock<SimpleIconCache>*nodeCacheLocker,
AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model* model, IconSource &source,
IconDrawMode mode, icon_size size, LazyBitmapAllocator* lazyBitmap)
{
*resultingOpenCache = nodeCacheLocker;
nodeCacheLocker->Lock();
IconCacheEntry* entry = 0;
if (source != kUnknownSource) {
// cached in the node cache
entry = fNodeCache.FindItem(model->NodeRef());
if (entry) {
entry = IconCacheEntry::ResolveIfAlias(&fSharedCache, entry);
if (source == kTrackerDefault) {
// if tracker default, resolved entry is from shared cache
// this could be done a little cleaner if entry had a way to
// reach the cache it is in
*resultingOpenCache = sharedCacheLocker;
sharedCacheLocker->Lock();
}
if (entry->HaveIconBitmap(mode, size))
return entry;
}
}
// try getting using the BVolume::GetIcon call; if miss,
// go for the default mime based icon
if (!entry || !entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
BVolume volume(model->NodeRef()->device);
if (volume.IsShared()) {
// Check if it's a network share and give it a special icon
BBitmap* bitmap = lazyBitmap->Get();
GetTrackerResources()->GetIconResource(R_ShareIcon, size, bitmap);
if (!entry) {
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry for model %s\n",
__FILE__, __LINE__, model->Name()));
entry = fNodeCache.AddItem(model->NodeRef());
}
entry->SetIcon(lazyBitmap->Adopt(), kNormalIcon, size);
} else if (volume.GetIcon(lazyBitmap->Get(), size) == B_OK) {
// Ask the device for an icon
BBitmap* bitmap = lazyBitmap->Adopt();
ASSERT(bitmap);
if (!entry) {
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry for model %s\n",
__FILE__, __LINE__, model->Name()));
entry = fNodeCache.AddItem(model->NodeRef());
}
ASSERT(entry);
entry->SetIcon(bitmap, kNormalIcon, size);
source = kVolume;
} else {
*resultingOpenCache = sharedCacheLocker;
sharedCacheLocker->Lock();
// If the volume doesnt have a device it should have
// the generic icon
entry = GetIconFromMetaMime(B_VOLUME_MIMETYPE, mode,
size, lazyBitmap, entry);
}
}
if (mode != kNormalIcon
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
return entry;
}
IconCacheEntry*
IconCache::GetRootIcon(AutoLock<SimpleIconCache>*,
AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model*, IconSource &source, IconDrawMode mode,
icon_size size, LazyBitmapAllocator* lazyBitmap)
{
*resultingOpenCache = sharedCacheLocker;
(*resultingOpenCache)->Lock();
source = kTrackerSupplied;
return GetIconFromMetaMime(B_ROOT_MIMETYPE, mode, size, lazyBitmap, 0);
}
IconCacheEntry*
IconCache::GetWellKnownIcon(AutoLock<SimpleIconCache>*,
AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model* model, IconSource &source, IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap)
{
const WellKnowEntryList::WellKnownEntry* wellKnownEntry
= WellKnowEntryList::MatchEntry(model->NodeRef());
if (!wellKnownEntry)
return NULL;
IconCacheEntry* entry = NULL;
BString type("tracker/active_");
type += wellKnownEntry->name;
*resultingOpenCache = sharedCacheLocker;
(*resultingOpenCache)->Lock();
source = kTrackerSupplied;
entry = fSharedCache.FindItem(type.String());
if (entry) {
entry = entry->ResolveIfAlias(&fSharedCache, entry);
if (entry->HaveIconBitmap(mode, size))
return entry;
}
if (!entry || !entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
// match up well known entries in the file system with specialized
// icons stored in Tracker's resources
int32 resid = -1;
switch ((uint32)wellKnownEntry->which) {
case B_BOOT_DISK:
resid = R_BootVolumeIcon;
break;
case B_BEOS_DIRECTORY:
resid = R_BeosFolderIcon;
break;
case B_USER_DIRECTORY:
resid = R_HomeDirIcon;
break;
case B_BEOS_FONTS_DIRECTORY:
case B_COMMON_FONTS_DIRECTORY:
case B_USER_FONTS_DIRECTORY:
resid = R_FontDirIcon;
break;
case B_BEOS_APPS_DIRECTORY:
case B_APPS_DIRECTORY:
case B_USER_DESKBAR_APPS_DIRECTORY:
resid = R_AppsDirIcon;
break;
case B_BEOS_PREFERENCES_DIRECTORY:
case B_PREFERENCES_DIRECTORY:
case B_USER_DESKBAR_PREFERENCES_DIRECTORY:
resid = R_PrefsDirIcon;
break;
case B_USER_MAIL_DIRECTORY:
resid = R_MailDirIcon;
break;
case B_USER_QUERIES_DIRECTORY:
resid = R_QueryDirIcon;
break;
case B_COMMON_DEVELOP_DIRECTORY:
case B_USER_DESKBAR_DEVELOP_DIRECTORY:
resid = R_DevelopDirIcon;
break;
case B_USER_CONFIG_DIRECTORY:
resid = R_ConfigDirIcon;
break;
case B_USER_PEOPLE_DIRECTORY:
resid = R_PersonDirIcon;
break;
case B_USER_DOWNLOADS_DIRECTORY:
resid = R_DownloadDirIcon;
break;
default:
return NULL;
}
entry = fSharedCache.AddItem(type.String());
BBitmap* bitmap = lazyBitmap->Get();
GetTrackerResources()->GetIconResource(resid, size, bitmap);
entry->SetIcon(lazyBitmap->Adopt(), kNormalIcon, size);
}
if (mode != kNormalIcon
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
ASSERT(entry->HaveIconBitmap(mode, size));
return entry;
}
IconCacheEntry*
IconCache::GetNodeIcon(ModelNodeLazyOpener* modelOpener,
AutoLock<SimpleIconCache>* nodeCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model* model, IconSource &source,
IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry, bool permanent)
{
*resultingOpenCache = nodeCacheLocker;
(*resultingOpenCache)->Lock();
entry = fNodeCache.FindItem(model->NodeRef());
if (!entry || !entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
modelOpener->OpenNode();
BFile* file = NULL;
// if we are dealing with an application, use the BAppFileInfo
// superset of node; this makes GetIcon grab the proper icon for
// an app
if (model->IsExecutable())
file = dynamic_cast<BFile*>(model->Node());
PRINT_DISK_HITS(("File %s; Line %d # hitting disk for node %s\n",
__FILE__, __LINE__, model->Name()));
status_t result;
if (file)
result = GetAppIconFromAttr(file, lazyBitmap->Get(), size);
else {
result = GetFileIconFromAttr(model->Node(), lazyBitmap->Get(),
size);
}
if (result == B_OK) {
// node has it's own icon, use it
BBitmap* bitmap = lazyBitmap->Adopt();
PRINT_ADD_ITEM(("File %s; Line %d # adding entry for model %s\n",
__FILE__, __LINE__, model->Name()));
entry = fNodeCache.AddItem(model->NodeRef(), permanent);
ASSERT(entry);
entry->SetIcon(bitmap, kNormalIcon, size);
if (mode != kNormalIcon) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
source = kNode;
}
}
if (!entry) {
(*resultingOpenCache)->Unlock();
*resultingOpenCache = NULL;
} else if (!entry->HaveIconBitmap(mode, size)
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
ASSERT(entry->HaveIconBitmap(mode, size));
}
return entry;
}
IconCacheEntry*
IconCache::GetGenericIcon(AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model* model, IconSource &source,
IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry)
{
*resultingOpenCache = sharedCacheLocker;
(*resultingOpenCache)->Lock();
entry = GetIconFromMetaMime(B_FILE_MIMETYPE, mode,
size, lazyBitmap, 0);
if (!entry)
return NULL;
// make an aliased entry so that the next time we get a
// hit and substitute a generic icon right away
PRINT_ADD_ITEM(
("File %s; Line %d # adding entry for preferredApp %s, type %s\n",
__FILE__, __LINE__, model->PreferredAppSignature(),
model->MimeType()));
IconCacheEntry* aliasedEntry = fSharedCache.AddItem(
(SharedCacheEntry**)&entry, model->MimeType(),
model->PreferredAppSignature());
aliasedEntry->SetAliasFor(&fSharedCache, (SharedCacheEntry*)entry);
source = kMetaMime;
ASSERT(entry->HaveIconBitmap(mode, size));
return entry;
}
IconCacheEntry*
IconCache::GetFallbackIcon(AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingOpenCache,
Model* model, IconDrawMode mode, icon_size size,
LazyBitmapAllocator* lazyBitmap, IconCacheEntry* entry)
{
*resultingOpenCache = sharedCacheLocker;
(*resultingOpenCache)->Lock();
entry = fSharedCache.AddItem(model->MimeType(),
model->PreferredAppSignature());
BBitmap* bitmap = lazyBitmap->Get();
GetTrackerResources()->GetIconResource(R_FileIcon, size, bitmap);
entry->SetIcon(lazyBitmap->Adopt(), kNormalIcon, size);
if (mode != kNormalIcon) {
entry->ConstructBitmap(mode, size, lazyBitmap);
entry->SetIcon(lazyBitmap->Adopt(), mode, size);
}
ASSERT(entry->HaveIconBitmap(mode, size));
return entry;
}
IconCacheEntry*
IconCache::Preload(AutoLock<SimpleIconCache>* nodeCacheLocker,
AutoLock<SimpleIconCache>* sharedCacheLocker,
AutoLock<SimpleIconCache>** resultingCache,
Model* model, IconDrawMode mode, icon_size size,
bool permanent)
{
IconCacheEntry* entry = NULL;
AutoLock<SimpleIconCache>* resultingOpenCache = NULL;
// resultingOpenCache is the locker that points to the cache that
// ended with a hit and will be used for the drawing
{ // scope for modelOpener
ModelNodeLazyOpener modelOpener(model);
// this opener takes care of opening the model and possibly
// closing it when we are done
LazyBitmapAllocator lazyBitmap(size);
// lazyBitmap manages bitmap allocation and freeing if needed
IconSource source = model->IconFrom();
if (source == kUnknownSource || source == kUnknownNotFromNode) {
// fish for special first models and handle them appropriately
if (model->IsVolume()) {
// volume may use specialized icon in the volume node
entry = GetNodeIcon(&modelOpener, nodeCacheLocker,
&resultingOpenCache, model, source, mode, size,
&lazyBitmap, entry, permanent);
if (!entry || !entry->HaveIconBitmap(mode, size))
// look for volume defined icon
entry = GetVolumeIcon(nodeCacheLocker, sharedCacheLocker,
&resultingOpenCache, model, source, mode,
size, &lazyBitmap);
} else if (model->IsRoot()) {
entry = GetRootIcon(nodeCacheLocker, sharedCacheLocker,
&resultingOpenCache, model, source, mode, size, &lazyBitmap);
ASSERT(entry);
} else {
if (source == kUnknownSource)
// look for node icons first
entry = GetNodeIcon(&modelOpener, nodeCacheLocker,
&resultingOpenCache, model, source,
mode, size, &lazyBitmap, entry, permanent);
if (!entry) {
// no node icon, look for file type based one
modelOpener.OpenNode();
// use file types to get the icon
resultingOpenCache = sharedCacheLocker;
resultingOpenCache->Lock();
entry = GetIconFromFileTypes(&modelOpener, source, mode, size,
&lazyBitmap, 0);
if (!entry) // we don't have an icon, go with the generic
entry = GetGenericIcon(sharedCacheLocker, &resultingOpenCache,
model, source, mode, size, &lazyBitmap, entry);
}
}
// update the icon source
model->SetIconFrom(source);
} else {
// we already know where the icon should come from,
// use shortcuts to get it
switch (source) {
case kNode:
resultingOpenCache = nodeCacheLocker;
resultingOpenCache->Lock();
entry = GetNodeIcon(&modelOpener, nodeCacheLocker,
&resultingOpenCache, model, source, mode,
size, &lazyBitmap, entry, permanent);
if (entry) {
entry = IconCacheEntry::ResolveIfAlias(&fSharedCache, entry);
if (!entry->HaveIconBitmap(mode, size)
&& entry->HaveIconBitmap(NORMAL_ICON_ONLY, size)) {
entry->ConstructBitmap(mode, size, &lazyBitmap);
entry->SetIcon(lazyBitmap.Adopt(), mode, size);
}
ASSERT(entry->HaveIconBitmap(mode, size));
}
break;
case kTrackerSupplied:
if (model->IsRoot()) {
entry = GetRootIcon(nodeCacheLocker, sharedCacheLocker,
&resultingOpenCache, model, source, mode, size,
&lazyBitmap);
break;
} else {
entry = GetWellKnownIcon(nodeCacheLocker, sharedCacheLocker,
&resultingOpenCache, model, source, mode, size,
&lazyBitmap);
if (entry)
break;
}
// fall through
case kTrackerDefault:
case kVolume:
if (model->IsVolume()) {
entry = GetNodeIcon(&modelOpener, nodeCacheLocker,
&resultingOpenCache, model, source,
mode, size, &lazyBitmap, entry, permanent);
if (!entry || !entry->HaveIconBitmap(mode, size))
entry = GetVolumeIcon(nodeCacheLocker, sharedCacheLocker,
&resultingOpenCache, model, source, mode, size,
&lazyBitmap);
break;
}
// fall through
case kMetaMime:
case kPreferredAppForType:
case kPreferredAppForNode:
resultingOpenCache = sharedCacheLocker;
resultingOpenCache->Lock();
entry = GetIconFromFileTypes(&modelOpener, source, mode, size,
&lazyBitmap, 0);
ASSERT(!entry || entry->HaveIconBitmap(mode, size));