LocationProfiles.cpp 35 KB

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