1
0

UnixDirView.cpp 32 KB

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