Skip to content

Commit 3d39420

Browse files
Fixed "New Profile", refactored background UI updates and cleaned up code
1 parent dfa3a71 commit 3d39420

21 files changed

+386
-396
lines changed

WireSockUI/Config/Profile.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ internal static void ValidateAddresses(string section, string key, string keyVal
7373
public String PrivateKey
7474
{
7575
get { return _privateKey; }
76-
set {
76+
set
77+
{
7778
ValidateKey("Interface", "PrivateKey", value);
7879
_privateKey = value;
7980
}
@@ -89,7 +90,7 @@ public String PublicKey
8990
if (!String.IsNullOrEmpty(_privateKey))
9091
{
9192
// Determine public key from private key data
92-
return
93+
return
9394
Convert.ToBase64String(
9495
Curve25519.GetPublicKey(
9596
Convert.FromBase64String(this.PrivateKey)));
@@ -144,10 +145,7 @@ public String MTU
144145
{
145146
if (!String.IsNullOrWhiteSpace(value))
146147
{
147-
148-
int mtu;
149-
150-
if (!int.TryParse(value, out mtu))
148+
if (!int.TryParse(value, out int mtu))
151149
throw new FormatException("\"MTU\" in \"Interface\", is not a numerical value.");
152150

153151
if (mtu < 576 || mtu > 65535)
@@ -180,7 +178,7 @@ public String PeerKey
180178
/// </summary>
181179
public String PresharedKey
182180
{
183-
get { return _presharedKey; }
181+
get { return _presharedKey; }
184182
set
185183
{
186184
ValidateKey("Peer", "PresharedKey", value);
@@ -210,11 +208,12 @@ public String AllowedIPs
210208
public String Endpoint
211209
{
212210
get { return _endpoint; }
213-
set {
211+
set
212+
{
214213
if (!Native.IPHelper.IsValidAddress(value))
215214
throw new FormatException("\"Endpoint\" in \"Peer\", is not a valid IPv4, IPv6 or domain address.");
216215

217-
_endpoint = value;
216+
_endpoint = value;
218217
}
219218
}
220219

@@ -232,10 +231,7 @@ public String PersistentKeepAlive
232231
{
233232
if (!String.IsNullOrWhiteSpace(value))
234233
{
235-
236-
int mtu;
237-
238-
if (!int.TryParse(value, out mtu))
234+
if (!int.TryParse(value, out int mtu))
239235
throw new FormatException("\"PersistentKeepalive\" in \"Peer\", is not a numerical value.");
240236

241237
if (mtu < 0 || mtu > 65535)
@@ -316,7 +312,8 @@ public String Socks5Proxy
316312
throw new FormatException("\"Endpoint\" in \"Peer\", is not a valid IPv4, IPv6 or domain address.");
317313

318314
_socks5Proxy = value;
319-
} else
315+
}
316+
else
320317
{
321318
_socks5Proxy = null;
322319
}
@@ -391,8 +388,8 @@ public static Profile LoadProfile(string profileName)
391388
/// <summary>
392389
/// Create an empty profile from scratch
393390
/// </summary>
394-
public Profile()
395-
{
391+
public Profile()
392+
{
396393
}
397394

398395
/// <summary>

WireSockUI/Extensions/ControlTextExtender.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.ComponentModel;
43
using System.Resources;
54
using System.Windows.Forms;
65

76
namespace WireSockUI.Extensions
87
{
98
[ProvideProperty("ResourceKey", typeof(Control))]
10-
public class ControlTextExtender: Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
9+
public class ControlTextExtender : Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
1110
{
12-
private Dictionary<Control, string> _items;
11+
private readonly Dictionary<Control, string> _items;
1312

1413
public ControlTextExtender() : base()
1514
{
@@ -26,9 +25,7 @@ public bool CanExtend(object extendee)
2625

2726
public string GetResourceKey(Control item)
2827
{
29-
string value;
30-
31-
if (_items.TryGetValue(item, out value))
28+
if (_items.TryGetValue(item, out string value))
3229
return value;
3330

3431
return null;

WireSockUI/Extensions/DictionaryExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ namespace WireSockUI.Extensions
44
{
55
public static class DictionaryExtensions
66
{
7-
public static TVal Get<TKey, TVal>(this Dictionary<TKey, TVal> dictionary, TKey key, TVal defaultVal = default(TVal))
7+
public static TVal Get<TKey, TVal>(this Dictionary<TKey, TVal> dictionary, TKey key, TVal defaultVal = default)
88
{
9-
TVal val;
10-
11-
if (dictionary.TryGetValue(key, out val))
9+
if (dictionary.TryGetValue(key, out TVal val))
1210
return val;
1311

1412
return defaultVal;

WireSockUI/Extensions/MenuTextExtender.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.ComponentModel;
43
using System.Resources;
54
using System.Windows.Forms;
65

76
namespace WireSockUI.Extensions
87
{
98
[ProvideProperty("ResourceKey", typeof(ToolStripItem))]
10-
internal class MenuTextExtender: Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
9+
internal class MenuTextExtender : Component, System.ComponentModel.IExtenderProvider, ISupportInitialize
1110
{
1211
private Dictionary<ToolStripItem, string> _items;
1312

14-
public MenuTextExtender() : base()
13+
public MenuTextExtender() : base()
1514
{
1615
_items = new Dictionary<ToolStripItem, string>();
1716
}
@@ -58,6 +57,6 @@ public void EndInit()
5857
}
5958
}
6059

61-
60+
6261
}
6362
}

WireSockUI/Extensions/TimeExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using WireSockUI.Properties;
73

84
namespace WireSockUI.Extensions

WireSockUI/Forms/EditForm.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WireSockUI/Forms/EditForm.cs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace WireSockUI.Forms
1111
{
1212
public partial class frmEdit : Form
1313
{
14-
private static Regex profileMatch = new Regex(@"^\s*((?<comment>[;#].*)|(?<section>\[\w+\])|((?<key>[a-zA-Z0-9]+)[ \t]*=[ \t]*(?<value>.*?)))$", RegexOptions.Multiline | RegexOptions.Compiled);
15-
private static Regex multiValueMatch = new Regex(@"[^, ]*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
14+
private static readonly Regex _profileMatch = new Regex(@"^\s*((?<comment>[;#].*)|(?<section>\[\w+\])|((?<key>[a-zA-Z0-9]+)[ \t]*=[ \t]*(?<value>.*?)))$", RegexOptions.Multiline | RegexOptions.Compiled);
15+
private static readonly Regex _multiValueMatch = new Regex(@"[^, ]*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
1616

1717
public String ReturnValue
1818
{
1919
get; private set;
2020
}
2121

22-
private void applySyntaxHighlighting()
22+
private void ApplySyntaxHighlighting()
2323
{
2424
bool hasErrors = false;
2525

@@ -36,7 +36,7 @@ private void applySyntaxHighlighting()
3636
txtEditor.SelectionColor = originalColor;
3737
txtEditor.SelectionFont = new Font(txtEditor.SelectionFont, FontStyle.Regular);
3838

39-
foreach (Match m in profileMatch.Matches(txtEditor.Text))
39+
foreach (Match m in _profileMatch.Matches(txtEditor.Text))
4040
{
4141
if (m.Groups["comment"].Success)
4242
{
@@ -102,7 +102,7 @@ private void applySyntaxHighlighting()
102102
// base64 256-bit keys
103103
case "privatekey":
104104
case "publickey":
105-
case "presharedkey":
105+
case "presharedkey":
106106
{
107107
if (!String.IsNullOrEmpty(value))
108108
{
@@ -133,7 +133,7 @@ private void applySyntaxHighlighting()
133133
case "allowedips":
134134
case "disallowedips":
135135
{
136-
foreach (Match e in multiValueMatch.Matches(value))
136+
foreach (Match e in _multiValueMatch.Matches(value))
137137
{
138138
if (!String.IsNullOrWhiteSpace(e.Value) && !IPHelper.IsValidIPNetwork(e.Value))
139139
{
@@ -149,7 +149,7 @@ private void applySyntaxHighlighting()
149149
// IPv4/IPv6 values
150150
case "dns":
151151
{
152-
foreach (Match e in multiValueMatch.Matches(value))
152+
foreach (Match e in _multiValueMatch.Matches(value))
153153
{
154154
if (!String.IsNullOrWhiteSpace(e.Value) && !IPHelper.IsValidIPAddress(e.Value))
155155
{
@@ -194,7 +194,7 @@ private void applySyntaxHighlighting()
194194
case "allowedapps":
195195
case "disallowedapps":
196196
{
197-
foreach (Match e in multiValueMatch.Matches(value))
197+
foreach (Match e in _multiValueMatch.Matches(value))
198198
{
199199
if (!String.IsNullOrWhiteSpace(e.Value) && !Regex.IsMatch(e.Value, @"^[a-z0-9_-]+$", RegexOptions.IgnoreCase))
200200
{
@@ -232,34 +232,37 @@ private void applySyntaxHighlighting()
232232
btnSave.Enabled = !hasErrors;
233233
}
234234

235-
public frmEdit()
235+
private void Initialize()
236236
{
237237
InitializeComponent();
238238

239239
this.Icon = Resources.ico;
240-
241240
txtProfileName.SetCueBanner(Resources.EditProfileCue);
242241
}
243242

244-
public frmEdit(string config): this()
243+
public frmEdit()
245244
{
246-
if (String.IsNullOrEmpty(config))
247-
{
248-
Text = Resources.EditProfileTitleNew;
249-
txtEditor.Text = Resources.template_conf;
250-
}
251-
else
252-
{
253-
Text = String.Format(Resources.EditProfileTitle, config);
245+
Initialize();
254246

255-
txtProfileName.Text = config.ToLowerInvariant();
256-
txtEditor.Text = File.ReadAllText(Path.Combine(Global.ConfigsFolder, config + ".conf"));
257-
}
247+
this.Text = Resources.EditProfileTitleNew;
248+
txtEditor.Text = Resources.template_conf;
258249

259-
applySyntaxHighlighting();
250+
ApplySyntaxHighlighting();
260251
}
261252

262-
private void SaveProfile(object sender, EventArgs e)
253+
public frmEdit(string config)
254+
{
255+
Initialize();
256+
257+
Text = String.Format(Resources.EditProfileTitle, config);
258+
259+
txtProfileName.Text = config.ToLowerInvariant();
260+
txtEditor.Text = File.ReadAllText(Path.Combine(Global.ConfigsFolder, config + ".conf"));
261+
262+
ApplySyntaxHighlighting();
263+
}
264+
265+
private void OnSaveClick(object sender, EventArgs e)
263266
{
264267
String tmpProfile = Path.GetTempFileName();
265268
File.WriteAllText(tmpProfile, txtEditor.Text);
@@ -292,11 +295,11 @@ private void SaveProfile(object sender, EventArgs e)
292295
File.Move(tmpProfile, profilePath);
293296

294297
this.ReturnValue = txtProfileName.Text;
295-
298+
296299
Close();
297300
}
298-
299-
private void FindProcess(object sender, EventArgs e)
301+
302+
private void OnProcessClick(object sender, EventArgs e)
300303
{
301304
using (TaskManager taskManager = new TaskManager())
302305
{
@@ -308,9 +311,9 @@ private void FindProcess(object sender, EventArgs e)
308311
}
309312
}
310313

311-
private void ProfileChanged(object sender, EventArgs e)
314+
private void OnProfileChanged(object sender, EventArgs e)
312315
{
313-
applySyntaxHighlighting();
316+
ApplySyntaxHighlighting();
314317
}
315318
}
316319
}

0 commit comments

Comments
 (0)