UnixDriveView.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "UnixDriveView.h"
  6. #ifndef DESIGN_ONLY
  7. #include <Terminal.h>
  8. #include <RemoteFiles.h>
  9. #include <VCLCommon.h>
  10. #endif
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. static inline void ValidCtrCheck(TUnixDriveView *)
  14. {
  15. new TUnixDriveView(NULL);
  16. }
  17. //---------------------------------------------------------------------------
  18. namespace Unixdriveview
  19. {
  20. void __fastcall PACKAGE Register()
  21. {
  22. TComponentClass classes[1] = {__classid(TUnixDriveView)};
  23. RegisterComponents(L"Scp", classes, 0);
  24. }
  25. }
  26. //---------------------------------------------------------------------------
  27. __fastcall TUnixDriveView::TUnixDriveView(TComponent * Owner) :
  28. TCustomUnixDriveView(Owner)
  29. {
  30. }
  31. //---------------------------------------------------------------------------
  32. struct TNodeData
  33. {
  34. TRemoteFileList * FileList;
  35. TRemoteFile * File;
  36. UnicodeString Directory;
  37. };
  38. //---------------------------------------------------------------------------
  39. __fastcall TCustomUnixDriveView::TCustomUnixDriveView(TComponent* Owner) :
  40. TCustomDriveView(Owner)
  41. {
  42. FTerminal = NULL;
  43. FRootName = Customunixdirview_SUnixDefaultRootName;
  44. FIgnoreChange = false;
  45. FPrevSelected = NULL;
  46. FDDAllowMove = false;
  47. FShowInaccesibleDirectories = true;
  48. FDummyDragFile = NULL;
  49. FPendingDelete = new TList();
  50. FDragDropFilesEx->PreferCopy = true;
  51. FChangingDirectory = false;
  52. }
  53. //---------------------------------------------------------------------------
  54. __fastcall TCustomUnixDriveView::~TCustomUnixDriveView()
  55. {
  56. Terminal = NULL;
  57. if (FDummyDragFile != NULL)
  58. {
  59. #ifndef DESIGN_ONLY
  60. SAFE_DESTROY(FDummyDragFile);
  61. #endif
  62. }
  63. SAFE_DESTROY(FPendingDelete);
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TCustomUnixDriveView::CreateWnd()
  67. {
  68. TCustomDriveView::CreateWnd();
  69. FDragDropFilesEx->TargetEffects = TDropEffectSet() << deCopy << deMove;
  70. // in case the items were recreated
  71. FPrevSelected = Selected;
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TCustomUnixDriveView::DestroyWnd()
  75. {
  76. // in case we are recreating (TCustomTreeView.DestroyWnd deletes items)
  77. FPrevSelected = NULL;
  78. TCustomDriveView::DestroyWnd();
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TCustomUnixDriveView::SetTerminal(TTerminal * value)
  82. {
  83. #ifndef DESIGN_ONLY
  84. if ((FTerminal != value) || ((FTerminal != NULL) && !FTerminal->Active)) // Abused by TCustomScpExplorerForm::DisconnectSession
  85. {
  86. FTerminal = value;
  87. Items->Clear();
  88. // If terminal is not active initially, we will never load fixed paths, when it becomes active.
  89. // But actually terminal is not active here, only when we are replacing an abandoned terminal
  90. // with a dummy one (which will never become active)
  91. if ((FTerminal != NULL) && FTerminal->Active)
  92. {
  93. TStrings * FixedPaths = FTerminal->FixedPaths;
  94. if (FixedPaths != NULL)
  95. {
  96. for (int i = 0; i < FixedPaths->Count; i++)
  97. {
  98. LoadPath(FixedPaths->Strings[i]);
  99. }
  100. }
  101. }
  102. }
  103. #else
  104. DebugUsedParam(value);
  105. #endif
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCustomUnixDriveView::SetDirView(TUnixDirView * Value)
  109. {
  110. if (FDirView != Value)
  111. {
  112. if (FDirView != NULL)
  113. {
  114. FDirView->DriveView = NULL;
  115. }
  116. FDirView = Value;
  117. if (FDirView != NULL)
  118. {
  119. FDirView->DriveView = this;
  120. }
  121. }
  122. }
  123. //---------------------------------------------------------------------------
  124. TCustomDirView * __fastcall TCustomUnixDriveView::GetCustomDirView()
  125. {
  126. return FDirView;
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall TCustomUnixDriveView::SetCustomDirView(TCustomDirView * Value)
  130. {
  131. SetDirView(dynamic_cast<TUnixDirView *>(Value));
  132. }
  133. //---------------------------------------------------------------------------
  134. bool __fastcall TCustomUnixDriveView::IsRootNameStored()
  135. {
  136. return (FRootName != Customunixdirview_SUnixDefaultRootName);
  137. }
  138. //---------------------------------------------------------------------------
  139. bool __fastcall TCustomUnixDriveView::NodeIsHidden(const TTreeNode * Node)
  140. {
  141. #ifndef DESIGN_ONLY
  142. TNodeData * Data = NodeData(Node);
  143. TRemoteFile * File = Data->File;
  144. return
  145. ((File != NULL) && File->IsHidden) ||
  146. ((File == NULL) && IsUnixHiddenFile(UnixExtractFileName(Data->Directory)));
  147. #else
  148. return false;
  149. #endif
  150. }
  151. //---------------------------------------------------------------------------
  152. bool __fastcall TCustomUnixDriveView::NodeTryDelete(TTreeNode * Node, bool RememberIfFails)
  153. {
  154. bool Result =
  155. (Selected == NULL) ||
  156. ((Selected != Node) && !Selected->HasAsParent(Node));
  157. if (Result)
  158. {
  159. Node->Delete();
  160. }
  161. else
  162. {
  163. if (RememberIfFails)
  164. {
  165. int I = FPendingDelete->IndexOf(Node);
  166. if (I < 0)
  167. {
  168. FPendingDelete->Add(Node);
  169. }
  170. }
  171. }
  172. return Result;
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TCustomUnixDriveView::UpdatePath(TTreeNode * Node, bool Force,
  176. bool CanLoad)
  177. {
  178. #ifndef DESIGN_ONLY
  179. TNodeData * Data = NodeData(Node);
  180. UnicodeString Path = Data->Directory;
  181. TDateTime Timestamp = (Data->FileList != NULL) ? Data->FileList->Timestamp : TDateTime();
  182. TRemoteFileList * FileList = FTerminal->DirectoryFileList(Path, Timestamp, CanLoad);
  183. if ((FileList != NULL) ||
  184. ((Data->FileList != NULL) && Force))
  185. {
  186. TStringList * ChildrenDirs = new TStringList();
  187. TRemoteFileList * OldFileList = NULL;
  188. try
  189. {
  190. if (FileList != NULL)
  191. {
  192. OldFileList = Data->FileList;
  193. Data->FileList = FileList;
  194. }
  195. ChildrenDirs->Duplicates = Types::dupAccept;
  196. ChildrenDirs->CaseSensitive = true;
  197. bool FirstChild = true;
  198. TTreeNode * ChildNode = Node->getFirstChild();
  199. while (ChildNode != NULL)
  200. {
  201. TNodeData * ChildData = NodeData(ChildNode);
  202. ChildData->File = NULL;
  203. ChildrenDirs->AddObject(UnixExtractFileName(ChildData->Directory),
  204. ChildNode);
  205. ChildNode = Node->GetNextChild(ChildNode);
  206. FirstChild = false;
  207. }
  208. // sort only after adding all items.
  209. // should be faster than keeping the list sorted since beginning
  210. ChildrenDirs->Sorted = true;
  211. for (int i = 0; i < Data->FileList->Count; i++)
  212. {
  213. TRemoteFile * File = Data->FileList->Files[i];
  214. if (File->IsDirectory && IsRealFile(File->FileName) &&
  215. (ShowHiddenDirs || !File->IsHidden) &&
  216. (ShowInaccesibleDirectories || !File->IsInaccesibleDirectory))
  217. {
  218. int ChildIndex = ChildrenDirs->IndexOf(File->FileName);
  219. if (ChildIndex >= 0)
  220. {
  221. TTreeNode * ChildNode =
  222. dynamic_cast<TTreeNode *>(ChildrenDirs->Objects[ChildIndex]);
  223. TNodeData * ChildData = NodeData(ChildNode);
  224. ChildData->File = File;
  225. UpdatePath(ChildNode, Force);
  226. }
  227. else
  228. {
  229. UnicodeString ChildPath = UnixIncludeTrailingBackslash(Path) + File->FileName;
  230. DebugAssert(!IsUnixRootPath(ChildPath));
  231. LoadPathEasy(Node, ChildPath, File);
  232. if (FirstChild)
  233. {
  234. LoadNodeState(Node, Path);
  235. FirstChild = false;
  236. }
  237. }
  238. }
  239. }
  240. for (int i = 0; i < ChildrenDirs->Count; i++)
  241. {
  242. TTreeNode * ChildNode = dynamic_cast<TTreeNode *>(ChildrenDirs->Objects[i]);
  243. TNodeData * ChildData = NodeData(ChildNode);
  244. if (ChildData->File == NULL)
  245. {
  246. NodeTryDelete(ChildNode, true);
  247. }
  248. }
  249. Node->AlphaSort(false);
  250. }
  251. __finally
  252. {
  253. delete ChildrenDirs;
  254. // Release files only now, once they are no longer referenced in the tree
  255. delete OldFileList; // if not NULL
  256. }
  257. }
  258. else if (Force)
  259. {
  260. TTreeNode * ChildNode = Node->GetLastChild();
  261. while (ChildNode != NULL)
  262. {
  263. TTreeNode * PrevChildNode = Node->GetPrevChild(ChildNode);
  264. TRemoteFile * File = NodeFile(ChildNode);
  265. if (((ShowHiddenDirs || !NodeIsHidden(ChildNode)) &&
  266. (ShowInaccesibleDirectories || (File == NULL) || !File->IsInaccesibleDirectory)) ||
  267. !NodeTryDelete(ChildNode, true))
  268. {
  269. UpdatePath(ChildNode, true);
  270. }
  271. ChildNode = PrevChildNode;
  272. }
  273. }
  274. #else
  275. DebugUsedParam(Node);
  276. DebugUsedParam(Force);
  277. DebugUsedParam(CanLoad);
  278. #endif
  279. }
  280. //---------------------------------------------------------------------------
  281. TTreeNode * __fastcall TCustomUnixDriveView::LoadPathEasy(TTreeNode * Parent,
  282. UnicodeString Path, TRemoteFile * File)
  283. {
  284. #ifndef DESIGN_ONLY
  285. DebugAssert(Path == UnixExcludeTrailingBackslash(Path));
  286. UnicodeString DirName;
  287. if (IsUnixRootPath(Path))
  288. {
  289. DirName = RootName;
  290. }
  291. else
  292. {
  293. DirName = UnixExtractFileName(Path);
  294. }
  295. TTreeNode * Node = Items->AddChild(Parent, DirName);
  296. TNodeData * Data = new TNodeData();
  297. Node->Data = Data;
  298. Data->Directory = Path;
  299. Data->File = File;
  300. Data->FileList = NULL;
  301. UpdatePath(Node, true);
  302. return Node;
  303. #else
  304. DebugUsedParam(Parent);
  305. DebugUsedParam(File);
  306. return NULL;
  307. #endif
  308. }
  309. //---------------------------------------------------------------------------
  310. TTreeNode * __fastcall TCustomUnixDriveView::LoadPath(UnicodeString Path)
  311. {
  312. #ifndef DESIGN_ONLY
  313. if (Path.IsEmpty())
  314. {
  315. Path = ROOTDIRECTORY;
  316. }
  317. TTreeNode * Node = FindNodeToPath(Path);
  318. if (Node == NULL)
  319. {
  320. Path = UnixExcludeTrailingBackslash(Path);
  321. TTreeNode * Parent = NULL;
  322. TRemoteFile * File = NULL;
  323. UnicodeString ParentPath;
  324. if (!IsUnixRootPath(Path))
  325. {
  326. ParentPath = UnixExtractFileDir(Path);
  327. Parent = LoadPath(ParentPath);
  328. }
  329. Node = FindNodeToPath(Path);
  330. if (Node == NULL)
  331. {
  332. // node still does not exist, this should happen only when
  333. // if the parent directory is not in cache
  334. if (Parent != NULL)
  335. {
  336. TRemoteFileList * ParentFileList = NodeFileList(Parent);
  337. if (ParentFileList != NULL)
  338. {
  339. File = ParentFileList->FindFile(UnixExtractFileName(Path));
  340. }
  341. }
  342. Node = LoadPathEasy(Parent, Path, File);
  343. if (Parent != NULL)
  344. {
  345. LoadNodeState(Parent, ParentPath);
  346. Parent->AlphaSort(false);
  347. }
  348. }
  349. else
  350. {
  351. UpdatePath(Node, false);
  352. }
  353. }
  354. else
  355. {
  356. UpdatePath(Node, false);
  357. }
  358. return Node;
  359. #else
  360. return NULL;
  361. #endif
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TCustomUnixDriveView::LoadDirectory()
  365. {
  366. #ifndef DESIGN_ONLY
  367. DebugAssert(!FIgnoreChange);
  368. FIgnoreChange = true;
  369. try
  370. {
  371. Selected = LoadPath(FTerminal->Files->Directory);
  372. DebugAssert(Selected != NULL);
  373. FPrevSelected = Selected;
  374. }
  375. __finally
  376. {
  377. FIgnoreChange = false;
  378. FDirectoryLoaded = true;
  379. }
  380. #endif
  381. }
  382. //---------------------------------------------------------------------------
  383. TNodeData * __fastcall TCustomUnixDriveView::NodeData(const TTreeNode * Node)
  384. {
  385. DebugAssert(Node->Data != NULL);
  386. return static_cast<TNodeData*>(Node->Data);
  387. }
  388. //---------------------------------------------------------------------------
  389. TRemoteFileList * __fastcall TCustomUnixDriveView::NodeFileList(const TTreeNode * Node)
  390. {
  391. DebugAssert(Node->Data != NULL);
  392. return static_cast<TNodeData*>(Node->Data)->FileList;
  393. }
  394. //---------------------------------------------------------------------------
  395. TRemoteFile * __fastcall TCustomUnixDriveView::NodeFile(const TTreeNode * Node)
  396. {
  397. DebugAssert(Node->Data != NULL);
  398. return static_cast<TNodeData*>(Node->Data)->File;
  399. }
  400. //---------------------------------------------------------------------------
  401. TRemoteFile * __fastcall TCustomUnixDriveView::NodeFileForce(TTreeNode * Node)
  402. {
  403. TRemoteFile * File = NodeFile(Node);
  404. if (File == NULL)
  405. {
  406. #ifndef DESIGN_ONLY
  407. SAFE_DESTROY(FDummyDragFile);
  408. FDummyDragFile = new TRemoteDirectoryFile();
  409. FDummyDragFile->FileName = Node->Text;
  410. FDummyDragFile->FullFileName = NodePathName(Node);
  411. File = FDummyDragFile;
  412. #endif
  413. }
  414. return File;
  415. }
  416. //---------------------------------------------------------------------------
  417. void __fastcall TCustomUnixDriveView::Delete(TTreeNode * Node)
  418. {
  419. if (Node == FPrevSelected)
  420. {
  421. FPrevSelected = NULL;
  422. }
  423. // optimization check
  424. if (FPendingDelete->Count > 0)
  425. {
  426. int I = FPendingDelete->IndexOf(Node);
  427. if (I >= 0)
  428. {
  429. FPendingDelete->Delete(I);
  430. }
  431. }
  432. TNodeData * Data = NULL;
  433. if (Node != NULL)
  434. {
  435. Data = NodeData(Node);
  436. }
  437. TCustomDriveView::Delete(Node);
  438. // delete file list at last, when we are sure that no child nodes exist
  439. if (Data != NULL)
  440. {
  441. #ifndef DESIGN_ONLY
  442. delete Data->FileList;
  443. #endif
  444. delete Data;
  445. }
  446. }
  447. //---------------------------------------------------------------------------
  448. bool __fastcall TCustomUnixDriveView::CanChange(TTreeNode * Node)
  449. {
  450. return
  451. TCustomDriveView::CanChange(Node) &&
  452. !FChangingDirectory;
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TCustomUnixDriveView::Change(TTreeNode * Node)
  456. {
  457. #ifndef DESIGN_ONLY
  458. bool Expand = false;
  459. try
  460. {
  461. // During D&D Selected is set to NULL and then back to previous selection,
  462. // prevent actually changing directory in such case
  463. if (Reading || ControlState.Contains(csRecreating) || FRecreatingHandle ||
  464. (Node == NULL) || (Node == FPrevSelected))
  465. {
  466. TCustomDriveView::Change(Node);
  467. }
  468. else
  469. {
  470. // if previous node is child to newly selected one, do not expand it.
  471. // it is either already expanded and it is even being collapsed.
  472. Expand = (FPrevSelected == NULL) || !FPrevSelected->HasAsParent(Node);
  473. if (FIgnoreChange)
  474. {
  475. TCustomDriveView::Change(Node);
  476. }
  477. else
  478. {
  479. if (FDirView != NULL)
  480. {
  481. // remember current directory, so it gets selected if we move to parent
  482. // directory
  483. FDirView->ContinueSession(true);
  484. }
  485. FDirectoryLoaded = false;
  486. bool SetBusy = !ControlState.Contains(csRecreating);
  487. if (SetBusy)
  488. {
  489. StartBusy();
  490. }
  491. try
  492. {
  493. {
  494. // Prevent further changes while loading the folder.
  495. // Particularly prevent user from trying to proceed with incremental search.
  496. TValueRestorer<bool> ChangingDirectoryRestorer(FChangingDirectory, true);
  497. Terminal->ChangeDirectory(NodePathName(Node));
  498. }
  499. TCustomDriveView::Change(Node);
  500. }
  501. __finally
  502. {
  503. if (SetBusy)
  504. {
  505. EndBusy();
  506. }
  507. if (!FDirectoryLoaded)
  508. {
  509. DebugAssert(!FIgnoreChange);
  510. Expand = false;
  511. FIgnoreChange = true;
  512. try
  513. {
  514. Selected = FPrevSelected;
  515. }
  516. __finally
  517. {
  518. FIgnoreChange = false;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. __finally
  526. {
  527. FPrevSelected = Selected;
  528. if (Expand)
  529. {
  530. Selected->Expand(false);
  531. }
  532. CheckPendingDeletes();
  533. }
  534. #else
  535. TCustomDriveView::Change(Node);
  536. #endif
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TCustomUnixDriveView::CheckPendingDeletes()
  540. {
  541. int Index = 0;
  542. while (Index < FPendingDelete->Count)
  543. {
  544. TTreeNode * Node = static_cast<TTreeNode *>(FPendingDelete->Items[Index]);
  545. // this as well deletes node from FPendingDelete
  546. if (!NodeTryDelete(Node, false))
  547. {
  548. Index++;
  549. }
  550. }
  551. }
  552. //---------------------------------------------------------------------------
  553. void __fastcall TCustomUnixDriveView::PerformDragDropFileOperation(
  554. TTreeNode * Node, int Effect)
  555. {
  556. if (OnDDFileOperation)
  557. {
  558. // see a comment in TUnixDirView::PerformItemDragDropOperation
  559. if (DragDropFilesEx->FileList->Count > 0)
  560. {
  561. DebugAssert(Node != NULL);
  562. UnicodeString SourceDirectory;
  563. UnicodeString TargetDirectory;
  564. SourceDirectory = ExtractFilePath(DragDropFilesEx->FileList->Items[0]->Name);
  565. TargetDirectory = NodeData(Node)->Directory;
  566. bool DoFileOperation = true;
  567. OnDDFileOperation(
  568. this, Effect, SourceDirectory, TargetDirectory, false, DoFileOperation);
  569. }
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. void __fastcall TCustomUnixDriveView::DDChooseEffect(int KeyState, int & Effect, int PreferredEffect)
  574. {
  575. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  576. if (Effect != DROPEFFECT_NONE)
  577. {
  578. if (DropTarget != NULL)
  579. {
  580. if ((KeyState & (MK_CONTROL | MK_SHIFT)) == 0)
  581. {
  582. Effect = DROPEFFECT_COPY;
  583. }
  584. }
  585. else
  586. {
  587. Effect = DROPEFFECT_NONE;
  588. }
  589. }
  590. TCustomDriveView::DDChooseEffect(KeyState, Effect, PreferredEffect);
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TCustomUnixDriveView::UpdateDropTarget()
  594. {
  595. // should never be NULL
  596. if (DropTarget != NULL)
  597. {
  598. UpdatePath(DropTarget, false, true);
  599. }
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TCustomUnixDriveView::UpdateDropSource()
  603. {
  604. // DragNode may be NULL if its parent directory was reloaded as result
  605. // of D&D operation and thus all child nodes are recreated
  606. if ((DragNode != NULL) && (DragNode->Parent != NULL))
  607. {
  608. UpdatePath(DragNode->Parent, false, true);
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. TStrings * __fastcall TCustomUnixDriveView::DragFileList()
  613. {
  614. DebugAssert(DragNode != NULL);
  615. TStrings * FileList = new TStringList();
  616. try
  617. {
  618. #ifndef DESIGN_ONLY
  619. FileList->AddObject(ExcludeTrailingBackslash(NodePathName(DragNode)),
  620. NodeFileForce(DragNode));
  621. #endif
  622. }
  623. catch(...)
  624. {
  625. delete FileList;
  626. throw;
  627. }
  628. return FileList;
  629. }
  630. //---------------------------------------------------------------------------
  631. bool __fastcall TCustomUnixDriveView::DragCompleteFileList()
  632. {
  633. return true;
  634. }
  635. //---------------------------------------------------------------------------
  636. TDropEffectSet __fastcall TCustomUnixDriveView::DDSourceEffects()
  637. {
  638. TDropEffectSet Result;
  639. Result << deCopy;
  640. if (DDAllowMove)
  641. {
  642. Result << deMove;
  643. }
  644. return Result;
  645. }
  646. //---------------------------------------------------------------------------
  647. UnicodeString __fastcall TCustomUnixDriveView::NodePathName(TTreeNode * Node)
  648. {
  649. // same as NodePath
  650. return NodeData(Node)->Directory;
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TCustomUnixDriveView::ClearDragFileList(TFileList * FileList)
  654. {
  655. #ifndef DESIGN_ONLY
  656. if (FDummyDragFile != NULL)
  657. {
  658. SAFE_DESTROY(FDummyDragFile);
  659. }
  660. #endif
  661. TCustomDriveView::ClearDragFileList(FileList);
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TCustomUnixDriveView::AddToDragFileList(TFileList * FileList,
  665. TTreeNode * Node)
  666. {
  667. UnicodeString FileName = NodePathName(Node);
  668. TRemoteFile * File = NodeFileForce(Node);
  669. if (OnDDDragFileName != NULL)
  670. {
  671. OnDDDragFileName(this, File, FileName);
  672. }
  673. FileList->AddItem(NULL, FileName);
  674. }
  675. //---------------------------------------------------------------------------
  676. UnicodeString __fastcall TCustomUnixDriveView::NodePath(TTreeNode * Node)
  677. {
  678. // same as NodePathName
  679. return NodeData(Node)->Directory;
  680. }
  681. //---------------------------------------------------------------------------
  682. bool __fastcall TCustomUnixDriveView::NodeIsRecycleBin(TTreeNode * /*Node*/)
  683. {
  684. return false;
  685. }
  686. //---------------------------------------------------------------------------
  687. bool __fastcall TCustomUnixDriveView::NodePathExists(TTreeNode * /*Node*/)
  688. {
  689. return true;
  690. }
  691. //---------------------------------------------------------------------------
  692. TColor __fastcall TCustomUnixDriveView::NodeColor(TTreeNode * Node)
  693. {
  694. DebugAssert(Node != NULL);
  695. TColor Result = static_cast<TColor>(clDefaultItemColor);
  696. #ifndef DESIGN_ONLY
  697. if (FDimmHiddenDirs && !Node->Selected)
  698. {
  699. if (NodeIsHidden(Node))
  700. {
  701. Result = clGrayText;
  702. }
  703. }
  704. #else
  705. DebugUsedParam(Node);
  706. #endif
  707. return Result;
  708. }
  709. //---------------------------------------------------------------------------
  710. Word __fastcall TCustomUnixDriveView::NodeOverlayIndexes(TTreeNode * Node)
  711. {
  712. #ifndef DESIGN_ONLY
  713. Word Result = oiNoOverlay;
  714. // Cannot query root node for file
  715. if (Node->Parent != NULL)
  716. {
  717. TRemoteFile * File = NodeFile(Node);
  718. if (File != NULL)
  719. {
  720. if (File->IsSymLink)
  721. {
  722. // broken link cannot probably happen anyway
  723. // as broken links are treated as files
  724. Result |= File->BrokenLink ? oiBrokenLink : oiLink;
  725. }
  726. if (File->IsEncrypted)
  727. {
  728. Result |= oiEncrypted;
  729. }
  730. }
  731. }
  732. return Result;
  733. #else
  734. DebugUsedParam(Node);
  735. return 0;
  736. #endif
  737. }
  738. //---------------------------------------------------------------------------
  739. void __fastcall TCustomUnixDriveView::GetImageIndex(TTreeNode * Node)
  740. {
  741. TCustomDriveView::GetImageIndex(Node);
  742. Node->ImageIndex = StdDirIcon;
  743. Node->SelectedIndex = StdDirSelIcon;
  744. }
  745. //---------------------------------------------------------------------------
  746. TTreeNode * __fastcall TCustomUnixDriveView::FindNodeToPath(UnicodeString Path)
  747. {
  748. TTreeNode * Result;
  749. #ifndef DESIGN_ONLY
  750. if (IsUnixRootPath(Path))
  751. {
  752. Result = Items->GetFirstNode();
  753. }
  754. else
  755. {
  756. Result = NULL;
  757. Path = UnixExcludeTrailingBackslash(Path);
  758. TTreeNode * Parent = NULL;
  759. if (!IsUnixRootPath(Path))
  760. {
  761. Parent = FindNodeToPath(UnixExtractFileDir(Path));
  762. }
  763. if ((Parent != NULL) && (Parent->getFirstChild() != NULL))
  764. {
  765. UnicodeString DirName = UnixExtractFileName(Path);
  766. int StartIndex = 0;
  767. int EndIndex = Parent->Count - 1;
  768. while (true)
  769. {
  770. int Index = (StartIndex + EndIndex) / 2;
  771. UnicodeString NodeDir = Parent->Item[Index]->Text;
  772. int C = DoCompareText(DirName, NodeDir);
  773. if (C == 0)
  774. {
  775. Result = Parent->Item[Index];
  776. break;
  777. }
  778. else if (C < 0)
  779. {
  780. if (Index == StartIndex)
  781. {
  782. break;
  783. }
  784. EndIndex = Index - 1;
  785. }
  786. else
  787. {
  788. if (Index == EndIndex)
  789. {
  790. break;
  791. }
  792. StartIndex = Index + 1;
  793. }
  794. }
  795. }
  796. }
  797. #else
  798. Result = NULL;
  799. #endif
  800. return Result;
  801. }
  802. //---------------------------------------------------------------------------
  803. TTreeNode * __fastcall TCustomUnixDriveView::FindPathNode(UnicodeString Path)
  804. {
  805. TTreeNode * Result = NULL;
  806. #ifndef DESIGN_ONLY
  807. if (Items->GetFirstNode() != NULL)
  808. {
  809. do
  810. {
  811. Result = FindNodeToPath(Path);
  812. if (Result == NULL)
  813. {
  814. DebugAssert(!IsUnixRootPath(Path));
  815. Path = UnixExtractFileDir(UnixExcludeTrailingBackslash(Path));
  816. }
  817. }
  818. while (Result == NULL);
  819. }
  820. #endif
  821. return Result;
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TCustomUnixDriveView::ValidateDirectoryEx(TTreeNode * /*Node*/,
  825. TRecursiveScan /*Recurse*/, bool /*NewDirs*/)
  826. {
  827. // nothing
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TCustomUnixDriveView::RebuildTree()
  831. {
  832. TTreeNode * First = Items->GetFirstNode();
  833. if (First != NULL)
  834. {
  835. UpdatePath(First, true);
  836. }
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall TCustomUnixDriveView::SetShowInaccesibleDirectories(bool value)
  840. {
  841. if (FShowInaccesibleDirectories != value)
  842. {
  843. FShowInaccesibleDirectories = value;
  844. RebuildTree();
  845. }
  846. }
  847. //---------------------------------------------------------------------------
  848. void __fastcall TCustomUnixDriveView::CMShowingChanged(TMessage & Message)
  849. {
  850. TCustomDriveView::Dispatch(&Message);
  851. if (Showing && (Terminal != NULL))
  852. {
  853. LoadDirectory();
  854. }
  855. }
  856. //---------------------------------------------------------------------------
  857. void __fastcall TCustomUnixDriveView::DisplayContextMenu(TTreeNode * /*Node*/,
  858. const TPoint & /*ScreenPos*/)
  859. {
  860. // TODO
  861. }
  862. //---------------------------------------------------------------------------
  863. void __fastcall TCustomUnixDriveView::DisplayPropertiesMenu(TTreeNode * /*Node*/)
  864. {
  865. // TODO
  866. }
  867. //---------------------------------------------------------------------------
  868. void TCustomUnixDriveView::LoadNodeState(TTreeNode * Node, const UnicodeString & Path)
  869. {
  870. if ((FDirView != NULL) && (FDirView->FAnnouncedDriveViewState != NULL))
  871. {
  872. TStrings * AnnouncedDriveViewState = DebugNotNull(dynamic_cast<TStrings *>(FDirView->FAnnouncedDriveViewState));
  873. if (AnnouncedDriveViewState->IndexOf(Path) >= 0)
  874. {
  875. Node->Expand(false);
  876. }
  877. }
  878. }
  879. //---------------------------------------------------------------------------
  880. TObject * TCustomUnixDriveView::SaveState()
  881. {
  882. #ifndef DESIGN_ONLY
  883. std::unique_ptr<TStrings> ExpandedNodes(CreateSortedStringList());
  884. TTreeNode * Node = Items->GetFirstNode();
  885. while (Node != NULL)
  886. {
  887. // Node->HasChildren would be false in extreme cases only (like while closing the window)
  888. if (Node->Expanded && Node->HasChildren)
  889. {
  890. ExpandedNodes->Add(NodePathName(Node));
  891. }
  892. Node = Node->GetNext();
  893. }
  894. return ExpandedNodes.release();
  895. #else
  896. return NULL;
  897. #endif
  898. }