diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 5dd95b50..4d26b72f 100644 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -125,6 +125,16 @@ namespace Shadowsocks.Model } } + public static Server AddDefaultServerOrServer(Configuration config, Server server = null) + { + if (config != null && config.configs != null) + { + server = (server ?? GetDefaultServer()); + config.configs.Add(server); + } + return server; + } + public static Server GetDefaultServer() { return new Server(); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 17aad474..2716e931 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -273,8 +273,7 @@ namespace Shadowsocks.View { return; } - Server server = Configuration.GetDefaultServer(); - _modifiedConfiguration.configs.Add(server); + Configuration.AddDefaultServerOrServer(_modifiedConfiguration); LoadServerNameListToUI(_modifiedConfiguration); ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; _lastSelectedIndex = ServersListBox.SelectedIndex; @@ -282,7 +281,8 @@ namespace Shadowsocks.View private void DuplicateButton_Click(object sender, EventArgs e) { - if (!ValidateAndSaveSelectedServerDetails()) + if (_lastSelectedIndex == -1 || _lastSelectedIndex > _modifiedConfiguration.configs.Count + || !ValidateAndSaveSelectedServerDetails()) { return; } @@ -301,6 +301,10 @@ namespace Shadowsocks.View { _modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex); } + if (_modifiedConfiguration.configs.Count == 0) + { + Configuration.AddDefaultServerOrServer(_modifiedConfiguration); + } if (_lastSelectedIndex >= _modifiedConfiguration.configs.Count) { // can be -1 @@ -310,6 +314,8 @@ namespace Shadowsocks.View LoadServerNameListToUI(_modifiedConfiguration); ServersListBox.SelectedIndex = _lastSelectedIndex; LoadSelectedServerDetails(); + + UpdateButtons(); } private void OKButton_Click(object sender, EventArgs e)