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

View File

@ -9,6 +9,16 @@ class HelloWorld(Wox):
results.append({ results.append({
"Title": "Hello World", "Title": "Hello World",
"SubTitle": "Query: {}".format(query), "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" "IcoPath":"Images/app.ico"
}) })
return results return results

View File

@ -204,25 +204,30 @@ namespace Wox.Plugin.Program.Programs
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes) 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 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) catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{ {
Log.Exception($"|Program.Win32.ProgramPaths|Can't parse directory <{directory}>", e); Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e);
return new string[] { }; 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) 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 WoX
=== ===

View File

@ -39,5 +39,16 @@ namespace Wox.Core.Plugin
_startInfo.Arguments = $"\"{rpcRequest}\""; _startInfo.Arguments = $"\"{rpcRequest}\"";
return Execute(_startInfo); 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; string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0) 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) + "]"; parameters = parameters.Substring(0, parameters.Length - 1) + "]";
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters); 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 (parameter == null) {
if (paramter is string) return "null";
{
return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
} }
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 return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client .Replace(@"\", @"\\") //Escapes itself when passed to client

View File

@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
/// Represent the plugin that using JsonPRC /// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance /// every JsonRPC plugin should has its own plugin instance
/// </summary> /// </summary>
internal abstract class JsonRPCPlugin : IPlugin internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
{ {
protected PluginInitContext context; protected PluginInitContext context;
public const string JsonRPC = "JsonRPC"; public const string JsonRPC = "JsonRPC";
@ -29,13 +29,39 @@ namespace Wox.Core.Plugin
protected abstract string ExecuteQuery(Query query); protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest); protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
protected abstract string ExecuteContextMenu(Result selectedResult);
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
string output = ExecuteQuery(query); string output = ExecuteQuery(query);
if (!String.IsNullOrEmpty(output))
{
try 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))
{ {
List<Result> results = new List<Result>(); List<Result> results = new List<Result>();
@ -73,13 +99,11 @@ namespace Wox.Core.Plugin
} }
return results; return results;
} }
catch (Exception e) else
{ {
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
}
}
return null; return null;
} }
}
private void ExecuteWoxAPI(string method, object[] parameters) private void ExecuteWoxAPI(string method, object[] parameters)
{ {
@ -149,7 +173,7 @@ namespace Wox.Core.Plugin
} }
else if (result.StartsWith("DEBUG:")) 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; return string.Empty;
} }
else else

View File

@ -83,7 +83,7 @@ namespace Wox.Core.Plugin
// Plugins.Initialize(); // Plugins.Initialize();
//} //}
if (MessageBox.Show($"You have installed plugin {plugin.Name} successfully.{Environment.NewLine}" + 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) "Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{ {
PluginManager.API.RestarApp(); PluginManager.API.RestarApp();

View File

@ -49,5 +49,16 @@ namespace Wox.Core.Plugin
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo); 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);
}
} }
} }