diff --git a/Wox.Infrastructure/HttpRequest.cs b/Wox.Infrastructure/HttpRequest.cs
index 1e53db6d0b..e082d07525 100644
--- a/Wox.Infrastructure/HttpRequest.cs
+++ b/Wox.Infrastructure/HttpRequest.cs
@@ -11,21 +11,11 @@ using System.Text;
//From:http://blog.csdn.net/zhoufoxcn/article/details/6404236
namespace Wox.Infrastructure
{
- ///
- /// 有关HTTP请求的辅助类
- ///
public class HttpRequest
{
private static readonly string DefaultUserAgent = "Wox/" + Assembly.GetEntryAssembly().GetName().Version.ToString() + " (+https://github.com/qianlifeng/Wox)";
- ///
- /// 创建GET方式的HTTP请求
- ///
- /// 请求的URL
- /// 请求的超时时间
- /// 请求的客户端浏览器信息,可以为空
- /// 随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空
- ///
+
public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
@@ -50,16 +40,7 @@ namespace Wox.Infrastructure
}
return request.GetResponse() as HttpWebResponse;
}
- ///
- /// 创建POST方式的HTTP请求
- ///
- /// 请求的URL
- /// 随同请求POST的参数名称及参数值字典
- /// 请求的超时时间
- /// 请求的客户端浏览器信息,可以为空
- /// 发送HTTP请求时所用的编码
- /// 随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空
- ///
+
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
@@ -71,7 +52,6 @@ namespace Wox.Infrastructure
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
- //如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
@@ -103,7 +83,6 @@ namespace Wox.Infrastructure
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
- //如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
@@ -131,7 +110,7 @@ namespace Wox.Infrastructure
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
- return true; //总是接受
+ return true;
}
}
}
diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs
index 18d1962995..8350fce8a6 100644
--- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs
+++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs
@@ -70,6 +70,12 @@ namespace Wox.Infrastructure.Storage.UserSettings
[JsonProperty]
public OpacityMode OpacityMode { get; set; }
+ [JsonProperty]
+ public bool EnableWebSearchSuggestion { get; set; }
+
+ [JsonProperty]
+ public string WebSearchSuggestionSource { get; set; }
+
[JsonProperty]
public bool LeaveCmdOpen { get; set; }
diff --git a/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs b/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs
new file mode 100644
index 0000000000..2b4c03dfc1
--- /dev/null
+++ b/Wox.Plugin.SystemPlugins/SuggestionSources/Baidu.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Xml;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using Newtonsoft.Json.Serialization;
+using Wox.Infrastructure;
+using YAMP.Numerics;
+
+namespace Wox.Plugin.SystemPlugins.SuggestionSources
+{
+ public class Baidu : AbstractSuggestionSource
+ {
+ Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
+
+ public override List GetSuggestions(string query)
+ {
+ try
+ {
+ 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)
+ {
+ var body = new StreamReader(stream, Encoding.GetEncoding("GB2312")).ReadToEnd();
+ Match m = reg.Match(body);
+ if (m.Success)
+ {
+ var json = JsonConvert.DeserializeObject(m.Groups[1].Value) as JContainer;
+ if (json != null)
+ {
+ var results = json["s"] as JArray;
+ if (results != null)
+ {
+ return results.OfType().Select(o => o.Value).OfType().ToList();
+ }
+ }
+ }
+ }
+ }
+ catch
+ { }
+
+ return null;
+ }
+ }
+}
diff --git a/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs b/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs
new file mode 100644
index 0000000000..3262320b94
--- /dev/null
+++ b/Wox.Plugin.SystemPlugins/SuggestionSources/SuggestionSourceFactory.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Wox.Plugin.SystemPlugins.SuggestionSources
+{
+ public class SuggestionSourceFactory
+ {
+ public static ISuggestionSource GetSuggestionSource(string name)
+ {
+ switch (name.ToLower())
+ {
+ case "google":
+ return new Google();
+
+ case "baidu":
+ return new Baidu();
+
+ default:
+ return null;
+ }
+ }
+ }
+}
diff --git a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs
index b7afa08dd2..97fe4e505e 100644
--- a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs
+++ b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs
@@ -32,7 +32,7 @@ namespace Wox.Plugin.SystemPlugins
title = subtitle;
subtitle = null;
}
- context.API.PushResults(query,context.CurrentPluginMetadata, new List()
+ context.API.PushResults(query, context.CurrentPluginMetadata, new List()
{
new Result()
{
@@ -48,24 +48,29 @@ namespace Wox.Plugin.SystemPlugins
}
});
- if (!string.IsNullOrEmpty(keyword))
+ if (UserSettingStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
{
- ISuggestionSource sugg = new Google();
- var result = sugg.GetSuggestions(keyword);
- if (result != null)
+ ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(
+ UserSettingStorage.Instance.WebSearchSuggestionSource);
+ if (sugg != null)
{
- context.API.PushResults(query,context.CurrentPluginMetadata, result.Select(o => new Result()
+ var result = sugg.GetSuggestions(keyword);
+ if (result != null)
{
- Title = o,
- SubTitle = subtitle,
- Score = 5,
- IcoPath = webSearch.IconPath,
- Action = (c) =>
- {
- Process.Start(webSearch.Url.Replace("{q}", o));
- return true;
- }
- }).ToList());
+ context.API.PushResults(query, context.CurrentPluginMetadata,
+ result.Select(o => new Result()
+ {
+ Title = o,
+ SubTitle = subtitle,
+ Score = 5,
+ IcoPath = webSearch.IconPath,
+ Action = (c) =>
+ {
+ Process.Start(webSearch.Url.Replace("{q}", o));
+ return true;
+ }
+ }).ToList());
+ }
}
}
}
diff --git a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml
index 419d350076..8e24a5993e 100644
--- a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml
+++ b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml
@@ -7,10 +7,16 @@
d:DesignHeight="300" d:DesignWidth="300">
-
+
+
-
+
+ Enable search suggestions
+
+
+
+
@@ -30,7 +36,7 @@
-
+
diff --git a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml.cs b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml.cs
index 219b8b14fd..0df9622241 100644
--- a/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml.cs
+++ b/Wox.Plugin.SystemPlugins/WebSearch/WebSearchesSetting.xaml.cs
@@ -1,4 +1,6 @@
-using System.Windows;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure.Storage.UserSettings;
@@ -19,6 +21,24 @@ namespace Wox.Plugin.SystemPlugins
private void Setting_Loaded(object sender, RoutedEventArgs e)
{
webSearchView.ItemsSource = UserSettingStorage.Instance.WebSearches;
+ cbEnableWebSearchSuggestion.IsChecked = UserSettingStorage.Instance.EnableWebSearchSuggestion;
+ comboBoxSuggestionSource.Visibility = UserSettingStorage.Instance.EnableWebSearchSuggestion
+ ? Visibility.Visible
+ : Visibility.Collapsed;
+
+ List items = new List()
+ {
+ new ComboBoxItem() {Content = "Google"},
+ new ComboBoxItem() {Content = "Bing" },
+ new ComboBoxItem() {Content = "Baidu"},
+ };
+ ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == UserSettingStorage.Instance.WebSearchSuggestionSource);
+ if (selected == null)
+ {
+ selected = items[0];
+ }
+ comboBoxSuggestionSource.ItemsSource = items;
+ comboBoxSuggestionSource.SelectedItem = selected;
}
public void ReloadWebSearchView()
@@ -66,5 +86,24 @@ namespace Wox.Plugin.SystemPlugins
}
}
+ private void CbEnableWebSearchSuggestion_OnChecked(object sender, RoutedEventArgs e)
+ {
+ comboBoxSuggestionSource.Visibility = Visibility.Visible;
+ UserSettingStorage.Instance.EnableWebSearchSuggestion = true;
+ UserSettingStorage.Instance.Save();
+ }
+
+ private void CbEnableWebSearchSuggestion_OnUnchecked(object sender, RoutedEventArgs e)
+ {
+ comboBoxSuggestionSource.Visibility = Visibility.Collapsed;
+ UserSettingStorage.Instance.EnableWebSearchSuggestion = false;
+ UserSettingStorage.Instance.Save();
+ }
+
+ private void ComboBoxSuggestionSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
+ {
+ UserSettingStorage.Instance.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
+ UserSettingStorage.Instance.Save();
+ }
}
}
diff --git a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj
index 0c101e28f1..78a2e11d4d 100644
--- a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj
+++ b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj
@@ -79,6 +79,8 @@
+
+
WebSearchesSetting.xaml
diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs
index 077dd6bc11..321f1f7cdc 100644
--- a/Wox/MainWindow.xaml.cs
+++ b/Wox/MainWindow.xaml.cs
@@ -55,7 +55,6 @@ namespace Wox
private string lastQuery;
private ToolTip toolTip = new ToolTip();
- private bool isCMDMode = false;
private bool ignoreTextChange = false;
#endregion
@@ -98,7 +97,7 @@ namespace Wox
{
Dispatcher.Invoke(new Action(() =>
{
- var m = new Msg {Owner = GetWindow(this)};
+ var m = new Msg { Owner = GetWindow(this) };
m.Show(title, subTitle, iconPath);
}));
}
@@ -324,8 +323,37 @@ namespace Wox
}
}, TimeSpan.FromSeconds(0), lastQuery);
}
- }, TimeSpan.FromMilliseconds((isCMDMode = tbQuery.Text.StartsWith(">")) ? 0 : 150));
+ }, TimeSpan.FromMilliseconds(ShouldNotDelayQuery ? 0 : 150));
}
+ private bool ShouldNotDelayQuery
+ {
+ get
+ {
+ return (bool)Dispatcher.Invoke(new Func(() =>
+ {
+ return IsCMDMode || IsWebSearchMode;
+ }));
+ }
+ }
+
+ private bool IsCMDMode
+ {
+ get
+ {
+ return tbQuery.Text.StartsWith(">");
+ }
+ }
+
+ private bool IsWebSearchMode
+ {
+ get
+ {
+ Query q = new Query(tbQuery.Text);
+ return !UserSettingStorage.Instance.EnableWebSearchSuggestion &&
+ UserSettingStorage.Instance.WebSearches.Exists(o => o.ActionWord == q.ActionName && o.Enabled);
+ }
+ }
+
private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
{
@@ -446,11 +474,11 @@ namespace Wox
private void updateCmdMode()
{
- var selected = resultCtrl.AcceptSelect();
- if (selected != null)
+ var currentSelectedItem = resultCtrl.GetActiveResult();
+ if (currentSelectedItem != null)
{
ignoreTextChange = true;
- tbQuery.Text = ">" + selected.Title;
+ tbQuery.Text = ">" + currentSelectedItem.Title;
tbQuery.CaretIndex = tbQuery.Text.Length;
}
}
@@ -468,41 +496,41 @@ namespace Wox
case Key.Down:
resultCtrl.SelectNext();
- if (isCMDMode) updateCmdMode();
+ if (IsCMDMode) updateCmdMode();
toolTip.IsOpen = false;
e.Handled = true;
break;
case Key.Up:
resultCtrl.SelectPrev();
- if (isCMDMode) updateCmdMode();
+ if (IsCMDMode) updateCmdMode();
toolTip.IsOpen = false;
e.Handled = true;
break;
case Key.PageDown:
resultCtrl.SelectNextPage();
- if (isCMDMode) updateCmdMode();
+ if (IsCMDMode) updateCmdMode();
toolTip.IsOpen = false;
e.Handled = true;
break;
case Key.PageUp:
resultCtrl.SelectPrevPage();
- if (isCMDMode) updateCmdMode();
+ if (IsCMDMode) updateCmdMode();
toolTip.IsOpen = false;
e.Handled = true;
break;
case Key.Enter:
- AcceptSelect(resultCtrl.AcceptSelect());
+ AcceptSelect(resultCtrl.GetActiveResult());
e.Handled = true;
break;
case Key.Back:
if (BackKeyDownEvent != null)
{
- BackKeyDownEvent(tbQuery,new WoxKeyDownEventArgs()
+ BackKeyDownEvent(tbQuery, new WoxKeyDownEventArgs()
{
Query = tbQuery.Text,
keyEventArgs = e
@@ -511,7 +539,7 @@ namespace Wox
break;
case Key.Tab:
- AcceptSelect(resultCtrl.AcceptSelect());
+ AcceptSelect(resultCtrl.GetActiveResult());
e.Handled = true;
break;
}
@@ -559,7 +587,7 @@ namespace Wox
List l = waitShowResultList.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == lastQuery).ToList();
waitShowResultList.Clear();
resultCtrl.AddResults(l);
- })), TimeSpan.FromMilliseconds(isCMDMode ? 0 : 50));
+ })), TimeSpan.FromMilliseconds(ShouldNotDelayQuery ? 0 : 50));
}
}
diff --git a/Wox/ResultPanel.xaml.cs b/Wox/ResultPanel.xaml.cs
index b55c75a6c8..b65ae4f42d 100644
--- a/Wox/ResultPanel.xaml.cs
+++ b/Wox/ResultPanel.xaml.cs
@@ -126,7 +126,7 @@ namespace Wox
}
}
- public Result AcceptSelect()
+ public Result GetActiveResult()
{
int index = lbResults.SelectedIndex;
if (index < 0) return null;