LocationProfiles.cpp 36 KB

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