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