Files
RadioDJViewer/RadioDJViewer/radiodj-restapi-template.cs
2025-09-08 16:38:29 -04:00

224 lines
8.6 KiB
C#

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;
using System.IO;
namespace RadioDJViewer
{
public partial class Form1 : Form
{
private string outputFolderPath = string.Empty;
private string mainImagesFolderPath = string.Empty;
public Form1()
{
InitializeComponent();
textBox4.UseSystemPasswordChar = true; // Obscure password field
comboBoxScrollSpeed.Items.Clear();
comboBoxScrollSpeed.Items.AddRange(new object[] {
"Very Slow",
"Slow",
"Medium",
"Fast",
"Very Fast"
});
comboBoxScrollSpeed.SelectedItem = "Medium";
this.buttonSelectOutputFolder.Click += ButtonSelectOutputFolder_Click;
this.button6.Click += ButtonSelectMainImagesFolder_Click;
this.button3.Click += button3_Click;
this.button1.Click += button1_Click;
this.button4.Click += button4_Click;
this.button5.Click += button5_Click;
this.button2.Click += button2_Click;
// Populate profile list on open
LoadProfileList();
// Auto-load first profile's data into fields if available
LoadSelectedProfileFields();
this.comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
}
private void LoadProfileList()
{
comboBox1.Items.Clear();
var names = ProfileStorage.GetProfileNames();
comboBox1.Items.AddRange(names.ToArray());
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = 0;
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
LoadSelectedProfileFields();
}
private void LoadSelectedProfileFields()
{
if (comboBox1.SelectedItem != null)
{
var profile = ProfileStorage.LoadProfile(comboBox1.SelectedItem.ToString());
if (profile != null)
{
textBox6.Text = profile.Name;
textBox2.Text = profile.IP;
textBox3.Text = profile.Port;
textBox4.Text = profile.Password;
outputFolderPath = profile.OutputFolder;
mainImagesFolderPath = profile.MainImagesFolder;
labelOutputFolderPath.Text = outputFolderPath;
label6.Text = mainImagesFolderPath;
textBox5.Text = profile.OutputImageName;
textBox1.Text = profile.UrlFormat;
// Default polling rate to 3 if missing or zero
textBox7.Text = (profile.PollingRateSeconds > 0 ? profile.PollingRateSeconds : 3).ToString();
comboBoxScrollSpeed.SelectedItem = profile.MarqueeScrollSpeed ?? "Medium";
textBoxPauseTime.Text = profile.MarqueePauseTime > 0 ? profile.MarqueePauseTime.ToString() : "6";
separatingcharacterformatbox.Text = profile.MarqueeSeparator ?? " | ";
}
}
}
private void ButtonSelectOutputFolder_Click(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
outputFolderPath = dialog.SelectedPath;
labelOutputFolderPath.Text = outputFolderPath;
}
}
}
private void ButtonSelectMainImagesFolder_Click(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
mainImagesFolderPath = dialog.SelectedPath;
label6.Text = mainImagesFolderPath;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
// Save Profile (Save Profile button)
if (string.IsNullOrWhiteSpace(textBox6.Text))
{
MessageBox.Show("Profile name is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
int pollingRate = 3;
int.TryParse(textBox7.Text, out pollingRate);
var profile = new Profile
{
Name = textBox6.Text,
IP = textBox2.Text,
Port = textBox3.Text,
Password = textBox4.Text,
OutputFolder = outputFolderPath,
MainImagesFolder = mainImagesFolderPath,
OutputImageName = textBox5.Text,
UrlFormat = textBox1.Text,
PollingRateSeconds = pollingRate,
MarqueeScrollSpeed = comboBoxScrollSpeed.SelectedItem?.ToString() ?? "Medium",
MarqueePauseTime = int.TryParse(textBoxPauseTime.Text, out int pt) ? pt : 6,
MarqueeSeparator = separatingcharacterformatbox.Text
};
ProfileStorage.SaveProfile(profile);
// Refresh profile list
comboBox1.Items.Clear();
var names = ProfileStorage.GetProfileNames();
comboBox1.Items.AddRange(names.ToArray());
MessageBox.Show("Profile saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button4_Click(object sender, EventArgs e)
{
// Remove Profile (Remove Profile button)
if (comboBox1.SelectedItem != null)
{
ProfileStorage.DeleteProfile(comboBox1.SelectedItem.ToString());
comboBox1.Items.Clear();
var names = ProfileStorage.GetProfileNames();
comboBox1.Items.AddRange(names.ToArray());
}
}
private void button5_Click(object sender, EventArgs e)
{
// Load Profile (Load Profile button)
if (comboBox1.SelectedItem != null)
{
var profile = ProfileStorage.LoadProfile(comboBox1.SelectedItem.ToString());
if (profile != null)
{
textBox6.Text = profile.Name;
textBox2.Text = profile.IP;
textBox3.Text = profile.Port;
textBox4.Text = profile.Password;
outputFolderPath = profile.OutputFolder;
mainImagesFolderPath = profile.MainImagesFolder;
labelOutputFolderPath.Text = outputFolderPath;
label6.Text = mainImagesFolderPath;
textBox5.Text = profile.OutputImageName;
textBox1.Text = profile.UrlFormat;
Main mainForm = Application.OpenForms["Main"] as Main;
if (mainForm != null)
{
mainForm.LoadProfile(profile);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
// Save (bottom Save button) - save all fields to the currently selected or new profile
button3_Click(sender, e); // Reuse Save Profile logic
if (!string.IsNullOrWhiteSpace(textBox6.Text))
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
// Cancel (bottom Cancel button) - close the settings window without saving
this.Close();
}
private void label8_Click(object sender, EventArgs e)
{
}
public void ApplyTheme(bool darkMode)
{
Color backColor = darkMode ? Color.FromArgb(32, 32, 32) : SystemColors.Control;
Color foreColor = darkMode ? Color.White : SystemColors.ControlText;
this.BackColor = backColor;
this.ForeColor = foreColor;
foreach (Control c in this.Controls)
{
ApplyThemeRecursive(c, backColor, foreColor);
}
}
private void ApplyThemeRecursive(Control control, Color backColor, Color foreColor)
{
control.BackColor = backColor;
control.ForeColor = foreColor;
foreach (Control child in control.Controls)
{
ApplyThemeRecursive(child, backColor, foreColor);
}
}
}
}