UnixDirView.cpp 31 KB

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