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

73 lines
2.5 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
2014-10-22 22:49:34 +08:00
// TODO: periodically refresh the Cache?
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
2014-01-08 23:21:37 +08:00
public void Init(PluginInitContext context)
2014-01-08 23:21:37 +08:00
{
this.context = context;
// Cache all bookmarks
var chromeBookmarks = new ChromeBookmarks();
var mozBookmarks = new FirefoxBookmarks();
//TODO: Let the user select which browser's bookmarks are displayed
// Add Firefox bookmarks
cachedBookmarks.AddRange(mozBookmarks.GetBookmarks());
// Add Chrome bookmarks
cachedBookmarks.AddRange(chromeBookmarks.GetBookmarks());
cachedBookmarks = cachedBookmarks.Distinct().ToList();
}
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);
var returnList = cachedBookmarks;
2014-07-02 23:01:31 +08:00
if (!topResults)
{
// Since we mixed chrome and firefox bookmarks, we should order them again
var fuzzyMatcher = FuzzyMatcher.Create(param);
returnList = cachedBookmarks.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
2014-07-02 23:01:31 +08:00
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
{
2014-07-21 19:48:17 +08:00
context.API.HideApp();
context.API.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;
}
}
}