-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
Model.cpp
1584 lines (1256 loc) · 33.1 KB
/
Model.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.
*/
// Dedicated to BModel
// ToDo:
// Consider moving iconFrom logic to BPose
// use a more efficient way of storing file type and preferred app strings
#include "Model.h"
#include <stdlib.h>
#include <strings.h>
#include <fs_info.h>
#include <fs_attr.h>
#include <AppDefs.h>
#include <Bitmap.h>
#include <Catalog.h>
#include <Debug.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <Locale.h>
#include <NodeInfo.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <SymLink.h>
#include <Query.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include "Attributes.h"
#include "Bitmaps.h"
#include "FindPanel.h"
#include "FSUtils.h"
#include "MimeTypes.h"
#include "Tracker.h"
#include "Utilities.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Model"
#ifdef CHECK_OPEN_MODEL_LEAKS
BObjectList<Model>* writableOpenModelList = NULL;
BObjectList<Model>* readOnlyOpenModelList = NULL;
#endif
namespace BPrivate {
extern
#ifdef _IMPEXP_BE
_IMPEXP_BE
#endif
bool CheckNodeIconHintPrivate(const BNode*, bool);
} // namespace BPrivate
static bool
HasVectorIconHint(BNode* node)
{
attr_info info;
return node->GetAttrInfo(kAttrIcon, &info) == B_OK;
}
// #pragma mark - Model()
Model::Model()
:
fPreferredAppName(NULL),
fBaseType(kUnknownNode),
fIconFrom(kUnknownSource),
fWritable(false),
fNode(NULL),
fStatus(B_NO_INIT),
fHasLocalizedName(false),
fLocalizedNameIsCached(false)
{
}
Model::Model(const Model& other)
:
fEntryRef(other.fEntryRef),
fMimeType(other.fMimeType),
fPreferredAppName(NULL),
fBaseType(other.fBaseType),
fIconFrom(other.fIconFrom),
fWritable(false),
fNode(NULL),
fLocalizedName(other.fLocalizedName),
fHasLocalizedName(other.fHasLocalizedName),
fLocalizedNameIsCached(other.fLocalizedNameIsCached)
{
fStatBuf.st_dev = other.NodeRef()->device;
fStatBuf.st_ino = other.NodeRef()->node;
if (other.IsSymLink() && other.LinkTo())
fLinkTo = new Model(*other.LinkTo());
fStatus = OpenNode(other.IsNodeOpenForWriting());
if (fStatus == B_OK) {
ASSERT(fNode);
fNode->GetStat(&fStatBuf);
ASSERT(fStatBuf.st_dev == other.NodeRef()->device);
ASSERT(fStatBuf.st_ino == other.NodeRef()->node);
}
if (!other.IsNodeOpen())
CloseNode();
}
Model::Model(const node_ref* dirNode, const node_ref* node, const char* name,
bool open, bool writable)
:
fPreferredAppName(NULL),
fWritable(false),
fNode(NULL),
fHasLocalizedName(false),
fLocalizedNameIsCached(false)
{
SetTo(dirNode, node, name, open, writable);
}
Model::Model(const BEntry* entry, bool open, bool writable)
:
fPreferredAppName(NULL),
fWritable(false),
fNode(NULL),
fHasLocalizedName(false),
fLocalizedNameIsCached(false)
{
SetTo(entry, open, writable);
}
Model::Model(const entry_ref* ref, bool traverse, bool open, bool writable)
:
fPreferredAppName(NULL),
fBaseType(kUnknownNode),
fIconFrom(kUnknownSource),
fWritable(false),
fNode(NULL),
fHasLocalizedName(false),
fLocalizedNameIsCached(false)
{
BEntry entry(ref, traverse);
fStatus = entry.InitCheck();
if (fStatus == B_OK)
SetTo(&entry, open, writable);
}
void
Model::DeletePreferredAppVolumeNameLinkTo()
{
if (IsSymLink()) {
Model* tmp = fLinkTo;
// deal with link to link to self
fLinkTo = NULL;
delete tmp;
} else if (IsVolume())
free(fVolumeName);
else
free(fPreferredAppName);
fPreferredAppName = NULL;
}
Model::~Model()
{
#ifdef CHECK_OPEN_MODEL_LEAKS
if (writableOpenModelList != NULL)
writableOpenModelList->RemoveItem(this);
if (readOnlyOpenModelList != NULL)
readOnlyOpenModelList->RemoveItem(this);
#endif
DeletePreferredAppVolumeNameLinkTo();
if (IconCache::NeedsDeletionNotification((IconSource)fIconFrom)) {
// this check allows us to use temporary Model in the IconCache
// without the danger of a deadlock
IconCache::sIconCache->Deleting(this);
}
#if xDEBUG
if (fNode != NULL)
PRINT(("destructor closing node for %s\n", Name()));
#endif
delete fNode;
}
status_t
Model::SetTo(const BEntry* entry, bool open, bool writable)
{
delete fNode;
fNode = NULL;
DeletePreferredAppVolumeNameLinkTo();
fIconFrom = kUnknownSource;
fBaseType = kUnknownNode;
fMimeType = "";
fStatus = entry->GetRef(&fEntryRef);
if (fStatus != B_OK)
return fStatus;
fStatus = entry->GetStat(&fStatBuf);
if (fStatus != B_OK)
return fStatus;
fStatus = OpenNode(writable);
if (!open)
CloseNode();
return fStatus;
}
status_t
Model::SetTo(const entry_ref* newRef, bool traverse, bool open, bool writable)
{
delete fNode;
fNode = NULL;
DeletePreferredAppVolumeNameLinkTo();
fIconFrom = kUnknownSource;
fBaseType = kUnknownNode;
fMimeType = "";
BEntry tmpEntry(newRef, traverse);
fStatus = tmpEntry.InitCheck();
if (fStatus != B_OK)
return fStatus;
if (traverse)
tmpEntry.GetRef(&fEntryRef);
else
fEntryRef = *newRef;
fStatus = tmpEntry.GetStat(&fStatBuf);
if (fStatus != B_OK)
return fStatus;
fStatus = OpenNode(writable);
if (!open)
CloseNode();
return fStatus;
}
status_t
Model::SetTo(const node_ref* dirNode, const node_ref* nodeRef,
const char* name, bool open, bool writable)
{
delete fNode;
fNode = NULL;
DeletePreferredAppVolumeNameLinkTo();
fIconFrom = kUnknownSource;
fBaseType = kUnknownNode;
fMimeType = "";
fStatBuf.st_dev = nodeRef->device;
fStatBuf.st_ino = nodeRef->node;
fEntryRef.device = dirNode->device;
fEntryRef.directory = dirNode->node;
fEntryRef.name = strdup(name);
BEntry tmpNode(&fEntryRef);
fStatus = tmpNode.InitCheck();
if (fStatus != B_OK)
return fStatus;
fStatus = tmpNode.GetStat(&fStatBuf);
if (fStatus != B_OK)
return fStatus;
fStatus = OpenNode(writable);
if (!open)
CloseNode();
return fStatus;
}
status_t
Model::InitCheck() const
{
return fStatus;
}
int
Model::CompareFolderNamesFirst(const Model* compareModel) const
{
if (compareModel == NULL)
return -1;
const Model* resolvedCompareModel = compareModel->ResolveIfLink();
const Model* resolvedMe = ResolveIfLink();
bool meIsDirOrVolume = resolvedMe->IsDirectory() || resolvedMe->IsVolume()
|| resolvedMe->IsVirtualDirectory();
bool otherIsDirOrVolume = resolvedCompareModel->IsDirectory()
|| resolvedCompareModel->IsVolume()
|| resolvedCompareModel->IsVirtualDirectory();
if (meIsDirOrVolume) {
if (!otherIsDirOrVolume)
return -1;
} else if (otherIsDirOrVolume)
return 1;
return NaturalCompare(Name(), compareModel->Name());
}
const char*
Model::Name() const
{
static const char* kRootNodeName = B_TRANSLATE_MARK(B_DISKS_DIR_NAME);
static const char* kTrashNodeName = B_TRANSLATE_MARK(B_TRASH_DIR_NAME);
static const char* kDesktopNodeName = B_TRANSLATE_MARK(B_DESKTOP_DIR_NAME);
switch (fBaseType) {
case kRootNode:
return B_TRANSLATE_NOCOLLECT(kRootNodeName);
case kVolumeNode:
if (fVolumeName != NULL)
return fVolumeName;
break;
case kTrashNode:
return B_TRANSLATE_NOCOLLECT(kTrashNodeName);
case kDesktopNode:
return B_TRANSLATE_NOCOLLECT(kDesktopNodeName);
default:
break;
}
if (fHasLocalizedName && gLocalizedNamePreferred)
return fLocalizedName.String();
else
return fEntryRef.name;
}
status_t
Model::OpenNode(bool writable)
{
if (IsNodeOpen() && (writable == IsNodeOpenForWriting()))
return B_OK;
OpenNodeCommon(writable);
return fStatus;
}
status_t
Model::UpdateStatAndOpenNode(bool writable)
{
if (IsNodeOpen() && (writable == IsNodeOpenForWriting()))
return B_OK;
// try reading the stat structure again
BEntry tmpEntry(&fEntryRef);
fStatus = tmpEntry.InitCheck();
if (fStatus != B_OK)
return fStatus;
fStatus = tmpEntry.GetStat(&fStatBuf);
if (fStatus != B_OK)
return fStatus;
OpenNodeCommon(writable);
return fStatus;
}
status_t
Model::OpenNodeCommon(bool writable)
{
#if xDEBUG
PRINT(("opening node for %s\n", Name()));
#endif
#ifdef CHECK_OPEN_MODEL_LEAKS
if (writableOpenModelList != NULL)
writableOpenModelList->RemoveItem(this);
if (readOnlyOpenModelList != NULL)
readOnlyOpenModelList->RemoveItem(this);
#endif
if (fBaseType == kUnknownNode)
SetupBaseType();
switch (fBaseType) {
case kPlainNode:
case kExecutableNode:
case kQueryNode:
case kQueryTemplateNode:
case kVirtualDirectoryNode:
// open or reopen
delete fNode;
fNode = new BFile(&fEntryRef,
(uint32)(writable ? O_RDWR : O_RDONLY));
break;
case kDirectoryNode:
case kVolumeNode:
case kRootNode:
case kTrashNode:
case kDesktopNode:
if (!IsNodeOpen())
fNode = new BDirectory(&fEntryRef);
if (fBaseType == kDirectoryNode
&& static_cast<BDirectory*>(fNode)->IsRootDirectory()) {
// promote from directory to volume
fBaseType = kVolumeNode;
}
break;
case kLinkNode:
if (!IsNodeOpen()) {
BEntry entry(&fEntryRef);
fNode = new BSymLink(&entry);
}
break;
default:
#if DEBUG
PrintToStream();
#endif
TRESPASS();
// this can only happen if GetStat failed before,
// in which case we shouldn't be here
// ToDo: Obviously, we can also be here if the type could not
// be determined, for example for block devices (so the TRESPASS()
// macro shouldn't be used here)!
return fStatus = B_ERROR;
}
fStatus = fNode->InitCheck();
if (fStatus != B_OK) {
delete fNode;
fNode = NULL;
// original code snoozed an error here and returned B_OK
return fStatus;
}
fWritable = writable;
if (fMimeType.Length() <= 0)
FinishSettingUpType();
#ifdef CHECK_OPEN_MODEL_LEAKS
if (fWritable) {
if (!writableOpenModelList) {
TRACE();
writableOpenModelList = new BObjectList<Model>(100);
}
writableOpenModelList->AddItem(this);
} else {
if (!readOnlyOpenModelList) {
TRACE();
readOnlyOpenModelList = new BObjectList<Model>(100);
}
readOnlyOpenModelList->AddItem(this);
}
#endif
if (gLocalizedNamePreferred)
CacheLocalizedName();
return fStatus;
}
void
Model::CloseNode()
{
#if xDEBUG
PRINT(("closing node for %s\n", Name()));
#endif
#ifdef CHECK_OPEN_MODEL_LEAKS
if (writableOpenModelList != NULL)
writableOpenModelList->RemoveItem(this);
if (readOnlyOpenModelList != NULL)
readOnlyOpenModelList->RemoveItem(this);
#endif
delete fNode;
fNode = NULL;
}
bool
Model::IsNodeOpen() const
{
return fNode != NULL;
}
bool
Model::IsNodeOpenForWriting() const
{
return fNode != NULL && fWritable;
}
void
Model::SetupBaseType()
{
switch (fStatBuf.st_mode & S_IFMT) {
case S_IFDIR:
// folder
fBaseType = kDirectoryNode;
break;
case S_IFREG:
// regular file
if ((fStatBuf.st_mode & S_IXUSR) != 0) {
// executable
fBaseType = kExecutableNode;
} else {
// non-executable
fBaseType = kPlainNode;
}
break;
case S_IFLNK:
// symlink
fBaseType = kLinkNode;
break;
default:
fBaseType = kUnknownNode;
break;
}
}
void
Model::CacheLocalizedName()
{
if (!fLocalizedNameIsCached) {
fLocalizedNameIsCached = true;
if (BLocaleRoster::Default()->GetLocalizedFileName(
fLocalizedName, fEntryRef, true) == B_OK)
fHasLocalizedName = true;
else
fHasLocalizedName = false;
}
}
void
Model::FinishSettingUpType()
{
char mimeString[B_MIME_TYPE_LENGTH];
BEntry entry;
// while we are reading the node, do a little
// snooping to see if it even makes sense to look for a node-based
// icon
// This serves as a hint to the icon cache, allowing it to not hit the
// disk again for models that do not have an icon defined by the node
if (IsNodeOpen()
&& fBaseType != kLinkNode
&& !CheckNodeIconHintPrivate(fNode,
dynamic_cast<TTracker*>(be_app) == NULL)
&& !HasVectorIconHint(fNode)) {
// when checking for the node icon hint, if we are libtracker,
// only check for small icons - checking for the large icons
// is a little more work for the filesystem and this will
// speed up the test. This makes node icons only work if there
// is a small and a large node icon on a file - for libtracker
// that is not a problem though
fIconFrom = kUnknownNotFromNode;
}
if (fBaseType != kDirectoryNode
&& fBaseType != kVolumeNode
&& fBaseType != kLinkNode
&& IsNodeOpen()) {
BNodeInfo info(fNode);
// check if a specific mime type is set
if (info.GetType(mimeString) == B_OK) {
// node has a specific mime type
fMimeType = mimeString;
if (strcmp(mimeString, B_QUERY_MIMETYPE) == 0)
fBaseType = kQueryNode;
else if (strcmp(mimeString, B_QUERY_TEMPLATE_MIMETYPE) == 0)
fBaseType = kQueryTemplateNode;
else if (strcmp(mimeString, kVirtualDirectoryMimeType) == 0)
fBaseType = kVirtualDirectoryNode;
if (info.GetPreferredApp(mimeString) == B_OK) {
if (fPreferredAppName)
DeletePreferredAppVolumeNameLinkTo();
if (mimeString[0])
fPreferredAppName = strdup(mimeString);
}
}
}
switch (fBaseType) {
case kDirectoryNode:
entry.SetTo(&fEntryRef);
if (entry.InitCheck() == B_OK) {
if (FSIsTrashDir(&entry))
fBaseType = kTrashNode;
else if (FSIsDeskDir(&entry))
fBaseType = kDesktopNode;
}
fMimeType = B_DIR_MIMETYPE;
// should use a shared string here
if (IsNodeOpen()) {
BNodeInfo info(fNode);
if (info.GetType(mimeString) == B_OK)
fMimeType = mimeString;
if (fIconFrom == kUnknownNotFromNode
&& WellKnowEntryList::Match(NodeRef())
> (directory_which)-1) {
// one of home, beos, system, boot, etc.
fIconFrom = kTrackerSupplied;
}
}
break;
case kVolumeNode:
{
if (NodeRef()->node == fEntryRef.directory
&& NodeRef()->device == fEntryRef.device) {
// promote from volume to file system root
fBaseType = kRootNode;
fMimeType = B_ROOT_MIMETYPE;
break;
}
// volumes have to have a B_VOLUME_MIMETYPE type
fMimeType = B_VOLUME_MIMETYPE;
if (fIconFrom == kUnknownNotFromNode) {
if (WellKnowEntryList::Match(NodeRef()) > (directory_which)-1)
fIconFrom = kTrackerSupplied;
else
fIconFrom = kVolume;
}
char name[B_FILE_NAME_LENGTH];
BVolume volume(NodeRef()->device);
if (volume.InitCheck() == B_OK && volume.GetName(name) == B_OK) {
if (fVolumeName != NULL)
DeletePreferredAppVolumeNameLinkTo();
fVolumeName = strdup(name);
}
#if DEBUG
else
PRINT(("get volume name failed for %s\n", fEntryRef.name));
#endif
break;
}
case kLinkNode:
fMimeType = B_LINK_MIMETYPE;
// should use a shared string here
break;
case kExecutableNode:
if (IsNodeOpen()) {
char signature[B_MIME_TYPE_LENGTH];
if (GetAppSignatureFromAttr(dynamic_cast<BFile*>(fNode),
signature) == B_OK) {
if (fPreferredAppName)
DeletePreferredAppVolumeNameLinkTo();
if (signature[0])
fPreferredAppName = strdup(signature);
}
}
if (fMimeType.Length() <= 0)
fMimeType = B_APP_MIME_TYPE;
// should use a shared string here
break;
default:
if (fMimeType.Length() <= 0)
fMimeType = B_FILE_MIMETYPE;
break;
}
}
void
Model::ResetIconFrom()
{
BModelOpener opener(this);
if (InitCheck() != B_OK)
return;
// mirror the logic from FinishSettingUpType
if ((fBaseType == kDirectoryNode || fBaseType == kVolumeNode
|| fBaseType == kTrashNode || fBaseType == kDesktopNode)
&& !CheckNodeIconHintPrivate(fNode,
dynamic_cast<TTracker*>(be_app) == NULL)) {
BDirectory* directory = dynamic_cast<BDirectory*>(fNode);
if (WellKnowEntryList::Match(NodeRef()) > (directory_which)-1) {
fIconFrom = kTrackerSupplied;
return;
} else if (directory != NULL && directory->IsRootDirectory()) {
fIconFrom = kVolume;
return;
}
}
fIconFrom = kUnknownSource;
}
const char*
Model::PreferredAppSignature() const
{
if (IsVolume() || IsSymLink())
return "";
return fPreferredAppName ? fPreferredAppName : "";
}
void
Model::SetPreferredAppSignature(const char* signature)
{
ASSERT(!IsVolume() && !IsSymLink());
ASSERT(signature != fPreferredAppName);
// self assignment should not be an option
free(fPreferredAppName);
if (signature)
fPreferredAppName = strdup(signature);
else
fPreferredAppName = NULL;
}
const Model*
Model::ResolveIfLink() const
{
if (!IsSymLink())
return this;
if (!fLinkTo)
return this;
return fLinkTo;
}
Model*
Model::ResolveIfLink()
{
if (!IsSymLink())
return this;
if (!fLinkTo)
return this;
return fLinkTo;
}
void
Model::SetLinkTo(Model* model)
{
ASSERT(IsSymLink());
ASSERT(!fLinkTo || (fLinkTo != model));
delete fLinkTo;
fLinkTo = model;
}
void
Model::GetPreferredAppForBrokenSymLink(BString &result)
{
if (!IsSymLink() || LinkTo()) {
result = "";
return;
}
BModelOpener opener(this);
BNodeInfo info(fNode);
status_t error
= info.GetPreferredApp(result.LockBuffer(B_MIME_TYPE_LENGTH));
result.UnlockBuffer();
if (error != B_OK) {
// Tracker will have to do
result = kTrackerSignature;
}
}
// #pragma mark - Node monitor updating methods
void
Model::UpdateEntryRef(const node_ref* dirNode, const char* name)
{
if (IsVolume()) {
if (fVolumeName != NULL)
DeletePreferredAppVolumeNameLinkTo();
fVolumeName = strdup(name);
}
fEntryRef.device = dirNode->device;
fEntryRef.directory = dirNode->node;
if (fEntryRef.name != NULL && strcmp(fEntryRef.name, name) == 0)
return;
fEntryRef.set_name(name);
}
status_t
Model::WatchVolumeAndMountPoint(uint32 , BHandler* target)
{
ASSERT(IsVolume());
if (fEntryRef.name != NULL && fVolumeName != NULL
&& strcmp(fEntryRef.name, "boot") == 0) {
// watch mount point for boot volume
BString bootMountPoint("/");
bootMountPoint += fVolumeName;
BEntry mountPointEntry(bootMountPoint.String());
Model mountPointModel(&mountPointEntry);
TTracker::WatchNode(mountPointModel.NodeRef(),
B_WATCH_NAME | B_WATCH_STAT | B_WATCH_ATTR, target);
}
return TTracker::WatchNode(NodeRef(),
B_WATCH_NAME | B_WATCH_STAT | B_WATCH_ATTR, target);
}
bool
Model::AttrChanged(const char* attrName)
{
// called on an attribute changed node monitor
// sync up cached values of mime type and preferred app and
// return true if icon needs updating
ASSERT(IsNodeOpen());
if (attrName != NULL
&& (strcmp(attrName, kAttrIcon) == 0
|| strcmp(attrName, kAttrMiniIcon) == 0
|| strcmp(attrName, kAttrLargeIcon) == 0)) {
return true;
}
if (attrName == NULL
|| strcmp(attrName, kAttrMIMEType) == 0
|| strcmp(attrName, kAttrPreferredApp) == 0) {
char mimeString[B_MIME_TYPE_LENGTH];
BNodeInfo info(fNode);
if (info.GetType(mimeString) != B_OK)
fMimeType = "";
else {
// node has a specific mime type
fMimeType = mimeString;
if (!IsVolume() && !IsSymLink()
&& info.GetPreferredApp(mimeString) == B_OK) {
SetPreferredAppSignature(mimeString);
}
}
#if xDEBUG
if (fIconFrom != kNode) {
PRINT(("%s, %s:updating icon because file type changed\n",
Name(), attrName != NULL ? attrName : ""));
} else {
PRINT(("Not updating icon even though type changed "
"because icon is from node.\n"));
}
#endif
return fIconFrom != kNode;
// update icon unless it is coming from a node
}
return attrName == NULL;
}
bool
Model::StatChanged()
{
ASSERT(IsNodeOpen());
mode_t oldMode = fStatBuf.st_mode;
fStatus = fNode->GetStat(&fStatBuf);
if (oldMode != fStatBuf.st_mode) {
bool forWriting = IsNodeOpenForWriting();
CloseNode();
//SetupBaseType();
// the node type can't change with a stat update...
OpenNodeCommon(forWriting);
return true;
}
return false;
}
// #pragma mark - Mime handling methods
bool
Model::IsDropTarget(const Model* forDocument, bool traverse) const
{
switch (CanHandleDrops()) {
case kCanHandle:
return true;
case kCannotHandle:
return false;
default:
break;
}
if (forDocument == NULL)
return true;
if (traverse) {
BEntry entry(forDocument->EntryRef(), true);
if (entry.InitCheck() != B_OK)
return false;