UnixDirView.cpp 30 KB

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