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

67 lines
2.3 KiB
C#
Raw Normal View History

2014-07-02 23:01:31 +08:00
using System.Collections.Generic;
2014-01-08 23:21:37 +08:00
using System.Linq;
using Wox.Infrastructure;
2014-01-08 23:21:37 +08:00
namespace Wox.Plugin.BrowserBookmark
2014-01-08 23:21:37 +08:00
{
public class Main : IPlugin
2014-01-08 23:21:37 +08:00
{
private PluginInitContext context;
2014-07-02 23:01:31 +08:00
private ChromeBookmarks chromeBookmarks = new ChromeBookmarks();
private FirefoxBookmarks mozBookmarks = new FirefoxBookmarks();
2014-01-08 23:21:37 +08:00
public void Init(PluginInitContext context)
2014-01-08 23:21:37 +08:00
{
this.context = context;
}
public List<Result> Query(Query query)
{
2014-07-02 23:01:31 +08:00
string param = query.GetAllRemainingParameter().TrimStart();
// Should top results be returned? (true if no search parameters have been passed)
var topResults = string.IsNullOrEmpty(param);
2014-07-02 23:01:31 +08:00
var returnList = new List<Bookmark>();
2014-01-08 23:21:37 +08:00
//TODO: Let the user select which browser's bookmarks are displayed
2014-07-02 23:01:31 +08:00
// Add Firefox bookmarks
returnList.AddRange(mozBookmarks.GetBookmarks(param, topResults));
// Add Chrome bookmarks
returnList.AddRange(chromeBookmarks.GetBookmarks(param));
if (!topResults)
{
// Since we mixed chrome and firefox bookmarks, we should order them again
var fuzzyMatcher = FuzzyMatcher.Create(param);
returnList = returnList.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
returnList = returnList.OrderByDescending(o => o.Score).ToList();
}
2014-01-08 23:21:37 +08:00
return returnList.Select(c => new Result()
{
Title = c.Name,
SubTitle = "Bookmark: " + c.Url,
IcoPath = @"Images\bookmark.png",
2014-01-08 23:21:37 +08:00
Score = 5,
Action = (e) =>
2014-01-08 23:21:37 +08:00
{
context.HideApp();
context.ShellRun(c.Url);
2014-02-28 23:21:01 +08:00
return true;
2014-01-08 23:21:37 +08:00
}
}).ToList();
}
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
2014-01-08 23:21:37 +08:00
{
if ((bookmark.Score = matcher.Evaluate(bookmark.Name).Score) > 0) return true;
if ((bookmark.Score = matcher.Evaluate(bookmark.PinyinName).Score) > 0) return true;
if ((bookmark.Score = matcher.Evaluate(bookmark.Url).Score / 10) > 0) return true;
2014-01-08 23:21:37 +08:00
return false;
}
}
}