UnixDirView.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "UnixDirView.h"
  6. #include "UnixDriveView.h"
  7. #include <FileCtrl.hpp>
  8. #ifndef DESIGN_ONLY
  9. #include <CoreMain.h>
  10. #include <Terminal.h>
  11. #include <WinConfiguration.h>
  12. #include <VCLCommon.h>
  13. #endif
  14. #pragma package(smart_init)
  15. #ifndef DESIGN_ONLY
  16. #define ITEMFILE ((TRemoteFile *)(Item->Data))
  17. // noop, previously this tested that the file was in terminal's file listing,
  18. // but that cannot be safely checked now the terminal is used in multithreaded
  19. // environment
  20. #define ASSERT_VALID_ITEM
  21. #endif
  22. //---------------------------------------------------------------------------
  23. static inline void ValidCtrCheck(TUnixDirView *)
  24. {
  25. new TUnixDirView(NULL);
  26. }
  27. //---------------------------------------------------------------------------
  28. namespace Unixdirview
  29. {
  30. void __fastcall PACKAGE Register()
  31. {
  32. TComponentClass classes[1] = {__classid(TUnixDirView)};
  33. RegisterComponents(L"Scp", classes, 0);
  34. }
  35. }
  36. //---------------------------------------------------------------------------
  37. #ifndef DESIGN_ONLY
  38. #define RFILE(N) ((TRemoteFile *)(Item ## N->Data))
  39. int __stdcall CompareDirectories(TListItem *Item1, TListItem *Item2)
  40. {
  41. // Because CompareDirectories is called from each other compare functions
  42. // it's sufficient to check pointers only here (see below)
  43. assert(Item1 && RFILE(1) && Item2 && RFILE(2));
  44. if (RFILE(1)->IsParentDirectory && !RFILE(2)->IsParentDirectory) return -1;
  45. else
  46. if (!RFILE(1)->IsParentDirectory && RFILE(2)->IsParentDirectory) return 1;
  47. else
  48. if (RFILE(1)->IsDirectory && !RFILE(2)->IsDirectory) return -1;
  49. else
  50. if (!RFILE(1)->IsDirectory && RFILE(2)->IsDirectory) return 1;
  51. else
  52. return 0;
  53. }
  54. //---------------------------------------------------------------------------
  55. int __fastcall CompareExtThenFull(const UnicodeString & Name1, const UnicodeString & Name2)
  56. {
  57. UnicodeString Ext1 = UnixExtractFileExt(Name1);
  58. UnicodeString Ext2 = UnixExtractFileExt(Name2);
  59. int Result = CompareLogicalText(Ext1, Ext2);
  60. if (Result == 0)
  61. {
  62. Result = CompareLogicalText(Name1, Name2);
  63. }
  64. return Result;
  65. }
  66. //---------------------------------------------------------------------------
  67. #define DEFINE_COMPARE_FUNC_EX(PROPERTY, NAME, COMPAREFUNCFILE, COMPAREFUNCDIR, FALLBACK) \
  68. int __stdcall Compare ## NAME(TListItem *Item1, TListItem *Item2, TUnixDirView *DirView) \
  69. { \
  70. int Result = CompareDirectories(Item1, Item2); \
  71. if (!Result) \
  72. { \
  73. if (RFILE(1)->IsDirectory) \
  74. { \
  75. Result = COMPAREFUNCDIR(RFILE(1)->PROPERTY, RFILE(2)->PROPERTY); \
  76. } \
  77. else \
  78. { \
  79. Result = COMPAREFUNCFILE(RFILE(1)->PROPERTY, RFILE(2)->PROPERTY); \
  80. } \
  81. if (!DirView->UnixColProperties->SortAscending) Result = -Result; \
  82. if (Result == 0) \
  83. { \
  84. Result = FALLBACK(Item1, Item2, DirView); \
  85. } \
  86. } \
  87. return Result; \
  88. }
  89. #define DEFINE_COMPARE_FUNC(PROPERTY, COMPAREFUNC) \
  90. DEFINE_COMPARE_FUNC_EX(PROPERTY, PROPERTY, COMPAREFUNC, COMPAREFUNC, CompareItemFileName)
  91. #define COMPARE_NUMBER(Num1, Num2) ( Num1 < Num2 ? -1 : ( Num1 > Num2 ? 1 : 0) )
  92. #define COMPARE_DUMMY(X1, X2) 0
  93. #define COMPARE_ITEM_DUMMY(X1, X2, DV) 0
  94. #define COMPARE_TOKEN(Token1, Token2) Token1.Compare(Token2)
  95. //---------------------------------------------------------------------------
  96. DEFINE_COMPARE_FUNC_EX(FileName, ItemFileName, CompareLogicalText, CompareLogicalText, COMPARE_ITEM_DUMMY);
  97. DEFINE_COMPARE_FUNC(Size, COMPARE_NUMBER);
  98. DEFINE_COMPARE_FUNC(Modification, COMPARE_NUMBER);
  99. DEFINE_COMPARE_FUNC(RightsStr, AnsiCompareText);
  100. DEFINE_COMPARE_FUNC(Owner, COMPARE_TOKEN);
  101. DEFINE_COMPARE_FUNC(Group, COMPARE_TOKEN);
  102. DEFINE_COMPARE_FUNC_EX(Extension, Extension, CompareLogicalText, COMPARE_DUMMY, CompareItemFileName);
  103. DEFINE_COMPARE_FUNC(LinkTo, CompareLogicalText);
  104. DEFINE_COMPARE_FUNC_EX(TypeName, TypeName, CompareLogicalText, CompareLogicalText, CompareExtension);
  105. //---------------------------------------------------------------------------
  106. #undef DEFINE_COMPARE_FUNC
  107. #undef COMPARE_NUMBER
  108. #undef RFILE
  109. #endif
  110. //---------------------------------------------------------------------------
  111. #define HOMEDIRECTORY L""
  112. //---------------------------------------------------------------------------
  113. __fastcall TUnixDirView::TUnixDirView(TComponent* Owner)
  114. : TCustomUnixDirView(Owner)
  115. {
  116. #ifndef DESIGN_ONLY
  117. FTerminal = NULL;
  118. #endif
  119. FCaseSensitive = true;
  120. DDAllowMove = false;
  121. FShowInaccesibleDirectories = true;
  122. FFullLoad = false;
  123. FDriveView = NULL;
  124. FInvalidNameChars = L"/";
  125. }
  126. //---------------------------------------------------------------------------
  127. __fastcall TUnixDirView::~TUnixDirView()
  128. {
  129. #ifndef DESIGN_ONLY
  130. Terminal = NULL;
  131. #endif
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TUnixDirView::DisplayContextMenu(const TPoint &Where)
  135. {
  136. bool Handled = false;
  137. if (OnContextPopup)
  138. {
  139. OnContextPopup(this, ScreenToClient(Where), Handled);
  140. }
  141. if (!Handled)
  142. {
  143. if (PopupMenu && !PopupMenu->AutoPopup)
  144. {
  145. PopupMenu->Popup(Where.x, Where.y);
  146. }
  147. }
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TUnixDirView::DisplayPropertiesMenu()
  151. {
  152. if (OnDisplayProperties) OnDisplayProperties(this);
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TUnixDirView::ExecuteFile(TListItem * Item)
  156. {
  157. #ifndef DESIGN_ONLY
  158. ASSERT_VALID_ITEM;
  159. if (ITEMFILE->IsDirectory ||
  160. !Terminal->ResolvingSymlinks)
  161. {
  162. PathChanging(true);
  163. ChangeDirectory(ITEMFILE->FileName);
  164. }
  165. else
  166. {
  167. if (ItemFocused != Item) ItemFocused = Item;
  168. DisplayPropertiesMenu();
  169. }
  170. #else
  171. USEDPARAM(Item);
  172. #endif
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TUnixDirView::ExecuteParentDirectory()
  176. {
  177. PathChanging(true);
  178. #ifndef DESIGN_ONLY
  179. ChangeDirectory(PARENTDIRECTORY);
  180. #endif
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TUnixDirView::ExecuteHomeDirectory()
  184. {
  185. #ifndef DESIGN_ONLY
  186. // don't select any directory
  187. PathChanging(false);
  188. UnicodeString APath = Terminal->SessionData->RemoteDirectory;
  189. if (WinConfiguration->DefaultDirIsHome && !APath.IsEmpty() &&
  190. !Terminal->SessionData->UpdateDirectories)
  191. {
  192. if (APath[1] != L'/')
  193. {
  194. Terminal->BeginTransaction();
  195. try
  196. {
  197. ChangeDirectory(HOMEDIRECTORY);
  198. ChangeDirectory(APath);
  199. }
  200. __finally
  201. {
  202. Terminal->EndTransaction();
  203. }
  204. }
  205. else
  206. {
  207. ChangeDirectory(APath);
  208. }
  209. }
  210. else
  211. {
  212. ChangeDirectory(HOMEDIRECTORY);
  213. }
  214. #endif
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TUnixDirView::ReloadDirectory()
  218. {
  219. #ifndef DESIGN_ONLY
  220. FLastPath = L"";
  221. DoAnimation(true);
  222. Terminal->ReloadDirectory();
  223. #endif
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TUnixDirView::ExecuteRootDirectory()
  227. {
  228. #ifndef DESIGN_ONLY
  229. // We set LastPath to top directory, so it will be selected
  230. // after entering root directory
  231. // DISABLED: see PathChanged(): back moves to top directory, not to current
  232. PathChanging(false);
  233. ChangeDirectory(ROOTDIRECTORY);
  234. #endif
  235. }
  236. //---------------------------------------------------------------------------
  237. bool __fastcall TUnixDirView::ItemIsDirectory(TListItem * Item)
  238. {
  239. #ifndef DESIGN_ONLY
  240. ASSERT_VALID_ITEM;
  241. return ITEMFILE->IsDirectory;
  242. #else
  243. USEDPARAM(Item);
  244. return false;
  245. #endif
  246. }
  247. //---------------------------------------------------------------------------
  248. bool __fastcall TUnixDirView::ItemIsFile(TListItem * Item)
  249. {
  250. #ifndef DESIGN_ONLY
  251. ASSERT_VALID_ITEM;
  252. return !(ITEMFILE->IsParentDirectory);
  253. #else
  254. USEDPARAM(Item);
  255. return false;
  256. #endif
  257. }
  258. //---------------------------------------------------------------------------
  259. bool __fastcall TUnixDirView::ItemIsParentDirectory(TListItem * Item)
  260. {
  261. #ifndef DESIGN_ONLY
  262. ASSERT_VALID_ITEM;
  263. return ITEMFILE->IsParentDirectory;
  264. #else
  265. USEDPARAM(Item);
  266. return false;
  267. #endif
  268. }
  269. //---------------------------------------------------------------------------
  270. UnicodeString __fastcall TUnixDirView::ItemFileName(TListItem * Item)
  271. {
  272. #ifndef DESIGN_ONLY
  273. ASSERT_VALID_ITEM;
  274. return ITEMFILE->FileName;
  275. #else
  276. USEDPARAM(Item);
  277. return UnicodeString();
  278. #endif
  279. }
  280. //---------------------------------------------------------------------------
  281. __int64 __fastcall TUnixDirView::ItemFileSize(TListItem * Item)
  282. {
  283. #ifndef DESIGN_ONLY
  284. ASSERT_VALID_ITEM;
  285. return ITEMFILE->Size;
  286. #else
  287. USEDPARAM(Item);
  288. return 0;
  289. #endif
  290. }
  291. //---------------------------------------------------------------------------
  292. UnicodeString __fastcall TUnixDirView::ItemFullFileName(TListItem * Item)
  293. {
  294. #ifndef DESIGN_ONLY
  295. ASSERT_VALID_ITEM;
  296. return ITEMFILE->FullFileName;
  297. #else
  298. USEDPARAM(Item);
  299. return UnicodeString();
  300. #endif
  301. }
  302. //---------------------------------------------------------------------------
  303. int __fastcall TUnixDirView::ItemImageIndex(TListItem * Item, bool /*Cache*/)
  304. {
  305. #ifndef DESIGN_ONLY
  306. ASSERT_VALID_ITEM;
  307. // TCustomDirView::ItemImageIndex is used for icon caching
  308. // so we don't need it here. But it's implemented anyway.
  309. return ITEMFILE->IconIndex;
  310. #else
  311. USEDPARAM(Item);
  312. return 0;
  313. #endif
  314. }
  315. //---------------------------------------------------------------------------
  316. bool __fastcall TUnixDirView::ItemMatchesFilter(TListItem * Item,
  317. const TFileFilter &Filter)
  318. {
  319. #ifndef DESIGN_ONLY
  320. ASSERT_VALID_ITEM;
  321. TRemoteFile *File = ITEMFILE;
  322. int Attr = File->Attr;
  323. return
  324. ((Attr & Filter.IncludeAttr) == Filter.IncludeAttr) &&
  325. ((Attr & Filter.ExcludeAttr) == 0) &&
  326. ((Filter.FileSizeFrom == 0) || (File->Size >= Filter.FileSizeFrom)) &&
  327. ((Filter.FileSizeTo == 0) || (File->Size <= Filter.FileSizeTo)) &&
  328. ((!(int)Filter.ModificationFrom) || (File->Modification >= Filter.ModificationFrom)) &&
  329. ((!(int)Filter.ModificationTo) || (File->Modification <= Filter.ModificationTo)) &&
  330. ((Filter.Masks.IsEmpty()) ||
  331. FileNameMatchesMasks(File->FileName, File->IsDirectory, File->Size, File->Modification, Filter.Masks, false) ||
  332. (File->IsDirectory && Filter.Directories &&
  333. FileNameMatchesMasks(File->FileName, false, File->Size, File->Modification, Filter.Masks, false)));
  334. #else
  335. USEDPARAM(Item);
  336. USEDPARAM(Filter);
  337. return false;
  338. #endif
  339. }
  340. //---------------------------------------------------------------------------
  341. Word __fastcall TUnixDirView::ItemOverlayIndexes(TListItem * Item)
  342. {
  343. #ifndef DESIGN_ONLY
  344. ASSERT_VALID_ITEM;
  345. Word Result = TCustomDirView::ItemOverlayIndexes(Item);
  346. if (ITEMFILE->IsParentDirectory)
  347. {
  348. Result |= oiDirUp;
  349. }
  350. if (ITEMFILE->IsSymLink)
  351. {
  352. Result |= ITEMFILE->BrokenLink ? oiBrokenLink : oiLink;
  353. }
  354. return Result;
  355. #else
  356. USEDPARAM(Item);
  357. return 0;
  358. #endif
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TUnixDirView::LoadFiles()
  362. {
  363. #ifndef DESIGN_ONLY
  364. assert(Terminal);
  365. if (DirOK)
  366. {
  367. // it's enough if we reach this point, we don't require that loading files into
  368. // list succeeded. FDirLoadedAfterChangeDir == false tells only that
  369. // loding file list from server failed, not loading into listview
  370. FDirLoadedAfterChangeDir = true;
  371. FFilesSize = 0;
  372. FHasParentDir = false;
  373. int VisibleFiles = 0;
  374. FHiddenCount = 0;
  375. FFilteredCount = 0;
  376. for (int Index = 0; Index < Terminal->Files->Count; Index++)
  377. {
  378. TRemoteFile *File = Terminal->Files->Files[Index];
  379. assert(File);
  380. if ((!ShowHiddenFiles && File->IsHidden) ||
  381. (!ShowInaccesibleDirectories && File->IsInaccesibleDirectory))
  382. {
  383. FHiddenCount++;
  384. }
  385. else if (!Mask.IsEmpty() &&
  386. !File->IsParentDirectory && !File->IsThisDirectory &&
  387. !FileNameMatchesMasks(File->FileName, File->IsDirectory, File->Size, File->Modification, Mask, true))
  388. {
  389. FFilteredCount++;
  390. }
  391. else
  392. {
  393. VisibleFiles++;
  394. FFilesSize += File->Size;
  395. if (File->IsParentDirectory) FHasParentDir = true;
  396. TListItem * Item = new TListItem(Items);
  397. Item->Data = File;
  398. // Need to add before assigning to .Caption and .OverlayIndex,
  399. // as the setters these call back to owning view.
  400. // Assignment is redundant
  401. Item = Items->AddItem(Item);
  402. Item->Caption = File->FileName;
  403. if (FFullLoad)
  404. {
  405. // this is out of date
  406. // (missing columns and does not update then file properties are loaded)
  407. Item->ImageIndex = File->IconIndex;
  408. Item->SubItems->Add(!File->IsDirectory ? FormatPanelBytes(File->Size, FormatSizeBytes) : UnicodeString());
  409. Item->SubItems->Add(File->UserModificationStr);
  410. Item->SubItems->Add(File->RightsStr);
  411. Item->SubItems->Add(File->Owner.DisplayText);
  412. Item->SubItems->Add(File->Group.DisplayText);
  413. Item->SubItems->Add(File->Extension);
  414. }
  415. }
  416. }
  417. if (OwnerData)
  418. {
  419. Items->Count = VisibleFiles;
  420. }
  421. }
  422. #endif
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TUnixDirView::GetDisplayInfo(TListItem * Item, tagLVITEMW &DispInfo)
  426. {
  427. if (!FFullLoad)
  428. {
  429. #ifndef DESIGN_ONLY
  430. TRemoteFile * File = ITEMFILE;
  431. if (DispInfo.mask & LVIF_TEXT)
  432. {
  433. UnicodeString Value;
  434. switch (DispInfo.iSubItem) {
  435. case uvName: Value = File->FileName; break;
  436. case uvSize:
  437. // expanded from ?: to avoid memory leaks
  438. if (!File->IsDirectory)
  439. {
  440. Value = FormatPanelBytes(File->Size, FormatSizeBytes);
  441. }
  442. break;
  443. case uvChanged: Value = File->UserModificationStr; break;
  444. case uvRights: Value = File->RightsStr; break;
  445. case uvOwner: Value = File->Owner.DisplayText; break;
  446. case uvGroup: Value = File->Group.DisplayText; break;
  447. case uvExt: Value = File->Extension; break;
  448. case uvLinkTarget: Value = File->LinkTo; break;
  449. case uvType: Value = File->TypeName; break;
  450. default: FAIL;
  451. }
  452. StrPLCopy(DispInfo.pszText, Value, DispInfo.cchTextMax);
  453. }
  454. if (DispInfo.iSubItem == 0 && DispInfo.mask & LVIF_IMAGE)
  455. {
  456. DispInfo.iImage = File->IconIndex;
  457. DispInfo.mask |= LVIF_DI_SETITEM;
  458. }
  459. #else
  460. USEDPARAM(Item);
  461. USEDPARAM(DispInfo);
  462. #endif
  463. }
  464. }
  465. //---------------------------------------------------------------------------
  466. bool __fastcall TUnixDirView::PasteFromClipBoard(UnicodeString TargetPath)
  467. {
  468. DragDropFilesEx->FileList->Clear();
  469. bool Result = false;
  470. if (CanPasteFromClipBoard() &&
  471. DragDropFilesEx->GetFromClipboard())
  472. {
  473. if (TargetPath.IsEmpty())
  474. {
  475. TargetPath = PathName;
  476. }
  477. PerformItemDragDropOperation(NULL, DROPEFFECT_COPY);
  478. if (OnDDExecuted != NULL)
  479. {
  480. OnDDExecuted(this, DROPEFFECT_COPY);
  481. }
  482. Result = true;
  483. }
  484. return Result;
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TUnixDirView::PerformItemDragDropOperation(TListItem * Item,
  488. int Effect)
  489. {
  490. #ifndef DESIGN_ONLY
  491. if (OnDDFileOperation)
  492. {
  493. assert(DragDropFilesEx->FileList->Count > 0);
  494. UnicodeString SourceDirectory;
  495. UnicodeString TargetDirectory;
  496. SourceDirectory = ExtractFilePath(DragDropFilesEx->FileList->Items[0]->Name);
  497. if (Item)
  498. {
  499. assert(ITEMFILE->IsDirectory && (Terminal->Files->IndexOf(ITEMFILE) >= 0));
  500. TargetDirectory = ITEMFILE->FullFileName;
  501. }
  502. else
  503. {
  504. TargetDirectory = Path;
  505. }
  506. bool DoFileOperation = true;
  507. OnDDFileOperation(this, Effect, SourceDirectory, TargetDirectory,
  508. DoFileOperation);
  509. }
  510. #else
  511. USEDPARAM(Item);
  512. USEDPARAM(Effect);
  513. #endif
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TUnixDirView::SetItemImageIndex(TListItem * /* Item */, int /* Index */)
  517. {
  518. // TCustomDirView::SetItemImageIndex is used for icon caching
  519. // so we don't need it here.
  520. }
  521. //---------------------------------------------------------------------------
  522. void __fastcall TUnixDirView::DDMenuDone(TObject* /* Sender */, HMENU /* AMenu */)
  523. {
  524. // TODO: Why I need to duplicate this method? (see TCustomDirView::DDMenuDone)
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TUnixDirView::SetDriveView(TCustomUnixDriveView * Value)
  528. {
  529. if (Value != FDriveView)
  530. {
  531. if (FDriveView != NULL)
  532. {
  533. FDriveView->Terminal = NULL;
  534. }
  535. FDriveView = Value;
  536. if (FDriveView != NULL)
  537. {
  538. FDriveView->Terminal = Terminal;
  539. }
  540. }
  541. }
  542. //---------------------------------------------------------------------------
  543. #ifndef DESIGN_ONLY
  544. void __fastcall TUnixDirView::SetTerminal(TTerminal *value)
  545. {
  546. if (FTerminal != value)
  547. {
  548. if (FTerminal)
  549. {
  550. assert(FTerminal->OnReadDirectory == DoReadDirectory);
  551. FTerminal->OnReadDirectory = NULL;
  552. assert(FTerminal->OnStartReadDirectory == DoStartReadDirectory);
  553. FTerminal->OnStartReadDirectory = NULL;
  554. assert(FTerminal->OnChangeDirectory == DoChangeDirectory);
  555. FTerminal->OnChangeDirectory = NULL;
  556. if (!value || !value->Files->Loaded)
  557. {
  558. ClearItems();
  559. }
  560. }
  561. FTerminal = value;
  562. PathChanging(false);
  563. if (FDriveView != NULL)
  564. {
  565. FDriveView->Terminal = FTerminal;
  566. }
  567. if (FTerminal)
  568. {
  569. FTerminal->OnReadDirectory = DoReadDirectory;
  570. FTerminal->OnStartReadDirectory = DoStartReadDirectory;
  571. FTerminal->OnChangeDirectory = DoChangeDirectory;
  572. FTerminal->Files->IncludeParentDirectory = AddParentDir;
  573. if (FTerminal->Files->Loaded)
  574. {
  575. DoChangeDirectory(FTerminal);
  576. DoStartReadDirectory(FTerminal); // just for style and the assertions
  577. DoReadDirectoryImpl(FTerminal, false);
  578. }
  579. }
  580. }
  581. }
  582. #endif
  583. //---------------------------------------------------------------------------
  584. void __fastcall TUnixDirView::DoStartReadDirectory(TObject * /*Sender*/)
  585. {
  586. assert(!FLoading);
  587. FLoading = true;
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TUnixDirView::DoReadDirectory(TObject * Sender, bool ReloadOnly)
  591. {
  592. DoReadDirectoryImpl(Sender, ReloadOnly);
  593. if (FOnRead != NULL)
  594. {
  595. FOnRead(this);
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TUnixDirView::DoReadDirectoryImpl(TObject * /*Sender*/, bool ReloadOnly)
  600. {
  601. assert(FLoading);
  602. FLoading = false;
  603. #ifndef DESIGN_ONLY
  604. if (Terminal->Active)
  605. {
  606. CancelEdit();
  607. if (ReloadOnly)
  608. {
  609. Reload(false);
  610. }
  611. else
  612. {
  613. Load();
  614. PathChanged();
  615. }
  616. if ((FDriveView != NULL) && FDriveView->Visible)
  617. {
  618. FDriveView->LoadDirectory();
  619. }
  620. }
  621. else
  622. {
  623. // Make sure file list is cleared, to remove all references to invalid
  624. // file objects. LoadFiles check for disconnected terminal, so no reloading
  625. // actually occures.
  626. Load();
  627. }
  628. #else
  629. USEDPARAM(ReloadOnly);
  630. #endif
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TUnixDirView::DoChangeDirectory(TObject * /*Sender*/)
  634. {
  635. #ifndef DESIGN_ONLY
  636. // Reload(false);
  637. #endif
  638. }
  639. //---------------------------------------------------------------------------
  640. bool __fastcall TUnixDirView::GetDirOK()
  641. {
  642. #ifndef DESIGN_ONLY
  643. return (Active && Terminal->Files->Loaded);
  644. #else
  645. return false;
  646. #endif
  647. }
  648. //---------------------------------------------------------------------------
  649. UnicodeString __fastcall TUnixDirView::GetPathName()
  650. {
  651. #ifndef DESIGN_ONLY
  652. if (DirOK) return Terminal->CurrentDirectory;
  653. else
  654. #endif
  655. return L"";
  656. }
  657. //---------------------------------------------------------------------------
  658. UnicodeString __fastcall TUnixDirView::GetPath()
  659. {
  660. #ifndef DESIGN_ONLY
  661. if (DirOK) return UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  662. else
  663. #endif
  664. return L"";
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TUnixDirView::SetPath(UnicodeString Value)
  668. {
  669. #ifndef DESIGN_ONLY
  670. Value = UnixExcludeTrailingBackslash(Value);
  671. if (Active && (Terminal->CurrentDirectory != Value))
  672. {
  673. PathChanging(true);
  674. Terminal->CurrentDirectory = Value;
  675. }
  676. #endif
  677. }
  678. //---------------------------------------------------------------------------
  679. void __fastcall TUnixDirView::SortItems()
  680. {
  681. #ifndef DESIGN_ONLY
  682. assert(Terminal);
  683. if (HandleAllocated())
  684. {
  685. PFNLVCOMPARE SortProc;
  686. switch (SortColumn) {
  687. case uvName: SortProc = (PFNLVCOMPARE)CompareItemFileName; break;
  688. case uvSize: SortProc = (PFNLVCOMPARE)CompareSize; break;
  689. case uvChanged: SortProc = (PFNLVCOMPARE)CompareModification; break;
  690. case uvRights: SortProc = (PFNLVCOMPARE)CompareRightsStr; break;
  691. case uvOwner: SortProc = (PFNLVCOMPARE)CompareOwner; break;
  692. case uvGroup: SortProc = (PFNLVCOMPARE)CompareGroup; break;
  693. case uvExt: SortProc = (PFNLVCOMPARE)CompareExtension; break;
  694. case uvLinkTarget: SortProc = (PFNLVCOMPARE)CompareLinkTo; break;
  695. case uvType: SortProc = (PFNLVCOMPARE)CompareTypeName; break;
  696. default: FAIL;
  697. }
  698. CustomSortItems(SortProc);
  699. }
  700. #endif
  701. }
  702. //---------------------------------------------------------------------------
  703. bool __fastcall TUnixDirView::GetActive()
  704. {
  705. #ifndef DESIGN_ONLY
  706. return ((Terminal != NULL) && Terminal->Active);
  707. #else
  708. return false;
  709. #endif
  710. }
  711. //---------------------------------------------------------------------------
  712. void __fastcall TUnixDirView::DDDragDetect(int grfKeyState,
  713. const TPoint &DetectStart, const TPoint &Point, TDragDetectStatus DragStatus)
  714. {
  715. if ((DragStatus == ddsDrag) && (!Loading) && (MarkedCount > 0))
  716. {
  717. TCustomUnixDirView::DDDragDetect(grfKeyState, DetectStart, Point, DragStatus);
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TUnixDirView::SetAddParentDir(bool Value)
  722. {
  723. if (Value != AddParentDir)
  724. {
  725. #ifndef DESIGN_ONLY
  726. if (Terminal) Terminal->Files->IncludeParentDirectory = Value;
  727. #endif
  728. TCustomUnixDirView::SetAddParentDir(Value);
  729. }
  730. }
  731. //---------------------------------------------------------------------------
  732. bool __fastcall TUnixDirView::TargetHasDropHandler(TListItem * /* Item */, int /* Effect */)
  733. {
  734. return false;
  735. }
  736. //---------------------------------------------------------------------------
  737. void __fastcall TUnixDirView::DDChooseEffect(int grfKeyState, int &dwEffect)
  738. {
  739. if ((grfKeyState & (MK_CONTROL | MK_SHIFT)) == 0)
  740. {
  741. dwEffect = DROPEFFECT_Copy;
  742. }
  743. TCustomDirView::DDChooseEffect(grfKeyState, dwEffect);
  744. }
  745. //---------------------------------------------------------------------------
  746. void __fastcall TUnixDirView::SetDDAllowMove(bool value)
  747. {
  748. if (DDAllowMove != value)
  749. {
  750. assert(DragDropFilesEx);
  751. FDDAllowMove = value;
  752. DragDropFilesEx->SourceEffects = DragSourceEffects;
  753. }
  754. }
  755. //---------------------------------------------------------------------------
  756. TDropEffectSet __fastcall TUnixDirView::GetDragSourceEffects()
  757. {
  758. TDropEffectSet Result;
  759. Result << deCopy;
  760. if (DDAllowMove) Result << deMove;
  761. return Result;
  762. }
  763. //---------------------------------------------------------------------------
  764. void __fastcall TUnixDirView::ChangeDirectory(UnicodeString Path)
  765. {
  766. UnicodeString LastFile = L"";
  767. if (ItemFocused) LastFile = ItemFileName(ItemFocused);
  768. ClearItems();
  769. DoAnimation(true);
  770. #ifndef DESIGN_ONLY
  771. try
  772. {
  773. FDirLoadedAfterChangeDir = false;
  774. if (Path == HOMEDIRECTORY)
  775. {
  776. Terminal->HomeDirectory();
  777. }
  778. else
  779. // this works even with LockInHome
  780. if (Path == ROOTDIRECTORY)
  781. {
  782. Terminal->CurrentDirectory = ROOTDIRECTORY;
  783. }
  784. else
  785. {
  786. Terminal->ChangeDirectory(Path);
  787. }
  788. }
  789. __finally
  790. {
  791. // changing directory failed, so we load again old directory
  792. if (!FDirLoadedAfterChangeDir)
  793. {
  794. FSelectFile = LastFile;
  795. Reload(false);
  796. };
  797. }
  798. #endif
  799. }
  800. //---------------------------------------------------------------------------
  801. bool __fastcall TUnixDirView::CanEdit(TListItem* Item)
  802. {
  803. #ifndef DESIGN_ONLY
  804. assert(Terminal);
  805. return TCustomUnixDirView::CanEdit(Item) && Terminal->IsCapable[fcRename];
  806. #else
  807. USEDPARAM(Item);
  808. return false;
  809. #endif
  810. }
  811. //---------------------------------------------------------------------------
  812. void __fastcall TUnixDirView::InternalEdit(const tagLVITEMW & HItem)
  813. {
  814. #ifndef DESIGN_ONLY
  815. TListItem *Item = GetItemFromHItem(HItem);
  816. ASSERT_VALID_ITEM;
  817. LoadEnabled = true;
  818. if (ITEMFILE->FileName != HItem.pszText)
  819. {
  820. FSelectFile = HItem.pszText;
  821. Terminal->RenameFile(ITEMFILE, HItem.pszText, true);
  822. }
  823. #else
  824. USEDPARAM(HItem);
  825. #endif
  826. }
  827. //---------------------------------------------------------------------------
  828. int __fastcall TUnixDirView::HiddenCount()
  829. {
  830. return FHiddenCount;
  831. }
  832. //---------------------------------------------------------------------------
  833. int __fastcall TUnixDirView::FilteredCount()
  834. {
  835. return FFilteredCount;
  836. }
  837. //---------------------------------------------------------------------------
  838. void __fastcall TUnixDirView::CreateDirectory(UnicodeString DirName)
  839. {
  840. CreateDirectoryEx(DirName, NULL);
  841. }
  842. //---------------------------------------------------------------------------
  843. void __fastcall TUnixDirView::CreateDirectoryEx(UnicodeString DirName, const TRemoteProperties * Properties)
  844. {
  845. #ifndef DESIGN_ONLY
  846. assert(Terminal);
  847. // if file would be created in current directory, select it after reload
  848. if (UnixExtractFileName(DirName) == DirName)
  849. {
  850. FSelectFile = DirName;
  851. }
  852. Terminal->CreateDirectory(DirName, Properties);
  853. #else
  854. USEDPARAM(Properties);
  855. #endif
  856. }
  857. //---------------------------------------------------------------------------
  858. bool __fastcall TUnixDirView::GetIsRoot()
  859. {
  860. #ifndef DESIGN_ONLY
  861. return (PathName == ROOTDIRECTORY);
  862. #else
  863. return false;
  864. #endif
  865. }
  866. //---------------------------------------------------------------------------
  867. TColor __fastcall TUnixDirView::ItemColor(TListItem * Item)
  868. {
  869. assert(Item);
  870. #ifndef DESIGN_ONLY
  871. if (DimmHiddenFiles && !Item->Selected && ITEMFILE->IsHidden)
  872. {
  873. return clGrayText;
  874. }
  875. else
  876. #else
  877. USEDPARAM(Item);
  878. #endif
  879. {
  880. return (TColor)clDefaultItemColor;
  881. }
  882. }
  883. //---------------------------------------------------------------------------
  884. TDateTime __fastcall TUnixDirView::ItemFileTime(TListItem * Item,
  885. TDateTimePrecision & Precision)
  886. {
  887. assert(Item);
  888. #ifndef DESIGN_ONLY
  889. switch (ITEMFILE->ModificationFmt)
  890. {
  891. case mfNone:
  892. Precision = tpNone;
  893. break;
  894. case mfMDHM:
  895. Precision = tpMinute;
  896. break;
  897. case mfMDY:
  898. Precision = tpDay;
  899. break;
  900. case mfFull:
  901. default:
  902. Precision = tpSecond;
  903. break;
  904. }
  905. return ITEMFILE->Modification;
  906. #else
  907. USEDPARAM(Item);
  908. Precision = tpSecond;
  909. return Now();
  910. #endif
  911. }
  912. //---------------------------------------------------------------------------
  913. void __fastcall TUnixDirView::SetShowInaccesibleDirectories(bool value)
  914. {
  915. if (FShowInaccesibleDirectories != value)
  916. {
  917. FShowInaccesibleDirectories = value;
  918. if (DirOK) Reload(false);
  919. }
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TUnixDirView::AddToDragFileList(TFileList * FileList,
  923. TListItem * Item)
  924. {
  925. UnicodeString FileName = ItemFullFileName(Item);
  926. #ifndef DESIGN_ONLY
  927. if (OnDDDragFileName != NULL)
  928. {
  929. OnDDDragFileName(this, ITEMFILE, FileName);
  930. }
  931. #endif
  932. FileList->AddItem(NULL, FileName);
  933. }