2019-08-06 10:36:28 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2019-11-10 21:00:31 +00:00
|
|
|
|
using System.Windows.Controls;
|
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
|
|
|
|
{
|
2019-11-10 21:00:31 +00:00
|
|
|
|
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, ISavable
|
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
|
|
|
|
|
2019-11-10 21:00:31 +00:00
|
|
|
|
private readonly Settings _settings;
|
|
|
|
|
|
private readonly PluginJsonStorage<Settings> _storage;
|
|
|
|
|
|
|
|
|
|
|
|
public Main()
|
2019-08-06 10:36:28 +00:00
|
|
|
|
{
|
2019-11-10 21:00:31 +00:00
|
|
|
|
_storage = new PluginJsonStorage<Settings>();
|
|
|
|
|
|
_settings = _storage.Load();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
|
2019-10-06 01:55:57 +00:00
|
|
|
|
cachedBookmarks = Bookmarks.LoadAllBookmarks();
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-10 21:00:31 +00:00
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.context = context;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
2019-11-10 21:00:31 +00:00
|
|
|
|
}
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}).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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
|
{
|
|
|
|
|
|
_storage.Save();
|
|
|
|
|
|
}
|
2019-08-06 10:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|