Merge branch 'master' into dev

This commit is contained in:
jhdxr 2017-10-21 15:11:53 +08:00
commit d3169b10f8
9 changed files with 140 additions and 66 deletions

View File

@ -19,7 +19,8 @@ class Wox(object):
request_method = dict(methods)[request_method_name]
results = request_method(*request_parameters)
if request_method_name == "query":
if request_method_name == "query" or request_method_name == "context_menu":
print(json.dumps({"result": results}))
def query(self,query):
@ -28,6 +29,12 @@ class Wox(object):
"""
return []
def context_menu(self, data):
"""
optional context menu entries for a result
"""
return []
def debug(self,msg):
"""
alert msg

View File

@ -9,6 +9,16 @@ class HelloWorld(Wox):
results.append({
"Title": "Hello World",
"SubTitle": "Query: {}".format(query),
"IcoPath":"Images/app.ico",
"ContextData": "ctxData"
})
return results
def context_menu(self, data):
results = []
results.append({
"Title": "Context menu entry",
"SubTitle": "Data: {}".format(data),
"IcoPath":"Images/app.ico"
})
return results

View File

@ -204,25 +204,30 @@ namespace Wox.Plugin.Program.Programs
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
{
if (Directory.Exists(directory))
if (!Directory.Exists(directory))
return new string[] { };
var ds = Directory.GetDirectories(directory);
var paths = ds.SelectMany(d =>
{
IEnumerable<string> files;
try
{
files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
var paths_for_suffixes = suffixes.SelectMany(s =>
{
var pattern = $"*.{s}";
var ps = Directory.EnumerateFiles(d, pattern, SearchOption.AllDirectories);
return ps;
});
return paths_for_suffixes;
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
Log.Exception($"|Program.Win32.ProgramPaths|Can't parse directory <{directory}>", e);
return new string[] { };
Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e);
return new List<string>();
}
files = files.Where(f => suffixes.Contains(Extension(f)));
return files;
}
else
{
return new string[] { };
}
});
return paths;
}
private static string Extension(string path)

View File

@ -1,3 +1,7 @@
NOTE
====
This project will not be maintained by @happlebao for about 2 month due to personal reason.
WoX
===

View File

@ -39,5 +39,16 @@ namespace Wox.Core.Plugin
_startInfo.Arguments = $"\"{rpcRequest}\"";
return Execute(_startInfo);
}
protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "contextmenu",
Parameters = new object[] { selectedResult.ContextData },
};
_startInfo.Arguments = $"\"{request}\"";
return Execute(_startInfo);
}
}
}

View File

@ -56,7 +56,7 @@ namespace Wox.Core.Plugin
string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0)
{
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ","));
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
}
@ -69,25 +69,27 @@ namespace Wox.Core.Plugin
}
private string GetParamterByType(object paramter)
private string GetParameterByType(object parameter)
{
if (paramter is string)
{
return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
if (parameter == null) {
return "null";
}
if (paramter is int || paramter is float || paramter is double)
if (parameter is string)
{
return string.Format(@"{0}", paramter);
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
}
if (paramter is bool)
if (parameter is int || parameter is float || parameter is double)
{
return string.Format(@"{0}", paramter.ToString().ToLower());
return string.Format(@"{0}", parameter);
}
return paramter.ToString();
if (parameter is bool)
{
return string.Format(@"{0}", parameter.ToString().ToLower());
}
return parameter.ToString();
}
private string RepalceEscapes(string str)
private string ReplaceEscapes(string str)
{
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client

View File

@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
/// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance
/// </summary>
internal abstract class JsonRPCPlugin : IPlugin
internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
{
protected PluginInitContext context;
public const string JsonRPC = "JsonRPC";
@ -29,56 +29,80 @@ namespace Wox.Core.Plugin
protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
protected abstract string ExecuteContextMenu(Result selectedResult);
public List<Result> Query(Query query)
{
string output = ExecuteQuery(query);
try
{
return DeserializedResult(output);
}
catch (Exception e)
{
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
return null;
}
}
public List<Result> LoadContextMenus(Result selectedResult)
{
string output = ExecuteContextMenu(selectedResult);
try
{
return DeserializedResult(output);
}
catch (Exception e)
{
Log.Exception($"|JsonRPCPlugin.LoadContextMenus|Exception on result <{selectedResult}>", e);
return null;
}
}
private List<Result> DeserializedResult(string output)
{
if (!String.IsNullOrEmpty(output))
{
try
List<Result> results = new List<Result>();
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null) return null;
foreach (JsonRPCResult result in queryResponseModel.Result)
{
List<Result> results = new List<Result>();
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null) return null;
foreach (JsonRPCResult result in queryResponseModel.Result)
JsonRPCResult result1 = result;
result.Action = c =>
{
JsonRPCResult result1 = result;
result.Action = c =>
{
if (result1.JsonRPCAction == null) return false;
if (result1.JsonRPCAction == null) return false;
if (!String.IsNullOrEmpty(result1.JsonRPCAction.Method))
if (!String.IsNullOrEmpty(result1.JsonRPCAction.Method))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
{
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
{
string actionReponse = ExecuteCallback(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);
}
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
results.Add(result);
}
return results;
}
catch (Exception e)
{
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
results.Add(result);
}
return results;
}
else
{
return null;
}
return null;
}
private void ExecuteWoxAPI(string method, object[] parameters)
@ -149,7 +173,7 @@ namespace Wox.Core.Plugin
}
else if (result.StartsWith("DEBUG:"))
{
MessageBox.Show(new Form {TopMost = true}, result.Substring(6));
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
return string.Empty;
}
else

View File

@ -83,7 +83,7 @@ namespace Wox.Core.Plugin
// Plugins.Initialize();
//}
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" +
" Restart Wox to take effect?",
"Restart Wox to take effect?",
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
PluginManager.API.RestarApp();

View File

@ -49,5 +49,16 @@ namespace Wox.Core.Plugin
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo);
}
protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "context_menu",
Parameters = new object[] { selectedResult.ContextData },
};
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo);
}
}
}