UnixDriveView.cpp 27 KB

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