UnixDirView.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. DDAllowMove = false;
  47. FShowInaccesibleDirectories = true;
  48. FFullLoad = false;
  49. FDriveView = NULL;
  50. FInvalidNameChars = L"/";
  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. if (FTerminal != value)
  473. {
  474. if (FTerminal)
  475. {
  476. DebugAssert((FTerminal->OnReadDirectory == DoReadDirectory) || Replace);
  477. if (FTerminal->OnReadDirectory == DoReadDirectory)
  478. {
  479. FTerminal->OnReadDirectory = NULL;
  480. }
  481. DebugAssert((FTerminal->OnStartReadDirectory == DoStartReadDirectory) || Replace);
  482. if (FTerminal->OnStartReadDirectory == DoStartReadDirectory)
  483. {
  484. FTerminal->OnStartReadDirectory = NULL;
  485. }
  486. DebugAssert((FTerminal->OnChangeDirectory == DoChangeDirectory) || Replace);
  487. if (FTerminal->OnChangeDirectory == DoChangeDirectory)
  488. {
  489. FTerminal->OnChangeDirectory = NULL;
  490. }
  491. if (!value || !value->Files->Loaded)
  492. {
  493. ClearItems();
  494. }
  495. }
  496. FTerminal = value;
  497. PathChanging(false);
  498. if (FDriveView != NULL)
  499. {
  500. FDriveView->Terminal = FTerminal;
  501. }
  502. if (FTerminal)
  503. {
  504. FTerminal->OnReadDirectory = DoReadDirectory;
  505. FTerminal->OnStartReadDirectory = DoStartReadDirectory;
  506. FTerminal->OnChangeDirectory = DoChangeDirectory;
  507. FTerminal->Files->IncludeParentDirectory = AddParentDir;
  508. if (FTerminal->Files->Loaded)
  509. {
  510. DoChangeDirectory(FTerminal);
  511. DoStartReadDirectory(FTerminal); // just for style and the assertions
  512. DoReadDirectoryImpl(FTerminal, false);
  513. }
  514. }
  515. UpdatePathLabel();
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TUnixDirView::SetTerminal(TTerminal * value)
  520. {
  521. DoSetTerminal(value, false);
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TUnixDirView::ReplaceTerminal(TTerminal * value)
  525. {
  526. DoSetTerminal(value, true);
  527. }
  528. #endif
  529. //---------------------------------------------------------------------------
  530. void __fastcall TUnixDirView::DoStartReadDirectory(TObject * /*Sender*/)
  531. {
  532. DebugAssert(!FLoading);
  533. FLoading = true;
  534. }
  535. //---------------------------------------------------------------------------
  536. void __fastcall TUnixDirView::DoReadDirectory(TObject * Sender, bool ReloadOnly)
  537. {
  538. DoReadDirectoryImpl(Sender, ReloadOnly);
  539. if (FOnRead != NULL)
  540. {
  541. FOnRead(this);
  542. }
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TUnixDirView::DoReadDirectoryImpl(TObject * /*Sender*/, bool ReloadOnly)
  546. {
  547. DebugAssert(FLoading);
  548. FLoading = false;
  549. #ifndef DESIGN_ONLY
  550. if (Terminal->Active)
  551. {
  552. CancelEdit();
  553. if (ReloadOnly)
  554. {
  555. Reload(false);
  556. }
  557. else
  558. {
  559. Load(true);
  560. PathChanged();
  561. }
  562. if ((FDriveView != NULL) && FDriveView->Visible)
  563. {
  564. FDriveView->LoadDirectory();
  565. }
  566. }
  567. else
  568. {
  569. // Make sure file list is cleared, to remove all references to invalid
  570. // file objects. LoadFiles check for disconnected terminal, so no reloading
  571. // actually occures.
  572. Load(true);
  573. }
  574. #else
  575. DebugUsedParam(ReloadOnly);
  576. #endif
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TUnixDirView::DoChangeDirectory(TObject * /*Sender*/)
  580. {
  581. #ifndef DESIGN_ONLY
  582. // Reload(false);
  583. #endif
  584. }
  585. //---------------------------------------------------------------------------
  586. bool __fastcall TUnixDirView::GetDirOK()
  587. {
  588. #ifndef DESIGN_ONLY
  589. return (Active && Terminal->Files->Loaded);
  590. #else
  591. return false;
  592. #endif
  593. }
  594. //---------------------------------------------------------------------------
  595. UnicodeString __fastcall TUnixDirView::GetPathName()
  596. {
  597. #ifndef DESIGN_ONLY
  598. if (DirOK) return Terminal->CurrentDirectory;
  599. else
  600. #endif
  601. return L"";
  602. }
  603. //---------------------------------------------------------------------------
  604. UnicodeString __fastcall TUnixDirView::GetPath()
  605. {
  606. #ifndef DESIGN_ONLY
  607. if (DirOK) return UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  608. else
  609. #endif
  610. return L"";
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TUnixDirView::SetPath(UnicodeString Value)
  614. {
  615. #ifndef DESIGN_ONLY
  616. Value = UnixExcludeTrailingBackslash(Value);
  617. if (Active && (Terminal->CurrentDirectory != Value))
  618. {
  619. PathChanging(true);
  620. Terminal->CurrentDirectory = Value;
  621. }
  622. #endif
  623. }
  624. //---------------------------------------------------------------------------
  625. #ifndef DESIGN_ONLY
  626. #define COMPARE_NUMBER(Num1, Num2) ( Num1 < Num2 ? -1 : ( Num1 > Num2 ? 1 : 0) )
  627. //---------------------------------------------------------------------------
  628. int __stdcall CompareFile(TListItem * Item1, TListItem * Item2, TUnixDirView * DirView)
  629. {
  630. DebugAssert((Item1 != NULL) && (Item2 != NULL));
  631. TRemoteFile * File1 = DebugNotNull((TRemoteFile *)(Item1->Data));
  632. TRemoteFile * File2 = DebugNotNull((TRemoteFile *)(Item2->Data));
  633. int Result;
  634. if (File1->IsParentDirectory && !File2->IsParentDirectory)
  635. {
  636. Result = -1;
  637. }
  638. else if (!File1->IsParentDirectory && File2->IsParentDirectory)
  639. {
  640. Result = 1;
  641. }
  642. else if (File1->IsDirectory && !File2->IsDirectory)
  643. {
  644. Result = -1;
  645. }
  646. else if (!File1->IsDirectory && File2->IsDirectory)
  647. {
  648. Result = 1;
  649. }
  650. else
  651. {
  652. Result = 0;
  653. switch (DirView->SortColumn)
  654. {
  655. case uvName:
  656. // fallback
  657. break;
  658. case uvSize:
  659. Result = COMPARE_NUMBER(File1->Size, File2->Size);
  660. break;
  661. case uvChanged:
  662. Result = COMPARE_NUMBER(File1->Modification, File2->Modification);
  663. break;
  664. case uvRights:
  665. Result = AnsiCompareText(File1->RightsStr, File2->RightsStr);
  666. break;
  667. case uvOwner:
  668. Result = File1->Owner.Compare(File2->Owner);
  669. break;
  670. case uvGroup:
  671. Result = File1->Group.Compare(File2->Group);
  672. break;
  673. case uvExt:
  674. // Duplicated in uvType branch
  675. if (!File1->IsDirectory)
  676. {
  677. Result = CompareLogicalText(File1->Extension, File2->Extension, DirView->NaturalOrderNumericalSorting);
  678. }
  679. else
  680. {
  681. // fallback
  682. }
  683. break;
  684. case uvLinkTarget:
  685. Result = CompareLogicalText(File1->LinkTo, File2->LinkTo, DirView->NaturalOrderNumericalSorting);
  686. break;
  687. case uvType:
  688. Result = CompareLogicalText(File1->TypeName, File2->TypeName, DirView->NaturalOrderNumericalSorting);
  689. // fallback to uvExt
  690. if ((Result == 0) && !File1->IsDirectory)
  691. {
  692. Result = CompareLogicalText(File1->Extension, File2->Extension, DirView->NaturalOrderNumericalSorting);
  693. }
  694. break;
  695. default:
  696. DebugFail();
  697. }
  698. if (Result == 0)
  699. {
  700. Result = CompareLogicalText(File1->FileName, File2->FileName, DirView->NaturalOrderNumericalSorting);
  701. }
  702. if (!DirView->UnixColProperties->SortAscending)
  703. {
  704. Result = -Result;
  705. }
  706. }
  707. return Result;
  708. }
  709. //---------------------------------------------------------------------------
  710. #undef COMPARE_NUMBER
  711. #endif
  712. //---------------------------------------------------------------------------
  713. void __fastcall TUnixDirView::SortItems()
  714. {
  715. #ifndef DESIGN_ONLY
  716. if (HandleAllocated())
  717. {
  718. CustomSortItems(CompareFile);
  719. }
  720. #endif
  721. }
  722. //---------------------------------------------------------------------------
  723. bool __fastcall TUnixDirView::GetActive()
  724. {
  725. #ifndef DESIGN_ONLY
  726. return ((Terminal != NULL) && Terminal->Active);
  727. #else
  728. return false;
  729. #endif
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TUnixDirView::DDDragDetect(int grfKeyState,
  733. const TPoint &DetectStart, const TPoint &Point, TDragDetectStatus DragStatus)
  734. {
  735. if ((DragStatus == ddsDrag) && (!Loading) && (MarkedCount > 0))
  736. {
  737. TCustomUnixDirView::DDDragDetect(grfKeyState, DetectStart, Point, DragStatus);
  738. }
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TUnixDirView::SetAddParentDir(bool Value)
  742. {
  743. if (Value != AddParentDir)
  744. {
  745. #ifndef DESIGN_ONLY
  746. if (Terminal) Terminal->Files->IncludeParentDirectory = Value;
  747. #endif
  748. TCustomUnixDirView::SetAddParentDir(Value);
  749. }
  750. }
  751. //---------------------------------------------------------------------------
  752. bool __fastcall TUnixDirView::TargetHasDropHandler(TListItem * /* Item */, int /* Effect */)
  753. {
  754. return false;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TUnixDirView::DDChooseEffect(int grfKeyState, int &dwEffect)
  758. {
  759. if ((grfKeyState & (MK_CONTROL | MK_SHIFT)) == 0)
  760. {
  761. dwEffect = DROPEFFECT_Copy;
  762. }
  763. TCustomDirView::DDChooseEffect(grfKeyState, dwEffect);
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TUnixDirView::SetDDAllowMove(bool value)
  767. {
  768. if (DDAllowMove != value)
  769. {
  770. DebugAssert(DragDropFilesEx);
  771. FDDAllowMove = value;
  772. DragDropFilesEx->SourceEffects = DragSourceEffects;
  773. }
  774. }
  775. //---------------------------------------------------------------------------
  776. TDropEffectSet __fastcall TUnixDirView::GetDragSourceEffects()
  777. {
  778. TDropEffectSet Result;
  779. Result << deCopy;
  780. if (DDAllowMove) Result << deMove;
  781. return Result;
  782. }
  783. //---------------------------------------------------------------------------
  784. void __fastcall TUnixDirView::ChangeDirectory(UnicodeString Path)
  785. {
  786. UnicodeString LastFile = L"";
  787. if (ItemFocused) LastFile = ItemFileName(ItemFocused);
  788. ClearItems();
  789. #ifndef DESIGN_ONLY
  790. try
  791. {
  792. FDirLoadedAfterChangeDir = false;
  793. if (Path == HOMEDIRECTORY)
  794. {
  795. Terminal->HomeDirectory();
  796. }
  797. else
  798. // this works even with LockInHome
  799. if (Path == ROOTDIRECTORY)
  800. {
  801. Terminal->CurrentDirectory = ROOTDIRECTORY;
  802. }
  803. else
  804. {
  805. Terminal->ChangeDirectory(Path);
  806. }
  807. }
  808. __finally
  809. {
  810. // changing directory failed, so we load again old directory
  811. if (!FDirLoadedAfterChangeDir)
  812. {
  813. FSelectFile = LastFile;
  814. Reload(false);
  815. };
  816. }
  817. #endif
  818. }
  819. //---------------------------------------------------------------------------
  820. bool __fastcall TUnixDirView::CanEdit(TListItem* Item)
  821. {
  822. #ifndef DESIGN_ONLY
  823. DebugAssert(Terminal);
  824. return TCustomUnixDirView::CanEdit(Item) && Terminal->IsCapable[fcRename];
  825. #else
  826. DebugUsedParam(Item);
  827. return false;
  828. #endif
  829. }
  830. //---------------------------------------------------------------------------
  831. void __fastcall TUnixDirView::InternalEdit(const tagLVITEMW & HItem)
  832. {
  833. #ifndef DESIGN_ONLY
  834. TListItem *Item = GetItemFromHItem(HItem);
  835. ASSERT_VALID_ITEM;
  836. LoadEnabled = true;
  837. if (ITEMFILE->FileName != HItem.pszText)
  838. {
  839. FSelectFile = HItem.pszText;
  840. Terminal->RenameFile(ITEMFILE, HItem.pszText, true);
  841. }
  842. #else
  843. DebugUsedParam(HItem);
  844. #endif
  845. }
  846. //---------------------------------------------------------------------------
  847. int __fastcall TUnixDirView::HiddenCount()
  848. {
  849. return FHiddenCount;
  850. }
  851. //---------------------------------------------------------------------------
  852. int __fastcall TUnixDirView::FilteredCount()
  853. {
  854. return FFilteredCount;
  855. }
  856. //---------------------------------------------------------------------------
  857. void __fastcall TUnixDirView::CreateDirectory(UnicodeString DirName)
  858. {
  859. CreateDirectoryEx(DirName, NULL);
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall TUnixDirView::CreateDirectoryEx(UnicodeString DirName, const TRemoteProperties * Properties)
  863. {
  864. #ifndef DESIGN_ONLY
  865. DebugAssert(Terminal);
  866. // if file would be created in current directory, select it after reload
  867. if (UnixExtractFileName(DirName) == DirName)
  868. {
  869. FSelectFile = DirName;
  870. }
  871. Terminal->CreateDirectory(DirName, Properties);
  872. #else
  873. DebugUsedParam(Properties);
  874. #endif
  875. }
  876. //---------------------------------------------------------------------------
  877. bool __fastcall TUnixDirView::GetIsRoot()
  878. {
  879. #ifndef DESIGN_ONLY
  880. return (PathName == ROOTDIRECTORY);
  881. #else
  882. return false;
  883. #endif
  884. }
  885. //---------------------------------------------------------------------------
  886. TColor __fastcall TUnixDirView::ItemColor(TListItem * Item)
  887. {
  888. DebugAssert(Item);
  889. #ifndef DESIGN_ONLY
  890. if (DimmHiddenFiles && !Item->Selected && ITEMFILE->IsHidden)
  891. {
  892. return clGrayText;
  893. }
  894. else
  895. #else
  896. DebugUsedParam(Item);
  897. #endif
  898. {
  899. return (TColor)clDefaultItemColor;
  900. }
  901. }
  902. //---------------------------------------------------------------------------
  903. TDateTime __fastcall TUnixDirView::ItemFileTime(TListItem * Item,
  904. TDateTimePrecision & Precision)
  905. {
  906. DebugAssert(Item);
  907. #ifndef DESIGN_ONLY
  908. switch (ITEMFILE->ModificationFmt)
  909. {
  910. case mfNone:
  911. Precision = tpNone;
  912. break;
  913. case mfMDHM:
  914. Precision = tpMinute;
  915. break;
  916. case mfMDY:
  917. Precision = tpDay;
  918. break;
  919. case mfFull:
  920. default:
  921. Precision = tpSecond;
  922. break;
  923. }
  924. return ITEMFILE->Modification;
  925. #else
  926. DebugUsedParam(Item);
  927. Precision = tpSecond;
  928. return Now();
  929. #endif
  930. }
  931. //---------------------------------------------------------------------------
  932. TObject * __fastcall TUnixDirView::ItemData(TListItem * Item)
  933. {
  934. #ifndef DESIGN_ONLY
  935. return ITEMFILE;
  936. #else
  937. DebugUsedParam(Item);
  938. return NULL;
  939. #endif
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TUnixDirView::SetShowInaccesibleDirectories(bool value)
  943. {
  944. if (FShowInaccesibleDirectories != value)
  945. {
  946. FShowInaccesibleDirectories = value;
  947. if (DirOK) Reload(false);
  948. }
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TUnixDirView::AddToDragFileList(TFileList * FileList,
  952. TListItem * Item)
  953. {
  954. UnicodeString FileName = ItemFullFileName(Item);
  955. #ifndef DESIGN_ONLY
  956. if (OnDDDragFileName != NULL)
  957. {
  958. OnDDDragFileName(this, ITEMFILE, FileName);
  959. }
  960. #endif
  961. FileList->AddItem(NULL, FileName);
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TUnixDirView::UpdatePathLabelCaption()
  965. {
  966. if (Terminal != NULL)
  967. {
  968. TCustomDirView::UpdatePathLabelCaption();
  969. }
  970. else
  971. {
  972. PathLabel->Caption = UnicodeString();
  973. PathLabel->Mask = UnicodeString();
  974. }
  975. }