UnixDirView.cpp 32 KB

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