UnixDirView.cpp 32 KB

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