From 7faf2c992b3e1849a567c7fed09bfda5ce629a5a Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 9 Nov 2014 14:01:26 +0800 Subject: [PATCH] do not expose SaveConfig to Views --- .../Controller/ShadowsocksController.cs | 65 +++++++++++-------- .../View/ConfigForm.Designer.cs | 2 +- shadowsocks-csharp/View/ConfigForm.cs | 6 +- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 43af496..e6e80d0 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -52,32 +52,6 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } - public void SaveConfig(Configuration newConfig) - { - Configuration.Save(newConfig); - // some logic in configuration updated the config when saving, we need to read it again - _config = Configuration.Load(); - - pacServer.Stop(); - local.Stop(); - - // don't put polipoRunner.Start() before pacServer.Stop() - // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1 - // though UseShellExecute is set to true now - // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open - polipoRunner.Stop(); - polipoRunner.Start(_config); - - local = new Local(_config); - local.Start(); - pacServer.Start(_config); - - if (ConfigChanged != null) - { - ConfigChanged(this, new EventArgs()); - } - } - public Server GetCurrentServer() { return _config.GetCurrentServer(); @@ -89,6 +63,11 @@ namespace Shadowsocks.Controller return Configuration.Load(); } + public void SaveServers(List servers) + { + _config.configs = servers; + SaveConfig(_config); + } public void ToggleEnable(bool enabled) { @@ -111,6 +90,12 @@ namespace Shadowsocks.Controller } } + public void SelectServerIndex(int index) + { + _config.index = index; + SaveConfig(_config); + } + public void Stop() { if (stopped) @@ -143,6 +128,34 @@ namespace Shadowsocks.Controller return "ss://" + base64; } + + protected void SaveConfig(Configuration newConfig) + { + Configuration.Save(newConfig); + // some logic in configuration updated the config when saving, we need to read it again + _config = Configuration.Load(); + + pacServer.Stop(); + local.Stop(); + + // don't put polipoRunner.Start() before pacServer.Stop() + // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1 + // though UseShellExecute is set to true now + // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open + polipoRunner.Stop(); + polipoRunner.Start(_config); + + local = new Local(_config); + local.Start(); + pacServer.Start(_config); + + if (ConfigChanged != null) + { + ConfigChanged(this, new EventArgs()); + } + } + + private void UpdateSystemProxy() { if (_config.enabled) diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index d01fc43..cf87428 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -436,7 +436,7 @@ this.MaximizeBox = false; this.Name = "ConfigForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "Shadowsocks"; + 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); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index dfaf4b3..2a7e5db 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -258,7 +258,7 @@ namespace Shadowsocks.View MessageBox.Show("Please add at least one server"); return; } - controller.SaveConfig(modifiedConfiguration); + controller.SaveServers(modifiedConfiguration.configs); this.Hide(); ShowFirstTimeBalloon(); } @@ -306,9 +306,7 @@ namespace Shadowsocks.View private void AServerItem_Click(object sender, EventArgs e) { MenuItem item = (MenuItem)sender; - Configuration configuration = controller.GetConfiguration(); - configuration.index = (int)item.Tag; - controller.SaveConfig(configuration); + controller.SelectServerIndex((int)item.Tag); } private void ShowLogItem_Click(object sender, EventArgs e)