Authenticate.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Authenticate.h"
  6. #include <VCLCommon.h>
  7. #include <TextsWin.h>
  8. #include <Terminal.h>
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "PasswordEdit"
  12. #pragma resource "*.dfm"
  13. //---------------------------------------------------------------------------
  14. __fastcall TAuthenticateForm::TAuthenticateForm(TComponent * Owner,
  15. AnsiString SessionName)
  16. : TForm(Owner), FSessionName(SessionName), FHideTypingOnServerPrompt(true)
  17. {
  18. UseSystemSettings(this);
  19. FShowAsModalStorage = NULL;
  20. FFocusControl = NULL;
  21. ClearLog();
  22. }
  23. //---------------------------------------------------------------------------
  24. __fastcall TAuthenticateForm::~TAuthenticateForm()
  25. {
  26. ReleaseAsModal(this, FShowAsModalStorage);
  27. }
  28. //---------------------------------------------------------------------------
  29. void __fastcall TAuthenticateForm::ShowAsModal()
  30. {
  31. ::ShowAsModal(this, FShowAsModalStorage);
  32. }
  33. //---------------------------------------------------------------------------
  34. void __fastcall TAuthenticateForm::HideAsModal()
  35. {
  36. ::HideAsModal(this, FShowAsModalStorage);
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  40. {
  41. // bypass TForm::WMNCCreate no prevent disabling "resize"
  42. // (wish is done for bsDialog, see comments in CreateParams)
  43. DefaultHandler(&Message);
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  47. {
  48. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  49. if (Message.Msg == WM_NCCREATE)
  50. {
  51. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  52. }
  53. else
  54. {
  55. TForm::Dispatch(AMessage);
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  60. {
  61. TForm::CreateParams(Params);
  62. // Allow resizing of the window, even if this is bsDialog.
  63. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  64. // reason receive focus, if window is shown atop non-main window
  65. // (like editor)
  66. Params.Style = Params.Style | WS_THICKFRAME;
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  70. {
  71. AdjustControls();
  72. if (FFocusControl != NULL)
  73. {
  74. FFocusControl->SetFocus();
  75. }
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TAuthenticateForm::ClearLog()
  79. {
  80. // TListItems::Clear() does nothing without allocated handle
  81. LogView->HandleNeeded();
  82. LogView->Items->Clear();
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TAuthenticateForm::Log(const AnsiString Message)
  86. {
  87. TListItem * Item = LogView->Items->Add();
  88. Item->Caption = Message;
  89. Item->MakeVisible(false);
  90. LogView->Repaint();
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TAuthenticateForm::UpdateControls()
  94. {
  95. PasswordEdit->Password = HideTypingCheck->Checked;
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TAuthenticateForm::AdjustControls()
  99. {
  100. if (PasswordLabel->Caption != FPasswordCaption)
  101. {
  102. int LabelWidth = PasswordLabel->Width;
  103. int LabelHeight = PasswordLabel->Height;
  104. PasswordLabel->AutoSize = false;
  105. PasswordLabel->Caption = FPasswordCaption;
  106. PasswordLabel->AutoSize = true;
  107. PasswordLabel->Width = LabelWidth;
  108. int HeightDiff = (PasswordLabel->Height - LabelHeight);
  109. PasswordEditPanel->Height = PasswordEditPanel->Height + HeightDiff;
  110. PasswordPanel->Height = PasswordPanel->Height + HeightDiff;
  111. }
  112. if (FStatus.IsEmpty())
  113. {
  114. Caption = FSessionName;
  115. }
  116. else
  117. {
  118. Caption = FORMAT("%s - %s", (FStatus, FSessionName));
  119. }
  120. UpdateControls();
  121. }
  122. //---------------------------------------------------------------------------
  123. bool __fastcall TAuthenticateForm::PromptUser(AnsiString Caption,
  124. TPromptKind Kind, AnsiString & Response, bool ForceLog)
  125. {
  126. bool ShowServerPanel;
  127. AnsiString Title;
  128. switch (Kind)
  129. {
  130. case pkPassword:
  131. Title = LoadStr(PASSWORD_TITLE);
  132. HideTypingCheck->Checked = true;
  133. ShowServerPanel = false;
  134. break;
  135. case pkPassphrase:
  136. Title = LoadStr(PASSPHRASE_TITLE);
  137. HideTypingCheck->Checked = true;
  138. ShowServerPanel = false;
  139. break;
  140. case pkServerPrompt:
  141. Title = LoadStr(SERVER_PASSWORD_TITLE);
  142. ShowServerPanel = true;
  143. HideTypingCheck->Checked = FHideTypingOnServerPrompt;
  144. break;
  145. case pkPrompt:
  146. Title = CutToChar(Caption, '|', true);
  147. if (Caption.IsEmpty())
  148. {
  149. Caption = Title;
  150. }
  151. HideTypingCheck->Checked = false;
  152. ShowServerPanel = false;
  153. break;
  154. default:
  155. assert(false);
  156. }
  157. FPasswordCaption = Caption;
  158. if (ShowServerPanel != ServerPromptPanel->Visible)
  159. {
  160. ServerPromptPanel->Visible = ShowServerPanel;
  161. PasswordPanel->Height += (ShowServerPanel ? 1 : -1) * ServerPromptPanel->Height;
  162. }
  163. PasswordEdit->Text = Response;
  164. bool Result = Execute(Title, PasswordPanel, PasswordEdit,
  165. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  166. if (Result)
  167. {
  168. Response = PasswordEdit->Text;
  169. }
  170. if (Kind == pkServerPrompt)
  171. {
  172. FHideTypingOnServerPrompt = HideTypingCheck->Checked;
  173. }
  174. return Result;
  175. }
  176. //---------------------------------------------------------------------------
  177. void __fastcall TAuthenticateForm::Banner(const AnsiString & Banner,
  178. bool & NeverShowAgain, int Options)
  179. {
  180. BannerMemo->Lines->Text = Banner;
  181. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  182. NeverShowAgainCheck->Checked = NeverShowAgain;
  183. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  184. BannerCloseButton, BannerCloseButton, false, true, false);
  185. if (Result)
  186. {
  187. NeverShowAgain = NeverShowAgainCheck->Checked;
  188. }
  189. }
  190. //---------------------------------------------------------------------------
  191. bool __fastcall TAuthenticateForm::Execute(AnsiString Status, TControl * Control,
  192. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  193. bool FixHeight, bool Zoom, bool ForceLog)
  194. {
  195. TAlign Align = Control->Align;
  196. try
  197. {
  198. assert(FStatus.IsEmpty());
  199. FStatus = Status;
  200. DefaultButton->Default = true;
  201. CancelButton->Cancel = true;
  202. if (Zoom)
  203. {
  204. Control->Align = alClient;
  205. }
  206. if (ForceLog || Visible)
  207. {
  208. Control->Show();
  209. TCursor PrevCursor = Screen->Cursor;
  210. try
  211. {
  212. if (Zoom)
  213. {
  214. LogView->Hide();
  215. }
  216. else
  217. {
  218. if (LogView->Items->Count > 0)
  219. {
  220. TListItem * Item = LogView->ItemFocused;
  221. if (Item == NULL)
  222. {
  223. Item = LogView->Items->Item[LogView->Items->Count - 1];
  224. }
  225. Item->MakeVisible(false);
  226. }
  227. }
  228. Screen->Cursor = crDefault;
  229. if (!Visible)
  230. {
  231. assert(ForceLog);
  232. ShowAsModal();
  233. }
  234. FocusControl->SetFocus();
  235. ModalResult = mrNone;
  236. AdjustControls();
  237. do
  238. {
  239. Application->HandleMessage();
  240. }
  241. while (!Application->Terminated && (ModalResult == mrNone));
  242. }
  243. __finally
  244. {
  245. Control->Hide();
  246. Screen->Cursor = PrevCursor;
  247. if (Zoom)
  248. {
  249. LogView->Show();
  250. }
  251. Repaint();
  252. }
  253. }
  254. else
  255. {
  256. int PrevHeight = ClientHeight;
  257. int PrevMinHeight = Constraints->MinHeight;
  258. int PrevMaxHeight = Constraints->MaxHeight;
  259. try
  260. {
  261. Constraints->MinHeight = 0;
  262. ClientHeight = Control->Height;
  263. if (FixHeight)
  264. {
  265. Constraints->MinHeight = Height;
  266. Constraints->MaxHeight = Height;
  267. }
  268. LogView->Hide();
  269. Control->Show();
  270. FFocusControl = FocusControl;
  271. ShowModal();
  272. }
  273. __finally
  274. {
  275. FFocusControl = NULL;
  276. ClientHeight = PrevHeight;
  277. Constraints->MinHeight = PrevMinHeight;
  278. Constraints->MaxHeight = PrevMaxHeight;
  279. Control->Hide();
  280. LogView->Show();
  281. }
  282. }
  283. }
  284. __finally
  285. {
  286. Control->Align = Align;
  287. DefaultButton->Default = false;
  288. CancelButton->Cancel = false;
  289. FStatus = "";
  290. AdjustControls();
  291. }
  292. return (ModalResult != mrCancel);
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  296. {
  297. AdjustControls();
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  301. {
  302. FormHelp(this);
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TAuthenticateForm::HideTypingCheckClick(TObject * /*Sender*/)
  306. {
  307. UpdateControls();
  308. }
  309. //---------------------------------------------------------------------------