Files
123123/ZA.CoreService.ESBCertificateManager/Setup/AddTargetPathForm.cs
T

211 lines
6.2 KiB
C#

using ZA.CoreService.ESBCertificateManager.Models;
namespace ZA.CoreService.ESBCertificateManager.Setup;
public sealed class AddTargetPathForm : Form
{
private readonly SetupCoordinator _coordinator;
private readonly ComboBox _cmbContainer;
private readonly TextBox _txtDirectory;
private readonly TextBox _txtFileName;
private readonly TextBox _txtBackupFolder;
private readonly CheckBox _chkBackup;
private readonly Button _btnSave;
private bool _running;
public int? CreatedCertificateTargetId { get; private set; }
public AddTargetPathForm(
SetupCoordinator coordinator,
int? preferredSonicContainerId = null)
{
_coordinator = coordinator;
Text = "Zertifikat-Pfad hinzufügen";
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
ClientSize = new Size(560, 450);
BackColor = SettingsUi.Background;
ForeColor = SettingsUi.Text;
Font = new Font("Segoe UI", 10f);
Label title = new()
{
Text = "Neuer Zertifikat-Pfad",
Location = new Point(28, 22),
AutoSize = true,
Font = new Font("Segoe UI Semibold", 18f, FontStyle.Bold),
ForeColor = SettingsUi.Text
};
Label subtitle = new()
{
Text =
"Zielverzeichnis und Dateiname für die Zertifikatsablage.",
Location = new Point(30, 58),
AutoSize = true,
ForeColor = SettingsUi.Muted
};
Panel card = SettingsUi.CreateCard();
card.Location = new Point(28, 95);
card.Size = new Size(504, 270);
card.Paint += SettingsUi.PaintCardBorder;
_cmbContainer = SettingsUi.CreateComboBox();
_txtDirectory = SettingsUi.CreateTextBox();
_txtFileName = SettingsUi.CreateTextBox();
_txtBackupFolder = SettingsUi.CreateTextBox();
_txtBackupFolder.Text = "Backup";
_txtFileName.Text = "server.crt";
_chkBackup = new CheckBox
{
Text = "Backup vor dem Überschreiben",
AutoSize = true,
Checked = true,
ForeColor = SettingsUi.Text,
Font = new Font("Segoe UI", 9.5f)
};
AddField(card, "Container", _cmbContainer, 18, 18, 460);
AddField(card, "Zielverzeichnis", _txtDirectory, 18, 78, 460);
AddField(card, "Dateiname", _txtFileName, 18, 138, 220);
AddField(card, "Backup-Ordner", _txtBackupFolder, 258, 138, 220);
_chkBackup.Location = new Point(18, 210);
card.Controls.Add(_chkBackup);
_btnSave = SettingsUi.CreatePrimaryButton("Pfad anlegen");
_btnSave.Location = new Point(312, 385);
_btnSave.Size = new Size(220, 40);
_btnSave.Click += async (_, _) => await SaveAsync();
Button cancel = SettingsUi.CreateGhostButton("Abbrechen");
cancel.Location = new Point(190, 385);
cancel.Size = new Size(110, 40);
cancel.Click += (_, _) =>
{
DialogResult = DialogResult.Cancel;
Close();
};
Controls.Add(title);
Controls.Add(subtitle);
Controls.Add(card);
Controls.Add(cancel);
Controls.Add(_btnSave);
Shown += async (_, _) =>
await LoadContainersAsync(preferredSonicContainerId);
}
private async Task LoadContainersAsync(int? preferredSonicContainerId)
{
try
{
IReadOnlyList<ContainerOption> containers =
await _coordinator.LoadContainersAsync();
_cmbContainer.Items.Clear();
int selected = 0;
for (int index = 0; index < containers.Count; index++)
{
_cmbContainer.Items.Add(containers[index]);
if (preferredSonicContainerId is int id
&& containers[index].SonicContainerId == id)
{
selected = index;
}
}
if (_cmbContainer.Items.Count > 0)
{
_cmbContainer.SelectedIndex = selected;
}
}
catch (Exception ex)
{
MessageBox.Show(
this,
ex.Message,
"Zertifikat-Pfad",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private async Task SaveAsync()
{
if (_running)
{
return;
}
if (_cmbContainer.SelectedItem is not ContainerOption container)
{
MessageBox.Show(
this,
"Bitte einen Container wählen.",
"Zertifikat-Pfad",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
_running = true;
_btnSave.Enabled = false;
Cursor = Cursors.WaitCursor;
try
{
CreatedCertificateTargetId =
await _coordinator.AddCertificateTargetAsync(
container.SonicContainerId,
_txtDirectory.Text,
_txtFileName.Text,
_chkBackup.Checked,
_txtBackupFolder.Text);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(
this,
ex.Message,
"Zertifikat-Pfad",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
_running = false;
_btnSave.Enabled = true;
Cursor = Cursors.Default;
}
}
private static void AddField(
Control parent,
string label,
Control input,
int x,
int y,
int width)
{
Label fieldLabel = SettingsUi.CreateFieldLabel(label);
fieldLabel.Location = new Point(x, y);
input.Location = new Point(x, y + 22);
input.Width = width;
parent.Controls.Add(fieldLabel);
parent.Controls.Add(input);
}
}