Custom.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 <ProgParams.h>
  15. #include <Tools.h>
  16. #include <GUITools.h>
  17. #include <HistoryComboBox.hpp>
  18. #include <Math.hpp>
  19. #include "Custom.h"
  20. //---------------------------------------------------------------------
  21. #pragma link "PasswordEdit"
  22. #ifndef NO_RESOURCES
  23. #pragma resource "*.dfm"
  24. #endif
  25. //---------------------------------------------------------------------
  26. __fastcall TCustomDialog::TCustomDialog(UnicodeString AHelpKeyword)
  27. : TForm(GetFormOwner())
  28. {
  29. UseSystemSettings(this);
  30. FControlPadding = ScaleByTextHeight(this, 8);
  31. FPos = ScaleByTextHeight(this, 8);
  32. FPrePos = FPos;
  33. FHorizontalMargin = ScaleByTextHeight(this, 8);
  34. FIndent = FHorizontalMargin;
  35. FGroupBox = NULL;
  36. HelpKeyword = AHelpKeyword;
  37. TBorderIcons BI = BorderIcons;
  38. if (HelpKeyword.IsEmpty())
  39. {
  40. BI >> biHelp;
  41. OKButton->Left = CancelButton->Left;
  42. CancelButton->Left = HelpButton->Left;
  43. HelpButton->Visible = false;
  44. }
  45. else
  46. {
  47. BI << biHelp;
  48. }
  49. BorderIcons = BI;
  50. }
  51. //---------------------------------------------------------------------
  52. bool __fastcall TCustomDialog::Execute()
  53. {
  54. Changed();
  55. return (ShowModal() == DefaultResult(this));
  56. }
  57. //---------------------------------------------------------------------
  58. void __fastcall TCustomDialog::DoChange(bool & /*CanSubmit*/)
  59. {
  60. // noop
  61. }
  62. //---------------------------------------------------------------------
  63. void __fastcall TCustomDialog::Changed()
  64. {
  65. bool CanSubmit = true;
  66. DoChange(CanSubmit);
  67. EnableControl(OKButton, CanSubmit);
  68. }
  69. //---------------------------------------------------------------------
  70. void __fastcall TCustomDialog::Change(TObject * /*Sender*/)
  71. {
  72. Changed();
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TCustomDialog::DoHelp()
  76. {
  77. FormHelp(this);
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TCustomDialog::HelpButtonClick(TObject * /*Sender*/)
  81. {
  82. DoHelp();
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCustomDialog::DoShow()
  86. {
  87. OKButton->TabOrder = FCount;
  88. CancelButton->TabOrder = static_cast<short>(FCount + 1);
  89. HelpButton->TabOrder = static_cast<short>(FCount + 2);
  90. Changed();
  91. TForm::DoShow();
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TCustomDialog::DoValidate()
  95. {
  96. // noop
  97. }
  98. //---------------------------------------------------------------------------
  99. bool __fastcall TCustomDialog::CloseQuery()
  100. {
  101. if (ModalResult == DefaultResult(this))
  102. {
  103. DoValidate();
  104. }
  105. return TForm::CloseQuery();
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCustomDialog::AddImage(const UnicodeString & ImageName)
  109. {
  110. TImage * Image = new TImage(this);
  111. Image->Name = L"Image";
  112. Image->Parent = GetDefaultParent();
  113. LoadDialogImage(Image, ImageName);
  114. Image->SetBounds(FIndent, FPos + ScaleByTextHeight(this, 3), Image->Picture->Width, Image->Picture->Height);
  115. FIndent += Image->Width + ScaleByTextHeight(this, 12);
  116. }
  117. //---------------------------------------------------------------------------
  118. int __fastcall TCustomDialog::GetMaxControlWidth(TControl * Control)
  119. {
  120. return GetDefaultParent()->ClientWidth - Control->Left - FHorizontalMargin;
  121. }
  122. //---------------------------------------------------------------------------
  123. TWinControl * __fastcall TCustomDialog::GetDefaultParent()
  124. {
  125. return (FGroupBox != NULL) ? FGroupBox : static_cast<TWinControl *>(this);
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TCustomDialog::AdjustHeight(TControl * Control)
  129. {
  130. FPos = Control->Top + Control->Height + FControlPadding;
  131. int Delta = (FPos - FPrePos);
  132. ClientHeight = ClientHeight + Delta;
  133. if (FGroupBox != NULL)
  134. {
  135. FGroupBox->Height = FGroupBox->Height + Delta;
  136. }
  137. FPrePos = FPos;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TCustomDialog::AddWinControl(TWinControl * Control)
  141. {
  142. Control->TabOrder = FCount;
  143. FCount++;
  144. }
  145. //---------------------------------------------------------------------------
  146. TCheckBox * __fastcall TCustomDialog::CreateAndAddCheckBox(const UnicodeString & Caption)
  147. {
  148. TCheckBox * CheckBox = new TCheckBox(this);
  149. CheckBox->Caption = Caption;
  150. AddButtonControl(CheckBox);
  151. return CheckBox;
  152. }
  153. //---------------------------------------------------------------------------
  154. TLabel * __fastcall TCustomDialog::CreateLabel(UnicodeString Label)
  155. {
  156. TLabel * Result = new TLabel(this);
  157. Result->Caption = Label;
  158. return Result;
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TCustomDialog::AddEditLikeControl(TWinControl * Edit, TLabel * Label, bool OneLine)
  162. {
  163. Edit->Parent = GetDefaultParent();
  164. // this updates Height property to real value
  165. Edit->HandleNeeded();
  166. Label->Parent = GetDefaultParent();
  167. Label->Left = FIndent;
  168. if (OneLine)
  169. {
  170. DebugAssert(Edit->Height > Label->Height);
  171. Label->Top = FPos + ((Edit->Height - Label->Height) / 2);
  172. }
  173. else
  174. {
  175. Label->Top = FPos;
  176. FPos += Label->Height + ScaleByTextHeight(this, 4);
  177. }
  178. Edit->Top = FPos;
  179. if (OneLine)
  180. {
  181. Edit->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Edit->Width;
  182. }
  183. else
  184. {
  185. Edit->Left = FIndent;
  186. Edit->Width = GetMaxControlWidth(Edit);
  187. }
  188. AdjustHeight(Edit);
  189. if (Label->FocusControl == NULL)
  190. {
  191. Label->FocusControl = Edit;
  192. }
  193. else
  194. {
  195. DebugAssert(Label->FocusControl == Edit);
  196. }
  197. AddWinControl(Edit);
  198. }
  199. //---------------------------------------------------------------------------
  200. void __fastcall TCustomDialog::AddEdit(TCustomEdit * Edit, TLabel * Label)
  201. {
  202. AddEditLikeControl(Edit, Label, false);
  203. TEdit * PublicEdit = reinterpret_cast<TEdit *>(Edit);
  204. if (PublicEdit->OnChange == NULL)
  205. {
  206. PublicEdit->OnChange = Change;
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCustomDialog::AddComboBox(TCustomCombo * Combo, TLabel * Label, TStrings * Items, bool OneLine)
  211. {
  212. AddEditLikeControl(Combo, Label, OneLine);
  213. if (Items != NULL)
  214. {
  215. Combo->Items = Items;
  216. }
  217. if (OneLine)
  218. {
  219. int Width = 0;
  220. for (int Index = 0; Index < Combo->Items->Count; Index++)
  221. {
  222. Width = Max(Width, Combo->Canvas->TextWidth(Combo->Items->Strings[Index]));
  223. }
  224. Width += ScaleByTextHeight(Combo, 4 + 16 + 14);
  225. Width = Max(Width, HelpButton->Width);
  226. Combo->Width = Width;
  227. Combo->Left = GetDefaultParent()->ClientWidth - FHorizontalMargin - Width;
  228. }
  229. TComboBox * PublicCombo = reinterpret_cast<TComboBox *>(Combo);
  230. if (PublicCombo->OnChange == NULL)
  231. {
  232. PublicCombo->OnChange = Change;
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TCustomDialog::ScaleButtonControl(TButtonControl * Control)
  237. {
  238. // this updates Height property to real value
  239. Control->HandleNeeded();
  240. // buttons do not scale with text on their own
  241. Control->Height = ScaleByTextHeight(Control, Control->Height);
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TCustomDialog::AddButtonControl(TButtonControl * Control)
  245. {
  246. Control->Parent = GetDefaultParent();
  247. Control->Left = FIndent + ScaleByTextHeight(this, 6);
  248. Control->Top = FPos;
  249. Control->Width = GetMaxControlWidth(Control);
  250. ScaleButtonControl(Control);
  251. AdjustHeight(Control);
  252. AddWinControl(Control);
  253. TCheckBox * PublicControl = reinterpret_cast<TCheckBox *>(Control);
  254. if (PublicControl->OnClick == NULL)
  255. {
  256. PublicControl->OnClick = Change;
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. void __fastcall TCustomDialog::AddText(TLabel * Label)
  261. {
  262. Label->Parent = GetDefaultParent();
  263. Label->WordWrap = true;
  264. Label->Left = FIndent;
  265. Label->Width = GetMaxControlWidth(Label);
  266. Label->Top = FPos;
  267. Label->ShowAccelChar = false;
  268. TRect TextRect;
  269. SetRect(&TextRect, 0, 0, Label->Width, 0);
  270. DrawText(Label->Canvas->Handle, Label->Caption.c_str(), Label->Caption.Length() + 1, &TextRect,
  271. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  272. Label->DrawTextBiDiModeFlagsReadingOnly());
  273. Label->Height = TextRect.Height();
  274. AdjustHeight(Label);
  275. }
  276. //---------------------------------------------------------------------------
  277. void __fastcall TCustomDialog::AddText(TStaticText * Label)
  278. {
  279. Label->Parent = GetDefaultParent();
  280. Label->Left = FIndent;
  281. Label->Width = GetMaxControlWidth(Label);
  282. Label->Top = FPos;
  283. Label->ShowAccelChar = false;
  284. AdjustHeight(Label);
  285. AddWinControl(Label);
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TCustomDialog::AddSeparator()
  289. {
  290. TBevel * Bevel = new TBevel(this);
  291. Bevel->Parent = GetDefaultParent();
  292. Bevel->Left = FIndent;
  293. Bevel->Top = FPos;
  294. Bevel->Height = 2;
  295. Bevel->Width = GetMaxControlWidth(Bevel);
  296. AdjustHeight(Bevel);
  297. }
  298. //---------------------------------------------------------------------------
  299. void __fastcall TCustomDialog::StartGroup(const UnicodeString & Caption)
  300. {
  301. if (FGroupBox != NULL)
  302. {
  303. FIndent = FGroupBox->Left;
  304. FPos = FGroupBox->Top + FGroupBox->Height + FControlPadding;
  305. FPrePos = FPos;
  306. FGroupBox = NULL;
  307. }
  308. TGroupBox * GroupBox = new TGroupBox(this);
  309. GroupBox->Parent = this;
  310. GroupBox->Caption = Caption;
  311. GroupBox->Left = FIndent;
  312. GroupBox->Top = FPos;
  313. GroupBox->Height = ScaleByTextHeight(GroupBox, 20);
  314. GroupBox->Width = GetMaxControlWidth(GroupBox);
  315. AdjustHeight(GroupBox);
  316. FPos = ScaleByTextHeight(this, 16);
  317. FPrePos = FPos;
  318. FIndent = FHorizontalMargin;
  319. FGroupBox = GroupBox;
  320. }
  321. //---------------------------------------------------------------------------
  322. //---------------------------------------------------------------------------
  323. class TSaveSessionDialog : public TCustomDialog
  324. {
  325. public:
  326. __fastcall TSaveSessionDialog(TComponent* AOwner);
  327. void __fastcall Init(bool CanSavePassword, bool NotRecommendedSavingPassword,
  328. TStrings * AdditionalFolders);
  329. bool __fastcall Execute(UnicodeString & SessionName, bool & SavePassword,
  330. bool & CreateShortcut, const UnicodeString & OriginalSessionName);
  331. protected:
  332. virtual void __fastcall DoValidate();
  333. virtual void __fastcall DoChange(bool & CanSubmit);
  334. private:
  335. UnicodeString FOriginalSessionName;
  336. TEdit * SessionNameEdit;
  337. TComboBox * FolderCombo;
  338. TCheckBox * SavePasswordCheck;
  339. TCheckBox * CreateShortcutCheck;
  340. UnicodeString FRootFolder;
  341. UnicodeString __fastcall GetSessionName();
  342. };
  343. //---------------------------------------------------------------------------
  344. // Need to have an Owner argument for SafeFormCreate
  345. __fastcall TSaveSessionDialog::TSaveSessionDialog(TComponent* /*AOwner*/) :
  346. TCustomDialog(HELP_SESSION_SAVE)
  347. {
  348. }
  349. //---------------------------------------------------------------------------
  350. void __fastcall TSaveSessionDialog::Init(bool CanSavePassword,
  351. bool NotRecommendedSavingPassword, TStrings * AdditionalFolders)
  352. {
  353. Caption = LoadStr(SAVE_SESSION_CAPTION);
  354. SessionNameEdit = new TEdit(this);
  355. AddEdit(SessionNameEdit, CreateLabel(LoadStr(SAVE_SESSION_PROMPT)));
  356. FRootFolder = LoadStr(SAVE_SESSION_ROOT_FOLDER2);
  357. std::unique_ptr<TStringList> Folders(new TStringList());
  358. if (AdditionalFolders != NULL)
  359. {
  360. Folders->AddStrings(AdditionalFolders);
  361. }
  362. for (int Index = 0; Index < StoredSessions->Count; Index++)
  363. {
  364. TSessionData * Data = StoredSessions->Sessions[Index];
  365. if (!Data->Special && !Data->IsWorkspace)
  366. {
  367. UnicodeString Folder = Data->FolderName;
  368. if (!Folder.IsEmpty() && Folders->IndexOf(Folder) < 0)
  369. {
  370. Folders->Add(Folder);
  371. }
  372. }
  373. }
  374. DebugAssert(!Folders->CaseSensitive);
  375. Folders->Sort();
  376. FolderCombo = new TComboBox(this);
  377. AddComboBox(FolderCombo, CreateLabel(LoadStr(SAVE_SESSION_FOLDER)));
  378. FolderCombo->DropDownCount = Max(FolderCombo->DropDownCount, 16);
  379. FolderCombo->Items->Add(FRootFolder);
  380. FolderCombo->Items->AddStrings(Folders.get());
  381. SavePasswordCheck = CreateAndAddCheckBox(
  382. LoadStr(NotRecommendedSavingPassword ? SAVE_SESSION_PASSWORD :
  383. (CustomWinConfiguration->UseMasterPassword ? SAVE_SESSION_PASSWORD_MASTER : SAVE_SESSION_PASSWORD_RECOMMENDED)));
  384. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  385. EnableControl(SavePasswordCheck, CanSavePassword);
  386. }
  387. //---------------------------------------------------------------------------
  388. bool __fastcall TSaveSessionDialog::Execute(
  389. UnicodeString & SessionName, bool & SavePassword, bool & CreateShortcut,
  390. const UnicodeString & OriginalSessionName)
  391. {
  392. FOriginalSessionName = OriginalSessionName;
  393. SessionNameEdit->Text = TSessionData::ExtractLocalName(SessionName);
  394. UnicodeString Folder = TSessionData::ExtractFolderName(SessionName);
  395. if (Folder.IsEmpty())
  396. {
  397. FolderCombo->Text = FRootFolder;
  398. }
  399. else
  400. {
  401. FolderCombo->Text = Folder;
  402. }
  403. SavePasswordCheck->Checked = SavePassword;
  404. CreateShortcutCheck->Checked = CreateShortcut;
  405. bool Result = TCustomDialog::Execute();
  406. if (Result)
  407. {
  408. SessionName = GetSessionName();
  409. SavePassword = SavePasswordCheck->Checked;
  410. CreateShortcut = CreateShortcutCheck->Checked;
  411. }
  412. return Result;
  413. }
  414. //---------------------------------------------------------------------------
  415. UnicodeString __fastcall TSaveSessionDialog::GetSessionName()
  416. {
  417. UnicodeString Folder;
  418. if (FolderCombo->Text != FRootFolder)
  419. {
  420. Folder = FolderCombo->Text;
  421. }
  422. return TSessionData::ComposePath(Folder, SessionNameEdit->Text);
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TSaveSessionDialog::DoValidate()
  426. {
  427. TSessionData::ValidateName(SessionNameEdit->Text);
  428. SessionNameValidate(GetSessionName(), FOriginalSessionName);
  429. UnicodeString Folder = TSessionData::ExtractFolderName(GetSessionName());
  430. if (!Folder.IsEmpty() && StoredSessions->IsWorkspace(Folder))
  431. {
  432. throw Exception(FMTLOAD(WORKSPACE_NOT_FOLDER, (Folder)));
  433. }
  434. if (SavePasswordCheck->Enabled && SavePasswordCheck->Checked &&
  435. CustomWinConfiguration->UseMasterPassword)
  436. {
  437. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  438. }
  439. TCustomDialog::DoValidate();
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TSaveSessionDialog::DoChange(bool & CanSubmit)
  443. {
  444. CanSubmit = !SessionNameEdit->Text.IsEmpty();
  445. TCustomDialog::DoChange(CanSubmit);
  446. }
  447. //---------------------------------------------------------------------------
  448. TSessionData * __fastcall DoSaveSession(TSessionData * SessionData,
  449. TSessionData * OriginalSession, bool ForceDialog,
  450. TStrings * AdditionalFolders)
  451. {
  452. bool SavePassword = false;
  453. bool * PSavePassword;
  454. bool NotRecommendedSavingPassword =
  455. !CustomWinConfiguration->UseMasterPassword &&
  456. !SameText(SessionData->UserName, AnonymousUserName);
  457. if (Configuration->DisablePasswordStoring ||
  458. !SessionData->HasAnySessionPassword())
  459. {
  460. PSavePassword = NULL;
  461. }
  462. else
  463. {
  464. PSavePassword = &SavePassword;
  465. SavePassword =
  466. ((OriginalSession != NULL) && OriginalSession->HasAnySessionPassword()) ||
  467. !NotRecommendedSavingPassword;
  468. }
  469. UnicodeString SessionName = SessionData->SessionName;
  470. bool Result;
  471. bool CreateShortcut = false;
  472. if (!ForceDialog && ((PSavePassword == NULL) || SavePassword))
  473. {
  474. CustomWinConfiguration->AskForMasterPasswordIfNotSetAndNeededToPersistSessionData(SessionData);
  475. Result = true;
  476. }
  477. else
  478. {
  479. // This can be a standalone dialog when used with save URL (from GetLoginData)
  480. TSaveSessionDialog * Dialog = SafeFormCreate<TSaveSessionDialog>();
  481. try
  482. {
  483. Dialog->Init((PSavePassword != NULL), NotRecommendedSavingPassword, AdditionalFolders);
  484. Result = Dialog->Execute(SessionName, SavePassword, CreateShortcut, SessionData->Name);
  485. }
  486. __finally
  487. {
  488. delete Dialog;
  489. }
  490. }
  491. TSessionData * NewSession = NULL;
  492. if (Result)
  493. {
  494. if ((PSavePassword != NULL) && !SavePassword)
  495. {
  496. SessionData->ClearSessionPasswords();
  497. }
  498. NewSession =
  499. StoredSessions->NewSession(SessionName, SessionData);
  500. // modified only, explicit
  501. StoredSessions->Save(false, true);
  502. if (!SessionData->HostKey.IsEmpty())
  503. {
  504. SessionData->CacheHostKeyIfNotCached();
  505. }
  506. if (CreateShortcut)
  507. {
  508. TOperationVisualizer Visualizer;
  509. UnicodeString AdditionalParams =
  510. TProgramParams::FormatSwitch(DESKTOP_SWITCH) + L" " +
  511. TProgramParams::FormatSwitch(UPLOAD_IF_ANY_SWITCH);
  512. CreateDesktopSessionShortCut(SessionName, L"", AdditionalParams, -1, SITE_ICON);
  513. }
  514. }
  515. return NewSession;
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall SessionNameValidate(const UnicodeString & Text,
  519. const UnicodeString & OriginalName)
  520. {
  521. TSessionData::ValidatePath(Text);
  522. DebugAssert(StoredSessions);
  523. TSessionData * Data = (TSessionData *)StoredSessions->FindByName(Text);
  524. if (Data && Data->Special)
  525. {
  526. MessageDialog(FMTLOAD(CANNOT_OVERWRITE_SPECIAL_SESSION, (Text)),
  527. qtError, qaOK, HELP_NONE);
  528. Abort();
  529. }
  530. else if ((Data != NULL) && !Data->IsSameName(OriginalName) &&
  531. MessageDialog(MainInstructions(FMTLOAD(CONFIRM_OVERWRITE_SESSION, (Text))),
  532. qtConfirmation, qaYes | qaNo, HELP_SESSION_SAVE_OVERWRITE) != qaYes)
  533. {
  534. Abort();
  535. }
  536. }
  537. //---------------------------------------------------------------------------
  538. //---------------------------------------------------------------------------
  539. class TSaveWorkspaceDialog : public TCustomDialog
  540. {
  541. public:
  542. __fastcall TSaveWorkspaceDialog(bool CanSavePasswords,
  543. bool NotRecommendedSavingPasswords);
  544. bool __fastcall Execute(
  545. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  546. bool & EnableAutoSave);
  547. protected:
  548. virtual void __fastcall DoValidate();
  549. virtual void __fastcall DoChange(bool & CanSubmit);
  550. private:
  551. TComboBox * WorkspaceNameCombo;
  552. TCheckBox * SavePasswordsCheck;
  553. TCheckBox * CreateShortcutCheck;
  554. TCheckBox * EnableAutoSaveCheck;
  555. };
  556. //---------------------------------------------------------------------------
  557. __fastcall TSaveWorkspaceDialog::TSaveWorkspaceDialog(
  558. bool CanSavePasswords, bool NotRecommendedSavingPasswords) :
  559. TCustomDialog(HELP_WORKSPACE_SAVE)
  560. {
  561. Caption = LoadStr(SAVE_WORKSPACE_CAPTION);
  562. WorkspaceNameCombo = new TComboBox(this);
  563. WorkspaceNameCombo->AutoComplete = false;
  564. AddComboBox(WorkspaceNameCombo, CreateLabel(LoadStr(SAVE_WORKSPACE_PROMPT)));
  565. WorkspaceNameCombo->DropDownCount = Max(WorkspaceNameCombo->DropDownCount, 16);
  566. std::unique_ptr<TStrings> Workspaces(StoredSessions->GetWorkspaces());
  567. WorkspaceNameCombo->Items->AddStrings(Workspaces.get());
  568. SavePasswordsCheck = CreateAndAddCheckBox(
  569. LoadStr(NotRecommendedSavingPasswords ? SAVE_WORKSPACE_PASSWORDS :
  570. (CustomWinConfiguration->UseMasterPassword ?
  571. SAVE_WORKSPACE_PASSWORDS_MASTER : SAVE_WORKSPACE_PASSWORDS_RECOMMENDED)));
  572. EnableControl(SavePasswordsCheck, CanSavePasswords);
  573. CreateShortcutCheck = CreateAndAddCheckBox(LoadStr(SAVE_SITE_WORKSPACE_SHORTCUT));
  574. EnableAutoSaveCheck = CreateAndAddCheckBox(LoadStr(SAVE_WORKSPACE_AUTO));
  575. }
  576. //---------------------------------------------------------------------------
  577. bool __fastcall TSaveWorkspaceDialog::Execute(
  578. UnicodeString & WorkspaceName, bool & SavePasswords, bool & CreateShortcut,
  579. bool & EnableAutoSave)
  580. {
  581. WorkspaceNameCombo->Text = WorkspaceName;
  582. SavePasswordsCheck->Checked = SavePasswords;
  583. CreateShortcutCheck->Checked = CreateShortcut;
  584. EnableAutoSaveCheck->Checked = EnableAutoSave;
  585. bool Result = TCustomDialog::Execute();
  586. if (Result)
  587. {
  588. WorkspaceName = WorkspaceNameCombo->Text;
  589. SavePasswords = SavePasswordsCheck->Checked;
  590. CreateShortcut = CreateShortcutCheck->Checked;
  591. EnableAutoSave = EnableAutoSaveCheck->Checked;
  592. }
  593. return Result;
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TSaveWorkspaceDialog::DoValidate()
  597. {
  598. TSessionData::ValidateName(WorkspaceNameCombo->Text);
  599. if (StoredSessions->IsFolder(WorkspaceNameCombo->Text))
  600. {
  601. throw Exception(FMTLOAD(FOLDER_NOT_WORKSPACE, (WorkspaceNameCombo->Text)));
  602. }
  603. if (SavePasswordsCheck->Enabled && SavePasswordsCheck->Checked &&
  604. CustomWinConfiguration->UseMasterPassword)
  605. {
  606. CustomWinConfiguration->AskForMasterPasswordIfNotSet();
  607. }
  608. TCustomDialog::DoValidate();
  609. }
  610. //---------------------------------------------------------------------------
  611. void __fastcall TSaveWorkspaceDialog::DoChange(bool & CanSubmit)
  612. {
  613. CanSubmit = !WorkspaceNameCombo->Text.IsEmpty();
  614. TCustomDialog::DoChange(CanSubmit);
  615. }
  616. //---------------------------------------------------------------------------
  617. bool __fastcall DoSaveWorkspaceDialog(UnicodeString & WorkspaceName,
  618. bool * SavePasswords, bool NotRecommendedSavingPasswords,
  619. bool & CreateShortcut, bool & EnableAutoSave)
  620. {
  621. std::unique_ptr<TSaveWorkspaceDialog> Dialog(
  622. new TSaveWorkspaceDialog((SavePasswords != NULL), NotRecommendedSavingPasswords));
  623. bool Dummy = false;
  624. if (SavePasswords == NULL)
  625. {
  626. SavePasswords = &Dummy;
  627. }
  628. return
  629. Dialog->Execute(
  630. WorkspaceName, *SavePasswords, CreateShortcut, EnableAutoSave);
  631. }
  632. //---------------------------------------------------------------------------
  633. //---------------------------------------------------------------------------
  634. class TShortCutDialog : public TCustomDialog
  635. {
  636. public:
  637. __fastcall TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
  638. bool __fastcall Execute(TShortCut & ShortCut);
  639. private:
  640. TComboBox * ShortCutCombo;
  641. };
  642. //---------------------------------------------------------------------------
  643. __fastcall TShortCutDialog::TShortCutDialog(const TShortCuts & ShortCuts, UnicodeString HelpKeyword) :
  644. TCustomDialog(HelpKeyword)
  645. {
  646. Caption = LoadStr(SHORTCUT_CAPTION);
  647. ShortCutCombo = new TComboBox(this);
  648. AddComboBox(ShortCutCombo, CreateLabel(LoadStr(SHORTCUT_LABEL)));
  649. InitializeShortCutCombo(ShortCutCombo, ShortCuts);
  650. }
  651. //---------------------------------------------------------------------------
  652. bool __fastcall TShortCutDialog::Execute(TShortCut & ShortCut)
  653. {
  654. SetShortCutCombo(ShortCutCombo, ShortCut);
  655. bool Result = TCustomDialog::Execute();
  656. if (Result)
  657. {
  658. ShortCut = GetShortCutCombo(ShortCutCombo);
  659. }
  660. return Result;
  661. }
  662. //---------------------------------------------------------------------------
  663. bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
  664. const TShortCuts & ShortCuts, UnicodeString HelpKeyword)
  665. {
  666. bool Result;
  667. TShortCutDialog * Dialog = new TShortCutDialog(ShortCuts, HelpKeyword);
  668. try
  669. {
  670. Result = Dialog->Execute(ShortCut);
  671. }
  672. __finally
  673. {
  674. delete Dialog;
  675. }
  676. return Result;
  677. }
  678. //---------------------------------------------------------------------------
  679. //---------------------------------------------------------------------------
  680. class TRemoteMoveDialog : public TCustomDialog
  681. {
  682. public:
  683. __fastcall TRemoteMoveDialog(bool Multi);
  684. bool __fastcall Execute(UnicodeString & Target, UnicodeString & FileMask);
  685. protected:
  686. DYNAMIC void __fastcall DoShow();
  687. virtual void __fastcall DoValidate();
  688. UnicodeString __fastcall GetFileMask();
  689. private:
  690. THistoryComboBox * Combo;
  691. bool FMulti;
  692. };
  693. //---------------------------------------------------------------------------
  694. __fastcall TRemoteMoveDialog::TRemoteMoveDialog(bool Multi) :
  695. TCustomDialog(HELP_REMOTE_MOVE)
  696. {
  697. Caption = LoadStr(REMOTE_MOVE_TITLE);
  698. // The same as TRemoteTransferDialog
  699. ClientWidth = ScaleByTextHeight(this, 420);
  700. FMulti = Multi;
  701. AddImage(L"Move To");
  702. Combo = new THistoryComboBox(this);
  703. Combo->AutoComplete = false;
  704. AddComboBox(Combo, CreateLabel(LoadStr(REMOTE_TRANSFER_PROMPT2)));
  705. }
  706. //---------------------------------------------------------------------------
  707. bool __fastcall TRemoteMoveDialog::Execute(UnicodeString & Target, UnicodeString & FileMask)
  708. {
  709. Combo->Items = CustomWinConfiguration->History[L"RemoteTarget"];
  710. Combo->Text = UnixIncludeTrailingBackslash(Target) + FileMask;
  711. bool Result = TCustomDialog::Execute();
  712. if (Result)
  713. {
  714. Target = UnixExtractFilePath(Combo->Text);
  715. FileMask = GetFileMask();
  716. Combo->SaveToHistory();
  717. CustomWinConfiguration->History[L"RemoteTarget"] = Combo->Items;
  718. }
  719. return Result;
  720. }
  721. //---------------------------------------------------------------------------
  722. UnicodeString __fastcall TRemoteMoveDialog::GetFileMask()
  723. {
  724. return UnixExtractFileName(Combo->Text);
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TRemoteMoveDialog::DoShow()
  728. {
  729. TCustomDialog::DoShow();
  730. InstallPathWordBreakProc(Combo);
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TRemoteMoveDialog::DoValidate()
  734. {
  735. if (!IsFileNameMask(GetFileMask()) && FMulti)
  736. {
  737. UnicodeString Message =
  738. FormatMultiFilesToOneConfirmation(Combo->Text, true);
  739. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_NONE) == qaCancel)
  740. {
  741. Abort();
  742. }
  743. }
  744. TCustomDialog::DoValidate();
  745. }
  746. //---------------------------------------------------------------------------
  747. bool __fastcall DoRemoteMoveDialog(bool Multi, UnicodeString & Target, UnicodeString & FileMask)
  748. {
  749. std::unique_ptr<TRemoteMoveDialog> Dialog(new TRemoteMoveDialog(Multi));
  750. return Dialog->Execute(Target, FileMask);
  751. }
  752. //---------------------------------------------------------------------------
  753. //---------------------------------------------------------------------------
  754. class TCustomCommandOptionsDialog : public TCustomDialog
  755. {
  756. public:
  757. __fastcall TCustomCommandOptionsDialog(
  758. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags);
  759. bool __fastcall Execute();
  760. protected:
  761. virtual void __fastcall DoHelp();
  762. DYNAMIC void __fastcall DoShow();
  763. private:
  764. const TCustomCommandType * FCommand;
  765. TStrings * FCustomCommandOptions;
  766. std::vector<TControl *> FControls;
  767. std::vector<std::vector<UnicodeString> > FValues;
  768. unsigned int FFlags;
  769. UnicodeString __fastcall HistoryKey(const TCustomCommandType::TOption & Option);
  770. THistoryComboBox * __fastcall CreateHistoryComboBox(const TCustomCommandType::TOption & Option, const UnicodeString & Value);
  771. void __fastcall BrowseButtonClick(TObject * Sender);
  772. void __fastcall LinkLabelClick(TObject * Sender);
  773. UnicodeString __fastcall SaveHistoryComboBoxValue(TControl * Control, const TCustomCommandType::TOption & Option);
  774. void __fastcall AddOptionComboBox(
  775. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option,
  776. std::vector<UnicodeString> & Values);
  777. UnicodeString __fastcall GetComboBoxValue(TControl * Control, const UnicodeString & Default);
  778. int __fastcall GetOptionIndex(TControl * Control);
  779. int __fastcall GetControlIndex(TControl * Control);
  780. };
  781. //---------------------------------------------------------------------------
  782. __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
  783. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags) :
  784. TCustomDialog(HELP_EXTENSION_OPTIONS)
  785. {
  786. FCommand = Command;
  787. FFlags = Flags;
  788. FCustomCommandOptions = CustomCommandOptions;
  789. Caption = FMTLOAD(EXTENSION_OPTIONS_CAPTION, (StripEllipsis(StripHotkey(FCommand->Name))));
  790. Width = ScaleByTextHeight(this, 400);
  791. int ControlIndex = 0;
  792. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  793. {
  794. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  795. if ((Option.Flags & FFlags) != 0)
  796. {
  797. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  798. UnicodeString Value;
  799. if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
  800. {
  801. Value = FCustomCommandOptions->Values[OptionKey];
  802. }
  803. else
  804. {
  805. Value = Option.Default;
  806. }
  807. int Tag = (OptionIndex << 16) + ControlIndex;
  808. TControl * Control = NULL;
  809. std::vector<UnicodeString> Values;
  810. if (Option.Kind == TCustomCommandType::okUnknown)
  811. {
  812. Control = NULL;
  813. }
  814. else if (Option.Kind == TCustomCommandType::okLabel)
  815. {
  816. TLabel * Label = CreateLabel(Option.Caption);
  817. AddText(Label);
  818. Control = Label;
  819. }
  820. else if (Option.Kind == TCustomCommandType::okLink)
  821. {
  822. TStaticText * Label = new TStaticText(this);
  823. Label->Caption = Option.Caption;
  824. if (IsHttpOrHttpsUrl(Label->Caption))
  825. {
  826. Label->Caption = SecureUrl(Label->Caption);
  827. LinkLabel(Label);
  828. Label->TabStop = true;
  829. }
  830. else if (!Option.Default.IsEmpty() && IsHttpOrHttpsUrl(Option.Default))
  831. {
  832. Label->OnClick = LinkLabelClick;
  833. LinkLabel(Label);
  834. Label->TabStop = true;
  835. }
  836. else
  837. {
  838. // keep it plain text, as we have no URL
  839. }
  840. AddText(Label);
  841. Control = Label;
  842. }
  843. else if (Option.Kind == TCustomCommandType::okGroup)
  844. {
  845. StartGroup(Option.Caption);
  846. }
  847. else if (Option.Kind == TCustomCommandType::okSeparator)
  848. {
  849. AddSeparator();
  850. }
  851. else if (Option.Kind == TCustomCommandType::okTextBox)
  852. {
  853. Control = CreateHistoryComboBox(Option, Value);
  854. }
  855. else if (Option.Kind == TCustomCommandType::okFile)
  856. {
  857. THistoryComboBox * ComboBox = CreateHistoryComboBox(Option, Value);
  858. TButton * Button = new TButton(this);
  859. Button->Parent = GetDefaultParent();
  860. Button->Width = HelpButton->Width;
  861. Button->Left = GetDefaultParent()->ClientWidth - Button->Width - HorizontalMargin;
  862. ComboBox->Width = Button->Left - ComboBox->Left - ScaleByTextHeight(this, 6);
  863. Button->Top = ComboBox->Top - ScaleByTextHeight(this, 2);
  864. Button->Tag = Tag;
  865. Button->Caption = LoadStr(EXTENSION_OPTIONS_BROWSE);
  866. Button->OnClick = BrowseButtonClick;
  867. ScaleButtonControl(Button);
  868. AddWinControl(Button);
  869. Control = ComboBox;
  870. }
  871. else if (Option.Kind == TCustomCommandType::okDropDownList)
  872. {
  873. TComboBox * ComboBox = new TComboBox(this);
  874. ComboBox->Style = csDropDownList;
  875. AddOptionComboBox(ComboBox, Value, Option, Values);
  876. Control = ComboBox;
  877. }
  878. else if (Option.Kind == TCustomCommandType::okComboBox)
  879. {
  880. TComboBox * ComboBox = new TComboBox(this);
  881. ComboBox->Style = csDropDown;
  882. AddOptionComboBox(ComboBox, Value, Option, Values);
  883. if (ComboBox->ItemIndex < 0)
  884. {
  885. ComboBox->Text = Value;
  886. }
  887. Control = ComboBox;
  888. }
  889. else if (Option.Kind == TCustomCommandType::okCheckBox)
  890. {
  891. TCheckBox * CheckBox = CreateAndAddCheckBox(Option.Caption);
  892. CheckBox->Checked =
  893. (Option.Params.size() >= 1) &&
  894. (Value == Option.Params[0]);
  895. Control = CheckBox;
  896. }
  897. else
  898. {
  899. DebugFail();
  900. }
  901. if (Control != NULL)
  902. {
  903. Control->Tag = Tag;
  904. }
  905. FControls.push_back(Control);
  906. FValues.push_back(Values);
  907. ControlIndex++;
  908. DebugAssert(static_cast<int>(FControls.size()) == ControlIndex);
  909. }
  910. }
  911. }
  912. //---------------------------------------------------------------------------
  913. void __fastcall TCustomCommandOptionsDialog::AddOptionComboBox(
  914. TComboBox * ComboBox, const UnicodeString & Value, const TCustomCommandType::TOption & Option, std::vector<UnicodeString> & Values)
  915. {
  916. std::unique_ptr<TStringList> Items(new TStringList());
  917. int ItemIndex = -1;
  918. TCustomCommandType::TOption::TParams::const_iterator ParamI = Option.Params.begin();
  919. while (ParamI != Option.Params.end())
  920. {
  921. UnicodeString Item = (*ParamI);
  922. int P = Item.Pos(L"=");
  923. UnicodeString ParamValue;
  924. if (P > 0)
  925. {
  926. ParamValue = Item.SubString(1, P - 1);
  927. Item.Delete(1, P);
  928. }
  929. else
  930. {
  931. ParamValue = Item;
  932. }
  933. Items->Add(Item);
  934. if (Value == ParamValue)
  935. {
  936. ItemIndex = Items->Count - 1;
  937. }
  938. Values.push_back(ParamValue);
  939. ParamI++;
  940. }
  941. AddComboBox(ComboBox, CreateLabel(Option.Caption), Items.get(), true);
  942. ComboBox->ItemIndex = ItemIndex;
  943. }
  944. //---------------------------------------------------------------------------
  945. int __fastcall TCustomCommandOptionsDialog::GetOptionIndex(TControl * Control)
  946. {
  947. return (Control->Tag >> 16);
  948. }
  949. //---------------------------------------------------------------------------
  950. int __fastcall TCustomCommandOptionsDialog::GetControlIndex(TControl * Control)
  951. {
  952. return (Control->Tag & 0xFFFF);
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TCustomCommandOptionsDialog::LinkLabelClick(TObject * Sender)
  956. {
  957. TStaticText * Label = DebugNotNull(dynamic_cast<TStaticText *>(Sender));
  958. const TCustomCommandType::TOption & Option = FCommand->GetOption(GetOptionIndex(Label));
  959. OpenBrowser(SecureUrl(Option.Default));
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TCustomCommandOptionsDialog::BrowseButtonClick(TObject * Sender)
  963. {
  964. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  965. int OptionIndex = GetOptionIndex(Button);
  966. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  967. int ControlIndex = GetControlIndex(Button);
  968. THistoryComboBox * ComboBox = dynamic_cast<THistoryComboBox *>(FControls[ControlIndex]);
  969. std::unique_ptr<TOpenDialog> OpenDialog(new TOpenDialog(Application));
  970. UnicodeString Title;
  971. if (!Option.FileCaption.IsEmpty())
  972. {
  973. Title = Option.FileCaption;
  974. }
  975. else
  976. {
  977. UnicodeString Caption = Option.Caption;
  978. Caption = StripHotkey(Caption);
  979. if (!Caption.IsEmpty() && (Caption[Caption.Length()] == L':'))
  980. {
  981. Caption.SetLength(Caption.Length() - 1);
  982. }
  983. Title = FMTLOAD(EXTENSION_OPTIONS_BROWSE_TITLE, (Caption));
  984. }
  985. OpenDialog->Title = Title;
  986. UnicodeString Value;
  987. if (ComboBox->Text.IsEmpty())
  988. {
  989. Value = Option.FileInitial;
  990. }
  991. else
  992. {
  993. Value = ComboBox->Text;
  994. }
  995. UnicodeString ExpandedValue = ExpandEnvironmentVariables(Value);
  996. OpenDialog->FileName = ExpandedValue;
  997. UnicodeString InitialDir = ExtractFilePath(ExpandedValue);
  998. if (!InitialDir.IsEmpty())
  999. {
  1000. OpenDialog->InitialDir = InitialDir;
  1001. }
  1002. OpenDialog->Filter = Option.FileFilter;
  1003. OpenDialog->DefaultExt = Option.FileExt;
  1004. if (OpenDialog->Execute())
  1005. {
  1006. if (OpenDialog->FileName != ExpandedValue)
  1007. {
  1008. ComboBox->Text = OpenDialog->FileName;
  1009. }
  1010. // If user just confirms the initial value, persist it
  1011. else if (ComboBox->Text.IsEmpty())
  1012. {
  1013. DebugAssert(Option.FileInitial == Value);
  1014. ComboBox->Text = Value;
  1015. }
  1016. }
  1017. }
  1018. //---------------------------------------------------------------------------
  1019. THistoryComboBox * __fastcall TCustomCommandOptionsDialog::CreateHistoryComboBox(
  1020. const TCustomCommandType::TOption & Option, const UnicodeString & Value)
  1021. {
  1022. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  1023. ComboBox->AutoComplete = false;
  1024. AddComboBox(ComboBox, CreateLabel(Option.Caption));
  1025. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Option)];
  1026. ComboBox->Text = Value;
  1027. return ComboBox;
  1028. }
  1029. //---------------------------------------------------------------------------
  1030. UnicodeString __fastcall TCustomCommandOptionsDialog::HistoryKey(const TCustomCommandType::TOption & Option)
  1031. {
  1032. UnicodeString Result = FCommand->GetOptionKey(Option);
  1033. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  1034. return L"CustomCommandOption_" + Result;
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. bool __fastcall TCustomCommandOptionsDialog::Execute()
  1038. {
  1039. bool Result = TCustomDialog::Execute();
  1040. if (Result)
  1041. {
  1042. int ControlIndex = 0;
  1043. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1044. {
  1045. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1046. if ((Option.Flags & FFlags) != 0)
  1047. {
  1048. if ((Option.Kind != TCustomCommandType::okUnknown) &&
  1049. Option.IsControl)
  1050. {
  1051. UnicodeString OptionKey = FCommand->GetOptionKey(Option);
  1052. TControl * Control = FControls[ControlIndex];
  1053. UnicodeString Value;
  1054. if (Option.Kind == TCustomCommandType::okTextBox)
  1055. {
  1056. Value = SaveHistoryComboBoxValue(Control, Option);
  1057. }
  1058. else if (Option.Kind == TCustomCommandType::okFile)
  1059. {
  1060. Value = SaveHistoryComboBoxValue(Control, Option);
  1061. }
  1062. else if (Option.Kind == TCustomCommandType::okDropDownList)
  1063. {
  1064. Value = GetComboBoxValue(Control, Option.Default);
  1065. }
  1066. else if (Option.Kind == TCustomCommandType::okComboBox)
  1067. {
  1068. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1069. Value = GetComboBoxValue(Control, ComboBox->Text);
  1070. }
  1071. else if (Option.Kind == TCustomCommandType::okCheckBox)
  1072. {
  1073. TCheckBox * CheckBox = DebugNotNull(dynamic_cast<TCheckBox *>(Control));
  1074. int Index = (CheckBox->Checked ? 0 : 1);
  1075. Value = (Index < static_cast<int>(Option.Params.size())) ? Option.Params[Index] : UnicodeString();
  1076. }
  1077. else
  1078. {
  1079. DebugFail();
  1080. }
  1081. // The default value setter deletes the "name" when the value is empty.
  1082. // It would cause us to fall back to the default value, but we want to remember the empty value.
  1083. if (Value.IsEmpty())
  1084. {
  1085. int Index = FCustomCommandOptions->IndexOfName(OptionKey);
  1086. if (Index < 0)
  1087. {
  1088. Index = FCustomCommandOptions->Add(L"");
  1089. }
  1090. UnicodeString Line = OptionKey + FCustomCommandOptions->NameValueSeparator;
  1091. FCustomCommandOptions->Strings[Index] = Line;
  1092. }
  1093. else
  1094. {
  1095. FCustomCommandOptions->Values[OptionKey] = Value;
  1096. }
  1097. }
  1098. ControlIndex++;
  1099. }
  1100. }
  1101. }
  1102. return Result;
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. UnicodeString __fastcall TCustomCommandOptionsDialog::GetComboBoxValue(
  1106. TControl * Control, const UnicodeString & Default)
  1107. {
  1108. TComboBox * ComboBox = DebugNotNull(dynamic_cast<TComboBox *>(Control));
  1109. UnicodeString Result;
  1110. if (ComboBox->ItemIndex < 0)
  1111. {
  1112. Result = Default;
  1113. }
  1114. else
  1115. {
  1116. Result = FValues[GetControlIndex(Control)][ComboBox->ItemIndex];
  1117. }
  1118. return Result;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. UnicodeString __fastcall TCustomCommandOptionsDialog::SaveHistoryComboBoxValue(
  1122. TControl * Control, const TCustomCommandType::TOption & Option)
  1123. {
  1124. THistoryComboBox * ComboBox = DebugNotNull(dynamic_cast<THistoryComboBox *>(Control));
  1125. ComboBox->SaveToHistory();
  1126. CustomWinConfiguration->History[HistoryKey(Option)] = ComboBox->Items;
  1127. return ComboBox->Text;
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TCustomCommandOptionsDialog::DoHelp()
  1131. {
  1132. UnicodeString HelpPage;
  1133. if (!FCommand->OptionsPage.IsEmpty())
  1134. {
  1135. HelpPage = FCommand->OptionsPage;
  1136. }
  1137. else
  1138. {
  1139. HelpPage = FCommand->HomePage;
  1140. }
  1141. if (!HelpPage.IsEmpty())
  1142. {
  1143. OpenBrowser(HelpPage);
  1144. }
  1145. else
  1146. {
  1147. TCustomDialog::DoHelp();
  1148. }
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. void __fastcall TCustomCommandOptionsDialog::DoShow()
  1152. {
  1153. TCustomDialog::DoShow();
  1154. int ControlIndex = 0;
  1155. for (int OptionIndex = 0; OptionIndex < FCommand->OptionsCount; OptionIndex++)
  1156. {
  1157. const TCustomCommandType::TOption & Option = FCommand->GetOption(OptionIndex);
  1158. if ((Option.Flags & FFlags) != 0)
  1159. {
  1160. if (Option.Kind == TCustomCommandType::okFile)
  1161. {
  1162. TControl * Control = FControls[ControlIndex];
  1163. InstallPathWordBreakProc(DebugNotNull(dynamic_cast<TWinControl *>(Control)));
  1164. }
  1165. ControlIndex++;
  1166. }
  1167. }
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. bool __fastcall DoCustomCommandOptionsDialog(
  1171. const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags)
  1172. {
  1173. std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
  1174. new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags));
  1175. return Dialog->Execute();
  1176. }