Authenticate.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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)
  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::FormShow(TObject * /*Sender*/)
  40. {
  41. AdjustControls();
  42. if (FFocusControl != NULL)
  43. {
  44. FFocusControl->SetFocus();
  45. }
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TAuthenticateForm::ClearLog()
  49. {
  50. // TListItems::Clear() does nothing without allocated handle
  51. LogView->HandleNeeded();
  52. LogView->Items->Clear();
  53. }
  54. //---------------------------------------------------------------------------
  55. void __fastcall TAuthenticateForm::Log(const AnsiString Message)
  56. {
  57. TListItem * Item = LogView->Items->Add();
  58. Item->Caption = Message;
  59. Item->MakeVisible(false);
  60. LogView->Repaint();
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TAuthenticateForm::UpdateControls()
  64. {
  65. PasswordEdit->Password = HideTypingCheck->Checked;
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TAuthenticateForm::AdjustControls()
  69. {
  70. if (PasswordLabel->Caption != FPasswordCaption)
  71. {
  72. int LabelWidth = PasswordLabel->Width;
  73. int LabelHeight = PasswordLabel->Height;
  74. PasswordLabel->AutoSize = false;
  75. PasswordLabel->Caption = FPasswordCaption;
  76. PasswordLabel->AutoSize = true;
  77. PasswordLabel->Width = LabelWidth;
  78. int HeightDiff = (PasswordLabel->Height - LabelHeight);
  79. PasswordEditPanel->Height = PasswordEditPanel->Height + HeightDiff;
  80. PasswordPanel->Height = PasswordPanel->Height + HeightDiff;
  81. }
  82. if (FStatus.IsEmpty())
  83. {
  84. Caption = FSessionName;
  85. }
  86. else
  87. {
  88. Caption = FORMAT("%s - %s", (FStatus, FSessionName));
  89. }
  90. UpdateControls();
  91. }
  92. //---------------------------------------------------------------------------
  93. bool __fastcall TAuthenticateForm::PromptUser(AnsiString Caption,
  94. TPromptKind Kind, AnsiString & Response, bool ForceLog)
  95. {
  96. bool ShowServerPanel;
  97. AnsiString Title;
  98. switch (Kind)
  99. {
  100. case pkPassword:
  101. Title = LoadStr(PASSWORD_TITLE);
  102. HideTypingCheck->Checked = true;
  103. ShowServerPanel = false;
  104. break;
  105. case pkPassphrase:
  106. Title = LoadStr(PASSPHRASE_TITLE);
  107. HideTypingCheck->Checked = true;
  108. ShowServerPanel = false;
  109. break;
  110. case pkServerPrompt:
  111. Title = LoadStr(SERVER_PASSWORD_TITLE);
  112. ShowServerPanel = true;
  113. break;
  114. case pkPrompt:
  115. Title = CutToChar(Caption, '|', true);
  116. if (Caption.IsEmpty())
  117. {
  118. Caption = Title;
  119. }
  120. HideTypingCheck->Checked = false;
  121. ShowServerPanel = false;
  122. break;
  123. default:
  124. assert(false);
  125. }
  126. FPasswordCaption = Caption;
  127. if (ShowServerPanel != ServerPromptPanel->Visible)
  128. {
  129. ServerPromptPanel->Visible = ShowServerPanel;
  130. PasswordPanel->Height += (ShowServerPanel ? 1 : -1) * ServerPromptPanel->Height;
  131. }
  132. PasswordEdit->Text = Response;
  133. bool Result = Execute(Title, PasswordPanel, PasswordEdit,
  134. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  135. if (Result)
  136. {
  137. Response = PasswordEdit->Text;
  138. }
  139. return Result;
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TAuthenticateForm::Banner(const AnsiString & Banner,
  143. bool & NeverShowAgain, int Options)
  144. {
  145. BannerMemo->Lines->Text = Banner;
  146. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  147. NeverShowAgainCheck->Checked = NeverShowAgain;
  148. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  149. BannerCloseButton, BannerCloseButton, false, true, false);
  150. if (Result)
  151. {
  152. NeverShowAgain = NeverShowAgainCheck->Checked;
  153. }
  154. }
  155. //---------------------------------------------------------------------------
  156. bool __fastcall TAuthenticateForm::Execute(AnsiString Status, TControl * Control,
  157. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  158. bool FixHeight, bool Zoom, bool ForceLog)
  159. {
  160. TAlign Align = Control->Align;
  161. try
  162. {
  163. assert(FStatus.IsEmpty());
  164. FStatus = Status;
  165. DefaultButton->Default = true;
  166. CancelButton->Cancel = true;
  167. if (Zoom)
  168. {
  169. Control->Align = alClient;
  170. }
  171. if (ForceLog || Visible)
  172. {
  173. Control->Show();
  174. TCursor PrevCursor = Screen->Cursor;
  175. try
  176. {
  177. if (Zoom)
  178. {
  179. LogView->Hide();
  180. }
  181. else
  182. {
  183. if (LogView->Items->Count > 0)
  184. {
  185. TListItem * Item = LogView->ItemFocused;
  186. if (Item == NULL)
  187. {
  188. Item = LogView->Items->Item[LogView->Items->Count - 1];
  189. }
  190. Item->MakeVisible(false);
  191. }
  192. }
  193. Screen->Cursor = crDefault;
  194. if (!Visible)
  195. {
  196. assert(ForceLog);
  197. ShowAsModal();
  198. }
  199. FocusControl->SetFocus();
  200. ModalResult = mrNone;
  201. AdjustControls();
  202. do
  203. {
  204. Application->HandleMessage();
  205. }
  206. while (!Application->Terminated && (ModalResult == mrNone));
  207. }
  208. __finally
  209. {
  210. Control->Hide();
  211. Screen->Cursor = PrevCursor;
  212. if (Zoom)
  213. {
  214. LogView->Show();
  215. }
  216. Repaint();
  217. }
  218. }
  219. else
  220. {
  221. int PrevHeight = ClientHeight;
  222. int PrevMinHeight = Constraints->MinHeight;
  223. int PrevMaxHeight = Constraints->MaxHeight;
  224. try
  225. {
  226. Constraints->MinHeight = 0;
  227. ClientHeight = Control->Height;
  228. if (FixHeight)
  229. {
  230. Constraints->MinHeight = Height;
  231. Constraints->MaxHeight = Height;
  232. }
  233. LogView->Hide();
  234. Control->Show();
  235. FFocusControl = FocusControl;
  236. ShowModal();
  237. }
  238. __finally
  239. {
  240. FFocusControl = NULL;
  241. ClientHeight = PrevHeight;
  242. Constraints->MinHeight = PrevMinHeight;
  243. Constraints->MaxHeight = PrevMaxHeight;
  244. Control->Hide();
  245. LogView->Show();
  246. }
  247. }
  248. }
  249. __finally
  250. {
  251. Control->Align = Align;
  252. DefaultButton->Default = false;
  253. CancelButton->Cancel = false;
  254. FStatus = "";
  255. AdjustControls();
  256. }
  257. return (ModalResult != mrCancel);
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  261. {
  262. AdjustControls();
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  266. {
  267. FormHelp(this);
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TAuthenticateForm::HideTypingCheckClick(TObject * /*Sender*/)
  271. {
  272. UpdateControls();
  273. }
  274. //---------------------------------------------------------------------------