2021-02-14 08:00:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-06-04 15:17:51 +00:00
|
|
|
|
using System.IO;
|
2019-08-06 10:36:28 +00:00
|
|
|
|
using System.Linq;
|
2025-06-04 15:17:51 +00:00
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Threading;
|
2019-11-10 21:00:31 +00:00
|
|
|
|
using System.Windows.Controls;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Commands;
|
|
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Models;
|
|
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Views;
|
2025-04-09 04:12:32 +00:00
|
|
|
|
using Flow.Launcher.Plugin.SharedCommands;
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.BrowserBookmark;
|
|
|
|
|
|
|
|
|
|
|
|
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, IDisposable
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
2025-04-13 09:59:39 +00:00
|
|
|
|
private static readonly string ClassName = nameof(Main);
|
|
|
|
|
|
|
2025-04-02 14:39:30 +00:00
|
|
|
|
internal static string _faviconCacheDir;
|
|
|
|
|
|
|
2025-06-23 04:38:27 +00:00
|
|
|
|
internal static PluginInitContext Context { get; set; }
|
2020-10-26 03:34:59 +00:00
|
|
|
|
|
2025-06-04 15:18:01 +00:00
|
|
|
|
internal static Settings _settings;
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2025-06-04 15:18:01 +00:00
|
|
|
|
private static List<Bookmark> _cachedBookmarks = new();
|
2023-05-23 11:37:38 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
private static bool _initialized = false;
|
2025-03-20 10:11:15 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
Context = context;
|
2023-05-23 11:37:38 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
_settings = context.API.LoadSettingJsonStorage<Settings>();
|
2021-03-18 03:38:07 +00:00
|
|
|
|
|
2025-04-02 14:39:30 +00:00
|
|
|
|
_faviconCacheDir = Path.Combine(
|
2025-04-09 05:11:55 +00:00
|
|
|
|
context.CurrentPluginMetadata.PluginCacheDirectoryPath,
|
2025-04-02 14:39:30 +00:00
|
|
|
|
"FaviconCache");
|
2025-08-14 12:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Directory.Exists(_faviconCacheDir))
|
|
|
|
|
|
{
|
|
|
|
|
|
var files = Directory.GetFiles(_faviconCacheDir);
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
var extension = Path.GetExtension(file);
|
|
|
|
|
|
if (extension is ".db-shm" or ".db-wal" or ".sqlite-shm" or ".sqlite-wal")
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context.API.LogException(ClassName, "Failed to clean up orphaned cache files.", e);
|
|
|
|
|
|
}
|
2025-04-02 14:39:30 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
LoadBookmarksIfEnabled();
|
|
|
|
|
|
}
|
2023-05-23 11:37:38 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
private static void LoadBookmarksIfEnabled()
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
if (Context.CurrentPluginMetadata.Disabled)
|
2023-05-23 11:37:38 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
// Don't load or monitor files if disabled
|
|
|
|
|
|
return;
|
2019-11-10 21:00:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-12 09:19:16 +00:00
|
|
|
|
// Validate the cache directory before loading all bookmarks because Flow needs this directory to storage favicons
|
|
|
|
|
|
FilesFolders.ValidateDirectory(_faviconCacheDir);
|
|
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
_cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
|
|
|
|
|
|
_ = MonitorRefreshQueueAsync();
|
|
|
|
|
|
_initialized = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
|
{
|
2024-04-17 02:56:38 +00:00
|
|
|
|
// For when the plugin being previously disabled and is now re-enabled
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (!_initialized)
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
LoadBookmarksIfEnabled();
|
|
|
|
|
|
}
|
2023-06-15 13:30:30 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
string param = query.Search.TrimStart();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
// Should top results be returned? (true if no search parameters have been passed)
|
|
|
|
|
|
var topResults = string.IsNullOrEmpty(param);
|
2020-10-26 03:34:59 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (!topResults)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Since we mixed chrome and firefox bookmarks, we should order them again
|
|
|
|
|
|
return _cachedBookmarks
|
|
|
|
|
|
.Select(
|
|
|
|
|
|
c => new Result
|
2019-11-10 21:00:31 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Title = c.Name,
|
|
|
|
|
|
SubTitle = c.Url,
|
2025-03-20 07:35:16 +00:00
|
|
|
|
IcoPath = !string.IsNullOrEmpty(c.FaviconPath) && File.Exists(c.FaviconPath)
|
|
|
|
|
|
? c.FaviconPath
|
|
|
|
|
|
: @"Images\bookmark.png",
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Score = BookmarkLoader.MatchProgram(c, param).Score,
|
|
|
|
|
|
Action = _ =>
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
Context.API.OpenUrl(c.Url);
|
2020-10-26 03:34:59 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
},
|
|
|
|
|
|
ContextData = new BookmarkAttributes { Url = c.Url }
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
)
|
|
|
|
|
|
.Where(r => r.Score > 0)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return _cachedBookmarks
|
|
|
|
|
|
.Select(
|
|
|
|
|
|
c => new Result
|
2022-01-08 22:58:12 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Title = c.Name,
|
|
|
|
|
|
SubTitle = c.Url,
|
2025-03-20 07:35:16 +00:00
|
|
|
|
IcoPath = !string.IsNullOrEmpty(c.FaviconPath) && File.Exists(c.FaviconPath)
|
|
|
|
|
|
? c.FaviconPath
|
|
|
|
|
|
: @"Images\bookmark.png",
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Score = 5,
|
|
|
|
|
|
Action = _ =>
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
Context.API.OpenUrl(c.Url);
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
},
|
|
|
|
|
|
ContextData = new BookmarkAttributes { Url = c.Url }
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
)
|
|
|
|
|
|
.ToList();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
}
|
2019-10-06 01:58:36 +00:00
|
|
|
|
|
2025-03-20 08:54:14 +00:00
|
|
|
|
private static readonly Channel<byte> _refreshQueue = Channel.CreateBounded<byte>(1);
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
2025-03-20 08:54:14 +00:00
|
|
|
|
private static readonly SemaphoreSlim _fileMonitorSemaphore = new(1, 1);
|
2023-05-23 11:37:38 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
private static async Task MonitorRefreshQueueAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_fileMonitorSemaphore.CurrentCount < 1)
|
2022-01-08 22:58:12 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return;
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
await _fileMonitorSemaphore.WaitAsync();
|
|
|
|
|
|
var reader = _refreshQueue.Reader;
|
|
|
|
|
|
while (await reader.WaitToReadAsync())
|
2022-01-08 22:58:12 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (reader.TryRead(out _))
|
2023-05-22 15:13:29 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
ReloadAllBookmarks(false);
|
2023-05-22 15:13:29 +00:00
|
|
|
|
}
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
_fileMonitorSemaphore.Release();
|
|
|
|
|
|
}
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
private static readonly List<FileSystemWatcher> Watchers = new();
|
2023-02-11 17:58:53 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
internal static void RegisterBookmarkFile(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
var directory = Path.GetDirectoryName(path);
|
|
|
|
|
|
if (!Directory.Exists(directory) || !File.Exists(path))
|
2019-10-06 01:58:36 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return;
|
2019-10-06 01:58:36 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase)))
|
2019-10-07 15:08:06 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return;
|
2019-10-07 15:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-20 08:54:14 +00:00
|
|
|
|
var watcher = new FileSystemWatcher(directory!)
|
|
|
|
|
|
{
|
|
|
|
|
|
Filter = Path.GetFileName(path),
|
|
|
|
|
|
NotifyFilter = NotifyFilters.FileName |
|
|
|
|
|
|
NotifyFilters.LastWrite |
|
|
|
|
|
|
NotifyFilters.Size
|
|
|
|
|
|
};
|
2024-04-16 07:00:19 +00:00
|
|
|
|
|
|
|
|
|
|
watcher.Changed += static (_, _) =>
|
2019-10-07 15:08:06 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
_refreshQueue.Writer.TryWrite(default);
|
|
|
|
|
|
};
|
2019-10-07 15:08:06 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
watcher.Renamed += static (_, _) =>
|
2019-11-10 21:00:31 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
_refreshQueue.Writer.TryWrite(default);
|
|
|
|
|
|
};
|
2019-11-10 21:00:31 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
|
|
|
|
|
|
|
|
Watchers.Add(watcher);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ReloadData()
|
|
|
|
|
|
{
|
|
|
|
|
|
ReloadAllBookmarks();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void ReloadAllBookmarks(bool disposeFileWatchers = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cachedBookmarks.Clear();
|
|
|
|
|
|
if (disposeFileWatchers)
|
|
|
|
|
|
DisposeFileWatchers();
|
|
|
|
|
|
LoadBookmarksIfEnabled();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
return Localize.flowlauncher_plugin_browserbookmark_plugin_name();
|
2024-04-16 07:00:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
return Localize.flowlauncher_plugin_browserbookmark_plugin_description();
|
2024-04-16 07:00:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new SettingsControl(_settings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> LoadContextMenus(Result selectedResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new List<Result>()
|
2021-02-14 08:00:00 +00:00
|
|
|
|
{
|
2025-03-20 08:54:14 +00:00
|
|
|
|
new()
|
2022-01-08 22:58:12 +00:00
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
Title = Localize.flowlauncher_plugin_browserbookmark_copyurl_title(),
|
|
|
|
|
|
SubTitle = Localize.flowlauncher_plugin_browserbookmark_copyurl_subtitle(),
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Action = _ =>
|
2021-02-14 08:00:00 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
try
|
2021-02-14 08:00:00 +00:00
|
|
|
|
{
|
2025-06-23 04:38:27 +00:00
|
|
|
|
Context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
|
2021-02-14 08:00:00 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-20 13:01:22 +00:00
|
|
|
|
Context.API.LogException(ClassName, "Failed to set url in clipboard", e);
|
2025-07-20 13:01:48 +00:00
|
|
|
|
Context.API.ShowMsgError(Localize.flowlauncher_plugin_browserbookmark_copy_failed());
|
2024-04-16 07:00:19 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
IcoPath = @"Images\copylink.png",
|
|
|
|
|
|
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\ue8c8")
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class BookmarkAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
internal string Url { get; set; }
|
|
|
|
|
|
}
|
2023-05-23 11:37:38 +00:00
|
|
|
|
|
2024-04-16 07:00:19 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
DisposeFileWatchers();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void DisposeFileWatchers()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var watcher in Watchers)
|
2022-01-08 22:58:12 +00:00
|
|
|
|
{
|
2024-04-16 07:00:19 +00:00
|
|
|
|
watcher.Dispose();
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
2024-04-16 07:00:19 +00:00
|
|
|
|
Watchers.Clear();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
2022-12-12 04:08:22 +00:00
|
|
|
|
}
|