first test
This commit is contained in:
+344
-12
@@ -1,20 +1,352 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace PainoBar_Helper
|
||||
{
|
||||
public partial class settings_form : Form
|
||||
public partial class SettingsForm : Form
|
||||
{
|
||||
public settings_form()
|
||||
private readonly ProfileManager _profileManager;
|
||||
private ProfileSettings _currentSettings;
|
||||
private string? _currentProfileName;
|
||||
|
||||
public SettingsForm(ProfileManager profileManager, ProfileSettings currentSettings)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_profileManager = profileManager;
|
||||
_currentSettings = new ProfileSettings
|
||||
{
|
||||
EnableTextFile = currentSettings.EnableTextFile,
|
||||
TextFilePath = currentSettings.TextFilePath,
|
||||
EnableWebServer = currentSettings.EnableWebServer,
|
||||
WebServerPort = currentSettings.WebServerPort,
|
||||
EnableToastNotifications = currentSettings.EnableToastNotifications,
|
||||
EnableSMTC = currentSettings.EnableSMTC,
|
||||
OverrideCredentials = currentSettings.OverrideCredentials,
|
||||
ConfigFilePath = currentSettings.ConfigFilePath,
|
||||
PianobarUser = currentSettings.PianobarUser,
|
||||
PianobarPassword = currentSettings.PianobarPassword
|
||||
};
|
||||
|
||||
_currentProfileName = profileManager.ActiveProfileName;
|
||||
InitializeControls();
|
||||
}
|
||||
|
||||
#region Initialization
|
||||
|
||||
private void InitializeControls()
|
||||
{
|
||||
LoadProfileList();
|
||||
LoadSettingsToControls();
|
||||
}
|
||||
|
||||
private void LoadProfileList()
|
||||
{
|
||||
// Uses profiles_combo_box_selector from the Designer
|
||||
if (profiles_combo_box_selector == null)
|
||||
return;
|
||||
|
||||
profiles_combo_box_selector.Items.Clear();
|
||||
foreach (var profile in _profileManager.Profiles)
|
||||
{
|
||||
profiles_combo_box_selector.Items.Add(profile.Name);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_currentProfileName))
|
||||
profiles_combo_box_selector.SelectedItem = _currentProfileName;
|
||||
}
|
||||
|
||||
private void LoadSettingsToControls()
|
||||
{
|
||||
// Output Settings (Tab 1)
|
||||
if (obs_text_output_check_box != null)
|
||||
obs_text_output_check_box.Checked = _currentSettings.EnableTextFile;
|
||||
|
||||
if (output_folder_text_box != null)
|
||||
output_folder_text_box.Text = _currentSettings.TextFilePath;
|
||||
|
||||
if (local_web_server_check_box != null)
|
||||
local_web_server_check_box.Checked = _currentSettings.EnableWebServer;
|
||||
|
||||
if (textBox1 != null)
|
||||
textBox1.Text = _currentSettings.WebServerPort.ToString();
|
||||
|
||||
if (chk_toast_notifications != null)
|
||||
chk_toast_notifications.Checked = _currentSettings.EnableToastNotifications;
|
||||
|
||||
if (chkSMTC != null)
|
||||
chkSMTC.Checked = _currentSettings.EnableSMTC;
|
||||
|
||||
// Pianobar Config (Tab 2)
|
||||
if (chkOverrideCredentials != null)
|
||||
{
|
||||
chkOverrideCredentials.Checked = _currentSettings.OverrideCredentials;
|
||||
ToggleCredentialFields(chkOverrideCredentials.Checked);
|
||||
}
|
||||
|
||||
if (txtConfigFilePath != null)
|
||||
txtConfigFilePath.Text = _currentSettings.ConfigFilePath;
|
||||
|
||||
if (txtPianobarUser != null)
|
||||
txtPianobarUser.Text = _currentSettings.PianobarUser;
|
||||
|
||||
if (txtPianobarPass != null)
|
||||
txtPianobarPass.Text = _currentSettings.PianobarPassword;
|
||||
}
|
||||
|
||||
private void SaveSettingsFromControls()
|
||||
{
|
||||
// Output Settings
|
||||
if (obs_text_output_check_box != null)
|
||||
_currentSettings.EnableTextFile = obs_text_output_check_box.Checked;
|
||||
|
||||
if (output_folder_text_box != null)
|
||||
_currentSettings.TextFilePath = output_folder_text_box.Text;
|
||||
|
||||
if (local_web_server_check_box != null)
|
||||
_currentSettings.EnableWebServer = local_web_server_check_box.Checked;
|
||||
|
||||
if (textBox1 != null && int.TryParse(textBox1.Text, out int port))
|
||||
_currentSettings.WebServerPort = port;
|
||||
|
||||
if (chk_toast_notifications != null)
|
||||
_currentSettings.EnableToastNotifications = chk_toast_notifications.Checked;
|
||||
|
||||
if (chkSMTC != null)
|
||||
_currentSettings.EnableSMTC = chkSMTC.Checked;
|
||||
|
||||
// Pianobar Config
|
||||
if (chkOverrideCredentials != null)
|
||||
_currentSettings.OverrideCredentials = chkOverrideCredentials.Checked;
|
||||
|
||||
if (txtConfigFilePath != null)
|
||||
_currentSettings.ConfigFilePath = txtConfigFilePath.Text;
|
||||
|
||||
if (txtPianobarUser != null)
|
||||
_currentSettings.PianobarUser = txtPianobarUser.Text;
|
||||
|
||||
if (txtPianobarPass != null)
|
||||
_currentSettings.PianobarPassword = txtPianobarPass.Text;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Profile Management
|
||||
|
||||
// Wire this to profiles_combo_box_selector.SelectedIndexChanged in the Designer
|
||||
public void profiles_combo_box_selector_SelectedIndexChanged(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is not ComboBox cmb || cmb.SelectedItem is not string profileName)
|
||||
return;
|
||||
|
||||
var profile = _profileManager.Profiles.FirstOrDefault(p => p.Name == profileName);
|
||||
if (profile != null)
|
||||
{
|
||||
_currentProfileName = profileName;
|
||||
_currentSettings = new ProfileSettings
|
||||
{
|
||||
EnableTextFile = profile.Settings.EnableTextFile,
|
||||
TextFilePath = profile.Settings.TextFilePath,
|
||||
EnableWebServer = profile.Settings.EnableWebServer,
|
||||
WebServerPort = profile.Settings.WebServerPort,
|
||||
EnableToastNotifications = profile.Settings.EnableToastNotifications,
|
||||
EnableSMTC = profile.Settings.EnableSMTC,
|
||||
OverrideCredentials = profile.Settings.OverrideCredentials,
|
||||
ConfigFilePath = profile.Settings.ConfigFilePath,
|
||||
PianobarUser = profile.Settings.PianobarUser,
|
||||
PianobarPassword = profile.Settings.PianobarPassword
|
||||
};
|
||||
LoadSettingsToControls();
|
||||
}
|
||||
}
|
||||
|
||||
// Wire this to new_profile_button.Click in the Designer
|
||||
public void new_profile_button_Click(object? sender, EventArgs e)
|
||||
{
|
||||
string? profileName = PromptForProfileName("New Profile", "Enter profile name:");
|
||||
if (string.IsNullOrWhiteSpace(profileName))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var newSettings = new ProfileSettings();
|
||||
_profileManager.CreateProfile(profileName, newSettings);
|
||||
|
||||
LoadProfileList();
|
||||
if (profiles_combo_box_selector != null)
|
||||
profiles_combo_box_selector.SelectedItem = profileName;
|
||||
|
||||
MessageBox.Show($"Profile '{profileName}' created successfully.", "Profile Created",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to create profile:\n{ex.Message}", "Error",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// Wire this to delete_profile_button.Click in the Designer
|
||||
public void delete_profile_button_Click(object? sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_currentProfileName))
|
||||
{
|
||||
MessageBox.Show("No profile selected.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show($"Are you sure you want to delete profile '{_currentProfileName}'?",
|
||||
"Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
||||
|
||||
if (result == DialogResult.Yes)
|
||||
{
|
||||
try
|
||||
{
|
||||
_profileManager.DeleteProfile(_currentProfileName);
|
||||
_currentProfileName = _profileManager.ActiveProfileName;
|
||||
LoadProfileList();
|
||||
|
||||
var activeProfile = _profileManager.GetActiveProfile();
|
||||
if (activeProfile != null)
|
||||
{
|
||||
_currentSettings = activeProfile.Settings;
|
||||
LoadSettingsToControls();
|
||||
}
|
||||
|
||||
MessageBox.Show("Profile deleted successfully.", "Profile Deleted",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to delete profile:\n{ex.Message}", "Error",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Browsing
|
||||
|
||||
// Wire this to output_folder_browse_button.Click in the Designer
|
||||
public void output_folder_browse_button_Click(object? sender, EventArgs e)
|
||||
{
|
||||
using var dialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*",
|
||||
Title = "Select Text File Output Path",
|
||||
FileName = "now_playing.txt"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (output_folder_text_box != null)
|
||||
output_folder_text_box.Text = dialog.FileName;
|
||||
}
|
||||
}
|
||||
|
||||
// Wire this to btnBrowseConfigFile.Click in the Designer
|
||||
public void btnBrowseConfigFile_Click(object? sender, EventArgs e)
|
||||
{
|
||||
using var dialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Config Files (*.config)|*.config|All Files (*.*)|*.*",
|
||||
Title = "Select Pianobar Config File Path",
|
||||
FileName = "pianobar.config"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (txtConfigFilePath != null)
|
||||
txtConfigFilePath.Text = dialog.FileName;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Credential Override
|
||||
|
||||
// Wire this to chkOverrideCredentials.CheckedChanged in the Designer
|
||||
public void chkOverrideCredentials_CheckedChanged(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is CheckBox chk)
|
||||
ToggleCredentialFields(chk.Checked);
|
||||
}
|
||||
|
||||
private void ToggleCredentialFields(bool enabled)
|
||||
{
|
||||
if (txtConfigFilePath != null)
|
||||
txtConfigFilePath.Enabled = enabled;
|
||||
|
||||
if (btnBrowseConfigFile != null)
|
||||
btnBrowseConfigFile.Enabled = enabled;
|
||||
|
||||
if (txtPianobarUser != null)
|
||||
txtPianobarUser.Enabled = enabled;
|
||||
|
||||
if (txtPianobarPass != null)
|
||||
txtPianobarPass.Enabled = enabled;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Save/Cancel
|
||||
|
||||
// Wire this to btnSave.Click in the Designer (or OK button)
|
||||
public void btnSave_Click(object? sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
SaveSettingsFromControls();
|
||||
|
||||
if (!string.IsNullOrEmpty(_currentProfileName))
|
||||
{
|
||||
_profileManager.UpdateProfile(_currentProfileName, _currentSettings);
|
||||
}
|
||||
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to save settings:\n{ex.Message}", "Error",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// Wire this to btnCancel.Click in the Designer (or Cancel button)
|
||||
public void btnCancel_Click(object? sender, EventArgs e)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Methods
|
||||
|
||||
private string? PromptForProfileName(string title, string prompt)
|
||||
{
|
||||
using var form = new Form
|
||||
{
|
||||
Text = title,
|
||||
Width = 400,
|
||||
Height = 150,
|
||||
StartPosition = FormStartPosition.CenterParent,
|
||||
FormBorderStyle = FormBorderStyle.FixedDialog,
|
||||
MaximizeBox = false,
|
||||
MinimizeBox = false
|
||||
};
|
||||
|
||||
var label = new Label { Text = prompt, Left = 10, Top = 20, Width = 360 };
|
||||
var textBox = new TextBox { Left = 10, Top = 50, Width = 360 };
|
||||
var btnOk = new Button { Text = "OK", Left = 210, Top = 80, DialogResult = DialogResult.OK };
|
||||
var btnCancel = new Button { Text = "Cancel", Left = 290, Top = 80, DialogResult = DialogResult.Cancel };
|
||||
|
||||
form.Controls.AddRange(new Control[] { label, textBox, btnOk, btnCancel });
|
||||
form.AcceptButton = btnOk;
|
||||
form.CancelButton = btnCancel;
|
||||
|
||||
return form.ShowDialog() == DialogResult.OK ? textBox.Text : null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user