2014-07-06 22:57:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2014-07-10 23:57:08 +08:00
|
|
|
|
using System.Reflection;
|
2014-07-06 22:57:11 +08:00
|
|
|
|
using System.Text;
|
2014-07-10 23:57:08 +08:00
|
|
|
|
using System.Threading;
|
2014-07-06 22:57:11 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2014-07-09 23:44:57 +08:00
|
|
|
|
using Wox.JsonRPC;
|
2014-07-06 22:57:11 +08:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.PluginLoader
|
|
|
|
|
{
|
2014-07-09 18:15:23 +08:00
|
|
|
|
public abstract class BasePlugin : IPlugin
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
|
|
|
|
protected PluginInitContext context;
|
|
|
|
|
|
2014-07-09 23:44:57 +08:00
|
|
|
|
public abstract string SupportedLanguage { get; }
|
2014-07-06 22:57:11 +08:00
|
|
|
|
|
2014-07-07 23:05:06 +08:00
|
|
|
|
protected abstract string ExecuteQuery(Query query);
|
2014-07-09 18:15:23 +08:00
|
|
|
|
protected abstract string ExecuteAction(JsonRPCRequestModel rpcRequest);
|
2014-07-06 22:57:11 +08:00
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
2014-07-07 23:05:06 +08:00
|
|
|
|
string output = ExecuteQuery(query);
|
2014-07-06 22:57:11 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(output))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
2014-07-07 23:05:06 +08:00
|
|
|
|
|
|
|
|
|
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
|
2014-07-09 18:15:23 +08:00
|
|
|
|
foreach (JsonRPCResult result in queryResponseModel.Result)
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
2014-07-10 23:57:08 +08:00
|
|
|
|
JsonRPCResult result1 = result;
|
|
|
|
|
result.Action = (c) =>
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
2014-07-10 23:57:08 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(result1.JsonRPCAction.Method))
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
2014-07-10 23:57:08 +08:00
|
|
|
|
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
|
|
|
|
|
{
|
|
|
|
|
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ThreadPool.QueueUserWorkItem(state =>
|
|
|
|
|
{
|
|
|
|
|
string actionReponse = ExecuteAction(result1.JsonRPCAction);
|
|
|
|
|
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
|
|
|
|
|
if (jsonRpcRequestModel != null
|
|
|
|
|
&& string.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
|
|
|
|
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
|
|
|
|
|
{
|
|
|
|
|
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return !result1.JsonRPCAction.DontHideAfterAction;
|
|
|
|
|
};
|
2014-07-06 22:57:11 +08:00
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-10 23:57:08 +08:00
|
|
|
|
private void ExecuteWoxAPI(string method, object[] parameters)
|
|
|
|
|
{
|
|
|
|
|
MethodInfo methodInfo = App.Window.GetType().GetMethod(method);
|
|
|
|
|
if (methodInfo != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
methodInfo.Invoke(App.Window, parameters);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 23:05:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute external program and return the output
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
/// <param name="arguments"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected string Execute(string fileName, string arguments)
|
2014-07-06 22:57:11 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo start = new ProcessStartInfo();
|
|
|
|
|
start.FileName = fileName;
|
|
|
|
|
start.Arguments = arguments;
|
|
|
|
|
start.UseShellExecute = false;
|
|
|
|
|
start.CreateNoWindow = true;
|
|
|
|
|
start.RedirectStandardOutput = true;
|
|
|
|
|
using (Process process = Process.Start(start))
|
|
|
|
|
{
|
|
|
|
|
if (process != null)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = process.StandardOutput)
|
|
|
|
|
{
|
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init(PluginInitContext ctx)
|
|
|
|
|
{
|
|
|
|
|
this.context = ctx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|