UnixDirView.cpp 29 KB

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