unison/Views/Settings.xaml.cs

124 lines
4.5 KiB
C#
Raw Normal View History

using System.ComponentModel;
using System.Diagnostics;
2021-08-16 00:13:47 +00:00
using System.Globalization;
using System.IO;
using System.Reflection;
2021-08-14 15:14:37 +00:00
using System.Text.RegularExpressions;
using System.Windows;
2021-08-14 15:14:37 +00:00
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Navigation;
namespace unison
{
public partial class Settings : Window
{
2021-08-14 15:14:37 +00:00
public static string GetVersion => Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
public static string GetLicense
{
get
{
try
{
StreamReader Reader = new("LICENSE");
string file = "";
2021-08-14 15:14:37 +00:00
file += Reader.ReadToEnd();
return file;
}
catch (IOException e)
{
return e.Message;
}
}
}
public Settings()
{
InitHwnd();
InitializeComponent();
DataContext = this;
WindowState = WindowState.Minimized;
2021-08-14 15:14:37 +00:00
MpdHost.Text = Properties.Settings.Default.mpd_host;
MpdPort.Text = Properties.Settings.Default.mpd_port.ToString();
//MpdPassword.Text = Properties.Settings.Default.mpd_password;
2021-08-14 15:14:37 +00:00
SnapcastStartup.IsChecked = Properties.Settings.Default.snapcast_startup;
2021-08-30 23:42:00 +00:00
SnapcastWindow.IsChecked = Properties.Settings.Default.snapcast_window;
2021-08-14 15:14:37 +00:00
SnapcastPath.Text = Properties.Settings.Default.snapcast_path;
2021-08-14 23:18:23 +00:00
SnapcastPort.Text = Properties.Settings.Default.snapcast_port.ToString();
VolumeOffset.Text = Properties.Settings.Default.volume_offset.ToString();
2021-08-14 15:14:37 +00:00
}
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
ProcessStartInfo psi = new(e.Uri.AbsoluteUri);
psi.UseShellExecute = true;
Process.Start(psi);
e.Handled = true;
}
public void UpdateConnectionStatus()
{
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
2021-09-01 00:35:37 +00:00
if (mpd.IsConnected())
ConnectionStatus.Text = $"{unison.Resources.Resources.Settings_ConnectionStatusConnected} {mpd.GetVersion()}.";
else
ConnectionStatus.Text = unison.Resources.Resources.Settings_ConnectionStatusOffline;
}
2021-08-16 00:13:47 +00:00
private void MPDConnect_Clicked(object sender, RoutedEventArgs e)
{
SaveSettings();
ConnectionStatus.Text = unison.Resources.Resources.Settings_ConnectionStatusConnecting;
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
mpd.Connect();
2021-08-16 00:13:47 +00:00
}
2021-08-14 15:14:37 +00:00
private void SnapcastReset_Clicked(object sender, RoutedEventArgs e)
{
SnapcastPath.Text = (string)Application.Current.FindResource("snapcastPath");
SnapcastPort.Text = (string)Application.Current.FindResource("snapcastPort");
2021-08-14 15:14:37 +00:00
}
public void SaveSettings()
{
2021-08-14 15:14:37 +00:00
Properties.Settings.Default.mpd_host = MpdHost.Text;
2021-08-16 00:13:47 +00:00
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text, CultureInfo.InvariantCulture);
//Properties.Settings.Default.mpd_password = MpdPassword.Text;
2021-08-14 15:14:37 +00:00
Properties.Settings.Default.snapcast_startup = (bool)SnapcastStartup.IsChecked;
2021-08-30 23:42:00 +00:00
Properties.Settings.Default.snapcast_window = (bool)SnapcastWindow.IsChecked;
2021-08-14 15:14:37 +00:00
Properties.Settings.Default.snapcast_path = SnapcastPath.Text;
2021-08-16 00:13:47 +00:00
Properties.Settings.Default.snapcast_port = int.Parse(SnapcastPort.Text, CultureInfo.InvariantCulture);
Properties.Settings.Default.volume_offset = int.Parse(VolumeOffset.Text, CultureInfo.InvariantCulture);
2021-08-14 15:14:37 +00:00
Properties.Settings.Default.Save();
}
2021-08-14 15:14:37 +00:00
private void ConnectHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
MPDConnect_Clicked(null, null);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
SaveSettings();
WindowState = WindowState.Minimized;
Hide();
}
public void InitHwnd()
{
WindowInteropHelper helper = new(this);
helper.EnsureHandle();
}
}
}