mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
implement WPM Plugin
This commit is contained in:
@@ -2,15 +2,37 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Wox.Plugin.PluginManagement
|
||||
{
|
||||
public class WoxPlugin
|
||||
{
|
||||
public int apiVersion { get; set; }
|
||||
public List<WoxPluginResult> result { get; set; }
|
||||
}
|
||||
|
||||
public class WoxPluginResult
|
||||
{
|
||||
public string downloadUrl;
|
||||
public string author;
|
||||
public string description;
|
||||
public string id;
|
||||
public string language;
|
||||
public int like;
|
||||
public string name;
|
||||
}
|
||||
|
||||
|
||||
public class Main : IPlugin
|
||||
{
|
||||
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
||||
private static string PluginConfigName = "plugin.json";
|
||||
private static string pluginSearchUrl = "http://www.getwox.com/api/plugin/search/";
|
||||
private PluginInitContext context;
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
@@ -20,12 +42,12 @@ namespace Wox.Plugin.PluginManagement
|
||||
{
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = "wpm list",
|
||||
SubTitle = "list plugins installed",
|
||||
Title = "wpm install <pluginName>",
|
||||
SubTitle = "search and install wox plugins",
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
context.ChangeQuery("wpm list");
|
||||
context.ChangeQuery("wpm install ");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
@@ -40,26 +62,147 @@ namespace Wox.Plugin.PluginManagement
|
||||
return false;
|
||||
}
|
||||
});
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = "wpm list",
|
||||
SubTitle = "list plugins installed",
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
context.ChangeQuery("wpm list");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
if (query.ActionParameters.Count > 0)
|
||||
{
|
||||
bool hit = false;
|
||||
switch (query.ActionParameters[0].ToLower())
|
||||
{
|
||||
case "list":
|
||||
hit = true;
|
||||
results = ListInstalledPlugins();
|
||||
break;
|
||||
|
||||
case "uninstall":
|
||||
hit = true;
|
||||
results = ListUnInstalledPlugins(query);
|
||||
break;
|
||||
|
||||
case "install":
|
||||
hit = true;
|
||||
if (query.ActionParameters.Count > 1)
|
||||
{
|
||||
results = InstallPlugin(query);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!hit)
|
||||
{
|
||||
if ("install".Contains(query.ActionParameters[0].ToLower()))
|
||||
{
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = "wpm install <pluginName>",
|
||||
SubTitle = "search and install wox plugins",
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
context.ChangeQuery("wpm install ");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
if ("uninstall".Contains(query.ActionParameters[0].ToLower()))
|
||||
{
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = "wpm uninstall <pluginName>",
|
||||
SubTitle = "uninstall plugin",
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
context.ChangeQuery("wpm uninstall ");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
if ("list".Contains(query.ActionParameters[0].ToLower()))
|
||||
{
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = "wpm list",
|
||||
SubTitle = "list plugins installed",
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
context.ChangeQuery("wpm list");
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> InstallPlugin(Query query)
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + query.ActionParameters[1], null, null, null);
|
||||
Stream s = response.GetResponseStream();
|
||||
if (s != null)
|
||||
{
|
||||
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
||||
string json = reader.ReadToEnd();
|
||||
WoxPlugin o = JsonConvert.DeserializeObject<WoxPlugin>(json);
|
||||
foreach (WoxPluginResult r in o.result)
|
||||
{
|
||||
WoxPluginResult r1 = r;
|
||||
results.Add(new Result()
|
||||
{
|
||||
Title = r.name,
|
||||
SubTitle = r.description,
|
||||
IcoPath = "Images\\plugin.png",
|
||||
Action = e =>
|
||||
{
|
||||
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
|
||||
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
|
||||
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
|
||||
|
||||
context.StartLoadingBar();
|
||||
ThreadPool.QueueUserWorkItem(delegate
|
||||
{
|
||||
using (WebClient Client = new WebClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
Client.DownloadFile(r1.downloadUrl, filePath);
|
||||
context.InstallPlugin(filePath);
|
||||
context.ReloadPlugins();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
context.StopLoadingBar();
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> ListUnInstalledPlugins(Query query)
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
@@ -90,10 +233,10 @@ namespace Wox.Plugin.PluginManagement
|
||||
|
||||
private void UnInstalledPlugins(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)
|
||||
{
|
||||
File.Create(Path.Combine(plugin.PluginDirecotry, "NeedDelete.txt"));
|
||||
File.Create(Path.Combine(plugin.PluginDirecotry, "NeedDelete.txt")).Close();
|
||||
MessageBox.Show("This plugin has been removed, restart Wox to take effect");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user