UnixDirView.cpp 31 KB

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