Authenticate.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #include <CoreMain.h>
  10. #include <PasTools.hpp>
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma link "PasswordEdit"
  14. #ifndef NO_RESOURCES
  15. #pragma resource "*.dfm"
  16. #endif
  17. //---------------------------------------------------------------------------
  18. __fastcall TAuthenticateForm::TAuthenticateForm(TComponent * Owner)
  19. : TForm(Owner), FSessionData(NULL), FTerminal(NULL)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TAuthenticateForm::Init(TTerminal * Terminal)
  24. {
  25. FTerminal = Terminal;
  26. FSessionData = Terminal->SessionData;
  27. UseSystemSettings(this);
  28. FShowAsModalStorage = NULL;
  29. FFocusControl = NULL;
  30. UseDesktopFont(LogView);
  31. FPromptParent = InstructionsLabel->Parent;
  32. FPromptLeft = InstructionsLabel->Left;
  33. FPromptTop = InstructionsLabel->Top;
  34. FPromptRight = FPromptParent->ClientWidth - InstructionsLabel->Width - FPromptLeft;
  35. FPromptEditGap = PromptEdit1->Top - PromptLabel1->Top - PromptLabel1->Height;
  36. FPromptsGap = PromptLabel2->Top - PromptEdit1->Top - PromptEdit1->Height;
  37. ClientHeight = ScaleByTextHeight(this, 270);
  38. ClearLog();
  39. }
  40. //---------------------------------------------------------------------------
  41. __fastcall TAuthenticateForm::~TAuthenticateForm()
  42. {
  43. ReleaseAsModal(this, FShowAsModalStorage);
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TAuthenticateForm::ShowAsModal()
  47. {
  48. ::ShowAsModal(this, FShowAsModalStorage);
  49. }
  50. //---------------------------------------------------------------------------
  51. void __fastcall TAuthenticateForm::HideAsModal()
  52. {
  53. ::HideAsModal(this, FShowAsModalStorage);
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  57. {
  58. // bypass TForm::WMNCCreate to prevent disabling "resize"
  59. // (which is done for bsDialog, see comments in CreateParams)
  60. DefaultHandler(&Message);
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TAuthenticateForm::DoCancel()
  64. {
  65. if (FOnCancel != NULL)
  66. {
  67. FOnCancel(this);
  68. }
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  72. {
  73. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  74. if (Message.Msg == WM_NCCREATE)
  75. {
  76. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  77. }
  78. else if (Message.Msg == WM_CLOSE)
  79. {
  80. DoCancel();
  81. TForm::Dispatch(AMessage);
  82. }
  83. else
  84. {
  85. TForm::Dispatch(AMessage);
  86. }
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  90. {
  91. TForm::CreateParams(Params);
  92. // Allow resizing of the window, even if this is bsDialog.
  93. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  94. // reason receive focus, if window is shown atop non-main window
  95. // (like editor)
  96. Params.Style = Params.Style | WS_THICKFRAME;
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  100. {
  101. AdjustControls();
  102. if (FFocusControl != NULL)
  103. {
  104. ActiveControl = FFocusControl;
  105. }
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TAuthenticateForm::ClearLog()
  109. {
  110. // TListItems::Clear() does nothing without allocated handle
  111. LogView->HandleNeeded();
  112. LogView->Items->Clear();
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TAuthenticateForm::Log(const UnicodeString Message)
  116. {
  117. TListItem * Item = LogView->Items->Add();
  118. Item->Caption = Message;
  119. Item->MakeVisible(false);
  120. AdjustLogView();
  121. LogView->Repaint();
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TAuthenticateForm::AdjustControls()
  125. {
  126. if (FStatus.IsEmpty())
  127. {
  128. Caption = FSessionData->SessionName;
  129. }
  130. else
  131. {
  132. Caption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  133. }
  134. }
  135. //---------------------------------------------------------------------------
  136. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  137. {
  138. TLabel * Result = new TLabel(this);
  139. Result->Parent = FPromptParent;
  140. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  141. Result->WordWrap = true;
  142. Result->AutoSize = false;
  143. Result->Top = Current;
  144. Result->Left = FPromptLeft;
  145. Result->Caption = Caption;
  146. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  147. Result->Width = Width;
  148. Result->AutoSize = true;
  149. return Result;
  150. }
  151. //---------------------------------------------------------------------------
  152. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  153. {
  154. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  155. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  156. Result->Parent = FPromptParent;
  157. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  158. Result->Top = Current;
  159. Result->Left = FPromptLeft;
  160. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  161. return Result;
  162. }
  163. //---------------------------------------------------------------------------
  164. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  165. TStrings * Prompts)
  166. {
  167. while (FPromptParent->ControlCount > 0)
  168. {
  169. delete FPromptParent->Controls[0];
  170. }
  171. TList * Result = new TList;
  172. int Current = FPromptTop;
  173. if (!Instructions.IsEmpty())
  174. {
  175. TLabel * Label = GenerateLabel(Current, Instructions);
  176. Current += Label->Height + FPromptsGap;
  177. }
  178. for (int Index = 0; Index < Prompts->Count; Index++)
  179. {
  180. if (Index > 0)
  181. {
  182. Current += FPromptEditGap;
  183. }
  184. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  185. Current += Label->Height + FPromptEditGap;
  186. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  187. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  188. Result->Add(Edit);
  189. Label->FocusControl = Edit;
  190. Current += Edit->Height;
  191. }
  192. FPromptParent->ClientHeight = Current;
  193. return Result;
  194. }
  195. //---------------------------------------------------------------------------
  196. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  197. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  198. bool StoredCredentialsTried)
  199. {
  200. bool Result;
  201. TList * Edits = GeneratePrompt(Instructions, Prompts);
  202. try
  203. {
  204. bool ShowSessionRememberPasswordPanel = false;
  205. bool ShowSavePasswordPanel = false;
  206. TSessionData * Data = NULL;
  207. bool PasswordPrompt = IsPasswordPrompt(Kind, Prompts);
  208. if (PasswordPrompt && StoredCredentialsTried)
  209. {
  210. Data = StoredSessions->FindSame(FSessionData);
  211. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  212. }
  213. // do not offer to rememeber password,
  214. // if we are offering to save the password to stored session
  215. if (!ShowSavePasswordPanel &&
  216. (Prompts->Count == 1) &&
  217. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  218. ALWAYS_TRUE(PasswordPrompt))
  219. {
  220. ShowSessionRememberPasswordPanel = true;
  221. }
  222. SavePasswordCheck->Checked = false;
  223. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  224. SessionRememberPasswordCheck->Checked = false;
  225. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  226. if (PasswordPanel->AutoSize)
  227. {
  228. PasswordPanel->AutoSize = false;
  229. PasswordPanel->AutoSize = true;
  230. }
  231. PasswordPanel->Realign();
  232. assert(Results->Count == Edits->Count);
  233. for (int Index = 0; Index < Edits->Count; Index++)
  234. {
  235. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  236. Edit->Text = Results->Strings[Index];
  237. }
  238. Result = Execute(Name, PasswordPanel,
  239. ((Edits->Count > 0) ?
  240. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  241. static_cast<TWinControl *>(PasswordOKButton)),
  242. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  243. if (Result)
  244. {
  245. for (int Index = 0; Index < Edits->Count; Index++)
  246. {
  247. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  248. Results->Strings[Index] = Edit->Text;
  249. Prompts->Objects[Index] = (TObject *)
  250. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  251. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  252. }
  253. if (SavePasswordCheck->Checked)
  254. {
  255. assert(Data != NULL);
  256. assert(Results->Count >= 1);
  257. FSessionData->Password = Results->Strings[0];
  258. Data->Password = Results->Strings[0];
  259. // modified only, explicit
  260. StoredSessions->Save(false, true);
  261. }
  262. }
  263. }
  264. __finally
  265. {
  266. delete Edits;
  267. }
  268. return Result;
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  272. bool & NeverShowAgain, int Options)
  273. {
  274. BannerMemo->Lines->Text = Banner;
  275. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  276. NeverShowAgainCheck->Checked = NeverShowAgain;
  277. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  278. BannerCloseButton, BannerCloseButton, false, true, false);
  279. if (Result)
  280. {
  281. NeverShowAgain = NeverShowAgainCheck->Checked;
  282. }
  283. }
  284. //---------------------------------------------------------------------------
  285. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  286. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  287. bool FixHeight, bool Zoom, bool ForceLog)
  288. {
  289. TModalResult DefaultButtonResult;
  290. TAlign Align = Panel->Align;
  291. try
  292. {
  293. assert(FStatus.IsEmpty());
  294. FStatus = Status;
  295. DefaultButton->Default = true;
  296. CancelButton->Cancel = true;
  297. DefaultButtonResult = DefaultResult(this);
  298. if (Zoom)
  299. {
  300. Panel->Align = alClient;
  301. }
  302. if (ForceLog || Visible)
  303. {
  304. if (ClientHeight < Panel->Height)
  305. {
  306. ClientHeight = Panel->Height;
  307. }
  308. // Panel being hidden gets not realigned automatically, even if it
  309. // has Align property set
  310. Panel->Top = ClientHeight - Panel->Height;
  311. Panel->Show();
  312. TCursor PrevCursor = Screen->Cursor;
  313. try
  314. {
  315. if (Zoom)
  316. {
  317. LogView->Hide();
  318. }
  319. else
  320. {
  321. if (LogView->Items->Count > 0)
  322. {
  323. TListItem * Item = LogView->ItemFocused;
  324. if (Item == NULL)
  325. {
  326. Item = LogView->Items->Item[LogView->Items->Count - 1];
  327. }
  328. Item->MakeVisible(false);
  329. }
  330. }
  331. Screen->Cursor = crDefault;
  332. if (!Visible)
  333. {
  334. assert(ForceLog);
  335. ShowAsModal();
  336. }
  337. ActiveControl = FocusControl;
  338. ModalResult = mrNone;
  339. AdjustControls();
  340. do
  341. {
  342. Application->HandleMessage();
  343. }
  344. while (!Application->Terminated && (ModalResult == mrNone));
  345. }
  346. __finally
  347. {
  348. Panel->Hide();
  349. Screen->Cursor = PrevCursor;
  350. if (Zoom)
  351. {
  352. LogView->Show();
  353. }
  354. Repaint();
  355. }
  356. }
  357. else
  358. {
  359. int PrevHeight = ClientHeight;
  360. int PrevMinHeight = Constraints->MinHeight;
  361. int PrevMaxHeight = Constraints->MaxHeight;
  362. try
  363. {
  364. Constraints->MinHeight = 0;
  365. ClientHeight = Panel->Height;
  366. if (FixHeight)
  367. {
  368. Constraints->MinHeight = Height;
  369. Constraints->MaxHeight = Height;
  370. }
  371. LogView->Hide();
  372. Panel->Show();
  373. FFocusControl = FocusControl;
  374. ShowModal();
  375. }
  376. __finally
  377. {
  378. FFocusControl = NULL;
  379. ClientHeight = PrevHeight;
  380. Constraints->MinHeight = PrevMinHeight;
  381. Constraints->MaxHeight = PrevMaxHeight;
  382. Panel->Hide();
  383. LogView->Show();
  384. }
  385. }
  386. }
  387. __finally
  388. {
  389. Panel->Align = Align;
  390. DefaultButton->Default = false;
  391. CancelButton->Cancel = false;
  392. FStatus = L"";
  393. AdjustControls();
  394. }
  395. bool Result = (ModalResult == DefaultButtonResult);
  396. if (!Result)
  397. {
  398. DoCancel();
  399. }
  400. return Result;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  404. {
  405. FormHelp(this);
  406. }
  407. //---------------------------------------------------------------------------
  408. void __fastcall TAuthenticateForm::AdjustLogView()
  409. {
  410. ListView_SetColumnWidth(LogView->Handle, 0, LVSCW_AUTOSIZE);
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  414. {
  415. AdjustLogView();
  416. }
  417. //---------------------------------------------------------------------------