1
0

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