forked from marbocub/BAfx
/
DirectoryView.cpp
753 lines (607 loc) · 14.4 KB
/
DirectoryView.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
/*
* Copyright 2012-2014 @marbocub <marbocub@gmail.com>
* All rights reserved.
* Distributed under the terms of the MIT license.
*/
#include "DirectoryView.h"
#include "BafxMessage.h"
#include <SupportDefs.h>
#include <Messenger.h>
#include <String.h>
#include <View.h>
#include <ListView.h>
#include <ListItem.h>
#include <ScrollView.h>
#include <StringView.h>
#include <Directory.h>
#include <Entry.h>
#include <Path.h>
#include <NodeMonitor.h>
#include <SymLink.h>
#include <cstdio> // for sprintf
#undef DEBUG
#ifdef DEBUG
# include <cstdio>
# define DEBUG_PRINT(x) {printf("%s:%s:", Name(),__FUNCTION__); printf x;}
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif
/****************************************************
* class EntryCountView
****************************************************/
class EntryCountView : public BView
{
private:
BStringView* mTotalCount_;
BStringView* mMarkedCount_;
int32 totalFiles_;
int32 totalDirs_;
off_t totalBytes_;
int32 markedFiles_;
int32 markedDirs_;
off_t markedBytes_;
public:
EntryCountView(
BRect frame, const char* name,
uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_WILL_DRAW)
: BView(frame, name, resizingMode, flags),
mTotalCount_(new BStringView(BRect(0,0,0,0),NULL,"",B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW)),
mMarkedCount_(new BStringView(BRect(0,0,0,0),NULL,"",B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW)),
totalFiles_(0),
totalDirs_(0),
totalBytes_(0),
markedFiles_(0),
markedDirs_(0),
markedBytes_(0)
{
AddChild(mTotalCount_);
AddChild(mMarkedCount_);
}
~EntryCountView()
{
RemoveChild(mTotalCount_);
delete mTotalCount_;
RemoveChild(mMarkedCount_);
delete mMarkedCount_;
}
void AttachedToWindow()
{
if (Parent()) {
SetViewColor(Parent()->ViewColor());
SetHighColor(Parent()->HighColor());
SetLowColor (Parent()->LowColor() );
}
BRect rect;
rect = Bounds();
rect.bottom = rect.top + B_H_SCROLL_BAR_HEIGHT - 1;
mTotalCount_->ResizeTo(rect.Width(), rect.Height());
mTotalCount_->MoveTo(rect.left, rect.top);
rect = Bounds();
rect.top = rect.top + B_H_SCROLL_BAR_HEIGHT;
mMarkedCount_->ResizeTo(rect.Width(), rect.Height());
mMarkedCount_->MoveTo(rect.left, rect.top);
}
void SetFont(const BFont* font, uint32 properties = B_FONT_ALL)
{
BView::SetFont(font);
mTotalCount_ ->SetFont(font);
mMarkedCount_->SetFont(font);
}
void Invalidate()
{
BView::Invalidate();
mTotalCount_ ->Invalidate();
mMarkedCount_->Invalidate();
}
void SetTotalCount(int32 files, int32 dirs, off_t bytes)
{
totalFiles_ = files;
totalDirs_ = dirs;
totalBytes_ = bytes;
Display();
}
void SetFiles(int32 files)
{
totalFiles_ = files;
Display();
}
void SetDirs(int32 dirs)
{
totalDirs_ = dirs;
Display();
}
void SetBytes(off_t bytes)
{
totalBytes_ = bytes;
Display();
}
void SetMarkedCount(int32 files, int32 dirs, off_t bytes)
{
markedFiles_ = files;
markedDirs_ = dirs;
markedBytes_ = bytes;
Display();
}
void SetMarkedFiles(int32 files)
{
markedFiles_ = files;
Display();
}
void SetMarkedDirs(int32 dirs)
{
markedDirs_ = dirs;
Display();
}
void SetMarkedBytes(off_t bytes)
{
markedBytes_ = bytes;
Display();
}
private:
void FormatCountString(BString* status, int32 files, int32 dirs, off_t size)
{
char buf[80];
status->SetTo(" ");
if (dirs > 0) {
sprintf(buf, "%3d Dirs ", dirs);
status->Append(buf);
} else {
status->Append(" ");
}
if (files > 0) {
sprintf(buf, "%4d Files ", files);
status->Append(buf);
} else {
status->Append(" ");
}
if (size > 0) {
if (size > 1024*1024*1024) {
sprintf(buf, "%6.2lf GBytes", size/1024.0/1024.0/1024.0);
} else if (size > 1024*1024) {
sprintf(buf, "%6.2lf MBytes", size/1024.0/1024.0);
} else if (size > 1024) {
sprintf(buf, "%6.2lf KBytes", size/1024.0);
} else {
sprintf(buf, "%6lld Bytes ", size);
}
status->Append(buf);
} else {
status->Append(" ");
}
}
void Display()
{
BString count;
FormatCountString(&count, totalFiles_, totalDirs_, totalBytes_);
mTotalCount_->SetText(count.String());
if (markedDirs_ > 0 || markedFiles_ > 0) {
FormatCountString(&count, markedFiles_, markedDirs_, markedBytes_);
count << " Marked";
} else {
count.SetTo("");
}
mMarkedCount_->SetText(count.String());
Invalidate();
}
};
/****************************************************
* class DirectoryView
****************************************************/
DirectoryView::DirectoryView(BRect frame, const char* name)
: BView(frame, name, B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_FRAME_EVENTS),
mDirNameView(new BStringView(BRect(0,0,0,0),NULL,"",
B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP,B_WILL_DRAW)),
mListView(new EntryListView(BRect(0,0,0,0),NULL,
B_SINGLE_SELECTION_LIST,B_FOLLOW_ALL_SIDES,
B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE)),
mScrollView(new BScrollView(NULL,mListView,
B_FOLLOW_ALL_SIDES,0,false,true,B_NO_BORDER)),
mCountView(new EntryCountView(BRect(0,0,0,0),NULL,
B_FOLLOW_LEFT_RIGHT|B_FOLLOW_BOTTOM,B_WILL_DRAW)),
entry_(NULL),
parent_(NULL),
entryList_()
{
AddChild(mDirNameView);
AddChild(mCountView);
AddChild(mScrollView);
mListView->SetFont(be_fixed_font);
mDirNameView->SetFont(be_fixed_font);
mCountView->SetFont(be_fixed_font);
}
DirectoryView::~DirectoryView()
{
mScrollView->RemoveChild(mListView);
delete mListView;
RemoveChild(mScrollView);
delete mScrollView;
RemoveChild(mCountView);
delete mCountView;
RemoveChild(mDirNameView);
delete mDirNameView;
if (entry_)
delete entry_;
if (parent_)
delete parent_;
entryList_.DeleteAllItems();
}
status_t
DirectoryView::OpenDirectory(const char *pathname)
{
BEntry entry(pathname);
Directory dir(entry);
status_t ret = OpenDirectory(&dir);
return ret;
}
status_t
DirectoryView::OpenDirectory(const Entry* dir)
{
if (! dir->IsDirectory()) {
return B_ERROR;
}
// stop node watcher
stop_watching(this);
// clear entry data and listview
mListView->DeleteAllItems();
entryList_.DeleteAllItems();
if (entry_) {
delete entry_;
entry_ = NULL;
}
if (parent_) {
delete parent_;
parent_ = NULL;
}
UpdateStatus();
// set current directory
entry_ = new Directory(*(Directory*)dir);
// get parent directory
BEntry bentry;
if (entry_->GetBEntry(&bentry) == B_OK) {
BEntry parent;
if (bentry.GetParent(&parent) == B_OK) {
parent_ = new Directory(parent);
}
}
// get directory entry
ListVisitor* lv1 = new ListVisitor(*entry_, &entryList_);
lv1->Go();
delete lv1;
// show directory entry
if (parent_ != NULL) {
mListView->AddItem(new EntryListItem(parent_, EntryListItem::ENTRY_IS_PARENT));
}
mListView->AddEntryList((BList*) &entryList_);
mListView->DoSort();
if (mListView->CountItems() > 0)
mListView->Select(0);
UpdateStatus();
// start node watcher
if (bentry.InitCheck() == B_OK) {
node_ref nref;
bentry.GetNodeRef(&nref);
watch_node(&nref, B_WATCH_ALL, this);
}
return B_OK;
}
void
DirectoryView::UpdateStatus()
{
BString path;
if (!entry_) {
mDirNameView->SetText("");
} else if (entry_->IsDirectory()) {
entry_->GetPath(&path);
mDirNameView->SetText(path.String());
} else {
mDirNameView->SetText("");
}
int32 totalFiles=0, totalDirs=0;
int32 markedFiles=0, markedDirs=0;
off_t totalBytes=0, markedBytes=0, size=0;
int32 count = mListView->CountItems();
for (int32 i=0; i<count; i++) {
EntryListItem* item = (EntryListItem*)mListView->ItemAt(i);
if (item->IsParent())
continue;
if (item->IsDirectory()) {
totalDirs++;
if (item->IsMarked())
markedDirs++;
} else {
off_t size = item->GetSize();
totalFiles++;
totalBytes += size;
if (item->IsMarked()) {
markedFiles++;
markedBytes += size;
}
}
}
((EntryCountView*)mCountView)->SetTotalCount (totalFiles, totalDirs, totalBytes);
((EntryCountView*)mCountView)->SetMarkedCount(markedFiles, markedDirs, markedBytes);
}
void
DirectoryView::AttachedToWindow()
{
BRect view_rect = Bounds();
BRect rect(view_rect);
if (Parent()) {
SetViewColor(Parent()->ViewColor());
SetHighColor(Parent()->HighColor());
SetLowColor (Parent()->LowColor() );
}
rect.bottom = rect.top + B_H_SCROLL_BAR_HEIGHT;
mDirNameView->ResizeTo(rect.Width(), rect.Height());
mDirNameView->MoveTo(rect.left, rect.top);
view_rect.top = rect.bottom + 1;
rect = view_rect;
rect.top = rect.bottom - B_H_SCROLL_BAR_HEIGHT*2;
mCountView->ResizeTo(rect.Width(), rect.Height());
mCountView->MoveTo(rect.left, rect.top);
view_rect.bottom = rect.top - 1;
rect = view_rect;
mScrollView->ResizeTo(rect.Width(), rect.Height());
mScrollView->MoveTo(rect.left, rect.top);
BView::AttachedToWindow();
}
void
DirectoryView::MakeFocus(bool focused)
{
mListView->MakeFocus(focused);
}
void
DirectoryView::MessageReceived(BMessage *message)
{
DEBUG_PRINT(("message:%c%c%c%c\n",
message->what>>24,
message->what>>16,
message->what>>8,
message->what));
DEBUG_PRINT(("CountNames: %d\n", message->CountNames(B_ANY_TYPE)));
switch(message->what) {
//
// cursor move
//
case M_CURSOR_UP:
((EntryListView*)mListView)->CursorUp();
break;
case M_CURSOR_DOWN:
((EntryListView*)mListView)->CursorDown();
break;
case M_CURSOR_PAGEUP:
((EntryListView*)mListView)->CursorPageUp();
break;
case M_CURSOR_PAGEDOWN:
((EntryListView*)mListView)->CursorPageDown();
break;
case M_CURSOR_HOME:
((EntryListView*)mListView)->CursorHome();
break;
case M_CURSOR_END:
((EntryListView*)mListView)->CursorEnd();
break;
//
// filer reqest
//
case M_ENTER_FILE:
if (mListView->CurrentSelection() < 0)
break;
if (((EntryListView*)mListView)->IsDirectory()) {
bool parent = mListView->IsParent();
Entry* entry = ((EntryListView*)mListView)->GetEntry();
if (entry) {
Entry* cd = entry->duplicate();
Entry* wd = entry_->duplicate();
OpenDirectory(cd);
if (parent)
mListView->SelectAt(*wd);
delete cd;
delete wd;
break;
}
}
{
BMessage msg(M_OPEN_FILE);
msg.AddPointer("sender", this);
BMessenger(this).SendMessage(&msg);
}
break;
case M_PARENT_DIRECTORY:
if (parent_) {
Entry* cd = parent_->duplicate();
Entry* wd = entry_->duplicate();
OpenDirectory(cd);
mListView->SelectAt(*wd);
delete cd;
delete wd;
}
break;
case M_MARK_FILE:
((EntryListView*)mListView)->ToggleMark(mListView->CurrentSelection());
UpdateStatus();
((EntryListView*)mListView)->CursorDown();
break;
case M_MARK_ALL_FILE:
{
int32 count = mListView->CountItems();
for (int32 i=0; i<count; i++) {
Entry* entry;
entry = ((EntryListItem*)mListView->ItemAt(i))->GetEntry();
if (! entry->IsDirectory()) {
mListView->ToggleMark(i);
}
}
mListView->Invalidate();
UpdateStatus();
}
break;
case M_FILER_CHANGED:
UpdateStatus();
break;
case B_NODE_MONITOR:
// NodeWatcherHandler(message);
break;
default:
BView::MessageReceived(message);
break;
}
}
/*
void
DirectoryView::NodeWatcherHandler(BMessage *msg)
{
int32 opcode;
if (msg->FindInt32("opcode", &opcode) != B_OK)
return;
if (entry_ == NULL)
return;
const char* name;
entry_ref ref;
node_ref nref;
node_ref nref_from;
node_ref nref_to;
node_ref nref_cd;
dir_.GetNodeRef(&nref_cd);
switch (opcode) {
case B_ENTRY_CREATED:
msg->FindInt32("device", &ref.device);
msg->FindInt64("directory", &ref.directory);
msg->FindString("name", &name);
ref.set_name(name);
AddItem(ref);
SendUpdateNotify();
break;
case B_ENTRY_REMOVED:
msg->FindInt32("device", &nref.device);
msg->FindInt64("node", &nref.node);
RemoveItem(nref);
SendUpdateNotify();
break;
case B_ENTRY_MOVED:
// node_ref
msg->FindInt32("device", &nref.device);
msg->FindInt64("node", &nref.node);
// entry_ref
ref.device = nref.device;
msg->FindString("name", &name);
ref.set_name(name);
// node_ref(s)
nref_from.device = nref.device;
nref_to.device = nref.device;
msg->FindInt64("from directory", &nref_from.node);
msg->FindInt64("to directory", &nref_to.node);
if (nref_from == nref_to) {
{
BEntry entry;
dir_.Rewind();
while(dir_.GetNextEntry(&entry) == B_OK) {
node_ref tmp;
entry.GetNodeRef(&tmp);
if (nref == tmp) {
entry.GetRef(&ref);
break;
}
}
}
UpdateItem(nref, ref);
SendUpdateNotify();
} else if (nref_from == nref_cd) {
RemoveItem(nref);
SendUpdateNotify();
} else if (nref_to == nref_cd) {
ref.directory = nref_to.node;
AddItem(ref);
SendUpdateNotify();
}
break;
case B_STAT_CHANGED:
case B_ATTR_CHANGED:
Invalidate();
break;
default:
break;
}
}
*/
void
DirectoryView::SetBackgroundColor(rgb_color color)
{
((EntryListView*)mListView)->SetBackgroundColor(color);
}
void
DirectoryView::SetExecutableColor(rgb_color color)
{
((EntryListView*)mListView)->SetExecutableColor(color);
}
void
DirectoryView::SetSelectedColor(rgb_color color)
{
((EntryListView*)mListView)->SetSelectedColor(color);
}
void
DirectoryView::SetReadonlyColor (rgb_color color)
{
((EntryListView*)mListView)->SetReadonlyColor(color);
}
void
DirectoryView::SetHiddenColor (rgb_color color)
{
((EntryListView*)mListView)->SetHiddenColor(color);
}
void
DirectoryView::SetCursorColor(rgb_color color)
{
((EntryListView*)mListView)->SetCursorColor(color);
}
void
DirectoryView::SetFileColor(rgb_color color)
{
((EntryListView*)mListView)->SetFileColor(color);
}
void
DirectoryView::SetDirColor(rgb_color color)
{
((EntryListView*)mListView)->SetDirColor(color);
}
rgb_color
DirectoryView::GetBackgroundColor(void) const
{
return ((EntryListView*)mListView)->GetBackgroundColor();
}
rgb_color
DirectoryView::GetExecutableColor(void) const
{
return ((EntryListView*)mListView)->GetExecutableColor();
}
rgb_color
DirectoryView::GetSelectedColor(void) const
{
return ((EntryListView*)mListView)->GetSelectedColor();
}
rgb_color
DirectoryView::GetReadonlyColor(void) const
{
return ((EntryListView*)mListView)->GetReadonlyColor();
}
rgb_color
DirectoryView::GetHiddenColor(void) const
{
return ((EntryListView*)mListView)->GetHiddenColor();
}
rgb_color
DirectoryView::GetCursorColor(void) const
{
return ((EntryListView*)mListView)->GetCursorColor();
}
rgb_color
DirectoryView::GetFileColor(void) const
{
return ((EntryListView*)mListView)->GetFileColor();
}
rgb_color
DirectoryView::GetDirColor(void) const
{
return ((EntryListView*)mListView)->GetDirColor();
}