UserInterface.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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->MainFormHandle, 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. Menu->Add(Item);
  391. }
  392. //---------------------------------------------------------------------------
  393. static TComponent * LastPopupComponent = NULL;
  394. static TDateTime LastCloseUp;
  395. //---------------------------------------------------------------------------
  396. void __fastcall MenuPopup(TPopupMenu * AMenu, TPoint Point,
  397. TComponent * PopupComponent)
  398. {
  399. // pressing the same button within 200ms after closing its popup menu
  400. // does nothing.
  401. // it is to immitate close-by-click behaviour. note that menu closes itself
  402. // before onclick handler of button occurs
  403. if ((PopupComponent == LastPopupComponent) &&
  404. (Now() - LastCloseUp < TDateTime(0, 0, 0, 200)))
  405. {
  406. LastPopupComponent = NULL;
  407. }
  408. else
  409. {
  410. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  411. if (Menu == NULL)
  412. {
  413. Menu = CreateTBXPopupMenu(AMenu->Owner);
  414. Menu->OnPopup = AMenu->OnPopup;
  415. Menu->Items->SubMenuImages = AMenu->Images;
  416. for (int Index = 0; Index < AMenu->Items->Count; Index++)
  417. {
  418. TMenuItem * AItem = AMenu->Items->Items[Index];
  419. TTBCustomItem * Item;
  420. // recurse not implemented yet
  421. assert(AItem->Count == 0);
  422. if (!AItem->Enabled && !AItem->Visible)
  423. {
  424. TTBXLabelItem * LabelItem = new TTBXLabelItem(Menu);
  425. // TTBXLabelItem has it's own Caption
  426. LabelItem->Caption = AItem->Caption;
  427. LabelItem->SectionHeader = true;
  428. Item = LabelItem;
  429. }
  430. else
  431. {
  432. // see TB2DsgnConverter.pas DoConvert
  433. if (AItem->Caption == L"-")
  434. {
  435. Item = new TTBXSeparatorItem(Menu);
  436. }
  437. else
  438. {
  439. Item = new TTBXItem(Menu);
  440. Item->Action = AItem->Action;
  441. Item->AutoCheck = AItem->AutoCheck;
  442. Item->Caption = AItem->Caption;
  443. Item->Checked = AItem->Checked;
  444. if (AItem->Default)
  445. {
  446. Item->Options = Item->Options << tboDefault;
  447. }
  448. Item->Enabled = AItem->Enabled;
  449. Item->GroupIndex = AItem->GroupIndex;
  450. Item->HelpContext = AItem->HelpContext;
  451. Item->ImageIndex = AItem->ImageIndex;
  452. Item->RadioItem = AItem->RadioItem;
  453. Item->ShortCut = AItem->ShortCut;
  454. Item->SubMenuImages = AItem->SubMenuImages;
  455. Item->OnClick = AItem->OnClick;
  456. }
  457. Item->Hint = AItem->Hint;
  458. Item->Tag = AItem->Tag;
  459. Item->Visible = AItem->Visible;
  460. }
  461. Menu->Items->Add(Item);
  462. }
  463. }
  464. Menu->PopupComponent = PopupComponent;
  465. Menu->Popup(Point.x, Point.y);
  466. LastPopupComponent = PopupComponent;
  467. LastCloseUp = Now();
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  472. {
  473. // no-op yet
  474. }
  475. //---------------------------------------------------------------------------
  476. struct TThreadParam
  477. {
  478. TThreadFunc ThreadFunc;
  479. void * Parameter;
  480. };
  481. //---------------------------------------------------------------------------
  482. static int __fastcall ThreadProc(void * AParam)
  483. {
  484. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  485. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  486. delete Param;
  487. EndThread(Result);
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  492. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  493. TThreadID & ThreadId)
  494. {
  495. TThreadParam * Param = new TThreadParam;
  496. Param->ThreadFunc = ThreadFunc;
  497. Param->Parameter = Parameter;
  498. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  499. CreationFlags, ThreadId);
  500. }
  501. //---------------------------------------------------------------------------
  502. static TShortCut FirstCtrlNumberShortCut = ShortCut(L'0', TShiftState() << ssCtrl);
  503. static TShortCut LastCtrlNumberShortCut = ShortCut(L'9', TShiftState() << ssCtrl);
  504. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut(L'A', TShiftState() << ssShift << ssCtrl << ssAlt);
  505. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut(L'Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  506. //---------------------------------------------------------------------------
  507. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  508. const TShortCuts & ShortCuts)
  509. {
  510. ComboBox->Items->BeginUpdate();
  511. try
  512. {
  513. ComboBox->Items->Clear();
  514. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  515. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  516. {
  517. if (!ShortCuts.Has(AShortCut))
  518. {
  519. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  520. }
  521. }
  522. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  523. {
  524. if (!ShortCuts.Has(AShortCut))
  525. {
  526. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  527. }
  528. }
  529. }
  530. __finally
  531. {
  532. ComboBox->Items->EndUpdate();
  533. }
  534. ComboBox->Style = csDropDownList;
  535. ComboBox->DropDownCount = 16;
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  539. {
  540. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  541. {
  542. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  543. if (AShortCut == Value)
  544. {
  545. ComboBox->ItemIndex = Index;
  546. break;
  547. }
  548. else if (AShortCut < Value)
  549. {
  550. assert(Value != 0);
  551. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  552. reinterpret_cast<TObject* >(Value));
  553. ComboBox->ItemIndex = Index + 1;
  554. break;
  555. }
  556. assert(Index > 0);
  557. }
  558. }
  559. //---------------------------------------------------------------------------
  560. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  561. {
  562. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  563. }
  564. //---------------------------------------------------------------------------
  565. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  566. {
  567. return
  568. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  569. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  570. }
  571. //---------------------------------------------------------------------------
  572. //---------------------------------------------------------------------------
  573. class TMasterPasswordDialog : public TCustomDialog
  574. {
  575. public:
  576. __fastcall TMasterPasswordDialog(bool Current);
  577. bool __fastcall Execute();
  578. protected:
  579. virtual void __fastcall DoValidate();
  580. virtual void __fastcall DoChange(bool & CanSubmit);
  581. private:
  582. TPasswordEdit * CurrentEdit;
  583. TPasswordEdit * NewEdit;
  584. TPasswordEdit * ConfirmEdit;
  585. };
  586. //---------------------------------------------------------------------------
  587. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(bool Current) :
  588. TCustomDialog(Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE)
  589. {
  590. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  591. CurrentEdit = new TPasswordEdit(this);
  592. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  593. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  594. CurrentEdit->MaxLength = PasswordMaxLength();
  595. if (!Current)
  596. {
  597. NewEdit = new TPasswordEdit(this);
  598. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  599. NewEdit->MaxLength = CurrentEdit->MaxLength;
  600. if (!WinConfiguration->UseMasterPassword)
  601. {
  602. ActiveControl = NewEdit;
  603. }
  604. ConfirmEdit = new TPasswordEdit(this);
  605. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  606. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  607. }
  608. else
  609. {
  610. NewEdit = NULL;
  611. ConfirmEdit = NULL;
  612. }
  613. }
  614. //---------------------------------------------------------------------------
  615. bool __fastcall TMasterPasswordDialog::Execute()
  616. {
  617. bool Result = TCustomDialog::Execute();
  618. if (Result)
  619. {
  620. if (CurrentEdit->Enabled)
  621. {
  622. WinConfiguration->SetMasterPassword(CurrentEdit->Text);
  623. }
  624. if (NewEdit != NULL)
  625. {
  626. WinConfiguration->SetMasterPassword(NewEdit->Text);
  627. }
  628. }
  629. return Result;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  633. {
  634. CanSubmit =
  635. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  636. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  637. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  638. TCustomDialog::DoChange(CanSubmit);
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TMasterPasswordDialog::DoValidate()
  642. {
  643. TCustomDialog::DoValidate();
  644. if (WinConfiguration->UseMasterPassword &&
  645. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  646. {
  647. CurrentEdit->SetFocus();
  648. CurrentEdit->SelectAll();
  649. throw Exception(LoadStr(MASTER_PASSWORD_INCORRECT));
  650. }
  651. if (NewEdit != NULL)
  652. {
  653. if (NewEdit->Text != ConfirmEdit->Text)
  654. {
  655. ConfirmEdit->SetFocus();
  656. ConfirmEdit->SelectAll();
  657. throw Exception(LoadStr(MASTER_PASSWORD_DIFFERENT));
  658. }
  659. int Valid = IsValidPassword(NewEdit->Text);
  660. if (Valid <= 0)
  661. {
  662. assert(Valid == 0);
  663. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE), qtWarning,
  664. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  665. {
  666. NewEdit->SetFocus();
  667. NewEdit->SelectAll();
  668. Abort();
  669. }
  670. }
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. static bool __fastcall DoMasterPasswordDialog(bool Current)
  675. {
  676. bool Result;
  677. TMasterPasswordDialog * Dialog = new TMasterPasswordDialog(Current);
  678. try
  679. {
  680. Result = Dialog->Execute();
  681. }
  682. __finally
  683. {
  684. delete Dialog;
  685. }
  686. return Result;
  687. }
  688. //---------------------------------------------------------------------------
  689. bool __fastcall DoMasterPasswordDialog()
  690. {
  691. return DoMasterPasswordDialog(true);
  692. }
  693. //---------------------------------------------------------------------------
  694. bool __fastcall DoChangeMasterPasswordDialog()
  695. {
  696. return DoMasterPasswordDialog(false);
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall MessageWithNoHelp(const UnicodeString & Message)
  700. {
  701. TMessageParams Params;
  702. Params.AllowHelp = false; // to avoid recursion
  703. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE), qtConfirmation,
  704. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  705. {
  706. SearchHelp(Message);
  707. }
  708. }