UnixDriveView.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. //---------------------------------------------------------------------------
  2. #pragma warn -pch // WORKAROUND (see My.cpp)
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "UnixDriveView.h"
  7. #ifndef DESIGN_ONLY
  8. #include <Terminal.h>
  9. #include <RemoteFiles.h>
  10. #include <VCLCommon.h>
  11. #endif
  12. #pragma package(smart_init)
  13. //---------------------------------------------------------------------------
  14. static inline void ValidCtrCheck(TUnixDriveView *)
  15. {
  16. new TUnixDriveView(NULL);
  17. }
  18. //---------------------------------------------------------------------------
  19. namespace Unixdriveview
  20. {
  21. void __fastcall PACKAGE Register()
  22. {
  23. TComponentClass classes[1] = {__classid(TUnixDriveView)};
  24. RegisterComponents(L"Scp", classes, 0);
  25. }
  26. }
  27. //---------------------------------------------------------------------------
  28. __fastcall TUnixDriveView::TUnixDriveView(TComponent * Owner) :
  29. TCustomUnixDriveView(Owner)
  30. {
  31. }
  32. //---------------------------------------------------------------------------
  33. struct TNodeData
  34. {
  35. TRemoteFileList * FileList;
  36. TRemoteFile * File;
  37. UnicodeString Directory;
  38. };
  39. //---------------------------------------------------------------------------
  40. __fastcall TCustomUnixDriveView::TCustomUnixDriveView(TComponent* Owner) :
  41. TCustomDriveView(Owner)
  42. {
  43. FTerminal = NULL;
  44. FRootName = Customunixdirview_SUnixDefaultRootName;
  45. FIgnoreChange = false;
  46. FPrevSelected = NULL;
  47. FDDAllowMove = false;
  48. FShowInaccesibleDirectories = true;
  49. FDummyDragFile = NULL;
  50. FPendingDelete = new TList();
  51. FDragDropFilesEx->PreferCopy = true;
  52. FChangingDirectory = false;
  53. }
  54. //---------------------------------------------------------------------------
  55. __fastcall TCustomUnixDriveView::~TCustomUnixDriveView()
  56. {
  57. Terminal = NULL;
  58. if (FDummyDragFile != NULL)
  59. {
  60. #ifndef DESIGN_ONLY
  61. SAFE_DESTROY(FDummyDragFile);
  62. #endif
  63. }
  64. SAFE_DESTROY(FPendingDelete);
  65. }
  66. //---------------------------------------------------------------------------
  67. void __fastcall TCustomUnixDriveView::CreateWnd()
  68. {
  69. TCustomDriveView::CreateWnd();
  70. FDragDropFilesEx->TargetEffects = TDropEffectSet() << deCopy << deMove;
  71. // in case the items were recreated
  72. FPrevSelected = Selected;
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TCustomUnixDriveView::DestroyWnd()
  76. {
  77. // in case we are recreating (TCustomTreeView.DestroyWnd deletes items)
  78. FPrevSelected = NULL;
  79. TCustomDriveView::DestroyWnd();
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TCustomUnixDriveView::SetTerminal(TTerminal * value)
  83. {
  84. #ifndef DESIGN_ONLY
  85. if ((FTerminal != value) || ((FTerminal != NULL) && !FTerminal->Active)) // Abused by TCustomScpExplorerForm::DisconnectSession
  86. {
  87. FTerminal = value;
  88. Items->Clear();
  89. // If terminal is not active initially, we will never load fixed paths, when it becomes active.
  90. // But actually terminal is not active here, only when we are replacing an abandoned terminal
  91. // with a dummy one (which will never become active)
  92. if ((FTerminal != NULL) && FTerminal->Active)
  93. {
  94. TStrings * FixedPaths = FTerminal->FixedPaths;
  95. if (FixedPaths != NULL)
  96. {
  97. for (int i = 0; i < FixedPaths->Count; i++)
  98. {
  99. LoadPath(FixedPaths->Strings[i]);
  100. }
  101. }
  102. }
  103. }
  104. #else
  105. DebugUsedParam(value);
  106. #endif
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TCustomUnixDriveView::SetDirView(TUnixDirView * Value)
  110. {
  111. if (FDirView != Value)
  112. {
  113. if (FDirView != NULL)
  114. {
  115. FDirView->DriveView = NULL;
  116. }
  117. FDirView = Value;
  118. if (FDirView != NULL)
  119. {
  120. FDirView->DriveView = this;
  121. }
  122. }
  123. }
  124. //---------------------------------------------------------------------------
  125. TCustomDirView * __fastcall TCustomUnixDriveView::GetCustomDirView()
  126. {
  127. return FDirView;
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TCustomUnixDriveView::SetCustomDirView(TCustomDirView * Value)
  131. {
  132. SetDirView(dynamic_cast<TUnixDirView *>(Value));
  133. }
  134. //---------------------------------------------------------------------------
  135. bool __fastcall TCustomUnixDriveView::IsRootNameStored()
  136. {
  137. return (FRootName != Customunixdirview_SUnixDefaultRootName);
  138. }
  139. //---------------------------------------------------------------------------
  140. bool __fastcall TCustomUnixDriveView::NodeIsHidden(const TTreeNode * Node)
  141. {
  142. #ifndef DESIGN_ONLY
  143. TNodeData * Data = NodeData(Node);
  144. TRemoteFile * File = Data->File;
  145. return
  146. ((File != NULL) && File->IsHidden) ||
  147. ((File == NULL) && IsUnixHiddenFile(UnixExtractFileName(Data->Directory)));
  148. #else
  149. return false;
  150. #endif
  151. }
  152. //---------------------------------------------------------------------------
  153. bool __fastcall TCustomUnixDriveView::NodeTryDelete(TTreeNode * Node, bool RememberIfFails)
  154. {
  155. bool Result =
  156. (Selected == NULL) ||
  157. ((Selected != Node) && !Selected->HasAsParent(Node));
  158. if (Result)
  159. {
  160. Node->Delete();
  161. }
  162. else
  163. {
  164. if (RememberIfFails)
  165. {
  166. int I = FPendingDelete->IndexOf(Node);
  167. if (I < 0)
  168. {
  169. FPendingDelete->Add(Node);
  170. }
  171. }
  172. }
  173. return Result;
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TCustomUnixDriveView::UpdatePath(TTreeNode * Node, bool Force,
  177. bool CanLoad)
  178. {
  179. #ifndef DESIGN_ONLY
  180. TNodeData * Data = NodeData(Node);
  181. UnicodeString Path = Data->Directory;
  182. TDateTime Timestamp = (Data->FileList != NULL) ? Data->FileList->Timestamp : TDateTime();
  183. TRemoteFileList * FileList = FTerminal->DirectoryFileList(Path, Timestamp, CanLoad);
  184. if ((FileList != NULL) ||
  185. ((Data->FileList != NULL) && Force))
  186. {
  187. TStringList * ChildrenDirs = new TStringList();
  188. TRemoteFileList * OldFileList = NULL;
  189. try
  190. {
  191. if (FileList != NULL)
  192. {
  193. OldFileList = Data->FileList;
  194. Data->FileList = FileList;
  195. }
  196. ChildrenDirs->Duplicates = Types::dupAccept;
  197. ChildrenDirs->CaseSensitive = true;
  198. bool FirstChild = true;
  199. TTreeNode * ChildNode = Node->getFirstChild();
  200. while (ChildNode != NULL)
  201. {
  202. TNodeData * ChildData = NodeData(ChildNode);
  203. ChildData->File = NULL;
  204. ChildrenDirs->AddObject(UnixExtractFileName(ChildData->Directory),
  205. ChildNode);
  206. ChildNode = Node->GetNextChild(ChildNode);
  207. FirstChild = false;
  208. }
  209. // sort only after adding all items.
  210. // should be faster than keeping the list sorted since beginning
  211. ChildrenDirs->Sorted = true;
  212. for (int i = 0; i < Data->FileList->Count; i++)
  213. {
  214. TRemoteFile * File = Data->FileList->Files[i];
  215. if (File->IsDirectory && IsRealFile(File->FileName) &&
  216. (ShowHiddenDirs || !File->IsHidden) &&
  217. (ShowInaccesibleDirectories || !File->IsInaccesibleDirectory))
  218. {
  219. int ChildIndex = ChildrenDirs->IndexOf(File->FileName);
  220. if (ChildIndex >= 0)
  221. {
  222. TTreeNode * ChildNode =
  223. dynamic_cast<TTreeNode *>(ChildrenDirs->Objects[ChildIndex]);
  224. TNodeData * ChildData = NodeData(ChildNode);
  225. ChildData->File = File;
  226. UpdatePath(ChildNode, Force);
  227. }
  228. else
  229. {
  230. UnicodeString ChildPath = UnixIncludeTrailingBackslash(Path) + File->FileName;
  231. DebugAssert(!IsUnixRootPath(ChildPath));
  232. LoadPathEasy(Node, ChildPath, File);
  233. if (FirstChild)
  234. {
  235. LoadNodeState(Node, Path);
  236. FirstChild = false;
  237. }
  238. }
  239. }
  240. }
  241. for (int i = 0; i < ChildrenDirs->Count; i++)
  242. {
  243. TTreeNode * ChildNode = dynamic_cast<TTreeNode *>(ChildrenDirs->Objects[i]);
  244. TNodeData * ChildData = NodeData(ChildNode);
  245. if (ChildData->File == NULL)
  246. {
  247. NodeTryDelete(ChildNode, true);
  248. }
  249. }
  250. Node->AlphaSort(false);
  251. }
  252. __finally
  253. {
  254. delete ChildrenDirs;
  255. // Release files only now, once they are no longer referenced in the tree
  256. delete OldFileList; // if not NULL
  257. }
  258. }
  259. else if (Force)
  260. {
  261. TTreeNode * ChildNode = Node->GetLastChild();
  262. while (ChildNode != NULL)
  263. {
  264. TTreeNode * PrevChildNode = Node->GetPrevChild(ChildNode);
  265. TRemoteFile * File = NodeFile(ChildNode);
  266. if (((ShowHiddenDirs || !NodeIsHidden(ChildNode)) &&
  267. (ShowInaccesibleDirectories || (File == NULL) || !File->IsInaccesibleDirectory)) ||
  268. !NodeTryDelete(ChildNode, true))
  269. {
  270. UpdatePath(ChildNode, true);
  271. }
  272. ChildNode = PrevChildNode;
  273. }
  274. }
  275. #else
  276. DebugUsedParam(Node);
  277. DebugUsedParam(Force);
  278. DebugUsedParam(CanLoad);
  279. #endif
  280. }
  281. //---------------------------------------------------------------------------
  282. TTreeNode * __fastcall TCustomUnixDriveView::LoadPathEasy(TTreeNode * Parent,
  283. UnicodeString Path, TRemoteFile * File)
  284. {
  285. #ifndef DESIGN_ONLY
  286. DebugAssert(Path == UnixExcludeTrailingBackslash(Path));
  287. UnicodeString DirName;
  288. if (IsUnixRootPath(Path))
  289. {
  290. DirName = RootName;
  291. }
  292. else
  293. {
  294. DirName = UnixExtractFileName(Path);
  295. }
  296. TTreeNode * Node = Items->AddChild(Parent, DirName);
  297. TNodeData * Data = new TNodeData();
  298. Node->Data = Data;
  299. Data->Directory = Path;
  300. Data->File = File;
  301. Data->FileList = NULL;
  302. UpdatePath(Node, true);
  303. return Node;
  304. #else
  305. DebugUsedParam(Parent);
  306. DebugUsedParam(File);
  307. return NULL;
  308. #endif
  309. }
  310. //---------------------------------------------------------------------------
  311. TTreeNode * __fastcall TCustomUnixDriveView::LoadPath(UnicodeString Path)
  312. {
  313. #ifndef DESIGN_ONLY
  314. if (Path.IsEmpty())
  315. {
  316. Path = ROOTDIRECTORY;
  317. }
  318. TTreeNode * Node = FindNodeToPath(Path);
  319. if (Node == NULL)
  320. {
  321. Path = UnixExcludeTrailingBackslash(Path);
  322. TTreeNode * Parent = NULL;
  323. TRemoteFile * File = NULL;
  324. UnicodeString ParentPath;
  325. if (!IsUnixRootPath(Path))
  326. {
  327. ParentPath = UnixExtractFileDir(Path);
  328. Parent = LoadPath(ParentPath);
  329. }
  330. Node = FindNodeToPath(Path);
  331. if (Node == NULL)
  332. {
  333. // node still does not exist, this should happen only when
  334. // if the parent directory is not in cache
  335. if (Parent != NULL)
  336. {
  337. TRemoteFileList * ParentFileList = NodeFileList(Parent);
  338. if (ParentFileList != NULL)
  339. {
  340. File = ParentFileList->FindFile(UnixExtractFileName(Path));
  341. }
  342. }
  343. Node = LoadPathEasy(Parent, Path, File);
  344. if (Parent != NULL)
  345. {
  346. LoadNodeState(Parent, ParentPath);
  347. Parent->AlphaSort(false);
  348. }
  349. }
  350. else
  351. {
  352. UpdatePath(Node, false);
  353. }
  354. }
  355. else
  356. {
  357. UpdatePath(Node, false);
  358. }
  359. return Node;
  360. #else
  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. Result = NULL;
  811. #endif
  812. return Result;
  813. }
  814. //---------------------------------------------------------------------------
  815. TTreeNode * __fastcall TCustomUnixDriveView::FindPathNode(UnicodeString Path)
  816. {
  817. TTreeNode * Result = NULL;
  818. #ifndef DESIGN_ONLY
  819. if (Items->GetFirstNode() != NULL)
  820. {
  821. do
  822. {
  823. Result = FindNodeToPath(Path);
  824. if (Result == NULL)
  825. {
  826. DebugAssert(!IsUnixRootPath(Path));
  827. Path = UnixExtractFileDir(UnixExcludeTrailingBackslash(Path));
  828. }
  829. }
  830. while (Result == NULL);
  831. }
  832. #endif
  833. return Result;
  834. }
  835. //---------------------------------------------------------------------------
  836. void __fastcall TCustomUnixDriveView::ValidateDirectoryEx(TTreeNode * /*Node*/,
  837. TRecursiveScan /*Recurse*/, bool /*NewDirs*/)
  838. {
  839. // nothing
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TCustomUnixDriveView::RebuildTree()
  843. {
  844. TTreeNode * First = Items->GetFirstNode();
  845. if (First != NULL)
  846. {
  847. UpdatePath(First, true);
  848. }
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomUnixDriveView::SetShowInaccesibleDirectories(bool value)
  852. {
  853. if (FShowInaccesibleDirectories != value)
  854. {
  855. FShowInaccesibleDirectories = value;
  856. RebuildTree();
  857. }
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomUnixDriveView::CMShowingChanged(TMessage & Message)
  861. {
  862. TCustomDriveView::Dispatch(&Message);
  863. if (Showing && (Terminal != NULL))
  864. {
  865. LoadDirectory();
  866. }
  867. }
  868. //---------------------------------------------------------------------------
  869. void __fastcall TCustomUnixDriveView::DisplayContextMenu(TTreeNode * /*Node*/,
  870. const TPoint & /*ScreenPos*/)
  871. {
  872. // TODO
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall TCustomUnixDriveView::DisplayPropertiesMenu(TTreeNode * /*Node*/)
  876. {
  877. // TODO
  878. }
  879. //---------------------------------------------------------------------------
  880. void TCustomUnixDriveView::LoadNodeState(TTreeNode * Node, const UnicodeString & Path)
  881. {
  882. if ((FDirView != NULL) && (FDirView->FAnnouncedDriveViewState != NULL))
  883. {
  884. TStrings * AnnouncedDriveViewState = DebugNotNull(dynamic_cast<TStrings *>(FDirView->FAnnouncedDriveViewState));
  885. if (AnnouncedDriveViewState->IndexOf(Path) >= 0)
  886. {
  887. Node->Expand(false);
  888. }
  889. }
  890. }
  891. //---------------------------------------------------------------------------
  892. TObject * TCustomUnixDriveView::SaveState()
  893. {
  894. #ifndef DESIGN_ONLY
  895. std::unique_ptr<TStrings> ExpandedNodes(CreateSortedStringList());
  896. TTreeNode * Node = Items->GetFirstNode();
  897. while (Node != NULL)
  898. {
  899. // Node->HasChildren would be false in extreme cases only (like while closing the window)
  900. if (Node->Expanded && Node->HasChildren)
  901. {
  902. ExpandedNodes->Add(NodePathName(Node));
  903. }
  904. Node = Node->GetNext();
  905. }
  906. return ExpandedNodes.release();
  907. #else
  908. return NULL;
  909. #endif
  910. }