UnixDriveView.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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);
  497. FChangingDirectory = true;
  498. Terminal->ChangeDirectory(NodePathName(Node));
  499. }
  500. TCustomDriveView::Change(Node);
  501. }
  502. __finally
  503. {
  504. if (SetBusy)
  505. {
  506. EndBusy();
  507. }
  508. if (!FDirectoryLoaded)
  509. {
  510. DebugAssert(!FIgnoreChange);
  511. Expand = false;
  512. FIgnoreChange = true;
  513. try
  514. {
  515. Selected = FPrevSelected;
  516. }
  517. __finally
  518. {
  519. FIgnoreChange = false;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. }
  526. __finally
  527. {
  528. FPrevSelected = Selected;
  529. if (Expand)
  530. {
  531. Selected->Expand(false);
  532. }
  533. CheckPendingDeletes();
  534. }
  535. #else
  536. TCustomDriveView::Change(Node);
  537. #endif
  538. }
  539. //---------------------------------------------------------------------------
  540. void __fastcall TCustomUnixDriveView::CheckPendingDeletes()
  541. {
  542. int Index = 0;
  543. while (Index < FPendingDelete->Count)
  544. {
  545. TTreeNode * Node = static_cast<TTreeNode *>(FPendingDelete->Items[Index]);
  546. // this as well deletes node from FPendingDelete
  547. if (!NodeTryDelete(Node, false))
  548. {
  549. Index++;
  550. }
  551. }
  552. }
  553. //---------------------------------------------------------------------------
  554. void __fastcall TCustomUnixDriveView::PerformDragDropFileOperation(
  555. TTreeNode * Node, int Effect)
  556. {
  557. if (OnDDFileOperation)
  558. {
  559. // see a comment in TUnixDirView::PerformItemDragDropOperation
  560. if (DragDropFilesEx->FileList->Count > 0)
  561. {
  562. DebugAssert(Node != NULL);
  563. UnicodeString SourceDirectory;
  564. UnicodeString TargetDirectory;
  565. SourceDirectory = ExtractFilePath(DragDropFilesEx->FileList->Items[0]->Name);
  566. TargetDirectory = NodeData(Node)->Directory;
  567. bool DoFileOperation = true;
  568. OnDDFileOperation(
  569. this, Effect, SourceDirectory, TargetDirectory, false, DoFileOperation);
  570. }
  571. }
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TCustomUnixDriveView::DDChooseEffect(int KeyState, int & Effect, int PreferredEffect)
  575. {
  576. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  577. if (Effect != DROPEFFECT_NONE)
  578. {
  579. if (DropTarget != NULL)
  580. {
  581. if ((KeyState & (MK_CONTROL | MK_SHIFT)) == 0)
  582. {
  583. Effect = DROPEFFECT_COPY;
  584. }
  585. }
  586. else
  587. {
  588. Effect = DROPEFFECT_NONE;
  589. }
  590. }
  591. TCustomDriveView::DDChooseEffect(KeyState, Effect, PreferredEffect);
  592. }
  593. //---------------------------------------------------------------------------
  594. void __fastcall TCustomUnixDriveView::UpdateDropTarget()
  595. {
  596. // should never be NULL
  597. if (DropTarget != NULL)
  598. {
  599. UpdatePath(DropTarget, false, true);
  600. }
  601. }
  602. //---------------------------------------------------------------------------
  603. void __fastcall TCustomUnixDriveView::UpdateDropSource()
  604. {
  605. // DragNode may be NULL if its parent directory was reloaded as result
  606. // of D&D operation and thus all child nodes are recreated
  607. if ((DragNode != NULL) && (DragNode->Parent != NULL))
  608. {
  609. UpdatePath(DragNode->Parent, false, true);
  610. }
  611. }
  612. //---------------------------------------------------------------------------
  613. TStrings * __fastcall TCustomUnixDriveView::DragFileList()
  614. {
  615. DebugAssert(DragNode != NULL);
  616. TStrings * FileList = new TStringList();
  617. try
  618. {
  619. #ifndef DESIGN_ONLY
  620. FileList->AddObject(ExcludeTrailingBackslash(NodePathName(DragNode)),
  621. NodeFileForce(DragNode));
  622. #endif
  623. }
  624. catch(...)
  625. {
  626. delete FileList;
  627. throw;
  628. }
  629. return FileList;
  630. }
  631. //---------------------------------------------------------------------------
  632. bool __fastcall TCustomUnixDriveView::DragCompleteFileList()
  633. {
  634. return true;
  635. }
  636. //---------------------------------------------------------------------------
  637. TDropEffectSet __fastcall TCustomUnixDriveView::DDSourceEffects()
  638. {
  639. TDropEffectSet Result;
  640. Result << deCopy;
  641. if (DDAllowMove)
  642. {
  643. Result << deMove;
  644. }
  645. return Result;
  646. }
  647. //---------------------------------------------------------------------------
  648. UnicodeString __fastcall TCustomUnixDriveView::NodePathName(TTreeNode * Node)
  649. {
  650. // same as NodePath
  651. return NodeData(Node)->Directory;
  652. }
  653. //---------------------------------------------------------------------------
  654. void __fastcall TCustomUnixDriveView::ClearDragFileList(TFileList * FileList)
  655. {
  656. #ifndef DESIGN_ONLY
  657. if (FDummyDragFile != NULL)
  658. {
  659. SAFE_DESTROY(FDummyDragFile);
  660. }
  661. #endif
  662. TCustomDriveView::ClearDragFileList(FileList);
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TCustomUnixDriveView::AddToDragFileList(TFileList * FileList,
  666. TTreeNode * Node)
  667. {
  668. UnicodeString FileName = NodePathName(Node);
  669. TRemoteFile * File = NodeFileForce(Node);
  670. if (OnDDDragFileName != NULL)
  671. {
  672. OnDDDragFileName(this, File, FileName);
  673. }
  674. FileList->AddItem(NULL, FileName);
  675. }
  676. //---------------------------------------------------------------------------
  677. UnicodeString __fastcall TCustomUnixDriveView::NodePath(TTreeNode * Node)
  678. {
  679. // same as NodePathName
  680. return NodeData(Node)->Directory;
  681. }
  682. //---------------------------------------------------------------------------
  683. bool __fastcall TCustomUnixDriveView::NodeIsRecycleBin(TTreeNode * /*Node*/)
  684. {
  685. return false;
  686. }
  687. //---------------------------------------------------------------------------
  688. bool __fastcall TCustomUnixDriveView::NodePathExists(TTreeNode * /*Node*/)
  689. {
  690. return true;
  691. }
  692. //---------------------------------------------------------------------------
  693. TColor __fastcall TCustomUnixDriveView::NodeColor(TTreeNode * Node)
  694. {
  695. DebugAssert(Node != NULL);
  696. TColor Result = static_cast<TColor>(clDefaultItemColor);
  697. #ifndef DESIGN_ONLY
  698. if (FDimmHiddenDirs && !Node->Selected)
  699. {
  700. if (NodeIsHidden(Node))
  701. {
  702. Result = clGrayText;
  703. }
  704. }
  705. #else
  706. DebugUsedParam(Node);
  707. #endif
  708. return Result;
  709. }
  710. //---------------------------------------------------------------------------
  711. Word __fastcall TCustomUnixDriveView::NodeOverlayIndexes(TTreeNode * Node)
  712. {
  713. #ifndef DESIGN_ONLY
  714. Word Result = oiNoOverlay;
  715. // Cannot query root node for file
  716. if (Node->Parent != NULL)
  717. {
  718. TRemoteFile * File = NodeFile(Node);
  719. if (File != NULL)
  720. {
  721. if (File->IsSymLink)
  722. {
  723. // broken link cannot probably happen anyway
  724. // as broken links are treated as files
  725. Result |= File->BrokenLink ? oiBrokenLink : oiLink;
  726. }
  727. if (File->IsEncrypted)
  728. {
  729. Result |= oiEncrypted;
  730. }
  731. }
  732. }
  733. return Result;
  734. #else
  735. DebugUsedParam(Node);
  736. return 0;
  737. #endif
  738. }
  739. //---------------------------------------------------------------------------
  740. void __fastcall TCustomUnixDriveView::GetImageIndex(TTreeNode * Node)
  741. {
  742. TCustomDriveView::GetImageIndex(Node);
  743. Node->ImageIndex = StdDirIcon;
  744. Node->SelectedIndex = StdDirSelIcon;
  745. }
  746. //---------------------------------------------------------------------------
  747. TTreeNode * __fastcall TCustomUnixDriveView::FindNodeToPath(UnicodeString Path)
  748. {
  749. TTreeNode * Result;
  750. #ifndef DESIGN_ONLY
  751. if (IsUnixRootPath(Path))
  752. {
  753. Result = Items->GetFirstNode();
  754. }
  755. else
  756. {
  757. Result = NULL;
  758. Path = UnixExcludeTrailingBackslash(Path);
  759. TTreeNode * Parent = NULL;
  760. if (!IsUnixRootPath(Path))
  761. {
  762. Parent = FindNodeToPath(UnixExtractFileDir(Path));
  763. }
  764. if ((Parent != NULL) && (Parent->getFirstChild() != NULL))
  765. {
  766. UnicodeString DirName = UnixExtractFileName(Path);
  767. int StartIndex = 0;
  768. int EndIndex = Parent->Count - 1;
  769. while (true)
  770. {
  771. int Index = (StartIndex + EndIndex) / 2;
  772. UnicodeString NodeDir = Parent->Item[Index]->Text;
  773. int C = DoCompareText(DirName, NodeDir);
  774. if (C == 0)
  775. {
  776. Result = Parent->Item[Index];
  777. break;
  778. }
  779. else if (C < 0)
  780. {
  781. if (Index == StartIndex)
  782. {
  783. break;
  784. }
  785. EndIndex = Index - 1;
  786. }
  787. else
  788. {
  789. if (Index == EndIndex)
  790. {
  791. break;
  792. }
  793. StartIndex = Index + 1;
  794. }
  795. }
  796. }
  797. }
  798. #else
  799. Result = NULL;
  800. #endif
  801. return Result;
  802. }
  803. //---------------------------------------------------------------------------
  804. TTreeNode * __fastcall TCustomUnixDriveView::FindPathNode(UnicodeString Path)
  805. {
  806. TTreeNode * Result = NULL;
  807. #ifndef DESIGN_ONLY
  808. if (Items->GetFirstNode() != NULL)
  809. {
  810. do
  811. {
  812. Result = FindNodeToPath(Path);
  813. if (Result == NULL)
  814. {
  815. DebugAssert(!IsUnixRootPath(Path));
  816. Path = UnixExtractFileDir(UnixExcludeTrailingBackslash(Path));
  817. }
  818. }
  819. while (Result == NULL);
  820. }
  821. #endif
  822. return Result;
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall TCustomUnixDriveView::ValidateDirectoryEx(TTreeNode * /*Node*/,
  826. TRecursiveScan /*Recurse*/, bool /*NewDirs*/)
  827. {
  828. // nothing
  829. }
  830. //---------------------------------------------------------------------------
  831. void __fastcall TCustomUnixDriveView::RebuildTree()
  832. {
  833. TTreeNode * First = Items->GetFirstNode();
  834. if (First != NULL)
  835. {
  836. UpdatePath(First, true);
  837. }
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall TCustomUnixDriveView::SetShowInaccesibleDirectories(bool value)
  841. {
  842. if (FShowInaccesibleDirectories != value)
  843. {
  844. FShowInaccesibleDirectories = value;
  845. RebuildTree();
  846. }
  847. }
  848. //---------------------------------------------------------------------------
  849. void __fastcall TCustomUnixDriveView::CMShowingChanged(TMessage & Message)
  850. {
  851. TCustomDriveView::Dispatch(&Message);
  852. if (Showing && (Terminal != NULL))
  853. {
  854. LoadDirectory();
  855. }
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall TCustomUnixDriveView::DisplayContextMenu(TTreeNode * /*Node*/,
  859. const TPoint & /*ScreenPos*/)
  860. {
  861. // TODO
  862. }
  863. //---------------------------------------------------------------------------
  864. void __fastcall TCustomUnixDriveView::DisplayPropertiesMenu(TTreeNode * /*Node*/)
  865. {
  866. // TODO
  867. }
  868. //---------------------------------------------------------------------------
  869. void TCustomUnixDriveView::LoadNodeState(TTreeNode * Node, const UnicodeString & Path)
  870. {
  871. if ((FDirView != NULL) && (FDirView->FAnnouncedDriveViewState != NULL))
  872. {
  873. TStrings * AnnouncedDriveViewState = DebugNotNull(dynamic_cast<TStrings *>(FDirView->FAnnouncedDriveViewState));
  874. if (AnnouncedDriveViewState->IndexOf(Path) >= 0)
  875. {
  876. Node->Expand(false);
  877. }
  878. }
  879. }
  880. //---------------------------------------------------------------------------
  881. TObject * TCustomUnixDriveView::SaveState()
  882. {
  883. #ifndef DESIGN_ONLY
  884. std::unique_ptr<TStrings> ExpandedNodes(CreateSortedStringList());
  885. TTreeNode * Node = Items->GetFirstNode();
  886. while (Node != NULL)
  887. {
  888. // Node->HasChildren would be false in extreme cases only (like while closing the window)
  889. if (Node->Expanded && Node->HasChildren)
  890. {
  891. ExpandedNodes->Add(NodePathName(Node));
  892. }
  893. Node = Node->GetNext();
  894. }
  895. return ExpandedNodes.release();
  896. #else
  897. return NULL;
  898. #endif
  899. }