cache Firefox bookmarks and support pinyin search

This commit is contained in:
Ioannis G 2014-07-03 21:08:18 +03:00
parent d0d9de8583
commit cfe1e7745f
3 changed files with 23 additions and 58 deletions

View File

@ -2,9 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Wox.Infrastructure;
namespace Wox.Plugin.BrowserBookmark
{
@ -12,32 +10,12 @@ namespace Wox.Plugin.BrowserBookmark
{
private List<Bookmark> bookmarks = new List<Bookmark>();
public ChromeBookmarks()
public List<Bookmark> GetBookmarks()
{
bookmarks.Clear();
LoadChromeBookmarks();
bookmarks = bookmarks.Distinct().ToList();
}
public List<Bookmark> GetBookmarks(string search = null)
{
//TODO: Maybe load bookmarks here instead of pre-loading them at startup?
if (string.IsNullOrEmpty(search)) return bookmarks;
var fuzzyMatcher = FuzzyMatcher.Create(search);
var returnList = bookmarks.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
return returnList;
}
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
{
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;
return false;
return bookmarks;
}
private void ParseChromeBookmarks(String path, string source)

View File

@ -8,39 +8,20 @@ namespace Wox.Plugin.BrowserBookmark
{
public class FirefoxBookmarks
{
private const string queryBookmarks = @"SELECT url, title
FROM moz_places
WHERE id in (
SELECT bm.fk FROM moz_bookmarks bm WHERE bm.fk NOT NULL
)
AND ( url LIKE '%{0}%' OR title LIKE '%{0}%' )
ORDER BY visit_count DESC
LIMIT 20
";
private const string queryTopBookmarks = @"SELECT url, title
private const string queryAllBookmarks = @"SELECT url, title
FROM moz_places
WHERE id in (
SELECT bm.fk FROM moz_bookmarks bm WHERE bm.fk NOT NULL
)
ORDER BY visit_count DESC
LIMIT 20
";
private const string dbPathFormat = "Data Source ={0};Version=3;New=False;Compress=True;";
public List<Bookmark> GetBookmarks(string search = null, bool top = false)
{
// Create the query command for the given case
string query = top ? queryTopBookmarks : string.Format(queryBookmarks, search);
return GetResults(query);
}
/// <summary>
/// Searches the places.sqlite db based on the given query and returns the results
/// Searches the places.sqlite db and returns all bookmarks
/// </summary>
private List<Bookmark> GetResults(string query)
public List<Bookmark> GetBookmarks()
{
// Return empty list if the places.sqlite file cannot be found
if (string.IsNullOrEmpty(PlacesPath) || !File.Exists(PlacesPath))
@ -52,7 +33,7 @@ namespace Wox.Plugin.BrowserBookmark
// Open connection to the database file and execute the query
dbConnection.Open();
var reader = new SQLiteCommand(query, dbConnection).ExecuteReader();
var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader();
// return results in List<Bookmark> format
return reader.Select(x => new Bookmark()

View File

@ -8,12 +8,24 @@ namespace Wox.Plugin.BrowserBookmark
{
private PluginInitContext context;
private ChromeBookmarks chromeBookmarks = new ChromeBookmarks();
private FirefoxBookmarks mozBookmarks = new FirefoxBookmarks();
// TODO: periodically refresh the cache?
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
public void Init(PluginInitContext context)
{
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)
@ -22,20 +34,14 @@ namespace Wox.Plugin.BrowserBookmark
// Should top results be returned? (true if no search parameters have been passed)
var topResults = string.IsNullOrEmpty(param);
var returnList = new List<Bookmark>();
//TODO: Let the user select which browser's bookmarks are displayed
// Add Firefox bookmarks
returnList.AddRange(mozBookmarks.GetBookmarks(param, topResults));
// Add Chrome bookmarks
returnList.AddRange(chromeBookmarks.GetBookmarks(param));
var returnList = cachedBookmarks;
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 = cachedBookmarks.Where(o => MatchProgram(o, fuzzyMatcher)).ToList();
returnList = returnList.OrderByDescending(o => o.Score).ToList();
}