Browse Source

connect view and controller

tags/4.2.1.0
Student Main 4 years ago
parent
commit
787f33acae
No known key found for this signature in database GPG Key ID: AA78519C208C8742
4 changed files with 73 additions and 25 deletions
  1. +13
    -5
      shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs
  2. +2
    -2
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  3. +18
    -14
      shadowsocks-csharp/View/OnlineConfigForm.Designer.cs
  4. +40
    -4
      shadowsocks-csharp/View/OnlineConfigForm.cs

+ 13
- 5
shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs View File

@@ -29,7 +29,12 @@ namespace Shadowsocks.Controller.Service
try try
{ {
string str = await httpClient.GetStringAsync(url); string str = await httpClient.GetStringAsync(url);
return Get(str);
var ret = Get(str);
foreach (var item in ret)
{
item.group = url;
}
return ret;
} }
catch (Exception e) catch (Exception e)
{ {
@@ -62,11 +67,9 @@ namespace Shadowsocks.Controller.Service
{ {
var l = new List<Server>(); var l = new List<Server>();
if (o == null) return l; if (o == null) return l;
try
{
if (IsServerObject(o))
return new List<Server> { o.ToObject<Server>() }; return new List<Server> { o.ToObject<Server>() };
}
catch { };

foreach (var kv in o) foreach (var kv in o)
{ {
JToken v = kv.Value; JToken v = kv.Value;
@@ -87,5 +90,10 @@ namespace Shadowsocks.Controller.Service
return SearchJArray(t as JArray); return SearchJArray(t as JArray);
} }
} }

private static bool IsServerObject(JObject o)
{
return new[] { "server", "server_port", "password", "method" }.All(i => o.ContainsKey(i));
}
} }
} }

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

@@ -698,7 +698,7 @@ namespace Shadowsocks.Controller
.Concat(onlineServer) .Concat(onlineServer)
); );
_config.index = _config.configs.IndexOf(selected); _config.index = _config.configs.IndexOf(selected);
Configuration.Save(_config);
SaveConfig(_config);
} }
public async Task UpdateAllOnlineConfig() public async Task UpdateAllOnlineConfig()
@@ -714,7 +714,7 @@ namespace Shadowsocks.Controller
.Concat(r.SelectMany(s => s)) .Concat(r.SelectMany(s => s))
); );
_config.index = _config.configs.IndexOf(selected); _config.index = _config.configs.IndexOf(selected);
Configuration.Save(_config);
SaveConfig(_config);
} }
public void SaveOnlineConfigSource(IEnumerable<string> vs) public void SaveOnlineConfigSource(IEnumerable<string> vs)


+ 18
- 14
shadowsocks-csharp/View/OnlineConfigForm.Designer.cs View File

@@ -35,8 +35,8 @@
this.UpdateButton = new System.Windows.Forms.Button(); this.UpdateButton = new System.Windows.Forms.Button();
this.AddButton = new System.Windows.Forms.Button(); this.AddButton = new System.Windows.Forms.Button();
this.DeleteButton = new System.Windows.Forms.Button(); this.DeleteButton = new System.Windows.Forms.Button();
this.UpdateAllButton = new System.Windows.Forms.Button();
this.OkButton = new System.Windows.Forms.Button(); this.OkButton = new System.Windows.Forms.Button();
this.UpdateAllButton = new System.Windows.Forms.Button();
this.CancelButton = new System.Windows.Forms.Button(); this.CancelButton = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@@ -77,6 +77,7 @@
this.UrlListBox.Name = "UrlListBox"; this.UrlListBox.Name = "UrlListBox";
this.UrlListBox.Size = new System.Drawing.Size(482, 344); this.UrlListBox.Size = new System.Drawing.Size(482, 344);
this.UrlListBox.TabIndex = 0; this.UrlListBox.TabIndex = 0;
this.UrlListBox.SelectedIndexChanged += new System.EventHandler(this.UrlListBox_SelectedIndexChanged);
// //
// label1 // label1
// //
@@ -110,6 +111,7 @@
this.UpdateButton.TabIndex = 3; this.UpdateButton.TabIndex = 3;
this.UpdateButton.Text = "Update"; this.UpdateButton.Text = "Update";
this.UpdateButton.UseVisualStyleBackColor = true; this.UpdateButton.UseVisualStyleBackColor = true;
this.UpdateButton.Click += new System.EventHandler(this.UpdateButton_Click);
// //
// AddButton // AddButton
// //
@@ -137,19 +139,7 @@
this.DeleteButton.TabIndex = 5; this.DeleteButton.TabIndex = 5;
this.DeleteButton.Text = "Delete"; this.DeleteButton.Text = "Delete";
this.DeleteButton.UseVisualStyleBackColor = true; this.DeleteButton.UseVisualStyleBackColor = true;
//
// UpdateAllButton
//
this.UpdateAllButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.UpdateAllButton.Location = new System.Drawing.Point(20, 428);
this.UpdateAllButton.Margin = new System.Windows.Forms.Padding(20, 5, 20, 5);
this.UpdateAllButton.MaximumSize = new System.Drawing.Size(0, 32);
this.UpdateAllButton.MinimumSize = new System.Drawing.Size(0, 32);
this.UpdateAllButton.Name = "UpdateAllButton";
this.UpdateAllButton.Size = new System.Drawing.Size(122, 32);
this.UpdateAllButton.TabIndex = 6;
this.UpdateAllButton.Text = "Update all";
this.UpdateAllButton.UseVisualStyleBackColor = true;
this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click);
// //
// OkButton // OkButton
// //
@@ -164,6 +154,20 @@
this.OkButton.Text = "OK"; this.OkButton.Text = "OK";
this.OkButton.UseVisualStyleBackColor = true; this.OkButton.UseVisualStyleBackColor = true;
// //
// UpdateAllButton
//
this.UpdateAllButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.UpdateAllButton.Location = new System.Drawing.Point(20, 428);
this.UpdateAllButton.Margin = new System.Windows.Forms.Padding(20, 5, 20, 5);
this.UpdateAllButton.MaximumSize = new System.Drawing.Size(0, 32);
this.UpdateAllButton.MinimumSize = new System.Drawing.Size(0, 32);
this.UpdateAllButton.Name = "UpdateAllButton";
this.UpdateAllButton.Size = new System.Drawing.Size(122, 32);
this.UpdateAllButton.TabIndex = 6;
this.UpdateAllButton.Text = "Update all";
this.UpdateAllButton.UseVisualStyleBackColor = true;
this.UpdateAllButton.Click += new System.EventHandler(this.UpdateAllButton_Click);
//
// CancelButton // CancelButton
// //
this.CancelButton.Dock = System.Windows.Forms.DockStyle.Fill; this.CancelButton.Dock = System.Windows.Forms.DockStyle.Fill;


+ 40
- 4
shadowsocks-csharp/View/OnlineConfigForm.cs View File

@@ -39,6 +39,12 @@ namespace Shadowsocks.View


if (idx >= UrlListBox.Items.Count) idx = 0; if (idx >= UrlListBox.Items.Count) idx = 0;
UrlListBox.SelectedIndex = idx; UrlListBox.SelectedIndex = idx;
SelectItem();
}

private void SelectItem()
{
UrlTextBox.Text = (string)UrlListBox.SelectedItem;
} }


private bool ValidateUrl() private bool ValidateUrl()
@@ -57,10 +63,11 @@ namespace Shadowsocks.View


private bool Commit() private bool Commit()
{ {
if (!ValidateUrl()) return false;
UrlListBox.Items[UrlListBox.SelectedIndex] = UrlTextBox.Text;
controller.SaveOnlineConfigSource(UrlListBox.Items.OfType<string>().Where(s=>!string.IsNullOrWhiteSpace(s)));
if (ValidateUrl())
{
UrlListBox.Items[UrlListBox.SelectedIndex] = UrlTextBox.Text;
}
controller.SaveOnlineConfigSource(UrlListBox.Items.OfType<string>().Where(s => !string.IsNullOrWhiteSpace(s)));
LoadConfig(); LoadConfig();
return true; return true;
} }
@@ -72,5 +79,34 @@ namespace Shadowsocks.View
UrlTextBox.Text = DefaultPrefix; UrlTextBox.Text = DefaultPrefix;
UrlListBox.SelectedIndex = UrlListBox.Items.Count - 1; UrlListBox.SelectedIndex = UrlListBox.Items.Count - 1;
} }

private void UpdateButton_Click(object sender, EventArgs e)
{
// update content, also update online config
Commit();

_ = controller.UpdateOnlineConfig(UrlTextBox.Text);
}

private void UrlListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (!UrlListBox.CanSelect)
{
return;
}
SelectItem();
}

private void DeleteButton_Click(object sender, EventArgs e)
{
int idx = UrlListBox.SelectedIndex;
UrlListBox.Items.RemoveAt(idx);
Commit();
}

private void UpdateAllButton_Click(object sender, EventArgs e)
{
_ = controller.UpdateAllOnlineConfig();
}
} }
} }

Loading…
Cancel
Save