UnixDirView.cpp 28 KB

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