diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 77cf857f..9b61f512 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -619,7 +619,6 @@ this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Edit Servers"; 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); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ConfigForm_KeyDown); this.tableLayoutPanel1.ResumeLayout(false); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 08895455..c00560b1 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -71,14 +71,7 @@ namespace Shadowsocks.View LoadCurrentConfiguration(); } - private void ShowWindow() - { - this.Opacity = 1; - this.Show(); - IPTextBox.Focus(); - } - - private bool SaveOldSelectedServer() + private bool ValidateAndSaveSelectedServerDetails() { try { @@ -86,38 +79,10 @@ namespace Shadowsocks.View { return true; } - Server server = new Server(); + Server server = GetServerDetailsFromUI(); - if (Uri.CheckHostName(server.server = IPTextBox.Text.Trim()) == UriHostNameType.Unknown) - { - MessageBox.Show(I18N.GetString("Invalid server address")); - IPTextBox.Focus(); - return false; - } - if (!int.TryParse(ServerPortTextBox.Text, out server.server_port)) - { - MessageBox.Show(I18N.GetString("Illegal port number format")); - ServerPortTextBox.Focus(); - return false; - } - server.password = PasswordTextBox.Text; - server.method = EncryptionSelect.Text; - server.plugin = PluginTextBox.Text; - server.plugin_opts = PluginOptionsTextBox.Text; - server.plugin_args = PluginArgumentsTextBox.Text; - server.remarks = RemarksTextBox.Text; - if (!int.TryParse(TimeoutTextBox.Text, out server.timeout)) - { - MessageBox.Show(I18N.GetString("Illegal timeout format")); - TimeoutTextBox.Focus(); - return false; - } - int localPort = int.Parse(ProxyPortTextBox.Text); Configuration.CheckServer(server); - Configuration.CheckLocalPort(localPort); _modifiedConfiguration.configs[_lastSelectedIndex] = server; - _modifiedConfiguration.localPort = localPort; - _modifiedConfiguration.portableMode = PortableModeCheckBox.Checked; return true; } @@ -128,29 +93,62 @@ namespace Shadowsocks.View return false; } - private void LoadSelectedServer() + private Server GetServerDetailsFromUI() + { + Server server = new Server(); + if (Uri.CheckHostName(server.server = IPTextBox.Text.Trim()) == UriHostNameType.Unknown) + { + MessageBox.Show(I18N.GetString("Invalid server address")); + IPTextBox.Focus(); + return null; + } + if (!int.TryParse(ServerPortTextBox.Text, out server.server_port)) + { + MessageBox.Show(I18N.GetString("Illegal port number format")); + ServerPortTextBox.Focus(); + return null; + } + server.password = PasswordTextBox.Text; + server.method = EncryptionSelect.Text; + server.plugin = PluginTextBox.Text; + server.plugin_opts = PluginOptionsTextBox.Text; + server.plugin_args = PluginArgumentsTextBox.Text; + server.remarks = RemarksTextBox.Text; + if (!int.TryParse(TimeoutTextBox.Text, out server.timeout)) + { + MessageBox.Show(I18N.GetString("Illegal timeout format")); + TimeoutTextBox.Focus(); + return null; + } + return server; + } + + private void LoadSelectedServerDetails() { if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count) { Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex]; - - IPTextBox.Text = server.server; - ServerPortTextBox.Text = server.server_port.ToString(); - PasswordTextBox.Text = server.password; - ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString(); - EncryptionSelect.Text = server.method ?? "aes-256-cfb"; - PluginTextBox.Text = server.plugin; - PluginOptionsTextBox.Text = server.plugin_opts; - PluginArgumentsTextBox.Text = server.plugin_args; - RemarksTextBox.Text = server.remarks; - TimeoutTextBox.Text = server.timeout.ToString(); + SetServerDetailsToUI(server); } } - private void LoadConfiguration(Configuration configuration) + private void SetServerDetailsToUI(Server server) + { + IPTextBox.Text = server.server; + ServerPortTextBox.Text = server.server_port.ToString(); + PasswordTextBox.Text = server.password; + EncryptionSelect.Text = server.method ?? "aes-256-cfb"; + PluginTextBox.Text = server.plugin; + PluginOptionsTextBox.Text = server.plugin_opts; + PluginArgumentsTextBox.Text = server.plugin_args; + RemarksTextBox.Text = server.remarks; + TimeoutTextBox.Text = server.timeout.ToString(); + } + + private void LoadServerNameListToUI(Configuration configuration) { ServersListBox.Items.Clear(); - foreach (Server server in _modifiedConfiguration.configs) + foreach (Server server in configuration.configs) { ServersListBox.Items.Add(server.FriendlyName()); } @@ -159,23 +157,19 @@ namespace Shadowsocks.View private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfigurationCopy(); - LoadConfiguration(_modifiedConfiguration); + LoadServerNameListToUI(_modifiedConfiguration); _lastSelectedIndex = _modifiedConfiguration.index; if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count) { _lastSelectedIndex = 0; } ServersListBox.SelectedIndex = _lastSelectedIndex; - UpdateMoveUpAndDownButton(); - LoadSelectedServer(); + UpdateButtons(); + LoadSelectedServerDetails(); + ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString(); PortableModeCheckBox.Checked = _modifiedConfiguration.portableMode; } - private void ConfigForm_Load(object sender, EventArgs e) - { - - } - private void ConfigForm_KeyDown(object sender, KeyEventArgs e) { // Sometimes the users may hit enter key by mistake, and the form will close without saving entries. @@ -183,7 +177,7 @@ namespace Shadowsocks.View if (e.KeyCode == Keys.Enter) { Server server = controller.GetCurrentServer(); - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; } @@ -192,6 +186,12 @@ namespace Shadowsocks.View MessageBox.Show(I18N.GetString("Please add at least one server")); return; } + int localPort = int.Parse(ProxyPortTextBox.Text); + Configuration.CheckLocalPort(localPort); + _modifiedConfiguration.localPort = localPort; + + _modifiedConfiguration.portableMode = PortableModeCheckBox.Checked; + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode); controller.SelectServerIndex(_modifiedConfiguration.configs.IndexOf(server)); } @@ -209,7 +209,7 @@ namespace Shadowsocks.View // we are moving back to oldSelectedIndex or doing a force move return; } - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { // why this won't cause stack overflow? ServersListBox.SelectedIndex = _lastSelectedIndex; @@ -219,34 +219,34 @@ namespace Shadowsocks.View { ServersListBox.Items[_lastSelectedIndex] = _modifiedConfiguration.configs[_lastSelectedIndex].FriendlyName(); } - UpdateMoveUpAndDownButton(); - LoadSelectedServer(); + UpdateButtons(); + LoadSelectedServerDetails(); _lastSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; } Server server = Configuration.GetDefaultServer(); _modifiedConfiguration.configs.Add(server); - LoadConfiguration(_modifiedConfiguration); + LoadServerNameListToUI(_modifiedConfiguration); ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; _lastSelectedIndex = ServersListBox.SelectedIndex; } private void DuplicateButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; } Server currServer = _modifiedConfiguration.configs[_lastSelectedIndex]; var currIndex = _modifiedConfiguration.configs.IndexOf(currServer); _modifiedConfiguration.configs.Insert(currIndex + 1, currServer); - LoadConfiguration(_modifiedConfiguration); + LoadServerNameListToUI(_modifiedConfiguration); ServersListBox.SelectedIndex = currIndex + 1; _lastSelectedIndex = ServersListBox.SelectedIndex; } @@ -264,14 +264,14 @@ namespace Shadowsocks.View _lastSelectedIndex = _modifiedConfiguration.configs.Count - 1; } ServersListBox.SelectedIndex = _lastSelectedIndex; - LoadConfiguration(_modifiedConfiguration); + LoadServerNameListToUI(_modifiedConfiguration); ServersListBox.SelectedIndex = _lastSelectedIndex; - LoadSelectedServer(); + LoadSelectedServerDetails(); } private void OKButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; } @@ -280,6 +280,13 @@ namespace Shadowsocks.View MessageBox.Show(I18N.GetString("Please add at least one server")); return; } + + int localPort = int.Parse(ProxyPortTextBox.Text); + Configuration.CheckLocalPort(localPort); + _modifiedConfiguration.localPort = localPort; + + _modifiedConfiguration.portableMode = PortableModeCheckBox.Checked; + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode); // SelectedIndex remains valid // We handled this in event handlers, e.g. Add/DeleteButton, SelectedIndexChanged @@ -322,32 +329,19 @@ namespace Shadowsocks.View ServersListBox.SelectedIndex = index + step; ServersListBox.EndUpdate(); - UpdateMoveUpAndDownButton(); + UpdateButtons(); } - private void UpdateMoveUpAndDownButton() + private void UpdateButtons() { - if (ServersListBox.SelectedIndex == 0) - { - MoveUpButton.Enabled = false; - } - else - { - MoveUpButton.Enabled = true; - } - if (ServersListBox.SelectedIndex == ServersListBox.Items.Count - 1) - { - MoveDownButton.Enabled = false; - } - else - { - MoveDownButton.Enabled = true; - } + DeleteButton.Enabled = (ServersListBox.Items.Count > 0); + MoveUpButton.Enabled = (ServersListBox.SelectedIndex > 0); + MoveDownButton.Enabled = (ServersListBox.SelectedIndex < ServersListBox.Items.Count - 1); } private void MoveUpButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; } @@ -359,7 +353,7 @@ namespace Shadowsocks.View private void MoveDownButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!ValidateAndSaveSelectedServerDetails()) { return; }