Custom.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Dialogs.hpp>
  5. //---------------------------------------------------------------------
  6. #include <Common.h>
  7. #include <CustomWinConfiguration.h>
  8. #include <WinInterface.h>
  9. #include <VCLCommon.h>
  10. #include <TextsWin.h>
  11. #include <HelpWin.h>
  12. #include <CoreMain.h>
  13. #include <PasTools.hpp>
  14. #include "Custom.h"
  15. //---------------------------------------------------------------------
  16. #pragma link "PasswordEdit"
  17. #ifndef NO_RESOURCES
  18. #pragma resource "*.dfm"
  19. #endif
  20. //---------------------------------------------------------------------
  21. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  22. : TForm(GetFormOwner())
  23. {
  24. UseSystemSettings(this);
  25. FPos = ScaleByTextHeight(this, 8);
  26. HelpKeyword = AHelpKeyword;
  27. TBorderIcons BI = BorderIcons;
  28. if (HelpKeyword.IsEmpty())
  29. {
  30. BI >> biHelp;
  31. OKButton->Left = CancelButton->Left;
  32. CancelButton->Left = HelpButton->Left;
  33. HelpButton->Visible = false;
  34. }
  35. else
  36. {
  37. BI << biHelp;
  38. }
  39. BorderIcons = BI;
  40. }
  41. //---------------------------------------------------------------------
  42. bool __fastcall TCustomDialog::Execute()
  43. {
  44. Changed();
  45. return (ShowModal() == DefaultResult(this));
  46. }
  47. //---------------------------------------------------------------------
  48. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  49. {
  50. // noop
  51. }
  52. //---------------------------------------------------------------------
  53. void __fastcall TCustomDialog::Changed()
  54. {
  55. bool CanSubmit = true;
  56. DoChange(CanSubmit);
  57. EnableControl(OKButton, CanSubmit);
  58. }
  59. //---------------------------------------------------------------------
  60. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  61. {
  62. Changed();
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  66. {
  67. FormHelp(this);
  68. }
  69. //---------------------------------------------------------------------------
  70. void __fastcall TCustomDialog::DoShow()
  71. {
  72. OKButton->TabOrder = FCount;
  73. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  74. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  75. Changed();
  76. TForm::DoShow();
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TCustomDialog::DoValidate()
  80. {
  81. // noop
  82. }
  83. //---------------------------------------------------------------------------
  84. bool __fastcall TCustomDialog::CloseQuery()
  85. {
  86. if (ModalResult == DefaultResult(this))
  87. {
  88. DoValidate();
  89. }
  90. return TForm::CloseQuery();
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  94. {
  95. Control->TabOrder = FCount;
  96. FCount++;
  97. }
  98. //---------------------------------------------------------------------------
  99. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  100. {
  101. TLabel * Result = new TLabel(this);
  102. Result->Caption = Label;
  103. return Result;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label)
  107. {
  108. int PrePos = FPos;
  109. Label->Parent = this;
  110. Label->Left = ScaleByTextHeight(this, 8);
  111. Label->Top = FPos;
  112. FPos += Label->Height + ScaleByTextHeight(this, 4);
  113. Edit->Parent = this;
  114. Edit->Left = ScaleByTextHeight(this, 8);
  115. Edit->Top = FPos;
  116. Edit->Width = ClientWidth - (Edit->Left * 2);
  117. // this updates Height property to real value
  118. Edit->HandleNeeded();
  119. FPos += Edit->Height + ScaleByTextHeight(this, 8);
  120. if (Label->FocusControl == NULL)
  121. {
  122. Label->FocusControl = Edit;
  123. }
  124. else
  125. {
  126. assert(Label->FocusControl == Edit);
  127. }
  128. ClientHeight = ClientHeight + (FPos - PrePos);
  129. AddWinControl(Edit);
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label)
  133. {
  134. AddEditLikeControl(Edit, Label);
  135. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  136. if (PublicEdit->OnChange == NULL)
  137. {
  138. PublicEdit->OnChange = Change;
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label)
  143. {
  144. AddEditLikeControl(Combo, Label);
  145. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  146. if (PublicCombo->OnChange == NULL)
  147. {
  148. PublicCombo->OnChange = Change;
  149. }
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  153. {
  154. int PrePos = FPos;
  155. Control->Parent = this;
  156. Control->Left = ScaleByTextHeight(this, 14);
  157. Control->Top = FPos;
  158. Control->Width = ClientWidth - Control->Left - ScaleByTextHeight(this, 8);
  159. // this updates Height property to real value
  160. Control->HandleNeeded();
  161. // buttons do not scale with text on their own
  162. Control->Height = ScaleByTextHeight(Control, Control->Height);
  163. FPos += Control->Height + ScaleByTextHeight(this, 8);
  164. ClientHeight = ClientHeight + (FPos - PrePos);
  165. AddWinControl(Control);
  166. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  167. if (PublicControl->OnClick == NULL)
  168. {
  169. PublicControl->OnClick = Change;
  170. }
  171. }
  172. //---------------------------------------------------------------------------
  173. //---------------------------------------------------------------------------
  174. class TSaveSessionDialog : public TCustomDialog
  175. {
  176. public:
  177. __fastcall TSaveSessionDialog(bool CanSavePassword, bool NotRecommendedSavingPassword);
  178. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword);
  179. protected:
  180. virtual void __fastcall DoValidate();
  181. virtual void __fastcall DoChange(bool & CanSubmit);
  182. private:
  183. UnicodeString FOriginalSessionName;
  184. TEdit * SessionNameEdit;
  185. TComboBox * FolderCombo;
  186. TCheckBox * SavePasswordCheck;
  187. UnicodeString FRootFolder;
  188. UnicodeString __fastcall GetSessionName();
  189. };
  190. //---------------------------------------------------------------------------
  191. __fastcall TSaveSessionDialog::TSaveSessionDialog(
  192. bool CanSavePassword, bool NotRecommendedSavingPassword) :
  193. TCustomDialog(HELP_SESSION_SAVE)
  194. {
  195. Caption = LoadStr(SAVE_SESSION_CAPTION);
  196. SessionNameEdit = new TEdit(this);
  197. AddEdit(SessionNameEdit, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  198. FolderCombo = new TComboBox(this);
  199. AddComboBox(FolderCombo, CreateLabel(LoadStr(SAVE_SESSION_FOLDER)));
  200. FolderCombo->Items->BeginUpdate();
  201. try
  202. {
  203. FRootFolder = LoadStr(SAVE_SESSION_ROOT_FOLDER);
  204. FolderCombo->Items->Add(FRootFolder);
  205. for (int Index = 0; Index < StoredSessions->Count; Index++)
  206. {
  207. TSessionData * Data = StoredSessions->Sessions[Index];
  208. if (!Data->Special && !Data->IsWorkspace)
  209. {
  210. UnicodeString Folder = Data->FolderName;
  211. if (!Folder.IsEmpty() && FolderCombo->Items->IndexOf(Folder) < 0)
  212. {
  213. FolderCombo->Items->Add(Folder);
  214. }
  215. }
  216. }
  217. }
  218. __finally
  219. {
  220. FolderCombo->Items->EndUpdate();
  221. }
  222. SavePasswordCheck = new TCheckBox(this);
  223. SavePasswordCheck->Caption = LoadStr(
  224. NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  225. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED));
  226. AddButtonControl(SavePasswordCheck);
  227. EnableControl(SavePasswordCheck, CanSavePassword);
  228. }
  229. //---------------------------------------------------------------------------
  230. bool __fastcall TSaveSessionDialog::Execute(UnicodeString & SessionName, bool & SavePassword)
  231. {
  232. FOriginalSessionName = SessionName;
  233. SessionNameEdit->Text = TSessionData::ExtractLocalName(SessionName);
  234. UnicodeString Folder = TSessionData::ExtractFolderName(SessionName);
  235. if (Folder.IsEmpty())
  236. {
  237. FolderCombo->Text = FRootFolder;
  238. }
  239. else
  240. {
  241. FolderCombo->Text = Folder;
  242. }
  243. SavePasswordCheck->Checked = SavePassword;
  244. bool Result = TCustomDialog::Execute();
  245. if (Result)
  246. {
  247. SessionName = GetSessionName();
  248. SavePassword = SavePasswordCheck->Checked;
  249. }
  250. return Result;
  251. }
  252. //---------------------------------------------------------------------------
  253. UnicodeString __fastcall TSaveSessionDialog::GetSessionName()
  254. {
  255. UnicodeString Folder;
  256. if (FolderCombo->Text != FRootFolder)
  257. {
  258. Folder = FolderCombo->Text;
  259. }
  260. return TSessionData::ComposePath(Folder, SessionNameEdit->Text);
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TSaveSessionDialog::DoValidate()
  264. {
  265. TSessionData::ValidateName(SessionNameEdit->Text);
  266. SessionNameValidate(GetSessionName(), FOriginalSessionName);
  267. UnicodeString Folder = TSessionData::ExtractFolderName(GetSessionName());
  268. if (!Folder.IsEmpty() && StoredSessions->IsWorkspace(Folder))
  269. {
  270. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (Folder)));
  271. }
  272. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  273. CustomWinConfiguration->UseMasterPassword)
  274. {
  275. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  276. }
  277. TCustomDialog::DoValidate();
  278. }
  279. //---------------------------------------------------------------------------
  280. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  281. {
  282. CanSubmit = !SessionNameEdit->Text.IsEmpty();
  283. TCustomDialog::DoChange(CanSubmit);
  284. }
  285. //---------------------------------------------------------------------------
  286. TSessionData * __fastcall DoSaveSession(TSessionData * SessionData,
  287. TSessionData * OriginalSession, bool ForceDialog)
  288. {
  289. bool SavePassword = false;
  290. bool * PSavePassword;
  291. bool NotRecommendedSavingPassword =
  292. !CustomWinConfiguration->UseMasterPassword &&
  293. !SameText(SessionData->UserName, AnonymousUserName);
  294. if (Configuration->DisablePasswordStoring ||
  295. !SessionData->HasAnyPassword())
  296. {
  297. PSavePassword = NULL;
  298. }
  299. else
  300. {
  301. PSavePassword = &SavePassword;
  302. SavePassword =
  303. ((OriginalSession != NULL) &&
  304. !OriginalSession->Password.IsEmpty()) ||
  305. !NotRecommendedSavingPassword;
  306. }
  307. UnicodeString SessionName = SessionData->SessionName;
  308. bool Result;
  309. if (!ForceDialog && ((PSavePassword == NULL) || SavePassword))
  310. {
  311. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  312. Result = true;
  313. }
  314. else
  315. {
  316. TSaveSessionDialog * Dialog =
  317. new TSaveSessionDialog((PSavePassword != NULL), NotRecommendedSavingPassword);
  318. try
  319. {
  320. Result = Dialog->Execute(SessionName, SavePassword);
  321. }
  322. __finally
  323. {
  324. delete Dialog;
  325. }
  326. }
  327. TSessionData * NewSession = NULL;
  328. if (Result)
  329. {
  330. if ((PSavePassword != NULL) && !SavePassword)
  331. {
  332. SessionData->Password = L"";
  333. }
  334. NewSession =
  335. StoredSessions->NewSession(SessionName, SessionData);
  336. // modified only, explicit
  337. StoredSessions->Save(false, true);
  338. }
  339. return NewSession;
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall SessionNameValidate(const UnicodeString & Text,
  343. const UnicodeString & OriginalName)
  344. {
  345. TSessionData::ValidatePath(Text);
  346. assert(StoredSessions);
  347. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  348. if (Data && Data->Special)
  349. {
  350. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  351. qtError, qaOK, HELP_NONE);
  352. Abort();
  353. }
  354. else if ((Data != NULL) && (Text != OriginalName) &&
  355. MessageDialog(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text)),
  356. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  357. {
  358. Abort();
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. //---------------------------------------------------------------------------
  363. class TSaveWorkspaceDialog : public TCustomDialog
  364. {
  365. public:
  366. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  367. bool NotRecommendedSavingPasswords);
  368. bool __fastcall Execute(
  369. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  370. bool & EnableAutoSave);
  371. protected:
  372. virtual void __fastcall DoValidate();
  373. virtual void __fastcall DoChange(bool & CanSubmit);
  374. private:
  375. TComboBox * WorkspaceNameCombo;
  376. TCheckBox * SavePasswordsCheck;
  377. TCheckBox * CreateShortcutCheck;
  378. TCheckBox * EnableAutoSaveCheck;
  379. };
  380. //---------------------------------------------------------------------------
  381. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  382. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  383. TCustomDialog(HELP_WORKSPACE_SAVE)
  384. {
  385. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  386. WorkspaceNameCombo = new TComboBox(this);
  387. WorkspaceNameCombo->AutoComplete = false;
  388. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  389. std::auto_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  390. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  391. SavePasswordsCheck = new TCheckBox(this);
  392. SavePasswordsCheck->Caption = LoadStr(
  393. NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  394. (CustomWinConfiguration->UseMasterPassword ?
  395. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED));
  396. AddButtonControl(SavePasswordsCheck);
  397. EnableControl(SavePasswordsCheck, CanSavePasswords);
  398. CreateShortcutCheck = new TCheckBox(this);
  399. CreateShortcutCheck->Caption = LoadStr(SAVE_WORKSPACE_SHORTCUT);
  400. AddButtonControl(CreateShortcutCheck);
  401. EnableAutoSaveCheck = new TCheckBox(this);
  402. EnableAutoSaveCheck->Caption = LoadStr(SAVE_WORKSPACE_AUTO);
  403. AddButtonControl(EnableAutoSaveCheck);
  404. }
  405. //---------------------------------------------------------------------------
  406. bool __fastcall TSaveWorkspaceDialog::Execute(
  407. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  408. bool & EnableAutoSave)
  409. {
  410. WorkspaceNameCombo->Text = WorkspaceName;
  411. SavePasswordsCheck->Checked = SavePasswords;
  412. CreateShortcutCheck->Checked = CreateShortcut;
  413. EnableAutoSaveCheck->Checked = EnableAutoSave;
  414. bool Result = TCustomDialog::Execute();
  415. if (Result)
  416. {
  417. WorkspaceName = WorkspaceNameCombo->Text;
  418. SavePasswords = SavePasswordsCheck->Checked;
  419. CreateShortcut = CreateShortcutCheck->Checked;
  420. EnableAutoSave = EnableAutoSaveCheck->Checked;
  421. }
  422. return Result;
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TSaveWorkspaceDialog::DoValidate()
  426. {
  427. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  428. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  429. {
  430. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  431. }
  432. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  433. CustomWinConfiguration->UseMasterPassword)
  434. {
  435. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  436. }
  437. TCustomDialog::DoValidate();
  438. }
  439. //---------------------------------------------------------------------------
  440. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  441. {
  442. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  443. TCustomDialog::DoChange(CanSubmit);
  444. }
  445. //---------------------------------------------------------------------------
  446. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  447. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  448. bool & CreateShortcut, bool & EnableAutoSave)
  449. {
  450. std::auto_ptr<TSaveWorkspaceDialog> Dialog(
  451. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  452. bool Dummy = false;
  453. if (SavePasswords == NULL)
  454. {
  455. SavePasswords = &Dummy;
  456. }
  457. return
  458. Dialog->Execute(
  459. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  460. }
  461. //---------------------------------------------------------------------------
  462. //---------------------------------------------------------------------------
  463. class TShortCutDialog : public TCustomDialog
  464. {
  465. public:
  466. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  467. bool __fastcall Execute(TShortCut & ShortCut);
  468. private:
  469. TComboBox * ShortCutCombo;
  470. };
  471. //---------------------------------------------------------------------------
  472. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  473. TCustomDialog(HelpKeyword)
  474. {
  475. Caption = LoadStr(SHORTCUT_CAPTION);
  476. ShortCutCombo = new TComboBox(this);
  477. AddComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)));
  478. InitializeShortCutCombo(ShortCutCombo, ShortCuts);
  479. }
  480. //---------------------------------------------------------------------------
  481. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  482. {
  483. SetShortCutCombo(ShortCutCombo, ShortCut);
  484. bool Result = TCustomDialog::Execute();
  485. if (Result)
  486. {
  487. ShortCut = GetShortCutCombo(ShortCutCombo);
  488. }
  489. return Result;
  490. }
  491. //---------------------------------------------------------------------------
  492. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  493. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  494. {
  495. bool Result;
  496. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  497. try
  498. {
  499. Result = Dialog->Execute(ShortCut);
  500. }
  501. __finally
  502. {
  503. delete Dialog;
  504. }
  505. return Result;
  506. }