PowerToys/Wox.Plugin.SystemPlugins/WebSearch/WebSearchPlugin.cs

114 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Wox.Infrastructure;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin.SystemPlugins.SuggestionSources;
namespace Wox.Plugin.SystemPlugins
{
2014-03-29 16:13:36 +08:00
public class WebSearchPlugin : BaseSystemPlugin, ISettingProvider
{
private PluginInitContext context;
protected override List<Result> QueryInternal(Query query)
{
List<Result> results = new List<Result>();
WebSearch webSearch =
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled);
if (webSearch != null)
{
string keyword = query.ActionParameters.Count > 0 ? query.GetAllRemainingParameter() : "";
string title = keyword;
string subtitle = "Search " + webSearch.Title;
if (string.IsNullOrEmpty(keyword))
{
title = subtitle;
subtitle = null;
}
2014-07-05 23:10:34 +08:00
context.API.PushResults(query,context.CurrentPluginMetadata, new List<Result>()
{
new Result()
2014-02-28 23:21:01 +08:00
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", keyword));
return true;
}
2014-02-28 23:21:01 +08:00
}
});
if (!string.IsNullOrEmpty(keyword))
{
ISuggestionSource sugg = new Google();
var result = sugg.GetSuggestions(keyword);
if (result != null)
{
2014-07-05 23:10:34 +08:00
context.API.PushResults(query,context.CurrentPluginMetadata, result.Select(o => new Result()
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", o));
return true;
}
}).ToList());
}
}
}
return results;
}
protected override void InitInternal(PluginInitContext context)
{
this.context = context;
2014-03-27 17:19:50 +08:00
if (UserSettingStorage.Instance.WebSearches == null)
UserSettingStorage.Instance.WebSearches = UserSettingStorage.Instance.LoadDefaultWebSearches();
}
public override string ID
{
get { return "565B73353DBF4806919830B9202EE3BF"; }
}
public override string Name
{
get { return "Web Searches"; }
}
public override string IcoPath
{
2014-06-30 21:31:13 +08:00
get { return @"Images\web_search.png"; }
}
public override string Description
{
2014-06-30 21:31:13 +08:00
get { return "Provide the web search ability."; }
}
2014-03-29 16:13:36 +08:00
#region ISettingProvider Members
public System.Windows.Controls.Control CreateSettingPanel()
{
return new WebSearchesSetting();
}
#endregion
}
}