unison/Handlers/SnapcastHandler.cs

83 lines
2.9 KiB
C#
Raw Normal View History

using System;
2021-08-14 23:18:23 +00:00
using System.Diagnostics;
using System.Windows;
using Hardcodet.Wpf.TaskbarNotification;
namespace unison
{
public class SnapcastHandler
{
private readonly Process _snapcast = new();
2021-09-01 00:35:37 +00:00
public bool HasStarted { get; private set; }
public void OnConnectionChanged(object sender, EventArgs e)
{
2021-08-14 23:18:23 +00:00
if (Properties.Settings.Default.snapcast_startup)
{
2022-04-07 23:39:59 +00:00
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
2021-09-01 00:35:37 +00:00
if (mpd.IsConnected())
LaunchOrExit();
}
}
private void HandleExit(object sender, EventArgs e)
{
_snapcast.Kill();
HasStarted = false;
Application.Current.Dispatcher.Invoke(() =>
{
UpdateInterface();
});
}
public void UpdateInterface()
2021-08-20 16:26:32 +00:00
{
TaskbarIcon Systray = (TaskbarIcon)Application.Current.Properties["systray"];
SystrayViewModel DataContext = Systray.DataContext as SystrayViewModel;
DataContext.OnPropertyChanged("SnapcastText");
Application.Current.Dispatcher.Invoke(() =>
{
MainWindow MainWin = (MainWindow)Application.Current.MainWindow;
MainWin.OnSnapcastChanged();
2021-09-01 00:35:37 +00:00
});
2021-08-20 16:26:32 +00:00
}
2021-09-01 00:35:37 +00:00
public void LaunchOrExit(bool ForceExit = false)
{
2021-09-01 00:35:37 +00:00
if (!HasStarted && !ForceExit)
{
2022-04-07 23:39:59 +00:00
MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"];
_snapcast.StartInfo.FileName = Properties.Settings.Default.snapcast_path + @"\snapclient.exe";
2022-04-07 23:39:59 +00:00
_snapcast.StartInfo.Arguments = $"--host {mpd._ipAddress}";
2021-08-30 23:42:00 +00:00
_snapcast.StartInfo.CreateNoWindow = !Properties.Settings.Default.snapcast_window;
_snapcast.EnableRaisingEvents = true;
_snapcast.Exited += new EventHandler(HandleExit);
2021-08-14 23:18:23 +00:00
try
{
_snapcast.Start();
}
catch (Exception err)
{
2021-09-02 22:21:15 +00:00
MessageBox.Show($"[{unison.Resources.Resources.Snapcast_Popup1}]\n" +
$"{unison.Resources.Resources.Snapcast_Popup2} {err.Message}\n\n" +
$"{unison.Resources.Resources.Snapcast_Popup3} {Properties.Settings.Default.snapcast_path}\n" +
$"{unison.Resources.Resources.Snapcast_Popup4}",
2021-08-14 23:18:23 +00:00
"unison", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
2021-09-01 00:35:37 +00:00
HasStarted = true;
}
2021-09-01 00:35:37 +00:00
else if (HasStarted)
{
_snapcast.Kill();
2021-09-01 00:35:37 +00:00
HasStarted = false;
}
2021-09-01 00:35:37 +00:00
if (!ForceExit)
UpdateInterface();
}
}
}