-
Notifications
You must be signed in to change notification settings - Fork 190
/
filelist.cpp
9213 lines (7578 loc) · 244 KB
/
filelist.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
/*
filelist.cpp
Файловая панель
*/
/*
Copyright © 1996 Eugene Roshal
Copyright © 2000 Far Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// BUGBUG
#include "platform.headers.hpp"
// Self:
#include "filelist.hpp"
// Internal:
#include "keyboard.hpp"
#include "flink.hpp"
#include "keys.hpp"
#include "macroopcode.hpp"
#include "ctrlobj.hpp"
#include "filefilter.hpp"
#include "dialog.hpp"
#include "cmdline.hpp"
#include "manager.hpp"
#include "filepanels.hpp"
#include "help.hpp"
#include "fileedit.hpp"
#include "namelist.hpp"
#include "fileview.hpp"
#include "copy.hpp"
#include "history.hpp"
#include "qview.hpp"
#include "preservelongname.hpp"
#include "scrbuf.hpp"
#include "filemasks.hpp"
#include "interf.hpp"
#include "message.hpp"
#include "clipboard.hpp"
#include "console.hpp"
#include "delete.hpp"
#include "stddlg.hpp"
#include "print.hpp"
#include "mkdir.hpp"
#include "setattr.hpp"
#include "filetype.hpp"
#include "execute.hpp"
#include "fnparce.hpp"
#include "datetime.hpp"
#include "dirinfo.hpp"
#include "pathmix.hpp"
#include "network.hpp"
#include "dirmix.hpp"
#include "strmix.hpp"
#include "exitcode.hpp"
#include "panelmix.hpp"
#include "processname.hpp"
#include "mix.hpp"
#include "notification.hpp"
#include "elevation.hpp"
#include "uuids.far.hpp"
#include "uuids.far.dialogs.hpp"
#include "plugins.hpp"
#include "lang.hpp"
#include "language.hpp"
#include "taskbar.hpp"
#include "fileowner.hpp"
#include "colormix.hpp"
#include "keybar.hpp"
#include "panelctype.hpp"
#include "diskmenu.hpp"
#include "string_utils.hpp"
#include "cvtname.hpp"
#include "vmenu.hpp"
#include "vmenu2.hpp"
#include "filefilterparams.hpp"
#include "desktop.hpp"
#include "string_sort.hpp"
#include "global.hpp"
#include "log.hpp"
// Platform:
#include "platform.hpp"
#include "platform.fs.hpp"
// Common:
#include "common/enum_tokens.hpp"
#include "common/scope_exit.hpp"
#include "common/string_utils.hpp"
#include "common/utility.hpp"
#include "common/view/zip.hpp"
// External:
#include "format.hpp"
//----------------------------------------------------------------------------
static_assert(static_cast<size_t>(panel_sort::BY_USER) == static_cast<size_t>(OPENPANELINFO_SORTMODES::SM_USER));
constexpr auto operator+(panel_sort const Value) noexcept
{
return std::to_underlying(Value);
}
static const struct
{
lng Label;
int MenuPosition;
far_key_code MenuKey;
std::initializer_list<std::pair<panel_sort, sort_order>> DefaultLayers;
}
SortModes[]
{
{ lng::MMenuUnsorted, 5, KEY_CTRLF7, { { panel_sort::UNSORTED, sort_order::ascend, }, }, },
{ lng::MMenuSortByName, 0, KEY_CTRLF3, { { panel_sort::BY_NAME, sort_order::ascend, }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByExt, 2, KEY_CTRLF4, { { panel_sort::BY_EXT, sort_order::ascend, }, { panel_sort::BY_NAMEONLY, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByWrite, 3, KEY_CTRLF5, { { panel_sort::BY_MTIME, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByCreation, 6, KEY_CTRLF8, { { panel_sort::BY_CTIME, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByAccess, 7, KEY_CTRLF9, { { panel_sort::BY_ATIME, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortBySize, 4, KEY_CTRLF6, { { panel_sort::BY_SIZE, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByDiz, 9, KEY_CTRLF10, { { panel_sort::BY_DIZ, sort_order::ascend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByOwner, 10, KEY_CTRLF11, { { panel_sort::BY_OWNER, sort_order::ascend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByAllocatedSize, 11, NO_KEY, { { panel_sort::BY_COMPRESSEDSIZE,sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByNumLinks, 12, NO_KEY, { { panel_sort::BY_NUMLINKS, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByNumStreams, 13, NO_KEY, { { panel_sort::BY_NUMSTREAMS, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByStreamsSize, 14, NO_KEY, { { panel_sort::BY_STREAMSSIZE, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByNameOnly, 1, NO_KEY, { { panel_sort::BY_NAMEONLY, sort_order::ascend, }, { panel_sort::BY_EXT, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
{ lng::MMenuSortByChange, 8, NO_KEY, { { panel_sort::BY_CHTIME, sort_order::descend, }, { panel_sort::BY_NAME, sort_order::ascend }, { panel_sort::UNSORTED, sort_order::ascend }, }, },
};
static_assert(std::size(SortModes) == static_cast<size_t>(panel_sort::COUNT));
static constexpr auto order_indicator(sort_order const Order)
{
switch (Order)
{
default:
case sort_order::keep: return L'=';
case sort_order::ascend: return L'▲';
case sort_order::descend: return L'▼';
}
}
span<std::pair<panel_sort, sort_order> const> default_sort_layers(panel_sort const SortMode)
{
return SortModes[static_cast<size_t>(SortMode)].DefaultLayers;
}
template<typename T>
auto compare_numbers(T const First, T const Second)
{
return First < Second? -1 : First != Second;
}
static auto compare_time(os::chrono::time_point First, os::chrono::time_point Second)
{
return compare_numbers(First, Second);
}
// FAT Last Write time is rounded up to the even number of seconds, e.g. 2s 1ms -> 4s
static auto to_fat_write_time(os::chrono::time_point Point)
{
return (Point.time_since_epoch() + 2s - 1ns) / 2s;
}
// However, people also use this function with FTP, which rounds down to whole seconds
static auto to_whole_seconds(os::chrono::time_point Point)
{
return Point.time_since_epoch() / 1s;
}
// The ultimate question here is "can these times be considered 'equal'",
// so we take an opportunistic approach and try both methods:
static auto compare_fat_write_time(os::chrono::time_point First, os::chrono::time_point Second)
{
if (!compare_numbers(to_fat_write_time(First), to_fat_write_time(Second)))
return 0;
if (!compare_numbers(to_whole_seconds(First), to_whole_seconds(Second)))
return 0;
return compare_time(First, Second);
}
enum SELECT_MODES
{
SELECT_INVERT,
SELECT_INVERTALL,
SELECT_INVERTFILES,
SELECT_ADD,
SELECT_REMOVE,
SELECT_ADDEXT,
SELECT_REMOVEEXT,
SELECT_ADDNAME,
SELECT_REMOVENAME,
SELECT_ADDMASK,
SELECT_REMOVEMASK,
SELECT_INVERTMASK,
};
namespace custom_sort
{
static FileList* FileListPtr;
static void FileListToSortingPanelItem(const FileListItem *arr, int index, SortingPanelItem* ppi)
{
const auto& fi = arr[index];
auto& pi = *ppi;
pi.FileName = fi.FileName.c_str(); //! CHANGED
pi.AlternateFileName = fi.AlternateFileName().c_str(); //! CHANGED
pi.FileSize=fi.FileSize;
pi.AllocationSize=fi.AllocationSize;
pi.FileAttributes=fi.Attributes;
pi.LastWriteTime = os::chrono::nt_clock::to_filetime(fi.LastWriteTime);
pi.CreationTime = os::chrono::nt_clock::to_filetime(fi.CreationTime);
pi.LastAccessTime = os::chrono::nt_clock::to_filetime(fi.LastAccessTime);
pi.ChangeTime = os::chrono::nt_clock::to_filetime(fi.ChangeTime);
pi.Flags=fi.UserFlags;
if (fi.Selected)
pi.Flags|=PPIF_SELECTED;
pi.CustomColumnData=fi.CustomColumns.data();
pi.CustomColumnNumber=fi.CustomColumns.size();
pi.Description=fi.DizText; //BUGBUG???
pi.UserData = fi.UserData;
pi.CRC32=fi.CRC32;
pi.Position=fi.Position; //! CHANGED
pi.SortGroup=fi.SortGroup - DEFAULT_SORT_GROUP; //! CHANGED
pi.NumberOfLinks = fi.IsNumberOfLinksRead() || FileListPtr->IsColumnDisplayed(column_type::links_number)?fi.NumberOfLinks(FileListPtr) : 0;
pi.Owner = fi.IsOwnerRead() || FileListPtr->IsColumnDisplayed(column_type::owner)? EmptyToNull(fi.Owner(FileListPtr)) : nullptr;
pi.NumberOfStreams = fi.IsNumberOfStreamsRead() || FileListPtr->IsColumnDisplayed(column_type::streams_number)? fi.NumberOfStreams(FileListPtr) : 0;
pi.StreamsSize = fi.IsStreamsSizeRead() || FileListPtr->IsColumnDisplayed(column_type::streams_size)? fi.StreamsSize(FileListPtr) : 0;
}
struct CustomSort
{
unsigned int *Positions;
const FileListItem *Items;
size_t ItemsCount;
void(*FileListToSortingPanelItem)(const FileListItem*, int, SortingPanelItem*);
int ListSortGroups;
int ListSelectedFirst;
int ListDirectoriesFirst;
int ListSortMode;
int RevertSorting;
int Reserved[2];
HANDLE hSortPlugin;
};
static bool SortFileList(CustomSort* cs, wchar_t* indicator)
{
FarMacroValue values[]{ cs };
FarMacroCall fmc{ sizeof(fmc), std::size(values), values };
OpenMacroPluginInfo info{ MCT_PANELSORT, &fmc };
void *ptr;
if (!Global->CtrlObject->Plugins->CallPlugin(Global->Opt->KnownIDs.Luamacro.Id, OPEN_LUAMACRO, &info, &ptr) || !ptr)
return false;
indicator[0] = info.Ret.Values[0].String[0];
indicator[1] = info.Ret.Values[0].String[1];
return true;
}
static bool CanSort(int SortMode)
{
FarMacroValue values[]{ static_cast<double>(SortMode) };
FarMacroCall fmc{ sizeof(fmc), std::size(values), values };
OpenMacroPluginInfo info{ MCT_CANPANELSORT, &fmc };
void *ptr;
return Global->CtrlObject->Plugins->CallPlugin(Global->Opt->KnownIDs.Luamacro.Id, OPEN_LUAMACRO, &info, &ptr) && ptr;
}
}
struct FileList::PluginsListItem
{
NONCOPYABLE(PluginsListItem);
MOVE_CONSTRUCTIBLE(PluginsListItem);
PluginsListItem(std::unique_ptr<plugin_panel>&& hPlugin, string_view const HostFile, bool Modified, int PrevViewMode, panel_sort PrevSortMode, bool PrevSortOrder, bool PrevDirectoriesFirst, const PanelViewSettings& PrevViewSettings):
m_Plugin(std::move(hPlugin)),
m_HostFile(HostFile),
m_Modified(Modified),
m_PrevViewMode(PrevViewMode),
m_PrevSortMode(PrevSortMode),
m_PrevSortOrder(PrevSortOrder),
m_PrevDirectoriesFirst(PrevDirectoriesFirst),
m_PrevViewSettings(PrevViewSettings.clone())
{
}
std::unique_ptr<plugin_panel> m_Plugin;
string m_HostFile;
bool m_Modified;
int m_PrevViewMode;
panel_sort m_PrevSortMode;
bool m_PrevSortOrder;
bool m_PrevDirectoriesFirst;
PanelViewSettings m_PrevViewSettings;
};
FileListItem::FileListItem()
{
m_Owner = values::uninitialised(wchar_t());
}
static string GetItemFullName(const FileListItem& Item, const FileList* Owner)
{
return path::join(Owner->GetCurDir(), IsParentDirectory(Item)? string{} : Item.FileName);
}
bool FileListItem::IsNumberOfLinksRead() const
{
return m_NumberOfLinks != values::uninitialised(m_NumberOfLinks);
}
DWORD FileListItem::NumberOfLinks(const FileList* Owner) const
{
if (IsNumberOfLinksRead())
return m_NumberOfLinks;
if (Attributes & FILE_ATTRIBUTE_DIRECTORY || !Owner->HardlinksSupported())
{
m_NumberOfLinks = 1;
}
else
{
SCOPED_ACTION(elevation::suppress);
const auto Hardlinks = GetNumberOfLinks(GetItemFullName(*this, Owner));
static_assert(std::same_as<decltype(m_NumberOfLinks), DWORD>);
m_NumberOfLinks = Hardlinks? static_cast<DWORD>(*Hardlinks) : values::unknown(m_NumberOfLinks);
}
return m_NumberOfLinks;
}
static void GetStreamsCountAndSize(const FileList* Owner, const FileListItem& Item, unsigned long long& StreamsSize, DWORD& NumberOfStreams, bool Supported)
{
if (!Supported)
{
StreamsSize = Item.FileSize;
NumberOfStreams = 1;
}
else
{
SCOPED_ACTION(elevation::suppress);
size_t StreamsCount = 0;
if (EnumStreams(GetItemFullName(Item, Owner), StreamsSize, StreamsCount))
{
static_assert(std::same_as<decltype(NumberOfStreams), DWORD&>);
NumberOfStreams = static_cast<DWORD>(StreamsCount);
}
else
{
StreamsSize = FileListItem::values::unknown(StreamsSize);
NumberOfStreams = FileListItem::values::unknown(NumberOfStreams);
}
}
}
bool FileListItem::IsNumberOfStreamsRead() const
{
return m_NumberOfStreams != values::uninitialised(m_NumberOfStreams);
}
DWORD FileListItem::NumberOfStreams(const FileList* Owner) const
{
if (IsNumberOfStreamsRead())
return m_NumberOfStreams;
GetStreamsCountAndSize(Owner, *this, m_StreamsSize, m_NumberOfStreams, Owner->StreamsSupported());
return m_NumberOfStreams;
}
bool FileListItem::IsStreamsSizeRead() const
{
return m_StreamsSize != values::uninitialised(m_StreamsSize);
}
unsigned long long FileListItem::StreamsSize(const FileList* Owner) const
{
if (IsStreamsSizeRead())
return m_StreamsSize;
GetStreamsCountAndSize(Owner, *this, m_StreamsSize, m_NumberOfStreams, Owner->StreamsSupported());
return m_StreamsSize;
}
bool FileListItem::IsOwnerRead() const
{
return !(m_Owner.size() == 1 && m_Owner.front() == values::uninitialised(wchar_t()));
}
const string& FileListItem::Owner(const FileList* Owner) const
{
if (IsOwnerRead())
return m_Owner;
if (Owner->GetMode() == panel_mode::NORMAL_PANEL)
{
SCOPED_ACTION(elevation::suppress);
if (!GetFileOwner(Owner->GetComputerName(), GetItemFullName(*this, Owner), m_Owner))
{
// One try is enough
m_Owner.clear();
}
}
else
{
m_Owner.clear();
}
return m_Owner;
}
bool FileListItem::IsContentDataRead() const
{
return m_ContentData != nullptr; // bad
}
const std::unique_ptr<content_data>& FileListItem::ContentData(const FileList* Owner) const
{
if (IsContentDataRead())
return m_ContentData;
m_ContentData = Owner->GetContentData(GetItemFullName(*this, Owner));
return m_ContentData;
}
const string& FileListItem::AlternateOrNormal(bool Alternate) const
{
return Alternate? AlternateFileName() : FileName;
}
struct FileList::PrevDataItem
{
NONCOPYABLE(PrevDataItem);
MOVE_CONSTRUCTIBLE(PrevDataItem);
PrevDataItem(string rhsPrevName, list_data&& rhsPrevListData, int rhsPrevTopFile):
strPrevName(std::move(rhsPrevName)),
PrevListData(std::move(rhsPrevListData)),
PrevTopFile(rhsPrevTopFile)
{
}
string strPrevName;
list_data PrevListData;
int PrevTopFile;
};
class FileList::background_updater
{
public:
explicit background_updater(FileList* const Owner):
m_Owner(Owner)
{
}
const auto& event_id() const
{
return m_Listener.GetEventName();
}
private:
listener m_Listener{ listener::scope{L"FileList"sv}, [this]
{
m_Owner->background_update();
}};
FileList* m_Owner;
};
file_panel_ptr FileList::create(window_ptr Owner)
{
return std::make_shared<FileList>(private_tag(), Owner);
}
FileList::FileList(private_tag, window_ptr Owner):
Panel(std::move(Owner)),
m_BackgroundUpdater(std::make_unique<background_updater>(this))
{
if (const auto& data = msg(lng::MPanelBracketsForLongName); data.size() > 1)
{
*openBracket = data[0];
*closeBracket = data[1];
}
m_CurDir = os::fs::get_current_directory();
strOriginalCurDir = m_CurDir;
m_SortMode = panel_sort::BY_NAME;
m_ViewSettings = Global->Opt->ViewSettings[m_ViewMode].clone();
PreparePanelView();
}
FileList::~FileList()
{
if (m_PanelMode == panel_mode::PLUGIN_PANEL)
while (PopPlugin(FALSE))
;
FileList::StopFSWatcher();
FileList::ClearAllItem();
const auto DataLock = lock_data();
auto& m_ListData = *DataLock;
m_ListData.clear();
}
FileList::list_data& FileList::list_data::operator=(FileList::list_data&& rhs) noexcept
{
clear();
Items = std::move(rhs.Items);
rhs.Items.clear();
m_Plugin = std::move(rhs.m_Plugin);
rhs.m_Plugin = {};
return *this;
}
void FileList::list_data::clear()
{
for (auto& i: Items)
{
if (m_Plugin)
{
FreePluginPanelItemUserData(m_Plugin, i.UserData);
if (i.DeleteDiz)
delete[] i.DizText;
}
for (const auto& Column: i.CustomColumns)
{
delete[] Column;
}
i.CustomColumns.clear();
}
Items.clear();
m_Plugin = nullptr;
}
void FileList::ToBegin()
{
m_CurFile = 0;
ShowFileList();
}
void FileList::ToEnd()
{
m_CurFile = is_data_empty()? 0 : static_cast<int>(data_size() - 1);
ShowFileList();
}
void FileList::MoveCursor(int offset)
{
m_CurFile = is_data_empty()? 0 : std::clamp(m_CurFile + offset, 0, static_cast<int>(data_size() - 1));
}
void FileList::MoveCursorAndShow(int offset)
{
MoveCursor(offset);
ShowFileList();
}
void FileList::Scroll(int offset)
{
m_CurTopFile += offset;
MoveCursorAndShow(offset);
}
void FileList::CorrectPosition()
{
if (is_data_empty())
{
m_CurFile=m_CurTopFile=0;
return;
}
const auto DataSize = static_cast<int>(data_size());
if (m_CurTopFile + m_Stripes * m_Height > DataSize)
m_CurTopFile = DataSize - m_Stripes * m_Height;
m_CurFile = std::clamp(m_CurFile, 0, DataSize - 1);
m_CurTopFile = std::clamp(m_CurTopFile, 0, DataSize - 1);
if (m_CurFile<m_CurTopFile)
m_CurTopFile=m_CurFile;
if (m_CurFile>m_CurTopFile+m_Stripes*m_Height-1)
m_CurTopFile=m_CurFile-m_Stripes*m_Height+1;
}
class list_less
{
public:
explicit list_less(const FileList* Owner, const plugin_panel* SortPlugin, bool const IgnorePaths):
m_Owner(Owner),
m_ListSortMode(Owner->GetSortMode()),
m_ListPanelMode(Owner->GetMode()),
m_SortPlugin(SortPlugin),
m_SortLayers(Global->Opt->PanelSortLayers[static_cast<size_t>(m_ListSortMode)]),
m_Reverse(Owner->GetSortOrder()),
m_ListSortGroups(Owner->GetSortGroups()),
m_ListSelectedFirst(Owner->GetSelectedFirstMode()),
m_ListDirectoriesFirst(Owner->GetDirectoriesFirst()),
m_SortFolderExt(Global->Opt->SortFolderExt),
m_IgnorePaths(IgnorePaths)
{
}
bool operator()(const FileListItem& Item1, const FileListItem& Item2) const
{
const auto IsParentDirItem1 = IsParentDirectory(Item1);
const auto IsParentDirItem2 = IsParentDirectory(Item2);
if (IsParentDirItem1 && IsParentDirItem2)
return Item1.Position < Item2.Position;
if (IsParentDirItem1)
return true;
if (IsParentDirItem2)
return false;
if (m_ListDirectoriesFirst)
{
const auto IsDirItem1 = (Item1.Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
const auto IsDirItem2 = (Item2.Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (IsDirItem1 != IsDirItem2)
return IsDirItem1;
}
if (m_ListSelectedFirst && Item1.Selected != Item2.Selected)
return Item1.Selected;
if (m_ListSortGroups && Item1.SortGroup != Item2.SortGroup &&
(
m_ListSortMode == panel_sort::BY_NAME ||
m_ListSortMode == panel_sort::BY_EXT ||
m_ListSortMode == panel_sort::BY_NAMEONLY
)
)
return Item1.SortGroup < Item2.SortGroup;
if (m_SortPlugin && m_ListSortMode != panel_sort::UNSORTED)
{
const auto& [a, b] = m_Reverse? std::tie(Item2, Item1) : std::tie(Item1, Item2);
// Direct access, no copy. It's its own panel anyways.
PluginPanelItemHolderRef pi1, pi2;
m_Owner->FileListToPluginItem(a, pi1);
m_Owner->FileListToPluginItem(b, pi2);
if (const auto Result = Global->CtrlObject->Plugins->Compare(m_SortPlugin, &pi1.Item, &pi2.Item, internal_sort_mode_to_plugin(m_ListSortMode)))
{
if (Result != -2)
return Result < 0;
}
}
for (const auto& [ModeValue, Order]: m_SortLayers)
{
const auto LayerSort = static_cast<panel_sort>(ModeValue);
const auto Reverse = LayerSort == m_ListSortMode || Order == sort_order::keep? m_Reverse : Order == sort_order::descend;
if (const auto Result = compare(LayerSort, Reverse, Item1, Item2))
return Result < 0;
}
return false;
}
private:
int compare(panel_sort const SortMode, bool const Reverse, FileListItem const& Item1, FileListItem const& Item2) const
{
const auto& [a, b] = Reverse? std::tie(Item2, Item1) : std::tie(Item1, Item2);
const auto ignore_path_opt = [&](string_view const FullName)
{
return m_IgnorePaths? PointToName(FullName) : FullName;
};
const auto name_ext_opt = [SortFolderExt = m_SortFolderExt](FileListItem const& i)
{
return SortFolderExt || !(i.Attributes & FILE_ATTRIBUTE_DIRECTORY)?
name_ext(i.FileName) :
std::pair(string_view(i.FileName), L""sv);
};
switch (SortMode)
{
case panel_sort::UNSORTED:
return compare_numbers(a.Position, b.Position);
case panel_sort::BY_NAME:
return string_sort::compare(ignore_path_opt(a.FileName), ignore_path_opt(b.FileName));
case panel_sort::BY_NAMEONLY:
return string_sort::compare(ignore_path_opt(name_ext_opt(a).first), ignore_path_opt(name_ext_opt(b).first));
case panel_sort::BY_EXT:
return string_sort::compare(name_ext_opt(a).second, name_ext_opt(b).second);
case panel_sort::BY_MTIME:
return compare_time(a.LastWriteTime, b.LastWriteTime);
case panel_sort::BY_CTIME:
return compare_time(a.CreationTime, b.CreationTime);
case panel_sort::BY_ATIME:
return compare_time(a.LastAccessTime, b.LastAccessTime);
case panel_sort::BY_CHTIME:
return compare_time(a.ChangeTime, b.ChangeTime);
case panel_sort::BY_SIZE:
return compare_numbers(a.FileSize, b.FileSize);
case panel_sort::BY_DIZ:
return string_sort::compare(NullToEmpty(a.DizText), NullToEmpty(b.DizText));
case panel_sort::BY_OWNER:
return string_sort::compare(a.Owner(m_Owner), b.Owner(m_Owner));
case panel_sort::BY_COMPRESSEDSIZE:
return compare_numbers(a.AllocationSize, b.AllocationSize);
case panel_sort::BY_NUMLINKS:
return compare_numbers(a.NumberOfLinks(m_Owner), b.NumberOfLinks(m_Owner));
case panel_sort::BY_NUMSTREAMS:
return compare_numbers(a.NumberOfStreams(m_Owner), b.NumberOfStreams(m_Owner));
case panel_sort::BY_STREAMSSIZE:
return compare_numbers(a.StreamsSize(m_Owner), b.StreamsSize(m_Owner));
default:
UNREACHABLE;
}
}
const FileList* const m_Owner;
const panel_sort m_ListSortMode;
const panel_mode m_ListPanelMode;
const plugin_panel* m_SortPlugin;
const span<std::pair<panel_sort, sort_order>> m_SortLayers;
const bool m_Reverse;
bool m_ListSortGroups;
bool m_ListSelectedFirst;
bool m_ListDirectoriesFirst;
bool m_SortFolderExt;
bool m_IgnorePaths;
};
void FileList::SortFileList(bool KeepPosition)
{
if (is_data_empty() || m_InsideGetFindData)
return;
const auto DataLock = lock_data();
auto& m_ListData = *DataLock;
string strCurName;
if (m_SortMode == panel_sort::BY_DIZ)
ReadDiz();
if (KeepPosition)
{
assert(m_CurFile < static_cast<int>(m_ListData.size()));
strCurName = m_ListData[m_CurFile].FileName;
}
const auto PluginPanel = GetPluginHandle();
const auto hSortPlugin = (m_PanelMode == panel_mode::PLUGIN_PANEL && PluginPanel && PluginPanel->plugin()->has(iCompare))? PluginPanel : nullptr;
// ЭТО ЕСТЬ УЗКОЕ МЕСТО ДЛЯ СКОРОСТНЫХ ХАРАКТЕРИСТИК Far Manager
// при считывании директории
if (m_SortMode < panel_sort::COUNT)
{
const auto NameColumn = std::find_if(ALL_CONST_RANGE(m_ViewSettings.PanelColumns), [](column const& i){ return i.type == column_type::name; });
const auto IgnorePaths = NameColumn != m_ViewSettings.PanelColumns.cend() && NameColumn->type_flags & COLFLAGS_NAMEONLY;
list_less const Predicate(this, hSortPlugin, IgnorePaths);
const auto& SortLayers = Global->Opt->PanelSortLayers[static_cast<size_t>(m_SortMode)];
if (std::any_of(ALL_CONST_RANGE(SortLayers), [](std::pair<panel_sort, sort_order> const& Layer){ return Layer.first == panel_sort::UNSORTED; }))
{
// Unsorted criterion is deterministic and won't report equality, thus ensuring stability
std::sort(ALL_RANGE(m_ListData), Predicate);
}
else
{
std::stable_sort(ALL_RANGE(m_ListData), Predicate);
}
}
else if (m_SortMode >= panel_sort::BY_USER)
{
custom_sort::CustomSort cs{};
custom_sort::FileListPtr = this;
std::vector<unsigned int> Positions(m_ListData.size());
std::iota(ALL_RANGE(Positions), 0);
cs.Positions = Positions.data();
cs.Items = m_ListData.data();
cs.ItemsCount = m_ListData.size();
cs.FileListToSortingPanelItem = custom_sort::FileListToSortingPanelItem;
cs.ListSortGroups = m_SortGroups;
cs.ListSelectedFirst = SelectedFirst;
cs.ListDirectoriesFirst = m_DirectoriesFirst;
cs.ListSortMode = static_cast<int>(m_SortMode);
cs.RevertSorting = m_ReverseSortOrder;
cs.hSortPlugin = hSortPlugin;
if (custom_sort::SortFileList(&cs, CustomSortIndicator))
{
apply_permutation(ALL_RANGE(m_ListData), Positions.begin());
}
else
{
SetSortMode(panel_sort::BY_NAME); // recursive call
return;
}
}
else
{
LOGWARNING(L"Unknown sort mode {}"sv, m_SortMode);
}
if (KeepPosition)
GoToFile(strCurName);
}
bool FileList::SendKeyToPlugin(DWORD Key, bool Pred)
{
if (m_PanelMode != panel_mode::PLUGIN_PANEL)
return false;
const auto MacroState = Global->CtrlObject->Macro.GetState();
if (MacroState != MACROSTATE_RECORDING_COMMON && MacroState != MACROSTATE_EXECUTING_COMMON && MacroState != MACROSTATE_NOMACRO)
return false;
INPUT_RECORD rec;
KeyToInputRecord(Key, &rec);
const auto ProcessCode = Global->CtrlObject->Plugins->ProcessKey(GetPluginHandle(), &rec, Pred);
return ProcessCode != 0;
}
bool FileList::GetPluginInfo(PluginInfo *PInfo) const
{
const auto PluginPanel = GetPluginHandle();
if (GetMode() != panel_mode::PLUGIN_PANEL || !PluginPanel || !PluginPanel->plugin())
return false;
PInfo->StructSize = sizeof(PluginInfo);
return Global->CtrlObject->Plugins->GetPluginInfo(PluginPanel->plugin(), PInfo);
}
long long FileList::VMProcess(int OpCode,void *vParam,long long iParam)
{
switch (OpCode)
{
case MCODE_C_ROOTFOLDER:
if (m_PanelMode == panel_mode::PLUGIN_PANEL)
{
Global->CtrlObject->Plugins->GetOpenPanelInfo(GetPluginHandle(), &m_CachedOpenPanelInfo);
return !m_CachedOpenPanelInfo.CurDir || !*m_CachedOpenPanelInfo.CurDir;
}
return IsRootPath(m_CurDir)? 1 : equal_icase(m_CurDir, GetPathRoot(m_CurDir));
case MCODE_C_EOF:
return m_CurFile == static_cast<int>(data_size() - 1);
case MCODE_C_BOF:
return !m_CurFile;
case MCODE_C_SELECTED:
return GetRealSelCount() != 0;
case MCODE_V_ITEMCOUNT:
return data_size();
case MCODE_V_CURPOS:
return m_CurFile + 1;
case MCODE_C_APANEL_FILTER:
return m_Filter && m_Filter->IsEnabledOnPanel();
case MCODE_V_APANEL_PREFIX: // APanel.Prefix
case MCODE_V_PPANEL_PREFIX: // PPanel.Prefix
{
const auto PInfo = static_cast<PluginInfo*>(vParam);
const auto PluginPanel = GetPluginHandle();
if (GetMode() == panel_mode::PLUGIN_PANEL && PluginPanel && PluginPanel->plugin())
return Global->CtrlObject->Plugins->GetPluginInfo(PluginPanel->plugin(), PInfo)?1:0;
return 0;
}
case MCODE_V_APANEL_FORMAT: // APanel.Format
case MCODE_V_PPANEL_FORMAT: // PPanel.Format
{
const auto PluginPanel = GetPluginHandle();
if (GetMode() == panel_mode::PLUGIN_PANEL && PluginPanel)
{
Global->CtrlObject->Plugins->GetOpenPanelInfo(PluginPanel, &m_CachedOpenPanelInfo);
*static_cast<OpenPanelInfo*>(vParam) = m_CachedOpenPanelInfo;
return 1;
}
return 0;
}
case MCODE_F_PANEL_SELECT:
{
long long Result=-1;
const auto mps = static_cast<const MacroPanelSelect*>(vParam);
if (is_data_empty())
return Result;
if (mps->Mode == 1 && static_cast<size_t>(mps->Index) >= data_size())
return Result;
const auto IsRegularPanel = m_PanelMode == panel_mode::NORMAL_PANEL;
const auto ApplyToList = [&](const auto& Selector)
{
for (const auto& i: enum_tokens_with_quotes(mps->Item, L"\r\n"sv))
{
if (i.empty())
continue;
const auto NameToFind = IsRegularPanel? PointToName(i) : i;
const auto PartialCompare = IsRegularPanel || !contains(i, path::separator);
const auto Pos = FindFile(NameToFind, PartialCompare);
if (Pos == -1)
continue;
Selector(Pos);
Result++;
}
};
enum class ps_action
{
remove,