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;
|
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
|
|
|
|
{
|
2021-05-11 16:37:28 +00:00
|
|
|
|
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu
|
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
|
|
|
|
|
2021-03-18 03:59:48 +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;
|
2021-03-18 03:38:07 +00:00
|
|
|
|
|
2021-05-13 11:29:21 +00:00
|
|
|
|
_settings = context.API.LoadSettingJsonStorage<Settings>();
|
2021-03-18 03:38:07 +00:00
|
|
|
|
|
|
|
|
|
|
cachedBookmarks = Bookmarks.LoadAllBookmarks();
|
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",
|
|
|
|
|
|
Score = Bookmarks.MatchProgram(c, param).Score,
|
|
|
|
|
|
Action = _ =>
|
2019-11-10 21:00:31 +00:00
|
|
|
|
{
|
2020-10-26 03:34:59 +00:00
|
|
|
|
if (_settings.OpenInNewBrowserWindow)
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Url.NewBrowserWindow(_settings.BrowserPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Url.NewTabInBrowser(_settings.BrowserPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2021-02-14 08:00:00 +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
|
|
|
|
{
|
2020-10-26 03:34:59 +00:00
|
|
|
|
if (_settings.OpenInNewBrowserWindow)
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Url.NewBrowserWindow(_settings.BrowserPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Url.NewTabInBrowser(_settings.BrowserPath);
|
|
|
|
|
|
}
|
2019-11-10 21:00:31 +00:00
|
|
|
|
|
2020-10-26 03:34:59 +00:00
|
|
|
|
return true;
|
2021-02-14 08:00:00 +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
|
|
|
|
|
|
|
|
|
|
public void ReloadData()
|
|
|
|
|
|
{
|
|
|
|
|
|
cachedBookmarks.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
cachedBookmarks = Bookmarks.LoadAllBookmarks();
|
|
|
|
|
|
}
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new List<Result>() {
|
|
|
|
|
|
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";
|
|
|
|
|
|
Log.Exception("Main",message, e, "LoadContextMenus");
|
|
|
|
|
|
|
|
|
|
|
|
context.API.ShowMsg(message);
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
IcoPath = "Images\\copylink.png"
|
|
|
|
|
|
}};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class BookmarkAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
internal string Url { get; set; }
|
|
|
|
|
|
}
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|