2020-12-06 10:10:22 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Storage;
|
|
|
|
|
|
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
|
|
|
|
|
|
using Flow.Launcher.Plugin.PluginsManager.Views;
|
2020-12-06 08:58:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-12-27 13:13:25 +00:00
|
|
|
|
using System.Linq;
|
2020-12-06 10:10:22 +00:00
|
|
|
|
using System.Windows.Controls;
|
2020-12-27 13:13:25 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure;
|
2020-12-28 15:06:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-01-02 14:21:45 +00:00
|
|
|
|
using System.Threading;
|
2020-12-06 08:58:27 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Plugin.PluginsManager
|
|
|
|
|
|
{
|
2021-01-02 14:21:45 +00:00
|
|
|
|
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n, IAsyncReloadable
|
2020-12-06 08:58:27 +00:00
|
|
|
|
{
|
2020-12-29 04:14:59 +00:00
|
|
|
|
internal PluginInitContext Context { get; set; }
|
2020-12-06 10:10:22 +00:00
|
|
|
|
|
|
|
|
|
|
internal Settings Settings;
|
|
|
|
|
|
|
|
|
|
|
|
private SettingsViewModel viewModel;
|
|
|
|
|
|
|
|
|
|
|
|
private IContextMenu contextMenu;
|
|
|
|
|
|
|
2020-12-27 13:13:25 +00:00
|
|
|
|
internal PluginsManager pluginManager;
|
|
|
|
|
|
|
2020-12-29 10:33:53 +00:00
|
|
|
|
private DateTime lastUpdateTime = DateTime.MinValue;
|
2020-12-28 15:06:47 +00:00
|
|
|
|
|
2020-12-06 10:10:22 +00:00
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new PluginsManagerSettings(viewModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-20 10:00:16 +00:00
|
|
|
|
public Task InitAsync(PluginInitContext context)
|
2020-12-06 10:10:22 +00:00
|
|
|
|
{
|
|
|
|
|
|
Context = context;
|
|
|
|
|
|
viewModel = new SettingsViewModel(context);
|
|
|
|
|
|
Settings = viewModel.Settings;
|
2020-12-29 07:13:52 +00:00
|
|
|
|
contextMenu = new ContextMenu(Context);
|
2020-12-27 13:13:25 +00:00
|
|
|
|
pluginManager = new PluginsManager(Context, Settings);
|
2021-01-17 10:39:53 +00:00
|
|
|
|
var updateManifestTask = pluginManager.UpdateManifest();
|
2021-01-20 10:00:16 +00:00
|
|
|
|
_ = updateManifestTask.ContinueWith(t =>
|
2021-01-17 10:39:53 +00:00
|
|
|
|
{
|
2021-01-20 10:00:16 +00:00
|
|
|
|
if (t.IsCompletedSuccessfully)
|
2021-01-21 02:41:50 +00:00
|
|
|
|
{
|
2021-01-20 10:00:16 +00:00
|
|
|
|
lastUpdateTime = DateTime.Now;
|
2021-01-21 02:41:50 +00:00
|
|
|
|
}
|
2021-01-20 10:00:16 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
context.API.ShowMsg("Plugin Manifest Download Fail.",
|
|
|
|
|
|
"Please check if you can connect to github.com. " +
|
2021-01-18 21:52:12 +00:00
|
|
|
|
"This error means you may not be able to Install and Update Plugin.", pluginManager.icoPath, false);
|
2021-01-20 10:00:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2020-12-06 10:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> LoadContextMenus(Result selectedResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
return contextMenu.LoadContextMenus(selectedResult);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-02 14:21:45 +00:00
|
|
|
|
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
|
2020-12-06 10:10:22 +00:00
|
|
|
|
{
|
2020-12-14 10:05:32 +00:00
|
|
|
|
var search = query.Search.ToLower();
|
2020-12-28 15:06:47 +00:00
|
|
|
|
|
2020-12-27 13:13:25 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(search))
|
2020-12-29 04:23:15 +00:00
|
|
|
|
return pluginManager.GetDefaultHotKeys();
|
2020-12-06 20:40:42 +00:00
|
|
|
|
|
2020-12-29 07:56:51 +00:00
|
|
|
|
if ((DateTime.Now - lastUpdateTime).TotalHours > 12) // 12 hours
|
2020-12-28 15:06:47 +00:00
|
|
|
|
{
|
2021-01-02 14:21:45 +00:00
|
|
|
|
await pluginManager.UpdateManifest();
|
|
|
|
|
|
lastUpdateTime = DateTime.Now;
|
2020-12-28 15:06:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-27 13:13:25 +00:00
|
|
|
|
return search switch
|
|
|
|
|
|
{
|
2021-01-17 10:42:01 +00:00
|
|
|
|
var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token),
|
2020-12-27 13:13:25 +00:00
|
|
|
|
var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s),
|
|
|
|
|
|
var s when s.StartsWith(Settings.HotkeyUpdate) => pluginManager.RequestUpdate(s),
|
2020-12-29 04:23:15 +00:00
|
|
|
|
_ => pluginManager.GetDefaultHotKeys().Where(hotkey =>
|
2020-12-27 13:13:25 +00:00
|
|
|
|
{
|
|
|
|
|
|
hotkey.Score = StringMatcher.FuzzySearch(search, hotkey.Title).Score;
|
|
|
|
|
|
return hotkey.Score > 0;
|
|
|
|
|
|
}).ToList()
|
|
|
|
|
|
};
|
2020-12-06 10:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
|
{
|
|
|
|
|
|
viewModel.Save();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_name");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_description");
|
|
|
|
|
|
}
|
2021-01-02 05:58:23 +00:00
|
|
|
|
|
2021-01-02 14:21:45 +00:00
|
|
|
|
public async Task ReloadDataAsync()
|
2021-01-02 05:58:23 +00:00
|
|
|
|
{
|
2021-01-02 14:21:45 +00:00
|
|
|
|
await pluginManager.UpdateManifest();
|
2021-01-02 07:06:44 +00:00
|
|
|
|
lastUpdateTime = DateTime.Now;
|
2021-01-02 05:58:23 +00:00
|
|
|
|
}
|
2020-12-06 08:58:27 +00:00
|
|
|
|
}
|
2021-01-17 10:39:53 +00:00
|
|
|
|
}
|