diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index e6e80d05..9837dc38 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -1,4 +1,5 @@ -using Shadowsocks.Model; +using System.IO; +using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Text; @@ -129,7 +130,7 @@ namespace Shadowsocks.Controller } - protected void SaveConfig(Configuration newConfig) + public void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); // some logic in configuration updated the config when saving, we need to read it again @@ -173,5 +174,22 @@ namespace Shadowsocks.Controller UpdateSystemProxy(); } + private void SetLog() + { + try + { + FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); + TextWriter tmp = Console.Out; + StreamWriter sw = new StreamWriter(fs); + sw.AutoFlush = true; + Console.SetOut(sw); + Console.SetError(sw); + } + catch (IOException e) + { + Console.WriteLine(e.ToString()); + } + } + } } diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 65b0a3b6..60aabbdc 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -17,6 +17,7 @@ namespace Shadowsocks.Model public bool enabled; public bool shareOverLan; public bool isDefault; + public bool noChange; private static string CONFIG_FILE = "gui-config.json"; diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index a42a97ae..c90f089b 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -54,7 +54,6 @@ namespace Shadowsocks // TODO run without a main form to save RAM Application.Run(new ConfigForm(controller)); - } } } diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index cf874286..f8aa54cf 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -418,6 +418,7 @@ this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // + // // ConfigForm // this.AcceptButton = this.OKButton; diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 8bd980b8..0059678e 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -16,9 +16,10 @@ namespace Shadowsocks.View private UpdateChecker updateChecker; // this is a copy of configuration that we are working on - private Configuration modifiedConfiguration; - private int oldSelectedIndex = -1; - private bool isFirstRun; + private Configuration _modifiedConfiguration; + private Configuration _oldConfiguration; + private int _oldSelectedIndex = -1; + private bool _isFirstRun; public ConfigForm(ShadowsocksController controller) { @@ -66,7 +67,7 @@ namespace Shadowsocks.View notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; notifyIcon1.BalloonTipClicked += notifyIcon1_BalloonTipClicked; notifyIcon1.ShowBalloonTip(5000); - isFirstRun = false; + _isFirstRun = false; } void notifyIcon1_BalloonTipClicked(object sender, EventArgs e) @@ -82,11 +83,11 @@ namespace Shadowsocks.View IPTextBox.Focus(); } - private bool SaveOldSelectedServer() + private bool SaveOldSelectedServer(object sender) { try { - if (oldSelectedIndex == -1 || oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) { return true; } @@ -100,7 +101,27 @@ namespace Shadowsocks.View remarks = RemarksTextBox.Text }; Configuration.CheckServer(server); - modifiedConfiguration.configs[oldSelectedIndex] = server; + + _modifiedConfiguration.configs[_oldSelectedIndex] = server; + if (sender.Equals(OKButton)) + { + if (_oldConfiguration.configs[_oldConfiguration.index].server == + _modifiedConfiguration.configs[_oldSelectedIndex].server && + _oldConfiguration.configs[_oldConfiguration.index].server_port == + _modifiedConfiguration.configs[_oldSelectedIndex].server_port && + _oldConfiguration.configs[_oldConfiguration.index].password == + _modifiedConfiguration.configs[_oldSelectedIndex].password && + _oldConfiguration.configs[_oldConfiguration.index].local_port == + _modifiedConfiguration.configs[_oldSelectedIndex].local_port) + { + _modifiedConfiguration.noChange = true; + } + else + { + _modifiedConfiguration.noChange = false; + _oldConfiguration = _modifiedConfiguration; + } + } return true; } catch (FormatException) @@ -116,15 +137,15 @@ namespace Shadowsocks.View private void LoadSelectedServer() { - if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < modifiedConfiguration.configs.Count) + if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count) { - Server server = modifiedConfiguration.configs[ServersListBox.SelectedIndex]; + 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; + EncryptionSelect.Text = server.method ?? "aes-256-cfb"; RemarksTextBox.Text = server.remarks; ServerGroupBox.Visible = true; //IPTextBox.Focus(); @@ -138,7 +159,7 @@ namespace Shadowsocks.View private void LoadConfiguration(Configuration configuration) { ServersListBox.Items.Clear(); - foreach (Server server in modifiedConfiguration.configs) + foreach (Server server in _modifiedConfiguration.configs) { ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")"); } @@ -146,15 +167,16 @@ namespace Shadowsocks.View private void LoadCurrentConfiguration() { - modifiedConfiguration = controller.GetConfiguration(); - LoadConfiguration(modifiedConfiguration); - oldSelectedIndex = modifiedConfiguration.index; - ServersListBox.SelectedIndex = modifiedConfiguration.index; + _modifiedConfiguration = controller.GetConfiguration(); + _oldConfiguration = _modifiedConfiguration; + LoadConfiguration(_modifiedConfiguration); + _oldSelectedIndex = _modifiedConfiguration.index; + ServersListBox.SelectedIndex = _modifiedConfiguration.index; LoadSelectedServer(); UpdateServersMenu(); - enableItem.Checked = modifiedConfiguration.enabled; - ShareOverLANItem.Checked = modifiedConfiguration.shareOverLan; + enableItem.Checked = _modifiedConfiguration.enabled; + ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; } private void UpdateServersMenu() @@ -193,56 +215,56 @@ namespace Shadowsocks.View } else { - isFirstRun = true; + _isFirstRun = true; } updateChecker.CheckUpdate(); } private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) { - if (oldSelectedIndex == ServersListBox.SelectedIndex) + if (_oldSelectedIndex == ServersListBox.SelectedIndex) { // we are moving back to oldSelectedIndex or doing a force move return; } - if (!SaveOldSelectedServer()) + if (!SaveOldSelectedServer(sender)) { // why this won't cause stack overflow? - ServersListBox.SelectedIndex = oldSelectedIndex; + ServersListBox.SelectedIndex = _oldSelectedIndex; return; } LoadSelectedServer(); - oldSelectedIndex = ServersListBox.SelectedIndex; + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void AddButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!SaveOldSelectedServer(sender)) { return; } Server server = Configuration.GetDefaultServer(); - modifiedConfiguration.configs.Add(server); - LoadConfiguration(modifiedConfiguration); - ServersListBox.SelectedIndex = modifiedConfiguration.configs.Count - 1; - oldSelectedIndex = ServersListBox.SelectedIndex; + _modifiedConfiguration.configs.Add(server); + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; + _oldSelectedIndex = ServersListBox.SelectedIndex; } private void DeleteButton_Click(object sender, EventArgs e) { - oldSelectedIndex = ServersListBox.SelectedIndex; - if (oldSelectedIndex >= 0 && oldSelectedIndex < modifiedConfiguration.configs.Count) + _oldSelectedIndex = ServersListBox.SelectedIndex; + if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count) { - modifiedConfiguration.configs.RemoveAt(oldSelectedIndex); + _modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex); } - if (oldSelectedIndex >= modifiedConfiguration.configs.Count) + if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count) { // can be -1 - oldSelectedIndex = modifiedConfiguration.configs.Count - 1; + _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1; } - ServersListBox.SelectedIndex = oldSelectedIndex; - LoadConfiguration(modifiedConfiguration); - ServersListBox.SelectedIndex = oldSelectedIndex; + ServersListBox.SelectedIndex = _oldSelectedIndex; + LoadConfiguration(_modifiedConfiguration); + ServersListBox.SelectedIndex = _oldSelectedIndex; LoadSelectedServer(); } @@ -258,28 +280,28 @@ namespace Shadowsocks.View private void ShowFirstTimeBalloon() { - if (isFirstRun) + if (_isFirstRun) { notifyIcon1.BalloonTipTitle = "Shadowsocks is here"; notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu"; notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; notifyIcon1.ShowBalloonTip(0); - isFirstRun = false; + _isFirstRun = false; } } private void OKButton_Click(object sender, EventArgs e) { - if (!SaveOldSelectedServer()) + if (!SaveOldSelectedServer(sender)) { return; } - if (modifiedConfiguration.configs.Count == 0) + if (_modifiedConfiguration.configs.Count == 0) { MessageBox.Show("Please add at least one server"); return; } - controller.SaveServers(modifiedConfiguration.configs); + controller.SaveConfig(_modifiedConfiguration); this.Hide(); ShowFirstTimeBalloon(); } @@ -348,5 +370,10 @@ namespace Shadowsocks.View qrCodeForm.Icon = this.Icon; qrCodeForm.Show(); } + + private void enableLogBox_CheckedChanged(object sender, EventArgs e) + { + MessageBox.Show("This option only works on next startup."); + } } }