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 System.Windows.Threading ;
using Hardcodet.Wpf.TaskbarNotification ;
2021-08-13 23:20:38 +00:00
namespace unison
{
public class SnapcastHandler
{
private readonly Process _snapcast = new ( ) ;
public bool Started { get ; private set ; }
public SnapcastHandler ( )
{
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" ] ;
if ( mpd . _connected )
Start ( ) ;
}
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 ( ) ;
} , DispatcherPriority . ContextIdle ) ;
2021-08-20 16:26:32 +00:00
}
2021-08-14 23:18:23 +00:00
public void Start ( )
2021-08-13 23:20:38 +00:00
{
if ( ! Started )
{
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 ) ;
Trace . WriteLine ( err . Message ) ;
return ;
}
2021-08-13 23:20:38 +00:00
Started = true ;
2021-08-30 23:30:00 +00:00
UpdateInterface ( ) ;
2021-08-13 23:20:38 +00:00
}
else
{
_snapcast . Kill ( ) ;
Started = false ;
2021-08-30 23:30:00 +00:00
UpdateInterface ( ) ;
2021-08-13 23:20:38 +00:00
}
}
public void Stop ( )
{
if ( Started )
{
_snapcast . Kill ( ) ;
Started = false ;
2021-08-30 23:30:00 +00:00
UpdateInterface ( ) ;
2021-08-13 23:20:38 +00:00
}
}
}
}