diff --git a/Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs b/Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs index 23a07a2219..16e5c328cb 100644 --- a/Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs +++ b/Plugins/Wox.Plugin.BrowserBookmark/ChromeBookmarks.cs @@ -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 bookmarks = new List(); - public ChromeBookmarks() + public List GetBookmarks() { bookmarks.Clear(); LoadChromeBookmarks(); - bookmarks = bookmarks.Distinct().ToList(); - } - - public List 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) diff --git a/Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs b/Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs index 71d77257ae..36c38f2fd8 100644 --- a/Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs +++ b/Plugins/Wox.Plugin.BrowserBookmark/FirefoxBookmarks.cs @@ -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 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); - } - /// - /// Searches the places.sqlite db based on the given query and returns the results + /// Searches the places.sqlite db and returns all bookmarks /// - private List GetResults(string query) + public List 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 format return reader.Select(x => new Bookmark() diff --git a/Plugins/Wox.Plugin.BrowserBookmark/Main.cs b/Plugins/Wox.Plugin.BrowserBookmark/Main.cs index 38dacac19e..6a61d5b459 100644 --- a/Plugins/Wox.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Wox.Plugin.BrowserBookmark/Main.cs @@ -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 cachedBookmarks = new List(); 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 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(); - - //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(); }