UnixDriveView.cpp 27 KB

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