2014-07-02 23:01:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-01-08 23:21:37 +08:00
|
|
|
|
using System.Linq;
|
2014-02-06 22:22:02 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-01-08 23:21:37 +08:00
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
namespace Wox.Plugin.BrowserBookmark
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public class Main : IPlugin
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-21 03:53:18 +08:00
|
|
|
|
private PluginInitContext context;
|
2014-07-02 23:01:31 +08:00
|
|
|
|
|
2014-07-04 02:08:18 +08:00
|
|
|
|
// TODO: periodically refresh the cache?
|
|
|
|
|
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
|
2014-01-08 23:21:37 +08:00
|
|
|
|
|
2014-06-30 18:05:42 +08:00
|
|
|
|
public void Init(PluginInitContext context)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-06-30 18:05:42 +08:00
|
|
|
|
this.context = context;
|
2014-07-04 02:08:18 +08:00
|
|
|
|
|
|
|
|
|
// 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();
|
2014-06-30 18:05:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-04 02:08:18 +08:00
|
|
|
|
|
|
|
|
|
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);
|
2014-07-04 02:08:18 +08:00
|
|
|
|
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,
|
2014-06-30 18:05:42 +08:00
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
2014-01-08 23:21:37 +08:00
|
|
|
|
Score = 5,
|
2014-03-21 03:53:18 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
2014-06-30 18:05:42 +08:00
|
|
|
|
|
2014-03-25 13:36:33 +08:00
|
|
|
|
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
|
2014-01-08 23:21:37 +08:00
|
|
|
|
{
|
2014-03-25 13:36:33 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|