unison/Handlers/SnapcastHandler.cs

68 lines
2.1 KiB
C#
Raw Normal View History

2021-08-20 16:26:32 +00:00
using Hardcodet.Wpf.TaskbarNotification;
using System;
2021-08-14 23:18:23 +00:00
using System.Diagnostics;
using System.Windows;
namespace unison
{
public class SnapcastHandler
{
private readonly Process _snapcast = new();
public bool Started { get; private set; }
2021-08-14 23:18:23 +00:00
private string _snapcastPath;
public SnapcastHandler()
{
2021-08-14 23:18:23 +00:00
// wip: this will have to be moved after the mpd connection, later on
_snapcastPath = Properties.Settings.Default.snapcast_path;
if (Properties.Settings.Default.snapcast_startup)
Start();
}
2021-08-20 16:26:32 +00:00
private void UpdateSystray()
{
TaskbarIcon Systray = (TaskbarIcon)Application.Current.Properties["systray"];
SystrayViewModel DataContext = Systray.DataContext as SystrayViewModel;
DataContext.OnPropertyChanged("SnapcastText");
}
2021-08-14 23:18:23 +00:00
public void Start()
{
if (!Started)
{
2021-08-14 23:18:23 +00:00
_snapcast.StartInfo.FileName = _snapcastPath + @"\snapclient.exe";
_snapcast.StartInfo.Arguments = $"--host {Properties.Settings.Default.mpd_host}";
_snapcast.StartInfo.CreateNoWindow = true;
2021-08-14 23:18:23 +00:00
try
{
_snapcast.Start();
}
catch (Exception err)
{
MessageBox.Show($"[Snapcast error]\nInvalid path: {err.Message}\n\nCurrent path: {_snapcastPath}\nYou can reset it in the settings if needed.",
"unison", MessageBoxButton.OK, MessageBoxImage.Error);
Trace.WriteLine(err.Message);
return;
}
Started = true;
2021-08-20 16:26:32 +00:00
UpdateSystray();
}
else
{
_snapcast.Kill();
Started = false;
2021-08-20 16:26:32 +00:00
UpdateSystray();
}
}
public void Stop()
{
if (Started)
{
_snapcast.Kill();
Started = false;
2021-08-20 16:26:32 +00:00
UpdateSystray();
}
}
}
}