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

171 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;
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;
private readonly string SystemPluginSearchSourceWildCardSign = "*";
public void Save()
{
_viewModel.Save();
}
public List<Result> Query(Query query)
{
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
var searchSourceList = new List<SearchSource>();
_settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || o.ActionKeyword == SystemPluginSearchSourceWildCardSign)
&& 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
};
return new List<Result> {result};
}
else
2014-02-28 23:21:01 +08:00
{
var results = new List<Result>();
var result = new Result
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
};
results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
return results;
}
}
}
return new List<Result>();
}
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 =>
{
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
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;
}
}