LocationProfiles.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. BookmarkList = GetBookmarkList(GetProfilesSheet());
  510. ProfilesView = GetProfilesView(GetProfilesSheet());
  511. }
  512. TBookmark * Bookmark = BookmarkList->FindByName(SelectedNode, BookmarkName);
  513. if (Bookmark != NULL)
  514. {
  515. Bookmark->Local = LocalDirectory;
  516. Bookmark->Remote = RemoteDirectory;
  517. for (int Index = 0; Index < ProfilesView->Items->Count; Index++)
  518. {
  519. TTreeNode * Node = ProfilesView->Items->Item[Index];
  520. if (Node->Data == Bookmark)
  521. {
  522. Selected = Node;
  523. break;
  524. }
  525. }
  526. }
  527. else
  528. {
  529. Bookmark = new TBookmark();
  530. Bookmark->Name = BookmarkName;
  531. Bookmark->Local = LocalDirectory;
  532. Bookmark->Remote = RemoteDirectory;
  533. if (SelectedBookmark != NULL)
  534. {
  535. Bookmark->Node = SelectedBookmark->Node;
  536. BookmarkList->InsertBefore(SelectedBookmark, Bookmark);
  537. Selected = ProfilesView->Items->InsertObject(Selected, BookmarkText(Bookmark), Bookmark);
  538. }
  539. else if ((Selected != NULL) && (SelectedBookmark == NULL))
  540. {
  541. // must be a folder
  542. DebugAssert(!Selected->Parent); // more than one level of folders is not supported
  543. Bookmark->Node = Selected->Text;
  544. BookmarkList->Add(Bookmark);
  545. Selected = ProfilesView->Items->AddChildObject(Selected, BookmarkText(Bookmark), Bookmark);
  546. }
  547. else
  548. {
  549. BookmarkList->Add(Bookmark);
  550. Selected = ProfilesView->Items->AddObject(NULL, BookmarkText(Bookmark), Bookmark);
  551. }
  552. }
  553. ProfilesView->Selected = Selected;
  554. }
  555. }
  556. __finally
  557. {
  558. delete Dialog;
  559. }
  560. }
  561. __finally
  562. {
  563. delete PeerBookmarks;
  564. }
  565. UpdateControls();
  566. return Result;
  567. }
  568. //---------------------------------------------------------------------------
  569. void __fastcall TLocationProfilesDialog::AddBookmarkButtonClick(TObject * Sender)
  570. {
  571. AddAsBookmark(Sender, false);
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TLocationProfilesDialog::RemoveBookmark(TObject * Sender)
  575. {
  576. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  577. TTreeView * ProfilesView = GetProfilesView(Sender);
  578. TStringList * Folders = GetFolders(Sender);
  579. DebugAssert(ProfilesView->Selected);
  580. TTreeNode * Node = ProfilesView->Selected;
  581. if (Node->Data)
  582. {
  583. BookmarkList->Delete((TBookmark *)Node->Data);
  584. TTreeNode * ParentNode = Node->Parent;
  585. Node->Delete();
  586. if (ParentNode && !ParentNode->Count)
  587. {
  588. DebugAssert(Folders->IndexOfObject(ParentNode) >= 0);
  589. Folders->Delete(Folders->IndexOfObject(ParentNode));
  590. ParentNode->Delete();
  591. }
  592. }
  593. else
  594. {
  595. UnicodeString Message = MainInstructions(LoadStr(DELETE_BOOKMARK_FOLDER));
  596. if (MessageDialog(Message, qtConfirmation,
  597. qaYes | qaNo, HELP_LOCATION_PROFILE_DELETE) == qaYes)
  598. {
  599. DebugAssert(Node->Count);
  600. for (int i = 0; i < Node->Count; i++)
  601. {
  602. BookmarkList->Delete((TBookmark *)Node->Item[i]->Data);
  603. }
  604. DebugAssert(Folders->IndexOfObject(Node) >= 0);
  605. Folders->Delete(Folders->IndexOfObject(Node));
  606. Node->Delete();
  607. }
  608. }
  609. UpdateControls();
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TLocationProfilesDialog::RemoveBookmarkButtonClick(TObject * Sender)
  613. {
  614. RemoveBookmark(Sender);
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TLocationProfilesDialog::BookmarkMove(TObject * Sender,
  618. TTreeNode * Source, TTreeNode * Dest)
  619. {
  620. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  621. TTreeView * ProfilesView = GetProfilesView(Sender);
  622. TStringList * Folders = GetFolders(Sender);
  623. DebugAssert(Source && Source->Data);
  624. TBookmark * Bookmark = (TBookmark *)Source->Data;
  625. TTreeNode * PrevFolderNode = Source->Parent;
  626. if (!Dest || !Dest->Data)
  627. {
  628. Bookmark->Node = Dest ? Dest->Text : UnicodeString();
  629. BookmarkList->MoveTo(BookmarkList->Bookmarks[BookmarkList->Count - 1],
  630. Bookmark, false);
  631. ProfilesView->Selected->MoveTo(Dest, naAddChild);
  632. }
  633. else
  634. {
  635. TBookmark * DestBookmark = (TBookmark *)Dest->Data;
  636. Bookmark->Node = DestBookmark->Node;
  637. BookmarkList->MoveTo(DestBookmark, Bookmark,
  638. Source->AbsoluteIndex > Dest->AbsoluteIndex);
  639. if (Source->AbsoluteIndex > Dest->AbsoluteIndex)
  640. {
  641. Source->MoveTo(Dest, naInsert);
  642. }
  643. else if (Dest->getNextSibling() != NULL)
  644. {
  645. Source->MoveTo(Dest->getNextSibling(), naInsert);
  646. }
  647. else
  648. {
  649. Source->MoveTo(Dest, naAdd);
  650. }
  651. }
  652. if (PrevFolderNode && !PrevFolderNode->Count)
  653. {
  654. DebugAssert(Folders->IndexOfObject(PrevFolderNode) >= 0);
  655. Folders->Delete(Folders->IndexOfObject(PrevFolderNode));
  656. PrevFolderNode->Delete();
  657. }
  658. Source->MakeVisible();
  659. UpdateControls();
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TLocationProfilesDialog::BookmarkButtonClick(TObject * Sender)
  663. {
  664. TControl * Control = dynamic_cast<TControl *>(Sender);
  665. TTreeNode * Node = GetProfilesView(Sender)->Selected;
  666. DebugAssert(Node);
  667. DebugAssert(Node->Data);
  668. TTreeNode * TargetNode;
  669. if (Control->Tag < 0)
  670. {
  671. TargetNode = Node->getPrevSibling();
  672. DebugAssert(TargetNode);
  673. }
  674. else
  675. {
  676. TargetNode = Node->getNextSibling();
  677. }
  678. BookmarkMove(Sender, Node, TargetNode ? TargetNode : Node->Parent);
  679. }
  680. //---------------------------------------------------------------------------
  681. void __fastcall TLocationProfilesDialog::ProfilesViewStartDrag(
  682. TObject * Sender, TDragObject *& /*DragObject*/)
  683. {
  684. TTreeView * ProfilesView = GetProfilesView(Sender);
  685. if (!ProfilesView->Selected->Data)
  686. {
  687. Abort();
  688. }
  689. FBookmarkDragSource = ProfilesView->Selected;
  690. GetScrollOnDragOver(Sender)->StartDrag();
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall TLocationProfilesDialog::ProfilesViewDragOver(
  694. TObject * Sender, TObject * Source, int X, int Y,
  695. TDragState /*State*/, bool & Accept)
  696. {
  697. TTreeView * ProfilesView = GetProfilesView(Sender);
  698. if (Source == ProfilesView)
  699. {
  700. Accept = (ProfilesView->DropTarget != NULL) &&
  701. (FBookmarkDragSource != ProfilesView->DropTarget);
  702. GetScrollOnDragOver(Sender)->DragOver(TPoint(X, Y));
  703. }
  704. }
  705. //---------------------------------------------------------------------------
  706. void __fastcall TLocationProfilesDialog::ProfilesViewDragDrop(
  707. TObject * Sender, TObject * Source, int /*X*/, int /*Y*/)
  708. {
  709. TTreeView * ProfilesView = GetProfilesView(Sender);
  710. if ((Source == ProfilesView) && (ProfilesView->DropTarget != NULL) &&
  711. (FBookmarkDragSource != ProfilesView->DropTarget))
  712. {
  713. DebugAssert(FBookmarkDragSource);
  714. TTreeNode * Target = ProfilesView->DropTarget;
  715. BookmarkMove(Sender, FBookmarkDragSource, Target);
  716. FBookmarkDragSource = NULL;
  717. }
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall TLocationProfilesDialog::ProfilesViewDblClick(TObject * Sender)
  721. {
  722. TTreeView * ProfilesView = GetProfilesView(Sender);
  723. TPoint P = ProfilesView->ScreenToClient(Mouse->CursorPos);
  724. TTreeNode * Node = ProfilesView->GetNodeAt(P.x, P.y);
  725. if (OKBtn->Enabled && Node && Node->Data && Node->Selected)
  726. {
  727. ModalResult = DefaultResult(this);
  728. }
  729. }
  730. //---------------------------------------------------------------------------
  731. void __fastcall TLocationProfilesDialog::FormShow(TObject * /*Sender*/)
  732. {
  733. if (DebugAlwaysTrue(Terminal != NULL))
  734. {
  735. // cache session key, in case terminal is closed while the window is open
  736. FSessionKey = Terminal->SessionData->SessionKey;
  737. // WORKAROUND
  738. // Has to load this only now (not in Execute before ShowModal),
  739. // when the trees are finally (re)created,
  740. // otherwise the references in *Folders would be invalid already
  741. LoadBookmarks(SessionProfilesView, FSessionFolders, FSessionBookmarkList, WinConfiguration->Bookmarks[FSessionKey]);
  742. LoadBookmarks(SharedProfilesView, FSharedFolders, FSharedBookmarkList, WinConfiguration->SharedBookmarks);
  743. #ifdef _DEBUG
  744. FSessionProfilesViewHandle = SessionProfilesView->Handle;
  745. FSharedProfilesViewHandle = SharedProfilesView->Handle;
  746. #endif
  747. }
  748. if (Mode == odAddBookmark)
  749. {
  750. AddAsBookmark(GetProfilesSheet(), true);
  751. }
  752. InstallPathWordBreakProc(LocalDirectoryEdit);
  753. InstallPathWordBreakProc(RemoteDirectoryEdit);
  754. FindProfile();
  755. if (OperationSide == osLocal)
  756. {
  757. ActiveControl = LocalDirectoryEdit;
  758. }
  759. else
  760. {
  761. ActiveControl = RemoteDirectoryEdit;
  762. }
  763. UpdateControls();
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall TLocationProfilesDialog::ProfilesViewKeyDown(TObject * Sender,
  767. WORD & Key, TShiftState /*Shift*/)
  768. {
  769. TTreeView * ProfilesView = GetProfilesView(Sender);
  770. if (!ProfilesView->IsEditing())
  771. {
  772. if ((ProfilesView->Selected != NULL) && (Key == VK_DELETE))
  773. {
  774. RemoveBookmark(Sender);
  775. Key = 0;
  776. }
  777. else if ((ProfilesView->Selected != NULL) && (Key == VK_F2))
  778. {
  779. RenameBookmark(Sender);
  780. Key = 0;
  781. }
  782. }
  783. }
  784. //---------------------------------------------------------------------------
  785. void __fastcall TLocationProfilesDialog::DirectoryEditChange(TObject * /*Sender*/)
  786. {
  787. if (!FChanging)
  788. {
  789. FindProfile();
  790. UpdateControls();
  791. FBookmarkSelected = false;
  792. }
  793. }
  794. //---------------------------------------------------------------------------
  795. void __fastcall TLocationProfilesDialog::ProfilesViewChange(
  796. TObject * /*Sender*/, TTreeNode * Node)
  797. {
  798. if (Node && Node->Data)
  799. {
  800. DebugAssert(!FChanging);
  801. FChanging = true;
  802. try
  803. {
  804. LocalDirectoryEdit->Text = ((TBookmark *)Node->Data)->Local;
  805. RemoteDirectoryEdit->Text = ((TBookmark *)Node->Data)->Remote;
  806. }
  807. __finally
  808. {
  809. FChanging = false;
  810. }
  811. // try to locate the same profile in the other set
  812. FindProfile();
  813. FBookmarkSelected = true;
  814. }
  815. UpdateControls();
  816. }
  817. //---------------------------------------------------------------------------
  818. void __fastcall TLocationProfilesDialog::BookmarkMoveToButtonClick(TObject * Sender)
  819. {
  820. TTreeView * ProfilesView = GetProfilesView(Sender);
  821. TStringList * Folders = GetFolders(Sender);
  822. DebugAssert(ProfilesView->Selected->Data);
  823. TBookmark * Bookmark = (TBookmark *)ProfilesView->Selected->Data;
  824. TBookmarkFolderDialog * Dialog = new TBookmarkFolderDialog(Folders);
  825. try
  826. {
  827. UnicodeString NodeName = Bookmark->Node;
  828. if (Dialog->Execute(NodeName) &&
  829. (NodeName != Bookmark->Node))
  830. {
  831. TTreeNode * FolderNode;
  832. int I = Folders->IndexOf(NodeName);
  833. if (NodeName.IsEmpty())
  834. {
  835. FolderNode = NULL;
  836. }
  837. else if (I >= 0)
  838. {
  839. FolderNode = dynamic_cast<TTreeNode *>(Folders->Objects[I]);
  840. DebugAssert(FolderNode);
  841. }
  842. else
  843. {
  844. I = Folders->Add(NodeName);
  845. TTreeNode * NextNode;
  846. // duplicated in RenameButtonClick()
  847. if (I < Folders->Count-1)
  848. {
  849. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  850. DebugAssert(NextNode);
  851. }
  852. else if (Folders->Count > 1)
  853. {
  854. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  855. }
  856. else
  857. {
  858. DebugAssert(ProfilesView->Items->Count);
  859. NextNode = ProfilesView->Items->Item[0];
  860. }
  861. FolderNode = ProfilesView->Items->Insert(NextNode, NodeName);
  862. DebugAssert(FolderNode);
  863. Folders->Objects[I] = FolderNode;
  864. }
  865. BookmarkMove(Sender, ProfilesView->Selected, FolderNode);
  866. }
  867. }
  868. __finally
  869. {
  870. delete Dialog;
  871. }
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TLocationProfilesDialog::RenameBookmark(TObject * Sender)
  875. {
  876. TTreeView * ProfilesView = GetProfilesView(Sender);
  877. DebugAssert(ProfilesView->Selected != NULL);
  878. if (ProfilesView->Selected != NULL)
  879. {
  880. ProfilesView->SetFocus();
  881. ProfilesView->Selected->EditText();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TLocationProfilesDialog::RenameBookmarkButtonClick(TObject * Sender)
  886. {
  887. RenameBookmark(Sender);
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TLocationProfilesDialog::ProfilesViewGetImageIndex(
  891. TObject * /*Sender*/, TTreeNode * Node)
  892. {
  893. Node->ImageIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TLocationProfilesDialog::ProfilesViewGetSelectedIndex(
  897. TObject * /*Sender*/, TTreeNode * Node)
  898. {
  899. Node->SelectedIndex = Node->Data ? 0 : (Node->Expanded ? 1 : 2);
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall TLocationProfilesDialog::LocalDirectoryBrowseButtonClick(
  903. TObject * /*Sender*/)
  904. {
  905. UnicodeString Directory = LocalDirectoryEdit->Text;
  906. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true))
  907. {
  908. LocalDirectoryEdit->Text = Directory;
  909. DirectoryEditChange(LocalDirectoryEdit);
  910. }
  911. }
  912. //---------------------------------------------------------------------------
  913. void __fastcall TLocationProfilesDialog::SwitchButtonClick(TObject * /*Sender*/)
  914. {
  915. WinConfiguration->UseLocationProfiles = false;
  916. }
  917. //---------------------------------------------------------------------------
  918. void __fastcall TLocationProfilesDialog::HelpButtonClick(TObject * /*Sender*/)
  919. {
  920. FormHelp(this);
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TLocationProfilesDialog::ProfilesViewCollapsed(
  924. TObject * Sender, TTreeNode * Node)
  925. {
  926. DebugAssert(Node != NULL);
  927. DebugAssert(Node->Data == NULL);
  928. GetBookmarkList(Sender)->NodeOpened[Node->Text] = false;
  929. }
  930. //---------------------------------------------------------------------------
  931. void __fastcall TLocationProfilesDialog::ProfilesViewExpanded(
  932. TObject * Sender, TTreeNode * Node)
  933. {
  934. DebugAssert(Node != NULL);
  935. DebugAssert(Node->Data == NULL);
  936. GetBookmarkList(Sender)->NodeOpened[Node->Text] = true;
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TLocationProfilesDialog::ProfilesViewEdited(
  940. TObject * Sender, TTreeNode * Node, UnicodeString & S)
  941. {
  942. TTreeView * ProfilesView = GetProfilesView(Sender);
  943. TStringList * Folders = GetFolders(Sender);
  944. if (Node->Data != NULL)
  945. {
  946. BookmarkNameValidateName(S);
  947. // raises exception in case of duplicate name??
  948. ((TBookmark *)Node->Data)->Name = S;
  949. }
  950. else
  951. {
  952. BookmarkFolderValidateName(S, false);
  953. if (S.IsEmpty())
  954. {
  955. throw Exception(FMTLOAD(BOOKMARK_FOLDER_INVALID_NAME, (S)));
  956. }
  957. if ((Folders->IndexOf(S) >= 0) && AnsiCompareText(S, Node->Text))
  958. {
  959. throw Exception(FMTLOAD(DUPLICATE_BOOKMARK_FOLDER, (S)));
  960. }
  961. DebugAssert(Node->Count);
  962. Folders->Delete(Folders->IndexOf(Node->Text));
  963. int I = Folders->AddObject(S, Node);
  964. TTreeNode * NextNode;
  965. // duplicated in MoveToButtonClick()
  966. if (I < Folders->Count-1)
  967. {
  968. NextNode = dynamic_cast<TTreeNode *>(Folders->Objects[I+1]);
  969. DebugAssert(NextNode);
  970. }
  971. else if (Folders->Count > 1)
  972. {
  973. NextNode = (dynamic_cast<TTreeNode *>(Folders->Objects[I-1]))->getNextSibling();
  974. }
  975. else
  976. {
  977. DebugAssert(ProfilesView->Items->Count);
  978. NextNode = ProfilesView->Items->Item[0];
  979. }
  980. if (NextNode != Node)
  981. {
  982. Node->MoveTo(NextNode, NextNode ? naInsert : naAddChild);
  983. }
  984. for (int i = 0; i < Node->Count; i++)
  985. {
  986. ((TBookmark *)Node->Item[i]->Data)->Node = S;
  987. }
  988. Node->MakeVisible();
  989. }
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TLocationProfilesDialog::ProfilesViewEditing(
  993. TObject * /*Sender*/, TTreeNode * /*Node*/, bool & /*AllowEdit*/)
  994. {
  995. OKBtn->Default = false;
  996. CancelBtn->Cancel = false;
  997. }
  998. //---------------------------------------------------------------------------
  999. void __fastcall TLocationProfilesDialog::UpdateActions()
  1000. {
  1001. TForm::UpdateActions();
  1002. if ((!OKBtn->Default || !CancelBtn->Cancel) &&
  1003. !GetProfilesView(PageControl->ActivePage)->IsEditing())
  1004. {
  1005. OKBtn->Default = true;
  1006. CancelBtn->Cancel = true;
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. void __fastcall TLocationProfilesDialog::ProfilesViewEndDrag(
  1011. TObject * Sender, TObject * /*Target*/, int /*X*/, int /*Y*/)
  1012. {
  1013. GetScrollOnDragOver(Sender)->EndDrag();
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall TLocationProfilesDialog::ShortCutBookmarkButtonClick(
  1017. TObject * Sender)
  1018. {
  1019. TBookmarkList * BookmarkList = GetBookmarkList(Sender);
  1020. TTreeView * ProfilesView = GetProfilesView(Sender);
  1021. DebugAssert(ProfilesView->Selected != NULL);
  1022. TTreeNode * Node = ProfilesView->Selected;
  1023. DebugAssert(Node->Data != NULL);
  1024. TBookmark * Bookmark = static_cast<TBookmark *>(Node->Data);
  1025. TShortCuts ShortCuts;
  1026. WinConfiguration->CustomCommandShortCuts(ShortCuts);
  1027. BookmarkList->ShortCuts(ShortCuts);
  1028. TShortCut ShortCut = Bookmark->ShortCut;
  1029. if (DoShortCutDialog(ShortCut, ShortCuts, HelpKeyword))
  1030. {
  1031. Bookmark->ShortCut = ShortCut;
  1032. Node->Text = BookmarkText(Bookmark);
  1033. UpdateControls();
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. UnicodeString __fastcall TLocationProfilesDialog::BookmarkText(TBookmark * Bookmark)
  1038. {
  1039. UnicodeString Result = Bookmark->Name;
  1040. if (!Result.IsEmpty() && (Bookmark->ShortCut != 0))
  1041. {
  1042. Result = FORMAT(L"%s (%s)", (Result, ShortCutToText(Bookmark->ShortCut)));
  1043. }
  1044. return Result;
  1045. }
  1046. //---------------------------------------------------------------------------