Fix PluginManagement plugin for multiple action keyword

1. Fixup, part of #352
2. Windows.Form -> WPF
3. Refactoring
This commit is contained in:
bao-qian
2015-11-06 01:03:41 +00:00
parent 178710dabc
commit 00543bca17
2 changed files with 84 additions and 109 deletions

View File

@@ -7,134 +7,109 @@ using System.Net;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Wox.Plugin.PluginManagement namespace Wox.Plugin.PluginManagement
{ {
public class Main : IPlugin,IPluginI18n public class Main : IPlugin, IPluginI18n
{ {
private static string APIBASE = "https://api.getwox.com"; private static string APIBASE = "https://api.getwox.com";
private static string PluginConfigName = "plugin.json"; private static string PluginConfigName = "plugin.json";
private static string pluginSearchUrl = APIBASE + "/plugin/search/"; private static string pluginSearchUrl = APIBASE + "/plugin/search/";
private const string ListCommand = "list";
private const string InstallCommand = "install";
private const string UninstallCommand = "uninstall";
private PluginInitContext context; private PluginInitContext context;
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
List<Result> results = new List<Result>(); List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.Search)) if (string.IsNullOrEmpty(query.Search))
{ {
results.Add(new Result("install <pluginName>", "Images\\plugin.png", "search and install wox plugins") results.Add(ResultForListCommandAutoComplete(query));
{ results.Add(ResultForInstallCommandAutoComplete(query));
Action = e => ChangeToInstallCommand() results.Add(ResultForUninstallCommandAutoComplete(query));
});
results.Add(new Result("uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
{
Action = e => ChangeToUninstallCommand()
});
results.Add(new Result("list", "Images\\plugin.png", "list plugins installed")
{
Action = e => ChangeToListCommand()
});
return results; return results;
} }
if (!string.IsNullOrEmpty(query.FirstSearch)) string command = query.FirstSearch.ToLower();
if (string.IsNullOrEmpty(command)) return results;
if (command == ListCommand)
{ {
bool hit = false; return ResultForListInstalledPlugins();
switch (query.FirstSearch.ToLower()) }
{ if (command == UninstallCommand)
case "list": {
hit = true; return ResultForUnInstallPlugin(query);
results = ListInstalledPlugins(); }
break; if (command == InstallCommand)
{
return ResultForInstallPlugin(query);
}
case "uninstall": if (InstallCommand.Contains(command))
hit = true; {
results = UnInstallPlugins(query); results.Add(ResultForInstallCommandAutoComplete(query));
break; }
if (UninstallCommand.Contains(command))
case "install": {
hit = true; results.Add(ResultForUninstallCommandAutoComplete(query));
if (!string.IsNullOrEmpty(query.SecondSearch)) }
{ if (ListCommand.Contains(command))
results = InstallPlugin(query.SecondSearch); {
} results.Add(ResultForListCommandAutoComplete(query));
break;
}
if (!hit)
{
if ("install".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("install <pluginName>", "Images\\plugin.png", "search and install wox plugins")
{
Action = e => ChangeToInstallCommand()
});
}
if ("uninstall".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin")
{
Action = e => ChangeToUninstallCommand()
});
}
if ("list".Contains(query.FirstSearch.ToLower()))
{
results.Add(new Result("list", "Images\\plugin.png", "list plugins installed")
{
Action = e => ChangeToListCommand()
});
}
}
} }
return results; return results;
} }
private bool ChangeToListCommand() private Result ResultForListCommandAutoComplete(Query query)
{ {
if (context.CurrentPluginMetadata.ActionKeyword == "*") string title = ListCommand;
{ string subtitle = "list installed plugins";
context.API.ChangeQuery("list "); return ResultForCommand(query, ListCommand, title, subtitle);
}
else
{
context.API.ChangeQuery(string.Format("{0} list ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
} }
private bool ChangeToUninstallCommand() private Result ResultForInstallCommandAutoComplete(Query query)
{ {
if (context.CurrentPluginMetadata.ActionKeyword == "*") string title = $"{InstallCommand} <Package Name>";
{ string subtitle = "list installed plugins";
context.API.ChangeQuery("uninstall "); return ResultForCommand(query, InstallCommand, title, subtitle);
}
else
{
context.API.ChangeQuery(string.Format("{0} uninstall ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
} }
private bool ChangeToInstallCommand() private Result ResultForUninstallCommandAutoComplete(Query query)
{ {
if (context.CurrentPluginMetadata.ActionKeyword == "*") string title = $"{UninstallCommand} <Package Name>";
{ string subtitle = "list installed plugins";
context.API.ChangeQuery("install "); return ResultForCommand(query, UninstallCommand, title, subtitle);
}
else
{
context.API.ChangeQuery(string.Format("{0} install ", context.CurrentPluginMetadata.ActionKeyword));
}
return false;
} }
private List<Result> InstallPlugin(string queryPluginName) private Result ResultForCommand(Query query, string command, string title, string subtitle)
{
const string seperater = Plugin.Query.TermSeperater;
var result = new Result
{
Title = title,
IcoPath = "Images\\plugin.png",
SubTitle = subtitle,
Action = e =>
{
context.API.ChangeQuery($"{query.ActionKeyword}{seperater}{command}{seperater}");
return false;
}
};
return result;
}
private List<Result> ResultForInstallPlugin(Query query)
{ {
List<Result> results = new List<Result>(); List<Result> results = new List<Result>();
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + queryPluginName, context.Proxy); string pluginName = query.SecondSearch;
if (string.IsNullOrEmpty(pluginName)) return results;
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy);
Stream s = response.GetResponseStream(); Stream s = response.GetResponseStream();
if (s != null) if (s != null)
{ {
@@ -154,17 +129,17 @@ namespace Wox.Plugin.PluginManagement
foreach (WoxPluginResult r in searchedPlugins) foreach (WoxPluginResult r in searchedPlugins)
{ {
WoxPluginResult r1 = r; WoxPluginResult r1 = r;
results.Add(new Result() results.Add(new Result
{ {
Title = r.name, Title = r.name,
SubTitle = r.description, SubTitle = r.description,
IcoPath = "Images\\plugin.png", IcoPath = "Images\\plugin.png",
Action = e => Action = e =>
{ {
DialogResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin", MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButtons.YesNo); "Install plugin", MessageBoxButton.YesNo);
if (result == DialogResult.Yes) if (result == MessageBoxResult.Yes)
{ {
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload"); string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
@@ -201,7 +176,7 @@ namespace Wox.Plugin.PluginManagement
return results; return results;
} }
private List<Result> UnInstallPlugins(Query query) private List<Result> ResultForUnInstallPlugin(Query query)
{ {
List<Result> results = new List<Result>(); List<Result> results = new List<Result>();
List<PluginMetadata> allInstalledPlugins = context.API.GetAllPlugins().Select(o => o.Metadata).ToList(); List<PluginMetadata> allInstalledPlugins = context.API.GetAllPlugins().Select(o => o.Metadata).ToList();
@@ -213,15 +188,14 @@ namespace Wox.Plugin.PluginManagement
foreach (PluginMetadata plugin in allInstalledPlugins) foreach (PluginMetadata plugin in allInstalledPlugins)
{ {
var plugin1 = plugin; results.Add(new Result
results.Add(new Result()
{ {
Title = plugin.Name, Title = plugin.Name,
SubTitle = plugin.Description, SubTitle = plugin.Description,
IcoPath = plugin.FullIcoPath, IcoPath = plugin.FullIcoPath,
Action = e => Action = e =>
{ {
UnInstallPlugin(plugin1); UnInstallPlugin(plugin);
return false; return false;
} }
}); });
@@ -232,16 +206,16 @@ namespace Wox.Plugin.PluginManagement
private void UnInstallPlugin(PluginMetadata plugin) private void UnInstallPlugin(PluginMetadata plugin)
{ {
string content = string.Format("Do you want to uninstall following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", plugin.Name, plugin.Version, plugin.Author); string content = string.Format("Do you want to uninstall following plugin?\r\n\r\nName: {0}\r\nVersion: {1}\r\nAuthor: {2}", plugin.Name, plugin.Version, plugin.Author);
if (MessageBox.Show(content, "Wox", MessageBoxButtons.YesNo) == DialogResult.Yes) if (MessageBox.Show(content, "Wox", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{ {
File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close(); File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close();
if (MessageBox.Show( if (MessageBox.Show(
"You have uninstalled plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?", "You have uninstalled plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?",
"Install plugin", "Install plugin",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{ {
ProcessStartInfo Info = new ProcessStartInfo(); ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + Application.ExecutablePath + "\""; Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + Assembly.GetExecutingAssembly().Location + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden; Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true; Info.CreateNoWindow = true;
Info.FileName = "cmd.exe"; Info.FileName = "cmd.exe";
@@ -251,14 +225,15 @@ namespace Wox.Plugin.PluginManagement
} }
} }
private List<Result> ListInstalledPlugins() private List<Result> ResultForListInstalledPlugins()
{ {
List<Result> results = new List<Result>(); List<Result> results = new List<Result>();
foreach (PluginMetadata plugin in context.API.GetAllPlugins().Select(o => o.Metadata)) foreach (PluginMetadata plugin in context.API.GetAllPlugins().Select(o => o.Metadata))
{ {
results.Add(new Result() string actionKeywordString = string.Join(" or ", plugin.ActionKeywords);
results.Add(new Result
{ {
Title = plugin.Name + " - " + plugin.ActionKeyword, Title = $"{plugin.Name} - Action Keywords: {actionKeywordString}",
SubTitle = plugin.Description, SubTitle = plugin.Description,
IcoPath = plugin.FullIcoPath IcoPath = plugin.FullIcoPath
}); });

View File

@@ -38,9 +38,10 @@
<HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="PresentationFramework" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Presentation" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="HttpRequest.cs" /> <Compile Include="HttpRequest.cs" />
@@ -98,5 +99,4 @@
<Target Name="AfterBuild"> <Target Name="AfterBuild">
</Target> </Target>
--> -->
</Project> </Project>