1
0

UnixDirView.cpp 32 KB

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