UnixDirView.cpp 29 KB

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