UnixDirView.cpp 28 KB

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