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;
|
2016-05-08 02:16:13 +08:00
|
|
|
using System.IO;
|
2014-01-29 22:44:57 +08:00
|
|
|
using System.Linq;
|
2016-05-05 23:08:44 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2015-11-08 10:27:08 +08:00
|
|
|
using System.Windows.Controls;
|
2016-05-08 02:16:13 +08:00
|
|
|
using Wox.Infrastructure;
|
2016-04-21 08:53:21 +08:00
|
|
|
using Wox.Infrastructure.Storage;
|
2019-08-06 19:49:51 +08:00
|
|
|
using Wox.Plugin.SharedCommands;
|
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
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
private PluginInitContext _context;
|
2014-03-27 16:56:50 +08:00
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
private readonly Settings _settings;
|
|
|
|
private readonly SettingsViewModel _viewModel;
|
2016-05-05 23:08:44 +08:00
|
|
|
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;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
2019-08-02 10:32:02 +08:00
|
|
|
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
|
2019-08-01 18:55:13 +08:00
|
|
|
|
2016-05-03 05:37:01 +08:00
|
|
|
public void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
_viewModel.Save();
|
2016-04-21 08:53:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public List<Result> Query(Query query)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2019-08-04 19:38:36 +08:00
|
|
|
var searchSourceList = new List<SearchSource>();
|
|
|
|
var results = new List<Result>();
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
_updateSource?.Cancel();
|
|
|
|
_updateSource = new CancellationTokenSource();
|
|
|
|
_updateToken = _updateSource.Token;
|
2019-08-01 18:55:13 +08:00
|
|
|
|
2019-08-02 10:32:02 +08:00
|
|
|
_settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
|
2019-08-01 18:55:13 +08:00
|
|
|
&& o.Enabled)
|
|
|
|
.ToList()
|
|
|
|
.ForEach(x => searchSourceList.Add(x));
|
2016-05-05 23:08:44 +08:00
|
|
|
|
2019-08-01 18:55:13 +08:00
|
|
|
if (searchSourceList.Any())
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2019-08-01 18:55:13 +08:00
|
|
|
foreach (SearchSource searchSource in searchSourceList)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2019-10-05 16:21:09 +08:00
|
|
|
string keyword = string.Empty;
|
|
|
|
keyword = searchSource.ActionKeyword == SearchSourceGlobalPluginWildCardSign ? query.ToString() : query.Search;
|
|
|
|
var title = keyword;
|
|
|
|
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title;
|
|
|
|
|
2019-08-01 18:55:13 +08:00
|
|
|
if (string.IsNullOrEmpty(keyword))
|
2014-02-28 23:21:01 +08:00
|
|
|
{
|
2019-08-01 18:55:13 +08:00
|
|
|
var result = new Result
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2019-08-01 18:55:13 +08:00
|
|
|
Title = subtitle,
|
|
|
|
SubTitle = string.Empty,
|
|
|
|
IcoPath = searchSource.IconPath
|
|
|
|
};
|
2019-08-04 19:38:36 +08:00
|
|
|
results.Add(result);
|
2019-08-01 18:55:13 +08:00
|
|
|
}
|
|
|
|
else
|
2019-08-19 19:14:29 +08:00
|
|
|
{
|
2019-08-01 18:55:13 +08:00
|
|
|
var result = new Result
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2019-08-01 18:55:13 +08:00
|
|
|
Title = title,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 6,
|
|
|
|
IcoPath = searchSource.IconPath,
|
|
|
|
Action = c =>
|
|
|
|
{
|
2019-11-10 16:12:43 +08:00
|
|
|
if (_settings.OpenInNewBrowser)
|
|
|
|
{
|
2019-11-11 19:04:01 +08:00
|
|
|
searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)).NewBrowserWindow(_settings.BrowserPath);
|
2019-11-10 16:12:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-11 19:04:01 +08:00
|
|
|
searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)).NewTabInBrowser(_settings.BrowserPath);
|
2019-11-10 16:12:43 +08:00
|
|
|
}
|
2019-08-04 09:42:08 +08:00
|
|
|
|
2019-08-01 18:55:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2019-09-22 16:25:53 +08:00
|
|
|
|
2019-08-01 18:55:13 +08:00
|
|
|
results.Add(result);
|
2019-09-22 16:25:53 +08:00
|
|
|
ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
|
|
|
|
{
|
|
|
|
Results = results,
|
|
|
|
Query = query
|
|
|
|
});
|
2019-08-19 19:14:29 +08:00
|
|
|
|
2019-08-04 19:38:36 +08:00
|
|
|
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
|
2019-08-01 18:55:13 +08:00
|
|
|
}
|
2016-05-05 23:08:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-01 18:55:13 +08:00
|
|
|
|
2019-08-04 19:38:36 +08:00
|
|
|
return results;
|
2016-05-05 23:08:44 +08:00
|
|
|
}
|
2014-03-27 16:56:50 +08:00
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
|
|
|
|
SearchSource searchSource, Query query)
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
if (_settings.EnableSuggestion)
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2016-05-06 03:26:19 +08:00
|
|
|
const int waittime = 300;
|
2016-05-09 09:32:47 +08:00
|
|
|
var task = Task.Run(async () =>
|
2014-03-27 16:56:50 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
var suggestions = await Suggestions(keyword, subtitle, searchSource);
|
2016-05-09 09:32:47 +08:00
|
|
|
results.AddRange(suggestions);
|
2016-05-06 03:26:19 +08:00
|
|
|
}, _updateToken);
|
|
|
|
|
|
|
|
if (!task.Wait(waittime))
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2016-05-06 03:26:19 +08:00
|
|
|
task.ContinueWith(_ => ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs
|
2016-05-05 23:08:44 +08:00
|
|
|
{
|
2016-05-06 03:26:19 +08:00
|
|
|
Results = results,
|
|
|
|
Query = query
|
|
|
|
}), _updateToken);
|
2016-05-05 23:08:44 +08:00
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, SearchSource searchSource)
|
2015-02-28 18:20:06 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
var source = _settings.SelectedSuggestion;
|
2016-05-09 09:32:47 +08:00
|
|
|
if (source != null)
|
2015-02-28 18:20:06 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
var suggestions = await source.Suggestions(keyword);
|
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,
|
2016-06-21 07:14:32 +08:00
|
|
|
IcoPath = searchSource.IconPath,
|
2015-11-08 10:27:08 +08:00
|
|
|
Action = c =>
|
|
|
|
{
|
2019-11-10 16:12:43 +08:00
|
|
|
if (_settings.OpenInNewBrowser)
|
|
|
|
{
|
2019-11-11 19:04:01 +08:00
|
|
|
searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewBrowserWindow(_settings.BrowserPath);
|
2019-11-10 16:12:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-11 19:04:01 +08:00
|
|
|
searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewTabInBrowser(_settings.BrowserPath);
|
2019-11-10 16:12:43 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 10:27:08 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
public Main()
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
_viewModel = new SettingsViewModel();
|
|
|
|
_settings = _viewModel.Settings;
|
|
|
|
}
|
2016-05-08 02:16:13 +08:00
|
|
|
|
2016-06-21 07:14:32 +08:00
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
{
|
2017-02-07 08:20:55 +08:00
|
|
|
_context = context;
|
2017-02-07 08:21:39 +08:00
|
|
|
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-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
|
|
|
{
|
2016-06-21 07:14:32 +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()
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
return _context.API.GetTranslation("wox_plugin_websearch_plugin_name");
|
2015-02-07 20:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
{
|
2016-06-21 07:14:32 +08:00
|
|
|
return _context.API.GetTranslation("wox_plugin_websearch_plugin_description");
|
2015-11-09 11:20:02 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
public event ResultUpdatedEventHandler ResultsUpdated;
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
2019-08-02 10:32:02 +08:00
|
|
|
}
|