diff --git a/shadowsocks-csharp/Controller/HotkeyReg.cs b/shadowsocks-csharp/Controller/HotkeyReg.cs new file mode 100644 index 00000000..5482ec6d --- /dev/null +++ b/shadowsocks-csharp/Controller/HotkeyReg.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +using Shadowsocks.Controller.Hotkeys; +using Shadowsocks.Model; + +namespace Shadowsocks.Controller +{ + static class HotkeyReg + { + public static void RegAllHotkeys() + { + var hotkeyConfig = Configuration.Load().hotkey; + + if (hotkeyConfig == null || !hotkeyConfig.RegHotkeysAtStartup) + return; + + // if any of the hotkey reg fail, undo everything + if (RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback") + && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback") + && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback") + && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback") + && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback") + && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback") + ) + { + // success + } + else + { + RegHotkeyFromString("", "SwitchSystemProxyCallback"); + RegHotkeyFromString("", "SwitchSystemProxyModeCallback"); + RegHotkeyFromString("", "SwitchAllowLanCallback"); + RegHotkeyFromString("", "ShowLogsCallback"); + RegHotkeyFromString("", "ServerMoveUpCallback"); + RegHotkeyFromString("", "ServerMoveDownCallback"); + MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks")); + } + } + + public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Action onComplete = null) + { + var _callback = HotkeyCallbacks.GetCallback(callbackName); + if (_callback == null) + { + throw new Exception($"{callbackName} not found"); + } + + var callback = _callback as HotKeys.HotKeyCallBackHandler; + + if (hotkeyStr.IsNullOrEmpty()) + { + HotKeys.UnregExistingHotkey(callback); + onComplete?.Invoke(RegResult.UnregSuccess); + return true; + } + else + { + var hotkey = HotKeys.Str2HotKey(hotkeyStr); + if (hotkey == null) + { + Logging.Error($"Cannot parse hotkey: {hotkeyStr}"); + onComplete?.Invoke(RegResult.ParseError); + return false; + } + else + { + bool regResult = (HotKeys.RegHotkey(hotkey, callback)); + if (regResult) + { + onComplete?.Invoke(RegResult.RegSuccess); + } + else + { + onComplete?.Invoke(RegResult.RegFailure); + } + return regResult; + } + } + } + + public enum RegResult + { + RegSuccess, + RegFailure, + ParseError, + UnregSuccess, + //UnregFailure + } + } +} diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 93d48088..d25cc494 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -92,7 +92,7 @@ namespace Shadowsocks.Controller public void Start() { Reload(); - StartupHotkeyReg.RegHotkey(); + HotkeyReg.RegAllHotkeys(); } protected void ReportError(Exception e) diff --git a/shadowsocks-csharp/Controller/StartupHotkeyReg.cs b/shadowsocks-csharp/Controller/StartupHotkeyReg.cs deleted file mode 100644 index 11ecaec8..00000000 --- a/shadowsocks-csharp/Controller/StartupHotkeyReg.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Windows.Forms; -using GlobalHotKey; -using Shadowsocks.Controller.Hotkeys; -using Shadowsocks.Model; -using System.Reflection; - -namespace Shadowsocks.Controller -{ - static class StartupHotkeyReg - { - public static void RegHotkey() - { - var _hotKeyConf = Configuration.Load().hotkey; - - if (_hotKeyConf == null || !_hotKeyConf.RegHotkeysAtStartup) - return; - - var _hotKeyDic = new Dictionary(); - - try - { - MethodInfo[] fis = typeof(HotkeyCallbacks).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); - Type ht = _hotKeyConf.GetType(); - for (int i = 0; i < fis.Length; i++) - { - if (fis[i].Name.EndsWith("Callback")) - { - var callbackName = fis[i].Name; - var fieldName = callbackName.Replace("Callback", ""); - - var hk = HotKeys.Str2HotKey(ht.GetField(fieldName).GetValue(_hotKeyConf) as string); - var cb = HotkeyCallbacks.GetCallback(callbackName) as HotKeys.HotKeyCallBackHandler; - - if (hk != null && cb != null) - { - _hotKeyDic.Add(hk, cb); - } - } - } - - int regCount = 0; - foreach (var v in _hotKeyDic) - { - if (!HotKeys.RegHotkey(v.Key, v.Value)) - { - foreach (var k in _hotKeyDic) - { - if (regCount > 0) - { - HotKeys.UnregExistingHotkey(k.Value); - regCount--; - } - } - MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks")); - return; - } - regCount++; - } - } - catch (Exception e) - { - Logging.Error(e); - } - } - } -} diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.cs b/shadowsocks-csharp/View/HotkeySettingsForm.cs index 7a79b17a..8610e993 100644 --- a/shadowsocks-csharp/View/HotkeySettingsForm.cs +++ b/shadowsocks-csharp/View/HotkeySettingsForm.cs @@ -2,15 +2,13 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; -using System.Reflection; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; -using Shadowsocks.Controller.Hotkeys; using Shadowsocks.Model; using Shadowsocks.Properties; -using Shadowsocks.Util; +using static Shadowsocks.Controller.HotkeyReg; namespace Shadowsocks.View { @@ -174,52 +172,33 @@ namespace Shadowsocks.View private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig) { return - RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", SwitchSystemProxyLabel) - && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback", SwitchProxyModeLabel) - && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", SwitchAllowLanLabel) - && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", ShowLogsLabel) - && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", ServerMoveUpLabel) - && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", ServerMoveDownLabel); + RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxy, SwitchSystemProxyLabel, result)) + && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxyMode, SwitchProxyModeLabel, result)) + && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", result => HandleRegResult(hotkeyConfig.SwitchAllowLan, SwitchAllowLanLabel, result)) + && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", result => HandleRegResult(hotkeyConfig.ShowLogs, ShowLogsLabel, result)) + && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", result => HandleRegResult(hotkeyConfig.ServerMoveUp, ServerMoveUpLabel, result)) + && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", result => HandleRegResult(hotkeyConfig.ServerMoveDown, ServerMoveDownLabel, result)); } - private bool RegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = null) + private void HandleRegResult(string hotkeyStr, Label label, RegResult result) { - var _callback = HotkeyCallbacks.GetCallback(callbackName); - if (_callback == null) + switch (result) { - throw new Exception($"{callbackName} not found"); - } - - var callback = _callback as HotKeys.HotKeyCallBackHandler; - - if (hotkeyStr.IsNullOrEmpty()) - { - HotKeys.UnregExistingHotkey(callback); - if (indicator != null) - { - indicator.ResetBackColor(); - } - return true; - } - else - { - var hotkey = HotKeys.Str2HotKey(hotkeyStr); - if (hotkey == null) - { + case RegResult.ParseError: MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), hotkeyStr)); - return false; - } - else - { - bool regResult = (HotKeys.RegHotkey(hotkey, callback)); - if (indicator != null) - { - indicator.BackColor = regResult ? Color.Green : Color.Yellow; - } - return regResult; - } + break; + case RegResult.UnregSuccess: + label.ResetBackColor(); + break; + case RegResult.RegSuccess: + label.BackColor = Color.Green; + break; + case RegResult.RegFailure: + label.BackColor = Color.Red; + break; + default: + break; } - } } } \ No newline at end of file diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 33f3de03..ebd84e60 100644 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -100,7 +100,7 @@ - +