2014-08-25 16:20:08 +08:00
|
|
|
using System;
|
2014-01-29 22:44:57 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2015-01-07 18:45:55 +08:00
|
|
|
using System.IO;
|
2014-01-29 22:44:57 +08:00
|
|
|
using System.Linq;
|
2015-01-07 18:45:55 +08:00
|
|
|
using System.Reflection;
|
2015-01-03 15:20:34 +08:00
|
|
|
using Wox.Plugin.WebSearch.SuggestionSources;
|
2014-01-29 22:44:57 +08:00
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
namespace Wox.Plugin.WebSearch
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2015-02-06 18:13:22 +08:00
|
|
|
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
private PluginInitContext context;
|
2015-02-28 18:20:06 +08:00
|
|
|
private IDisposable suggestionTimer;
|
2014-03-27 16:56:50 +08:00
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public List<Result> Query(Query query)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
|
|
|
List<Result> results = new List<Result>();
|
2015-02-06 18:13:22 +08:00
|
|
|
if (!query.Search.Contains(' '))
|
|
|
|
{
|
|
|
|
return results;
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
WebSearch webSearch =
|
2015-02-06 18:13:22 +08:00
|
|
|
WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.FirstSearch.Trim() && o.Enabled);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
|
|
if (webSearch != null)
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
string keyword = query.SecondToEndSearch;
|
2014-03-27 16:56:50 +08:00
|
|
|
string title = keyword;
|
2015-02-28 18:20:06 +08:00
|
|
|
string subtitle = context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
|
2014-03-12 19:18:07 +08:00
|
|
|
if (string.IsNullOrEmpty(keyword))
|
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
title = subtitle;
|
|
|
|
subtitle = null;
|
2014-03-12 19:18:07 +08:00
|
|
|
}
|
2014-07-21 22:27:57 +08:00
|
|
|
context.API.PushResults(query, context.CurrentPluginMetadata, new List<Result>()
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
new Result()
|
2014-02-28 23:21:01 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
Title = title,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 6,
|
|
|
|
IcoPath = webSearch.IconPath,
|
|
|
|
Action = (c) =>
|
|
|
|
{
|
2014-08-25 16:20:08 +08:00
|
|
|
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
|
2014-03-27 16:56:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-28 23:21:01 +08:00
|
|
|
}
|
2015-01-26 20:54:01 +08:00
|
|
|
});
|
2014-03-27 16:56:50 +08:00
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
|
2014-03-27 16:56:50 +08:00
|
|
|
{
|
2015-02-28 18:20:06 +08:00
|
|
|
if (suggestionTimer != null)
|
2014-03-27 16:56:50 +08:00
|
|
|
{
|
2015-02-28 18:20:06 +08:00
|
|
|
suggestionTimer.Dispose();
|
2014-03-27 16:56:50 +08:00
|
|
|
}
|
2015-02-28 18:20:06 +08:00
|
|
|
suggestionTimer = EasyTimer.SetTimeout(() => { QuerySuggestions(keyword, query, subtitle, webSearch); }, 350);
|
2014-03-27 16:56:50 +08:00
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2015-02-28 18:20:06 +08:00
|
|
|
private void QuerySuggestions(string keyword, Query query, string subtitle, WebSearch webSearch)
|
|
|
|
{
|
|
|
|
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, context);
|
|
|
|
if (sugg != null)
|
|
|
|
{
|
|
|
|
var result = sugg.GetSuggestions(keyword);
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
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}", Uri.EscapeDataString(o)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}).ToList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public void Init(PluginInitContext context)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
this.context = context;
|
2014-03-27 17:19:50 +08:00
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
if (WebSearchStorage.Instance.WebSearches == null)
|
|
|
|
WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches();
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
2014-03-28 22:42:28 +08:00
|
|
|
|
2014-03-29 16:13:36 +08:00
|
|
|
#region ISettingProvider Members
|
|
|
|
|
|
|
|
public System.Windows.Controls.Control CreateSettingPanel()
|
|
|
|
{
|
2015-01-07 18:45:55 +08:00
|
|
|
return new WebSearchesSetting(context);
|
2014-03-29 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2015-01-07 18:45:55 +08:00
|
|
|
|
|
|
|
public string GetLanguagesFolder()
|
|
|
|
{
|
|
|
|
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
|
|
|
}
|
2015-01-27 22:59:03 +08:00
|
|
|
|
2015-02-07 20:17:49 +08:00
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
{
|
|
|
|
return context.API.GetTranslation("wox_plugin_websearch_plugin_name");
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
{
|
|
|
|
return context.API.GetTranslation("wox_plugin_websearch_plugin_description");
|
|
|
|
}
|
|
|
|
|
2015-11-03 08:34:27 +08:00
|
|
|
public bool IsInstantQuery(string query) => false;
|
|
|
|
|
|
|
|
public bool IsExclusiveQuery(Query query)
|
2015-01-27 22:59:03 +08:00
|
|
|
{
|
2015-11-03 08:34:27 +08:00
|
|
|
var strings = query.RawQuery.Split(' ');
|
2015-01-27 22:59:03 +08:00
|
|
|
if (strings.Length > 1)
|
|
|
|
{
|
2015-02-05 22:20:42 +08:00
|
|
|
return WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionWord == strings[0] && o.Enabled);
|
2015-01-27 22:59:03 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
}
|