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

173 lines
6.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
2016-05-08 02:16:13 +08:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
2016-05-08 02:16:13 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
2019-08-06 19:49:51 +08:00
using Wox.Plugin.SharedCommands;
namespace Wox.Plugin.WebSearch
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
{
private PluginInitContext _context;
private readonly Settings _settings;
private readonly SettingsViewModel _viewModel;
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
2016-05-08 02:16:13 +08:00
public const string Images = "Images";
2017-02-07 06:15:28 +08:00
public static string ImagesDirectory;
2019-08-02 10:32:02 +08:00
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
public void Save()
{
_viewModel.Save();
}
public List<Result> Query(Query query)
{
2019-08-04 19:38:36 +08:00
var searchSourceList = new List<SearchSource>();
var results = new List<Result>();
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
2019-08-02 10:32:02 +08:00
_settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
&& o.Enabled)
.ToList()
.ForEach(x => searchSourceList.Add(x));
if (searchSourceList.Any())
{
foreach (SearchSource searchSource in searchSourceList)
{
string keyword = query.Search;
string title = keyword;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " +
searchSource.Title;
if (string.IsNullOrEmpty(keyword))
{
var result = new Result
{
Title = subtitle,
SubTitle = string.Empty,
IcoPath = searchSource.IconPath
};
2019-08-04 19:38:36 +08:00
results.Add(result);
}
else
2014-02-28 23:21:01 +08:00
{
var result = new Result
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = searchSource.IconPath,
Action = c =>
{
searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)).NewBrowserWindow("");
return true;
}
};
results.Add(result);
2019-08-04 19:38:36 +08:00
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
}
}
}
2019-08-04 19:38:36 +08:00
return results;
}
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
SearchSource searchSource, Query query)
{
if (_settings.EnableSuggestion)
{
const int waittime = 300;
var task = Task.Run(async () =>
{
var suggestions = await Suggestions(keyword, subtitle, searchSource);
results.AddRange(suggestions);
}, _updateToken);
if (!task.Wait(waittime))
{
task.ContinueWith(_ => ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
{
Results = results,
Query = query
}), _updateToken);
}
}
}
private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, SearchSource searchSource)
{
var source = _settings.SelectedSuggestion;
if (source != null)
{
var suggestions = await source.Suggestions(keyword);
var resultsFromSuggestion = suggestions.Select(o => new Result
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = searchSource.IconPath,
Action = c =>
{
searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewBrowserWindow("");
return true;
}
});
return resultsFromSuggestion;
}
return new List<Result>();
}
public Main()
{
_viewModel = new SettingsViewModel();
_settings = _viewModel.Settings;
}
2016-05-08 02:16:13 +08:00
public void Init(PluginInitContext context)
{
_context = context;
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
2016-05-08 02:16:13 +08:00
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
2017-02-07 06:15:28 +08:00
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
2016-05-08 02:16:13 +08:00
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);
}
2014-03-29 16:13:36 +08:00
#region ISettingProvider Members
public Control CreateSettingPanel()
2014-03-29 16:13:36 +08:00
{
return new SettingsControl(_context, _viewModel);
2014-03-29 16:13:36 +08:00
}
#endregion
2015-01-07 18:45:55 +08:00
2015-02-07 20:17:49 +08:00
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("wox_plugin_websearch_plugin_name");
2015-02-07 20:17:49 +08:00
}
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("wox_plugin_websearch_plugin_description");
}
public event ResultUpdatedEventHandler ResultsUpdated;
}
2019-08-02 10:32:02 +08:00
}