UnixDirView.cpp 32 KB

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