2015-11-03 13:09:54 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2016-01-08 04:04:37 +08:00
|
|
|
|
using Wox.Core.Resource;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2015-11-09 09:32:33 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2014-12-29 22:09:54 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-05-03 05:37:01 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Core.Plugin
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The entry for managing Wox plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class PluginManager
|
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
private static IEnumerable<PluginPair> _contextMenuPlugins;
|
2014-12-26 22:51:04 +08:00
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Directories that will hold Wox plugin directory
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public static List<PluginPair> AllPlugins { get; private set; }
|
2016-01-08 09:13:36 +08:00
|
|
|
|
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
|
|
|
|
|
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
|
2015-11-06 04:44:14 +08:00
|
|
|
|
|
2015-11-02 06:59:56 +08:00
|
|
|
|
public static IPublicAPI API { private set; get; }
|
2016-05-05 08:57:03 +08:00
|
|
|
|
private static PluginsSettings _settings;
|
2016-05-05 23:08:44 +08:00
|
|
|
|
private static List<PluginMetadata> _metadatas;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
private static readonly string[] Directories = { Infrastructure.Wox.PreinstalledDirectory, Infrastructure.Wox.UserDirectory };
|
2015-11-02 06:59:56 +08:00
|
|
|
|
|
2016-04-27 09:15:53 +08:00
|
|
|
|
private static void ValidateUserDirectory()
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-05-04 09:26:19 +08:00
|
|
|
|
if (!Directory.Exists(Infrastructure.Wox.UserDirectory))
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-05-04 09:26:19 +08:00
|
|
|
|
Directory.CreateDirectory(Infrastructure.Wox.UserDirectory);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 05:37:01 +08:00
|
|
|
|
public static void Save()
|
|
|
|
|
{
|
|
|
|
|
foreach (var plugin in AllPlugins)
|
|
|
|
|
{
|
|
|
|
|
var savable = plugin.Plugin as ISavable;
|
|
|
|
|
savable?.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
|
static PluginManager()
|
|
|
|
|
{
|
|
|
|
|
ValidateUserDirectory();
|
|
|
|
|
|
|
|
|
|
// todo happlebao temp hack to let MainVM to register ResultsUpdated event
|
|
|
|
|
_metadatas = PluginConfig.Parse(Directories);
|
|
|
|
|
AllPlugins = PluginsLoader.CSharpPlugins(_metadatas).ToList();
|
|
|
|
|
}
|
2016-05-05 08:57:03 +08:00
|
|
|
|
public static void InitializePlugins(IPublicAPI api, PluginsSettings settings)
|
2016-03-28 08:09:40 +08:00
|
|
|
|
{
|
2016-05-05 08:57:03 +08:00
|
|
|
|
_settings = settings;
|
2016-05-05 23:08:44 +08:00
|
|
|
|
var plugins = PluginsLoader.PythonPlugins(_metadatas, _settings.PythonDirectory);
|
|
|
|
|
AllPlugins = AllPlugins.Concat(plugins).ToList();
|
2016-05-05 08:57:03 +08:00
|
|
|
|
_settings.UpdatePluginSettings(AllPlugins);
|
2016-04-27 09:15:53 +08:00
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
//load plugin i18n languages
|
|
|
|
|
ResourceMerger.UpdatePluginLanguages();
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
API = api;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Parallel.ForEach(AllPlugins, pair =>
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
var milliseconds = Stopwatch.Normal($"Plugin init: {pair.Metadata.Name}", () =>
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
pair.Plugin.Init(new PluginInitContext
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
CurrentPluginMetadata = pair.Metadata,
|
|
|
|
|
Proxy = HttpProxy.Instance,
|
|
|
|
|
API = API
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
2015-01-08 22:49:42 +08:00
|
|
|
|
});
|
2016-05-06 04:15:13 +08:00
|
|
|
|
pair.InitTime = milliseconds;
|
|
|
|
|
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
|
|
|
|
|
});
|
2015-01-27 22:59:03 +08:00
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
|
|
|
|
foreach (var plugin in AllPlugins)
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
if (IsGlobalPlugin(plugin.Metadata))
|
2015-11-07 11:50:15 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
GlobalPlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (string actionKeyword in plugin.Metadata.ActionKeywords)
|
2015-11-07 11:50:15 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
NonGlobalPlugins[actionKeyword] = plugin;
|
2015-11-07 11:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-06 04:15:13 +08:00
|
|
|
|
}
|
2016-05-05 23:08:44 +08:00
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 12:34:51 +08:00
|
|
|
|
public static void InstallPlugin(string path)
|
|
|
|
|
{
|
|
|
|
|
PluginInstaller.Install(path);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 13:09:54 +08:00
|
|
|
|
public static Query QueryInit(string text) //todo is that possible to move it into type Query?
|
2014-12-26 22:51:04 +08:00
|
|
|
|
{
|
2015-11-03 13:09:54 +08:00
|
|
|
|
// replace multiple white spaces with one white space
|
2015-11-05 06:49:40 +08:00
|
|
|
|
var terms = text.Split(new[] { Query.TermSeperater }, StringSplitOptions.RemoveEmptyEntries);
|
2015-11-06 04:44:14 +08:00
|
|
|
|
var rawQuery = string.Join(Query.TermSeperater, terms);
|
2015-11-03 13:09:54 +08:00
|
|
|
|
var actionKeyword = string.Empty;
|
|
|
|
|
var search = rawQuery;
|
2015-11-06 04:44:14 +08:00
|
|
|
|
List<string> actionParameters = terms.ToList();
|
2015-11-03 13:09:54 +08:00
|
|
|
|
if (terms.Length == 0) return null;
|
2015-11-07 11:50:15 +08:00
|
|
|
|
if (NonGlobalPlugins.ContainsKey(terms[0]))
|
2015-11-01 00:02:56 +08:00
|
|
|
|
{
|
2015-11-03 13:09:54 +08:00
|
|
|
|
actionKeyword = terms[0];
|
2015-11-06 04:44:14 +08:00
|
|
|
|
actionParameters = terms.Skip(1).ToList();
|
2015-11-05 06:49:40 +08:00
|
|
|
|
search = string.Join(Query.TermSeperater, actionParameters.ToArray());
|
2015-11-02 01:25:44 +08:00
|
|
|
|
}
|
2015-11-03 13:09:54 +08:00
|
|
|
|
return new Query
|
|
|
|
|
{
|
2015-11-05 05:35:04 +08:00
|
|
|
|
Terms = terms,
|
|
|
|
|
RawQuery = rawQuery,
|
|
|
|
|
ActionKeyword = actionKeyword,
|
|
|
|
|
Search = search,
|
2015-11-03 13:09:54 +08:00
|
|
|
|
// Obsolete value initialisation
|
2015-11-05 05:35:04 +08:00
|
|
|
|
ActionName = actionKeyword,
|
2015-11-06 04:44:14 +08:00
|
|
|
|
ActionParameters = actionParameters
|
2015-11-03 13:09:54 +08:00
|
|
|
|
};
|
2015-11-02 01:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
public static List<PluginPair> ValidPluginsForQuery(Query query)
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
|
2015-11-02 01:25:44 +08:00
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
var plugin = NonGlobalPlugins[query.ActionKeyword];
|
|
|
|
|
return new List<PluginPair> { plugin };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return GlobalPlugins;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2014-12-26 22:51:04 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
|
|
|
|
//happlebao todo prevent plugin initial when plugin is disabled
|
|
|
|
|
public static void DisablePlugin(PluginPair plugin)
|
|
|
|
|
{
|
|
|
|
|
var actionKeywords = plugin.Metadata.ActionKeywords;
|
|
|
|
|
if (actionKeywords == null || actionKeywords.Count == 0 || actionKeywords[0] == Query.GlobalPluginWildcardSign)
|
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Remove(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var actionkeyword in plugin.Metadata.ActionKeywords)
|
|
|
|
|
{
|
|
|
|
|
NonGlobalPlugins.Remove(actionkeyword);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AllPlugins.Remove(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void EnablePlugin(PluginPair plugin)
|
|
|
|
|
{
|
|
|
|
|
var actionKeywords = plugin.Metadata.ActionKeywords;
|
|
|
|
|
if (actionKeywords == null || actionKeywords.Count == 0 || actionKeywords[0] == Query.GlobalPluginWildcardSign)
|
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var actionkeyword in plugin.Metadata.ActionKeywords)
|
|
|
|
|
{
|
|
|
|
|
NonGlobalPlugins[actionkeyword] = plugin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AllPlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 08:09:40 +08:00
|
|
|
|
public static List<Result> QueryForPlugin(PluginPair pair, Query query)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-03-28 08:09:40 +08:00
|
|
|
|
var results = new List<Result>();
|
2015-11-02 06:59:56 +08:00
|
|
|
|
try
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2016-05-05 23:08:44 +08:00
|
|
|
|
var metadata = pair.Metadata;
|
|
|
|
|
var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {metadata.Name}", () =>
|
|
|
|
|
{
|
|
|
|
|
results = pair.Plugin.Query(query) ?? results;
|
|
|
|
|
UpdatePluginMetadata(results, metadata, query);
|
|
|
|
|
});
|
2015-11-05 05:35:04 +08:00
|
|
|
|
pair.QueryCount += 1;
|
|
|
|
|
pair.AvgQueryTime = pair.QueryCount == 1 ? milliseconds : (pair.AvgQueryTime + milliseconds) / 2;
|
2015-11-02 06:59:56 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
catch (Exception e)
|
2015-11-02 06:59:56 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
throw new WoxPluginException(pair.Metadata.Name, "QueryForPlugin failed", e);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
2016-03-28 08:09:40 +08:00
|
|
|
|
return results;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 23:08:44 +08:00
|
|
|
|
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
|
|
|
|
|
{
|
|
|
|
|
foreach (var r in results)
|
|
|
|
|
{
|
|
|
|
|
r.PluginDirectory = metadata.PluginDirectory;
|
|
|
|
|
r.PluginID = metadata.ID;
|
|
|
|
|
r.OriginQuery = query;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:19:13 +08:00
|
|
|
|
private static bool IsGlobalPlugin(PluginMetadata metadata)
|
2015-01-04 23:08:26 +08:00
|
|
|
|
{
|
2015-11-06 04:44:14 +08:00
|
|
|
|
return metadata.ActionKeywords.Contains(Query.GlobalPluginWildcardSign);
|
2015-01-04 23:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// get specified plugin, return null if not found
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static PluginPair GetPluginForId(string id)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-11-02 07:32:17 +08:00
|
|
|
|
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
2015-02-05 18:43:05 +08:00
|
|
|
|
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static IEnumerable<PluginPair> GetPluginsForInterface<T>() where T : IFeatures
|
2015-02-05 22:20:42 +08:00
|
|
|
|
{
|
2015-11-06 04:44:14 +08:00
|
|
|
|
return AllPlugins.Where(p => p.Plugin is T);
|
2015-02-05 22:20:42 +08:00
|
|
|
|
}
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
2015-11-06 04:44:14 +08:00
|
|
|
|
public static List<Result> GetContextMenusForPlugin(Result result)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
var pluginPair = _contextMenuPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
if (pluginPair != null)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var metadata = pluginPair.Metadata;
|
2016-04-26 03:49:56 +08:00
|
|
|
|
var plugin = (IContextMenu)pluginPair.Plugin;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2015-02-07 23:49:46 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
var results = plugin.LoadContextMenus(result);
|
|
|
|
|
foreach (var r in results)
|
|
|
|
|
{
|
|
|
|
|
r.PluginDirectory = metadata.PluginDirectory;
|
|
|
|
|
r.PluginID = metadata.ID;
|
|
|
|
|
r.OriginQuery = result.OriginQuery;
|
|
|
|
|
}
|
|
|
|
|
return results;
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
catch (Exception e)
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
2016-03-26 09:20:42 +08:00
|
|
|
|
Log.Error(new WoxPluginException(metadata.Name, "Couldn't load plugin context menus", e));
|
|
|
|
|
return new List<Result>();
|
2015-02-07 23:49:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 09:20:42 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
|
|
|
|
}
|
2015-11-09 11:20:02 +08:00
|
|
|
|
|
|
|
|
|
public static void UpdateActionKeywordForPlugin(PluginPair plugin, string oldActionKeyword, string newActionKeyword)
|
|
|
|
|
{
|
|
|
|
|
var actionKeywords = plugin.Metadata.ActionKeywords;
|
|
|
|
|
if (string.IsNullOrEmpty(newActionKeyword))
|
|
|
|
|
{
|
|
|
|
|
string msg = InternationalizationManager.Instance.GetTranslation("newActionKeywordsCannotBeEmpty");
|
|
|
|
|
throw new WoxPluginException(plugin.Metadata.Name, msg);
|
|
|
|
|
}
|
2015-11-13 13:14:27 +08:00
|
|
|
|
// do nothing if they are same
|
|
|
|
|
if (oldActionKeyword == newActionKeyword) return;
|
2015-11-09 11:20:02 +08:00
|
|
|
|
if (NonGlobalPlugins.ContainsKey(newActionKeyword))
|
|
|
|
|
{
|
|
|
|
|
string msg = InternationalizationManager.Instance.GetTranslation("newActionKeywordsHasBeenAssigned");
|
|
|
|
|
throw new WoxPluginException(plugin.Metadata.Name, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add new action keyword
|
|
|
|
|
if (string.IsNullOrEmpty(oldActionKeyword))
|
|
|
|
|
{
|
|
|
|
|
actionKeywords.Add(newActionKeyword);
|
|
|
|
|
if (newActionKeyword == Query.GlobalPluginWildcardSign)
|
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NonGlobalPlugins[newActionKeyword] = plugin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// update existing action keyword
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int index = actionKeywords.IndexOf(oldActionKeyword);
|
|
|
|
|
actionKeywords[index] = newActionKeyword;
|
|
|
|
|
if (oldActionKeyword == Query.GlobalPluginWildcardSign)
|
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Remove(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NonGlobalPlugins.Remove(oldActionKeyword);
|
|
|
|
|
}
|
|
|
|
|
if (newActionKeyword == Query.GlobalPluginWildcardSign)
|
|
|
|
|
{
|
|
|
|
|
GlobalPlugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NonGlobalPlugins[newActionKeyword] = plugin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|