PowerToys/Wox/PluginLoader/BasePluginWrapper.cs

93 lines
2.8 KiB
C#
Raw Normal View History

2014-07-06 22:57:11 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Wox.Plugin;
using Wox.RPC;
namespace Wox.PluginLoader
{
public abstract class BasePluginWrapper : IPlugin
{
protected PluginInitContext context;
public abstract List<string> GetAllowedLanguages();
2014-07-07 23:05:06 +08:00
protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteAction(string 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);
foreach (JsonRPCResult result in queryResponseModel.QueryResults)
2014-07-06 22:57:11 +08:00
{
2014-07-07 23:05:06 +08:00
if (result.JSONRPCActionModel != null)
2014-07-06 22:57:11 +08:00
{
result.Action = (c) =>
{
2014-07-07 23:05:06 +08:00
ExecuteAction(result.JSONRPCAction);
2014-07-06 22:57:11 +08:00
return true;
};
}
results.Add(result);
}
return results;
}
catch
{
}
}
return null;
}
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;
}
}
}