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-01-05 22:41:17 +08:00
|
|
|
using Wox.Core.UserSettings;
|
2015-02-05 22:20:42 +08:00
|
|
|
using Wox.Plugin.Features;
|
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-02-06 18:13:22 +08:00
|
|
|
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
private PluginInitContext context;
|
|
|
|
|
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-02-06 18:13:22 +08:00
|
|
|
if (!query.Search.Contains(' '))
|
|
|
|
{
|
|
|
|
return results;
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
WebSearch webSearch =
|
2015-02-06 18:13:22 +08:00
|
|
|
WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.FirstSearch.Trim() && o.Enabled);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
|
|
if (webSearch != null)
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
string keyword = query.SecondToEndSearch;
|
2014-03-27 16:56:50 +08:00
|
|
|
string title = keyword;
|
|
|
|
string subtitle = "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;
|
|
|
|
subtitle = null;
|
2014-03-12 19:18:07 +08:00
|
|
|
}
|
2014-07-21 22:27:57 +08:00
|
|
|
context.API.PushResults(query, context.CurrentPluginMetadata, new List<Result>()
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
new Result()
|
2014-02-28 23:21:01 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
Title = title,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 6,
|
|
|
|
IcoPath = webSearch.IconPath,
|
|
|
|
Action = (c) =>
|
|
|
|
{
|
2014-08-25 16:20:08 +08:00
|
|
|
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
|
2014-03-27 16:56:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-28 23:21:01 +08:00
|
|
|
}
|
2015-01-26 20:54:01 +08:00
|
|
|
});
|
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
|
|
|
{
|
2014-07-21 22:27:57 +08:00
|
|
|
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(
|
2015-01-26 22:50:38 +08:00
|
|
|
WebSearchStorage.Instance.WebSearchSuggestionSource);
|
2014-07-21 22:27:57 +08:00
|
|
|
if (sugg != null)
|
2014-03-27 16:56:50 +08:00
|
|
|
{
|
2014-07-21 22:27:57 +08:00
|
|
|
var result = sugg.GetSuggestions(keyword);
|
|
|
|
if (result != null)
|
2014-03-27 16:56:50 +08:00
|
|
|
{
|
2014-07-21 22:27:57 +08:00
|
|
|
context.API.PushResults(query, context.CurrentPluginMetadata,
|
|
|
|
result.Select(o => new Result()
|
|
|
|
{
|
|
|
|
Title = o,
|
|
|
|
SubTitle = subtitle,
|
|
|
|
Score = 5,
|
|
|
|
IcoPath = webSearch.IconPath,
|
|
|
|
Action = (c) =>
|
|
|
|
{
|
2014-08-25 16:20:08 +08:00
|
|
|
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
|
2014-07-21 22:27:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}).ToList());
|
|
|
|
}
|
2014-03-27 16:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public void Init(PluginInitContext context)
|
2014-01-29 22:44:57 +08:00
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
this.context = context;
|
2014-03-27 17:19:50 +08:00
|
|
|
|
2015-01-26 22:50:38 +08:00
|
|
|
if (WebSearchStorage.Instance.WebSearches == null)
|
|
|
|
WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches();
|
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
|
|
|
|
|
|
|
|
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-01-27 22:59:03 +08:00
|
|
|
|
2015-02-05 22:20:42 +08:00
|
|
|
public bool IsInstantQuery(string query)
|
2015-01-27 22:59:03 +08:00
|
|
|
{
|
|
|
|
var strings = query.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);
|
2015-01-27 22:59:03 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-05 22:20:42 +08:00
|
|
|
|
|
|
|
public bool IsExclusiveQuery(Query query)
|
|
|
|
{
|
|
|
|
return IsInstantQuery(query.RawQuery);
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
}
|
|
|
|
}
|