From af68cb5093b38c15b39e69cc1cdeebaa4173cd04 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 8 Jan 2022 16:58:12 -0600 Subject: [PATCH] detect browserbookmark file change --- .../ChromiumBookmarkLoader.cs | 5 ++ .../Commands/BookmarkLoader.cs | 5 ++ .../FirefoxBookmarkLoader.cs | 2 + .../Main.cs | 88 +++++++++++++++++-- 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs index d7b412392..c32f60af3 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs @@ -20,6 +20,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark var bookmarkPath = Path.Combine(profile, "Bookmarks"); if (!File.Exists(bookmarkPath)) continue; + + Main.RegisterBookmarkFile(bookmarkPath); var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})"); bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source)); @@ -31,6 +33,9 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { if (!File.Exists(path)) return new(); + + Main.RegisterBookmarkFile(path); + var bookmarks = new List(); using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path)); if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement)) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs index 40d10b26f..491c5c915 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs @@ -3,6 +3,10 @@ using System.Linq; using Flow.Launcher.Infrastructure; using Flow.Launcher.Plugin.BrowserBookmark.Models; using Flow.Launcher.Plugin.SharedModels; +using Microsoft.AspNetCore.Authentication; +using System.IO; +using System.Threading.Channels; +using System.Threading.Tasks; namespace Flow.Launcher.Plugin.BrowserBookmark.Commands { @@ -17,6 +21,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Commands return StringMatcher.FuzzySearch(queryString, bookmark.Url); } + internal static List LoadAllBookmarks(Settings setting) { diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs index 4d5135b30..9d29b9c41 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs @@ -29,6 +29,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark return new List(); var bookmarkList = new List(); + + Main.RegisterBookmarkFile(PlacesPath); // create the connection string and init the connection string dbPath = string.Format(dbPathFormat, PlacesPath); diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 1d58f84d8..f9127cd3c 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -9,24 +9,29 @@ using Flow.Launcher.Plugin.BrowserBookmark.Commands; using Flow.Launcher.Plugin.BrowserBookmark.Models; using Flow.Launcher.Plugin.BrowserBookmark.Views; using Flow.Launcher.Plugin.SharedCommands; +using System.IO; +using System.Threading.Channels; +using System.Threading.Tasks; namespace Flow.Launcher.Plugin.BrowserBookmark { - public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu + public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, IDisposable { private PluginInitContext context; private List cachedBookmarks = new List(); - private Settings _settings { get; set;} + private Settings _settings { get; set; } public void Init(PluginInitContext context) { this.context = context; - + _settings = context.API.LoadSettingJsonStorage(); cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + + _ = MonitorRefreshQueue(); } public List Query(Query query) @@ -52,7 +57,10 @@ namespace Flow.Launcher.Plugin.BrowserBookmark return true; }, - ContextData = new BookmarkAttributes { Url = c.Url } + ContextData = new BookmarkAttributes + { + Url = c.Url + } }).Where(r => r.Score > 0); return returnList.ToList(); } @@ -69,11 +77,64 @@ namespace Flow.Launcher.Plugin.BrowserBookmark context.API.OpenUrl(c.Url); return true; }, - ContextData = new BookmarkAttributes { Url = c.Url } + ContextData = new BookmarkAttributes + { + Url = c.Url + } }).ToList(); } } + + private static Channel refreshQueue = Channel.CreateBounded(1); + + private async Task MonitorRefreshQueue() + { + var reader = refreshQueue.Reader; + while (await reader.WaitToReadAsync()) + { + await Task.Delay(2000); + if (reader.TryRead(out _)) + { + ReloadData(); + } + } + } + + private static readonly List Watchers = new(); + + internal static void RegisterBookmarkFile(string path) + { + var directory = Path.GetDirectoryName(path); + if (!Directory.Exists(directory)) + return; + var watcher = new FileSystemWatcher(directory!); + if (File.Exists(path)) + { + var fileName = Path.GetFileName(path); + watcher.Filter = fileName; + } + + watcher.NotifyFilter = NotifyFilters.FileName | + NotifyFilters.LastAccess | + NotifyFilters.LastWrite | + NotifyFilters.Size; + + watcher.Changed += static (_, _) => + { + refreshQueue.Writer.TryWrite(default); + }; + + watcher.Renamed += static (_, _) => + { + refreshQueue.Writer.TryWrite(default); + }; + + watcher.EnableRaisingEvents = true; + + Watchers.Add(watcher); + } + public void ReloadData() { cachedBookmarks.Clear(); @@ -98,7 +159,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark public List LoadContextMenus(Result selectedResult) { - return new List() { + return new List() + { new Result { Title = context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"), @@ -114,7 +176,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark catch (Exception e) { var message = "Failed to set url in clipboard"; - Log.Exception("Main",message, e, "LoadContextMenus"); + Log.Exception("Main", message, e, "LoadContextMenus"); context.API.ShowMsg(message); @@ -122,12 +184,20 @@ namespace Flow.Launcher.Plugin.BrowserBookmark } }, IcoPath = "Images\\copylink.png" - }}; + } + }; } internal class BookmarkAttributes { internal string Url { get; set; } } + public void Dispose() + { + foreach (var watcher in Watchers) + { + watcher.Dispose(); + } + } } -} +} \ No newline at end of file