Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
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));
Expand All @@ -31,6 +33,7 @@ protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
{
if (!File.Exists(path))
return new();

var bookmarks = new List<Bookmark>();
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FirefoxBookmarkLoader : IBookmarkLoader
private const string queryAllBookmarks = @"SELECT moz_places.url, moz_bookmarks.title
FROM moz_places
INNER JOIN moz_bookmarks ON (
moz_bookmarks.fk NOT NULL AND moz_bookmarks.fk = moz_places.id
moz_bookmarks.fk NOT NULL AND moz_bookmarks.title NOT NULL AND moz_bookmarks.fk = moz_places.id
)
ORDER BY moz_places.visit_count DESC
";
Expand All @@ -29,21 +29,21 @@ public List<Bookmark> GetBookmarks()
return new List<Bookmark>();

var bookmarkList = new List<Bookmark>();

Main.RegisterBookmarkFile(PlacesPath);

// create the connection string and init the connection
string dbPath = string.Format(dbPathFormat, PlacesPath);
using (var dbConnection = new SQLiteConnection(dbPath))
{
// Open connection to the database file and execute the query
dbConnection.Open();
var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader();

// return results in List<Bookmark> format
bookmarkList = reader.Select(
x => new Bookmark(x["title"] is DBNull ? string.Empty : x["title"].ToString(),
x["url"].ToString())
).ToList();
}
using var dbConnection = new SQLiteConnection(dbPath);
// Open connection to the database file and execute the query
dbConnection.Open();
var reader = new SQLiteCommand(queryAllBookmarks, dbConnection).ExecuteReader();

// return results in List<Bookmark> format
bookmarkList = reader.Select(
x => new Bookmark(x["title"] is DBNull ? string.Empty : x["title"].ToString(),
x["url"].ToString())
).ToList();

return bookmarkList;
}
Expand Down
88 changes: 79 additions & 9 deletions Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@
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<Bookmark> cachedBookmarks = new List<Bookmark>();

private Settings _settings { get; set;}
private Settings _settings { get; set; }

public void Init(PluginInitContext context)
{
this.context = context;

_settings = context.API.LoadSettingJsonStorage<Settings>();

cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);

_ = MonitorRefreshQueue();
}

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

return true;
},
ContextData = new BookmarkAttributes { Url = c.Url }
ContextData = new BookmarkAttributes
{
Url = c.Url
}
}).Where(r => r.Score > 0);
return returnList.ToList();
}
Expand All @@ -69,11 +77,64 @@ public List<Result> Query(Query query)
context.API.OpenUrl(c.Url);
return true;
},
ContextData = new BookmarkAttributes { Url = c.Url }
ContextData = new BookmarkAttributes
{
Url = c.Url
}
}).ToList();
}
}


private static Channel<byte> refreshQueue = Channel.CreateBounded<byte>(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<FileSystemWatcher> 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();
Expand All @@ -98,7 +159,8 @@ public Control CreateSettingPanel()

public List<Result> LoadContextMenus(Result selectedResult)
{
return new List<Result>() {
return new List<Result>()
{
new Result
{
Title = context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"),
Expand All @@ -114,20 +176,28 @@ public List<Result> LoadContextMenus(Result selectedResult)
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);

return false;
}
},
IcoPath = "Images\\copylink.png"
}};
}
};
}

internal class BookmarkAttributes
{
internal string Url { get; set; }
}
public void Dispose()
{
foreach (var watcher in Watchers)
{
watcher.Dispose();
}
}
}
}
}
2 changes: 1 addition & 1 deletion Plugins/Flow.Launcher.Plugin.BrowserBookmark/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "Browser Bookmarks",
"Description": "Search your browser bookmarks",
"Author": "qianlifeng, Ioannis G.",
"Version": "1.6.3",
"Version": "1.7.0",
"Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.BrowserBookmark.dll",
Expand Down
Loading