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-11-08 10:27:08 +08:00
|
|
|
using System.Windows.Controls;
|
2015-11-09 11:20:02 +08:00
|
|
|
using Wox.Plugin.WebSearch.Annotations;
|
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-11-09 11:20:02 +08:00
|
|
|
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
public PluginInitContext Context { get; private set; }
|
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-01-26 22:50:38 +08:00
|
|
|
WebSearch webSearch =
|
2015-11-05 06:49:40 +08:00
|
|
|
WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
|
|
if (webSearch != null)
|
|
|
|
{
|
2015-11-08 10:27:08 +08:00
|
|
|
string keyword = query.Search;
|
2014-03-27 16:56:50 +08:00
|
|
|
string title = keyword;
|
2015-11-09 11:20:02 +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;
|
2015-11-08 10:27:08 +08:00
|
|
|
subtitle = string.Empty;
|
2014-03-12 19:18:07 +08:00
|
|
|
}
|
2015-11-08 10:27:08 +08:00
|
|
|
var result = new Result
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2015-11-08 10:27:08 +08:00
|
|
|
Title = title,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 6,
|
|
|
|
IcoPath = webSearch.IconPath,
|
|
|
|
Action = c =>
|
2014-02-28 23:21:01 +08:00
|
|
|
{
|
2015-11-08 10:27:08 +08:00
|
|
|
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword ?? string.Empty)));
|
|
|
|
return true;
|
2014-02-28 23:21:01 +08:00
|
|
|
}
|
2015-11-08 10:27:08 +08:00
|
|
|
};
|
|
|
|
results.Add(result);
|
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-11-08 10:27:08 +08:00
|
|
|
// todo use Task.Wait when .net upgraded
|
|
|
|
results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch));
|
2014-03-27 16:56:50 +08:00
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2015-11-08 10:27:08 +08:00
|
|
|
private IEnumerable<Result> ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch)
|
2015-02-28 18:20:06 +08:00
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, Context);
|
2015-11-08 10:27:08 +08:00
|
|
|
var suggestions = sugg?.GetSuggestions(keyword);
|
|
|
|
if (suggestions != null)
|
2015-02-28 18:20:06 +08:00
|
|
|
{
|
2015-11-08 10:27:08 +08:00
|
|
|
var resultsFromSuggestion = suggestions.Select(o => new Result
|
2015-02-28 18:20:06 +08:00
|
|
|
{
|
2015-11-08 10:27:08 +08:00
|
|
|
Title = o,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 5,
|
|
|
|
IcoPath = webSearch.IconPath,
|
|
|
|
Action = c =>
|
|
|
|
{
|
|
|
|
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return resultsFromSuggestion;
|
2015-02-28 18:20:06 +08:00
|
|
|
}
|
2015-11-08 10:27:08 +08:00
|
|
|
return new List<Result>();
|
2015-02-28 18:20:06 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public void Init(PluginInitContext context)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2016-01-04 13:00:16 +08:00
|
|
|
Context = context;
|
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
|
|
|
|
|
2015-11-08 10:27:08 +08:00
|
|
|
public Control CreateSettingPanel()
|
2014-03-29 16:13:36 +08:00
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
return new WebSearchesSetting(this);
|
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()
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
return Context.API.GetTranslation("wox_plugin_websearch_plugin_name");
|
2015-02-07 20:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
{
|
2015-11-09 11:20:02 +08:00
|
|
|
return Context.API.GetTranslation("wox_plugin_websearch_plugin_description");
|
2015-02-07 20:17:49 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 08:34:27 +08:00
|
|
|
public bool IsInstantQuery(string query) => false;
|
|
|
|
|
2015-11-09 11:20:02 +08:00
|
|
|
[NotifyPropertyChangedInvocator]
|
|
|
|
public void NotifyActionKeywordsUpdated(string oldActionKeywords, string newActionKeywords)
|
|
|
|
{
|
|
|
|
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
|
|
|
|
{
|
|
|
|
OldActionKeyword = oldActionKeywords,
|
|
|
|
NewActionKeyword = newActionKeywords
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[NotifyPropertyChangedInvocator]
|
|
|
|
public void NotifyActionKeywordsAdded(string newActionKeywords)
|
|
|
|
{
|
|
|
|
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
|
|
|
|
{
|
|
|
|
NewActionKeyword = newActionKeywords
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public event ActionKeywordsChangedEventHandler ActionKeywordsChanged;
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
}
|