2021-08-30 23:30:00 +00:00
using System ;
2021-08-14 23:18:23 +00:00
using System.Diagnostics ;
using System.Windows ;
2021-08-30 23:30:00 +00:00
using Hardcodet.Wpf.TaskbarNotification ;
2021-08-13 23:20:38 +00:00
namespace unison
{
public class SnapcastHandler
{
private readonly Process _snapcast = new ( ) ;
2021-09-01 00:35:37 +00:00
public bool HasStarted { get ; private set ; }
2021-08-30 23:30:00 +00:00
public void OnConnectionChanged ( object sender , EventArgs e )
{
2021-08-14 23:18:23 +00:00
if ( Properties . Settings . Default . snapcast_startup )
2021-08-30 23:30:00 +00:00
{
var mpd = ( MPDHandler ) Application . Current . Properties [ "mpd" ] ;
2021-09-01 00:35:37 +00:00
if ( mpd . IsConnected ( ) )
LaunchOrExit ( ) ;
2021-08-30 23:30:00 +00:00
}
2021-08-13 23:20:38 +00:00
}
2021-08-30 23:30:00 +00:00
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" ) ;
2021-08-30 23:30:00 +00:00
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-08-13 23:20:38 +00:00
{
2021-09-01 00:35:37 +00:00
if ( ! HasStarted & & ! ForceExit )
2021-08-13 23:20:38 +00:00
{
2021-08-30 23:30:00 +00:00
_snapcast . StartInfo . FileName = Properties . Settings . Default . snapcast_path + @"\snapclient.exe" ;
2021-08-14 23:18:23 +00:00
_snapcast . StartInfo . Arguments = $"--host {Properties.Settings.Default.mpd_host}" ;
2021-08-30 23:42:00 +00:00
_snapcast . StartInfo . CreateNoWindow = ! Properties . Settings . Default . snapcast_window ;
2021-08-14 23:18:23 +00:00
try
{
_snapcast . Start ( ) ;
}
catch ( Exception err )
{
2021-08-30 23:30:00 +00:00
MessageBox . Show ( $"[Snapcast error]\nInvalid path: {err.Message}\n\nCurrent path: {Properties.Settings.Default.snapcast_path}\nYou can reset it in the settings if needed." ,
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-08-13 23:20:38 +00:00
}
2021-09-01 00:35:37 +00:00
else if ( HasStarted )
2021-08-13 23:20:38 +00:00
{
_snapcast . Kill ( ) ;
2021-09-01 00:35:37 +00:00
HasStarted = false ;
2021-08-13 23:20:38 +00:00
}
2021-09-01 00:35:37 +00:00
if ( ! ForceExit )
2021-08-30 23:30:00 +00:00
UpdateInterface ( ) ;
2021-08-13 23:20:38 +00:00
}
}
}