2014-01-29 22:44:57 +08:00
|
|
|
|
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;
|
2014-03-27 16:56:50 +08:00
|
|
|
|
using Wox.Plugin.System.SuggestionSources;
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Plugin.System
|
|
|
|
|
{
|
|
|
|
|
public class WebSearchPlugin : BaseSystemPlugin
|
|
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
|
private PluginInitContext context;
|
|
|
|
|
|
2014-01-29 22:44:57 +08:00
|
|
|
|
protected override List<Result> QueryInternal(Query query)
|
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
|
|
|
|
if (string.IsNullOrEmpty(query.ActionName)) return results;
|
|
|
|
|
|
|
|
|
|
WebSearch webSearch =
|
2014-03-23 16:17:41 +08:00
|
|
|
|
UserSettingStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionWord == query.ActionName && o.Enabled);
|
2014-01-29 22:44:57 +08:00
|
|
|
|
|
|
|
|
|
if (webSearch != null)
|
|
|
|
|
{
|
2014-03-12 19:18:07 +08:00
|
|
|
|
string keyword = query.ActionParameters.Count > 0 ? query.GetAllRemainingParameter() : "";
|
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-03-27 16:56:50 +08:00
|
|
|
|
context.PushResults(query, 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) =>
|
|
|
|
|
{
|
|
|
|
|
Process.Start(webSearch.Url.Replace("{q}", keyword));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-02-28 23:21:01 +08:00
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
});
|
2014-03-27 16:56:50 +08:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(keyword))
|
|
|
|
|
{
|
|
|
|
|
ISuggestionSource sugg = new Google();
|
|
|
|
|
var result = sugg.GetSuggestions(keyword);
|
|
|
|
|
if (result != null)
|
|
|
|
|
{
|
|
|
|
|
context.PushResults(query, 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-29 22:44:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void InitInternal(PluginInitContext context)
|
|
|
|
|
{
|
2014-03-27 16:56:50 +08:00
|
|
|
|
this.context = context;
|
2014-03-27 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (UserSettingStorage.Instance.WebSearches == null)
|
|
|
|
|
UserSettingStorage.Instance.WebSearches = UserSettingStorage.Instance.LoadDefaultWebSearches();
|
2014-01-29 22:44:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|