Custom.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Dialogs.hpp>
  5. //---------------------------------------------------------------------
  6. #include <Common.h>
  7. #include <CustomWinConfiguration.h>
  8. #include <WinInterface.h>
  9. #include <VCLCommon.h>
  10. #include <TextsWin.h>
  11. #include <HelpWin.h>
  12. #include <CoreMain.h>
  13. #include <PasTools.hpp>
  14. #include <ProgParams.h>
  15. #include <Tools.h>
  16. #include <GUITools.h>
  17. #include <HistoryComboBox.hpp>
  18. #include <Math.hpp>
  19. #include "Custom.h"
  20. //---------------------------------------------------------------------
  21. #pragma link "PasswordEdit"
  22. #ifndef NO_RESOURCES
  23. #pragma resource "*.dfm"
  24. #endif
  25. //---------------------------------------------------------------------
  26. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  27. : TForm(GetFormOwner())
  28. {
  29. UseSystemSettings(this);
  30. FControlPadding = ScaleByTextHeight(this, 8);
  31. FPos = ScaleByTextHeight(this, 8);
  32. FPrePos = FPos;
  33. FHorizontalMargin = ScaleByTextHeight(this, 8);
  34. FIndent = FHorizontalMargin;
  35. FGroupBox = NULL;
  36. HelpKeyword = AHelpKeyword;
  37. TBorderIcons BI = BorderIcons;
  38. if (HelpKeyword.IsEmpty())
  39. {
  40. BI >> biHelp;
  41. OKButton->Left = CancelButton->Left;
  42. CancelButton->Left = HelpButton->Left;
  43. HelpButton->Visible = false;
  44. }
  45. else
  46. {
  47. BI << biHelp;
  48. }
  49. BorderIcons = BI;
  50. }
  51. //---------------------------------------------------------------------
  52. bool __fastcall TCustomDialog::Execute()
  53. {
  54. Changed();
  55. return (ShowModal() == DefaultResult(this));
  56. }
  57. //---------------------------------------------------------------------
  58. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  59. {
  60. // noop
  61. }
  62. //---------------------------------------------------------------------
  63. void __fastcall TCustomDialog::Changed()
  64. {
  65. bool CanSubmit = true;
  66. DoChange(CanSubmit);
  67. EnableControl(OKButton, CanSubmit);
  68. }
  69. //---------------------------------------------------------------------
  70. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  71. {
  72. Changed();
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TCustomDialog::DoHelp()
  76. {
  77. FormHelp(this);
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  81. {
  82. DoHelp();
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCustomDialog::DoShow()
  86. {
  87. OKButton->TabOrder = FCount;
  88. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  89. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  90. Changed();
  91. TForm::DoShow();
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TCustomDialog::DoValidate()
  95. {
  96. // noop
  97. }
  98. //---------------------------------------------------------------------------
  99. bool __fastcall TCustomDialog::CloseQuery()
  100. {
  101. if (ModalResult == DefaultResult(this))
  102. {
  103. DoValidate();
  104. }
  105. return TForm::CloseQuery();
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCustomDialog::AddImage(const UnicodeString & ImageName)
  109. {
  110. TImage * Image = new TImage(this);
  111. Image->Name = L"Image";
  112. Image->Parent = GetDefaultParent();
  113. LoadDialogImage(Image, ImageName);
  114. Image->SetBounds(FIndent, FPos + ScaleByTextHeight(this, 3), Image->Picture->Width, Image->Picture->Height);
  115. FIndent += Image->Width + ScaleByTextHeight(this, 12);
  116. }
  117. //---------------------------------------------------------------------------
  118. int __fastcall TCustomDialog::GetMaxControlWidth(TControl * Control)
  119. {
  120. return GetDefaultParent()->ClientWidth - Control->Left - FHorizontalMargin;
  121. }
  122. //---------------------------------------------------------------------------
  123. TWinControl * __fastcall TCustomDialog::GetDefaultParent()
  124. {
  125. return (FGroupBox != NULL) ? FGroupBox : static_cast<TWinControl *>(this);
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TCustomDialog::AdjustHeight(TControl * Control)
  129. {
  130. FPos = Control->Top + Control->Height + FControlPadding;
  131. int Delta = (FPos - FPrePos);
  132. ClientHeight = ClientHeight + Delta;
  133. if (FGroupBox != NULL)
  134. {
  135. FGroupBox->Height = FGroupBox->Height + Delta;
  136. }
  137. FPrePos = FPos;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  141. {
  142. Control->TabOrder = FCount;
  143. FCount++;
  144. }
  145. //---------------------------------------------------------------------------
  146. TCheckBox * __fastcall TCustomDialog::CreateAndAddCheckBox(const UnicodeString & Caption)
  147. {
  148. TCheckBox * CheckBox = new TCheckBox(this);
  149. CheckBox->Caption = Caption;
  150. AddButtonControl(CheckBox);
  151. return CheckBox;
  152. }
  153. //---------------------------------------------------------------------------
  154. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  155. {
  156. TLabel * Result = new TLabel(this);
  157. Result->Caption = Label;
  158. return Result;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label, bool OneLine)
  162. {
  163. Edit->Parent = GetDefaultParent();
  164. // this updates Height property to real value
  165. Edit->HandleNeeded();
  166. Label->Parent = GetDefaultParent();
  167. Label->Left = FIndent;
  168. if (OneLine)
  169. {
  170. DebugAssert(Edit->Height > Label->Height);
  171. Label->Top = FPos + ((Edit->Height - Label->Height) / 2);
  172. }
  173. else
  174. {
  175. Label->Top = FPos;
  176. FPos += Label->Height + ScaleByTextHeight(this, 4);
  177. }
  178. Edit->Top = FPos;
  179. if (OneLine)
  180. {
  181. Edit->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Edit->Width;
  182. }
  183. else
  184. {
  185. Edit->Left = FIndent;
  186. Edit->Width = GetMaxControlWidth(Edit);
  187. }
  188. AdjustHeight(Edit);
  189. if (Label->FocusControl == NULL)
  190. {
  191. Label->FocusControl = Edit;
  192. }
  193. else
  194. {
  195. DebugAssert(Label->FocusControl == Edit);
  196. }
  197. AddWinControl(Edit);
  198. }
  199. //---------------------------------------------------------------------------
  200. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label)
  201. {
  202. AddEditLikeControl(Edit, Label, false);
  203. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  204. if (PublicEdit->OnChange == NULL)
  205. {
  206. PublicEdit->OnChange = Change;
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label, TStrings * Items, bool OneLine)
  211. {
  212. AddEditLikeControl(Combo, Label, OneLine);
  213. if (Items != NULL)
  214. {
  215. Combo->Items = Items;
  216. }
  217. if (OneLine)
  218. {
  219. int Width = 0;
  220. for (int Index = 0; Index < Combo->Items->Count; Index++)
  221. {
  222. Width = Max(Width, Combo->Canvas->TextWidth(Combo->Items->Strings[Index]));
  223. }
  224. Width += ScaleByTextHeight(Combo, 4 + 16 + 14);
  225. Width = Max(Width, HelpButton->Width);
  226. Combo->Width = Width;
  227. Combo->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Width;
  228. }
  229. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  230. if (PublicCombo->OnChange == NULL)
  231. {
  232. PublicCombo->OnChange = Change;
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCustomDialog::ScaleButtonControl(TButtonControl * Control)
  237. {
  238. // this updates Height property to real value
  239. Control->HandleNeeded();
  240. // buttons do not scale with text on their own
  241. Control->Height = ScaleByTextHeight(Control, Control->Height);
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  245. {
  246. Control->Parent = GetDefaultParent();
  247. Control->Left = FIndent + ScaleByTextHeight(this, 6);
  248. Control->Top = FPos;
  249. Control->Width = GetMaxControlWidth(Control);
  250. ScaleButtonControl(Control);
  251. AdjustHeight(Control);
  252. AddWinControl(Control);
  253. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  254. if (PublicControl->OnClick == NULL)
  255. {
  256. PublicControl->OnClick = Change;
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TCustomDialog::AddText(TLabel * Label)
  261. {
  262. Label->Parent = GetDefaultParent();
  263. Label->WordWrap = true;
  264. Label->Left = FIndent;
  265. Label->Width = GetMaxControlWidth(Label);
  266. Label->Top = FPos;
  267. Label->ShowAccelChar = false;
  268. TRect TextRect;
  269. SetRect(&TextRect, 0, 0, Label->Width, 0);
  270. DrawText(Label->Canvas->Handle, Label->Caption.c_str(), Label->Caption.Length() + 1, &TextRect,
  271. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  272. Label->DrawTextBiDiModeFlagsReadingOnly());
  273. Label->Height = TextRect.Height();
  274. AdjustHeight(Label);
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TCustomDialog::AddText(TStaticText * Label)
  278. {
  279. Label->Parent = GetDefaultParent();
  280. Label->Left = FIndent;
  281. Label->Width = GetMaxControlWidth(Label);
  282. Label->Top = FPos;
  283. Label->ShowAccelChar = false;
  284. AdjustHeight(Label);
  285. AddWinControl(Label);
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TCustomDialog::AddSeparator()
  289. {
  290. TBevel * Bevel = new TBevel(this);
  291. Bevel->Parent = GetDefaultParent();
  292. Bevel->Left = FIndent;
  293. Bevel->Top = FPos;
  294. Bevel->Height = 2;
  295. Bevel->Width = GetMaxControlWidth(Bevel);
  296. AdjustHeight(Bevel);
  297. }
  298. //---------------------------------------------------------------------------
  299. void __fastcall TCustomDialog::StartGroup(const UnicodeString & Caption)
  300. {
  301. if (FGroupBox != NULL)
  302. {
  303. FIndent = FGroupBox->Left;
  304. FPos = FGroupBox->Top + FGroupBox->Height + FControlPadding;
  305. FPrePos = FPos;
  306. FGroupBox = NULL;
  307. }
  308. TGroupBox * GroupBox = new TGroupBox(this);
  309. GroupBox->Parent = this;
  310. GroupBox->Caption = Caption;
  311. GroupBox->Left = FIndent;
  312. GroupBox->Top = FPos;
  313. GroupBox->Height = ScaleByTextHeight(GroupBox, 20);
  314. GroupBox->Width = GetMaxControlWidth(GroupBox);
  315. AdjustHeight(GroupBox);
  316. AddWinControl(GroupBox);
  317. FPos = ScaleByTextHeight(this, 16);
  318. FPrePos = FPos;
  319. FIndent = FHorizontalMargin;
  320. FGroupBox = GroupBox;
  321. }
  322. //---------------------------------------------------------------------------
  323. //---------------------------------------------------------------------------
  324. class TSaveSessionDialog : public TCustomDialog
  325. {
  326. public:
  327. __fastcall TSaveSessionDialog(TComponent* AOwner);
  328. void __fastcall Init(bool CanSavePassword, bool NotRecommendedSavingPassword,
  329. TStrings * AdditionalFolders);
  330. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword,
  331. bool & CreateShortcut, const UnicodeString & OriginalSessionName);
  332. protected:
  333. virtual void __fastcall DoValidate();
  334. virtual void __fastcall DoChange(bool & CanSubmit);
  335. private:
  336. UnicodeString FOriginalSessionName;
  337. TEdit * SessionNameEdit;
  338. TComboBox * FolderCombo;
  339. TCheckBox * SavePasswordCheck;
  340. TCheckBox * CreateShortcutCheck;
  341. UnicodeString FRootFolder;
  342. UnicodeString __fastcall GetSessionName();
  343. };
  344. //---------------------------------------------------------------------------
  345. // Need to have an Owner argument for SafeFormCreate
  346. __fastcall TSaveSessionDialog::TSaveSessionDialog(TComponent* /*AOwner*/) :
  347. TCustomDialog(HELP_SESSION_SAVE)
  348. {
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TSaveSessionDialog::Init(bool CanSavePassword,
  352. bool NotRecommendedSavingPassword, TStrings * AdditionalFolders)
  353. {
  354. Caption = LoadStr(SAVE_SESSION_CAPTION);
  355. SessionNameEdit = new TEdit(this);
  356. AddEdit(SessionNameEdit, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  357. FRootFolder = LoadStr(SAVE_SESSION_ROOT_FOLDER2);
  358. std::unique_ptr<TStringList> Folders(new TStringList());
  359. if (AdditionalFolders != NULL)
  360. {
  361. Folders->AddStrings(AdditionalFolders);
  362. }
  363. for (int Index = 0; Index < StoredSessions->Count; Index++)
  364. {
  365. TSessionData * Data = StoredSessions->Sessions[Index];
  366. if (!Data->Special && !Data->IsWorkspace)
  367. {
  368. UnicodeString Folder = Data->FolderName;
  369. if (!Folder.IsEmpty() && Folders->IndexOf(Folder) < 0)
  370. {
  371. Folders->Add(Folder);
  372. }
  373. }
  374. }
  375. DebugAssert(!Folders->CaseSensitive);
  376. Folders->Sort();
  377. FolderCombo = new TComboBox(this);
  378. AddComboBox(FolderCombo, CreateLabel(LoadStr(SAVE_SESSION_FOLDER)));
  379. FolderCombo->DropDownCount = Max(FolderCombo->DropDownCount, 16);
  380. FolderCombo->Items->Add(FRootFolder);
  381. FolderCombo->Items->AddStrings(Folders.get());
  382. SavePasswordCheck = CreateAndAddCheckBox(
  383. LoadStr(NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  384. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED)));
  385. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  386. EnableControl(SavePasswordCheck, CanSavePassword);
  387. }
  388. //---------------------------------------------------------------------------
  389. bool __fastcall TSaveSessionDialog::Execute(
  390. UnicodeString & SessionName, bool & SavePassword, bool & CreateShortcut,
  391. const UnicodeString & OriginalSessionName)
  392. {
  393. FOriginalSessionName = OriginalSessionName;
  394. SessionNameEdit->Text = TSessionData::ExtractLocalName(SessionName);
  395. UnicodeString Folder = TSessionData::ExtractFolderName(SessionName);
  396. if (Folder.IsEmpty())
  397. {
  398. FolderCombo->Text = FRootFolder;
  399. }
  400. else
  401. {
  402. FolderCombo->Text = Folder;
  403. }
  404. SavePasswordCheck->Checked = SavePassword;
  405. CreateShortcutCheck->Checked = CreateShortcut;
  406. bool Result = TCustomDialog::Execute();
  407. if (Result)
  408. {
  409. SessionName = GetSessionName();
  410. SavePassword = SavePasswordCheck->Checked;
  411. CreateShortcut = CreateShortcutCheck->Checked;
  412. }
  413. return Result;
  414. }
  415. //---------------------------------------------------------------------------
  416. UnicodeString __fastcall TSaveSessionDialog::GetSessionName()
  417. {
  418. UnicodeString Folder;
  419. if (FolderCombo->Text != FRootFolder)
  420. {
  421. Folder = FolderCombo->Text;
  422. }
  423. return TSessionData::ComposePath(Folder, SessionNameEdit->Text);
  424. }
  425. //---------------------------------------------------------------------------
  426. void __fastcall TSaveSessionDialog::DoValidate()
  427. {
  428. TSessionData::ValidateName(SessionNameEdit->Text);
  429. SessionNameValidate(GetSessionName(), FOriginalSessionName);
  430. UnicodeString Folder = TSessionData::ExtractFolderName(GetSessionName());
  431. if (!Folder.IsEmpty() && StoredSessions->IsWorkspace(Folder))
  432. {
  433. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (Folder)));
  434. }
  435. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  436. CustomWinConfiguration->UseMasterPassword)
  437. {
  438. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  439. }
  440. TCustomDialog::DoValidate();
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  444. {
  445. CanSubmit = !SessionNameEdit->Text.IsEmpty();
  446. TCustomDialog::DoChange(CanSubmit);
  447. }
  448. //---------------------------------------------------------------------------
  449. TSessionData * __fastcall DoSaveSession(TSessionData * SessionData,
  450. TSessionData * OriginalSession, bool ForceDialog,
  451. TStrings * AdditionalFolders)
  452. {
  453. bool SavePassword = false;
  454. bool * PSavePassword;
  455. bool NotRecommendedSavingPassword =
  456. !CustomWinConfiguration->UseMasterPassword &&
  457. !SameText(SessionData->UserName, AnonymousUserName);
  458. if (Configuration->DisablePasswordStoring ||
  459. !SessionData->HasAnySessionPassword())
  460. {
  461. PSavePassword = NULL;
  462. }
  463. else
  464. {
  465. PSavePassword = &SavePassword;
  466. SavePassword =
  467. ((OriginalSession != NULL) && OriginalSession->HasAnySessionPassword()) ||
  468. !NotRecommendedSavingPassword;
  469. }
  470. UnicodeString SessionName = SessionData->SessionName;
  471. bool Result;
  472. bool CreateShortcut = false;
  473. if (!ForceDialog && ((PSavePassword == NULL) || SavePassword))
  474. {
  475. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  476. Result = true;
  477. }
  478. else
  479. {
  480. // This can be a standalone dialog when used with save URL (from GetLoginData)
  481. TSaveSessionDialog * Dialog = SafeFormCreate<TSaveSessionDialog>();
  482. try
  483. {
  484. Dialog->Init((PSavePassword != NULL), NotRecommendedSavingPassword, AdditionalFolders);
  485. Result = Dialog->Execute(SessionName, SavePassword, CreateShortcut, SessionData->Name);
  486. }
  487. __finally
  488. {
  489. delete Dialog;
  490. }
  491. }
  492. TSessionData * NewSession = NULL;
  493. if (Result)
  494. {
  495. if ((PSavePassword != NULL) && !SavePassword)
  496. {
  497. SessionData->ClearSessionPasswords();
  498. }
  499. NewSession =
  500. StoredSessions->NewSession(SessionName, SessionData);
  501. // modified only, explicit
  502. StoredSessions->Save(false, true);
  503. if (!SessionData->HostKey.IsEmpty())
  504. {
  505. SessionData->CacheHostKeyIfNotCached();
  506. }
  507. if (CreateShortcut)
  508. {
  509. TOperationVisualizer Visualizer;
  510. UnicodeString AdditionalParams =
  511. TProgramParams::FormatSwitch(DESKTOP_SWITCH) + L" " +
  512. TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH);
  513. CreateDesktopSessionShortCut(SessionName, L"", AdditionalParams, -1, SITE_ICON);
  514. }
  515. }
  516. return NewSession;
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall SessionNameValidate(const UnicodeString & Text,
  520. const UnicodeString & OriginalName)
  521. {
  522. TSessionData::ValidatePath(Text);
  523. DebugAssert(StoredSessions);
  524. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  525. if (Data && Data->Special)
  526. {
  527. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  528. qtError, qaOK, HELP_NONE);
  529. Abort();
  530. }
  531. else if ((Data != NULL) && !Data->IsSameName(OriginalName) &&
  532. MessageDialog(MainInstructions(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text))),
  533. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  534. {
  535. Abort();
  536. }
  537. }
  538. //---------------------------------------------------------------------------
  539. //---------------------------------------------------------------------------
  540. class TSaveWorkspaceDialog : public TCustomDialog
  541. {
  542. public:
  543. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  544. bool NotRecommendedSavingPasswords);
  545. bool __fastcall Execute(
  546. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  547. bool & EnableAutoSave);
  548. protected:
  549. virtual void __fastcall DoValidate();
  550. virtual void __fastcall DoChange(bool & CanSubmit);
  551. private:
  552. TComboBox * WorkspaceNameCombo;
  553. TCheckBox * SavePasswordsCheck;
  554. TCheckBox * CreateShortcutCheck;
  555. TCheckBox * EnableAutoSaveCheck;
  556. };
  557. //---------------------------------------------------------------------------
  558. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  559. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  560. TCustomDialog(HELP_WORKSPACE_SAVE)
  561. {
  562. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  563. WorkspaceNameCombo = new TComboBox(this);
  564. WorkspaceNameCombo->AutoComplete = false;
  565. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  566. WorkspaceNameCombo->DropDownCount = Max(WorkspaceNameCombo->DropDownCount, 16);
  567. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  568. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  569. SavePasswordsCheck = CreateAndAddCheckBox(
  570. LoadStr(NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  571. (CustomWinConfiguration->UseMasterPassword ?
  572. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED)));
  573. EnableControl(SavePasswordsCheck, CanSavePasswords);
  574. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  575. EnableAutoSaveCheck = CreateAndAddCheckBox(LoadStr(SAVE_WORKSPACE_AUTO));
  576. }
  577. //---------------------------------------------------------------------------
  578. bool __fastcall TSaveWorkspaceDialog::Execute(
  579. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  580. bool & EnableAutoSave)
  581. {
  582. WorkspaceNameCombo->Text = WorkspaceName;
  583. SavePasswordsCheck->Checked = SavePasswords;
  584. CreateShortcutCheck->Checked = CreateShortcut;
  585. EnableAutoSaveCheck->Checked = EnableAutoSave;
  586. bool Result = TCustomDialog::Execute();
  587. if (Result)
  588. {
  589. WorkspaceName = WorkspaceNameCombo->Text;
  590. SavePasswords = SavePasswordsCheck->Checked;
  591. CreateShortcut = CreateShortcutCheck->Checked;
  592. EnableAutoSave = EnableAutoSaveCheck->Checked;
  593. }
  594. return Result;
  595. }
  596. //---------------------------------------------------------------------------
  597. void __fastcall TSaveWorkspaceDialog::DoValidate()
  598. {
  599. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  600. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  601. {
  602. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  603. }
  604. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  605. CustomWinConfiguration->UseMasterPassword)
  606. {
  607. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  608. }
  609. TCustomDialog::DoValidate();
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  613. {
  614. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  615. TCustomDialog::DoChange(CanSubmit);
  616. }
  617. //---------------------------------------------------------------------------
  618. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  619. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  620. bool & CreateShortcut, bool & EnableAutoSave)
  621. {
  622. std::unique_ptr<TSaveWorkspaceDialog> Dialog(
  623. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  624. bool Dummy = false;
  625. if (SavePasswords == NULL)
  626. {
  627. SavePasswords = &Dummy;
  628. }
  629. return
  630. Dialog->Execute(
  631. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  632. }
  633. //---------------------------------------------------------------------------
  634. //---------------------------------------------------------------------------
  635. class TShortCutDialog : public TCustomDialog
  636. {
  637. public:
  638. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  639. bool __fastcall Execute(TShortCut & ShortCut);
  640. private:
  641. TComboBox * ShortCutCombo;
  642. };
  643. //---------------------------------------------------------------------------
  644. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  645. TCustomDialog(HelpKeyword)
  646. {
  647. Caption = LoadStr(SHORTCUT_CAPTION);
  648. ShortCutCombo = new TComboBox(this);
  649. AddComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)));
  650. InitializeShortCutCombo(ShortCutCombo, ShortCuts);
  651. }
  652. //---------------------------------------------------------------------------
  653. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  654. {
  655. SetShortCutCombo(ShortCutCombo, ShortCut);
  656. bool Result = TCustomDialog::Execute();
  657. if (Result)
  658. {
  659. ShortCut = GetShortCutCombo(ShortCutCombo);
  660. }
  661. return Result;
  662. }
  663. //---------------------------------------------------------------------------
  664. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  665. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  666. {
  667. bool Result;
  668. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  669. try
  670. {
  671. Result = Dialog->Execute(ShortCut);
  672. }
  673. __finally
  674. {
  675. delete Dialog;
  676. }
  677. return Result;
  678. }
  679. //---------------------------------------------------------------------------
  680. //---------------------------------------------------------------------------
  681. class TRemoteMoveDialog : public TCustomDialog
  682. {
  683. public:
  684. __fastcall TRemoteMoveDialog(bool Multi);
  685. bool __fastcall Execute(UnicodeString & Target, UnicodeString & FileMask);
  686. protected:
  687. DYNAMIC void __fastcall DoShow();
  688. virtual void __fastcall DoValidate();
  689. UnicodeString __fastcall GetFileMask();
  690. private:
  691. THistoryComboBox * Combo;
  692. bool FMulti;
  693. };
  694. //---------------------------------------------------------------------------
  695. __fastcall TRemoteMoveDialog::TRemoteMoveDialog(bool Multi) :
  696. TCustomDialog(HELP_REMOTE_MOVE)
  697. {
  698. Caption = LoadStr(REMOTE_MOVE_TITLE);
  699. // The same as TRemoteTransferDialog
  700. ClientWidth = ScaleByTextHeight(this, 420);
  701. FMulti = Multi;
  702. AddImage(L"Move To");
  703. Combo = new THistoryComboBox(this);
  704. Combo->AutoComplete = false;
  705. AddComboBox(Combo, CreateLabel(LoadStr(REMOTE_TRANSFER_PROMPT2)));
  706. }
  707. //---------------------------------------------------------------------------
  708. bool __fastcall TRemoteMoveDialog::Execute(UnicodeString & Target, UnicodeString & FileMask)
  709. {
  710. Combo->Items = CustomWinConfiguration->History[L"RemoteTarget"];
  711. Combo->Text = UnixIncludeTrailingBackslash(Target) + FileMask;
  712. bool Result = TCustomDialog::Execute();
  713. if (Result)
  714. {
  715. Target = UnixExtractFilePath(Combo->Text);
  716. FileMask = GetFileMask();
  717. Combo->SaveToHistory();
  718. CustomWinConfiguration->History[L"RemoteTarget"] = Combo->Items;
  719. }
  720. return Result;
  721. }
  722. //---------------------------------------------------------------------------
  723. UnicodeString __fastcall TRemoteMoveDialog::GetFileMask()
  724. {
  725. return UnixExtractFileName(Combo->Text);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TRemoteMoveDialog::DoShow()
  729. {
  730. TCustomDialog::DoShow();
  731. InstallPathWordBreakProc(Combo);
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TRemoteMoveDialog::DoValidate()
  735. {
  736. if (!IsFileNameMask(GetFileMask()) && FMulti)
  737. {
  738. UnicodeString Message =
  739. FormatMultiFilesToOneConfirmation(Combo->Text, true);
  740. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) == qaCancel)
  741. {
  742. Abort();
  743. }
  744. }
  745. TCustomDialog::DoValidate();
  746. }
  747. //---------------------------------------------------------------------------
  748. bool __fastcall DoRemoteMoveDialog(bool Multi, UnicodeString & Target, UnicodeString & FileMask)
  749. {
  750. std::unique_ptr<TRemoteMoveDialog> Dialog(new TRemoteMoveDialog(Multi));
  751. return Dialog->Execute(Target, FileMask);
  752. }
  753. //---------------------------------------------------------------------------
  754. //---------------------------------------------------------------------------
  755. class TCustomCommandOptionsDialog : public TCustomDialog
  756. {
  757. public:
  758. __fastcall TCustomCommandOptionsDialog(
  759. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
  760. TCustomCommand * CustomCommandForOptions, const UnicodeString & Site);
  761. bool __fastcall Execute();
  762. protected:
  763. virtual void __fastcall DoHelp();
  764. DYNAMIC void __fastcall DoShow();
  765. private:
  766. const TCustomCommandType * FCommand;
  767. TStrings * FCustomCommandOptions;
  768. std::vector<TControl *> FControls;
  769. std::vector<std::vector<UnicodeString> > FValues;
  770. unsigned int FFlags;
  771. UnicodeString FSite;
  772. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  773. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  774. void __fastcall BrowseButtonClick(TObject * Sender);
  775. void __fastcall LinkLabelClick(TObject * Sender);
  776. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  777. void __fastcall AddOptionComboBox(
  778. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  779. std::vector<UnicodeString> & Values);
  780. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  781. int __fastcall GetOptionIndex(TControl * Control);
  782. int __fastcall GetControlIndex(TControl * Control);
  783. };
  784. //---------------------------------------------------------------------------
  785. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  786. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  787. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  788. const UnicodeString & Site) :
  789. TCustomDialog(HELP_EXTENSION_OPTIONS)
  790. {
  791. FCommand = Command;
  792. FFlags = Flags;
  793. FCustomCommandOptions = CustomCommandOptions;
  794. FSite = Site;
  795. Caption = FMTLOAD(EXTENSION_OPTIONS_CAPTION, (StripEllipsis(StripHotkey(FCommand->Name))));
  796. Width = ScaleByTextHeight(this, 400);
  797. int ControlIndex = 0;
  798. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  799. {
  800. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  801. if ((Option.Flags & FFlags) != 0)
  802. {
  803. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  804. UnicodeString Value;
  805. if ((CustomCommandForOptions != NULL) &&
  806. Option.HasPatterns(CustomCommandForOptions))
  807. {
  808. Value = CustomCommandForOptions->Complete(Option.Default, true);
  809. }
  810. else
  811. {
  812. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  813. {
  814. Value = FCustomCommandOptions->Values[OptionKey];
  815. }
  816. else
  817. {
  818. Value = Option.Default;
  819. }
  820. }
  821. int Tag = (OptionIndex << 16) + ControlIndex;
  822. TControl * Control = NULL;
  823. std::vector<UnicodeString> Values;
  824. if (Option.Kind == TCustomCommandType::okUnknown)
  825. {
  826. Control = NULL;
  827. }
  828. else if (Option.Kind == TCustomCommandType::okLabel)
  829. {
  830. TLabel * Label = CreateLabel(Option.Caption);
  831. AddText(Label);
  832. Control = Label;
  833. }
  834. else if (Option.Kind == TCustomCommandType::okLink)
  835. {
  836. TStaticText * Label = new TStaticText(this);
  837. Label->Caption = Option.Caption;
  838. if (IsHttpOrHttpsUrl(Label->Caption))
  839. {
  840. Label->Caption = SecureUrl(Label->Caption);
  841. LinkLabel(Label);
  842. Label->TabStop = true;
  843. }
  844. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  845. {
  846. Label->OnClick = LinkLabelClick;
  847. LinkLabel(Label);
  848. Label->TabStop = true;
  849. }
  850. else
  851. {
  852. // keep it plain text, as we have no URL
  853. }
  854. AddText(Label);
  855. Control = Label;
  856. }
  857. else if (Option.Kind == TCustomCommandType::okGroup)
  858. {
  859. StartGroup(Option.Caption);
  860. }
  861. else if (Option.Kind == TCustomCommandType::okSeparator)
  862. {
  863. AddSeparator();
  864. }
  865. else if (Option.Kind == TCustomCommandType::okTextBox)
  866. {
  867. Control = CreateHistoryComboBox(Option, Value);
  868. }
  869. else if (Option.Kind == TCustomCommandType::okFile)
  870. {
  871. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  872. TButton * Button = new TButton(this);
  873. Button->Parent = GetDefaultParent();
  874. Button->Width = HelpButton->Width;
  875. Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
  876. ComboBox->Width = Button->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
  877. Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
  878. Button->Tag = Tag;
  879. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  880. Button->OnClick = BrowseButtonClick;
  881. ScaleButtonControl(Button);
  882. AddWinControl(Button);
  883. Control = ComboBox;
  884. }
  885. else if (Option.Kind == TCustomCommandType::okDropDownList)
  886. {
  887. TComboBox * ComboBox = new TComboBox(this);
  888. ComboBox->Style = csDropDownList;
  889. AddOptionComboBox(ComboBox, Value, Option, Values);
  890. Control = ComboBox;
  891. }
  892. else if (Option.Kind == TCustomCommandType::okComboBox)
  893. {
  894. TComboBox * ComboBox = new TComboBox(this);
  895. ComboBox->Style = csDropDown;
  896. AddOptionComboBox(ComboBox, Value, Option, Values);
  897. if (ComboBox->ItemIndex < 0)
  898. {
  899. ComboBox->Text = Value;
  900. }
  901. Control = ComboBox;
  902. }
  903. else if (Option.Kind == TCustomCommandType::okCheckBox)
  904. {
  905. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  906. CheckBox->Checked =
  907. (Option.Params.size() >= 1) &&
  908. (Value == Option.Params[0]);
  909. Control = CheckBox;
  910. }
  911. else
  912. {
  913. DebugFail();
  914. }
  915. if (Control != NULL)
  916. {
  917. Control->Tag = Tag;
  918. }
  919. FControls.push_back(Control);
  920. FValues.push_back(Values);
  921. ControlIndex++;
  922. DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
  923. }
  924. }
  925. }
  926. //---------------------------------------------------------------------------
  927. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  928. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  929. {
  930. std::unique_ptr<TStringList> Items(new TStringList());
  931. int ItemIndex = -1;
  932. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  933. while (ParamI != Option.Params.end())
  934. {
  935. UnicodeString Item = (*ParamI);
  936. int P = Item.Pos(L"=");
  937. UnicodeString ParamValue;
  938. if (P > 0)
  939. {
  940. ParamValue = Item.SubString(1, P - 1);
  941. Item.Delete(1, P);
  942. }
  943. else
  944. {
  945. ParamValue = Item;
  946. }
  947. Items->Add(Item);
  948. if (Value == ParamValue)
  949. {
  950. ItemIndex = Items->Count - 1;
  951. }
  952. Values.push_back(ParamValue);
  953. ParamI++;
  954. }
  955. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  956. ComboBox->ItemIndex = ItemIndex;
  957. }
  958. //---------------------------------------------------------------------------
  959. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  960. {
  961. return (Control->Tag >> 16);
  962. }
  963. //---------------------------------------------------------------------------
  964. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  965. {
  966. return (Control->Tag & 0xFFFF);
  967. }
  968. //---------------------------------------------------------------------------
  969. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  970. {
  971. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  972. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  973. OpenBrowser(SecureUrl(Option.Default));
  974. }
  975. //---------------------------------------------------------------------------
  976. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  977. {
  978. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  979. int OptionIndex = GetOptionIndex(Button);
  980. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  981. int ControlIndex = GetControlIndex(Button);
  982. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  983. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  984. UnicodeString Title;
  985. if (!Option.FileCaption.IsEmpty())
  986. {
  987. Title = Option.FileCaption;
  988. }
  989. else
  990. {
  991. UnicodeString Caption = Option.Caption;
  992. Caption = StripHotkey(Caption);
  993. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  994. {
  995. Caption.SetLength(Caption.Length() - 1);
  996. }
  997. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  998. }
  999. OpenDialog->Title = Title;
  1000. UnicodeString Value;
  1001. if (ComboBox->Text.IsEmpty())
  1002. {
  1003. Value = Option.FileInitial;
  1004. }
  1005. else
  1006. {
  1007. Value = ComboBox->Text;
  1008. }
  1009. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  1010. OpenDialog->FileName = ExpandedValue;
  1011. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  1012. if (!InitialDir.IsEmpty())
  1013. {
  1014. OpenDialog->InitialDir = InitialDir;
  1015. }
  1016. OpenDialog->Filter = Option.FileFilter;
  1017. OpenDialog->DefaultExt = Option.FileExt;
  1018. if (OpenDialog->Execute())
  1019. {
  1020. if (OpenDialog->FileName != ExpandedValue)
  1021. {
  1022. ComboBox->Text = OpenDialog->FileName;
  1023. }
  1024. // If user just confirms the initial value, persist it
  1025. else if (ComboBox->Text.IsEmpty())
  1026. {
  1027. DebugAssert(Option.FileInitial == Value);
  1028. ComboBox->Text = Value;
  1029. }
  1030. }
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1034. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1035. {
  1036. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1037. ComboBox->AutoComplete = false;
  1038. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1039. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1040. ComboBox->Text = Value;
  1041. return ComboBox;
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1045. {
  1046. UnicodeString Result = FCommand->GetOptionKey(Option, FSite);
  1047. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1048. return L"CustomCommandOption_" + Result;
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. bool __fastcall TCustomCommandOptionsDialog::Execute()
  1052. {
  1053. bool Result = TCustomDialog::Execute();
  1054. if (Result)
  1055. {
  1056. int ControlIndex = 0;
  1057. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1058. {
  1059. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1060. if ((Option.Flags & FFlags) != 0)
  1061. {
  1062. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1063. Option.IsControl)
  1064. {
  1065. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  1066. TControl * Control = FControls[ControlIndex];
  1067. UnicodeString Value;
  1068. if (Option.Kind == TCustomCommandType::okTextBox)
  1069. {
  1070. Value = SaveHistoryComboBoxValue(Control, Option);
  1071. }
  1072. else if (Option.Kind == TCustomCommandType::okFile)
  1073. {
  1074. Value = SaveHistoryComboBoxValue(Control, Option);
  1075. }
  1076. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1077. {
  1078. Value = GetComboBoxValue(Control, Option.Default);
  1079. }
  1080. else if (Option.Kind == TCustomCommandType::okComboBox)
  1081. {
  1082. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1083. Value = GetComboBoxValue(Control, ComboBox->Text);
  1084. }
  1085. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1086. {
  1087. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1088. int Index = (CheckBox->Checked ? 0 : 1);
  1089. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1090. }
  1091. else
  1092. {
  1093. DebugFail();
  1094. }
  1095. // The default value setter deletes the "name" when the value is empty.
  1096. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1097. if (Value.IsEmpty())
  1098. {
  1099. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  1100. if (Index < 0)
  1101. {
  1102. Index = FCustomCommandOptions->Add(L"");
  1103. }
  1104. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  1105. FCustomCommandOptions->Strings[Index] = Line;
  1106. }
  1107. else
  1108. {
  1109. FCustomCommandOptions->Values[OptionKey] = Value;
  1110. }
  1111. }
  1112. ControlIndex++;
  1113. }
  1114. }
  1115. }
  1116. return Result;
  1117. }
  1118. //---------------------------------------------------------------------------
  1119. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1120. TControl * Control, const UnicodeString & Default)
  1121. {
  1122. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1123. UnicodeString Result;
  1124. if (ComboBox->ItemIndex < 0)
  1125. {
  1126. Result = Default;
  1127. }
  1128. else
  1129. {
  1130. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1131. }
  1132. return Result;
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1136. TControl * Control, const TCustomCommandType::TOption & Option)
  1137. {
  1138. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1139. ComboBox->SaveToHistory();
  1140. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1141. return ComboBox->Text;
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1145. {
  1146. UnicodeString HelpPage;
  1147. if (!FCommand->OptionsPage.IsEmpty())
  1148. {
  1149. HelpPage = FCommand->OptionsPage;
  1150. }
  1151. else
  1152. {
  1153. HelpPage = FCommand->HomePage;
  1154. }
  1155. if (!HelpPage.IsEmpty())
  1156. {
  1157. OpenBrowser(HelpPage);
  1158. }
  1159. else
  1160. {
  1161. TCustomDialog::DoHelp();
  1162. }
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1166. {
  1167. TCustomDialog::DoShow();
  1168. int ControlIndex = 0;
  1169. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1170. {
  1171. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1172. if ((Option.Flags & FFlags) != 0)
  1173. {
  1174. if (Option.Kind == TCustomCommandType::okFile)
  1175. {
  1176. TControl * Control = FControls[ControlIndex];
  1177. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1178. }
  1179. ControlIndex++;
  1180. }
  1181. }
  1182. }
  1183. //---------------------------------------------------------------------------
  1184. bool __fastcall DoCustomCommandOptionsDialog(
  1185. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  1186. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  1187. const UnicodeString & Site)
  1188. {
  1189. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1190. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions, Site));
  1191. return Dialog->Execute();
  1192. }