Skip to content

Commit af68cb5

Browse files
committed
detect browserbookmark file change
1 parent 50144ea commit af68cb5

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
2020
var bookmarkPath = Path.Combine(profile, "Bookmarks");
2121
if (!File.Exists(bookmarkPath))
2222
continue;
23+
24+
Main.RegisterBookmarkFile(bookmarkPath);
2325

2426
var source = name + (Path.GetFileName(profile) == "Default" ? "" : $" ({Path.GetFileName(profile)})");
2527
bookmarks.AddRange(LoadBookmarksFromFile(bookmarkPath, source));
@@ -31,6 +33,9 @@ protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
3133
{
3234
if (!File.Exists(path))
3335
return new();
36+
37+
Main.RegisterBookmarkFile(path);
38+
3439
var bookmarks = new List<Bookmark>();
3540
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
3641
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
using Flow.Launcher.Infrastructure;
44
using Flow.Launcher.Plugin.BrowserBookmark.Models;
55
using Flow.Launcher.Plugin.SharedModels;
6+
using Microsoft.AspNetCore.Authentication;
7+
using System.IO;
8+
using System.Threading.Channels;
9+
using System.Threading.Tasks;
610

711
namespace Flow.Launcher.Plugin.BrowserBookmark.Commands
812
{
@@ -17,6 +21,7 @@ internal static MatchResult MatchProgram(Bookmark bookmark, string queryString)
1721
return StringMatcher.FuzzySearch(queryString, bookmark.Url);
1822
}
1923

24+
2025
internal static List<Bookmark> LoadAllBookmarks(Settings setting)
2126
{
2227

Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public List<Bookmark> GetBookmarks()
2929
return new List<Bookmark>();
3030

3131
var bookmarkList = new List<Bookmark>();
32+
33+
Main.RegisterBookmarkFile(PlacesPath);
3234

3335
// create the connection string and init the connection
3436
string dbPath = string.Format(dbPathFormat, PlacesPath);

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,29 @@
99
using Flow.Launcher.Plugin.BrowserBookmark.Models;
1010
using Flow.Launcher.Plugin.BrowserBookmark.Views;
1111
using Flow.Launcher.Plugin.SharedCommands;
12+
using System.IO;
13+
using System.Threading.Channels;
14+
using System.Threading.Tasks;
1215

1316
namespace Flow.Launcher.Plugin.BrowserBookmark
1417
{
15-
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu
18+
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, IDisposable
1619
{
1720
private PluginInitContext context;
1821

1922
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
2023

21-
private Settings _settings { get; set;}
24+
private Settings _settings { get; set; }
2225

2326
public void Init(PluginInitContext context)
2427
{
2528
this.context = context;
26-
29+
2730
_settings = context.API.LoadSettingJsonStorage<Settings>();
2831

2932
cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
33+
34+
_ = MonitorRefreshQueue();
3035
}
3136

3237
public List<Result> Query(Query query)
@@ -52,7 +57,10 @@ public List<Result> Query(Query query)
5257

5358
return true;
5459
},
55-
ContextData = new BookmarkAttributes { Url = c.Url }
60+
ContextData = new BookmarkAttributes
61+
{
62+
Url = c.Url
63+
}
5664
}).Where(r => r.Score > 0);
5765
return returnList.ToList();
5866
}
@@ -69,11 +77,64 @@ public List<Result> Query(Query query)
6977
context.API.OpenUrl(c.Url);
7078
return true;
7179
},
72-
ContextData = new BookmarkAttributes { Url = c.Url }
80+
ContextData = new BookmarkAttributes
81+
{
82+
Url = c.Url
83+
}
7384
}).ToList();
7485
}
7586
}
7687

88+
89+
private static Channel<byte> refreshQueue = Channel.CreateBounded<byte>(1);
90+
91+
private async Task MonitorRefreshQueue()
92+
{
93+
var reader = refreshQueue.Reader;
94+
while (await reader.WaitToReadAsync())
95+
{
96+
await Task.Delay(2000);
97+
if (reader.TryRead(out _))
98+
{
99+
ReloadData();
100+
}
101+
}
102+
}
103+
104+
private static readonly List<FileSystemWatcher> Watchers = new();
105+
106+
internal static void RegisterBookmarkFile(string path)
107+
{
108+
var directory = Path.GetDirectoryName(path);
109+
if (!Directory.Exists(directory))
110+
return;
111+
var watcher = new FileSystemWatcher(directory!);
112+
if (File.Exists(path))
113+
{
114+
var fileName = Path.GetFileName(path);
115+
watcher.Filter = fileName;
116+
}
117+
118+
watcher.NotifyFilter = NotifyFilters.FileName |
119+
NotifyFilters.LastAccess |
120+
NotifyFilters.LastWrite |
121+
NotifyFilters.Size;
122+
123+
watcher.Changed += static (_, _) =>
124+
{
125+
refreshQueue.Writer.TryWrite(default);
126+
};
127+
128+
watcher.Renamed += static (_, _) =>
129+
{
130+
refreshQueue.Writer.TryWrite(default);
131+
};
132+
133+
watcher.EnableRaisingEvents = true;
134+
135+
Watchers.Add(watcher);
136+
}
137+
77138
public void ReloadData()
78139
{
79140
cachedBookmarks.Clear();
@@ -98,7 +159,8 @@ public Control CreateSettingPanel()
98159

99160
public List<Result> LoadContextMenus(Result selectedResult)
100161
{
101-
return new List<Result>() {
162+
return new List<Result>()
163+
{
102164
new Result
103165
{
104166
Title = context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"),
@@ -114,20 +176,28 @@ public List<Result> LoadContextMenus(Result selectedResult)
114176
catch (Exception e)
115177
{
116178
var message = "Failed to set url in clipboard";
117-
Log.Exception("Main",message, e, "LoadContextMenus");
179+
Log.Exception("Main", message, e, "LoadContextMenus");
118180

119181
context.API.ShowMsg(message);
120182

121183
return false;
122184
}
123185
},
124186
IcoPath = "Images\\copylink.png"
125-
}};
187+
}
188+
};
126189
}
127190

128191
internal class BookmarkAttributes
129192
{
130193
internal string Url { get; set; }
131194
}
195+
public void Dispose()
196+
{
197+
foreach (var watcher in Watchers)
198+
{
199+
watcher.Dispose();
200+
}
201+
}
132202
}
133-
}
203+
}

0 commit comments

Comments
 (0)