1
0

Custom.cpp 62 KB

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