UnixDirView.cpp 29 KB

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