From afa46b63b7dbce6ab97df6151ed9d216bf3bf638 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 8 Nov 2014 12:33:00 +0800 Subject: [PATCH] implement removing server and saving --- .../Controller/ShadowsocksController.cs | 3 +- shadowsocks-csharp/Model/Configuration.cs | 10 +++- .../View/ConfigForm.Designer.cs | 28 +++++------ shadowsocks-csharp/View/ConfigForm.cs | 50 +++++++++++++++---- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index d5a02f6b..5823e448 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -53,7 +53,8 @@ namespace Shadowsocks.Controller public void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); - config = newConfig; + // some logic in configuration updated the config when saving, we need to read it again + config = Configuration.Load(); local.Stop(); polipoRunner.Stop(); diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 5efc2805..a19649fb 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -67,9 +67,17 @@ namespace Shadowsocks.Model public static void Save(Configuration config) { + if (config.index >= config.configs.Count) + { + config.index = config.configs.Count - 1; + } + if (config.index < 0) + { + config.index = 0; + } + config.isDefault = false; try { - config.isDefault = false; using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create))) { string jsonString = SimpleJson.SimpleJson.SerializeObject(config); diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 765aa5d6..4abd6fb9 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -56,12 +56,12 @@ this.panel3 = new System.Windows.Forms.Panel(); this.DeleteButton = new System.Windows.Forms.Button(); this.AddButton = new System.Windows.Forms.Button(); - this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); - this.groupBox1.SuspendLayout(); + this.ServerGroupBox.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 @@ -328,15 +328,15 @@ this.AddButton.UseVisualStyleBackColor = true; this.AddButton.Click += new System.EventHandler(this.AddButton_Click); // - // groupBox1 + // ServerGroupBox // - this.groupBox1.Controls.Add(this.tableLayoutPanel1); - this.groupBox1.Location = new System.Drawing.Point(182, 12); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(257, 186); - this.groupBox1.TabIndex = 6; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Server"; + this.ServerGroupBox.Controls.Add(this.tableLayoutPanel1); + this.ServerGroupBox.Location = new System.Drawing.Point(182, 12); + this.ServerGroupBox.Name = "ServerGroupBox"; + this.ServerGroupBox.Size = new System.Drawing.Size(257, 186); + this.ServerGroupBox.TabIndex = 6; + this.ServerGroupBox.TabStop = false; + this.ServerGroupBox.Text = "Server"; // // ServersListBox // @@ -355,7 +355,7 @@ this.AutoSize = true; this.ClientSize = new System.Drawing.Size(445, 286); this.Controls.Add(this.ServersListBox); - this.Controls.Add(this.groupBox1); + this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); this.Controls.Add(this.panel3); this.Controls.Add(this.panel2); @@ -371,8 +371,8 @@ this.tableLayoutPanel1.PerformLayout(); this.panel1.ResumeLayout(false); this.panel3.ResumeLayout(false); - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); + this.ServerGroupBox.ResumeLayout(false); + this.ServerGroupBox.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -406,7 +406,7 @@ private System.Windows.Forms.Panel panel3; private System.Windows.Forms.Button DeleteButton; private System.Windows.Forms.Button AddButton; - private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.GroupBox ServerGroupBox; private System.Windows.Forms.ListBox ServersListBox; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 4b5d4e13..2a284bb0 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -88,13 +88,21 @@ namespace Shadowsocks.View 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; + 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 = server.local_port.ToString(); + EncryptionSelect.Text = server.method == null ? "aes-256-cfb" : server.method; + ServerGroupBox.Visible = true; + } + else + { + ServerGroupBox.Visible = false; + } } private void loadConfiguration(Configuration configuration) @@ -161,7 +169,20 @@ namespace Shadowsocks.View private void DeleteButton_Click(object sender, EventArgs e) { - + oldSelectedIndex = ServersListBox.SelectedIndex; + if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count) + { + modifiedConfiguration.configs.RemoveAt(oldSelectedIndex); + } + if (oldSelectedIndex >= modifiedConfiguration.configs.Count) + { + // can be -1 + oldSelectedIndex = modifiedConfiguration.configs.Count - 1; + } + ServersListBox.SelectedIndex = oldSelectedIndex; + loadConfiguration(modifiedConfiguration); + ServersListBox.SelectedIndex = oldSelectedIndex; + loadSelectedServer(); } private void Config_Click(object sender, EventArgs e) @@ -176,9 +197,16 @@ namespace Shadowsocks.View private void OKButton_Click(object sender, EventArgs e) { - // TODO - Configuration config = controller.GetConfiguration(); - controller.SaveConfig(config); + if (!saveOldSelectedServer()) + { + return; + } + if (modifiedConfiguration.configs.Count == 0) + { + MessageBox.Show("Please add at least one server"); + return; + } + controller.SaveConfig(modifiedConfiguration); this.Hide(); }