@@ -14,9 +14,9 @@ namespace Shadowsocks.Controller | |||
{ | |||
class PACServer : Listener.Service | |||
{ | |||
public static readonly string PAC_FILE = "pac.txt"; | |||
public static readonly string USER_RULE_FILE = "user-rule.txt"; | |||
public static readonly string USER_ABP_FILE = "abp.txt"; | |||
public const string PAC_FILE = "pac.txt"; | |||
public const string USER_RULE_FILE = "user-rule.txt"; | |||
public const string USER_ABP_FILE = "abp.txt"; | |||
FileSystemWatcher PACFileWatcher; | |||
FileSystemWatcher UserRuleFileWatcher; | |||
@@ -1,6 +1,5 @@ | |||
using System; | |||
using System.Reflection; | |||
using Shadowsocks.View; | |||
namespace Shadowsocks.Controller.Hotkeys | |||
{ | |||
@@ -67,13 +66,7 @@ namespace Shadowsocks.Controller.Hotkeys | |||
private void ShowLogsCallback() | |||
{ | |||
// Get the current MenuViewController in this program via reflection | |||
FieldInfo fi = Assembly.GetExecutingAssembly().GetType("Shadowsocks.Program") | |||
.GetField("_viewController", | |||
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase); | |||
// To retrieve the value of a static field, pass null here | |||
var mvc = fi.GetValue(null) as MenuViewController; | |||
mvc.ShowLogForm_HotKey(); | |||
Program.MenuController.ShowLogForm_HotKey(); | |||
} | |||
private void ServerMoveUpCallback() | |||
@@ -2,7 +2,6 @@ | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.Linq; | |||
using System.Windows.Forms; | |||
using System.Windows.Input; | |||
using GlobalHotKey; | |||
@@ -171,11 +170,5 @@ namespace Shadowsocks.Controller.Hotkeys | |||
if(_keymap.ContainsKey(key)) | |||
_keymap.Remove(key); | |||
} | |||
public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control | |||
{ | |||
var children = control.Controls.Count > 0 ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>(); | |||
return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children); | |||
} | |||
} | |||
} |
@@ -14,9 +14,8 @@ namespace Shadowsocks | |||
{ | |||
static class Program | |||
{ | |||
private static ShadowsocksController _controller; | |||
// XXX: Don't change this name | |||
private static MenuViewController _viewController; | |||
public static ShadowsocksController MainController { get; private set; } | |||
public static MenuViewController MenuController { get; private set; } | |||
/// <summary> | |||
/// 应用程序的主入口点。 | |||
@@ -80,10 +79,10 @@ namespace Shadowsocks | |||
#else | |||
Logging.OpenLogFile(); | |||
#endif | |||
_controller = new ShadowsocksController(); | |||
_viewController = new MenuViewController(_controller); | |||
HotKeys.Init(_controller); | |||
_controller.Start(); | |||
MainController = new ShadowsocksController(); | |||
MenuController = new MenuViewController(MainController); | |||
HotKeys.Init(MainController); | |||
MainController.Start(); | |||
Application.Run(); | |||
} | |||
} | |||
@@ -121,7 +120,7 @@ namespace Shadowsocks | |||
{ | |||
case PowerModes.Resume: | |||
Logging.Info("os wake up"); | |||
if (_controller != null) | |||
if (MainController != null) | |||
{ | |||
System.Timers.Timer timer = new System.Timers.Timer(10 * 1000); | |||
timer.Elapsed += Timer_Elapsed; | |||
@@ -131,9 +130,9 @@ namespace Shadowsocks | |||
} | |||
break; | |||
case PowerModes.Suspend: | |||
if (_controller != null) | |||
if (MainController != null) | |||
{ | |||
_controller.Stop(); | |||
MainController.Stop(); | |||
Logging.Info("controller stopped"); | |||
} | |||
Logging.Info("os suspend"); | |||
@@ -145,9 +144,9 @@ namespace Shadowsocks | |||
{ | |||
try | |||
{ | |||
if (_controller != null) | |||
if (MainController != null) | |||
{ | |||
_controller.Start(); | |||
MainController.Start(); | |||
Logging.Info("controller started"); | |||
} | |||
} | |||
@@ -178,10 +177,10 @@ namespace Shadowsocks | |||
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; | |||
Application.ThreadException -= Application_ThreadException; | |||
HotKeys.Destroy(); | |||
if (_controller != null) | |||
if (MainController != null) | |||
{ | |||
_controller.Stop(); | |||
_controller = null; | |||
MainController.Stop(); | |||
MainController = null; | |||
} | |||
} | |||
} | |||
@@ -23,7 +23,7 @@ namespace Shadowsocks.Util | |||
} | |||
} | |||
public class Utils | |||
public static class Utils | |||
{ | |||
private static bool? _portableMode; | |||
private static string TempPath = null; | |||
@@ -0,0 +1,21 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Windows.Forms; | |||
namespace Shadowsocks.Util | |||
{ | |||
public static class ViewUtils | |||
{ | |||
public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control | |||
{ | |||
if (control.Controls.Count == 0) | |||
{ | |||
return Enumerable.Empty<TControl>(); | |||
} | |||
var children = control.Controls.OfType<TControl>().ToList(); | |||
return children.SelectMany(GetChildControls<TControl>).Concat(children); | |||
} | |||
} | |||
} |
@@ -16,23 +16,18 @@ namespace Shadowsocks.View | |||
{ | |||
public partial class HotkeySettingsForm : Form | |||
{ | |||
private ShadowsocksController _controller; | |||
private readonly ShadowsocksController _controller; | |||
// this is a copy of configuration that we are working on | |||
private HotkeyConfig _modifiedConfig; | |||
private StringBuilder _sb = new StringBuilder(); | |||
private IEnumerable<TextBox> _allTextBoxes; | |||
private Label _lb = null; | |||
private HotKeys.HotKeyCallBackHandler _callBack = null; | |||
private readonly IEnumerable<TextBox> _allTextBoxes; | |||
public HotkeySettingsForm(ShadowsocksController controller) | |||
{ | |||
InitializeComponent(); | |||
UpdateTexts(); | |||
this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); | |||
Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); | |||
_controller = controller; | |||
_controller.ConfigChanged += controller_ConfigChanged; | |||
@@ -40,7 +35,7 @@ namespace Shadowsocks.View | |||
LoadCurrentConfiguration(); | |||
// get all textboxes belong to this form | |||
_allTextBoxes = HotKeys.GetChildControls<TextBox>(this.tableLayoutPanel1); | |||
_allTextBoxes = tableLayoutPanel1.GetChildControls<TextBox>(); | |||
if (!_allTextBoxes.Any()) throw new Exception("Cannot get all textboxes"); | |||
} | |||
@@ -77,7 +72,7 @@ namespace Shadowsocks.View | |||
btnOK.Text = I18N.GetString("OK"); | |||
btnCancel.Text = I18N.GetString("Cancel"); | |||
btnRegisterAll.Text = I18N.GetString("Reg All"); | |||
this.Text = I18N.GetString("Edit Hotkeys..."); | |||
Text = I18N.GetString("Edit Hotkeys..."); | |||
} | |||
/// <summary> | |||
@@ -85,7 +80,7 @@ namespace Shadowsocks.View | |||
/// </summary> | |||
private void HotkeyDown(object sender, KeyEventArgs e) | |||
{ | |||
_sb.Length = 0; | |||
StringBuilder sb = new StringBuilder(); | |||
//Combination key only | |||
if (e.Modifiers != 0) | |||
{ | |||
@@ -93,15 +88,15 @@ namespace Shadowsocks.View | |||
// Windows key is reserved by operating system, we deny this key. | |||
if (e.Control) | |||
{ | |||
_sb.Append("Ctrl+"); | |||
sb.Append("Ctrl+"); | |||
} | |||
if (e.Alt) | |||
{ | |||
_sb.Append("Alt+"); | |||
sb.Append("Alt+"); | |||
} | |||
if (e.Shift) | |||
{ | |||
_sb.Append("Shift+"); | |||
sb.Append("Shift+"); | |||
} | |||
Keys keyvalue = (Keys) e.KeyValue; | |||
@@ -109,18 +104,18 @@ namespace Shadowsocks.View | |||
(keyvalue >= Keys.A && keyvalue <= Keys.Z) || | |||
(keyvalue >= Keys.F1 && keyvalue <= Keys.F12)) | |||
{ | |||
_sb.Append(e.KeyCode); | |||
sb.Append(e.KeyCode); | |||
} | |||
else if (keyvalue >= Keys.D0 && keyvalue <= Keys.D9) | |||
{ | |||
_sb.Append('D').Append((char) e.KeyValue); | |||
sb.Append('D').Append((char) e.KeyValue); | |||
} | |||
else if (keyvalue >= Keys.NumPad0 && keyvalue <= Keys.NumPad9) | |||
{ | |||
_sb.Append("NumPad").Append((char) (e.KeyValue - 48)); | |||
sb.Append("NumPad").Append((char) (e.KeyValue - 48)); | |||
} | |||
} | |||
((TextBox) sender).Text = _sb.ToString(); | |||
((TextBox) sender).Text = sb.ToString(); | |||
} | |||
/// <summary> | |||
@@ -128,8 +123,8 @@ namespace Shadowsocks.View | |||
/// </summary> | |||
private void HotkeyUp(object sender, KeyEventArgs e) | |||
{ | |||
TextBox tb = sender as TextBox; | |||
string content = tb.Text.TrimEnd(); | |||
var tb = (TextBox) sender; | |||
var content = tb.Text.TrimEnd(); | |||
if (content.Length >= 1 && content[content.Length - 1] == '+') | |||
{ | |||
tb.Text = ""; | |||
@@ -138,7 +133,7 @@ namespace Shadowsocks.View | |||
private void TextBox_TextChanged(object sender, EventArgs e) | |||
{ | |||
TextBox tb = sender as TextBox; | |||
var tb = (TextBox) sender; | |||
if (tb.Text == "") | |||
{ | |||
@@ -149,15 +144,17 @@ namespace Shadowsocks.View | |||
private void UnregHotkey(TextBox tb) | |||
{ | |||
HotKeys.HotKeyCallBackHandler callBack; | |||
Label lb; | |||
PrepareForHotkey(tb, out _callBack, out _lb); | |||
PrepareForHotkey(tb, out callBack, out lb); | |||
UnregPrevHotkey(_callBack); | |||
UnregPrevHotkey(callBack); | |||
} | |||
private void CancelButton_Click(object sender, EventArgs e) | |||
{ | |||
this.Close(); | |||
Close(); | |||
} | |||
private void OKButton_Click(object sender, EventArgs e) | |||
@@ -178,7 +175,7 @@ namespace Shadowsocks.View | |||
// All check passed, saving | |||
SaveConfig(); | |||
this.Close(); | |||
Close(); | |||
} | |||
private void RegisterAllButton_Click(object sender, EventArgs e) | |||
@@ -203,9 +200,12 @@ namespace Shadowsocks.View | |||
return false; | |||
} | |||
PrepareForHotkey(tb, out _callBack, out _lb); | |||
HotKeys.HotKeyCallBackHandler callBack; | |||
Label lb; | |||
PrepareForHotkey(tb, out callBack, out lb); | |||
UnregPrevHotkey(_callBack); | |||
UnregPrevHotkey(callBack); | |||
// try to register keys | |||
// if already registered by other progs | |||
@@ -218,8 +218,8 @@ namespace Shadowsocks.View | |||
// or change to another one | |||
// Transparent without color: first run or empty config | |||
bool regResult = HotKeys.Regist(hotkey, _callBack); | |||
_lb.BackColor = regResult ? Color.Green : Color.Yellow; | |||
bool regResult = HotKeys.Regist(hotkey, callBack); | |||
lb.BackColor = regResult ? Color.Green : Color.Yellow; | |||
return regResult; | |||
} | |||
@@ -277,7 +277,7 @@ namespace Shadowsocks.View | |||
} | |||
cb = callback as HotKeys.HotKeyCallBackHandler; | |||
object label = GetFieldViaName(this.GetType(), labelName, this); | |||
var label = GetFieldViaName(GetType(), labelName, this); | |||
if (label == null) | |||
{ | |||
throw new Exception($"{labelName} not found"); | |||
@@ -202,6 +202,7 @@ | |||
<Compile Include="Util\SystemProxy\RAS.cs" /> | |||
<Compile Include="Util\SystemProxy\WinINet.cs" /> | |||
<Compile Include="Util\Util.cs" /> | |||
<Compile Include="Util\ViewUtils.cs" /> | |||
<Compile Include="View\ConfigForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||