Skip to content

Commit c74eafb

Browse files
authored
Merge pull request #967 from Flow-Launcher/filewatcher
Auto re-index for Program and Bookmark plugins
2 parents 2c790bf + 2028218 commit c74eafb

File tree

8 files changed

+337
-127
lines changed

8 files changed

+337
-127
lines changed

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

Lines changed: 3 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,7 @@ protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
3133
{
3234
if (!File.Exists(path))
3335
return new();
36+
3437
var bookmarks = new List<Bookmark>();
3538
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
3639
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class FirefoxBookmarkLoader : IBookmarkLoader
1212
private const string queryAllBookmarks = @"SELECT moz_places.url, moz_bookmarks.title
1313
FROM moz_places
1414
INNER JOIN moz_bookmarks ON (
15-
moz_bookmarks.fk NOT NULL AND moz_bookmarks.fk = moz_places.id
15+
moz_bookmarks.fk NOT NULL AND moz_bookmarks.title NOT NULL AND moz_bookmarks.fk = moz_places.id
1616
)
1717
ORDER BY moz_places.visit_count DESC
1818
";
@@ -29,21 +29,21 @@ 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);
35-
using (var dbConnection = new SQLiteConnection(dbPath))
36-
{
37-
// Open connection to the database file and execute the query
38-
dbConnection.Open();
39-
var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader();
40-
41-
// return results in List<Bookmark> format
42-
bookmarkList = reader.Select(
43-
x => new Bookmark(x["title"] is DBNull ? string.Empty : x["title"].ToString(),
44-
x["url"].ToString())
45-
).ToList();
46-
}
37+
using var dbConnection = new SQLiteConnection(dbPath);
38+
// Open connection to the database file and execute the query
39+
dbConnection.Open();
40+
var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader();
41+
42+
// return results in List<Bookmark> format
43+
bookmarkList = reader.Select(
44+
x => new Bookmark(x["title"] is DBNull ? string.Empty : x["title"].ToString(),
45+
x["url"].ToString())
46+
).ToList();
4747

4848
return bookmarkList;
4949
}

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+
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Browser Bookmarks",
55
"Description": "Search your browser bookmarks",
66
"Author": "qianlifeng, Ioannis G.",
7-
"Version": "1.6.3",
7+
"Version": "1.7.0",
88
"Language": "csharp",
99
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1010
"ExecuteFileName": "Flow.Launcher.Plugin.BrowserBookmark.dll",

0 commit comments

Comments
 (0)