Authenticate.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <SecureShell.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. }
  22. //---------------------------------------------------------------------------
  23. __fastcall TAuthenticateForm::~TAuthenticateForm()
  24. {
  25. ReleaseAsModal(this, FShowAsModalStorage);
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TAuthenticateForm::ShowAsModal()
  29. {
  30. ::ShowAsModal(this, FShowAsModalStorage);
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TAuthenticateForm::HideAsModal()
  34. {
  35. ::HideAsModal(this, FShowAsModalStorage);
  36. }
  37. //---------------------------------------------------------------------------
  38. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  39. {
  40. ClearLog();
  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::ChangeStatus(const AnsiString Status)
  64. {
  65. Log(Status);
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TAuthenticateForm::UpdateControls()
  69. {
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TAuthenticateForm::AdjustControls()
  73. {
  74. AnsiString PasswordCaption = PasswordLabel->Hint;
  75. bool MultiLine = false;
  76. int P = PasswordCaption.Pos("\n");
  77. if (P > 0)
  78. {
  79. MultiLine = true;
  80. PasswordCaption.SetLength(P - 1);
  81. }
  82. P = PasswordCaption.Pos("\r");
  83. if (P > 0)
  84. {
  85. MultiLine = true;
  86. PasswordCaption.SetLength(P - 1);
  87. }
  88. bool NeedTrim;
  89. TControlCanvas * PasswordLabelCanvas = new TControlCanvas();
  90. try
  91. {
  92. PasswordLabelCanvas->Control = PasswordLabel;
  93. NeedTrim = MultiLine ||
  94. (PasswordLabelCanvas->TextWidth(PasswordCaption) > PasswordLabel->Width);
  95. if (NeedTrim)
  96. {
  97. static AnsiString Ellipsis(" ...");
  98. while (PasswordLabelCanvas->TextWidth(PasswordCaption + Ellipsis) >
  99. PasswordLabel->Width)
  100. {
  101. PasswordCaption.SetLength(PasswordCaption.Length() - 1);
  102. }
  103. PasswordCaption = PasswordCaption + Ellipsis;
  104. }
  105. }
  106. __finally
  107. {
  108. delete PasswordLabelCanvas;
  109. }
  110. PasswordLabel->Caption = PasswordCaption;
  111. if (NeedTrim)
  112. {
  113. HintLabel(PasswordLabel, Hint);
  114. PasswordLabel->TabStop = true;
  115. }
  116. else
  117. {
  118. HintLabelRestore(PasswordLabel);
  119. PasswordLabel->TabStop = false;
  120. }
  121. if (FStatus.IsEmpty())
  122. {
  123. Caption = FSessionName;
  124. }
  125. else
  126. {
  127. Caption = FORMAT("%s - %s", (FStatus, FSessionName));
  128. }
  129. }
  130. //---------------------------------------------------------------------------
  131. bool __fastcall TAuthenticateForm::PromptUser(AnsiString Caption,
  132. TPromptKind Kind, AnsiString &Password)
  133. {
  134. PasswordLabel->Hint = Caption;
  135. int Title;
  136. switch (Kind)
  137. {
  138. case pkPassword: Title = PASSWORD_TITLE; break;
  139. case pkPassphrase: Title = PASSPHRASE_TITLE; break;
  140. case pkServerPrompt: Title = SERVER_PASSWORD_TITLE; break;
  141. default: assert(false);
  142. }
  143. bool ShowServerPanel = (Kind == pkServerPrompt);
  144. if (ShowServerPanel != ServerPromptPanel->Visible)
  145. {
  146. ServerPromptPanel->Visible = ShowServerPanel;
  147. PasswordPanel->Height += (ShowServerPanel ? 1 : -1) * ServerPromptPanel->Height;
  148. }
  149. PasswordEdit->Text = Password;
  150. bool Result = Execute(LoadStr(Title), PasswordPanel, PasswordEdit,
  151. PasswordOKButton, PasswordCancelButton, true, false);
  152. if (Result)
  153. {
  154. Password = PasswordEdit->Text;
  155. }
  156. return Result;
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TAuthenticateForm::Banner(const AnsiString & Banner,
  160. bool & NeverShowAgain, int Options)
  161. {
  162. BannerMemo->Lines->Text = Banner;
  163. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  164. NeverShowAgainCheck->Checked = NeverShowAgain;
  165. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  166. BannerCloseButton, BannerCloseButton, false, true);
  167. if (Result)
  168. {
  169. NeverShowAgain = NeverShowAgainCheck->Checked;
  170. }
  171. }
  172. //---------------------------------------------------------------------------
  173. bool __fastcall TAuthenticateForm::Execute(AnsiString Status, TControl * Control,
  174. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  175. bool FixHeight, bool Zoom)
  176. {
  177. TAlign Align = Control->Align;
  178. try
  179. {
  180. assert(FStatus.IsEmpty());
  181. FStatus = Status;
  182. DefaultButton->Default = true;
  183. CancelButton->Cancel = true;
  184. if (Zoom)
  185. {
  186. Control->Align = alClient;
  187. }
  188. if (Visible)
  189. {
  190. Control->Show();
  191. TCursor PrevCursor = Screen->Cursor;
  192. try
  193. {
  194. if (Zoom)
  195. {
  196. LogView->Hide();
  197. }
  198. else
  199. {
  200. if (LogView->Items->Count > 0)
  201. {
  202. TListItem * Item = LogView->ItemFocused;
  203. if (Item == NULL)
  204. {
  205. Item = LogView->Items->Item[LogView->Items->Count - 1];
  206. }
  207. Item->MakeVisible(false);
  208. }
  209. }
  210. Screen->Cursor = crDefault;
  211. FocusControl->SetFocus();
  212. ModalResult = mrNone;
  213. AdjustControls();
  214. do
  215. {
  216. Application->ProcessMessages();
  217. }
  218. while (!Application->Terminated && (ModalResult == mrNone));
  219. }
  220. __finally
  221. {
  222. Control->Hide();
  223. Screen->Cursor = PrevCursor;
  224. if (Zoom)
  225. {
  226. LogView->Show();
  227. }
  228. Repaint();
  229. }
  230. }
  231. else
  232. {
  233. int PrevHeight = ClientHeight;
  234. int PrevMinHeight = Constraints->MinHeight;
  235. int PrevMaxHeight = Constraints->MaxHeight;
  236. try
  237. {
  238. Constraints->MinHeight = 0;
  239. ClientHeight = Control->Height;
  240. if (FixHeight)
  241. {
  242. Constraints->MinHeight = Height;
  243. Constraints->MaxHeight = Height;
  244. }
  245. LogView->Hide();
  246. Control->Show();
  247. FFocusControl = FocusControl;
  248. ShowModal();
  249. }
  250. __finally
  251. {
  252. FFocusControl = NULL;
  253. ClientHeight = PrevHeight;
  254. Constraints->MinHeight = PrevMinHeight;
  255. Constraints->MaxHeight = PrevMaxHeight;
  256. Control->Hide();
  257. LogView->Show();
  258. }
  259. }
  260. }
  261. __finally
  262. {
  263. Control->Align = Align;
  264. DefaultButton->Default = false;
  265. CancelButton->Cancel = false;
  266. FStatus = "";
  267. AdjustControls();
  268. }
  269. return (ModalResult != mrCancel);
  270. }
  271. //---------------------------------------------------------------------------
  272. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  273. {
  274. AdjustControls();
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  278. {
  279. FormHelp(this);
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall TAuthenticateForm::HideTypingCheckClick(TObject * /*Sender*/)
  283. {
  284. PasswordEdit->Password = HideTypingCheck->Checked;
  285. }
  286. //---------------------------------------------------------------------------