1
0

UnixDriveView.cpp 26 KB

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