PowerToys/Plugins/Wox.Plugin.WebSearch/WebQueryPlugin.cs

136 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
2015-01-07 18:45:55 +08:00
using System.IO;
using System.Linq;
2015-01-07 18:45:55 +08:00
using System.Reflection;
using Wox.Plugin.WebSearch.SuggestionSources;
namespace Wox.Plugin.WebSearch
{
2015-02-06 18:13:22 +08:00
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery
{
private PluginInitContext context;
private IDisposable suggestionTimer;
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
2015-02-06 18:13:22 +08:00
if (!query.Search.Contains(' '))
{
return results;
}
WebSearch webSearch =
2015-02-06 18:13:22 +08:00
WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.FirstSearch.Trim() && o.Enabled);
if (webSearch != null)
{
string keyword = query.SecondToEndSearch;
string title = keyword;
string subtitle = context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
if (string.IsNullOrEmpty(keyword))
{
title = subtitle;
subtitle = null;
}
context.API.PushResults(query, context.CurrentPluginMetadata, new List<Result>()
{
new Result()
2014-02-28 23:21:01 +08:00
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
2014-02-28 23:21:01 +08:00
}
2015-01-26 20:54:01 +08:00
});
if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
{
if (suggestionTimer != null)
{
suggestionTimer.Dispose();
}
suggestionTimer = EasyTimer.SetTimeout(() => { QuerySuggestions(keyword, query, subtitle, webSearch); }, 350);
}
}
return results;
}
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());
}
}
}
public void Init(PluginInitContext context)
{
this.context = context;
2014-03-27 17:19:50 +08:00
if (WebSearchStorage.Instance.WebSearches == null)
WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches();
}
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-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-11-03 08:34:27 +08:00
var strings = query.RawQuery.Split(' ');
if (strings.Length > 1)
{
2015-02-05 22:20:42 +08:00
return WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionWord == strings[0] && o.Enabled);
}
return false;
}
}
}