2015-01-27 14:56:52 -08:00
using System ;
2015-04-14 11:36:44 -07:00
using System.Globalization ;
2015-01-27 14:56:52 -08:00
using System.Windows.Forms ;
2015-09-01 06:16:27 -07:00
using xServer.Core.Cryptography ;
2015-08-15 07:06:07 -07:00
using xServer.Core.Data ;
2015-06-05 14:07:37 -07:00
using xServer.Core.Networking ;
2015-07-25 07:58:36 -07:00
using xServer.Core.Networking.Utilities ;
2015-07-25 15:10:59 -07:00
using xServer.Core.Utilities ;
2015-01-27 14:56:52 -08:00
namespace xServer.Forms
{
public partial class FrmSettings : Form
{
2015-09-09 13:18:50 -07:00
private readonly QuasarServer _listenServer ;
2015-01-27 14:56:52 -08:00
2015-09-09 13:18:50 -07:00
public FrmSettings ( QuasarServer listenServer )
2015-01-27 14:56:52 -08:00
{
this . _listenServer = listenServer ;
2015-04-21 11:27:52 -07:00
2015-01-27 14:56:52 -08:00
InitializeComponent ( ) ;
if ( listenServer . Listening )
{
btnListen . Text = "Stop listening" ;
ncPort . Enabled = false ;
txtPassword . Enabled = false ;
}
2015-07-10 10:50:41 -07:00
ShowPassword ( false ) ;
2015-01-27 14:56:52 -08:00
}
private void FrmSettings_Load ( object sender , EventArgs e )
{
2015-08-15 11:13:52 -07:00
ncPort . Value = Settings . ListenPort ;
chkAutoListen . Checked = Settings . AutoListen ;
chkPopup . Checked = Settings . ShowPopup ;
txtPassword . Text = Settings . Password ;
chkUseUpnp . Checked = Settings . UseUPnP ;
chkShowTooltip . Checked = Settings . ShowToolTip ;
chkNoIPIntegration . Checked = Settings . EnableNoIPUpdater ;
txtNoIPHost . Text = Settings . NoIPHost ;
txtNoIPUser . Text = Settings . NoIPUsername ;
txtNoIPPass . Text = Settings . NoIPPassword ;
2015-01-27 14:56:52 -08:00
}
2015-07-25 07:58:36 -07:00
private ushort GetPortSafe ( )
{
var portValue = ncPort . Value . ToString ( CultureInfo . InvariantCulture ) ;
ushort port ;
return ( ! ushort . TryParse ( portValue , out port ) ) ? ( ushort ) 0 : port ;
}
2015-01-27 14:56:52 -08:00
private void btnListen_Click ( object sender , EventArgs e )
{
2015-07-25 07:58:36 -07:00
ushort port = GetPortSafe ( ) ;
2015-08-27 13:59:53 -07:00
string password = txtPassword . Text ;
2015-07-25 07:58:36 -07:00
if ( port = = 0 )
{
MessageBox . Show ( "Please enter a valid port > 0." , "Please enter a valid port" , MessageBoxButtons . OK ,
MessageBoxIcon . Warning ) ;
return ;
}
2015-08-27 13:59:53 -07:00
if ( password . Length < 3 )
{
MessageBox . Show ( "Please enter a secure password with more than 3 characters." ,
"Please enter a secure password" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
return ;
}
2015-08-27 00:20:16 -07:00
2015-01-27 14:56:52 -08:00
if ( btnListen . Text = = "Start listening" & & ! _listenServer . Listening )
{
2015-04-12 19:19:08 -07:00
try
{
2015-08-27 13:59:53 -07:00
AES . PreHashKey ( password ) ;
2015-07-25 07:58:36 -07:00
if ( chkUseUpnp . Checked )
{
if ( ! UPnP . IsDeviceFound )
{
MessageBox . Show ( "No available UPnP device found!" , "No UPnP device" , MessageBoxButtons . OK ,
MessageBoxIcon . Information ) ;
}
else
{
int outPort ;
UPnP . CreatePortMap ( port , out outPort ) ;
if ( port ! = outPort )
{
MessageBox . Show ( "Creating a port map with the UPnP device failed!\nPlease check if your device allows to create new port maps." , "Creating port map failed" , MessageBoxButtons . OK ,
MessageBoxIcon . Warning ) ;
}
}
}
2015-05-10 08:28:02 -07:00
if ( chkNoIPIntegration . Checked )
2015-05-10 09:11:22 -07:00
NoIpUpdater . Start ( ) ;
2015-07-25 07:58:36 -07:00
_listenServer . Listen ( port ) ;
2015-04-12 19:19:08 -07:00
}
finally
{
btnListen . Text = "Stop listening" ;
ncPort . Enabled = false ;
txtPassword . Enabled = false ;
}
2015-01-27 14:56:52 -08:00
}
else if ( btnListen . Text = = "Stop listening" & & _listenServer . Listening )
{
2015-04-12 19:19:08 -07:00
try
{
_listenServer . Disconnect ( ) ;
2015-07-25 07:58:36 -07:00
UPnP . DeletePortMap ( port ) ;
2015-04-12 19:19:08 -07:00
}
finally
{
btnListen . Text = "Start listening" ;
ncPort . Enabled = true ;
txtPassword . Enabled = true ;
}
2015-01-27 14:56:52 -08:00
}
}
private void btnSave_Click ( object sender , EventArgs e )
{
2015-07-25 07:58:36 -07:00
ushort port = GetPortSafe ( ) ;
2015-08-27 13:59:53 -07:00
string password = txtPassword . Text ;
2015-07-25 07:58:36 -07:00
if ( port = = 0 )
{
MessageBox . Show ( "Please enter a valid port > 0." , "Please enter a valid port" , MessageBoxButtons . OK ,
MessageBoxIcon . Warning ) ;
return ;
}
2015-08-27 13:59:53 -07:00
if ( password . Length < 3 )
{
MessageBox . Show ( "Please enter a secure password with more than 3 characters." ,
"Please enter a secure password" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
return ;
}
2015-08-15 11:13:52 -07:00
Settings . ListenPort = port ;
Settings . AutoListen = chkAutoListen . Checked ;
Settings . ShowPopup = chkPopup . Checked ;
2015-08-27 13:59:53 -07:00
if ( password ! = Settings . Password )
AES . PreHashKey ( password ) ;
Settings . Password = password ;
2015-08-15 11:13:52 -07:00
Settings . UseUPnP = chkUseUpnp . Checked ;
Settings . ShowToolTip = chkShowTooltip . Checked ;
Settings . EnableNoIPUpdater = chkNoIPIntegration . Checked ;
Settings . NoIPHost = txtNoIPHost . Text ;
Settings . NoIPUsername = txtNoIPUser . Text ;
Settings . NoIPPassword = txtNoIPPass . Text ;
2015-01-27 14:56:52 -08:00
this . Close ( ) ;
}
private void btnCancel_Click ( object sender , EventArgs e )
{
2015-04-21 11:27:52 -07:00
if ( MessageBox . Show ( "Discard your changes?" , "Cancel" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = =
DialogResult . Yes )
2015-01-27 14:56:52 -08:00
this . Close ( ) ;
}
2015-05-10 08:28:02 -07:00
private void chkNoIPIntegration_CheckedChanged ( object sender , EventArgs e )
{
NoIPControlHandler ( chkNoIPIntegration . Checked ) ;
}
private void NoIPControlHandler ( bool enable )
{
lblHost . Enabled = enable ;
lblUser . Enabled = enable ;
lblPass . Enabled = enable ;
txtNoIPHost . Enabled = enable ;
txtNoIPUser . Enabled = enable ;
txtNoIPPass . Enabled = enable ;
2015-07-10 10:50:41 -07:00
chkShowPassword . Enabled = enable ;
}
private void ShowPassword ( bool show = true )
{
txtNoIPPass . PasswordChar = ( show ) ? ( char ) 0 : ( char ) '●' ;
}
private void chkShowPassword_CheckedChanged ( object sender , EventArgs e )
{
ShowPassword ( chkShowPassword . Checked ) ;
2015-05-10 08:28:02 -07:00
}
2015-01-27 14:56:52 -08:00
}
2015-04-21 11:27:52 -07:00
}