UserInterface.cpp 22 KB

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