Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,9 @@ protected List<Bookmark> LoadBookmarksFromFile(string path, string source)
{
if (!File.Exists(path))
return new();

Main.RegisterBookmarkFile(path);

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 @@ -3,6 +3,10 @@
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
{
public class FirefoxBookmarkLoader : IBookmarkLoader
{
private const string queryAllBookmarks = @"SELECT moz_places.url, moz_bookmarks.title
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();
}
}
}
}
}
16 changes: 11 additions & 5 deletions Plugins/Flow.Launcher.Plugin.Program/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Flow.Launcher.Plugin.Program
{
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, ISavable, IAsyncReloadable
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, ISavable, IAsyncReloadable, IDisposable
{
internal static Win32[] _win32s { get; set; }
internal static UWP.Application[] _uwps { get; set; }
Expand Down Expand Up @@ -126,6 +126,9 @@ public async Task InitAsync(PluginInitContext context)

if (!(_win32s.Any() && _uwps.Any()))
await indexTask;

Win32.WatchProgramUpdate(_settings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you not also need to add WatchProgramUpdate for UWP apps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I forget? I will check soon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no. UWP are watched by package update

UWP.WatchPackageChange();
}

public static void IndexWin32Programs()
Expand Down Expand Up @@ -209,13 +212,11 @@ private void DisableProgram(IProgram programToDelete)
return;

if (_uwps.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
_uwps.Where(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier)
.FirstOrDefault()
_uwps.FirstOrDefault(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier)!
.Enabled = false;

if (_win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
_win32s.Where(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier)
.FirstOrDefault()
_win32s.FirstOrDefault(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier)!
.Enabled = false;

_settings.DisabledProgramSources
Expand Down Expand Up @@ -248,5 +249,10 @@ public async Task ReloadDataAsync()
{
await IndexPrograms();
}
public void Dispose()
{
Win32.Dispose();
UWP.Dispose();
}
}
}
Loading