Quasar/Server/Forms/FrmSettings.cs

190 lines
6.6 KiB
C#
Raw Normal View History

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;
using xServer.Core.Data;
using xServer.Core.Networking;
2015-07-25 07:58:36 -07:00
using xServer.Core.Networking.Utilities;
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-01-27 14:56:52 -08:00
InitializeComponent();
if (listenServer.Listening)
{
btnListen.Text = "Stop listening";
ncPort.Enabled = false;
txtPassword.Enabled = false;
}
ShowPassword(false);
2015-01-27 14:56:52 -08:00
}
private void FrmSettings_Load(object sender, EventArgs e)
{
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();
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;
}
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-01-27 14:56:52 -08:00
if (btnListen.Text == "Start listening" && !_listenServer.Listening)
{
try
{
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);
}
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)
{
try
{
_listenServer.Disconnect();
2015-07-25 07:58:36 -07:00
UPnP.DeletePortMap(port);
}
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();
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;
}
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;
}
Settings.ListenPort = port;
Settings.AutoListen = chkAutoListen.Checked;
Settings.ShowPopup = chkPopup.Checked;
if (password != Settings.Password)
AES.PreHashKey(password);
Settings.Password = password;
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)
{
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;
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
}
}