Refactoring

This commit is contained in:
qianlifeng 2014-12-21 22:03:03 +08:00
parent c20314f83c
commit 2b211c2ba0
26 changed files with 168 additions and 273 deletions

View File

@ -1,9 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Helper namespace Wox.Infrastructure.Exceptions
{ {
public class WoxException : Exception public class WoxException : Exception
{ {

View File

@ -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)
{
}
}
}

View File

@ -0,0 +1,10 @@
namespace Wox.Infrastructure.Exceptions
{
public class WoxJsonRPCException : WoxException
{
public WoxJsonRPCException(string msg)
: base(msg)
{
}
}
}

View File

@ -4,7 +4,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Wox.Plugin; using Wox.Plugin;
namespace Wox.Infrastructure namespace Wox.Infrastructure.Hotkey
{ {
public enum KeyEvent : int public enum KeyEvent : int
{ {

View File

@ -1,11 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media.Animation;
namespace Wox.Infrastructure namespace Wox.Infrastructure.Hotkey
{ {
public class HotkeyModel public class HotkeyModel
{ {

View File

@ -1,7 +1,7 @@
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin; using Wox.Plugin;
namespace Wox.Helper namespace Wox.Infrastructure.Http
{ {
public class HttpProxy : IHttpProxy public class HttpProxy : IHttpProxy
{ {

View File

@ -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;
}
}
}

View File

@ -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<string, string> 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;
}
}
}

View File

@ -57,7 +57,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
public List<ProgramSource> ProgramSources { get; set; } public List<ProgramSource> ProgramSources { get; set; }
[JsonProperty] [JsonProperty]
public List<FolderLink> FolderLinks { get; set; } //Aaron public List<FolderLink> FolderLinks { get; set; }
public List<CustomizedPluginConfig> CustomizedPluginConfigs { get; set; } public List<CustomizedPluginConfig> CustomizedPluginConfigs { get; set; }

View File

@ -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;
}
}
}

View File

@ -56,6 +56,10 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Exceptions\WoxException.cs" />
<Compile Include="Exceptions\WoxHttpException.cs" />
<Compile Include="Exceptions\WoxJsonRPCException.cs" />
<Compile Include="Http\HttpProxy.cs" />
<Compile Include="Logger\Log.cs" /> <Compile Include="Logger\Log.cs" />
<Compile Include="PeHeaderReader.cs" /> <Compile Include="PeHeaderReader.cs" />
<Compile Include="Storage\BinaryStorage.cs" /> <Compile Include="Storage\BinaryStorage.cs" />
@ -66,11 +70,11 @@
<Compile Include="Timeit.cs" /> <Compile Include="Timeit.cs" />
<Compile Include="Unidecoder.Characters.cs" /> <Compile Include="Unidecoder.Characters.cs" />
<Compile Include="ChineseToPinYin.cs" /> <Compile Include="ChineseToPinYin.cs" />
<Compile Include="HttpRequest.cs" /> <Compile Include="Http\HttpRequest.cs" />
<Compile Include="Storage\BaseStorage.cs" /> <Compile Include="Storage\BaseStorage.cs" />
<Compile Include="FuzzyMatcher.cs" /> <Compile Include="FuzzyMatcher.cs" />
<Compile Include="GlobalHotkey.cs" /> <Compile Include="Hotkey\GlobalHotkey.cs" />
<Compile Include="HotkeyModel.cs" /> <Compile Include="Hotkey\HotkeyModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Storage\UserSelectedRecordStorage.cs" /> <Compile Include="Storage\UserSelectedRecordStorage.cs" />
<Compile Include="Storage\UserSettings\UserSettingStorage.cs" /> <Compile Include="Storage\UserSettings\UserSettingStorage.cs" />
@ -78,7 +82,6 @@
<Compile Include="Storage\UserSettings\ProgramSource.cs" /> <Compile Include="Storage\UserSettings\ProgramSource.cs" />
<Compile Include="Storage\UserSettings\WebSearch.cs" /> <Compile Include="Storage\UserSettings\WebSearch.cs" />
<Compile Include="StringEmptyConverter.cs" /> <Compile Include="StringEmptyConverter.cs" />
<Compile Include="StringNullOrEmptyToVisibilityConverter.cs" />
<Compile Include="Unidecoder.cs" /> <Compile Include="Unidecoder.cs" />
<Compile Include="WindowsShellRun.cs" /> <Compile Include="WindowsShellRun.cs" />
</ItemGroup> </ItemGroup>
@ -91,6 +94,7 @@
<Name>Wox.Plugin</Name> <Name>Wox.Plugin</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -9,6 +9,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using YAMP.Numerics; using YAMP.Numerics;
namespace Wox.Plugin.SystemPlugins.SuggestionSources namespace Wox.Plugin.SystemPlugins.SuggestionSources
@ -19,36 +20,30 @@ namespace Wox.Plugin.SystemPlugins.SuggestionSources
public override List<string> GetSuggestions(string query) public override List<string> GetSuggestions(string query)
{ {
try var result = HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), "GB2312");
{ if (string.IsNullOrEmpty(result)) return new List<string>();
var response =
HttpRequest.CreateGetHttpResponse(
"http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), null,
null, null);
var stream = response.GetResponseStream();
if (stream != null) Match match = reg.Match(result);
if (match.Success)
{
JContainer json = null;
try
{ {
var body = new StreamReader(stream, Encoding.GetEncoding("GB2312")).ReadToEnd(); json =JsonConvert.DeserializeObject(match.Groups[1].Value) as JContainer;
Match m = reg.Match(body); }
if (m.Success) catch{}
if (json != null)
{
var results = json["s"] as JArray;
if (results != null)
{ {
var json = JsonConvert.DeserializeObject(m.Groups[1].Value) as JContainer; return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
if (json != null)
{
var results = json["s"] as JArray;
if (results != null)
{
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
}
} }
} }
} }
catch
{ }
return null; return new List<string>();
} }
} }
} }

View File

@ -8,6 +8,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using YAMP.Numerics; using YAMP.Numerics;
namespace Wox.Plugin.SystemPlugins.SuggestionSources namespace Wox.Plugin.SystemPlugins.SuggestionSources
@ -16,32 +17,24 @@ namespace Wox.Plugin.SystemPlugins.SuggestionSources
{ {
public override List<string> GetSuggestions(string query) public override List<string> GetSuggestions(string query)
{ {
var result = HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query));
if (string.IsNullOrEmpty(result)) return new List<string>();
try try
{ {
var response = JContainer json = JsonConvert.DeserializeObject(result) as JContainer;
HttpRequest.CreateGetHttpResponse( if (json != null)
"https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), null,
null, null);
var stream = response.GetResponseStream();
if (stream != null)
{ {
var body = new StreamReader(stream).ReadToEnd(); var results = json[1] as JContainer;
var json = JsonConvert.DeserializeObject(body) as JContainer; if (results != null)
if (json != null)
{ {
var results = json[1] as JContainer; return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
if (results != null)
{
return results.OfType<JValue>().Select(o => o.Value).OfType<string>().ToList();
}
} }
} }
} }
catch catch { }
{ }
return new List<string>();
return null;
} }
} }
} }

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Plugin
{
public class PythonResult : Result
{
public string ActionName { get; set; }
public string ActionPara { get; set; }
}
}

View File

@ -55,7 +55,6 @@
<Compile Include="PluginMetadata.cs" /> <Compile Include="PluginMetadata.cs" />
<Compile Include="PluginType.cs" /> <Compile Include="PluginType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PythonResult.cs" />
<Compile Include="Query.cs" /> <Compile Include="Query.cs" />
<Compile Include="Result.cs" /> <Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" /> <Compile Include="ActionContext.cs" />

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
namespace Wox.Test.Infrastructure
{
[TestFixture]
public class HttpRequestTest
{
[Test]
public void RequestTest()
{
string results = HttpRequest.Get("https://www.bing.com");
Assert.IsNotNullOrEmpty(results);
}
}
}

View File

@ -44,6 +44,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="FuzzyMatcherTest.cs" /> <Compile Include="FuzzyMatcherTest.cs" />
<Compile Include="Infrastructure\HttpRequestTest.cs" />
<Compile Include="QueryTest.cs" /> <Compile Include="QueryTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UrlPluginTest.cs" /> <Compile Include="UrlPluginTest.cs" />

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Helper
{
public class WoxJsonPRCException : WoxException
{
public WoxJsonPRCException(string msg)
: base(msg)
{
}
}
}

View File

@ -8,6 +8,7 @@ using NHotkey;
using NHotkey.Wpf; using NHotkey.Wpf;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Plugin; using Wox.Plugin;
using KeyEventArgs = System.Windows.Input.KeyEventArgs; using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using UserControl = System.Windows.Controls.UserControl; using UserControl = System.Windows.Controls.UserControl;

View File

@ -18,6 +18,7 @@ using Wox.Commands;
using Wox.Helper; using Wox.Helper;
using Wox.ImageLoader; using Wox.ImageLoader;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin; using Wox.Plugin;

View File

@ -10,6 +10,7 @@ using System.Windows.Forms;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Helper; using Wox.Helper;
using Wox.Helper.ErrorReporting; using Wox.Helper.ErrorReporting;
using Wox.Infrastructure.Exceptions;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.JsonRPC; using Wox.JsonRPC;
using Wox.Plugin; using Wox.Plugin;
@ -142,7 +143,7 @@ namespace Wox.PluginLoader
string error = errorReader.ReadToEnd(); string error = errorReader.ReadToEnd();
if (!string.IsNullOrEmpty(error)) if (!string.IsNullOrEmpty(error))
{ {
ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonPRCException(error)); ErrorReporting.TryShowErrorMessageBox(error, new WoxJsonRPCException(error));
} }
} }
} }

View File

@ -6,6 +6,7 @@ using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure.Exceptions;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin; using Wox.Plugin;

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin; using Wox.Plugin;

View File

@ -2,6 +2,8 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.JsonRPC; using Wox.JsonRPC;
using Wox.Plugin; using Wox.Plugin;

View File

@ -8,6 +8,7 @@ using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Http;
namespace Wox.Update namespace Wox.Update
{ {
@ -25,28 +26,20 @@ namespace Wox.Update
public Release CheckUpgrade(bool forceCheck = false) public Release CheckUpgrade(bool forceCheck = false)
{ {
if (checkedUpdate && !forceCheck) return newRelease; if (checkedUpdate && !forceCheck) return newRelease;
string json = HttpRequest.Get(updateURL);
if (string.IsNullOrEmpty(json)) return null;
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(updateURL, HttpProxy.Instance); try
Stream s = response.GetResponseStream();
if (s != null)
{ {
StreamReader reader = new StreamReader(s, Encoding.UTF8); newRelease = JsonConvert.DeserializeObject<Release>(json);
string json = reader.ReadToEnd(); if (!IsNewerThanCurrent(newRelease))
try
{
newRelease = JsonConvert.DeserializeObject<Release>(json);
}
catch
{ {
newRelease = null;
} }
checkedUpdate = true;
} }
catch{}
if (!IsNewerThanCurrent(newRelease))
{
newRelease = null;
}
checkedUpdate = true;
return newRelease; return newRelease;
} }

View File

@ -136,7 +136,6 @@
<DependentUpon>WPFErrorReportingDialog.xaml</DependentUpon> <DependentUpon>WPFErrorReportingDialog.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Helper\FontHelper.cs" /> <Compile Include="Helper\FontHelper.cs" />
<Compile Include="Helper\HttpProxy.cs" />
<Compile Include="ImageLoader\ImageLoader.cs" /> <Compile Include="ImageLoader\ImageLoader.cs" />
<Compile Include="Helper\SingleInstance.cs" /> <Compile Include="Helper\SingleInstance.cs" />
<Compile Include="Helper\SyntaxSugars.cs" /> <Compile Include="Helper\SyntaxSugars.cs" />
@ -150,8 +149,6 @@
<Compile Include="Helper\DispatcherExtensions.cs" /> <Compile Include="Helper\DispatcherExtensions.cs" />
<Compile Include="Helper\DWMDropShadow.cs" /> <Compile Include="Helper\DWMDropShadow.cs" />
<Compile Include="Helper\PluginInstaller.cs" /> <Compile Include="Helper\PluginInstaller.cs" />
<Compile Include="Helper\WoxException.cs" />
<Compile Include="Helper\WoxPythonException.cs" />
<Compile Include="HotkeyControl.xaml.cs"> <Compile Include="HotkeyControl.xaml.cs">
<DependentUpon>HotkeyControl.xaml</DependentUpon> <DependentUpon>HotkeyControl.xaml</DependentUpon>
</Compile> </Compile>