diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 0e47f82..3ba8eb7 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -123,7 +123,7 @@ namespace Shadowsocks.Model { if (string.IsNullOrEmpty(server)) { - throw new ArgumentException("server can not be blank"); + throw new ArgumentException("server IP can not be blank"); } } diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 65bfe1e..2b36183 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -31,16 +31,16 @@ this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ConfigForm)); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); - this.label5 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); - this.EncryptionSelect = new System.Windows.Forms.ComboBox(); - this.label3 = new System.Windows.Forms.Label(); + this.ProxyPortTextBox = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); this.IPTextBox = new System.Windows.Forms.TextBox(); this.ServerPortTextBox = new System.Windows.Forms.TextBox(); this.PasswordTextBox = new System.Windows.Forms.TextBox(); - this.ProxyPortTextBox = new System.Windows.Forms.TextBox(); + this.label5 = new System.Windows.Forms.Label(); + this.EncryptionSelect = new System.Windows.Forms.ComboBox(); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.panel2 = new System.Windows.Forms.Panel(); this.OKButton = new System.Windows.Forms.Button(); @@ -54,8 +54,8 @@ this.menuItem3 = new System.Windows.Forms.MenuItem(); this.quitItem = new System.Windows.Forms.MenuItem(); this.panel3 = new System.Windows.Forms.Panel(); - this.AddButton = new System.Windows.Forms.Button(); this.DeleteButton = new System.Windows.Forms.Button(); + this.AddButton = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); this.tableLayoutPanel1.SuspendLayout(); @@ -94,16 +94,6 @@ this.tableLayoutPanel1.Size = new System.Drawing.Size(242, 141); this.tableLayoutPanel1.TabIndex = 0; // - // label5 - // - this.label5.Anchor = System.Windows.Forms.AnchorStyles.Right; - this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(11, 90); - this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(57, 13); - this.label5.TabIndex = 8; - this.label5.Text = "Encryption"; - // // label1 // this.label1.Anchor = System.Windows.Forms.AnchorStyles.Right; @@ -124,36 +114,14 @@ this.label2.TabIndex = 1; this.label2.Text = "Server Port"; // - // EncryptionSelect + // ProxyPortTextBox // - this.EncryptionSelect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.EncryptionSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.EncryptionSelect.FormattingEnabled = true; - this.EncryptionSelect.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.EncryptionSelect.ItemHeight = 13; - this.EncryptionSelect.Items.AddRange(new object[] { - "table", - "rc4-md5", - "aes-256-cfb", - "aes-192-cfb", - "aes-128-cfb", - "bf-cfb", - "rc4"}); - this.EncryptionSelect.Location = new System.Drawing.Point(74, 86); - this.EncryptionSelect.Name = "EncryptionSelect"; - this.EncryptionSelect.Size = new System.Drawing.Size(160, 21); - this.EncryptionSelect.TabIndex = 8; - // - // label3 - // - this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(15, 63); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(53, 13); - this.label3.TabIndex = 2; - this.label3.Text = "Password"; + this.ProxyPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + this.ProxyPortTextBox.Location = new System.Drawing.Point(74, 113); + this.ProxyPortTextBox.Name = "ProxyPortTextBox"; + this.ProxyPortTextBox.Size = new System.Drawing.Size(160, 20); + this.ProxyPortTextBox.TabIndex = 9; + this.ProxyPortTextBox.WordWrap = false; // // label4 // @@ -165,6 +133,16 @@ this.label4.TabIndex = 3; this.label4.Text = "Proxy Port"; // + // label3 + // + this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(15, 63); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(53, 13); + this.label3.TabIndex = 2; + this.label3.Text = "Password"; + // // IPTextBox // this.IPTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); @@ -192,14 +170,36 @@ this.PasswordTextBox.TabIndex = 7; this.PasswordTextBox.WordWrap = false; // - // ProxyPortTextBox + // label5 // - this.ProxyPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.ProxyPortTextBox.Location = new System.Drawing.Point(74, 113); - this.ProxyPortTextBox.Name = "ProxyPortTextBox"; - this.ProxyPortTextBox.Size = new System.Drawing.Size(160, 20); - this.ProxyPortTextBox.TabIndex = 9; - this.ProxyPortTextBox.WordWrap = false; + this.label5.Anchor = System.Windows.Forms.AnchorStyles.Right; + this.label5.AutoSize = true; + this.label5.Location = new System.Drawing.Point(11, 90); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(57, 13); + this.label5.TabIndex = 8; + this.label5.Text = "Encryption"; + // + // EncryptionSelect + // + this.EncryptionSelect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.EncryptionSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.EncryptionSelect.FormattingEnabled = true; + this.EncryptionSelect.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.EncryptionSelect.ItemHeight = 13; + this.EncryptionSelect.Items.AddRange(new object[] { + "table", + "rc4-md5", + "aes-256-cfb", + "aes-192-cfb", + "aes-128-cfb", + "bf-cfb", + "rc4"}); + this.EncryptionSelect.Location = new System.Drawing.Point(74, 86); + this.EncryptionSelect.Name = "EncryptionSelect"; + this.EncryptionSelect.Size = new System.Drawing.Size(160, 21); + this.EncryptionSelect.TabIndex = 8; // // notifyIcon1 // @@ -308,15 +308,6 @@ this.panel3.Size = new System.Drawing.Size(164, 30); this.panel3.TabIndex = 5; // - // AddButton - // - this.AddButton.Location = new System.Drawing.Point(4, 4); - this.AddButton.Name = "AddButton"; - this.AddButton.Size = new System.Drawing.Size(75, 23); - this.AddButton.TabIndex = 3; - this.AddButton.Text = "&Add"; - this.AddButton.UseVisualStyleBackColor = true; - // // DeleteButton // this.DeleteButton.Location = new System.Drawing.Point(86, 4); @@ -326,6 +317,15 @@ this.DeleteButton.Text = "&Delete"; this.DeleteButton.UseVisualStyleBackColor = true; // + // AddButton + // + this.AddButton.Location = new System.Drawing.Point(4, 4); + this.AddButton.Name = "AddButton"; + this.AddButton.Size = new System.Drawing.Size(75, 23); + this.AddButton.TabIndex = 3; + this.AddButton.Text = "&Add"; + this.AddButton.UseVisualStyleBackColor = true; + // // groupBox1 // this.groupBox1.Controls.Add(this.tableLayoutPanel1); @@ -346,6 +346,7 @@ this.ServersListBox.Name = "ServersListBox"; this.ServersListBox.Size = new System.Drawing.Size(164, 186); this.ServersListBox.TabIndex = 2; + this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // // ConfigForm // @@ -366,7 +367,7 @@ this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Shadowsocks"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed); - this.Load += new System.EventHandler(this.CinfigForm_Load); + this.Load += new System.EventHandler(this.ConfigForm_Load); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.panel1.ResumeLayout(false); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 014b128..8827aa4 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -12,7 +12,11 @@ namespace Shadowsocks.View { public partial class ConfigForm : Form { - ShadowsocksController controller; + private ShadowsocksController controller; + + // this is a copy of configuration that we are working on + private Configuration modifiedConfiguration; + private int oldSelectedIndex = -1; public ConfigForm(ShadowsocksController controller) { @@ -24,17 +28,17 @@ namespace Shadowsocks.View controller.ConfigChanged += controller_ConfigChanged; controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; - updateUI(); + loadCurrentConfiguration(); } private void controller_ConfigChanged(object sender, EventArgs e) { - updateUI(); + loadCurrentConfiguration(); } private void controller_EnableStatusChanged(object sender, EventArgs e) { - updateUI(); + enableItem.Checked = controller.GetConfiguration().enabled; } void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e) @@ -51,20 +55,64 @@ namespace Shadowsocks.View this.Show(); } - private void updateUI() + private bool saveOldSelectedServer() { - Server server = controller.GetCurrentServer(); + try + { + if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) + { + return true; + } + Server server = new Server + { + server = IPTextBox.Text, + server_port = int.Parse(ServerPortTextBox.Text), + password = PasswordTextBox.Text, + local_port = int.Parse(ProxyPortTextBox.Text), + method = EncryptionSelect.Text + }; + Configuration.CheckServer(server); + modifiedConfiguration.configs[oldSelectedIndex] = server; + return true; + } + catch (FormatException) + { + MessageBox.Show("illegal port number format"); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + return false; + } + + private void loadSelectedServer() + { + 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; - - enableItem.Checked = controller.GetConfiguration().enabled; } - private void CinfigForm_Load(object sender, EventArgs e) + private void loadCurrentConfiguration() + { + modifiedConfiguration = controller.GetConfiguration(); + + ServersListBox.Items.Clear(); + foreach (Server server in modifiedConfiguration.configs) + { + ServersListBox.Items.Add(server.server); + } + ServersListBox.SelectedIndex = modifiedConfiguration.index; + oldSelectedIndex = ServersListBox.SelectedIndex; + + enableItem.Checked = modifiedConfiguration.enabled; + } + + private void ConfigForm_Load(object sender, EventArgs e) { if (!controller.GetConfiguration().isDefault) { @@ -76,6 +124,23 @@ namespace Shadowsocks.View } } + private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) + { + if (oldSelectedIndex == ServersListBox.SelectedIndex) + { + // we are moving back to oldSelectedIndex + return; + } + if (!saveOldSelectedServer()) + { + // why this won't cause stack overflow? + ServersListBox.SelectedIndex = oldSelectedIndex; + return; + } + loadSelectedServer(); + oldSelectedIndex = ServersListBox.SelectedIndex; + } + private void Config_Click(object sender, EventArgs e) { showWindow(); @@ -88,37 +153,16 @@ namespace Shadowsocks.View private void OKButton_Click(object sender, EventArgs e) { - try - { - Server server = new Server - { - server = IPTextBox.Text, - server_port = int.Parse(ServerPortTextBox.Text), - password = PasswordTextBox.Text, - local_port = int.Parse(ProxyPortTextBox.Text), - method = EncryptionSelect.Text - }; - Configuration config = controller.GetConfiguration(); - config.configs.Clear(); - config.configs.Add(server); - config.index = 0; - controller.SaveConfig(config); - this.Hide(); - } - catch (FormatException) - { - MessageBox.Show("illegal port number format"); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message); - } + // TODO + Configuration config = controller.GetConfiguration(); + controller.SaveConfig(config); + this.Hide(); } private void CancelButton_Click(object sender, EventArgs e) { this.Hide(); - updateUI(); + loadCurrentConfiguration(); } private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)