PowerToys/Plugins/Wox.Plugin.PluginManagement/Main.cs

265 lines
10 KiB
C#
Raw Normal View History

2014-03-11 23:54:37 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2014-03-11 23:54:37 +08:00
using System.IO;
using System.Linq;
2014-03-13 22:31:44 +08:00
using System.Net;
2015-02-07 21:27:48 +08:00
using System.Reflection;
2014-03-13 22:31:44 +08:00
using System.Text;
using System.Threading;
using System.Windows;
2014-03-11 23:54:37 +08:00
using Newtonsoft.Json;
2014-07-18 20:00:55 +08:00
namespace Wox.Plugin.PluginManagement
{
public class Main : IPlugin, IPluginI18n
2014-07-18 20:00:55 +08:00
{
2014-12-14 22:24:05 +08:00
private static string APIBASE = "https://api.getwox.com";
2014-07-18 20:00:55 +08:00
private static string PluginConfigName = "plugin.json";
private static string pluginSearchUrl = APIBASE + "/plugin/search/";
private const string ListCommand = "list";
private const string InstallCommand = "install";
private const string UninstallCommand = "uninstall";
2014-07-18 20:00:55 +08:00
private PluginInitContext context;
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.Search))
2014-07-18 20:00:55 +08:00
{
results.Add(ResultForListCommandAutoComplete(query));
results.Add(ResultForInstallCommandAutoComplete(query));
results.Add(ResultForUninstallCommandAutoComplete(query));
2014-07-18 20:00:55 +08:00
return results;
}
string command = query.FirstSearch.ToLower();
if (string.IsNullOrEmpty(command)) return results;
2014-07-18 20:00:55 +08:00
if (command == ListCommand)
{
return ResultForListInstalledPlugins();
2014-07-18 20:00:55 +08:00
}
if (command == UninstallCommand)
{
return ResultForUnInstallPlugin(query);
}
if (command == InstallCommand)
{
return ResultForInstallPlugin(query);
}
if (InstallCommand.Contains(command))
{
results.Add(ResultForInstallCommandAutoComplete(query));
}
if (UninstallCommand.Contains(command))
{
results.Add(ResultForUninstallCommandAutoComplete(query));
}
if (ListCommand.Contains(command))
{
results.Add(ResultForListCommandAutoComplete(query));
}
return results;
}
private Result ResultForListCommandAutoComplete(Query query)
{
string title = ListCommand;
string subtitle = "list installed plugins";
return ResultForCommand(query, ListCommand, title, subtitle);
}
private Result ResultForInstallCommandAutoComplete(Query query)
{
string title = $"{InstallCommand} <Package Name>";
string subtitle = "list installed plugins";
return ResultForCommand(query, InstallCommand, title, subtitle);
}
private Result ResultForUninstallCommandAutoComplete(Query query)
{
string title = $"{UninstallCommand} <Package Name>";
string subtitle = "list installed plugins";
return ResultForCommand(query, UninstallCommand, title, subtitle);
}
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)
2014-07-18 20:00:55 +08:00
{
List<Result> results = new List<Result>();
string pluginName = query.SecondSearch;
if (string.IsNullOrEmpty(pluginName)) return results;
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy);
2014-07-18 20:00:55 +08:00
Stream s = response.GetResponseStream();
if (s != null)
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
string json = reader.ReadToEnd();
2014-12-14 22:24:05 +08:00
List<WoxPluginResult> searchedPlugins = null;
try
{
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
}
catch
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
2014-12-14 22:24:05 +08:00
return results;
}
foreach (WoxPluginResult r in searchedPlugins)
2014-07-18 20:00:55 +08:00
{
WoxPluginResult r1 = r;
results.Add(new Result
2014-07-18 20:00:55 +08:00
{
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
Action = e =>
{
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButton.YesNo);
2014-07-18 20:00:55 +08:00
if (result == MessageBoxResult.Yes)
2014-07-18 20:00:55 +08:00
{
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
context.API.StartLoadingBar();
ThreadPool.QueueUserWorkItem(delegate
{
using (WebClient Client = new WebClient())
{
try
{
2014-12-14 22:24:05 +08:00
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
Client.DownloadFile(pluginUrl, filePath);
2014-07-18 20:00:55 +08:00
context.API.InstallPlugin(filePath);
context.API.ReloadPlugins();
}
catch (Exception exception)
{
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
}
finally
{
2014-07-21 19:48:17 +08:00
context.API.StopLoadingBar();
2014-07-18 20:00:55 +08:00
}
}
});
}
return false;
}
});
}
}
return results;
}
private List<Result> ResultForUnInstallPlugin(Query query)
2014-07-18 20:00:55 +08:00
{
List<Result> results = new List<Result>();
List<PluginMetadata> allInstalledPlugins = context.API.GetAllPlugins().Select(o => o.Metadata).ToList();
if (!string.IsNullOrEmpty(query.SecondSearch))
2014-07-18 20:00:55 +08:00
{
allInstalledPlugins =
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(query.SecondSearch.ToLower())).ToList();
2014-07-18 20:00:55 +08:00
}
foreach (PluginMetadata plugin in allInstalledPlugins)
{
results.Add(new Result
2014-07-18 20:00:55 +08:00
{
Title = plugin.Name,
SubTitle = plugin.Description,
IcoPath = plugin.FullIcoPath,
2014-07-18 20:00:55 +08:00
Action = e =>
{
UnInstallPlugin(plugin);
2014-07-18 20:00:55 +08:00
return false;
}
});
}
return results;
}
private void UnInstallPlugin(PluginMetadata plugin)
2014-07-18 20:00:55 +08:00
{
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", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
2014-07-18 20:00:55 +08:00
{
File.Create(Path.Combine(plugin.PluginDirectory, "NeedDelete.txt")).Close();
if (MessageBox.Show(
"You have uninstalled plugin " + plugin.Name + " successfully.\r\n Restart Wox to take effect?",
"Install plugin",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 1 && \"" + Assembly.GetExecutingAssembly().Location + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
context.API.CloseApp();
}
2014-07-18 20:00:55 +08:00
}
}
private List<Result> ResultForListInstalledPlugins()
2014-07-18 20:00:55 +08:00
{
List<Result> results = new List<Result>();
foreach (PluginMetadata plugin in context.API.GetAllPlugins().Select(o => o.Metadata))
2014-07-18 20:00:55 +08:00
{
string actionKeywordString = string.Join(" or ", plugin.ActionKeywords.ToArray());
results.Add(new Result
2014-07-18 20:00:55 +08:00
{
Title = $"{plugin.Name} - Action Keywords: {actionKeywordString}",
2014-07-18 20:00:55 +08:00
SubTitle = plugin.Description,
IcoPath = plugin.FullIcoPath
2014-07-18 20:00:55 +08:00
});
}
return results;
}
public void Init(PluginInitContext context)
{
this.context = context;
}
2015-02-07 21:27:48 +08:00
public string GetLanguagesFolder()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
}
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("wox_plugin_plugin_management_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return context.API.GetTranslation("wox_plugin_plugin_management_plugin_description");
}
2014-07-18 20:00:55 +08:00
}
2014-03-11 23:54:37 +08:00
}