UnixDirView.cpp 28 KB

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