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

@@ -0,0 +1,13 @@
using System;
namespace Wox.Infrastructure.Exceptions
{
public class WoxException : Exception
{
public WoxException(string msg)
: base(msg)
{
}
}
}

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 Wox.Plugin;
namespace Wox.Infrastructure
namespace Wox.Infrastructure.Hotkey
{
public enum KeyEvent : int
{

View File

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

View File

@@ -0,0 +1,44 @@
using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin;
namespace Wox.Infrastructure.Http
{
public class HttpProxy : IHttpProxy
{
private static readonly HttpProxy instance = new HttpProxy();
private HttpProxy()
{
}
public static HttpProxy Instance
{
get { return instance; }
}
public bool Enabled
{
get { return UserSettingStorage.Instance.ProxyEnabled; }
}
public string Server
{
get { return UserSettingStorage.Instance.ProxyServer; }
}
public int Port
{
get { return UserSettingStorage.Instance.ProxyPort; }
}
public string UserName
{
get { return UserSettingStorage.Instance.ProxyUserName; }
}
public string Password
{
get { return UserSettingStorage.Instance.ProxyPassword; }
}
}
}

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; }
[JsonProperty]
public List<FolderLink> FolderLinks { get; set; } //Aaron
public List<FolderLink> FolderLinks { 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" />
</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="PeHeaderReader.cs" />
<Compile Include="Storage\BinaryStorage.cs" />
@@ -66,11 +70,11 @@
<Compile Include="Timeit.cs" />
<Compile Include="Unidecoder.Characters.cs" />
<Compile Include="ChineseToPinYin.cs" />
<Compile Include="HttpRequest.cs" />
<Compile Include="Http\HttpRequest.cs" />
<Compile Include="Storage\BaseStorage.cs" />
<Compile Include="FuzzyMatcher.cs" />
<Compile Include="GlobalHotkey.cs" />
<Compile Include="HotkeyModel.cs" />
<Compile Include="Hotkey\GlobalHotkey.cs" />
<Compile Include="Hotkey\HotkeyModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Storage\UserSelectedRecordStorage.cs" />
<Compile Include="Storage\UserSettings\UserSettingStorage.cs" />
@@ -78,7 +82,6 @@
<Compile Include="Storage\UserSettings\ProgramSource.cs" />
<Compile Include="Storage\UserSettings\WebSearch.cs" />
<Compile Include="StringEmptyConverter.cs" />
<Compile Include="StringNullOrEmptyToVisibilityConverter.cs" />
<Compile Include="Unidecoder.cs" />
<Compile Include="WindowsShellRun.cs" />
</ItemGroup>
@@ -91,6 +94,7 @@
<Name>Wox.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.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.