add config lock
Add a lock on config form to avoid accidental changes
This commit is contained in:
@@ -16,6 +16,8 @@ namespace Shadowsocks.Model
|
||||
public int index;
|
||||
public bool enabled;
|
||||
public bool isDefault;
|
||||
public bool lockStatus;
|
||||
public bool enableLog;
|
||||
|
||||
private static string CONFIG_FILE = "gui-config.json";
|
||||
|
||||
@@ -33,11 +35,11 @@ namespace Shadowsocks.Model
|
||||
|
||||
public static void CheckServer(Server server)
|
||||
{
|
||||
checkPort(server.local_port);
|
||||
checkPort(server.server_port);
|
||||
checkPassword(server.password);
|
||||
checkServer(server.server);
|
||||
checkRemark(server.remarks);
|
||||
CheckPort(server.local_port);
|
||||
CheckPort(server.server_port);
|
||||
CheckPassword(server.password);
|
||||
CheckServer(server.server);
|
||||
CheckRemark(server.remarks);
|
||||
}
|
||||
|
||||
public static Configuration Load()
|
||||
@@ -106,7 +108,7 @@ namespace Shadowsocks.Model
|
||||
};
|
||||
}
|
||||
|
||||
private static void assert(bool condition)
|
||||
private static void Assert(bool condition)
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
@@ -114,7 +116,7 @@ namespace Shadowsocks.Model
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkPort(int port)
|
||||
private static void CheckPort(int port)
|
||||
{
|
||||
if (port <= 0 || port > 65535)
|
||||
{
|
||||
@@ -122,7 +124,7 @@ namespace Shadowsocks.Model
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkPassword(string password)
|
||||
private static void CheckPassword(string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(password))
|
||||
{
|
||||
@@ -130,7 +132,7 @@ namespace Shadowsocks.Model
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkServer(string server)
|
||||
private static void CheckServer(string server)
|
||||
{
|
||||
if (string.IsNullOrEmpty(server))
|
||||
{
|
||||
@@ -138,7 +140,7 @@ namespace Shadowsocks.Model
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkRemark(string remark)
|
||||
private static void CheckRemark(string remark)
|
||||
{
|
||||
//remark is optional
|
||||
}
|
||||
|
||||
+33
-5
@@ -55,6 +55,7 @@
|
||||
this.ConfigItem = new System.Windows.Forms.MenuItem();
|
||||
this.menuItem4 = new System.Windows.Forms.MenuItem();
|
||||
this.editPACFileItem = new System.Windows.Forms.MenuItem();
|
||||
this.QRCodeItem = new System.Windows.Forms.MenuItem();
|
||||
this.aboutItem = new System.Windows.Forms.MenuItem();
|
||||
this.menuItem3 = new System.Windows.Forms.MenuItem();
|
||||
this.quitItem = new System.Windows.Forms.MenuItem();
|
||||
@@ -63,11 +64,13 @@
|
||||
this.AddButton = new System.Windows.Forms.Button();
|
||||
this.ServerGroupBox = new System.Windows.Forms.GroupBox();
|
||||
this.ServersListBox = new System.Windows.Forms.ListBox();
|
||||
this.QRCodeItem = new System.Windows.Forms.MenuItem();
|
||||
this.LockButton = new System.Windows.Forms.Button();
|
||||
this.panel4 = new System.Windows.Forms.Panel();
|
||||
this.tableLayoutPanel1.SuspendLayout();
|
||||
this.panel1.SuspendLayout();
|
||||
this.panel3.SuspendLayout();
|
||||
this.ServerGroupBox.SuspendLayout();
|
||||
this.panel4.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// tableLayoutPanel1
|
||||
@@ -329,6 +332,12 @@
|
||||
this.editPACFileItem.Text = "Edit &PAC File...";
|
||||
this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click);
|
||||
//
|
||||
// QRCodeItem
|
||||
//
|
||||
this.QRCodeItem.Index = 4;
|
||||
this.QRCodeItem.Text = "Show &QRCode...";
|
||||
this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click);
|
||||
//
|
||||
// aboutItem
|
||||
//
|
||||
this.aboutItem.Index = 5;
|
||||
@@ -396,11 +405,25 @@
|
||||
this.ServersListBox.TabIndex = 5;
|
||||
this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged);
|
||||
//
|
||||
// QRCodeItem
|
||||
// LockButton
|
||||
//
|
||||
this.QRCodeItem.Index = 4;
|
||||
this.QRCodeItem.Text = "Show &QRCode...";
|
||||
this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click);
|
||||
this.LockButton.Location = new System.Drawing.Point(4, 4);
|
||||
this.LockButton.Name = "LockButton";
|
||||
this.LockButton.Size = new System.Drawing.Size(89, 23);
|
||||
this.LockButton.TabIndex = 6;
|
||||
this.LockButton.Text = "&Lock";
|
||||
this.LockButton.UseVisualStyleBackColor = true;
|
||||
this.LockButton.Click += new System.EventHandler(this.LockButton_Click);
|
||||
//
|
||||
// panel4
|
||||
//
|
||||
this.panel4.AutoSize = true;
|
||||
this.panel4.Controls.Add(this.LockButton);
|
||||
this.panel4.Location = new System.Drawing.Point(12, 251);
|
||||
this.panel4.Margin = new System.Windows.Forms.Padding(0);
|
||||
this.panel4.Name = "panel4";
|
||||
this.panel4.Size = new System.Drawing.Size(192, 30);
|
||||
this.panel4.TabIndex = 7;
|
||||
//
|
||||
// ConfigForm
|
||||
//
|
||||
@@ -409,6 +432,7 @@
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
|
||||
this.AutoSize = true;
|
||||
this.ClientSize = new System.Drawing.Size(489, 286);
|
||||
this.Controls.Add(this.panel4);
|
||||
this.Controls.Add(this.ServersListBox);
|
||||
this.Controls.Add(this.ServerGroupBox);
|
||||
this.Controls.Add(this.panel1);
|
||||
@@ -420,6 +444,7 @@
|
||||
this.Name = "ConfigForm";
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
|
||||
this.Text = "Shadowsocks";
|
||||
this.TopMost = true;
|
||||
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed);
|
||||
this.Load += new System.EventHandler(this.ConfigForm_Load);
|
||||
this.Shown += new System.EventHandler(this.ConfigForm_Shown);
|
||||
@@ -429,6 +454,7 @@
|
||||
this.panel3.ResumeLayout(false);
|
||||
this.ServerGroupBox.ResumeLayout(false);
|
||||
this.ServerGroupBox.PerformLayout();
|
||||
this.panel4.ResumeLayout(false);
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
@@ -470,6 +496,8 @@
|
||||
private System.Windows.Forms.TextBox RemarksTextBox;
|
||||
private System.Windows.Forms.Label label6;
|
||||
private System.Windows.Forms.MenuItem QRCodeItem;
|
||||
private System.Windows.Forms.Button LockButton;
|
||||
private System.Windows.Forms.Panel panel4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Diagnostics;
|
||||
@@ -15,9 +16,10 @@ namespace Shadowsocks.View
|
||||
private ShadowsocksController controller;
|
||||
|
||||
// this is a copy of configuration that we are working on
|
||||
private Configuration modifiedConfiguration;
|
||||
private int oldSelectedIndex = -1;
|
||||
private bool isFirstRun;
|
||||
private Configuration _modifiedConfiguration;
|
||||
private int _oldSelectedIndex = -1;
|
||||
private bool _isFirstRun;
|
||||
private bool _lockStatus;
|
||||
|
||||
public ConfigForm(ShadowsocksController controller)
|
||||
{
|
||||
@@ -29,12 +31,12 @@ namespace Shadowsocks.View
|
||||
controller.ConfigChanged += controller_ConfigChanged;
|
||||
controller.PACFileReadyToOpen += controller_PACFileReadyToOpen;
|
||||
|
||||
loadCurrentConfiguration();
|
||||
LoadCurrentConfiguration();
|
||||
}
|
||||
|
||||
private void controller_ConfigChanged(object sender, EventArgs e)
|
||||
{
|
||||
loadCurrentConfiguration();
|
||||
LoadCurrentConfiguration();
|
||||
}
|
||||
|
||||
private void controller_EnableStatusChanged(object sender, EventArgs e)
|
||||
@@ -50,18 +52,31 @@ namespace Shadowsocks.View
|
||||
}
|
||||
|
||||
|
||||
private void showWindow()
|
||||
private void ShowWindow()
|
||||
{
|
||||
this.Opacity = 1;
|
||||
this.Show();
|
||||
IPTextBox.Focus();
|
||||
if (_lockStatus)
|
||||
{
|
||||
IPTextBox.Enabled = false;
|
||||
ServerPortTextBox.Enabled = false;
|
||||
PasswordTextBox.Enabled = false;
|
||||
EncryptionSelect.Enabled = false;
|
||||
ProxyPortTextBox.Enabled = false;
|
||||
RemarksTextBox.Enabled = false;
|
||||
AddButton.Enabled = false;
|
||||
DeleteButton.Enabled = false;
|
||||
_lockStatus = true;
|
||||
LockButton.Text = "&Unlock Settings";
|
||||
}
|
||||
}
|
||||
|
||||
private bool saveOldSelectedServer()
|
||||
private bool SaveOldSelectedServer()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count)
|
||||
if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -75,7 +90,7 @@ namespace Shadowsocks.View
|
||||
remarks = RemarksTextBox.Text
|
||||
};
|
||||
Configuration.CheckServer(server);
|
||||
modifiedConfiguration.configs[oldSelectedIndex] = server;
|
||||
_modifiedConfiguration.configs[_oldSelectedIndex] = server;
|
||||
return true;
|
||||
}
|
||||
catch (FormatException)
|
||||
@@ -89,17 +104,17 @@ namespace Shadowsocks.View
|
||||
return false;
|
||||
}
|
||||
|
||||
private void loadSelectedServer()
|
||||
private void LoadSelectedServer()
|
||||
{
|
||||
if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count)
|
||||
if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count)
|
||||
{
|
||||
Server server = modifiedConfiguration.configs[ServersListBox.SelectedIndex];
|
||||
Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex];
|
||||
|
||||
IPTextBox.Text = server.server;
|
||||
ServerPortTextBox.Text = server.server_port.ToString();
|
||||
PasswordTextBox.Text = server.password;
|
||||
ProxyPortTextBox.Text = server.local_port.ToString();
|
||||
EncryptionSelect.Text = server.method == null ? "aes-256-cfb" : server.method;
|
||||
EncryptionSelect.Text = server.method ?? "aes-256-cfb";
|
||||
RemarksTextBox.Text = server.remarks;
|
||||
ServerGroupBox.Visible = true;
|
||||
IPTextBox.Focus();
|
||||
@@ -110,28 +125,29 @@ namespace Shadowsocks.View
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfiguration(Configuration configuration)
|
||||
private void LoadConfiguration(Configuration configuration)
|
||||
{
|
||||
ServersListBox.Items.Clear();
|
||||
foreach (Server server in modifiedConfiguration.configs)
|
||||
foreach (Server server in _modifiedConfiguration.configs)
|
||||
{
|
||||
ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")");
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCurrentConfiguration()
|
||||
private void LoadCurrentConfiguration()
|
||||
{
|
||||
modifiedConfiguration = controller.GetConfiguration();
|
||||
loadConfiguration(modifiedConfiguration);
|
||||
oldSelectedIndex = modifiedConfiguration.index;
|
||||
ServersListBox.SelectedIndex = modifiedConfiguration.index;
|
||||
loadSelectedServer();
|
||||
_modifiedConfiguration = controller.GetConfiguration();
|
||||
LoadConfiguration(_modifiedConfiguration);
|
||||
_oldSelectedIndex = _modifiedConfiguration.index;
|
||||
_lockStatus = _modifiedConfiguration.lockStatus;
|
||||
ServersListBox.SelectedIndex = _modifiedConfiguration.index;
|
||||
LoadSelectedServer();
|
||||
|
||||
updateServersMenu();
|
||||
enableItem.Checked = modifiedConfiguration.enabled;
|
||||
UpdateServersMenu();
|
||||
enableItem.Checked = _modifiedConfiguration.enabled;
|
||||
}
|
||||
|
||||
private void updateServersMenu()
|
||||
private void UpdateServersMenu()
|
||||
{
|
||||
var items = ServersItem.MenuItems;
|
||||
|
||||
@@ -157,71 +173,68 @@ namespace Shadowsocks.View
|
||||
|
||||
private void ConfigForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
if (!controller.GetConfiguration().isDefault)
|
||||
if (controller == null || controller.GetConfiguration().isDefault)
|
||||
{
|
||||
this.Opacity = 0;
|
||||
BeginInvoke(new MethodInvoker(delegate
|
||||
{
|
||||
this.Hide();
|
||||
}));
|
||||
_isFirstRun = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isFirstRun = true;
|
||||
this.Opacity = 0;
|
||||
BeginInvoke(new MethodInvoker(delegate { this.Hide(); }));
|
||||
}
|
||||
}
|
||||
|
||||
private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (oldSelectedIndex == ServersListBox.SelectedIndex)
|
||||
if (_oldSelectedIndex == ServersListBox.SelectedIndex)
|
||||
{
|
||||
// we are moving back to oldSelectedIndex or doing a force move
|
||||
return;
|
||||
}
|
||||
if (!saveOldSelectedServer())
|
||||
if (!SaveOldSelectedServer())
|
||||
{
|
||||
// why this won't cause stack overflow?
|
||||
ServersListBox.SelectedIndex = oldSelectedIndex;
|
||||
ServersListBox.SelectedIndex = _oldSelectedIndex;
|
||||
return;
|
||||
}
|
||||
loadSelectedServer();
|
||||
oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
LoadSelectedServer();
|
||||
_oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
}
|
||||
|
||||
private void AddButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!saveOldSelectedServer())
|
||||
if (!SaveOldSelectedServer())
|
||||
{
|
||||
return;
|
||||
}
|
||||
Server server = Configuration.GetDefaultServer();
|
||||
modifiedConfiguration.configs.Add(server);
|
||||
loadConfiguration(modifiedConfiguration);
|
||||
ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1;
|
||||
oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
_modifiedConfiguration.configs.Add(server);
|
||||
LoadConfiguration(_modifiedConfiguration);
|
||||
ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1;
|
||||
_oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
}
|
||||
|
||||
private void DeleteButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count)
|
||||
_oldSelectedIndex = ServersListBox.SelectedIndex;
|
||||
if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count)
|
||||
{
|
||||
modifiedConfiguration.configs.RemoveAt(oldSelectedIndex);
|
||||
_modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex);
|
||||
}
|
||||
if (oldSelectedIndex >= modifiedConfiguration.configs.Count)
|
||||
if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count)
|
||||
{
|
||||
// can be -1
|
||||
oldSelectedIndex = modifiedConfiguration.configs.Count - 1;
|
||||
_oldSelectedIndex = _modifiedConfiguration.configs.Count - 1;
|
||||
}
|
||||
ServersListBox.SelectedIndex = oldSelectedIndex;
|
||||
loadConfiguration(modifiedConfiguration);
|
||||
ServersListBox.SelectedIndex = oldSelectedIndex;
|
||||
loadSelectedServer();
|
||||
ServersListBox.SelectedIndex = _oldSelectedIndex;
|
||||
LoadConfiguration(_modifiedConfiguration);
|
||||
ServersListBox.SelectedIndex = _oldSelectedIndex;
|
||||
LoadSelectedServer();
|
||||
}
|
||||
|
||||
private void Config_Click(object sender, EventArgs e)
|
||||
{
|
||||
showWindow();
|
||||
ShowWindow();
|
||||
}
|
||||
|
||||
private void Quit_Click(object sender, EventArgs e)
|
||||
@@ -229,38 +242,39 @@ namespace Shadowsocks.View
|
||||
this.Close();
|
||||
}
|
||||
|
||||
private void showFirstTimeBalloon()
|
||||
private void ShowFirstTimeBalloon()
|
||||
{
|
||||
if (isFirstRun)
|
||||
if (_isFirstRun)
|
||||
{
|
||||
notifyIcon1.BalloonTipTitle = "Shadowsocks is here";
|
||||
notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu";
|
||||
notifyIcon1.ShowBalloonTip(0);
|
||||
isFirstRun = false;
|
||||
_isFirstRun = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OKButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!saveOldSelectedServer())
|
||||
if (!SaveOldSelectedServer())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (modifiedConfiguration.configs.Count == 0)
|
||||
if (_modifiedConfiguration.configs.Count == 0)
|
||||
{
|
||||
MessageBox.Show("Please add at least one server");
|
||||
return;
|
||||
}
|
||||
controller.SaveConfig(modifiedConfiguration);
|
||||
_modifiedConfiguration.lockStatus = _lockStatus;
|
||||
controller.SaveConfig(_modifiedConfiguration);
|
||||
this.Hide();
|
||||
showFirstTimeBalloon();
|
||||
ShowFirstTimeBalloon();
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
this.Hide();
|
||||
loadCurrentConfiguration();
|
||||
showFirstTimeBalloon();
|
||||
LoadCurrentConfiguration();
|
||||
ShowFirstTimeBalloon();
|
||||
}
|
||||
|
||||
private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
|
||||
@@ -275,7 +289,7 @@ namespace Shadowsocks.View
|
||||
|
||||
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
|
||||
{
|
||||
showWindow();
|
||||
ShowWindow();
|
||||
}
|
||||
|
||||
|
||||
@@ -307,5 +321,31 @@ namespace Shadowsocks.View
|
||||
{
|
||||
new QRCodeForm(controller.GetQRCodeForCurrentServer()).Show();
|
||||
}
|
||||
|
||||
private void ChangeLockStatus(bool lockStatus)
|
||||
{
|
||||
IPTextBox.Enabled = lockStatus;
|
||||
ServerPortTextBox.Enabled = lockStatus;
|
||||
PasswordTextBox.Enabled = lockStatus;
|
||||
EncryptionSelect.Enabled = lockStatus;
|
||||
ProxyPortTextBox.Enabled = lockStatus;
|
||||
RemarksTextBox.Enabled = lockStatus;
|
||||
AddButton.Enabled = lockStatus;
|
||||
DeleteButton.Enabled = lockStatus;
|
||||
_lockStatus = !lockStatus;
|
||||
if (lockStatus)
|
||||
{
|
||||
LockButton.Text = "&Lock";
|
||||
}
|
||||
else
|
||||
{
|
||||
LockButton.Text = "&Unlock";
|
||||
}
|
||||
}
|
||||
|
||||
private void LockButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
ChangeLockStatus(_lockStatus);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user