Add IExclusiveSearch interface [WIP]

This commit is contained in:
qianlifeng 2015-02-05 18:43:05 +08:00
parent fa53bce27a
commit 7b0a643de3
6 changed files with 83 additions and 4 deletions

View File

@ -9,11 +9,12 @@ using WindowsInput;
using WindowsInput.Native;
using Wox.Infrastructure;
using Wox.Infrastructure.Hotkey;
using Wox.Plugin.Features;
using Control = System.Windows.Controls.Control;
namespace Wox.Plugin.CMD
{
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantSearch
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantSearch,IExclusiveSearch
{
private PluginInitContext context;
private bool WinRStroked;
@ -217,5 +218,10 @@ namespace Wox.Plugin.CMD
if (query.StartsWith(">")) return true;
return false;
}
public bool IsExclusiveSearch(Query query)
{
return query.Search.StartsWith(">");
}
}
}

View File

@ -12,6 +12,7 @@ using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Wox.Plugin.Features;
namespace Wox.Core.Plugin
{
@ -23,7 +24,7 @@ namespace Wox.Core.Plugin
public const string ActionKeywordWildcardSign = "*";
private static List<PluginMetadata> pluginMetadatas;
private static List<KeyValuePair<PluginMetadata, IInstantSearch>> instantSearches;
private static List<KeyValuePair<PluginPair,IExclusiveSearch>> exclusiveSearchPlugins;
public static String DebuggerMode { get; private set; }
public static IPublicAPI API { get; private set; }
@ -116,6 +117,7 @@ namespace Wox.Core.Plugin
{
if (!string.IsNullOrEmpty(query.RawQuery.Trim()))
{
query.Search = IsUserPluginQuery(query) ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : query.RawQuery;
QueryDispatcher.QueryDispatcher.Dispatch(query);
}
}
@ -238,5 +240,51 @@ namespace Wox.Core.Plugin
{
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
}
internal static List<KeyValuePair<PluginPair, IExclusiveSearch>> LoadExclusiveSearchPlugins()
{
if (exclusiveSearchPlugins != null) return exclusiveSearchPlugins;
exclusiveSearchPlugins = new List<KeyValuePair<PluginPair, IExclusiveSearch>>();
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
{
try
{
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IExclusiveSearch))).ToList();
if (types.Count == 0)
{
continue;
}
foreach (Type type in types)
{
exclusiveSearchPlugins.Add(new KeyValuePair<PluginPair, IExclusiveSearch>(AllPlugins.First(o => o.Metadata.ID == metadata.ID),
Activator.CreateInstance(type) as IExclusiveSearch));
}
}
catch (System.Exception e)
{
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
return exclusiveSearchPlugins;
}
internal static PluginPair GetExclusiveSearchPlugin(Query query)
{
KeyValuePair<PluginPair, IExclusiveSearch> plugin = LoadExclusiveSearchPlugins().FirstOrDefault(o => o.Value.IsExclusiveSearch((query)));
if (plugin.Key != null) return plugin.Key;
return null;
}
}
}

View File

@ -1,4 +1,7 @@

using System.Threading;
using Wox.Plugin;
namespace Wox.Core.Plugin.QueryDispatcher
{
internal static class QueryDispatcher
@ -8,14 +11,22 @@ namespace Wox.Core.Plugin.QueryDispatcher
public static void Dispatch(Wox.Plugin.Query query)
{
PluginPair exclusiveSearchPlugin = PluginManager.GetExclusiveSearchPlugin(query);
if (exclusiveSearchPlugin != null)
{
ThreadPool.QueueUserWorkItem(state =>
{
PluginManager.ExecutePluginQuery(exclusiveSearchPlugin, query);
});
return;
}
if (PluginManager.IsUserPluginQuery(query))
{
query.Search = query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1);
UserPluginDispatcher.Dispatch(query);
}
else
{
query.Search = query.RawQuery;
SystemPluginDispatcher.Dispatch(query);
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Plugin.Features
{
public interface IExclusiveSearch
{
bool IsExclusiveSearch(Query query);
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wox.Plugin.Features;
namespace Wox.Plugin
{

View File

@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="AllowedLanguage.cs" />
<Compile Include="EventHandler.cs" />
<Compile Include="Features\IExclusiveSearch.cs" />
<Compile Include="IInstantSearch.cs" />
<Compile Include="IHttpProxy.cs" />
<Compile Include="IPluginI18n.cs" />