2021-02-14 08:00:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2019-08-06 10:36:28 +00:00
|
|
|
|
using System.Linq;
|
2021-02-14 08:00:00 +00:00
|
|
|
|
using System.Windows;
|
2019-11-10 21:00:31 +00:00
|
|
|
|
using System.Windows.Controls;
|
2021-02-14 08:00:00 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Storage;
|
|
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Commands;
|
|
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Models;
|
|
|
|
|
|
using Flow.Launcher.Plugin.BrowserBookmark.Views;
|
|
|
|
|
|
using Flow.Launcher.Plugin.SharedCommands;
|
2022-01-08 22:58:12 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.BrowserBookmark
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
2022-01-08 22:58:12 +00:00
|
|
|
|
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, IDisposable
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
|
|
|
|
|
private PluginInitContext context;
|
2020-10-26 03:34:59 +00:00
|
|
|
|
|
2019-11-10 21:00:31 +00:00
|
|
|
|
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2022-01-08 22:58:12 +00:00
|
|
|
|
private Settings _settings { get; set; }
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2019-11-10 21:00:31 +00:00
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.context = context;
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
2021-05-13 11:29:21 +00:00
|
|
|
|
_settings = context.API.LoadSettingJsonStorage<Settings>();
|
2021-03-18 03:38:07 +00:00
|
|
|
|
|
2021-09-23 18:13:01 +00:00
|
|
|
|
cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
|
|
|
|
|
_ = MonitorRefreshQueue();
|
2019-11-10 21:00:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-06 10:36:28 +00:00
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
|
{
|
2020-08-04 10:10:42 +00:00
|
|
|
|
string param = query.Search.TrimStart();
|
2019-08-06 10:36:28 +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
|
|
|
|
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
|
|
|
|
|
if (!topResults)
|
|
|
|
|
|
{
|
2019-09-29 04:30:33 +00:00
|
|
|
|
// Since we mixed chrome and firefox bookmarks, we should order them again
|
2020-10-26 03:34:59 +00:00
|
|
|
|
var returnList = cachedBookmarks.Select(c => new Result()
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
2020-10-26 03:34:59 +00:00
|
|
|
|
Title = c.Name,
|
|
|
|
|
|
SubTitle = c.Url,
|
|
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
2021-09-22 23:09:30 +00:00
|
|
|
|
Score = BookmarkLoader.MatchProgram(c, param).Score,
|
2020-10-26 03:34:59 +00:00
|
|
|
|
Action = _ =>
|
2019-11-10 21:00:31 +00:00
|
|
|
|
{
|
2021-12-05 21:31:39 +00:00
|
|
|
|
context.API.OpenUrl(c.Url);
|
2020-10-26 03:34:59 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2021-02-14 08:00:00 +00:00
|
|
|
|
},
|
2022-01-08 22:58:12 +00:00
|
|
|
|
ContextData = new BookmarkAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
Url = c.Url
|
|
|
|
|
|
}
|
2020-10-26 03:34:59 +00:00
|
|
|
|
}).Where(r => r.Score > 0);
|
|
|
|
|
|
return returnList.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return cachedBookmarks.Select(c => new Result()
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = c.Name,
|
|
|
|
|
|
SubTitle = c.Url,
|
|
|
|
|
|
IcoPath = @"Images\bookmark.png",
|
|
|
|
|
|
Score = 5,
|
|
|
|
|
|
Action = _ =>
|
2019-11-10 21:00:31 +00:00
|
|
|
|
{
|
2021-12-05 21:31:39 +00:00
|
|
|
|
context.API.OpenUrl(c.Url);
|
2020-10-26 03:34:59 +00:00
|
|
|
|
return true;
|
2021-02-14 08:00:00 +00:00
|
|
|
|
},
|
2022-01-08 22:58:12 +00:00
|
|
|
|
ContextData = new BookmarkAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
Url = c.Url
|
|
|
|
|
|
}
|
2020-10-26 03:34:59 +00:00
|
|
|
|
}).ToList();
|
|
|
|
|
|
}
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
2019-10-06 01:58:36 +00:00
|
|
|
|
|
2022-01-08 22:58:12 +00:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-06 01:58:36 +00:00
|
|
|
|
public void ReloadData()
|
|
|
|
|
|
{
|
|
|
|
|
|
cachedBookmarks.Clear();
|
|
|
|
|
|
|
2021-09-23 18:13:01 +00:00
|
|
|
|
cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
|
2019-10-06 01:58:36 +00:00
|
|
|
|
}
|
2019-10-07 15:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
|
{
|
2020-04-21 12:54:41 +00:00
|
|
|
|
return context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_name");
|
2019-10-07 15:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
2020-04-21 12:54:41 +00:00
|
|
|
|
return context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_description");
|
2019-10-07 15:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-10 21:00:31 +00:00
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new SettingsControl(_settings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-14 08:00:00 +00:00
|
|
|
|
public List<Result> LoadContextMenus(Result selectedResult)
|
|
|
|
|
|
{
|
2022-01-08 22:58:12 +00:00
|
|
|
|
return new List<Result>()
|
|
|
|
|
|
{
|
2021-02-14 08:00:00 +00:00
|
|
|
|
new Result
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"),
|
|
|
|
|
|
SubTitle = context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_subtitle"),
|
|
|
|
|
|
Action = _ =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-02-14 09:32:20 +00:00
|
|
|
|
Clipboard.SetDataObject(((BookmarkAttributes)selectedResult.ContextData).Url);
|
2021-02-14 08:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = "Failed to set url in clipboard";
|
2022-01-08 22:58:12 +00:00
|
|
|
|
Log.Exception("Main", message, e, "LoadContextMenus");
|
2021-02-14 08:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
context.API.ShowMsg(message);
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2022-12-12 04:08:22 +00:00
|
|
|
|
IcoPath = "Images\\copylink.png",
|
|
|
|
|
|
Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\ue8c8")
|
2022-01-08 22:58:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2021-02-14 08:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class BookmarkAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
internal string Url { get; set; }
|
|
|
|
|
|
}
|
2022-01-08 22:58:12 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var watcher in Watchers)
|
|
|
|
|
|
{
|
|
|
|
|
|
watcher.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
2022-12-12 04:08:22 +00:00
|
|
|
|
}
|