UnixDirView.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 not 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. TListItem *Item;
  363. if ((!ShowHiddenFiles && File->IsHidden) ||
  364. (!ShowInaccesibleDirectories && File->IsInaccesibleDirectory))
  365. {
  366. FHiddenCount++;
  367. }
  368. else if (!Mask.IsEmpty() &&
  369. !File->IsDirectory &&
  370. !FileNameMatchesMasks(File->FileName, false, File->Size, File->Modification, Mask))
  371. {
  372. FFilteredCount++;
  373. }
  374. else
  375. {
  376. VisibleFiles++;
  377. if (!File->IsDirectory) FFilesSize += File->Size;
  378. if (File->IsParentDirectory) FHasParentDir = true;
  379. Item = Items->Add();
  380. Item->Data = File;
  381. Item->Caption = File->FileName;
  382. if (FFullLoad)
  383. {
  384. // this is out of date
  385. // (missing columns and does not update then file properties are loaded)
  386. Item->ImageIndex = File->IconIndex;
  387. Item->SubItems->Add((!File->IsDirectory ? FormatFloat(L"#,##0", File->Size) : UnicodeString()));
  388. Item->SubItems->Add(File->UserModificationStr);
  389. Item->SubItems->Add(File->RightsStr);
  390. Item->SubItems->Add(File->Owner.DisplayText);
  391. Item->SubItems->Add(File->Group.DisplayText);
  392. Item->SubItems->Add(File->Extension);
  393. }
  394. }
  395. }
  396. if (OwnerData)
  397. {
  398. Items->Count = VisibleFiles;
  399. }
  400. }
  401. #endif
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TUnixDirView::GetDisplayInfo(TListItem * Item, tagLVITEMW &DispInfo)
  405. {
  406. if (!FFullLoad)
  407. {
  408. #ifndef DESIGN_ONLY
  409. TRemoteFile * File = ITEMFILE;
  410. if (DispInfo.mask & LVIF_TEXT)
  411. {
  412. UnicodeString Value;
  413. switch (DispInfo.iSubItem) {
  414. case uvName: Value = File->FileName; break;
  415. case uvSize:
  416. // expanded from ?: to avoid memory leaks
  417. if (!File->IsDirectory)
  418. {
  419. Value = FormatBytes(File->Size, FormatSizeBytes, FormatSizeBytes);
  420. }
  421. break;
  422. case uvChanged: Value = File->UserModificationStr; break;
  423. case uvRights: Value = File->RightsStr; break;
  424. case uvOwner: Value = File->Owner.DisplayText; break;
  425. case uvGroup: Value = File->Group.DisplayText; break;
  426. case uvExt: Value = File->Extension; break;
  427. case uvLinkTarget: Value = File->LinkTo; break;
  428. case uvType: Value = File->TypeName; break;
  429. default: assert(false);
  430. }
  431. StrPLCopy(DispInfo.pszText, Value, DispInfo.cchTextMax);
  432. }
  433. if (DispInfo.iSubItem == 0 && DispInfo.mask & LVIF_IMAGE)
  434. {
  435. DispInfo.iImage = File->IconIndex;
  436. DispInfo.mask |= LVIF_DI_SETITEM;
  437. }
  438. #else
  439. USEDPARAM(Item);
  440. USEDPARAM(DispInfo);
  441. #endif
  442. }
  443. }
  444. //---------------------------------------------------------------------------
  445. bool __fastcall TUnixDirView::PasteFromClipBoard(UnicodeString TargetPath)
  446. {
  447. DragDropFilesEx->FileList->Clear();
  448. bool Result = false;
  449. if (CanPasteFromClipBoard() &&
  450. DragDropFilesEx->GetFromClipboard())
  451. {
  452. if (TargetPath.IsEmpty())
  453. {
  454. TargetPath = PathName;
  455. }
  456. PerformItemDragDropOperation(NULL, DROPEFFECT_COPY);
  457. if (OnDDExecuted != NULL)
  458. {
  459. OnDDExecuted(this, DROPEFFECT_COPY);
  460. }
  461. Result = true;
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TUnixDirView::PerformItemDragDropOperation(TListItem * Item,
  467. int Effect)
  468. {
  469. #ifndef DESIGN_ONLY
  470. if (OnDDFileOperation)
  471. {
  472. assert(DragDropFilesEx->FileList->Count > 0);
  473. UnicodeString SourceDirectory;
  474. UnicodeString TargetDirectory;
  475. SourceDirectory = ExtractFilePath(DragDropFilesEx->FileList->Items[0]->Name);
  476. if (Item)
  477. {
  478. assert(ITEMFILE->IsDirectory && (Terminal->Files->IndexOf(ITEMFILE) >= 0));
  479. TargetDirectory = ITEMFILE->FullFileName;
  480. }
  481. else
  482. {
  483. TargetDirectory = Path;
  484. }
  485. bool DoFileOperation = true;
  486. OnDDFileOperation(this, Effect, SourceDirectory, TargetDirectory,
  487. DoFileOperation);
  488. }
  489. #else
  490. USEDPARAM(Item);
  491. USEDPARAM(Effect);
  492. #endif
  493. }
  494. //---------------------------------------------------------------------------
  495. void __fastcall TUnixDirView::SetItemImageIndex(TListItem * /* Item */, int /* Index */)
  496. {
  497. // TCustomDirView::SetItemImageIndex is used for icon caching
  498. // so we don't need it here.
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TUnixDirView::DDMenuDone(TObject* /* Sender */, HMENU /* AMenu */)
  502. {
  503. // TODO: Why I need to duplicate this method? (see TCustomDirView::DDMenuDone)
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TUnixDirView::SetDriveView(TCustomUnixDriveView * Value)
  507. {
  508. if (Value != FDriveView)
  509. {
  510. if (FDriveView != NULL)
  511. {
  512. FDriveView->Terminal = NULL;
  513. }
  514. FDriveView = Value;
  515. if (FDriveView != NULL)
  516. {
  517. FDriveView->Terminal = Terminal;
  518. }
  519. }
  520. }
  521. //---------------------------------------------------------------------------
  522. #ifndef DESIGN_ONLY
  523. void __fastcall TUnixDirView::SetTerminal(TTerminal *value)
  524. {
  525. if (FTerminal != value)
  526. {
  527. if (FTerminal)
  528. {
  529. assert(FTerminal->OnReadDirectory == DoReadDirectory);
  530. FTerminal->OnReadDirectory = NULL;
  531. assert(FTerminal->OnStartReadDirectory == DoStartReadDirectory);
  532. FTerminal->OnStartReadDirectory = NULL;
  533. assert(FTerminal->OnChangeDirectory == DoChangeDirectory);
  534. FTerminal->OnChangeDirectory = NULL;
  535. if (!value || !value->Files->Loaded)
  536. {
  537. ClearItems();
  538. }
  539. }
  540. FTerminal = value;
  541. PathChanging(false);
  542. if (FDriveView != NULL)
  543. {
  544. FDriveView->Terminal = FTerminal;
  545. }
  546. if (FTerminal)
  547. {
  548. FTerminal->OnReadDirectory = DoReadDirectory;
  549. FTerminal->OnStartReadDirectory = DoStartReadDirectory;
  550. FTerminal->OnChangeDirectory = DoChangeDirectory;
  551. FTerminal->Files->IncludeParentDirectory = AddParentDir;
  552. if (FTerminal->Files->Loaded)
  553. {
  554. DoChangeDirectory(FTerminal);
  555. DoStartReadDirectory(FTerminal); // just for style and the assertions
  556. DoReadDirectoryImpl(FTerminal, false);
  557. }
  558. }
  559. }
  560. }
  561. #endif
  562. //---------------------------------------------------------------------------
  563. void __fastcall TUnixDirView::DoStartReadDirectory(TObject * /*Sender*/)
  564. {
  565. assert(!FLoading);
  566. FLoading = true;
  567. }
  568. //---------------------------------------------------------------------------
  569. void __fastcall TUnixDirView::DoReadDirectory(TObject * Sender, bool ReloadOnly)
  570. {
  571. DoReadDirectoryImpl(Sender, ReloadOnly);
  572. if (FOnRead != NULL)
  573. {
  574. FOnRead(this);
  575. }
  576. }
  577. //---------------------------------------------------------------------------
  578. void __fastcall TUnixDirView::DoReadDirectoryImpl(TObject * /*Sender*/, bool ReloadOnly)
  579. {
  580. assert(FLoading);
  581. FLoading = false;
  582. #ifndef DESIGN_ONLY
  583. if (Terminal->Active)
  584. {
  585. CancelEdit();
  586. if (ReloadOnly)
  587. {
  588. Reload(false);
  589. }
  590. else
  591. {
  592. Load();
  593. PathChanged();
  594. }
  595. if ((FDriveView != NULL) && FDriveView->Visible)
  596. {
  597. FDriveView->LoadDirectory();
  598. }
  599. }
  600. else
  601. {
  602. // Make sure file list is cleared, to remove all references to invalid
  603. // file objects. LoadFiles check for disconnected terminal, so no reloading
  604. // actually occures.
  605. Load();
  606. }
  607. #else
  608. USEDPARAM(ReloadOnly);
  609. #endif
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TUnixDirView::DoChangeDirectory(TObject * /*Sender*/)
  613. {
  614. #ifndef DESIGN_ONLY
  615. // Reload(false);
  616. #endif
  617. }
  618. //---------------------------------------------------------------------------
  619. bool __fastcall TUnixDirView::GetDirOK()
  620. {
  621. #ifndef DESIGN_ONLY
  622. return (Active && Terminal->Files->Loaded);
  623. #else
  624. return false;
  625. #endif
  626. }
  627. //---------------------------------------------------------------------------
  628. UnicodeString __fastcall TUnixDirView::GetPathName()
  629. {
  630. #ifndef DESIGN_ONLY
  631. if (DirOK) return Terminal->CurrentDirectory;
  632. else
  633. #endif
  634. return L"";
  635. }
  636. //---------------------------------------------------------------------------
  637. UnicodeString __fastcall TUnixDirView::GetPath()
  638. {
  639. #ifndef DESIGN_ONLY
  640. if (DirOK) return UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  641. else
  642. #endif
  643. return L"";
  644. }
  645. //---------------------------------------------------------------------------
  646. void __fastcall TUnixDirView::SetPath(UnicodeString Value)
  647. {
  648. #ifndef DESIGN_ONLY
  649. Value = UnixExcludeTrailingBackslash(Value);
  650. if (Active && (Terminal->CurrentDirectory != Value))
  651. {
  652. PathChanging(true);
  653. Terminal->CurrentDirectory = Value;
  654. }
  655. #endif
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TUnixDirView::SortItems()
  659. {
  660. #ifndef DESIGN_ONLY
  661. assert(Terminal);
  662. if (HandleAllocated())
  663. {
  664. PFNLVCOMPARE SortProc;
  665. switch (SortColumn) {
  666. case uvName: SortProc = (PFNLVCOMPARE)CompareItemFileName; break;
  667. case uvSize: SortProc = (PFNLVCOMPARE)CompareSize; break;
  668. case uvChanged: SortProc = (PFNLVCOMPARE)CompareModification; break;
  669. case uvRights: SortProc = (PFNLVCOMPARE)CompareRightsStr; break;
  670. case uvOwner: SortProc = (PFNLVCOMPARE)CompareOwner; break;
  671. case uvGroup: SortProc = (PFNLVCOMPARE)CompareGroup; break;
  672. case uvExt: SortProc = (PFNLVCOMPARE)CompareExtension; break;
  673. case uvLinkTarget: SortProc = (PFNLVCOMPARE)CompareLinkTo; break;
  674. case uvType: SortProc = (PFNLVCOMPARE)CompareTypeName; break;
  675. default: assert(false);
  676. }
  677. CustomSortItems(SortProc);
  678. }
  679. #endif
  680. }
  681. //---------------------------------------------------------------------------
  682. bool __fastcall TUnixDirView::GetActive()
  683. {
  684. #ifndef DESIGN_ONLY
  685. return ((Terminal != NULL) && Terminal->Active);
  686. #else
  687. return false;
  688. #endif
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TUnixDirView::DDDragDetect(int grfKeyState,
  692. const TPoint &DetectStart, const TPoint &Point, TDragDetectStatus DragStatus)
  693. {
  694. if ((DragStatus == ddsDrag) && (!Loading) && (MarkedCount > 0))
  695. {
  696. TCustomUnixDirView::DDDragDetect(grfKeyState, DetectStart, Point, DragStatus);
  697. }
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TUnixDirView::SetAddParentDir(bool Value)
  701. {
  702. if (Value != AddParentDir)
  703. {
  704. #ifndef DESIGN_ONLY
  705. if (Terminal) Terminal->Files->IncludeParentDirectory = Value;
  706. #endif
  707. TCustomUnixDirView::SetAddParentDir(Value);
  708. }
  709. }
  710. //---------------------------------------------------------------------------
  711. bool __fastcall TUnixDirView::TargetHasDropHandler(TListItem * /* Item */, int /* Effect */)
  712. {
  713. return false;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TUnixDirView::DDChooseEffect(int grfKeyState, int &dwEffect)
  717. {
  718. if ((grfKeyState & (MK_CONTROL | MK_SHIFT)) == 0)
  719. {
  720. dwEffect = DROPEFFECT_Copy;
  721. }
  722. TCustomDirView::DDChooseEffect(grfKeyState, dwEffect);
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TUnixDirView::SetDDAllowMove(bool value)
  726. {
  727. if (DDAllowMove != value)
  728. {
  729. assert(DragDropFilesEx);
  730. FDDAllowMove = value;
  731. DragDropFilesEx->SourceEffects = DragSourceEffects;
  732. }
  733. }
  734. //---------------------------------------------------------------------------
  735. TDropEffectSet __fastcall TUnixDirView::GetDragSourceEffects()
  736. {
  737. TDropEffectSet Result;
  738. Result << deCopy;
  739. if (DDAllowMove) Result << deMove;
  740. return Result;
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TUnixDirView::ChangeDirectory(UnicodeString Path)
  744. {
  745. UnicodeString LastFile = L"";
  746. if (ItemFocused) LastFile = ItemFileName(ItemFocused);
  747. ClearItems();
  748. DoAnimation(true);
  749. #ifndef DESIGN_ONLY
  750. try
  751. {
  752. FDirLoadedAfterChangeDir = false;
  753. APPLICATION_EXCEPTION_HACK_BEGIN
  754. {
  755. if (Path == HOMEDIRECTORY)
  756. {
  757. Terminal->HomeDirectory();
  758. }
  759. else
  760. // this works even with LockInHome
  761. if (Path == ROOTDIRECTORY)
  762. {
  763. Terminal->CurrentDirectory = ROOTDIRECTORY;
  764. }
  765. else
  766. {
  767. Terminal->ChangeDirectory(Path);
  768. }
  769. }
  770. APPLICATION_EXCEPTION_HACK_END;
  771. }
  772. __finally
  773. {
  774. // changing directory failed, so we load again old directory
  775. if (!FDirLoadedAfterChangeDir)
  776. {
  777. FSelectFile = LastFile;
  778. Reload(false);
  779. };
  780. }
  781. #endif
  782. }
  783. //---------------------------------------------------------------------------
  784. bool __fastcall TUnixDirView::CanEdit(TListItem* Item)
  785. {
  786. #ifndef DESIGN_ONLY
  787. assert(Terminal);
  788. return TCustomUnixDirView::CanEdit(Item) && Terminal->IsCapable[fcRename];
  789. #else
  790. USEDPARAM(Item);
  791. return false;
  792. #endif
  793. }
  794. //---------------------------------------------------------------------------
  795. void __fastcall TUnixDirView::InternalEdit(const tagLVITEMW & HItem)
  796. {
  797. #ifndef DESIGN_ONLY
  798. TListItem *Item = GetItemFromHItem(HItem);
  799. ASSERT_VALID_ITEM;
  800. LoadEnabled = true;
  801. if (ITEMFILE->FileName != HItem.pszText)
  802. {
  803. FSelectFile = HItem.pszText;
  804. Terminal->RenameFile(ITEMFILE, HItem.pszText, true);
  805. }
  806. #else
  807. USEDPARAM(HItem);
  808. #endif
  809. }
  810. //---------------------------------------------------------------------------
  811. int __fastcall TUnixDirView::SecondaryColumnHeader(int Index, bool & AliasOnly)
  812. {
  813. AliasOnly = false;
  814. return ((Index == uvName) ? uvExt : -1);
  815. }
  816. //---------------------------------------------------------------------------
  817. int __fastcall TUnixDirView::HiddenCount()
  818. {
  819. return FHiddenCount;
  820. }
  821. //---------------------------------------------------------------------------
  822. int __fastcall TUnixDirView::FilteredCount()
  823. {
  824. return FFilteredCount;
  825. }
  826. //---------------------------------------------------------------------------
  827. void __fastcall TUnixDirView::CreateDirectory(UnicodeString DirName)
  828. {
  829. CreateDirectoryEx(DirName, NULL);
  830. }
  831. //---------------------------------------------------------------------------
  832. void __fastcall TUnixDirView::CreateDirectoryEx(UnicodeString DirName, const TRemoteProperties * Properties)
  833. {
  834. #ifndef DESIGN_ONLY
  835. assert(Terminal);
  836. // if file would be created in current directory, select it after reload
  837. if (UnixExtractFileName(DirName) == DirName)
  838. {
  839. FSelectFile = DirName;
  840. }
  841. Terminal->CreateDirectory(DirName, Properties);
  842. #else
  843. USEDPARAM(Properties);
  844. #endif
  845. }
  846. //---------------------------------------------------------------------------
  847. UnicodeString __fastcall TUnixDirView::MinimizePath(UnicodeString Path, int Length)
  848. {
  849. return StringReplace(MinimizeName(
  850. StringReplace(Path, L'/', L'\\', TReplaceFlags() << rfReplaceAll),
  851. Canvas, Length), L'\\', L'/', TReplaceFlags() << rfReplaceAll);
  852. }
  853. //---------------------------------------------------------------------------
  854. bool __fastcall TUnixDirView::GetIsRoot()
  855. {
  856. #ifndef DESIGN_ONLY
  857. return (PathName == ROOTDIRECTORY);
  858. #else
  859. return false;
  860. #endif
  861. }
  862. //---------------------------------------------------------------------------
  863. TColor __fastcall TUnixDirView::ItemColor(TListItem * Item)
  864. {
  865. assert(Item);
  866. #ifndef DESIGN_ONLY
  867. if (DimmHiddenFiles && !Item->Selected && ITEMFILE->IsHidden)
  868. {
  869. return clGrayText;
  870. }
  871. else
  872. #else
  873. USEDPARAM(Item);
  874. #endif
  875. {
  876. return (TColor)clDefaultItemColor;
  877. }
  878. }
  879. //---------------------------------------------------------------------------
  880. TDateTime __fastcall TUnixDirView::ItemFileTime(TListItem * Item,
  881. TDateTimePrecision & Precision)
  882. {
  883. assert(Item);
  884. #ifndef DESIGN_ONLY
  885. switch (ITEMFILE->ModificationFmt)
  886. {
  887. case mfNone:
  888. Precision = tpNone;
  889. break;
  890. case mfMDHM:
  891. Precision = tpMinute;
  892. break;
  893. case mfMDY:
  894. Precision = tpDay;
  895. break;
  896. case mfFull:
  897. default:
  898. Precision = tpSecond;
  899. break;
  900. }
  901. return ITEMFILE->Modification;
  902. #else
  903. USEDPARAM(Item);
  904. Precision = tpSecond;
  905. return Now();
  906. #endif
  907. }
  908. //---------------------------------------------------------------------------
  909. void __fastcall TUnixDirView::SetShowInaccesibleDirectories(bool value)
  910. {
  911. if (FShowInaccesibleDirectories != value)
  912. {
  913. FShowInaccesibleDirectories = value;
  914. if (DirOK) Reload(false);
  915. }
  916. }
  917. //---------------------------------------------------------------------------
  918. void __fastcall TUnixDirView::AddToDragFileList(TFileList * FileList,
  919. TListItem * Item)
  920. {
  921. UnicodeString FileName = ItemFullFileName(Item);
  922. #ifndef DESIGN_ONLY
  923. if (OnDDDragFileName != NULL)
  924. {
  925. OnDDDragFileName(this, ITEMFILE, FileName);
  926. }
  927. #endif
  928. FileList->AddItem(NULL, FileName);
  929. }