From 2b211c2ba083fbddf5115ac0c8dab6f6fe747bdd Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sun, 21 Dec 2014 22:03:03 +0800 Subject: [PATCH] Refactoring --- .../Exceptions}/WoxException.cs | 5 +- .../Exceptions/WoxHttpException.cs | 14 ++ .../Exceptions/WoxJsonRPCException.cs | 10 ++ .../{ => Hotkey}/GlobalHotkey.cs | 2 +- .../{ => Hotkey}/HotkeyModel.cs | 4 +- .../Http}/HttpProxy.cs | 2 +- Wox.Infrastructure/Http/HttpRequest.cs | 61 ++++++++ Wox.Infrastructure/HttpRequest.cs | 142 ------------------ .../UserSettings/UserSettingStorage.cs | 2 +- .../StringNullOrEmptyToVisibilityConverter.cs | 26 ---- Wox.Infrastructure/Wox.Infrastructure.csproj | 12 +- .../SuggestionSources/Baidu.cs | 41 +++-- .../SuggestionSources/Google.cs | 31 ++-- Wox.Plugin/PythonResult.cs | 13 -- Wox.Plugin/Wox.Plugin.csproj | 1 - Wox.Test/Infrastructure/HttpRequestTest.cs | 21 +++ Wox.Test/Wox.Test.csproj | 1 + Wox/Helper/WoxPythonException.cs | 15 -- Wox/HotkeyControl.xaml.cs | 1 + Wox/MainWindow.xaml.cs | 1 + Wox/PluginLoader/BasePlugin.cs | 3 +- Wox/PluginLoader/PluginConfigLoader.cs | 1 + Wox/PluginLoader/Plugins.cs | 2 + Wox/PluginLoader/PythonPlugin.cs | 2 + Wox/Update/UpdateChecker.cs | 25 ++- Wox/Wox.csproj | 3 - 26 files changed, 168 insertions(+), 273 deletions(-) rename {Wox/Helper => Wox.Infrastructure/Exceptions}/WoxException.cs (63%) create mode 100644 Wox.Infrastructure/Exceptions/WoxHttpException.cs create mode 100644 Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs rename Wox.Infrastructure/{ => Hotkey}/GlobalHotkey.cs (99%) rename Wox.Infrastructure/{ => Hotkey}/HotkeyModel.cs (97%) rename {Wox/Helper => Wox.Infrastructure/Http}/HttpProxy.cs (96%) create mode 100644 Wox.Infrastructure/Http/HttpRequest.cs delete mode 100644 Wox.Infrastructure/HttpRequest.cs delete mode 100644 Wox.Infrastructure/StringNullOrEmptyToVisibilityConverter.cs delete mode 100644 Wox.Plugin/PythonResult.cs create mode 100644 Wox.Test/Infrastructure/HttpRequestTest.cs delete mode 100644 Wox/Helper/WoxPythonException.cs diff --git a/Wox/Helper/WoxException.cs b/Wox.Infrastructure/Exceptions/WoxException.cs similarity index 63% rename from Wox/Helper/WoxException.cs rename to Wox.Infrastructure/Exceptions/WoxException.cs index dcfb5ff6d6..581f36edb7 100644 --- a/Wox/Helper/WoxException.cs +++ b/Wox.Infrastructure/Exceptions/WoxException.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -namespace Wox.Helper +namespace Wox.Infrastructure.Exceptions { public class WoxException : Exception { diff --git a/Wox.Infrastructure/Exceptions/WoxHttpException.cs b/Wox.Infrastructure/Exceptions/WoxHttpException.cs new file mode 100644 index 0000000000..026d1d8ab9 --- /dev/null +++ b/Wox.Infrastructure/Exceptions/WoxHttpException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Infrastructure.Exceptions +{ + public class WoxHttpException :WoxException + { + public WoxHttpException(string msg) : base(msg) + { + } + } +} diff --git a/Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs b/Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs new file mode 100644 index 0000000000..0e984a3e6f --- /dev/null +++ b/Wox.Infrastructure/Exceptions/WoxJsonRPCException.cs @@ -0,0 +1,10 @@ +namespace Wox.Infrastructure.Exceptions +{ + public class WoxJsonRPCException : WoxException + { + public WoxJsonRPCException(string msg) + : base(msg) + { + } + } +} diff --git a/Wox.Infrastructure/GlobalHotkey.cs b/Wox.Infrastructure/Hotkey/GlobalHotkey.cs similarity index 99% rename from Wox.Infrastructure/GlobalHotkey.cs rename to Wox.Infrastructure/Hotkey/GlobalHotkey.cs index be7cde76a0..8a6f31323b 100644 --- a/Wox.Infrastructure/GlobalHotkey.cs +++ b/Wox.Infrastructure/Hotkey/GlobalHotkey.cs @@ -4,7 +4,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Wox.Plugin; -namespace Wox.Infrastructure +namespace Wox.Infrastructure.Hotkey { public enum KeyEvent : int { diff --git a/Wox.Infrastructure/HotkeyModel.cs b/Wox.Infrastructure/Hotkey/HotkeyModel.cs similarity index 97% rename from Wox.Infrastructure/HotkeyModel.cs rename to Wox.Infrastructure/Hotkey/HotkeyModel.cs index f53447731b..dfc86c404b 100644 --- a/Wox.Infrastructure/HotkeyModel.cs +++ b/Wox.Infrastructure/Hotkey/HotkeyModel.cs @@ -1,11 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Windows.Input; -using System.Windows.Media.Animation; -namespace Wox.Infrastructure +namespace Wox.Infrastructure.Hotkey { public class HotkeyModel { diff --git a/Wox/Helper/HttpProxy.cs b/Wox.Infrastructure/Http/HttpProxy.cs similarity index 96% rename from Wox/Helper/HttpProxy.cs rename to Wox.Infrastructure/Http/HttpProxy.cs index c236994a75..21ce2ded02 100644 --- a/Wox/Helper/HttpProxy.cs +++ b/Wox.Infrastructure/Http/HttpProxy.cs @@ -1,7 +1,7 @@ using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; -namespace Wox.Helper +namespace Wox.Infrastructure.Http { public class HttpProxy : IHttpProxy { diff --git a/Wox.Infrastructure/Http/HttpRequest.cs b/Wox.Infrastructure/Http/HttpRequest.cs new file mode 100644 index 0000000000..6631b633bb --- /dev/null +++ b/Wox.Infrastructure/Http/HttpRequest.cs @@ -0,0 +1,61 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using Wox.Plugin; + +namespace Wox.Infrastructure.Http +{ + public class HttpRequest + { + public static string Get(string url, string encoding = "UTF8") + { + return Get(url, encoding, HttpProxy.Instance); + } + + private static string Get(string url, string encoding, IHttpProxy proxy) + { + if (string.IsNullOrEmpty(url)) return string.Empty; + + HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; + request.Method = "GET"; + request.Timeout = 10 * 1000; + if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server)) + { + if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password)) + { + request.Proxy = new WebProxy(proxy.Server, proxy.Port); + } + else + { + request.Proxy = new WebProxy(proxy.Server, proxy.Port) + { + Credentials = new NetworkCredential(proxy.UserName, proxy.Password) + }; + } + } + + try + { + HttpWebResponse response = request.GetResponse() as HttpWebResponse; + if (response != null) + { + Stream stream = response.GetResponseStream(); + if (stream != null) + { + using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding))) + { + return reader.ReadToEnd(); + } + } + } + } + catch (Exception e) + { + return string.Empty; + } + + return string.Empty; + } + } +} \ No newline at end of file diff --git a/Wox.Infrastructure/HttpRequest.cs b/Wox.Infrastructure/HttpRequest.cs deleted file mode 100644 index c5592e87df..0000000000 --- a/Wox.Infrastructure/HttpRequest.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Security; -using System.Reflection; -using System.Security.Cryptography.X509Certificates; -using System.Text; - -//From:http://blog.csdn.net/zhoufoxcn/article/details/6404236 -using Wox.Plugin; - -namespace Wox.Infrastructure -{ - public class HttpRequest - { - private static readonly string DefaultUserAgent = "Wox/" + Assembly.GetEntryAssembly().GetName().Version.ToString() + " (+https://github.com/qianlifeng/Wox)"; - - - public static HttpWebResponse CreateGetHttpResponse(string url, IHttpProxy proxy) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentNullException("url"); - } - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server)) - { - if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password)) - { - request.Proxy = new WebProxy(proxy.Server, proxy.Port); - } - else - { - request.Proxy = new WebProxy(proxy.Server, proxy.Port); - request.Proxy.Credentials = new NetworkCredential(proxy.UserName, proxy.Password); - } - } - request.Method = "GET"; - request.UserAgent = DefaultUserAgent; - return request.GetResponse() as HttpWebResponse; - } - - public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentNullException("url"); - } - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - request.Method = "GET"; - request.UserAgent = DefaultUserAgent; - if (!string.IsNullOrEmpty(userAgent)) - { - request.UserAgent = userAgent; - } - if (timeout.HasValue) - { - request.Timeout = timeout.Value; - } - if (cookies != null) - { - request.CookieContainer = new CookieContainer(); - request.CookieContainer.Add(cookies); - } - return request.GetResponse() as HttpWebResponse; - } - - public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies) - { - if (string.IsNullOrEmpty(url)) - { - throw new ArgumentNullException("url"); - } - if (requestEncoding == null) - { - throw new ArgumentNullException("requestEncoding"); - } - HttpWebRequest request = null; - if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) - { - ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); - request = WebRequest.Create(url) as HttpWebRequest; - request.ProtocolVersion = HttpVersion.Version10; - } - else - { - request = WebRequest.Create(url) as HttpWebRequest; - } - request.Method = "POST"; - request.ContentType = "application/x-www-form-urlencoded"; - - if (!string.IsNullOrEmpty(userAgent)) - { - request.UserAgent = userAgent; - } - else - { - request.UserAgent = DefaultUserAgent; - } - - if (timeout.HasValue) - { - request.Timeout = timeout.Value; - } - if (cookies != null) - { - request.CookieContainer = new CookieContainer(); - request.CookieContainer.Add(cookies); - } - if (!(parameters == null || parameters.Count == 0)) - { - StringBuilder buffer = new StringBuilder(); - int i = 0; - foreach (string key in parameters.Keys) - { - if (i > 0) - { - buffer.AppendFormat("&{0}={1}", key, parameters[key]); - } - else - { - buffer.AppendFormat("{0}={1}", key, parameters[key]); - } - i++; - } - byte[] data = requestEncoding.GetBytes(buffer.ToString()); - using (Stream stream = request.GetRequestStream()) - { - stream.Write(data, 0, data.Length); - } - } - return request.GetResponse() as HttpWebResponse; - } - - private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) - { - return true; - } - } -} diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index 6f82703e18..229e100a2a 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -57,7 +57,7 @@ namespace Wox.Infrastructure.Storage.UserSettings public List ProgramSources { get; set; } [JsonProperty] - public List FolderLinks { get; set; } //Aaron + public List FolderLinks { get; set; } public List CustomizedPluginConfigs { get; set; } diff --git a/Wox.Infrastructure/StringNullOrEmptyToVisibilityConverter.cs b/Wox.Infrastructure/StringNullOrEmptyToVisibilityConverter.cs deleted file mode 100644 index 841b446a51..0000000000 --- a/Wox.Infrastructure/StringNullOrEmptyToVisibilityConverter.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Globalization; -using System.Windows; -using System.Windows.Data; -using System.Windows.Markup; - -namespace Wox.Infrastructure -{ - public class StringNullOrEmptyToVisibilityConverter : MarkupExtension, IValueConverter - { - public override object ProvideValue(IServiceProvider serviceProvider) - { - return this; - } - - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return string.IsNullOrEmpty(value as string) ? Visibility.Collapsed : Visibility.Visible; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - return null; - } - } -} \ No newline at end of file diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 442f0561ad..c50978f96d 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -56,6 +56,10 @@ + + + + @@ -66,11 +70,11 @@ - + - - + + @@ -78,7 +82,6 @@ - @@ -91,6 +94,7 @@ Wox.Plugin +