LocationProfiles.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <CoreMain.h>
  5. #include <Configuration.h>
  6. #include <RemoteFiles.h>
  7. #include <VCLCommon.h>
  8. #include <TextsWin.h>
  9. #include <HelpWin.h>
  10. #include <Common.h>
  11. #include "LocationProfiles.h"
  12. #include "WinConfiguration.h"
  13. #include "Custom.h"
  14. #include <Math.hpp>
  15. #include <GUITools.h>
  16. //---------------------------------------------------------------------
  17. #pragma link "HistoryComboBox"
  18. #pragma link "PngImageList"
  19. #pragma resource "*.dfm"
  20. //---------------------------------------------------------------------
  21. bool __fastcall LocationProfilesDialog(TOpenDirectoryMode Mode,
  22. TOperationSide Side, UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  23. TStrings * LocalDirectories, TStrings * RemoteDirectories, TTerminal * Terminal)
  24. {
  25. bool Result;
  26. TLocationProfilesDialog * Dialog = new TLocationProfilesDialog(Application);
  27. try
  28. {
  29. Dialog->LocalDirectory = LocalDirectory;
  30. Dialog->RemoteDirectory = RemoteDirectory;
  31. Dialog->OperationSide = Side;
  32. Dialog->Terminal = Terminal;
  33. Dialog->RemoteDirectories = RemoteDirectories;
  34. Dialog->LocalDirectories = LocalDirectories;
  35. Dialog->Mode = Mode;
  36. Result = Dialog->Execute();
  37. if (Result)
  38. {
  39. LocalDirectory = Dialog->LocalDirectory;
  40. RemoteDirectory = Dialog->RemoteDirectory;
  41. }
  42. }
  43. __finally
  44. {
  45. delete Dialog;
  46. }
  47. return Result;
  48. }
  49. //---------------------------------------------------------------------------
  50. //---------------------------------------------------------------------------
  51. void __fastcall BookmarkNameValidateName(const UnicodeString Name)
  52. {
  53. if (Name.IsEmpty() || IsNumber(Name))
  54. {
  55. throw Exception(FMTLOAD(BOOKMARK_INVALID_NAME, (Name)));
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall BookmarkFolderValidateName(const UnicodeString Name,
  60. bool AllowEmpty)
  61. {
  62. if ((!AllowEmpty && Name.IsEmpty()) || Name.Pos(L"\\"))
  63. {
  64. throw Exception(FMTLOAD(BOOKMARK_FOLDER_INVALID_NAME, (Name)));
  65. }
  66. }
  67. //---------------------------------------------------------------------------
  68. //---------------------------------------------------------------------------
  69. class TBookmarkNameDialog : public TCustomDialog
  70. {
  71. public:
  72. __fastcall TBookmarkNameDialog(TStrings * PeerBookmarks, bool AllowShared);
  73. bool __fastcall Execute(UnicodeString & Name, bool & Shared);
  74. protected:
  75. virtual void __fastcall DoValidate();
  76. private:
  77. TComboBox * NameCombo;
  78. TCheckBox * SharedCheck;
  79. };
  80. //---------------------------------------------------------------------
  81. __fastcall TBookmarkNameDialog::TBookmarkNameDialog(TStrings * PeerBookmarks,
  82. bool AllowShared) :
  83. TCustomDialog(HELP_LOCATION_PROFILE_ADD)
  84. {
  85. Caption = LoadStr(ADD_BOOKMARK_CAPTION);
  86. NameCombo = new TComboBox(this);
  87. NameCombo->AutoComplete = false;
  88. NameCombo->DropDownCount = Max(NameCombo->DropDownCount, 16);
  89. AddComboBox(NameCombo, CreateLabel(LoadStr(ADD_BOOKMARK_PROMPT)));
  90. NameCombo->Items = PeerBookmarks;
  91. if (AllowShared)
  92. {
  93. SharedCheck = new TCheckBox(this);
  94. SharedCheck->Caption = LoadStr(ADD_BOOKMARK_SHARED);
  95. AddButtonControl(SharedCheck);
  96. }
  97. else
  98. {
  99. SharedCheck = NULL;
  100. }
  101. }
  102. //---------------------------------------------------------------------
  103. void __fastcall TBookmarkNameDialog::DoValidate()
  104. {
  105. if (NameCombo->Text.IsEmpty() || IsNumber(NameCombo->Text))
  106. {
  107. throw Exception(FMTLOAD(BOOKMARK_INVALID_NAME, (NameCombo->Text)));
  108. }
  109. TCustomDialog::DoValidate();
  110. }
  111. //---------------------------------------------------------------------
  112. bool __fastcall TBookmarkNameDialog::Execute(UnicodeString & Name, bool & Shared)
  113. {
  114. NameCombo->Text = Name;
  115. if (SharedCheck != NULL)
  116. {
  117. SharedCheck->Checked = Shared;
  118. }
  119. bool Result = TCustomDialog::Execute();
  120. if (Result)
  121. {
  122. Name = NameCombo->Text;
  123. if (SharedCheck != NULL)
  124. {
  125. Shared = SharedCheck->Checked;
  126. }
  127. }
  128. return Result;
  129. }
  130. //---------------------------------------------------------------------------
  131. //---------------------------------------------------------------------------
  132. class TBookmarkFolderDialog : public TCustomDialog
  133. {
  134. public:
  135. __fastcall TBookmarkFolderDialog(TStrings * Folders);
  136. bool __fastcall Execute(UnicodeString & Name);
  137. protected:
  138. virtual void __fastcall DoValidate();
  139. private:
  140. TComboBox * NameCombo;
  141. };
  142. //---------------------------------------------------------------------
  143. __fastcall TBookmarkFolderDialog::TBookmarkFolderDialog(TStrings * Folders) :
  144. TCustomDialog(HELP_LOCATION_PROFILE_MOVE)
  145. {
  146. Caption = LoadStr(MOVE_BOOKMARK_CAPTION);
  147. NameCombo = new TComboBox(this);
  148. NameCombo->AutoComplete = false;
  149. AddComboBox(NameCombo, CreateLabel(LoadStr(MOVE_BOOKMARK_PROMPT)));
  150. NameCombo->Items = Folders;
  151. }
  152. //---------------------------------------------------------------------
  153. void __fastcall TBookmarkFolderDialog::DoValidate()
  154. {
  155. BookmarkFolderValidateName(NameCombo->Text, true);
  156. TCustomDialog::DoValidate();
  157. }
  158. //---------------------------------------------------------------------
  159. bool __fastcall TBookmarkFolderDialog::Execute(UnicodeString & Name)
  160. {
  161. NameCombo->Text = Name;
  162. bool Result = TCustomDialog::Execute();
  163. if (Result)
  164. {
  165. Name = NameCombo->Text;
  166. }
  167. return Result;
  168. }
  169. //---------------------------------------------------------------------
  170. //---------------------------------------------------------------------
  171. __fastcall TLocationProfilesDialog::TLocationProfilesDialog(TComponent * AOwner):
  172. TForm(AOwner)
  173. {
  174. FOperationSide = osLocal;
  175. FBookmarkDragSource = NULL;
  176. FTerminal = NULL;
  177. FSessionBookmarkList = new TBookmarkList();
  178. FSharedBookmarkList = new TBookmarkList();
  179. FChanging = false;
  180. FSessionFolders = CreateSortedStringList();
  181. FSharedFolders = CreateSortedStringList();
  182. FSessionScrollOnDragOver = new TTreeViewScrollOnDragOver(SessionProfilesView, true);
  183. FSharedScrollOnDragOver = new TTreeViewScrollOnDragOver(SharedProfilesView, true);
  184. UseSystemSettings(this);
  185. SelectScaledImageList(BookmarkImageList);
  186. LoadDialogImage(Image, L"Open folder");
  187. }
  188. //---------------------------------------------------------------------
  189. __fastcall TLocationProfilesDialog::~TLocationProfilesDialog()
  190. {
  191. SAFE_DESTROY(FSharedScrollOnDragOver);
  192. SAFE_DESTROY(FSessionScrollOnDragOver);
  193. SAFE_DESTROY(FSharedBookmarkList);
  194. SAFE_DESTROY(FSessionBookmarkList);
  195. SAFE_DESTROY(FSharedFolders);
  196. SAFE_DESTROY(FSessionFolders);
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TLocationProfilesDialog::SetLocalDirectory(UnicodeString value)
  200. {
  201. if (LocalDirectory != value)
  202. {
  203. LocalDirectoryEdit->Text = value;
  204. FindProfile();
  205. UpdateControls();
  206. }
  207. }
  208. //---------------------------------------------------------------------------
  209. UnicodeString __fastcall TLocationProfilesDialog::GetLocalDirectory()
  210. {
  211. return ExcludeTrailingBackslash(LocalDirectoryEdit->Text);
  212. }
  213. //---------------------------------------------------------------------------
  214. void __fastcall TLocationProfilesDialog::SetRemoteDirectory(UnicodeString value)
  215. {
  216. if (RemoteDirectory != value)
  217. {
  218. RemoteDirectoryEdit->Text = value;
  219. FindProfile();
  220. UpdateControls();
  221. }
  222. }
  223. //---------------------------------------------------------------------------
  224. UnicodeString __fastcall TLocationProfilesDialog::GetRemoteDirectory()
  225. {
  226. return UnixExcludeTrailingBackslash(RemoteDirectoryEdit->Text);
  227. }
  228. //---------------------------------------------------------------------------
  229. void __fastcall TLocationProfilesDialog::SetRemoteDirectories(TStrings * value)
  230. {
  231. RemoteDirectoryEdit->Items = value;
  232. }
  233. //---------------------------------------------------------------------------
  234. TStrings * __fastcall TLocationProfilesDialog::GetRemoteDirectories()
  235. {
  236. return RemoteDirectoryEdit->Items;
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TLocationProfilesDialog::SetLocalDirectories(TStrings * value)
  240. {
  241. LocalDirectoryEdit->Items = value;
  242. }
  243. //---------------------------------------------------------------------------
  244. TStrings * __fastcall TLocationProfilesDialog::GetLocalDirectories()
  245. {
  246. return LocalDirectoryEdit->Items;
  247. }
  248. //---------------------------------------------------------------------------
  249. bool __fastcall TLocationProfilesDialog::ProfileMatch(TTreeNode * Node)
  250. {
  251. bool Result = false;
  252. if (Node->Data)
  253. {
  254. TBookmark * Bookmark = (TBookmark *)Node->Data;
  255. Result =
  256. (Bookmark->Local == LocalDirectory) &&
  257. (Bookmark->Remote == RemoteDirectory);
  258. }
  259. return Result;
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TLocationProfilesDialog::FindProfile(TTreeView * ProfilesView)
  263. {
  264. if ((ProfilesView->Selected == NULL) ||
  265. !ProfileMatch(ProfilesView->Selected))
  266. {
  267. TTreeNode * Match = NULL;
  268. for (int Index = 0; Index < ProfilesView->Items->Count; Index++)
  269. {
  270. TTreeNode * Node = ProfilesView->Items->Item[Index];
  271. if (ProfileMatch(Node))
  272. {
  273. Match = Node;
  274. break;
  275. }
  276. }
  277. if (Match)
  278. {
  279. ProfilesView->Selected = Match;
  280. Match->MakeVisible();
  281. }
  282. else
  283. {
  284. ProfilesView->Selected = NULL;
  285. }
  286. }
  287. }
  288. //---------------------------------------------------------------------------
  289. void __fastcall TLocationProfilesDialog::FindProfile()
  290. {
  291. FindProfile(SessionProfilesView);
  292. FindProfile(SharedProfilesView);
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TLocationProfilesDialog::ControlChange(TObject * /*Sender*/)
  296. {
  297. UpdateControls();
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TLocationProfilesDialog::UpdateProfilesControls(
  301. TTreeView * ProfilesView,
  302. TButton * AddBookmarkButton, TButton * RemoveBookmarkButton,
  303. TButton * RenameBookmarkButton, TButton * BookmarkMoveToButton,
  304. TButton * ShortCutBookmarkButton,
  305. TButton * UpBookmarkButton, TButton * DownBookmarkButton)
  306. {
  307. EnableControl(AddBookmarkButton,
  308. !LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  309. EnableControl(RemoveBookmarkButton, ProfilesView->Selected);
  310. EnableControl(RenameBookmarkButton, ProfilesView->Selected);
  311. EnableControl(BookmarkMoveToButton, ProfilesView->Selected && ProfilesView->Selected->Data);
  312. if (ShortCutBookmarkButton != NULL)
  313. {
  314. EnableControl(ShortCutBookmarkButton, ProfilesView->Selected && ProfilesView->Selected->Data);
  315. }
  316. EnableControl(UpBookmarkButton, ProfilesView->Selected &&
  317. ProfilesView->Selected->Data && ProfilesView->Selected->getPrevSibling() &&
  318. ProfilesView->Selected->getPrevSibling()->Data);
  319. EnableControl(DownBookmarkButton, ProfilesView->Selected &&
  320. ProfilesView->Selected->Data && ProfilesView->Selected->getNextSibling() &&
  321. ProfilesView->Selected->getNextSibling()->Data);
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TLocationProfilesDialog::UpdateControls()
  325. {
  326. EnableControl(OKBtn, !LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  327. UpdateProfilesControls(SessionProfilesView,
  328. AddSessionBookmarkButton, RemoveSessionBookmarkButton,
  329. RenameSessionBookmarkButton, SessionBookmarkMoveToButton, NULL,
  330. UpSessionBookmarkButton, DownSessionBookmarkButton);
  331. UpdateProfilesControls(SharedProfilesView,
  332. AddSharedBookmarkButton, RemoveSharedBookmarkButton,
  333. RenameSharedBookmarkButton, SharedBookmarkMoveToButton,
  334. ShortCutSharedBookmarkButton,
  335. UpSharedBookmarkButton, DownSharedBookmarkButton);
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TLocationProfilesDialog::LoadBookmarks(
  339. TTreeView * ProfilesView, TStringList * Folders, TBookmarkList * BookmarkList,
  340. TBookmarkList * Source)
  341. {
  342. if (Source != NULL)
  343. {
  344. BookmarkList->Assign(Source);
  345. }
  346. else
  347. {
  348. BookmarkList->Clear();
  349. }
  350. Configuration->Usage->SetMax(L"MaxBookmarks", BookmarkList->Count);
  351. DebugAssert(BookmarkList != NULL);
  352. Folders->Clear();
  353. for (int Index = 0; Index < BookmarkList->Count; Index++)
  354. {
  355. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  356. if (!Bookmark->Node.IsEmpty())
  357. {
  358. Folders->Add(Bookmark->Node);
  359. }
  360. }
  361. // WORKAROUND
  362. // TTreeNodes::Clear is noop, when tree does not have a handle yet.
  363. // (what happens here for a tree view on an inactive page)
  364. ProfilesView->HandleNeeded();
  365. ProfilesView->Items->Clear();
  366. for (int Index = 0; Index < Folders->Count; Index++)
  367. {
  368. Folders->Objects[Index] = ProfilesView->Items->Add(NULL, Folders->Strings[Index]);
  369. }
  370. for (int Index = 0; Index < BookmarkList->Count; Index++)
  371. {
  372. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  373. TTreeNode * Parent = NULL;
  374. if (!Bookmark->Node.IsEmpty())
  375. {
  376. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  377. Parent = dynamic_cast<TTreeNode *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  378. }
  379. ProfilesView->Items->AddChildObject(Parent, BookmarkText(Bookmark), Bookmark);
  380. if ((Parent != NULL) && (Parent->Count == 1))
  381. {
  382. // only now, when folder node has its first child, we can eventually expand it
  383. Parent->Expanded = BookmarkList->NodeOpened[Parent->Text];
  384. }
  385. }
  386. }
  387. //---------------------------------------------------------------------------
  388. bool __fastcall TLocationProfilesDialog::Execute()
  389. {
  390. bool Result;
  391. PageControl->ActivePage = GetProfilesSheet();
  392. FBookmarkSelected = false;
  393. Result = (ShowModal() == DefaultResult(this));
  394. if (Terminal)
  395. {
  396. WinConfiguration->Bookmarks[FSessionKey] = FSessionBookmarkList;
  397. WinConfiguration->SharedBookmarks = FSharedBookmarkList;
  398. WinConfiguration->UseSharedBookmarks = (PageControl->ActivePage == SharedProfilesSheet);
  399. }
  400. if (Result)
  401. {
  402. if (FBookmarkSelected)
  403. {
  404. Configuration->Usage->Inc(L"OpenedBookmark");
  405. }
  406. else
  407. {
  408. Configuration->Usage->Inc(L"OpenedPath");
  409. }
  410. }
  411. return Result;
  412. }
  413. //---------------------------------------------------------------------------
  414. TTabSheet * TLocationProfilesDialog::GetProfilesSheet()
  415. {
  416. return WinConfiguration->UseSharedBookmarks ? SharedProfilesSheet : SessionProfilesSheet;
  417. }
  418. //---------------------------------------------------------------------------
  419. template<class T>
  420. typename T * GetProfilesObject(TObject * Sender, T * SessionObject, T * SharedObject)
  421. {
  422. TControl * Control = dynamic_cast<TControl *>(Sender);
  423. DebugAssert(Control != NULL);
  424. switch (abs(Control->Tag))
  425. {
  426. case 1: return SessionObject;
  427. case 2: return SharedObject;
  428. default: DebugFail(); return NULL;
  429. }
  430. }
  431. //---------------------------------------------------------------------------
  432. TBookmarkList * TLocationProfilesDialog::GetBookmarkList(TObject * Sender)
  433. {
  434. return GetProfilesObject(Sender, FSessionBookmarkList, FSharedBookmarkList);
  435. }
  436. //---------------------------------------------------------------------------
  437. TStringList * TLocationProfilesDialog::GetFolders(TObject * Sender)
  438. {
  439. #ifdef _DEBUG
  440. DebugAssert(FSessionProfilesViewHandle == SessionProfilesView->Handle);
  441. DebugAssert(FSharedProfilesViewHandle == SharedProfilesView->Handle);
  442. #endif
  443. return GetProfilesObject(Sender, FSessionFolders, FSharedFolders);
  444. }
  445. //---------------------------------------------------------------------------
  446. TTreeView * TLocationProfilesDialog::GetProfilesView(TObject * Sender)
  447. {
  448. return GetProfilesObject(Sender, SessionProfilesView, SharedProfilesView);
  449. }
  450. //---------------------------------------------------------------------------
  451. TTreeViewScrollOnDragOver * TLocationProfilesDialog::GetScrollOnDragOver(TObject * Sender)
  452. {
  453. return GetProfilesObject(Sender, FSessionScrollOnDragOver, FSharedScrollOnDragOver);
  454. }
  455. //---------------------------------------------------------------------------
  456. bool __fastcall TLocationProfilesDialog::AddAsBookmark(TObject * Sender, bool Initial)
  457. {
  458. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  459. TTreeView * ProfilesView = GetProfilesView(Sender);
  460. DebugAssert(!LocalDirectory.IsEmpty() || !RemoteDirectory.IsEmpty());
  461. bool Result;
  462. UnicodeString BookmarkName;
  463. if ((OperationSide == osLocal && !LocalDirectory.IsEmpty()) ||
  464. RemoteDirectory.IsEmpty())
  465. {
  466. BookmarkName = LocalDirectory;
  467. }
  468. else
  469. {
  470. BookmarkName = RemoteDirectory;
  471. }
  472. TTreeNode * Selected = ProfilesView->Selected;
  473. TBookmark * SelectedBookmark = NULL;
  474. UnicodeString SelectedNode;
  475. if (Selected != NULL)
  476. {
  477. DebugAssert(!Initial);
  478. SelectedBookmark = (TBookmark *)Selected->Data;
  479. if (SelectedBookmark != NULL)
  480. {
  481. SelectedNode = SelectedBookmark->Node;
  482. }
  483. else
  484. {
  485. SelectedNode = Selected->Text;
  486. }
  487. }
  488. TStrings * PeerBookmarks = new TStringList();
  489. try
  490. {
  491. for (int Index = 0; Index < BookmarkList->Count; Index++)
  492. {
  493. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  494. if (Bookmark->Node == SelectedNode)
  495. {
  496. PeerBookmarks->Add(Bookmark->Name);
  497. }
  498. }
  499. TBookmarkNameDialog * Dialog = new TBookmarkNameDialog(PeerBookmarks, Initial);
  500. try
  501. {
  502. bool Shared = WinConfiguration->UseSharedBookmarks;
  503. Result = Dialog->Execute(BookmarkName, Shared);
  504. if (Result)
  505. {
  506. if (Initial)
  507. {
  508. WinConfiguration->UseSharedBookmarks = Shared;
  509. PageControl->ActivePage = GetProfilesSheet();
  510. BookmarkList = GetBookmarkList(PageControl->ActivePage);
  511. ProfilesView = GetProfilesView(PageControl->ActivePage);
  512. }
  513. TBookmark * Bookmark = BookmarkList->FindByName(SelectedNode, BookmarkName);
  514. if (Bookmark != NULL)
  515. {
  516. Bookmark->Local = LocalDirectory;
  517. Bookmark->Remote = RemoteDirectory;
  518. for (int Index = 0; Index < ProfilesView->Items->Count; Index++)
  519. {
  520. TTreeNode * Node = ProfilesView->Items->Item[Index];
  521. if (Node->Data == Bookmark)
  522. {
  523. Selected = Node;
  524. break;
  525. }
  526. }
  527. }
  528. else
  529. {
  530. Bookmark = new TBookmark();
  531. Bookmark->Name = BookmarkName;
  532. Bookmark->Local = LocalDirectory;
  533. Bookmark->Remote = RemoteDirectory;
  534. if (SelectedBookmark != NULL)
  535. {
  536. Bookmark->Node = SelectedBookmark->Node;
  537. BookmarkList->InsertBefore(SelectedBookmark, Bookmark);
  538. Selected = ProfilesView->Items->InsertObject(Selected, BookmarkText(Bookmark), Bookmark);
  539. }
  540. else if ((Selected != NULL) && (SelectedBookmark == NULL))
  541. {
  542. // must be a folder
  543. DebugAssert(!Selected->Parent); // more than one level of folders is not supported
  544. Bookmark->Node = Selected->Text;
  545. BookmarkList->Add(Bookmark);
  546. Selected = ProfilesView->Items->AddChildObject(Selected, BookmarkText(Bookmark), Bookmark);
  547. }
  548. else
  549. {
  550. BookmarkList->Add(Bookmark);
  551. Selected = ProfilesView->Items->AddObject(NULL, BookmarkText(Bookmark), Bookmark);
  552. }
  553. }
  554. ProfilesView->Selected = Selected;
  555. }
  556. }
  557. __finally
  558. {
  559. delete Dialog;
  560. }
  561. }
  562. __finally
  563. {
  564. delete PeerBookmarks;
  565. }
  566. UpdateControls();
  567. return Result;
  568. }
  569. //---------------------------------------------------------------------------
  570. void __fastcall TLocationProfilesDialog::AddBookmarkButtonClick(TObject * Sender)
  571. {
  572. AddAsBookmark(Sender, false);
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall TLocationProfilesDialog::RemoveBookmark(TObject * Sender)
  576. {
  577. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  578. TTreeView * ProfilesView = GetProfilesView(Sender);
  579. TStringList * Folders = GetFolders(Sender);
  580. DebugAssert(ProfilesView->Selected);
  581. TTreeNode * Node = ProfilesView->Selected;
  582. if (Node->Data)
  583. {
  584. BookmarkList->Delete((TBookmark *)Node->Data);
  585. TTreeNode * ParentNode = Node->Parent;
  586. Node->Delete();
  587. if (ParentNode && !ParentNode->Count)
  588. {
  589. DebugAssert(Folders->IndexOfObject(ParentNode) >= 0);
  590. Folders->Delete(Folders->IndexOfObject(ParentNode));
  591. ParentNode->Delete();
  592. }
  593. }
  594. else
  595. {
  596. UnicodeString Message = MainInstructions(LoadStr(DELETE_BOOKMARK_FOLDER));
  597. if (MessageDialog(Message, qtConfirmation,
  598. qaYes | qaNo, HELP_LOCATION_PROFILE_DELETE) == qaYes)
  599. {
  600. DebugAssert(Node->Count);
  601. for (int i = 0; i < Node->Count; i++)
  602. {
  603. BookmarkList->Delete((TBookmark *)Node->Item[i]->Data);
  604. }
  605. DebugAssert(Folders->IndexOfObject(Node) >= 0);
  606. Folders->Delete(Folders->IndexOfObject(Node));
  607. Node->Delete();
  608. }
  609. }
  610. UpdateControls();
  611. }
  612. //---------------------------------------------------------------------------
  613. void __fastcall TLocationProfilesDialog::RemoveBookmarkButtonClick(TObject * Sender)
  614. {
  615. RemoveBookmark(Sender);
  616. }
  617. //---------------------------------------------------------------------------
  618. void __fastcall TLocationProfilesDialog::BookmarkMove(TObject * Sender,
  619. TTreeNode * Source, TTreeNode * Dest)
  620. {
  621. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  622. TTreeView * ProfilesView = GetProfilesView(Sender);
  623. TStringList * Folders = GetFolders(Sender);
  624. DebugAssert(Source && Source->Data);
  625. TBookmark * Bookmark = (TBookmark *)Source->Data;
  626. TTreeNode * PrevFolderNode = Source->Parent;
  627. if (!Dest || !Dest->Data)
  628. {
  629. Bookmark->Node = Dest ? Dest->Text : UnicodeString();
  630. BookmarkList->MoveTo(BookmarkList->Bookmarks[BookmarkList->Count - 1],
  631. Bookmark, false);
  632. ProfilesView->Selected->MoveTo(Dest, naAddChild);
  633. }
  634. else
  635. {
  636. TBookmark * DestBookmark = (TBookmark *)Dest->Data;
  637. Bookmark->Node = DestBookmark->Node;
  638. BookmarkList->MoveTo(DestBookmark, Bookmark,
  639. Source->AbsoluteIndex > Dest->AbsoluteIndex);
  640. if (Source->AbsoluteIndex > Dest->AbsoluteIndex)
  641. {
  642. Source->MoveTo(Dest, naInsert);
  643. }
  644. else if (Dest->getNextSibling() != NULL)
  645. {
  646. Source->MoveTo(Dest->getNextSibling(), naInsert);
  647. }
  648. else
  649. {
  650. Source->MoveTo(Dest, naAdd);
  651. }
  652. }
  653. if (PrevFolderNode && !PrevFolderNode->Count)
  654. {
  655. DebugAssert(Folders->IndexOfObject(PrevFolderNode) >= 0);
  656. Folders->Delete(Folders->IndexOfObject(PrevFolderNode));
  657. PrevFolderNode->Delete();
  658. }
  659. Source->MakeVisible();
  660. UpdateControls();
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TLocationProfilesDialog::BookmarkButtonClick(TObject * Sender)
  664. {
  665. TControl * Control = dynamic_cast<TControl *>(Sender);
  666. TTreeNode * Node = GetProfilesView(Sender)->Selected;
  667. DebugAssert(Node);
  668. DebugAssert(Node->Data);
  669. TTreeNode * TargetNode;
  670. if (Control->Tag < 0)
  671. {
  672. TargetNode = Node->getPrevSibling();
  673. DebugAssert(TargetNode);
  674. }
  675. else
  676. {
  677. TargetNode = Node->getNextSibling();
  678. }
  679. BookmarkMove(Sender, Node, TargetNode ? TargetNode : Node->Parent);
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TLocationProfilesDialog::ProfilesViewStartDrag(
  683. TObject * Sender, TDragObject *& /*DragObject*/)
  684. {
  685. TTreeView * ProfilesView = GetProfilesView(Sender);
  686. if (!ProfilesView->Selected->Data)
  687. {
  688. Abort();
  689. }
  690. FBookmarkDragSource = ProfilesView->Selected;
  691. GetScrollOnDragOver(Sender)->StartDrag();
  692. }
  693. //---------------------------------------------------------------------------
  694. void __fastcall TLocationProfilesDialog::ProfilesViewDragOver(
  695. TObject * Sender, TObject * Source, int X, int Y,
  696. TDragState /*State*/, bool & Accept)
  697. {
  698. TTreeView * ProfilesView = GetProfilesView(Sender);
  699. if (Source == ProfilesView)
  700. {
  701. Accept = (ProfilesView->DropTarget != NULL) &&
  702. (FBookmarkDragSource != ProfilesView->DropTarget);
  703. GetScrollOnDragOver(Sender)->DragOver(TPoint(X, Y));
  704. }
  705. }
  706. //---------------------------------------------------------------------------
  707. void __fastcall TLocationProfilesDialog::ProfilesViewDragDrop(
  708. TObject * Sender, TObject * Source, int /*X*/, int /*Y*/)
  709. {
  710. TTreeView * ProfilesView = GetProfilesView(Sender);
  711. if ((Source == ProfilesView) && (ProfilesView->DropTarget != NULL) &&
  712. (FBookmarkDragSource != ProfilesView->DropTarget))
  713. {
  714. DebugAssert(FBookmarkDragSource);
  715. TTreeNode * Target = ProfilesView->DropTarget;
  716. BookmarkMove(Sender, FBookmarkDragSource, Target);
  717. FBookmarkDragSource = NULL;
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. void __fastcall TLocationProfilesDialog::ProfilesViewDblClick(TObject * Sender)
  722. {
  723. TTreeView * ProfilesView = GetProfilesView(Sender);
  724. TPoint P = ProfilesView->ScreenToClient(Mouse->CursorPos);
  725. TTreeNode * Node = ProfilesView->GetNodeAt(P.x, P.y);
  726. if (OKBtn->Enabled && Node && Node->Data && Node->Selected)
  727. {
  728. ModalResult = DefaultResult(this);
  729. }
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TLocationProfilesDialog::FormShow(TObject * /*Sender*/)
  733. {
  734. if (DebugAlwaysTrue(Terminal != NULL))
  735. {
  736. // cache session key, in case terminal is closed while the window is open
  737. FSessionKey = Terminal->SessionData->SessionKey;
  738. // WORKAROUND
  739. // Has to load this only now (not in Execute before ShowModal),
  740. // when the trees are finally (re)created,
  741. // otherwise the references in *Folders would be invalid already
  742. LoadBookmarks(SessionProfilesView, FSessionFolders, FSessionBookmarkList, WinConfiguration->Bookmarks[FSessionKey]);
  743. LoadBookmarks(SharedProfilesView, FSharedFolders, FSharedBookmarkList, WinConfiguration->SharedBookmarks);
  744. #ifdef _DEBUG
  745. FSessionProfilesViewHandle = SessionProfilesView->Handle;
  746. FSharedProfilesViewHandle = SharedProfilesView->Handle;
  747. #endif
  748. }
  749. if (Mode == odAddBookmark)
  750. {
  751. AddAsBookmark(GetProfilesSheet(), true);
  752. }
  753. InstallPathWordBreakProc(LocalDirectoryEdit);
  754. InstallPathWordBreakProc(RemoteDirectoryEdit);
  755. FindProfile();
  756. if (OperationSide == osLocal)
  757. {
  758. ActiveControl = LocalDirectoryEdit;
  759. }
  760. else
  761. {
  762. ActiveControl = RemoteDirectoryEdit;
  763. }
  764. UpdateControls();
  765. }
  766. //---------------------------------------------------------------------------
  767. void __fastcall TLocationProfilesDialog::ProfilesViewKeyDown(TObject * Sender,
  768. WORD & Key, TShiftState /*Shift*/)
  769. {
  770. TTreeView * ProfilesView = GetProfilesView(Sender);
  771. if (!ProfilesView->IsEditing())
  772. {
  773. if ((ProfilesView->Selected != NULL) && (Key == VK_DELETE))
  774. {
  775. RemoveBookmark(Sender);
  776. Key = 0;
  777. }
  778. else if ((ProfilesView->Selected != NULL) && (Key == VK_F2))
  779. {
  780. RenameBookmark(Sender);
  781. Key = 0;
  782. }
  783. }
  784. }
  785. //---------------------------------------------------------------------------
  786. void __fastcall TLocationProfilesDialog::DirectoryEditChange(TObject * /*Sender*/)
  787. {
  788. if (!FChanging)
  789. {
  790. FindProfile();
  791. UpdateControls();
  792. FBookmarkSelected = false;
  793. }
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TLocationProfilesDialog::ProfilesViewChange(
  797. TObject * /*Sender*/, TTreeNode * Node)
  798. {
  799. if (Node && Node->Data)
  800. {
  801. DebugAssert(!FChanging);
  802. FChanging = true;
  803. try
  804. {
  805. LocalDirectoryEdit->Text = ((TBookmark *)Node->Data)->Local;
  806. RemoteDirectoryEdit->Text = ((TBookmark *)Node->Data)->Remote;
  807. }
  808. __finally
  809. {
  810. FChanging = false;
  811. }
  812. // try to locate the same profile in the other set
  813. FindProfile();
  814. FBookmarkSelected = true;
  815. }
  816. UpdateControls();
  817. }
  818. //---------------------------------------------------------------------------
  819. void __fastcall TLocationProfilesDialog::BookmarkMoveToButtonClick(TObject * Sender)
  820. {
  821. TTreeView * ProfilesView = GetProfilesView(Sender);
  822. TStringList * Folders = GetFolders(Sender);
  823. DebugAssert(ProfilesView->Selected->Data);
  824. TBookmark * Bookmark = (TBookmark *)ProfilesView->Selected->Data;
  825. TBookmarkFolderDialog * Dialog = new TBookmarkFolderDialog(Folders);
  826. try
  827. {
  828. UnicodeString NodeName = Bookmark->Node;
  829. if (Dialog->Execute(NodeName) &&
  830. (NodeName != Bookmark->Node))
  831. {
  832. TTreeNode * FolderNode;
  833. int I = Folders->IndexOf(NodeName);
  834. if (NodeName.IsEmpty())
  835. {
  836. FolderNode = NULL;
  837. }
  838. else if (I >= 0)
  839. {
  840. FolderNode = dynamic_cast<TTreeNode *>(Folders->Objects[I]);
  841. DebugAssert(FolderNode);
  842. }
  843. else
  844. {
  845. I = Folders->Add(NodeName);
  846. TTreeNode * NextNode;
  847. // duplicated in RenameButtonClick()
  848. if (I < Folders->Count-1)
  849. {
  850. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  851. DebugAssert(NextNode);
  852. }
  853. else if (Folders->Count > 1)
  854. {
  855. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  856. }
  857. else
  858. {
  859. DebugAssert(ProfilesView->Items->Count);
  860. NextNode = ProfilesView->Items->Item[0];
  861. }
  862. FolderNode = ProfilesView->Items->Insert(NextNode, NodeName);
  863. DebugAssert(FolderNode);
  864. Folders->Objects[I] = FolderNode;
  865. }
  866. BookmarkMove(Sender, ProfilesView->Selected, FolderNode);
  867. }
  868. }
  869. __finally
  870. {
  871. delete Dialog;
  872. }
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall TLocationProfilesDialog::RenameBookmark(TObject * Sender)
  876. {
  877. TTreeView * ProfilesView = GetProfilesView(Sender);
  878. DebugAssert(ProfilesView->Selected != NULL);
  879. if (ProfilesView->Selected != NULL)
  880. {
  881. ProfilesView->SetFocus();
  882. ProfilesView->Selected->EditText();
  883. }
  884. }
  885. //---------------------------------------------------------------------------
  886. void __fastcall TLocationProfilesDialog::RenameBookmarkButtonClick(TObject * Sender)
  887. {
  888. RenameBookmark(Sender);
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TLocationProfilesDialog::ProfilesViewGetImageIndex(
  892. TObject * /*Sender*/, TTreeNode * Node)
  893. {
  894. Node->ImageIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TLocationProfilesDialog::ProfilesViewGetSelectedIndex(
  898. TObject * /*Sender*/, TTreeNode * Node)
  899. {
  900. Node->SelectedIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TLocationProfilesDialog::LocalDirectoryBrowseButtonClick(
  904. TObject * /*Sender*/)
  905. {
  906. UnicodeString Directory = LocalDirectoryEdit->Text;
  907. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  908. {
  909. LocalDirectoryEdit->Text = Directory;
  910. DirectoryEditChange(LocalDirectoryEdit);
  911. }
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TLocationProfilesDialog::SwitchButtonClick(TObject * /*Sender*/)
  915. {
  916. WinConfiguration->UseLocationProfiles = false;
  917. }
  918. //---------------------------------------------------------------------------
  919. void __fastcall TLocationProfilesDialog::HelpButtonClick(TObject * /*Sender*/)
  920. {
  921. FormHelp(this);
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall TLocationProfilesDialog::ProfilesViewCollapsed(
  925. TObject * Sender, TTreeNode * Node)
  926. {
  927. DebugAssert(Node != NULL);
  928. DebugAssert(Node->Data == NULL);
  929. GetBookmarkList(Sender)->NodeOpened[Node->Text] = false;
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall TLocationProfilesDialog::ProfilesViewExpanded(
  933. TObject * Sender, TTreeNode * Node)
  934. {
  935. DebugAssert(Node != NULL);
  936. DebugAssert(Node->Data == NULL);
  937. GetBookmarkList(Sender)->NodeOpened[Node->Text] = true;
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TLocationProfilesDialog::ProfilesViewEdited(
  941. TObject * Sender, TTreeNode * Node, UnicodeString & S)
  942. {
  943. TTreeView * ProfilesView = GetProfilesView(Sender);
  944. TStringList * Folders = GetFolders(Sender);
  945. if (Node->Data != NULL)
  946. {
  947. BookmarkNameValidateName(S);
  948. // raises exception in case of duplicate name??
  949. ((TBookmark *)Node->Data)->Name = S;
  950. }
  951. else
  952. {
  953. BookmarkFolderValidateName(S, false);
  954. if (S.IsEmpty())
  955. {
  956. throw Exception(FMTLOAD(BOOKMARK_FOLDER_INVALID_NAME, (S)));
  957. }
  958. if ((Folders->IndexOf(S) >= 0) && AnsiCompareText(S, Node->Text))
  959. {
  960. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK_FOLDER, (S)));
  961. }
  962. DebugAssert(Node->Count);
  963. Folders->Delete(Folders->IndexOf(Node->Text));
  964. int I = Folders->AddObject(S, Node);
  965. TTreeNode * NextNode;
  966. // duplicated in MoveToButtonClick()
  967. if (I < Folders->Count-1)
  968. {
  969. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  970. DebugAssert(NextNode);
  971. }
  972. else if (Folders->Count > 1)
  973. {
  974. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  975. }
  976. else
  977. {
  978. DebugAssert(ProfilesView->Items->Count);
  979. NextNode = ProfilesView->Items->Item[0];
  980. }
  981. if (NextNode != Node)
  982. {
  983. Node->MoveTo(NextNode, NextNode ? naInsert : naAddChild);
  984. }
  985. for (int i = 0; i < Node->Count; i++)
  986. {
  987. ((TBookmark *)Node->Item[i]->Data)->Node = S;
  988. }
  989. Node->MakeVisible();
  990. }
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TLocationProfilesDialog::ProfilesViewEditing(
  994. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & /*AllowEdit*/)
  995. {
  996. OKBtn->Default = false;
  997. CancelBtn->Cancel = false;
  998. }
  999. //---------------------------------------------------------------------------
  1000. void __fastcall TLocationProfilesDialog::UpdateActions()
  1001. {
  1002. TForm::UpdateActions();
  1003. if ((!OKBtn->Default || !CancelBtn->Cancel) &&
  1004. !GetProfilesView(PageControl->ActivePage)->IsEditing())
  1005. {
  1006. OKBtn->Default = true;
  1007. CancelBtn->Cancel = true;
  1008. }
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. void __fastcall TLocationProfilesDialog::ProfilesViewEndDrag(
  1012. TObject * Sender, TObject * /*Target*/, int /*X*/, int /*Y*/)
  1013. {
  1014. GetScrollOnDragOver(Sender)->EndDrag();
  1015. }
  1016. //---------------------------------------------------------------------------
  1017. void __fastcall TLocationProfilesDialog::ShortCutBookmarkButtonClick(
  1018. TObject * Sender)
  1019. {
  1020. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  1021. TTreeView * ProfilesView = GetProfilesView(Sender);
  1022. DebugAssert(ProfilesView->Selected != NULL);
  1023. TTreeNode * Node = ProfilesView->Selected;
  1024. DebugAssert(Node->Data != NULL);
  1025. TBookmark * Bookmark = static_cast<TBookmark *>(Node->Data);
  1026. TShortCuts ShortCuts;
  1027. WinConfiguration->CustomCommandShortCuts(ShortCuts);
  1028. BookmarkList->ShortCuts(ShortCuts);
  1029. TShortCut ShortCut = Bookmark->ShortCut;
  1030. if (DoShortCutDialog(ShortCut, ShortCuts, HelpKeyword))
  1031. {
  1032. Bookmark->ShortCut = ShortCut;
  1033. Node->Text = BookmarkText(Bookmark);
  1034. UpdateControls();
  1035. }
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. UnicodeString __fastcall TLocationProfilesDialog::BookmarkText(TBookmark * Bookmark)
  1039. {
  1040. UnicodeString Result = Bookmark->Name;
  1041. if (!Result.IsEmpty() && (Bookmark->ShortCut != 0))
  1042. {
  1043. Result = FORMAT(L"%s (%s)", (Result, ShortCutToText(Bookmark->ShortCut)));
  1044. }
  1045. return Result;
  1046. }
  1047. //---------------------------------------------------------------------------