UnixDirView.cpp 28 KB

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