Custom.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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 <PuttyTools.h>
  18. #include <HistoryComboBox.hpp>
  19. #include <Math.hpp>
  20. #include <System.Character.hpp>
  21. #include "Custom.h"
  22. //---------------------------------------------------------------------
  23. #pragma resource "*.dfm"
  24. //---------------------------------------------------------------------
  25. const int GroupBoxBorderWidth = 1;
  26. //---------------------------------------------------------------------
  27. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  28. : TForm(GetFormOwner())
  29. {
  30. UseSystemSettings(this);
  31. FControlPadding = ScaleByTextHeight(this, 6);
  32. FPos = ScaleByTextHeight(this, 8);
  33. FPrePos = FPos;
  34. FHorizontalMargin = ScaleByTextHeight(this, 8);
  35. FIndent = FHorizontalMargin;
  36. FGroupBox = NULL;
  37. HelpKeyword = AHelpKeyword;
  38. TBorderIcons BI = BorderIcons;
  39. if (HelpKeyword.IsEmpty())
  40. {
  41. BI >> biHelp;
  42. OKButton->Left = CancelButton->Left;
  43. CancelButton->Left = HelpButton->Left;
  44. HelpButton->Visible = false;
  45. }
  46. else
  47. {
  48. BI << biHelp;
  49. }
  50. BorderIcons = BI;
  51. }
  52. //---------------------------------------------------------------------
  53. bool __fastcall TCustomDialog::Execute()
  54. {
  55. Changed();
  56. return (ShowModal() == DefaultResult(this));
  57. }
  58. //---------------------------------------------------------------------
  59. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  60. {
  61. // noop
  62. }
  63. //---------------------------------------------------------------------
  64. void __fastcall TCustomDialog::Changed()
  65. {
  66. bool CanSubmit = true;
  67. DoChange(CanSubmit);
  68. EnableControl(OKButton, CanSubmit);
  69. }
  70. //---------------------------------------------------------------------
  71. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  72. {
  73. Changed();
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TCustomDialog::DoHelp()
  77. {
  78. FormHelp(this);
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  82. {
  83. DoHelp();
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TCustomDialog::DoShow()
  87. {
  88. OKButton->TabOrder = FCount;
  89. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  90. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  91. Changed();
  92. TForm::DoShow();
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TCustomDialog::DoValidate()
  96. {
  97. // noop
  98. }
  99. //---------------------------------------------------------------------------
  100. bool __fastcall TCustomDialog::CloseQuery()
  101. {
  102. if (ModalResult == DefaultResult(this))
  103. {
  104. DoValidate();
  105. }
  106. return TForm::CloseQuery();
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TCustomDialog::RemoveCancelButton()
  110. {
  111. CancelButton->Visible = false;
  112. OKButton->Left = CancelButton->Left;
  113. OKButton->Cancel = true;
  114. DebugAssert(OKButton->Width == CancelButton->Width);
  115. DebugAssert(OKButton->Top == CancelButton->Top);
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TCustomDialog::AddDialogButton(TButton * Button)
  119. {
  120. Button->Parent = this;
  121. Button->Top = OKButton->Top;
  122. Button->Left = FHorizontalMargin;
  123. Button->Height = OKButton->Height;
  124. AddWinControl(Button);
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TCustomDialog::AddImage(const UnicodeString & ImageName)
  128. {
  129. TImage * Image = new TImage(this);
  130. Image->Name = L"Image";
  131. Image->Parent = GetDefaultParent();
  132. LoadDialogImage(Image, ImageName);
  133. Image->SetBounds(FIndent, FPos + ScaleByTextHeight(this, 3), Image->Picture->Width, Image->Picture->Height);
  134. FIndent += Image->Width + ScaleByTextHeight(this, 12);
  135. }
  136. //---------------------------------------------------------------------------
  137. int __fastcall TCustomDialog::GetMaxControlWidth(TControl * Control)
  138. {
  139. return GetDefaultParent()->ClientWidth - Control->Left - FHorizontalMargin - (FGroupBox != NULL ? GroupBoxBorderWidth : 0);
  140. }
  141. //---------------------------------------------------------------------------
  142. TWinControl * __fastcall TCustomDialog::GetDefaultParent()
  143. {
  144. return (FGroupBox != NULL) ? FGroupBox : static_cast<TWinControl *>(this);
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TCustomDialog::AdjustHeight(TControl * Control)
  148. {
  149. FPos = Control->Top + Control->Height + FControlPadding;
  150. int Delta = (FPos - FPrePos);
  151. ClientHeight = ClientHeight + Delta;
  152. if (FGroupBox != NULL)
  153. {
  154. FGroupBox->Height = FGroupBox->Height + Delta;
  155. }
  156. FPrePos = FPos;
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  160. {
  161. Control->TabOrder = FCount;
  162. FCount++;
  163. }
  164. //---------------------------------------------------------------------------
  165. TCheckBox * __fastcall TCustomDialog::CreateAndAddCheckBox(const UnicodeString & Caption)
  166. {
  167. TCheckBox * CheckBox = new TCheckBox(this);
  168. CheckBox->Caption = Caption;
  169. AddButtonControl(CheckBox);
  170. return CheckBox;
  171. }
  172. //---------------------------------------------------------------------------
  173. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  174. {
  175. TLabel * Result = ::CreateLabel(this);
  176. Result->Caption = Label;
  177. return Result;
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label, bool OneLine)
  181. {
  182. Edit->Parent = GetDefaultParent();
  183. // this updates Height property to real value
  184. Edit->HandleNeeded();
  185. if (Label != NULL)
  186. {
  187. Label->Parent = GetDefaultParent();
  188. Label->Left = FIndent;
  189. if (OneLine)
  190. {
  191. DebugAssert(Edit->Height > Label->Height);
  192. Label->Top = FPos + ((Edit->Height - Label->Height) / 2);
  193. }
  194. else
  195. {
  196. Label->Top = FPos;
  197. FPos += Label->Height + ScaleByTextHeight(this, 3);
  198. }
  199. }
  200. Edit->Top = FPos;
  201. if (OneLine)
  202. {
  203. Edit->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Edit->Width;
  204. }
  205. else
  206. {
  207. Edit->Left = FIndent;
  208. Edit->Width = GetMaxControlWidth(Edit);
  209. }
  210. AdjustHeight(Edit);
  211. if (Label != NULL)
  212. {
  213. if (Label->FocusControl == NULL)
  214. {
  215. Label->FocusControl = Edit;
  216. }
  217. else
  218. {
  219. DebugAssert(Label->FocusControl == Edit);
  220. }
  221. }
  222. AddWinControl(Edit);
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label, bool OneLine)
  226. {
  227. AddEditLikeControl(Edit, Label, OneLine);
  228. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  229. if (PublicEdit->OnChange == NULL)
  230. {
  231. PublicEdit->OnChange = Change;
  232. }
  233. }
  234. //---------------------------------------------------------------------------
  235. void __fastcall TCustomDialog::SetUpComboBox(TCustomCombo * Combo, TStrings * Items, bool OneLine)
  236. {
  237. if (Items != NULL)
  238. {
  239. Combo->Items = Items;
  240. }
  241. if (OneLine)
  242. {
  243. int Width = 0;
  244. for (int Index = 0; Index < Combo->Items->Count; Index++)
  245. {
  246. Width = Max(Width, Combo->Canvas->TextWidth(Combo->Items->Strings[Index]));
  247. }
  248. Width += ScaleByTextHeight(Combo, 4 + 16 + 14);
  249. Width = Max(Width, HelpButton->Width);
  250. Combo->Width = Width;
  251. Combo->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Width;
  252. }
  253. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  254. if (PublicCombo->OnChange == NULL)
  255. {
  256. PublicCombo->OnChange = Change;
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label, TStrings * Items, bool OneLine)
  261. {
  262. AddEditLikeControl(Combo, Label, OneLine);
  263. SetUpComboBox(Combo, Items, OneLine);
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TCustomDialog::AddShortCutComboBox(TComboBox * Combo, TLabel * Label, const TShortCuts & ShortCuts)
  267. {
  268. AddEditLikeControl(Combo, Label, true);
  269. InitializeShortCutCombo(Combo, ShortCuts);
  270. SetUpComboBox(Combo, NULL, true);
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TCustomDialog::ScaleButtonControl(TButtonControl * Control)
  274. {
  275. // this updates Height property to real value
  276. Control->HandleNeeded();
  277. // buttons do not scale with text on their own
  278. Control->Height = ScaleByTextHeight(Control, Control->Height);
  279. }
  280. //---------------------------------------------------------------------------
  281. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  282. {
  283. Control->Parent = GetDefaultParent();
  284. Control->Left = FIndent + ScaleByTextHeight(this, 2);
  285. Control->Top = FPos;
  286. Control->Width = GetMaxControlWidth(Control);
  287. ScaleButtonControl(Control);
  288. AdjustHeight(Control);
  289. AddWinControl(Control);
  290. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  291. if (PublicControl->OnClick == NULL)
  292. {
  293. PublicControl->OnClick = Change;
  294. }
  295. }
  296. //---------------------------------------------------------------------------
  297. void TCustomDialog::AddButtonNextToEdit(TButton * Button, TWinControl * Edit)
  298. {
  299. Button->Parent = GetDefaultParent();
  300. Button->Width = HelpButton->Width;
  301. Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
  302. Edit->Width = Button->Left - Edit->Left - ScaleByTextHeight(this, 6);
  303. Button->Top = Edit->Top - ScaleByTextHeight(this, 1);
  304. ScaleButtonControl(Button);
  305. AddWinControl(Button);
  306. }
  307. //---------------------------------------------------------------------------
  308. void __fastcall TCustomDialog::AddText(TLabel * Label)
  309. {
  310. Label->Parent = GetDefaultParent();
  311. DebugAssert(Label->AutoSize);
  312. Label->WordWrap = true;
  313. Label->Left = FIndent;
  314. Label->Width = GetMaxControlWidth(Label);
  315. Label->Top = FPos;
  316. Label->ShowAccelChar = false;
  317. AutoSizeLabel(Label);
  318. AdjustHeight(Label);
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TCustomDialog::AddText(TStaticText * Label)
  322. {
  323. Label->Parent = GetDefaultParent();
  324. DebugAssert(Label->AutoSize);
  325. Label->Left = FIndent;
  326. Label->Top = FPos;
  327. Label->ShowAccelChar = false;
  328. AdjustHeight(Label);
  329. AddWinControl(Label);
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TCustomDialog::AddSeparator()
  333. {
  334. TBevel * Bevel = new TBevel(this);
  335. Bevel->Parent = GetDefaultParent();
  336. Bevel->Left = FIndent;
  337. Bevel->Top = FPos;
  338. Bevel->Height = 2;
  339. Bevel->Width = GetMaxControlWidth(Bevel);
  340. AdjustHeight(Bevel);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TCustomDialog::StartGroup(const UnicodeString & Caption)
  344. {
  345. if (FGroupBox != NULL)
  346. {
  347. FIndent = FGroupBox->Left;
  348. FPos = FGroupBox->Top + FGroupBox->Height + FControlPadding;
  349. FPrePos = FPos;
  350. FGroupBox = NULL;
  351. }
  352. TGroupBox * GroupBox = new TGroupBox(this);
  353. GroupBox->Parent = this;
  354. GroupBox->Caption = Caption;
  355. GroupBox->Left = FIndent;
  356. GroupBox->Top = FPos;
  357. GroupBox->Height = ScaleByTextHeight(GroupBox, 26);
  358. GroupBox->Width = GetMaxControlWidth(GroupBox);
  359. AdjustHeight(GroupBox);
  360. AddWinControl(GroupBox);
  361. // but if the first control is oneline box, then we should roll back a bit
  362. FPos = ScaleByTextHeight(this, 22);
  363. FPrePos = FPos;
  364. FIndent = FHorizontalMargin + GroupBoxBorderWidth;
  365. FGroupBox = GroupBox;
  366. }
  367. //---------------------------------------------------------------------------
  368. //---------------------------------------------------------------------------
  369. class TSaveSessionDialog : public TCustomDialog
  370. {
  371. public:
  372. __fastcall TSaveSessionDialog(TComponent* AOwner);
  373. void __fastcall Init(bool CanSavePassword, bool NotRecommendedSavingPassword,
  374. TStrings * AdditionalFolders);
  375. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword,
  376. bool & CreateShortcut, const UnicodeString & OriginalSessionName);
  377. protected:
  378. virtual void __fastcall DoValidate();
  379. virtual void __fastcall DoChange(bool & CanSubmit);
  380. private:
  381. UnicodeString FOriginalSessionName;
  382. TEdit * SessionNameEdit;
  383. TComboBox * FolderCombo;
  384. TCheckBox * SavePasswordCheck;
  385. TCheckBox * CreateShortcutCheck;
  386. UnicodeString FRootFolder;
  387. UnicodeString __fastcall GetSessionName();
  388. };
  389. //---------------------------------------------------------------------------
  390. // Need to have an Owner argument for SafeFormCreate
  391. __fastcall TSaveSessionDialog::TSaveSessionDialog(TComponent* /*AOwner*/) :
  392. TCustomDialog(HELP_SESSION_SAVE)
  393. {
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TSaveSessionDialog::Init(bool CanSavePassword,
  397. bool NotRecommendedSavingPassword, TStrings * AdditionalFolders)
  398. {
  399. Caption = LoadStr(SAVE_SESSION_CAPTION);
  400. SessionNameEdit = new TEdit(this);
  401. AddEdit(SessionNameEdit, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  402. FRootFolder = LoadStr(SAVE_SESSION_ROOT_FOLDER2);
  403. std::unique_ptr<TStringList> Folders(new TStringList());
  404. if (AdditionalFolders != NULL)
  405. {
  406. Folders->AddStrings(AdditionalFolders);
  407. }
  408. for (int Index = 0; Index < StoredSessions->Count; Index++)
  409. {
  410. TSessionData * Data = StoredSessions->Sessions[Index];
  411. if (!Data->Special && !Data->IsWorkspace)
  412. {
  413. UnicodeString Folder = Data->FolderName;
  414. if (!Folder.IsEmpty() && Folders->IndexOf(Folder) < 0)
  415. {
  416. Folders->Add(Folder);
  417. }
  418. }
  419. }
  420. DebugAssert(!Folders->CaseSensitive);
  421. Folders->Sort();
  422. FolderCombo = new TUIStateAwareComboBox(this);
  423. AddComboBox(FolderCombo, CreateLabel(LoadStr(SAVE_SESSION_FOLDER)));
  424. FolderCombo->DropDownCount = Max(FolderCombo->DropDownCount, 16);
  425. FolderCombo->Items->Add(FRootFolder);
  426. FolderCombo->Items->AddStrings(Folders.get());
  427. SavePasswordCheck = CreateAndAddCheckBox(
  428. LoadStr(NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  429. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED)));
  430. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  431. EnableControl(SavePasswordCheck, CanSavePassword);
  432. }
  433. //---------------------------------------------------------------------------
  434. bool __fastcall TSaveSessionDialog::Execute(
  435. UnicodeString & SessionName, bool & SavePassword, bool & CreateShortcut,
  436. const UnicodeString & OriginalSessionName)
  437. {
  438. FOriginalSessionName = OriginalSessionName;
  439. SessionNameEdit->Text = TSessionData::ExtractLocalName(SessionName);
  440. UnicodeString Folder = TSessionData::ExtractFolderName(SessionName);
  441. if (Folder.IsEmpty())
  442. {
  443. FolderCombo->Text = FRootFolder;
  444. }
  445. else
  446. {
  447. FolderCombo->Text = Folder;
  448. }
  449. SavePasswordCheck->Checked = SavePassword;
  450. CreateShortcutCheck->Checked = CreateShortcut;
  451. bool Result = TCustomDialog::Execute();
  452. if (Result)
  453. {
  454. SessionName = GetSessionName();
  455. SavePassword = SavePasswordCheck->Checked;
  456. CreateShortcut = CreateShortcutCheck->Checked;
  457. }
  458. return Result;
  459. }
  460. //---------------------------------------------------------------------------
  461. UnicodeString __fastcall TSaveSessionDialog::GetSessionName()
  462. {
  463. UnicodeString Folder;
  464. if (FolderCombo->Text != FRootFolder)
  465. {
  466. Folder = FolderCombo->Text;
  467. }
  468. return TSessionData::ComposePath(Folder, SessionNameEdit->Text);
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TSaveSessionDialog::DoValidate()
  472. {
  473. TSessionData::ValidateName(SessionNameEdit->Text);
  474. SessionNameValidate(GetSessionName(), FOriginalSessionName);
  475. UnicodeString Folder = TSessionData::ExtractFolderName(GetSessionName());
  476. if (!Folder.IsEmpty() && StoredSessions->IsWorkspace(Folder))
  477. {
  478. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (Folder)));
  479. }
  480. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  481. CustomWinConfiguration->UseMasterPassword)
  482. {
  483. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  484. }
  485. TCustomDialog::DoValidate();
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  489. {
  490. CanSubmit = !SessionNameEdit->Text.IsEmpty();
  491. TCustomDialog::DoChange(CanSubmit);
  492. }
  493. //---------------------------------------------------------------------------
  494. TSessionData * __fastcall DoSaveSession(TSessionData * SessionData,
  495. TSessionData * OriginalSession, bool ForceDialog,
  496. TStrings * AdditionalFolders)
  497. {
  498. bool SavePassword = false;
  499. bool * PSavePassword;
  500. bool NotRecommendedSavingPassword =
  501. !CustomWinConfiguration->UseMasterPassword &&
  502. !SameText(SessionData->UserName, AnonymousUserName);
  503. if (Configuration->DisablePasswordStoring ||
  504. !SessionData->HasAnySessionPassword())
  505. {
  506. PSavePassword = NULL;
  507. }
  508. else
  509. {
  510. PSavePassword = &SavePassword;
  511. SavePassword =
  512. ((OriginalSession != NULL) && OriginalSession->HasAnySessionPassword()) ||
  513. !NotRecommendedSavingPassword;
  514. }
  515. UnicodeString SessionName = SessionData->SessionName;
  516. bool Result;
  517. bool CreateShortcut = false;
  518. if (!ForceDialog && ((PSavePassword == NULL) || SavePassword))
  519. {
  520. // This is probably here to ask before session is started saving.
  521. // Otherwise we would ask implicitly, when saving passwords, but at that moment,
  522. // part of the site is already saved and when the user cancel the prompt it's too late.
  523. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  524. Result = true;
  525. }
  526. else
  527. {
  528. // This can be a standalone dialog when used with save URL (from GetLoginData)
  529. TSaveSessionDialog * Dialog = SafeFormCreate<TSaveSessionDialog>();
  530. try
  531. {
  532. Dialog->Init((PSavePassword != NULL), NotRecommendedSavingPassword, AdditionalFolders);
  533. Result = Dialog->Execute(SessionName, SavePassword, CreateShortcut, SessionData->Name);
  534. }
  535. __finally
  536. {
  537. delete Dialog;
  538. }
  539. }
  540. TSessionData * NewSession = NULL;
  541. if (Result)
  542. {
  543. if ((PSavePassword != NULL) && !SavePassword)
  544. {
  545. SessionData->ClearSessionPasswords();
  546. }
  547. NewSession =
  548. StoredSessions->NewSession(SessionName, SessionData);
  549. // modified only, explicit
  550. StoredSessions->Save(false, true);
  551. if (!SessionData->HostKey.IsEmpty())
  552. {
  553. SessionData->CacheHostKeyIfNotCached();
  554. }
  555. if (CreateShortcut)
  556. {
  557. TOperationVisualizer Visualizer;
  558. UnicodeString AdditionalParams =
  559. TProgramParams::FormatSwitch(DESKTOP_SWITCH) + L" " +
  560. TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH);
  561. CreateDesktopSessionShortCut(SessionName, L"", AdditionalParams, -1, SITE_ICON);
  562. }
  563. }
  564. return NewSession;
  565. }
  566. //---------------------------------------------------------------------------
  567. void __fastcall SessionNameValidate(const UnicodeString & Text,
  568. const UnicodeString & OriginalName)
  569. {
  570. TSessionData::ValidatePath(Text);
  571. DebugAssert(StoredSessions);
  572. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  573. if (Data && Data->Special)
  574. {
  575. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  576. qtError, qaOK, HELP_NONE);
  577. Abort();
  578. }
  579. else if ((Data != NULL) && !Data->IsSameName(OriginalName) &&
  580. MessageDialog(MainInstructions(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text))),
  581. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  582. {
  583. Abort();
  584. }
  585. }
  586. //---------------------------------------------------------------------------
  587. //---------------------------------------------------------------------------
  588. class TSaveWorkspaceDialog : public TCustomDialog
  589. {
  590. public:
  591. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  592. bool NotRecommendedSavingPasswords);
  593. bool __fastcall Execute(
  594. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  595. bool & EnableAutoSave);
  596. protected:
  597. virtual void __fastcall DoValidate();
  598. virtual void __fastcall DoChange(bool & CanSubmit);
  599. private:
  600. TComboBox * WorkspaceNameCombo;
  601. TCheckBox * SavePasswordsCheck;
  602. TCheckBox * CreateShortcutCheck;
  603. TCheckBox * EnableAutoSaveCheck;
  604. };
  605. //---------------------------------------------------------------------------
  606. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  607. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  608. TCustomDialog(HELP_WORKSPACE_SAVE)
  609. {
  610. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  611. WorkspaceNameCombo = new TUIStateAwareComboBox(this);
  612. WorkspaceNameCombo->AutoComplete = false;
  613. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  614. WorkspaceNameCombo->DropDownCount = Max(WorkspaceNameCombo->DropDownCount, 16);
  615. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  616. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  617. SavePasswordsCheck = CreateAndAddCheckBox(
  618. LoadStr(NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  619. (CustomWinConfiguration->UseMasterPassword ?
  620. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED)));
  621. EnableControl(SavePasswordsCheck, CanSavePasswords);
  622. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  623. EnableAutoSaveCheck = CreateAndAddCheckBox(LoadStr(SAVE_WORKSPACE_AUTO));
  624. }
  625. //---------------------------------------------------------------------------
  626. bool __fastcall TSaveWorkspaceDialog::Execute(
  627. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  628. bool & EnableAutoSave)
  629. {
  630. WorkspaceNameCombo->Text = WorkspaceName;
  631. SavePasswordsCheck->Checked = SavePasswords;
  632. CreateShortcutCheck->Checked = CreateShortcut;
  633. EnableAutoSaveCheck->Checked = EnableAutoSave;
  634. bool Result = TCustomDialog::Execute();
  635. if (Result)
  636. {
  637. WorkspaceName = WorkspaceNameCombo->Text;
  638. SavePasswords = SavePasswordsCheck->Checked;
  639. CreateShortcut = CreateShortcutCheck->Checked;
  640. EnableAutoSave = EnableAutoSaveCheck->Checked;
  641. }
  642. return Result;
  643. }
  644. //---------------------------------------------------------------------------
  645. void __fastcall TSaveWorkspaceDialog::DoValidate()
  646. {
  647. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  648. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  649. {
  650. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  651. }
  652. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  653. CustomWinConfiguration->UseMasterPassword)
  654. {
  655. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  656. }
  657. TCustomDialog::DoValidate();
  658. }
  659. //---------------------------------------------------------------------------
  660. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  661. {
  662. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  663. TCustomDialog::DoChange(CanSubmit);
  664. }
  665. //---------------------------------------------------------------------------
  666. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  667. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  668. bool & CreateShortcut, bool & EnableAutoSave)
  669. {
  670. std::unique_ptr<TSaveWorkspaceDialog> Dialog(
  671. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  672. bool Dummy = false;
  673. if (SavePasswords == NULL)
  674. {
  675. SavePasswords = &Dummy;
  676. }
  677. return
  678. Dialog->Execute(
  679. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  680. }
  681. //---------------------------------------------------------------------------
  682. //---------------------------------------------------------------------------
  683. class TShortCutDialog : public TCustomDialog
  684. {
  685. public:
  686. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  687. bool __fastcall Execute(TShortCut & ShortCut);
  688. private:
  689. TComboBox * ShortCutCombo;
  690. };
  691. //---------------------------------------------------------------------------
  692. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  693. TCustomDialog(HelpKeyword)
  694. {
  695. Caption = LoadStr(SHORTCUT_CAPTION);
  696. ShortCutCombo = new TUIStateAwareComboBox(this);
  697. AddShortCutComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)), ShortCuts);
  698. }
  699. //---------------------------------------------------------------------------
  700. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  701. {
  702. SetShortCutCombo(ShortCutCombo, ShortCut);
  703. bool Result = TCustomDialog::Execute();
  704. if (Result)
  705. {
  706. ShortCut = GetShortCutCombo(ShortCutCombo);
  707. }
  708. return Result;
  709. }
  710. //---------------------------------------------------------------------------
  711. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  712. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  713. {
  714. bool Result;
  715. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  716. try
  717. {
  718. Result = Dialog->Execute(ShortCut);
  719. }
  720. __finally
  721. {
  722. delete Dialog;
  723. }
  724. return Result;
  725. }
  726. //---------------------------------------------------------------------------
  727. //---------------------------------------------------------------------------
  728. class TRemoteMoveDialog : public TCustomDialog
  729. {
  730. public:
  731. __fastcall TRemoteMoveDialog(bool Multi, TDirectoryExistsEvent OnDirectoryExists);
  732. bool __fastcall Execute(UnicodeString & Target, UnicodeString & FileMask);
  733. protected:
  734. DYNAMIC void __fastcall DoShow();
  735. virtual void __fastcall DoValidate();
  736. UnicodeString __fastcall GetFileMask();
  737. private:
  738. THistoryComboBox * Combo;
  739. bool FMulti;
  740. TDirectoryExistsEvent FOnDirectoryExists;
  741. };
  742. //---------------------------------------------------------------------------
  743. __fastcall TRemoteMoveDialog::TRemoteMoveDialog(bool Multi, TDirectoryExistsEvent OnDirectoryExists) :
  744. TCustomDialog(HELP_REMOTE_MOVE)
  745. {
  746. Caption = LoadStr(REMOTE_MOVE_TITLE);
  747. // The same as TRemoteTransferDialog
  748. ClientWidth = ScaleByTextHeight(this, 466);
  749. FMulti = Multi;
  750. FOnDirectoryExists = OnDirectoryExists;
  751. AddImage(L"Move L to R");
  752. Combo = new THistoryComboBox(this);
  753. Combo->AutoComplete = false;
  754. AddComboBox(Combo, CreateLabel(LoadStr(REMOTE_TRANSFER_PROMPT2)));
  755. }
  756. //---------------------------------------------------------------------------
  757. bool __fastcall TRemoteMoveDialog::Execute(UnicodeString & Target, UnicodeString & FileMask)
  758. {
  759. Combo->Items = CustomWinConfiguration->History[L"RemoteTarget"];
  760. Combo->Text = UnixIncludeTrailingBackslash(Target) + FileMask;
  761. bool Result = TCustomDialog::Execute();
  762. if (Result)
  763. {
  764. Target = UnixExtractFilePath(Combo->Text);
  765. FileMask = GetFileMask();
  766. Combo->SaveToHistory();
  767. CustomWinConfiguration->History[L"RemoteTarget"] = Combo->Items;
  768. }
  769. return Result;
  770. }
  771. //---------------------------------------------------------------------------
  772. UnicodeString __fastcall TRemoteMoveDialog::GetFileMask()
  773. {
  774. return UnixExtractFileName(Combo->Text);
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TRemoteMoveDialog::DoShow()
  778. {
  779. TCustomDialog::DoShow();
  780. InstallPathWordBreakProc(Combo);
  781. }
  782. //---------------------------------------------------------------------------
  783. void __fastcall TRemoteMoveDialog::DoValidate()
  784. {
  785. if (FOnDirectoryExists(NULL, Combo->Text))
  786. {
  787. Combo->Text = UnixCombinePaths(Combo->Text, AnyMask);
  788. }
  789. if (!IsFileNameMask(GetFileMask()) && FMulti)
  790. {
  791. UnicodeString Message =
  792. FormatMultiFilesToOneConfirmation(Combo->Text, true);
  793. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) == qaCancel)
  794. {
  795. Abort();
  796. }
  797. }
  798. TCustomDialog::DoValidate();
  799. }
  800. //---------------------------------------------------------------------------
  801. bool __fastcall DoRemoteMoveDialog(
  802. bool Multi, UnicodeString & Target, UnicodeString & FileMask, TDirectoryExistsEvent OnDirectoryExists)
  803. {
  804. std::unique_ptr<TRemoteMoveDialog> Dialog(new TRemoteMoveDialog(Multi, OnDirectoryExists));
  805. return Dialog->Execute(Target, FileMask);
  806. }
  807. //---------------------------------------------------------------------------
  808. //---------------------------------------------------------------------------
  809. class TCustomCommandOptionsDialog : public TCustomDialog
  810. {
  811. public:
  812. __fastcall TCustomCommandOptionsDialog(
  813. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
  814. TCustomCommand * CustomCommandForOptions, const UnicodeString & Site, const TShortCuts * ShortCuts);
  815. bool __fastcall Execute(TShortCut * ShortCut);
  816. protected:
  817. virtual void __fastcall DoHelp();
  818. DYNAMIC void __fastcall DoShow();
  819. private:
  820. const TCustomCommandType * FCommand;
  821. TStrings * FCustomCommandOptions;
  822. std::vector<TControl *> FControls;
  823. std::vector<std::vector<UnicodeString> > FValues;
  824. unsigned int FFlags;
  825. UnicodeString FSite;
  826. TComboBox * FShortCutCombo;
  827. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  828. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  829. void __fastcall BrowseButtonClick(TObject * Sender);
  830. void __fastcall LinkLabelClick(TObject * Sender);
  831. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  832. void __fastcall AddOptionComboBox(
  833. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  834. std::vector<UnicodeString> & Values);
  835. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  836. int __fastcall GetOptionIndex(TControl * Control);
  837. int __fastcall GetControlIndex(TControl * Control);
  838. };
  839. //---------------------------------------------------------------------------
  840. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  841. const TCustomCommandType * Command, TStrings * CustomCommandOptions,
  842. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  843. const UnicodeString & Site, const TShortCuts * ShortCuts) :
  844. TCustomDialog(HELP_EXTENSION_OPTIONS)
  845. {
  846. FCommand = Command;
  847. FFlags = Flags;
  848. FCustomCommandOptions = CustomCommandOptions;
  849. FSite = Site;
  850. Caption = StripEllipsis(StripHotkey(FCommand->Name));
  851. Width = ScaleByTextHeight(this, 444);
  852. bool HasGroups = false;
  853. int ControlIndex = 0;
  854. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  855. {
  856. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  857. if ((Option.Flags & FFlags) != 0)
  858. {
  859. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  860. UnicodeString Value;
  861. if ((CustomCommandForOptions != NULL) &&
  862. Option.HasPatterns(CustomCommandForOptions))
  863. {
  864. Value = CustomCommandForOptions->Complete(Option.Default, true);
  865. }
  866. else
  867. {
  868. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  869. {
  870. Value = FCustomCommandOptions->Values[OptionKey];
  871. }
  872. else
  873. {
  874. Value = Option.Default;
  875. }
  876. }
  877. int Tag = (OptionIndex << 16) + ControlIndex;
  878. TControl * Control = NULL;
  879. std::vector<UnicodeString> Values;
  880. if (Option.Kind == TCustomCommandType::okUnknown)
  881. {
  882. Control = NULL;
  883. }
  884. else if (Option.Kind == TCustomCommandType::okLabel)
  885. {
  886. TLabel * Label = CreateLabel(Option.Caption);
  887. AddText(Label);
  888. Control = Label;
  889. }
  890. else if (Option.Kind == TCustomCommandType::okLink)
  891. {
  892. TStaticText * Label = new TStaticText(this);
  893. Label->Caption = Option.Caption;
  894. if (IsHttpOrHttpsUrl(Label->Caption))
  895. {
  896. Label->Caption = SecureUrl(Label->Caption);
  897. LinkLabel(Label);
  898. Label->TabStop = true;
  899. }
  900. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  901. {
  902. Label->OnClick = LinkLabelClick;
  903. LinkLabel(Label);
  904. Label->TabStop = true;
  905. }
  906. else
  907. {
  908. // keep it plain text, as we have no URL
  909. }
  910. AddText(Label);
  911. Control = Label;
  912. }
  913. else if (Option.Kind == TCustomCommandType::okGroup)
  914. {
  915. StartGroup(Option.Caption);
  916. HasGroups = true;
  917. }
  918. else if (Option.Kind == TCustomCommandType::okSeparator)
  919. {
  920. AddSeparator();
  921. }
  922. else if (Option.Kind == TCustomCommandType::okTextBox)
  923. {
  924. Control = CreateHistoryComboBox(Option, Value);
  925. }
  926. else if (Option.Kind == TCustomCommandType::okFile)
  927. {
  928. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  929. TButton * Button = CreateButton(this);
  930. AddButtonNextToEdit(Button, ComboBox);
  931. Button->Tag = Tag;
  932. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  933. Button->OnClick = BrowseButtonClick;
  934. Control = ComboBox;
  935. }
  936. else if (Option.Kind == TCustomCommandType::okDropDownList)
  937. {
  938. TComboBox * ComboBox = new TUIStateAwareComboBox(this);
  939. ComboBox->Style = csDropDownList;
  940. AddOptionComboBox(ComboBox, Value, Option, Values);
  941. Control = ComboBox;
  942. }
  943. else if (Option.Kind == TCustomCommandType::okComboBox)
  944. {
  945. TComboBox * ComboBox = new TUIStateAwareComboBox(this);
  946. ComboBox->Style = csDropDown;
  947. AddOptionComboBox(ComboBox, Value, Option, Values);
  948. if (ComboBox->ItemIndex < 0)
  949. {
  950. ComboBox->Text = Value;
  951. }
  952. Control = ComboBox;
  953. }
  954. else if (Option.Kind == TCustomCommandType::okCheckBox)
  955. {
  956. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  957. CheckBox->Checked =
  958. (Option.Params.size() >= 1) &&
  959. (Value == Option.Params[0]);
  960. Control = CheckBox;
  961. }
  962. else
  963. {
  964. DebugFail();
  965. }
  966. if (Control != NULL)
  967. {
  968. Control->Tag = Tag;
  969. }
  970. FControls.push_back(Control);
  971. FValues.push_back(Values);
  972. ControlIndex++;
  973. DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
  974. }
  975. }
  976. if (ShortCuts != NULL)
  977. {
  978. if (HasGroups)
  979. {
  980. StartGroup(LoadStr(EXTENSION_GENERAL_GROUP));
  981. }
  982. else if (ControlIndex > 0)
  983. {
  984. AddSeparator();
  985. }
  986. FShortCutCombo = new TUIStateAwareComboBox(this);
  987. AddShortCutComboBox(FShortCutCombo, CreateLabel(LoadStr(EXTENSION_SHORTCUT)), *ShortCuts);
  988. }
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  992. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  993. {
  994. std::unique_ptr<TStringList> Items(new TStringList());
  995. int ItemIndex = -1;
  996. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  997. while (ParamI != Option.Params.end())
  998. {
  999. UnicodeString Item = (*ParamI);
  1000. int P = Item.Pos(L"=");
  1001. UnicodeString ParamValue;
  1002. if (P > 0)
  1003. {
  1004. ParamValue = Item.SubString(1, P - 1);
  1005. Item.Delete(1, P);
  1006. }
  1007. else
  1008. {
  1009. ParamValue = Item;
  1010. }
  1011. Item = WinConfiguration->ExtensionStringTranslation(FCommand->Id, Item);
  1012. Items->Add(Item);
  1013. if (Value == ParamValue)
  1014. {
  1015. ItemIndex = Items->Count - 1;
  1016. }
  1017. Values.push_back(ParamValue);
  1018. ParamI++;
  1019. }
  1020. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  1021. ComboBox->ItemIndex = ItemIndex;
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  1025. {
  1026. return (Control->Tag >> 16);
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  1030. {
  1031. return (Control->Tag & 0xFFFF);
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  1035. {
  1036. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  1037. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  1038. OpenBrowser(SecureUrl(Option.Default));
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  1042. {
  1043. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  1044. int OptionIndex = GetOptionIndex(Button);
  1045. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1046. int ControlIndex = GetControlIndex(Button);
  1047. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  1048. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  1049. UnicodeString Title;
  1050. if (!Option.FileCaption.IsEmpty())
  1051. {
  1052. Title = Option.FileCaption;
  1053. }
  1054. else
  1055. {
  1056. UnicodeString Caption = Option.Caption;
  1057. Caption = StripHotkey(Caption);
  1058. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  1059. {
  1060. Caption.SetLength(Caption.Length() - 1);
  1061. }
  1062. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  1063. }
  1064. OpenDialog->Title = Title;
  1065. UnicodeString Value;
  1066. if (ComboBox->Text.IsEmpty())
  1067. {
  1068. Value = Option.FileInitial;
  1069. }
  1070. else
  1071. {
  1072. Value = ComboBox->Text;
  1073. }
  1074. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  1075. OpenDialog->FileName = ExpandedValue;
  1076. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  1077. if (!InitialDir.IsEmpty())
  1078. {
  1079. OpenDialog->InitialDir = InitialDir;
  1080. }
  1081. OpenDialog->Filter = Option.FileFilter;
  1082. OpenDialog->DefaultExt = Option.FileExt;
  1083. if (OpenDialog->Execute())
  1084. {
  1085. if (OpenDialog->FileName != ExpandedValue)
  1086. {
  1087. ComboBox->Text = OpenDialog->FileName;
  1088. }
  1089. // If user just confirms the initial value, persist it
  1090. else if (ComboBox->Text.IsEmpty())
  1091. {
  1092. DebugAssert(Option.FileInitial == Value);
  1093. ComboBox->Text = Value;
  1094. }
  1095. }
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1099. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1100. {
  1101. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1102. ComboBox->AutoComplete = false;
  1103. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1104. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1105. ComboBox->Text = Value;
  1106. return ComboBox;
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1110. {
  1111. UnicodeString Result = FCommand->GetOptionKey(Option, FSite);
  1112. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1113. return L"CustomCommandOption_" + Result;
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. bool __fastcall TCustomCommandOptionsDialog::Execute(TShortCut * ShortCut)
  1117. {
  1118. if (ShortCut != NULL)
  1119. {
  1120. SetShortCutCombo(FShortCutCombo, *ShortCut);
  1121. }
  1122. bool Result = TCustomDialog::Execute();
  1123. if (Result)
  1124. {
  1125. int ControlIndex = 0;
  1126. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1127. {
  1128. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1129. if ((Option.Flags & FFlags) != 0)
  1130. {
  1131. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1132. Option.IsControl)
  1133. {
  1134. UnicodeString OptionKey = FCommand->GetOptionKey(Option, FSite);
  1135. TControl * Control = FControls[ControlIndex];
  1136. UnicodeString Value;
  1137. if (Option.Kind == TCustomCommandType::okTextBox)
  1138. {
  1139. Value = SaveHistoryComboBoxValue(Control, Option);
  1140. }
  1141. else if (Option.Kind == TCustomCommandType::okFile)
  1142. {
  1143. Value = SaveHistoryComboBoxValue(Control, Option);
  1144. }
  1145. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1146. {
  1147. Value = GetComboBoxValue(Control, Option.Default);
  1148. }
  1149. else if (Option.Kind == TCustomCommandType::okComboBox)
  1150. {
  1151. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1152. Value = GetComboBoxValue(Control, ComboBox->Text);
  1153. }
  1154. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1155. {
  1156. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1157. int Index = (CheckBox->Checked ? 0 : 1);
  1158. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1159. }
  1160. else
  1161. {
  1162. DebugFail();
  1163. }
  1164. // The default value setter deletes the "name" when the value is empty.
  1165. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1166. SetStringValueEvenIfEmpty(FCustomCommandOptions, OptionKey, Value);
  1167. }
  1168. ControlIndex++;
  1169. }
  1170. }
  1171. if (ShortCut != NULL)
  1172. {
  1173. *ShortCut = GetShortCutCombo(FShortCutCombo);
  1174. }
  1175. }
  1176. return Result;
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1180. TControl * Control, const UnicodeString & Default)
  1181. {
  1182. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1183. UnicodeString Result;
  1184. if (ComboBox->ItemIndex < 0)
  1185. {
  1186. Result = Default;
  1187. }
  1188. else
  1189. {
  1190. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1191. }
  1192. return Result;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1196. TControl * Control, const TCustomCommandType::TOption & Option)
  1197. {
  1198. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1199. ComboBox->SaveToHistory();
  1200. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1201. return ComboBox->Text;
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1205. {
  1206. UnicodeString HelpPage;
  1207. if (!FCommand->OptionsPage.IsEmpty())
  1208. {
  1209. HelpPage = FCommand->OptionsPage;
  1210. }
  1211. else
  1212. {
  1213. HelpPage = FCommand->HomePage;
  1214. }
  1215. if (!HelpPage.IsEmpty())
  1216. {
  1217. OpenBrowser(HelpPage);
  1218. }
  1219. else
  1220. {
  1221. TCustomDialog::DoHelp();
  1222. }
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1226. {
  1227. TCustomDialog::DoShow();
  1228. int ControlIndex = 0;
  1229. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1230. {
  1231. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1232. if ((Option.Flags & FFlags) != 0)
  1233. {
  1234. if (Option.Kind == TCustomCommandType::okFile)
  1235. {
  1236. TControl * Control = FControls[ControlIndex];
  1237. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1238. }
  1239. ControlIndex++;
  1240. }
  1241. }
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. bool __fastcall DoCustomCommandOptionsDialog(
  1245. const TCustomCommandType * Command, TStrings * CustomCommandOptions, TShortCut * ShortCut,
  1246. unsigned int Flags, TCustomCommand * CustomCommandForOptions,
  1247. const UnicodeString & Site, const TShortCuts * ShortCuts)
  1248. {
  1249. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1250. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions, Site, ShortCuts));
  1251. return Dialog->Execute(ShortCut);
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. //---------------------------------------------------------------------------
  1255. class TUsageStatisticsDialog : public TCustomDialog
  1256. {
  1257. public:
  1258. __fastcall TUsageStatisticsDialog();
  1259. protected:
  1260. virtual void __fastcall DoChange(bool & CanSubmit);
  1261. private:
  1262. TEdit * FilterEdit;
  1263. TMemo * UsageMemo;
  1264. TButton * ClipboardButton;
  1265. void __fastcall ClipboardButtonClick(TObject * Sender);
  1266. };
  1267. //---------------------------------------------------------------------------
  1268. __fastcall TUsageStatisticsDialog::TUsageStatisticsDialog() :
  1269. TCustomDialog(HELP_USAGE)
  1270. {
  1271. Caption = LoadStr(USAGE_CAPTION);
  1272. Width = ScaleByTextHeight(this, 444);
  1273. // UnformatMessage is called, because previously, ** markup was used and translations may still contain that
  1274. AddText(CreateLabel(UnformatMessage(LoadStr(USAGE_DATA2))));
  1275. FilterEdit = new TEdit(this);
  1276. FilterEdit->Width = ScaleByTextHeight(this, 277);
  1277. AddEdit(FilterEdit, CreateLabel(LoadStr(USAGE_FILTER)), true);
  1278. UsageMemo = new TMemo(this);
  1279. UsageMemo->Height = ScaleByTextHeight(this, 333);
  1280. UsageMemo->ScrollBars = ssVertical;
  1281. AddEdit(UsageMemo, NULL);
  1282. ReadOnlyControl(UsageMemo);
  1283. ClipboardButton = CreateButton(this);
  1284. ClipboardButton->Caption = LoadStr(USAGE_COPY);
  1285. ClipboardButton->Width = ScaleByTextHeight(this, 179);
  1286. ClipboardButton->OnClick = ClipboardButtonClick;
  1287. AddDialogButton(ClipboardButton);
  1288. RemoveCancelButton();
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TUsageStatisticsDialog::ClipboardButtonClick(TObject * /*Sender*/)
  1292. {
  1293. TInstantOperationVisualizer Visualizer;
  1294. CopyToClipboard(UsageMemo->Lines);
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TUsageStatisticsDialog::DoChange(bool & CanSubmit)
  1298. {
  1299. TCustomDialog::DoChange(CanSubmit);
  1300. UnicodeString Text = Configuration->Usage->Serialize(L"\n", FilterEdit->Text);
  1301. bool NoUsage = Text.IsEmpty();
  1302. ClipboardButton->Enabled = !NoUsage;
  1303. if (NoUsage)
  1304. {
  1305. Text = LoadStr(USAGE_DATA_NONE2);
  1306. }
  1307. UsageMemo->Lines->Text = Text;
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall DoUsageStatisticsDialog()
  1311. {
  1312. std::unique_ptr<TUsageStatisticsDialog> Dialog(new TUsageStatisticsDialog());
  1313. Dialog->Execute();
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. //---------------------------------------------------------------------------
  1317. class TSiteRawDialog : public TCustomDialog
  1318. {
  1319. public:
  1320. __fastcall TSiteRawDialog();
  1321. bool __fastcall Execute(TSessionData * Data);
  1322. protected:
  1323. DYNAMIC void __fastcall DoShow();
  1324. private:
  1325. TMemo * SettingsMemo;
  1326. void __fastcall AddButtonClick(TObject * Sender);
  1327. void __fastcall SettingsMemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift);
  1328. void DeleteNames(TStrings * Names, TStrings * Options);
  1329. };
  1330. //---------------------------------------------------------------------------
  1331. __fastcall TSiteRawDialog::TSiteRawDialog() :
  1332. TCustomDialog(HELP_SITE_RAW)
  1333. {
  1334. Caption = LoadStr(SITE_RAW_CAPTION);
  1335. Width = ScaleByTextHeight(this, 444);
  1336. SettingsMemo = new TMemo(this);
  1337. SettingsMemo->Height = ScaleByTextHeight(this, 333);
  1338. SettingsMemo->OnKeyDown = SettingsMemoKeyDown;
  1339. AddEdit(SettingsMemo, NULL);
  1340. TButton * AddButton = CreateButton(this);
  1341. AddButton->Caption = LoadStr(SITE_RAW_ADD);
  1342. AddButton->Width = OKButton->Width;
  1343. AddButton->OnClick = AddButtonClick;
  1344. AddDialogButton(AddButton);
  1345. }
  1346. //---------------------------------------------------------------------------
  1347. void __fastcall TSiteRawDialog::DoShow()
  1348. {
  1349. TCustomDialog::DoShow();
  1350. InstallPathWordBreakProc(SettingsMemo);
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. bool __fastcall TSiteRawDialog::Execute(TSessionData * Data)
  1354. {
  1355. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  1356. std::unique_ptr<TSessionData> RawData(new TSessionData(L""));
  1357. RawData->Assign(Data);
  1358. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  1359. if (RawData->FSProtocol != fsSFTPonly)
  1360. {
  1361. RawData->FSProtocol = FactoryDefaults->FSProtocol;
  1362. }
  1363. RawData->HostName = FactoryDefaults->HostName;
  1364. RawData->PortNumber = FactoryDefaults->PortNumber;
  1365. RawData->UserName = FactoryDefaults->UserName;
  1366. RawData->Password = FactoryDefaults->Password;
  1367. RawData->Ftps = FactoryDefaults->Ftps;
  1368. std::unique_ptr<TStrings> Options(RawData->SaveToOptions(FactoryDefaults.get(), false, false));
  1369. SettingsMemo->Lines = Options.get();
  1370. bool Result = TCustomDialog::Execute();
  1371. if (Result)
  1372. {
  1373. std::unique_ptr<TSessionData> BackupData(new TSessionData(L""));
  1374. BackupData->Assign(Data);
  1375. Data->DefaultSettings();
  1376. Data->FSProtocol = BackupData->FSProtocol;
  1377. Data->HostName = BackupData->HostName;
  1378. Data->PortNumber = BackupData->PortNumber;
  1379. Data->UserName = BackupData->UserName;
  1380. Data->Password = BackupData->Password;
  1381. Data->Ftps = BackupData->Ftps;
  1382. Data->ApplyRawSettings(SettingsMemo->Lines, false);
  1383. }
  1384. return Result;
  1385. }
  1386. //---------------------------------------------------------------------------
  1387. void __fastcall TSiteRawDialog::SettingsMemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  1388. {
  1389. MemoKeyDown(Sender, Key, Shift);
  1390. }
  1391. //---------------------------------------------------------------------------
  1392. void __fastcall TSiteRawDialog::AddButtonClick(TObject *)
  1393. {
  1394. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  1395. std::unique_ptr<TSessionData> BasicData(new TSessionData(L""));
  1396. BasicData->FSProtocol = TFSProtocol(FactoryDefaults->FSProtocol + 1);
  1397. UnicodeString RandomAppendix(L"_");
  1398. BasicData->HostName = FactoryDefaults->HostName + RandomAppendix;
  1399. BasicData->Ftps = TFtps(FactoryDefaults->Ftps + 1);
  1400. BasicData->PortNumber = BasicData->GetDefaultPort() + 1;
  1401. BasicData->UserName = FactoryDefaults->UserName + RandomAppendix;
  1402. BasicData->Password = FactoryDefaults->Password + RandomAppendix;
  1403. std::unique_ptr<TStrings> BasicOptions(BasicData->SaveToOptions(FactoryDefaults.get(), false, false));
  1404. std::unique_ptr<TStrings> AllOptions(TSessionData::GetAllOptionNames(false));
  1405. std::unique_ptr<TStrings> Names(CreateSortedStringList());
  1406. for (int Index = 0; Index < AllOptions->Count; Index++)
  1407. {
  1408. Names->Add(AllOptions->Names[Index]);
  1409. }
  1410. DeleteNames(Names.get(), BasicOptions.get());
  1411. DeleteNames(Names.get(), SettingsMemo->Lines);
  1412. std::unique_ptr<TCustomDialog> AddDialog(new TCustomDialog(HelpKeyword));
  1413. AddDialog->Caption = LoadStr(SITE_RAW_ADD_CAPTION);
  1414. TComboBox * AddComboBox = new TUIStateAwareComboBox(AddDialog.get());
  1415. AddComboBox->Style = csDropDownList;
  1416. AddComboBox->DropDownCount = Max(AddComboBox->DropDownCount, 16);
  1417. AddDialog->AddComboBox(AddComboBox, CreateLabel(LoadStr(SITE_RAW_ADD_LABEL)), Names.get(), true);
  1418. AddComboBox->ItemIndex = 0;
  1419. if (AddDialog->Execute())
  1420. {
  1421. UnicodeString Name = AddComboBox->Items->Strings[AddComboBox->ItemIndex];
  1422. UnicodeString Value = AllOptions->Values[Name];
  1423. UnicodeString NameAndSeparator = Name + SettingsMemo->Lines->NameValueSeparator;
  1424. int Start = (SettingsMemo->Lines->Text + NameAndSeparator).Length();
  1425. SettingsMemo->Lines->Add(NameAndSeparator + Value);
  1426. SettingsMemo->SetFocus();
  1427. SettingsMemo->SelStart = Start;
  1428. SettingsMemo->SelLength = Value.Length();
  1429. }
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. void TSiteRawDialog::DeleteNames(TStrings * Names, TStrings * Options)
  1433. {
  1434. for (int Index = 0; Index < Options->Count; Index++)
  1435. {
  1436. UnicodeString Name = Options->Names[Index];
  1437. if (!Name.IsEmpty())
  1438. {
  1439. int I = Names->IndexOf(Name);
  1440. if (I >= 0)
  1441. {
  1442. Names->Delete(I);
  1443. }
  1444. }
  1445. }
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __fastcall DoSiteRawDialog(TSessionData * Data)
  1449. {
  1450. std::unique_ptr<TSiteRawDialog> Dialog(new TSiteRawDialog());
  1451. Dialog->Execute(Data);
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. //---------------------------------------------------------------------------
  1455. class TSshHostCADialog : public TCustomDialog
  1456. {
  1457. public:
  1458. TSshHostCADialog(bool Add);
  1459. bool Execute(TSshHostCA & SshHostCA);
  1460. protected:
  1461. virtual void __fastcall DoChange(bool & CanSubmit);
  1462. virtual void __fastcall DoValidate();
  1463. private:
  1464. TEdit * NameEdit;
  1465. TEdit * PublicKeyEdit;
  1466. TEdit * PublicKeyLabel;
  1467. TEdit * ValidityExpressionEdit;
  1468. TCheckBox * PermitRsaSha1Check;
  1469. TCheckBox * PermitRsaSha256Check;
  1470. TCheckBox * PermitRsaSha512Check;
  1471. void __fastcall BrowseButtonClick(TObject * Sender);
  1472. TCheckBox * AddValidityCheckBox(int CaptionStrPart);
  1473. bool ValidatePublicKey(UnicodeString & Status);
  1474. };
  1475. //---------------------------------------------------------------------------
  1476. TSshHostCADialog::TSshHostCADialog(bool Add) :
  1477. TCustomDialog(HELP_SSH_HOST_CA)
  1478. {
  1479. ClientWidth = ScaleByTextHeight(this, 577);
  1480. Caption = LoadStr(Add ? SSH_HOST_CA_ADD : SSH_HOST_CA_EDIT);
  1481. NameEdit = new TEdit(this);
  1482. AddEdit(NameEdit, CreateLabel(LoadStr(SSH_HOST_CA_NAME)));
  1483. PublicKeyEdit = new TEdit(this);
  1484. AddEdit(PublicKeyEdit, CreateLabel(LoadStr(SSH_HOST_CA_PUBLIC_KEY)));
  1485. TButton * BrowseButton = CreateButton(this);
  1486. BrowseButton->Caption = LoadStr(SSH_HOST_CA_BROWSE);
  1487. BrowseButton->OnClick = BrowseButtonClick;
  1488. AddButtonNextToEdit(BrowseButton, PublicKeyEdit);
  1489. NameEdit->Width = PublicKeyEdit->Width;
  1490. PublicKeyLabel = new TEdit(this);
  1491. ReadOnlyControl(PublicKeyLabel);
  1492. PublicKeyLabel->BorderStyle = bsNone;
  1493. PublicKeyLabel->TabStop = false;
  1494. AddEditLikeControl(PublicKeyLabel, NULL);
  1495. ValidityExpressionEdit = new TEdit(this);
  1496. AddEdit(ValidityExpressionEdit, CreateLabel(LoadStr(SSH_HOST_CA_PUBLIC_HOSTS)));
  1497. TLabel * Label = CreateLabel(LoadStr(SSH_HOST_CA_SIGNATURE_TYPES));
  1498. AddText(Label);
  1499. PermitRsaSha1Check = AddValidityCheckBox(1);
  1500. int PermitCheckBoxTop = Label->Top - (PermitRsaSha1Check->Height - Label->Height) / 2;
  1501. PermitRsaSha1Check->Left = OKButton->Left + 1;
  1502. PermitRsaSha1Check->Top = PermitCheckBoxTop;
  1503. PermitRsaSha256Check = AddValidityCheckBox(2);
  1504. PermitRsaSha256Check->Left = CancelButton->Left + 1;
  1505. PermitRsaSha256Check->Top = PermitCheckBoxTop;
  1506. PermitRsaSha512Check = AddValidityCheckBox(3);
  1507. PermitRsaSha512Check->Left = HelpButton->Left + 1;
  1508. PermitRsaSha512Check->Top = PermitCheckBoxTop;
  1509. }
  1510. //---------------------------------------------------------------------------
  1511. TCheckBox * TSshHostCADialog::AddValidityCheckBox(int CaptionStrPart)
  1512. {
  1513. TCheckBox * Result = new TCheckBox(this);
  1514. Result->Parent = this;
  1515. Result->Caption = LoadStrPart(SSH_HOST_CA_SIGNATURES, CaptionStrPart);
  1516. ScaleButtonControl(Result);
  1517. Result->Left = Left;
  1518. Result->OnClick = Change;
  1519. AddWinControl(Result);
  1520. return Result;
  1521. }
  1522. //---------------------------------------------------------------------------
  1523. bool TSshHostCADialog::ValidatePublicKey(UnicodeString & Status)
  1524. {
  1525. bool Result = false;
  1526. UnicodeString PublicKeyText = PublicKeyEdit->Text.Trim();
  1527. if (PublicKeyText.IsEmpty())
  1528. {
  1529. Status = LoadStr(SSH_HOST_CA_NO_KEY);
  1530. }
  1531. else
  1532. {
  1533. RawByteString PublicKeyDummy;
  1534. try
  1535. {
  1536. ParseCertificatePublicKey(PublicKeyText, PublicKeyDummy, Status);
  1537. Result = true;
  1538. }
  1539. catch (Exception & E)
  1540. {
  1541. Status = E.Message;
  1542. }
  1543. }
  1544. return Result;
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. void __fastcall TSshHostCADialog::DoChange(bool & CanSubmit)
  1548. {
  1549. TCustomDialog::DoChange(CanSubmit);
  1550. UnicodeString PublicKeyStatus;
  1551. ValidatePublicKey(PublicKeyStatus);
  1552. // Should drop "SHA256:" prefix, the way we do in TSecureShell::VerifyHostKey
  1553. PublicKeyLabel->Text = PublicKeyStatus;
  1554. CanSubmit = !NameEdit->Text.Trim().IsEmpty() && !PublicKeyEdit->Text.Trim().IsEmpty();
  1555. }
  1556. //---------------------------------------------------------------------
  1557. void __fastcall TSshHostCADialog::DoValidate()
  1558. {
  1559. TCustomDialog::DoValidate();
  1560. UnicodeString PublicKeyStatus;
  1561. if (!ValidatePublicKey(PublicKeyStatus))
  1562. {
  1563. PublicKeyEdit->SetFocus();
  1564. throw Exception(MainInstructions(PublicKeyStatus));
  1565. }
  1566. if (ValidityExpressionEdit->Text.Trim().IsEmpty())
  1567. {
  1568. ValidityExpressionEdit->SetFocus();
  1569. throw Exception(MainInstructions(LoadStr(SSH_HOST_CA_NO_HOSTS)));
  1570. }
  1571. UnicodeString Error;
  1572. int ErrorStart, ErrorLen;
  1573. if (!IsCertificateValidityExpressionValid(ValidityExpressionEdit->Text, Error, ErrorStart, ErrorLen))
  1574. {
  1575. std::unique_ptr<TStrings> MoreMessages(TextToStringList(Error));
  1576. MoreMessageDialog(MainInstructions(LoadStr(SSH_HOST_CA_HOSTS_INVALID)), MoreMessages.get(), qtError, qaOK, HelpKeyword);
  1577. ValidityExpressionEdit->SetFocus();
  1578. ValidityExpressionEdit->SelStart = ErrorStart;
  1579. ValidityExpressionEdit->SelLength = ErrorLen;
  1580. Abort();
  1581. }
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. bool TSshHostCADialog::Execute(TSshHostCA & SshHostCA)
  1585. {
  1586. NameEdit->Text = SshHostCA.Name;
  1587. RawByteString PublicKey = SshHostCA.PublicKey;
  1588. PublicKeyEdit->Text = EncodeStrToBase64(PublicKey);
  1589. ValidityExpressionEdit->Text = SshHostCA.ValidityExpression;
  1590. PermitRsaSha1Check->Checked = SshHostCA.PermitRsaSha1;
  1591. PermitRsaSha256Check->Checked = SshHostCA.PermitRsaSha256;
  1592. PermitRsaSha512Check->Checked = SshHostCA.PermitRsaSha512;
  1593. bool Result = TCustomDialog::Execute();
  1594. if (Result)
  1595. {
  1596. SshHostCA.Name = NameEdit->Text;
  1597. SshHostCA.PublicKey = DecodeBase64ToStr(PublicKeyEdit->Text);
  1598. SshHostCA.ValidityExpression = ValidityExpressionEdit->Text;
  1599. SshHostCA.PermitRsaSha1 = PermitRsaSha1Check->Checked;
  1600. SshHostCA.PermitRsaSha256 = PermitRsaSha256Check->Checked;
  1601. SshHostCA.PermitRsaSha512 = PermitRsaSha512Check->Checked;
  1602. }
  1603. return Result;
  1604. }
  1605. //---------------------------------------------------------------------------
  1606. void __fastcall TSshHostCADialog::BrowseButtonClick(TObject *)
  1607. {
  1608. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  1609. OpenDialog->Title = LoadStr(SSH_HOST_CA_BROWSE_TITLE);
  1610. OpenDialog->Filter = LoadStr(SSH_HOST_CA_BROWSE_FILTER);
  1611. bool Result = OpenDialog->Execute();
  1612. if (Result)
  1613. {
  1614. UnicodeString FileName = OpenDialog->FileName;
  1615. UnicodeString Algorithm, Comment;
  1616. bool HasCertificate;
  1617. RawByteString PublicKey;
  1618. try
  1619. {
  1620. PublicKey = LoadPublicKey(FileName, Algorithm, Comment, HasCertificate);
  1621. }
  1622. catch (Exception & E)
  1623. {
  1624. throw ExtException(&E, MainInstructions(FMTLOAD(SSH_HOST_CA_LOAD_ERROR, (FileName))));
  1625. }
  1626. if (NameEdit->Text.IsEmpty())
  1627. {
  1628. NameEdit->Text = Comment;
  1629. }
  1630. PublicKeyEdit->Text = EncodeStrToBase64(PublicKey);
  1631. }
  1632. }
  1633. //---------------------------------------------------------------------------
  1634. bool DoSshHostCADialog(bool Add, TSshHostCA & SshHostCA)
  1635. {
  1636. std::unique_ptr<TSshHostCADialog> Dialog(new TSshHostCADialog(Add));
  1637. return Dialog->Execute(SshHostCA);
  1638. }
  1639. //---------------------------------------------------------------------------
  1640. //---------------------------------------------------------------------------
  1641. class TTagDialog : public TCustomDialog
  1642. {
  1643. public:
  1644. TTagDialog(bool Add, TStrings * Tags);
  1645. bool Execute(UnicodeString & Key, UnicodeString & Value);
  1646. protected:
  1647. virtual void __fastcall DoChange(bool & CanSubmit);
  1648. virtual void __fastcall DoValidate();
  1649. private:
  1650. TEdit * KeyEdit;
  1651. TEdit * ValueEdit;
  1652. TStrings * FTags;
  1653. void ValidateTag(TEdit * Edit);
  1654. };
  1655. //---------------------------------------------------------------------------
  1656. TTagDialog::TTagDialog(bool Add, TStrings * Tags) :
  1657. TCustomDialog(HELP_TAG)
  1658. {
  1659. Caption = LoadStr(Add ? TAG_ADD : TAG_EDIT);
  1660. FTags = Tags;
  1661. KeyEdit = new TEdit(this);
  1662. KeyEdit->MaxLength = 128;
  1663. AddEdit(KeyEdit, CreateLabel(LoadStr(TAG_KEY)));
  1664. ValueEdit = new TEdit(this);
  1665. ValueEdit->MaxLength = 256;
  1666. AddEdit(ValueEdit, CreateLabel(LoadStr(TAG_VALUE)));
  1667. }
  1668. //---------------------------------------------------------------------------
  1669. bool TTagDialog::Execute(UnicodeString & Key, UnicodeString & Value)
  1670. {
  1671. // if we happen to get values longer than what we believed was possible, allow them.
  1672. KeyEdit->MaxLength = std::max(KeyEdit->MaxLength, Key.Length());
  1673. KeyEdit->Text = Key;
  1674. ValueEdit->MaxLength = std::max(ValueEdit->MaxLength, Value.Length());
  1675. ValueEdit->Text = Value;
  1676. bool Result = TCustomDialog::Execute();
  1677. if (Result)
  1678. {
  1679. Key = KeyEdit->Text;
  1680. Value = ValueEdit->Text;
  1681. }
  1682. return Result;
  1683. }
  1684. //---------------------------------------------------------------------------
  1685. void __fastcall TTagDialog::DoChange(bool & CanSubmit)
  1686. {
  1687. CanSubmit = !KeyEdit->Text.IsEmpty();
  1688. }
  1689. //---------------------------------------------------------------------------
  1690. void TTagDialog::ValidateTag(TEdit * Edit)
  1691. {
  1692. // there are lot more in various scripts
  1693. UnicodeString InvalidChars = L"!\"#$%&'()*,;<>?[\\]^`{|}~¡¢£¤¥¦§¨©«¬­®¯°±´¶·¸»¿×÷";
  1694. UnicodeString Text = Edit->Text;
  1695. for (int Index = 1; Index <= Text.Length(); Index++)
  1696. {
  1697. wchar_t Ch = Text[Index];
  1698. if (TCharacter::IsControl(Ch) ||
  1699. (InvalidChars.Pos(Ch) > 0))
  1700. {
  1701. UnicodeString Message = MainInstructions(FMTLOAD(TAG_INVALID_CHAR, (Ch)));
  1702. if (MoreMessageDialog(Message, NULL, qtWarning, qaIgnore | qaAbort, HelpKeyword) != qaIgnore)
  1703. {
  1704. Edit->SetFocus();
  1705. Abort();
  1706. }
  1707. else
  1708. {
  1709. break;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. void __fastcall TTagDialog::DoValidate()
  1716. {
  1717. UnicodeString Key = KeyEdit->Text;
  1718. if (FTags->IndexOf(Key) >= 0)
  1719. {
  1720. throw Exception(MainInstructions(LoadStr(TAG_NOT_UNIQUE)));
  1721. }
  1722. ValidateTag(KeyEdit);
  1723. ValidateTag(ValueEdit);
  1724. TCustomDialog::DoValidate();
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. bool DoTagDialog(bool Add, TStrings * Tags, UnicodeString & Key, UnicodeString & Value)
  1728. {
  1729. std::unique_ptr<TTagDialog> Dialog(new TTagDialog(Add, Tags));
  1730. return Dialog->Execute(Key, Value);
  1731. }