UserInterface.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "ScpCommander.h"
  5. #include "ScpExplorer.h"
  6. #include <CoreMain.h>
  7. #include <Common.h>
  8. #include <Exceptions.h>
  9. #include <Cryptography.h>
  10. #include "ProgParams.h"
  11. #include "VCLCommon.h"
  12. #include "WinConfiguration.h"
  13. #include "TerminalManager.h"
  14. #include "TextsWin.h"
  15. #include "TBXThemes.hpp"
  16. #include "TBXOfficeXPTheme.hpp"
  17. #include "TBXOffice2003Theme.hpp"
  18. #include "ProgParams.h"
  19. #include "Tools.h"
  20. #include "Custom.h"
  21. #include "HelpWin.h"
  22. //---------------------------------------------------------------------------
  23. #pragma package(smart_init)
  24. //---------------------------------------------------------------------------
  25. const AnsiString AppName = "WinSCP";
  26. //---------------------------------------------------------------------------
  27. TConfiguration * __fastcall CreateConfiguration()
  28. {
  29. TConfiguration * Configuration = new TWinConfiguration();
  30. TProgramParams * Params = TProgramParams::Instance();
  31. AnsiString IniFileName = Params->SwitchValue("ini");
  32. if (!IniFileName.IsEmpty())
  33. {
  34. IniFileName = ExpandFileName(ExpandEnvironmentVariables(IniFileName));
  35. Configuration->IniFileStorageName = IniFileName;
  36. }
  37. return Configuration;
  38. }
  39. //---------------------------------------------------------------------------
  40. TCustomScpExplorerForm * __fastcall CreateScpExplorer()
  41. {
  42. TCustomScpExplorerForm * ScpExplorer;
  43. if (WinConfiguration->Interface == ifExplorer)
  44. {
  45. ScpExplorer = SafeFormCreate<TScpExplorerForm>();
  46. }
  47. else
  48. {
  49. ScpExplorer = SafeFormCreate<TScpCommanderForm>();
  50. }
  51. ScpExplorer->Icon->Assign(Application->Icon);
  52. return ScpExplorer;
  53. }
  54. //---------------------------------------------------------------------------
  55. AnsiString __fastcall SshVersionString()
  56. {
  57. return FORMAT("WinSCP-release-%s", (Configuration->Version));
  58. }
  59. //---------------------------------------------------------------------------
  60. AnsiString __fastcall AppNameString()
  61. {
  62. return "WinSCP";
  63. }
  64. //---------------------------------------------------------------------------
  65. AnsiString __fastcall GetRegistryKey()
  66. {
  67. return "Software\\Martin Prikryl\\WinSCP 2";
  68. }
  69. //---------------------------------------------------------------------------
  70. static bool ForcedOnForeground = false;
  71. void __fastcall SetOnForeground(bool OnForeground)
  72. {
  73. ForcedOnForeground = OnForeground;
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall FlashOnBackground()
  77. {
  78. assert(Application);
  79. if (!ForcedOnForeground && !ForegroundTask())
  80. {
  81. FlashWindow(Application->Handle, true);
  82. }
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall LocalSystemSettings(TCustomForm * /*Control*/)
  86. {
  87. // noop
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall ShowExtendedException(Exception * E)
  91. {
  92. ShowExtendedExceptionEx(NULL, E);
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall ShowExtendedExceptionEx(TTerminal * Terminal,
  96. Exception * E)
  97. {
  98. AnsiString Message; // not used
  99. if (ExceptionMessage(E, Message))
  100. {
  101. TTerminalManager * Manager = TTerminalManager::Instance(false);
  102. TQueryType Type;
  103. ESshTerminate * Terminate = dynamic_cast<ESshTerminate*>(E);
  104. bool CloseOnCompletion = (Terminate != NULL);
  105. Type = CloseOnCompletion ? qtInformation : qtError;
  106. bool ConfirmExitOnCompletion =
  107. CloseOnCompletion &&
  108. (Terminate->Operation == odoDisconnect) &&
  109. WinConfiguration->ConfirmExitOnCompletion;
  110. if (E->InheritsFrom(__classid(EFatal)) && (Terminal != NULL) &&
  111. (Manager != NULL) && (Manager->ActiveTerminal == Terminal))
  112. {
  113. if (CloseOnCompletion)
  114. {
  115. Manager->DisconnectActiveTerminal();
  116. }
  117. int SessionReopenTimeout = 0;
  118. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  119. if ((ManagedTerminal != NULL) &&
  120. ((Configuration->SessionReopenTimeout == 0) ||
  121. ((double)ManagedTerminal->ReopenStart == 0) ||
  122. (int(double(Now() - ManagedTerminal->ReopenStart) * 24*60*60*1000) < Configuration->SessionReopenTimeout)))
  123. {
  124. SessionReopenTimeout = GUIConfiguration->SessionReopenAutoIdle;
  125. }
  126. int Result;
  127. if (CloseOnCompletion)
  128. {
  129. if (ConfirmExitOnCompletion)
  130. {
  131. TMessageParams Params(mpNeverAskAgainCheck);
  132. Result = FatalExceptionMessageDialog(E, Type, 0,
  133. (Manager->Count > 1) ?
  134. FMTLOAD(DISCONNECT_ON_COMPLETION, (Manager->Count - 1)) :
  135. LoadStr(EXIT_ON_COMPLETION),
  136. qaYes | qaNo, HELP_NONE, &Params);
  137. if (Result == qaNeverAskAgain)
  138. {
  139. Result = qaYes;
  140. WinConfiguration->ConfirmExitOnCompletion = false;
  141. }
  142. }
  143. else
  144. {
  145. Result = qaYes;
  146. }
  147. }
  148. else
  149. {
  150. Result = FatalExceptionMessageDialog(E, Type, SessionReopenTimeout);
  151. }
  152. if (Result == qaYes)
  153. {
  154. assert(Terminate != NULL);
  155. assert(Terminate->Operation != odoIdle);
  156. Application->Terminate();
  157. switch (Terminate->Operation)
  158. {
  159. case odoDisconnect:
  160. break;
  161. case odoShutDown:
  162. ShutDownWindows();
  163. break;
  164. default:
  165. assert(false);
  166. }
  167. }
  168. else if (Result == qaRetry)
  169. {
  170. Manager->ReconnectActiveTerminal();
  171. }
  172. else
  173. {
  174. Manager->FreeActiveTerminal();
  175. }
  176. }
  177. else
  178. {
  179. // this should not happen as we never use Terminal->CloseOnCompletion
  180. // on inactive terminal
  181. if (CloseOnCompletion)
  182. {
  183. if (ConfirmExitOnCompletion)
  184. {
  185. TMessageParams Params(mpNeverAskAgainCheck);
  186. if (ExceptionMessageDialog(E, Type, "", qaOK, HELP_NONE, &Params) ==
  187. qaNeverAskAgain)
  188. {
  189. WinConfiguration->ConfirmExitOnCompletion = false;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. ExceptionMessageDialog(E, Type);
  196. }
  197. }
  198. }
  199. }
  200. //---------------------------------------------------------------------------
  201. void __fastcall ShowNotification(TTerminal * Terminal, const AnsiString & Str,
  202. TQueryType Type)
  203. {
  204. TTerminalManager * Manager = TTerminalManager::Instance(false);
  205. assert(Manager != NULL);
  206. Manager->ScpExplorer->PopupTrayBalloon(Terminal, Str, Type);
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall ConfigureInterface()
  210. {
  211. AnsiString S;
  212. S = LoadStr(MIDDLE_EAST);
  213. if (!S.IsEmpty())
  214. {
  215. SysLocale.MiddleEast = static_cast<bool>(StrToInt(S));
  216. }
  217. else
  218. {
  219. SysLocale.MiddleEast = false;
  220. }
  221. S = LoadStr(BIDI_MODE);
  222. if (!S.IsEmpty())
  223. {
  224. Application->BiDiMode = static_cast<TBiDiMode>(StrToInt(bdRightToLeft));
  225. }
  226. else
  227. {
  228. Application->BiDiMode = bdLeftToRight;
  229. }
  230. SetTBXSysParam(TSP_XPVISUALSTYLE, XPVS_AUTOMATIC);
  231. // Can be called during configuration creation.
  232. // Skip now, will be called again later.
  233. if (Configuration != NULL)
  234. {
  235. TBXSetTheme(WinConfiguration->Theme);
  236. }
  237. }
  238. //---------------------------------------------------------------------------
  239. // dummy function to force linking of TBXOfficeXPTheme.pas
  240. void __fastcall CreateThemes()
  241. {
  242. new TTBXOfficeXPTheme("OfficeXP");
  243. new TTBXOffice2003Theme("Office2003");
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall DoAboutDialog(TConfiguration *Configuration)
  247. {
  248. DoAboutDialog(Configuration, true, NULL);
  249. }
  250. //---------------------------------------------------------------------
  251. void __fastcall DoProductLicense()
  252. {
  253. DoLicenseDialog(lcWinScp);
  254. }
  255. //---------------------------------------------------------------------
  256. static inline void __fastcall GetToolbarKey(const AnsiString & ToolbarName,
  257. const AnsiString & Value, AnsiString & ToolbarKey)
  258. {
  259. int ToolbarNameLen;
  260. if ((ToolbarName.Length() > 7) &&
  261. (ToolbarName.SubString(ToolbarName.Length() - 7 + 1, 7) == "Toolbar"))
  262. {
  263. ToolbarNameLen = ToolbarName.Length() - 7;
  264. }
  265. else
  266. {
  267. ToolbarNameLen = ToolbarName.Length();
  268. }
  269. ToolbarKey = ToolbarName.SubString(1, ToolbarNameLen) + "_" + Value;
  270. }
  271. //---------------------------------------------------------------------------
  272. static int __fastcall ToolbarReadInt(const AnsiString ToolbarName,
  273. const AnsiString Value, const int Default, const void * ExtraData)
  274. {
  275. int Result;
  276. if (Value == "Rev")
  277. {
  278. Result = 2000;
  279. }
  280. else
  281. {
  282. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  283. AnsiString ToolbarKey;
  284. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  285. if (Storage->IndexOfName(ToolbarKey) >= 0)
  286. {
  287. Result = StrToIntDef(Storage->Values[ToolbarKey], Default);
  288. }
  289. else
  290. {
  291. Result = Default;
  292. }
  293. }
  294. return Result;
  295. }
  296. //---------------------------------------------------------------------------
  297. static AnsiString __fastcall ToolbarReadString(const AnsiString ToolbarName,
  298. const AnsiString Value, const AnsiString Default, const void * ExtraData)
  299. {
  300. AnsiString Result;
  301. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  302. AnsiString ToolbarKey;
  303. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  304. if (Storage->IndexOfName(ToolbarKey) >= 0)
  305. {
  306. Result = Storage->Values[ToolbarKey];
  307. }
  308. else
  309. {
  310. Result = Default;
  311. }
  312. return Result;
  313. }
  314. //---------------------------------------------------------------------------
  315. static void __fastcall ToolbarWriteInt(const AnsiString ToolbarName,
  316. const AnsiString Value, const int Data, const void * ExtraData)
  317. {
  318. if (Value != "Rev")
  319. {
  320. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  321. AnsiString ToolbarKey;
  322. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  323. assert(Storage->IndexOfName(ToolbarKey) < 0);
  324. Storage->Values[ToolbarKey] = IntToStr(Data);
  325. }
  326. }
  327. //---------------------------------------------------------------------------
  328. static void __fastcall ToolbarWriteString(const AnsiString ToolbarName,
  329. const AnsiString Value, const AnsiString Data, const void * ExtraData)
  330. {
  331. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  332. AnsiString ToolbarKey;
  333. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  334. assert(Storage->IndexOfName(ToolbarKey) < 0);
  335. Storage->Values[ToolbarKey] = Data;
  336. }
  337. //---------------------------------------------------------------------------
  338. AnsiString __fastcall GetToolbarsLayoutStr(const TComponent * OwnerComponent)
  339. {
  340. AnsiString Result;
  341. TStrings * Storage = new TStringList();
  342. try
  343. {
  344. TBCustomSavePositions(OwnerComponent, ToolbarWriteInt, ToolbarWriteString,
  345. Storage);
  346. Result = Storage->CommaText;
  347. }
  348. __finally
  349. {
  350. delete Storage;
  351. }
  352. return Result;
  353. }
  354. //---------------------------------------------------------------------------
  355. void __fastcall LoadToolbarsLayoutStr(const TComponent * OwnerComponent, AnsiString LayoutStr)
  356. {
  357. TStrings * Storage = new TStringList();
  358. try
  359. {
  360. Storage->CommaText = LayoutStr;
  361. TBCustomLoadPositions(OwnerComponent, ToolbarReadInt, ToolbarReadString,
  362. Storage);
  363. }
  364. __finally
  365. {
  366. delete Storage;
  367. }
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall AddMenuSeparator(TTBCustomItem * Menu)
  371. {
  372. TTBXSeparatorItem * Item = new TTBXSeparatorItem(Menu);
  373. Item->Caption = "-";
  374. Item->Hint = "E";
  375. Menu->Add(Item);
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall MenuPopup(TPopupMenu * AMenu, TPoint Point,
  379. TComponent * PopupComponent)
  380. {
  381. static TComponent * LastPopupComponent = NULL;
  382. static TDateTime LastCloseUp;
  383. // pressing the same button within 200ms after closing its popup menu
  384. // does nothing.
  385. // it is to immitate close-by-click behaviour. note that menu closes itself
  386. // before onclick handler of button occurs
  387. if ((PopupComponent == LastPopupComponent) &&
  388. (Now() - LastCloseUp < TDateTime(0, 0, 0, 200)))
  389. {
  390. LastPopupComponent = NULL;
  391. }
  392. else
  393. {
  394. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  395. if (Menu == NULL)
  396. {
  397. Menu = CreateTBXPopupMenu(AMenu->Owner);
  398. Menu->OnPopup = AMenu->OnPopup;
  399. Menu->Items->SubMenuImages = AMenu->Images;
  400. for (int Index = 0; Index < AMenu->Items->Count; Index++)
  401. {
  402. TMenuItem * AItem = AMenu->Items->Items[Index];
  403. TTBCustomItem * Item;
  404. // recurse not implemented yet
  405. assert(AItem->Count == 0);
  406. // see TB2DsgnConverter.pas DoConvert
  407. if (AItem->Caption == "-")
  408. {
  409. Item = new TTBXSeparatorItem(Menu);
  410. }
  411. else
  412. {
  413. Item = new TTBXItem(Menu);
  414. Item->Action = AItem->Action;
  415. Item->AutoCheck = AItem->AutoCheck;
  416. Item->Caption = AItem->Caption;
  417. Item->Checked = AItem->Checked;
  418. if (AItem->Default)
  419. {
  420. Item->Options = Item->Options << tboDefault;
  421. }
  422. Item->Enabled = AItem->Enabled;
  423. Item->GroupIndex = AItem->GroupIndex;
  424. Item->HelpContext = AItem->HelpContext;
  425. Item->ImageIndex = AItem->ImageIndex;
  426. Item->RadioItem = AItem->RadioItem;
  427. Item->ShortCut = AItem->ShortCut;
  428. Item->SubMenuImages = AItem->SubMenuImages;
  429. Item->OnClick = AItem->OnClick;
  430. }
  431. Item->Hint = AItem->Hint;
  432. Item->Tag = AItem->Tag;
  433. Item->Visible = AItem->Visible;
  434. Menu->Items->Add(Item);
  435. }
  436. }
  437. Menu->PopupComponent = PopupComponent;
  438. Menu->Popup(Point.x, Point.y);
  439. LastPopupComponent = PopupComponent;
  440. LastCloseUp = Now();
  441. }
  442. }
  443. //---------------------------------------------------------------------------
  444. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  445. {
  446. // no-op yet
  447. }
  448. //---------------------------------------------------------------------------
  449. struct TThreadParam
  450. {
  451. TThreadFunc ThreadFunc;
  452. void * Parameter;
  453. };
  454. //---------------------------------------------------------------------------
  455. static int __fastcall ThreadProc(void * AParam)
  456. {
  457. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  458. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  459. delete Param;
  460. EndThread(Result);
  461. return Result;
  462. }
  463. //---------------------------------------------------------------------------
  464. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  465. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  466. unsigned & ThreadId)
  467. {
  468. TThreadParam * Param = new TThreadParam;
  469. Param->ThreadFunc = ThreadFunc;
  470. Param->Parameter = Parameter;
  471. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  472. CreationFlags, ThreadId);
  473. }
  474. //---------------------------------------------------------------------------
  475. static TShortCut FirstCtrlNumberShortCut = ShortCut('0', TShiftState() << ssCtrl);
  476. static TShortCut LastCtrlNumberShortCut = ShortCut('9', TShiftState() << ssCtrl);
  477. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut('A', TShiftState() << ssShift << ssCtrl << ssAlt);
  478. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut('Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  479. //---------------------------------------------------------------------------
  480. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  481. const TShortCuts & ShortCuts)
  482. {
  483. ComboBox->Items->BeginUpdate();
  484. try
  485. {
  486. ComboBox->Items->Clear();
  487. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  488. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  489. {
  490. if (!ShortCuts.Has(AShortCut))
  491. {
  492. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  493. }
  494. }
  495. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  496. {
  497. if (!ShortCuts.Has(AShortCut))
  498. {
  499. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  500. }
  501. }
  502. }
  503. __finally
  504. {
  505. ComboBox->Items->EndUpdate();
  506. }
  507. ComboBox->Style = csDropDownList;
  508. ComboBox->DropDownCount = 16;
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  512. {
  513. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  514. {
  515. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  516. if (AShortCut == Value)
  517. {
  518. ComboBox->ItemIndex = Index;
  519. break;
  520. }
  521. else if (AShortCut < Value)
  522. {
  523. assert(Value != 0);
  524. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  525. reinterpret_cast<TObject* >(Value));
  526. ComboBox->ItemIndex = Index + 1;
  527. break;
  528. }
  529. assert(Index > 0);
  530. }
  531. }
  532. //---------------------------------------------------------------------------
  533. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  534. {
  535. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  536. }
  537. //---------------------------------------------------------------------------
  538. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  539. {
  540. return
  541. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  542. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  543. }
  544. //---------------------------------------------------------------------------
  545. //---------------------------------------------------------------------------
  546. class TMasterPasswordDialog : public TCustomDialog
  547. {
  548. public:
  549. __fastcall TMasterPasswordDialog(bool Current);
  550. bool __fastcall Execute();
  551. protected:
  552. virtual void __fastcall DoValidate();
  553. virtual void __fastcall DoChange(bool & CanSubmit);
  554. private:
  555. TPasswordEdit * CurrentEdit;
  556. TPasswordEdit * NewEdit;
  557. TPasswordEdit * ConfirmEdit;
  558. };
  559. //---------------------------------------------------------------------------
  560. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(bool Current) :
  561. TCustomDialog(Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE)
  562. {
  563. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  564. CurrentEdit = new TPasswordEdit(this);
  565. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  566. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  567. CurrentEdit->MaxLength = PasswordMaxLength();
  568. if (!Current)
  569. {
  570. NewEdit = new TPasswordEdit(this);
  571. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  572. NewEdit->MaxLength = CurrentEdit->MaxLength;
  573. if (!WinConfiguration->UseMasterPassword)
  574. {
  575. ActiveControl = NewEdit;
  576. }
  577. ConfirmEdit = new TPasswordEdit(this);
  578. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  579. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  580. }
  581. else
  582. {
  583. NewEdit = NULL;
  584. ConfirmEdit = NULL;
  585. }
  586. }
  587. //---------------------------------------------------------------------------
  588. bool __fastcall TMasterPasswordDialog::Execute()
  589. {
  590. bool Result = TCustomDialog::Execute();
  591. if (Result)
  592. {
  593. if (CurrentEdit->Enabled)
  594. {
  595. WinConfiguration->SetMasterPassword(CurrentEdit->Text);
  596. }
  597. if (NewEdit != NULL)
  598. {
  599. WinConfiguration->SetMasterPassword(NewEdit->Text);
  600. }
  601. }
  602. return Result;
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  606. {
  607. CanSubmit =
  608. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  609. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  610. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  611. TCustomDialog::DoChange(CanSubmit);
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TMasterPasswordDialog::DoValidate()
  615. {
  616. TCustomDialog::DoValidate();
  617. if (WinConfiguration->UseMasterPassword &&
  618. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  619. {
  620. CurrentEdit->SetFocus();
  621. CurrentEdit->SelectAll();
  622. throw Exception(LoadStr(MASTER_PASSWORD_INCORRECT));
  623. }
  624. if (NewEdit != NULL)
  625. {
  626. if (NewEdit->Text != ConfirmEdit->Text)
  627. {
  628. ConfirmEdit->SetFocus();
  629. ConfirmEdit->SelectAll();
  630. throw Exception(LoadStr(MASTER_PASSWORD_DIFFERENT));
  631. }
  632. int Valid = IsValidPassword(NewEdit->Text);
  633. if (Valid <= 0)
  634. {
  635. assert(Valid == 0);
  636. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE), qtWarning,
  637. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  638. {
  639. NewEdit->SetFocus();
  640. NewEdit->SelectAll();
  641. Abort();
  642. }
  643. }
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. static bool __fastcall DoMasterPasswordDialog(bool Current)
  648. {
  649. bool Result;
  650. TMasterPasswordDialog * Dialog = new TMasterPasswordDialog(Current);
  651. try
  652. {
  653. Result = Dialog->Execute();
  654. }
  655. __finally
  656. {
  657. delete Dialog;
  658. }
  659. return Result;
  660. }
  661. //---------------------------------------------------------------------------
  662. bool __fastcall DoMasterPasswordDialog()
  663. {
  664. return DoMasterPasswordDialog(true);
  665. }
  666. //---------------------------------------------------------------------------
  667. bool __fastcall DoChangeMasterPasswordDialog()
  668. {
  669. return DoMasterPasswordDialog(false);
  670. }
  671. //---------------------------------------------------------------------------
  672. void __fastcall MessageWithNoHelp(const AnsiString & Message)
  673. {
  674. TMessageParams Params;
  675. Params.AllowHelp = false; // to avoid recursion
  676. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE), qtConfirmation,
  677. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  678. {
  679. SearchHelp(Message);
  680. }
  681. }