UnixDirView.cpp 28 KB

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