using System; using System.Collections.Generic; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; using StarfaceOutlookSync.Models; using StarfaceOutlookSync.Services; namespace StarfaceOutlookSync.UI { public class ProfileEditorForm : Form { private readonly ProfileManager _pm = new ProfileManager(); private readonly SyncProfile _existingProfile; private readonly bool _isNew; // Controls private TextBox _txtName, _txtHost, _txtPort, _txtLoginId, _txtPassword, _txtOutlookFolder; private CheckBox _chkSsl, _chkEnabled; private ComboBox _cmbAddressBook, _cmbDirection; private Button _btnBrowseFolder; private NumericUpDown _numAutoSync; private Button _btnTest, _btnLoadBooks, _btnSave, _btnCancel; private Label _lblTestResult; private List _addressBooks = new List(); private List _outlookFolderPaths = new List(); private string _selectedOutlookPath = ""; private string _selectedOutlookName = ""; public ProfileEditorForm(SyncProfile profile) { _existingProfile = profile; _isNew = profile == null; InitializeComponent(); LoadData(); } private void InitializeComponent() { Text = _isNew ? "Neues Sync-Profil" : "Profil bearbeiten"; Size = new Size(480, 620); FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false; StartPosition = FormStartPosition.CenterParent; Font = new Font("Segoe UI", 9); var panel = new Panel { Dock = DockStyle.Fill, AutoScroll = true, Padding = new Padding(16) }; int y = 8; // Profilname panel.Controls.Add(MakeLabel("Profilname:", 12, y)); y += 22; _txtName = new TextBox { Left = 12, Top = y, Width = 420 }; panel.Controls.Add(_txtName); y += 32; // === Starface === panel.Controls.Add(MakeSectionLabel("Starface-Verbindung", 12, y)); y += 26; panel.Controls.Add(MakeLabel("Host / IP-Adresse:", 12, y)); y += 22; _txtHost = new TextBox { Left = 12, Top = y, Width = 320 }; _txtPort = new TextBox { Left = 340, Top = y, Width = 60, Text = "443" }; panel.Controls.Add(_txtHost); panel.Controls.Add(MakeLabel("Port:", 340, y - 22)); panel.Controls.Add(_txtPort); y += 32; _chkSsl = new CheckBox { Text = "HTTPS verwenden", Left = 12, Top = y, Checked = true, AutoSize = true }; panel.Controls.Add(_chkSsl); y += 28; panel.Controls.Add(MakeLabel("Login-ID:", 12, y)); y += 22; _txtLoginId = new TextBox { Left = 12, Top = y, Width = 200 }; panel.Controls.Add(_txtLoginId); y += 32; panel.Controls.Add(MakeLabel("Kennwort:", 12, y)); y += 22; _txtPassword = new TextBox { Left = 12, Top = y, Width = 200, UseSystemPasswordChar = true }; panel.Controls.Add(_txtPassword); y += 32; _btnTest = new Button { Text = "Verbindung testen", Left = 12, Top = y, Width = 130, Height = 28 }; _btnTest.Click += async (s, e) => await TestConnection(); _btnLoadBooks = new Button { Text = "Adressbuecher laden", Left = 150, Top = y, Width = 140, Height = 28 }; _btnLoadBooks.Click += async (s, e) => await LoadAddressBooks(); panel.Controls.Add(_btnTest); panel.Controls.Add(_btnLoadBooks); y += 32; _lblTestResult = new Label { Left = 12, Top = y, Width = 420, Height = 20, ForeColor = Color.Gray }; panel.Controls.Add(_lblTestResult); y += 26; panel.Controls.Add(MakeLabel("Starface-Adressbuch:", 12, y)); y += 22; _cmbAddressBook = new ComboBox { Left = 12, Top = y, Width = 420, DropDownStyle = ComboBoxStyle.DropDownList }; panel.Controls.Add(_cmbAddressBook); y += 32; // === Outlook === panel.Controls.Add(MakeSectionLabel("Outlook-Einstellungen", 12, y)); y += 26; panel.Controls.Add(MakeLabel("Kontakte-Ordner:", 12, y)); y += 22; _txtOutlookFolder = new TextBox { Left = 12, Top = y, Width = 330, ReadOnly = true, BackColor = SystemColors.Window }; _btnBrowseFolder = new Button { Text = "Durchsuchen...", Left = 348, Top = y - 1, Width = 84, Height = 24 }; _btnBrowseFolder.Click += (s, e) => BrowseOutlookFolder(); panel.Controls.Add(_txtOutlookFolder); panel.Controls.Add(_btnBrowseFolder); y += 32; panel.Controls.Add(MakeLabel("Sync-Richtung:", 12, y)); y += 22; _cmbDirection = new ComboBox { Left = 12, Top = y, Width = 250, DropDownStyle = ComboBoxStyle.DropDownList }; _cmbDirection.Items.AddRange(new object[] { "Bidirektional", "Outlook -> Starface", "Starface -> Outlook" }); _cmbDirection.SelectedIndex = 0; panel.Controls.Add(_cmbDirection); y += 32; panel.Controls.Add(MakeLabel("Auto-Sync Intervall (Minuten, 0 = manuell):", 12, y)); y += 22; _numAutoSync = new NumericUpDown { Left = 12, Top = y, Width = 80, Minimum = 0, Maximum = 1440, Value = 0 }; panel.Controls.Add(_numAutoSync); y += 32; _chkEnabled = new CheckBox { Text = "Profil aktiviert", Left = 12, Top = y, Checked = true, AutoSize = true }; panel.Controls.Add(_chkEnabled); y += 36; // Buttons _btnSave = new Button { Text = "Speichern", Left = 12, Top = y, Width = 100, Height = 30, DialogResult = DialogResult.None }; _btnSave.Click += (s, e) => SaveProfile(); _btnCancel = new Button { Text = "Abbrechen", Left = 120, Top = y, Width = 100, Height = 30, DialogResult = DialogResult.Cancel }; panel.Controls.Add(_btnSave); panel.Controls.Add(_btnCancel); Controls.Add(panel); AcceptButton = _btnSave; CancelButton = _btnCancel; } private Label MakeLabel(string text, int x, int y) { return new Label { Text = text, Left = x, Top = y, AutoSize = true }; } private Label MakeSectionLabel(string text, int x, int y) { return new Label { Text = text, Left = x, Top = y, AutoSize = true, Font = new Font("Segoe UI", 10, FontStyle.Bold) }; } private void LoadData() { // Outlook-Ordner laden try { using (var outlook = new OutlookContactsService()) { _outlookFolderPaths = outlook.GetContactFolderPaths(); } } catch (Exception ex) { MessageBox.Show( $"Outlook-Kontaktordner konnten nicht geladen werden:\n{ex.Message}\n\nIst Outlook gestartet?", "Outlook-Verbindung", MessageBoxButtons.OK, MessageBoxIcon.Warning); _outlookFolderPaths = new List(); } // Bestehende Werte laden if (_existingProfile != null) { _txtName.Text = _existingProfile.Name; _txtHost.Text = _existingProfile.StarfaceConnection.Host; _txtPort.Text = _existingProfile.StarfaceConnection.Port.ToString(); _chkSsl.Checked = _existingProfile.StarfaceConnection.UseSsl; _txtLoginId.Text = _existingProfile.StarfaceConnection.LoginId; _txtPassword.Text = _existingProfile.StarfaceConnection.Password; _chkEnabled.Checked = _existingProfile.Enabled; _numAutoSync.Value = _existingProfile.AutoSyncIntervalMinutes; _cmbDirection.SelectedIndex = (int)_existingProfile.SyncDirection; // Outlook-Ordner _selectedOutlookPath = _existingProfile.OutlookFolderPath; _selectedOutlookName = _existingProfile.OutlookFolderName; _txtOutlookFolder.Text = _selectedOutlookPath; // Adressbuch if (_existingProfile.StarfaceAddressBook != null) { _addressBooks.Add(_existingProfile.StarfaceAddressBook); _cmbAddressBook.Items.Add(_existingProfile.StarfaceAddressBook.Name); _cmbAddressBook.SelectedIndex = 0; } } else if (_outlookFolderPaths.Count > 0) { // Standard-Ordner vorauswaehlen _selectedOutlookPath = _outlookFolderPaths[0]; _selectedOutlookName = _selectedOutlookPath.Substring(_selectedOutlookPath.LastIndexOf('\\') + 1); _txtOutlookFolder.Text = _selectedOutlookPath; } } private void BrowseOutlookFolder() { using (var browser = new OutlookFolderBrowserForm(_outlookFolderPaths, _selectedOutlookPath)) { if (browser.ShowDialog(this) == DialogResult.OK) { _selectedOutlookPath = browser.SelectedFolderPath; _selectedOutlookName = browser.SelectedFolderName; _txtOutlookFolder.Text = _selectedOutlookPath; } } } private StarfaceConnection GetConnection() { return new StarfaceConnection { Host = _txtHost.Text.Trim(), Port = int.TryParse(_txtPort.Text, out var p) ? p : 443, UseSsl = _chkSsl.Checked, LoginId = _txtLoginId.Text.Trim(), Password = _txtPassword.Text }; } private async Task TestConnection() { _lblTestResult.Text = "Teste Verbindung..."; _lblTestResult.ForeColor = Color.Gray; _btnTest.Enabled = false; try { using (var client = new StarfaceApiClient(GetConnection())) { var ok = await client.LoginAsync(); if (ok) { _lblTestResult.Text = "Verbindung erfolgreich!"; _lblTestResult.ForeColor = Color.Green; await client.LogoutAsync(); } else { _lblTestResult.Text = "Login fehlgeschlagen. Zugangsdaten pruefen."; _lblTestResult.ForeColor = Color.Red; } } } catch (Exception ex) { _lblTestResult.Text = $"Fehler: {ex.Message}"; _lblTestResult.ForeColor = Color.Red; } _btnTest.Enabled = true; } private async Task LoadAddressBooks() { _lblTestResult.Text = "Lade Adressbuecher..."; _lblTestResult.ForeColor = Color.Gray; _btnLoadBooks.Enabled = false; try { using (var client = new StarfaceApiClient(GetConnection())) { var ok = await client.LoginAsync(); if (!ok) { _lblTestResult.Text = "Login fehlgeschlagen."; _lblTestResult.ForeColor = Color.Red; _btnLoadBooks.Enabled = true; return; } _addressBooks = await client.GetAddressBooksAsync(); await client.LogoutAsync(); _cmbAddressBook.Items.Clear(); foreach (var book in _addressBooks) _cmbAddressBook.Items.Add(book.Name); if (_addressBooks.Count > 0) { _cmbAddressBook.SelectedIndex = 0; _lblTestResult.Text = $"{_addressBooks.Count} Adressbuch/buecher gefunden."; _lblTestResult.ForeColor = Color.Green; } else { _lblTestResult.Text = "Keine Adressbuecher gefunden."; _lblTestResult.ForeColor = Color.Orange; } } } catch (Exception ex) { _lblTestResult.Text = $"Fehler: {ex.Message}"; _lblTestResult.ForeColor = Color.Red; } _btnLoadBooks.Enabled = true; } private void SaveProfile() { if (string.IsNullOrWhiteSpace(_txtName.Text)) { MessageBox.Show("Bitte Profilnamen eingeben.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (string.IsNullOrWhiteSpace(_txtHost.Text)) { MessageBox.Show("Bitte Starface-Host eingeben.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (_cmbAddressBook.SelectedIndex < 0 || _addressBooks.Count == 0) { MessageBox.Show("Bitte ein Starface-Adressbuch auswaehlen.\nZuerst 'Adressbuecher laden' klicken.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (string.IsNullOrEmpty(_selectedOutlookPath)) { MessageBox.Show("Bitte einen Outlook-Kontaktordner waehlen.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var profile = new SyncProfile { Id = _existingProfile?.Id ?? _pm.GenerateId(), Name = _txtName.Text.Trim(), StarfaceConnection = GetConnection(), StarfaceAddressBook = _addressBooks[_cmbAddressBook.SelectedIndex], OutlookFolderPath = _selectedOutlookPath, OutlookFolderName = _selectedOutlookName, SyncDirection = (SyncDirection)_cmbDirection.SelectedIndex, Enabled = _chkEnabled.Checked, AutoSyncIntervalMinutes = (int)_numAutoSync.Value, LastSync = _existingProfile?.LastSync ?? "" }; if (_isNew) _pm.AddProfile(profile); else _pm.UpdateProfile(profile); DialogResult = DialogResult.OK; Close(); } } }