Browse Source

implement removing server and saving

tags/2.3
clowwindy 10 years ago
parent
commit
afa46b63b7
4 changed files with 64 additions and 27 deletions
  1. +2
    -1
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  2. +9
    -1
      shadowsocks-csharp/Model/Configuration.cs
  3. +14
    -14
      shadowsocks-csharp/View/ConfigForm.Designer.cs
  4. +39
    -11
      shadowsocks-csharp/View/ConfigForm.cs

+ 2
- 1
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -53,7 +53,8 @@ namespace Shadowsocks.Controller
public void SaveConfig(Configuration newConfig) public void SaveConfig(Configuration newConfig)
{ {
Configuration.Save(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(); local.Stop();
polipoRunner.Stop(); polipoRunner.Stop();


+ 9
- 1
shadowsocks-csharp/Model/Configuration.cs View File

@@ -67,9 +67,17 @@ namespace Shadowsocks.Model
public static void Save(Configuration config) 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 try
{ {
config.isDefault = false;
using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create))) using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
{ {
string jsonString = SimpleJson.SimpleJson.SerializeObject(config); string jsonString = SimpleJson.SimpleJson.SerializeObject(config);


+ 14
- 14
shadowsocks-csharp/View/ConfigForm.Designer.cs View File

@@ -56,12 +56,12 @@
this.panel3 = new System.Windows.Forms.Panel(); this.panel3 = new System.Windows.Forms.Panel();
this.DeleteButton = new System.Windows.Forms.Button(); this.DeleteButton = new System.Windows.Forms.Button();
this.AddButton = 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.ServersListBox = new System.Windows.Forms.ListBox();
this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
this.panel3.SuspendLayout(); this.panel3.SuspendLayout();
this.groupBox1.SuspendLayout();
this.ServerGroupBox.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// tableLayoutPanel1 // tableLayoutPanel1
@@ -328,15 +328,15 @@
this.AddButton.UseVisualStyleBackColor = true; this.AddButton.UseVisualStyleBackColor = true;
this.AddButton.Click += new System.EventHandler(this.AddButton_Click); 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 // ServersListBox
// //
@@ -355,7 +355,7 @@
this.AutoSize = true; this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(445, 286); this.ClientSize = new System.Drawing.Size(445, 286);
this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServersListBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.ServerGroupBox);
this.Controls.Add(this.panel1); this.Controls.Add(this.panel1);
this.Controls.Add(this.panel3); this.Controls.Add(this.panel3);
this.Controls.Add(this.panel2); this.Controls.Add(this.panel2);
@@ -371,8 +371,8 @@
this.tableLayoutPanel1.PerformLayout(); this.tableLayoutPanel1.PerformLayout();
this.panel1.ResumeLayout(false); this.panel1.ResumeLayout(false);
this.panel3.ResumeLayout(false); this.panel3.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ServerGroupBox.ResumeLayout(false);
this.ServerGroupBox.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -406,7 +406,7 @@
private System.Windows.Forms.Panel panel3; private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Button DeleteButton; private System.Windows.Forms.Button DeleteButton;
private System.Windows.Forms.Button AddButton; private System.Windows.Forms.Button AddButton;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox ServerGroupBox;
private System.Windows.Forms.ListBox ServersListBox; private System.Windows.Forms.ListBox ServersListBox;
} }
} }


+ 39
- 11
shadowsocks-csharp/View/ConfigForm.cs View File

@@ -88,13 +88,21 @@ namespace Shadowsocks.View
private void loadSelectedServer() 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) private void loadConfiguration(Configuration configuration)
@@ -161,7 +169,20 @@ namespace Shadowsocks.View
private void DeleteButton_Click(object sender, EventArgs e) 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) private void Config_Click(object sender, EventArgs e)
@@ -176,9 +197,16 @@ namespace Shadowsocks.View
private void OKButton_Click(object sender, EventArgs e) 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(); this.Hide();
} }


Loading…
Cancel
Save