UnixDirView.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. USEDPARAM(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. int Attr = File->Attr;
  249. return
  250. ((Attr & Filter.IncludeAttr) == Filter.IncludeAttr) &&
  251. ((Attr & Filter.ExcludeAttr) == 0) &&
  252. ((Filter.FileSizeFrom == 0) || (File->Size >= Filter.FileSizeFrom)) &&
  253. ((Filter.FileSizeTo == 0) || (File->Size <= Filter.FileSizeTo)) &&
  254. ((!(int)Filter.ModificationFrom) || (File->Modification >= Filter.ModificationFrom)) &&
  255. ((!(int)Filter.ModificationTo) || (File->Modification <= Filter.ModificationTo)) &&
  256. ((Filter.Masks.IsEmpty()) ||
  257. FileNameMatchesMasks(File->FileName, File->IsDirectory, File->Size, File->Modification, Filter.Masks, false) ||
  258. (File->IsDirectory && Filter.Directories &&
  259. FileNameMatchesMasks(File->FileName, false, File->Size, File->Modification, Filter.Masks, false)));
  260. #else
  261. USEDPARAM(Item);
  262. USEDPARAM(Filter);
  263. return false;
  264. #endif
  265. }
  266. //---------------------------------------------------------------------------
  267. Word __fastcall TUnixDirView::ItemOverlayIndexes(TListItem * Item)
  268. {
  269. #ifndef DESIGN_ONLY
  270. ASSERT_VALID_ITEM;
  271. Word Result = TCustomDirView::ItemOverlayIndexes(Item);
  272. if (ITEMFILE->IsParentDirectory)
  273. {
  274. Result |= oiDirUp;
  275. }
  276. if (ITEMFILE->IsSymLink)
  277. {
  278. Result |= ITEMFILE->BrokenLink ? oiBrokenLink : oiLink;
  279. }
  280. return Result;
  281. #else
  282. USEDPARAM(Item);
  283. return 0;
  284. #endif
  285. }
  286. //---------------------------------------------------------------------------
  287. void __fastcall TUnixDirView::LoadFiles()
  288. {
  289. #ifndef DESIGN_ONLY
  290. assert(Terminal);
  291. if (DirOK)
  292. {
  293. // it's enough if we reach this point, we don't require that loading files into
  294. // list succeeded. FDirLoadedAfterChangeDir == false tells only that
  295. // loding file list from server failed, not loading into listview
  296. FDirLoadedAfterChangeDir = true;
  297. FFilesSize = 0;
  298. FHasParentDir = false;
  299. int VisibleFiles = 0;
  300. FHiddenCount = 0;
  301. FFilteredCount = 0;
  302. for (int Index = 0; Index < Terminal->Files->Count; Index++)
  303. {
  304. TRemoteFile *File = Terminal->Files->Files[Index];
  305. assert(File);
  306. if ((!ShowHiddenFiles && File->IsHidden) ||
  307. (!ShowInaccesibleDirectories && File->IsInaccesibleDirectory))
  308. {
  309. FHiddenCount++;
  310. }
  311. else if (!Mask.IsEmpty() &&
  312. !File->IsParentDirectory && !File->IsThisDirectory &&
  313. !FileNameMatchesMasks(File->FileName, File->IsDirectory, File->Size, File->Modification, Mask, true))
  314. {
  315. FFilteredCount++;
  316. }
  317. else
  318. {
  319. VisibleFiles++;
  320. FFilesSize += File->Size;
  321. if (File->IsParentDirectory) FHasParentDir = true;
  322. TListItem * Item = new TListItem(Items);
  323. Item->Data = File;
  324. // Need to add before assigning to .Caption and .OverlayIndex,
  325. // as the setters these call back to owning view.
  326. // Assignment is redundant
  327. Item = Items->AddItem(Item);
  328. Item->Caption = File->FileName;
  329. if (FFullLoad)
  330. {
  331. // this is out of date
  332. // (missing columns and does not update then file properties are loaded)
  333. Item->ImageIndex = File->IconIndex;
  334. Item->SubItems->Add(!File->IsDirectory ? FormatPanelBytes(File->Size, FormatSizeBytes) : UnicodeString());
  335. Item->SubItems->Add(File->UserModificationStr);
  336. Item->SubItems->Add(File->RightsStr);
  337. Item->SubItems->Add(File->Owner.DisplayText);
  338. Item->SubItems->Add(File->Group.DisplayText);
  339. Item->SubItems->Add(File->Extension);
  340. }
  341. }
  342. }
  343. if (OwnerData)
  344. {
  345. Items->Count = VisibleFiles;
  346. }
  347. }
  348. #endif
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TUnixDirView::GetDisplayInfo(TListItem * Item, tagLVITEMW &DispInfo)
  352. {
  353. if (!FFullLoad)
  354. {
  355. #ifndef DESIGN_ONLY
  356. TRemoteFile * File = ITEMFILE;
  357. if (DispInfo.mask & LVIF_TEXT)
  358. {
  359. UnicodeString Value;
  360. switch (DispInfo.iSubItem) {
  361. case uvName: Value = File->FileName; break;
  362. case uvSize:
  363. // expanded from ?: to avoid memory leaks
  364. if (!File->IsDirectory)
  365. {
  366. Value = FormatPanelBytes(File->Size, FormatSizeBytes);
  367. }
  368. break;
  369. case uvChanged: Value = File->UserModificationStr; break;
  370. case uvRights: Value = File->RightsStr; break;
  371. case uvOwner: Value = File->Owner.DisplayText; break;
  372. case uvGroup: Value = File->Group.DisplayText; break;
  373. case uvExt: Value = File->Extension; break;
  374. case uvLinkTarget: Value = File->LinkTo; break;
  375. case uvType: Value = File->TypeName; break;
  376. default: FAIL;
  377. }
  378. StrPLCopy(DispInfo.pszText, Value, DispInfo.cchTextMax);
  379. }
  380. if (DispInfo.iSubItem == 0 && DispInfo.mask & LVIF_IMAGE)
  381. {
  382. DispInfo.iImage = File->IconIndex;
  383. DispInfo.mask |= LVIF_DI_SETITEM;
  384. }
  385. #else
  386. USEDPARAM(Item);
  387. USEDPARAM(DispInfo);
  388. #endif
  389. }
  390. }
  391. //---------------------------------------------------------------------------
  392. bool __fastcall TUnixDirView::PasteFromClipBoard(UnicodeString TargetPath)
  393. {
  394. DragDropFilesEx->FileList->Clear();
  395. bool Result = false;
  396. if (CanPasteFromClipBoard() &&
  397. DragDropFilesEx->GetFromClipboard())
  398. {
  399. if (TargetPath.IsEmpty())
  400. {
  401. TargetPath = PathName;
  402. }
  403. PerformItemDragDropOperation(NULL, DROPEFFECT_COPY);
  404. if (OnDDExecuted != NULL)
  405. {
  406. OnDDExecuted(this, DROPEFFECT_COPY);
  407. }
  408. Result = true;
  409. }
  410. return Result;
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TUnixDirView::PerformItemDragDropOperation(TListItem * Item,
  414. int Effect)
  415. {
  416. #ifndef DESIGN_ONLY
  417. if (OnDDFileOperation)
  418. {
  419. assert(DragDropFilesEx->FileList->Count > 0);
  420. UnicodeString SourceDirectory;
  421. UnicodeString TargetDirectory;
  422. SourceDirectory = ExtractFilePath(DragDropFilesEx->FileList->Items[0]->Name);
  423. if (Item)
  424. {
  425. assert(ITEMFILE->IsDirectory && (Terminal->Files->IndexOf(ITEMFILE) >= 0));
  426. TargetDirectory = ITEMFILE->FullFileName;
  427. }
  428. else
  429. {
  430. TargetDirectory = Path;
  431. }
  432. bool DoFileOperation = true;
  433. OnDDFileOperation(this, Effect, SourceDirectory, TargetDirectory,
  434. DoFileOperation);
  435. }
  436. #else
  437. USEDPARAM(Item);
  438. USEDPARAM(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. assert(FTerminal->OnReadDirectory == DoReadDirectory);
  477. FTerminal->OnReadDirectory = NULL;
  478. assert(FTerminal->OnStartReadDirectory == DoStartReadDirectory);
  479. FTerminal->OnStartReadDirectory = NULL;
  480. assert(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. }
  507. }
  508. #endif
  509. //---------------------------------------------------------------------------
  510. void __fastcall TUnixDirView::DoStartReadDirectory(TObject * /*Sender*/)
  511. {
  512. assert(!FLoading);
  513. FLoading = true;
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TUnixDirView::DoReadDirectory(TObject * Sender, bool ReloadOnly)
  517. {
  518. DoReadDirectoryImpl(Sender, ReloadOnly);
  519. if (FOnRead != NULL)
  520. {
  521. FOnRead(this);
  522. }
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TUnixDirView::DoReadDirectoryImpl(TObject * /*Sender*/, bool ReloadOnly)
  526. {
  527. assert(FLoading);
  528. FLoading = false;
  529. #ifndef DESIGN_ONLY
  530. if (Terminal->Active)
  531. {
  532. CancelEdit();
  533. if (ReloadOnly)
  534. {
  535. Reload(false);
  536. }
  537. else
  538. {
  539. Load();
  540. PathChanged();
  541. }
  542. if ((FDriveView != NULL) && FDriveView->Visible)
  543. {
  544. FDriveView->LoadDirectory();
  545. }
  546. }
  547. else
  548. {
  549. // Make sure file list is cleared, to remove all references to invalid
  550. // file objects. LoadFiles check for disconnected terminal, so no reloading
  551. // actually occures.
  552. Load();
  553. }
  554. #else
  555. USEDPARAM(ReloadOnly);
  556. #endif
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TUnixDirView::DoChangeDirectory(TObject * /*Sender*/)
  560. {
  561. #ifndef DESIGN_ONLY
  562. // Reload(false);
  563. #endif
  564. }
  565. //---------------------------------------------------------------------------
  566. bool __fastcall TUnixDirView::GetDirOK()
  567. {
  568. #ifndef DESIGN_ONLY
  569. return (Active && Terminal->Files->Loaded);
  570. #else
  571. return false;
  572. #endif
  573. }
  574. //---------------------------------------------------------------------------
  575. UnicodeString __fastcall TUnixDirView::GetPathName()
  576. {
  577. #ifndef DESIGN_ONLY
  578. if (DirOK) return Terminal->CurrentDirectory;
  579. else
  580. #endif
  581. return L"";
  582. }
  583. //---------------------------------------------------------------------------
  584. UnicodeString __fastcall TUnixDirView::GetPath()
  585. {
  586. #ifndef DESIGN_ONLY
  587. if (DirOK) return UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  588. else
  589. #endif
  590. return L"";
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TUnixDirView::SetPath(UnicodeString Value)
  594. {
  595. #ifndef DESIGN_ONLY
  596. Value = UnixExcludeTrailingBackslash(Value);
  597. if (Active && (Terminal->CurrentDirectory != Value))
  598. {
  599. PathChanging(true);
  600. Terminal->CurrentDirectory = Value;
  601. }
  602. #endif
  603. }
  604. //---------------------------------------------------------------------------
  605. #ifndef DESIGN_ONLY
  606. #define COMPARE_NUMBER(Num1, Num2) ( Num1 < Num2 ? -1 : ( Num1 > Num2 ? 1 : 0) )
  607. //---------------------------------------------------------------------------
  608. int __stdcall CompareFile(TListItem * Item1, TListItem * Item2, TUnixDirView * DirView)
  609. {
  610. assert((Item1 != NULL) && (Item2 != NULL));
  611. TRemoteFile * File1 = NOT_NULL((TRemoteFile *)(Item1->Data));
  612. TRemoteFile * File2 = NOT_NULL((TRemoteFile *)(Item2->Data));
  613. int Result;
  614. if (File1->IsParentDirectory && !File2->IsParentDirectory)
  615. {
  616. Result = -1;
  617. }
  618. else if (!File1->IsParentDirectory && File2->IsParentDirectory)
  619. {
  620. Result = 1;
  621. }
  622. else if (File1->IsDirectory && !File2->IsDirectory)
  623. {
  624. Result = -1;
  625. }
  626. else if (!File1->IsDirectory && File2->IsDirectory)
  627. {
  628. Result = 1;
  629. }
  630. else
  631. {
  632. Result = 0;
  633. switch (DirView->SortColumn)
  634. {
  635. case uvName:
  636. // fallback
  637. break;
  638. case uvSize:
  639. Result = COMPARE_NUMBER(File1->Size, File2->Size);
  640. break;
  641. case uvChanged:
  642. Result = COMPARE_NUMBER(File1->Modification, File2->Modification);
  643. break;
  644. case uvRights:
  645. Result = AnsiCompareText(File1->RightsStr, File2->RightsStr);
  646. break;
  647. case uvOwner:
  648. Result = File1->Owner.Compare(File2->Owner);
  649. break;
  650. case uvGroup:
  651. Result = File1->Group.Compare(File2->Group);
  652. break;
  653. case uvExt:
  654. // Duplicated in uvType branch
  655. if (!File1->IsDirectory)
  656. {
  657. Result = CompareLogicalText(File1->Extension, File2->Extension);
  658. }
  659. else
  660. {
  661. // fallback
  662. }
  663. break;
  664. case uvLinkTarget:
  665. Result = CompareLogicalText(File1->LinkTo, File2->LinkTo);
  666. break;
  667. case uvType:
  668. Result = CompareLogicalText(File1->TypeName, File2->TypeName);
  669. // fallback to uvExt
  670. if ((Result == 0) && !File1->IsDirectory)
  671. {
  672. Result = CompareLogicalText(File1->Extension, File2->Extension);
  673. }
  674. break;
  675. default:
  676. FAIL;
  677. }
  678. if (Result == 0)
  679. {
  680. Result = CompareLogicalText(File1->FileName, File2->FileName);
  681. }
  682. if (!DirView->UnixColProperties->SortAscending)
  683. {
  684. Result = -Result;
  685. }
  686. }
  687. return Result;
  688. }
  689. //---------------------------------------------------------------------------
  690. #undef COMPARE_NUMBER
  691. #endif
  692. //---------------------------------------------------------------------------
  693. void __fastcall TUnixDirView::SortItems()
  694. {
  695. #ifndef DESIGN_ONLY
  696. assert(Terminal);
  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. assert(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. DoAnimation(true);
  771. #ifndef DESIGN_ONLY
  772. try
  773. {
  774. FDirLoadedAfterChangeDir = false;
  775. if (Path == HOMEDIRECTORY)
  776. {
  777. Terminal->HomeDirectory();
  778. }
  779. else
  780. // this works even with LockInHome
  781. if (Path == ROOTDIRECTORY)
  782. {
  783. Terminal->CurrentDirectory = ROOTDIRECTORY;
  784. }
  785. else
  786. {
  787. Terminal->ChangeDirectory(Path);
  788. }
  789. }
  790. __finally
  791. {
  792. // changing directory failed, so we load again old directory
  793. if (!FDirLoadedAfterChangeDir)
  794. {
  795. FSelectFile = LastFile;
  796. Reload(false);
  797. };
  798. }
  799. #endif
  800. }
  801. //---------------------------------------------------------------------------
  802. bool __fastcall TUnixDirView::CanEdit(TListItem* Item)
  803. {
  804. #ifndef DESIGN_ONLY
  805. assert(Terminal);
  806. return TCustomUnixDirView::CanEdit(Item) && Terminal->IsCapable[fcRename];
  807. #else
  808. USEDPARAM(Item);
  809. return false;
  810. #endif
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TUnixDirView::InternalEdit(const tagLVITEMW & HItem)
  814. {
  815. #ifndef DESIGN_ONLY
  816. TListItem *Item = GetItemFromHItem(HItem);
  817. ASSERT_VALID_ITEM;
  818. LoadEnabled = true;
  819. if (ITEMFILE->FileName != HItem.pszText)
  820. {
  821. FSelectFile = HItem.pszText;
  822. Terminal->RenameFile(ITEMFILE, HItem.pszText, true);
  823. }
  824. #else
  825. USEDPARAM(HItem);
  826. #endif
  827. }
  828. //---------------------------------------------------------------------------
  829. int __fastcall TUnixDirView::HiddenCount()
  830. {
  831. return FHiddenCount;
  832. }
  833. //---------------------------------------------------------------------------
  834. int __fastcall TUnixDirView::FilteredCount()
  835. {
  836. return FFilteredCount;
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TUnixDirView::CreateDirectory(UnicodeString DirName)
  840. {
  841. CreateDirectoryEx(DirName, NULL);
  842. }
  843. //---------------------------------------------------------------------------
  844. void __fastcall TUnixDirView::CreateDirectoryEx(UnicodeString DirName, const TRemoteProperties * Properties)
  845. {
  846. #ifndef DESIGN_ONLY
  847. assert(Terminal);
  848. // if file would be created in current directory, select it after reload
  849. if (UnixExtractFileName(DirName) == DirName)
  850. {
  851. FSelectFile = DirName;
  852. }
  853. Terminal->CreateDirectory(DirName, Properties);
  854. #else
  855. USEDPARAM(Properties);
  856. #endif
  857. }
  858. //---------------------------------------------------------------------------
  859. bool __fastcall TUnixDirView::GetIsRoot()
  860. {
  861. #ifndef DESIGN_ONLY
  862. return (PathName == ROOTDIRECTORY);
  863. #else
  864. return false;
  865. #endif
  866. }
  867. //---------------------------------------------------------------------------
  868. TColor __fastcall TUnixDirView::ItemColor(TListItem * Item)
  869. {
  870. assert(Item);
  871. #ifndef DESIGN_ONLY
  872. if (DimmHiddenFiles && !Item->Selected && ITEMFILE->IsHidden)
  873. {
  874. return clGrayText;
  875. }
  876. else
  877. #else
  878. USEDPARAM(Item);
  879. #endif
  880. {
  881. return (TColor)clDefaultItemColor;
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. TDateTime __fastcall TUnixDirView::ItemFileTime(TListItem * Item,
  886. TDateTimePrecision & Precision)
  887. {
  888. assert(Item);
  889. #ifndef DESIGN_ONLY
  890. switch (ITEMFILE->ModificationFmt)
  891. {
  892. case mfNone:
  893. Precision = tpNone;
  894. break;
  895. case mfMDHM:
  896. Precision = tpMinute;
  897. break;
  898. case mfMDY:
  899. Precision = tpDay;
  900. break;
  901. case mfFull:
  902. default:
  903. Precision = tpSecond;
  904. break;
  905. }
  906. return ITEMFILE->Modification;
  907. #else
  908. USEDPARAM(Item);
  909. Precision = tpSecond;
  910. return Now();
  911. #endif
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TUnixDirView::SetShowInaccesibleDirectories(bool value)
  915. {
  916. if (FShowInaccesibleDirectories != value)
  917. {
  918. FShowInaccesibleDirectories = value;
  919. if (DirOK) Reload(false);
  920. }
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TUnixDirView::AddToDragFileList(TFileList * FileList,
  924. TListItem * Item)
  925. {
  926. UnicodeString FileName = ItemFullFileName(Item);
  927. #ifndef DESIGN_ONLY
  928. if (OnDDDragFileName != NULL)
  929. {
  930. OnDDDragFileName(this, ITEMFILE, FileName);
  931. }
  932. #endif
  933. FileList->AddItem(NULL, FileName);
  934. }