UnixDriveView.cpp 27 KB

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