UnixDirView.cpp 28 KB

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