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