UnixDirView.cpp 29 KB

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