UnixDirView.cpp 28 KB

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