UnixDirView.cpp 28 KB

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