2014-03-11 23:54:37 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2014-03-13 22:31:44 +08:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-11-06 09:03:41 +08:00
|
|
|
|
using System.Windows;
|
2014-03-11 23:54:37 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
using Wox.Infrastructure.Http;
|
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-03-11 23:54:37 +08:00
|
|
|
|
|
2014-07-18 20:00:55 +08:00
|
|
|
|
namespace Wox.Plugin.PluginManagement
|
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
public class Main : IPlugin, IPluginI18n
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2015-12-10 03:25:29 +08:00
|
|
|
|
private static string APIBASE = "http://api.getwox.com";
|
2014-07-18 20:00:55 +08:00
|
|
|
|
private static string PluginConfigName = "plugin.json";
|
2015-01-26 17:46:55 +08:00
|
|
|
|
private static string pluginSearchUrl = APIBASE + "/plugin/search/";
|
2015-11-06 09:03:41 +08:00
|
|
|
|
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>();
|
2015-11-06 09:03:41 +08:00
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
if (string.IsNullOrEmpty(query.Search))
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
results.Add(ResultForListCommandAutoComplete(query));
|
|
|
|
|
results.Add(ResultForInstallCommandAutoComplete(query));
|
|
|
|
|
results.Add(ResultForUninstallCommandAutoComplete(query));
|
2014-07-18 20:00:55 +08:00
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
string command = query.FirstSearch.ToLower();
|
|
|
|
|
if (string.IsNullOrEmpty(command)) return results;
|
2014-07-18 20:00:55 +08:00
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (command == ListCommand)
|
|
|
|
|
{
|
|
|
|
|
return ResultForListInstalledPlugins();
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (command == UninstallCommand)
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
return ResultForUnInstallPlugin(query);
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (command == InstallCommand)
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
return ResultForInstallPlugin(query);
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (InstallCommand.Contains(command))
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
results.Add(ResultForInstallCommandAutoComplete(query));
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (UninstallCommand.Contains(command))
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
results.Add(ResultForUninstallCommandAutoComplete(query));
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
2015-11-06 09:03:41 +08:00
|
|
|
|
if (ListCommand.Contains(command))
|
|
|
|
|
{
|
|
|
|
|
results.Add(ResultForListCommandAutoComplete(query));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
private Result ResultForListCommandAutoComplete(Query query)
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
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
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
Title = title,
|
|
|
|
|
IcoPath = "Images\\plugin.png",
|
|
|
|
|
SubTitle = subtitle,
|
|
|
|
|
Action = e =>
|
|
|
|
|
{
|
|
|
|
|
context.API.ChangeQuery($"{query.ActionKeyword}{seperater}{command}{seperater}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return result;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
private List<Result> ResultForInstallPlugin(Query query)
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
2015-11-06 09:03:41 +08:00
|
|
|
|
string pluginName = query.SecondSearch;
|
|
|
|
|
if (string.IsNullOrEmpty(pluginName)) return results;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
string json;
|
|
|
|
|
try
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2016-06-19 23:18:43 +08:00
|
|
|
|
json = Http.Get(pluginSearchUrl + pluginName).Result;
|
2016-05-11 03:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
//todo happlebao add option in log to decide give user prompt or not
|
|
|
|
|
context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Can't connect to Wox plugin website, check your conenction");
|
|
|
|
|
Log.Exception("|PluginManagement.ResultForInstallPlugin|Can't connect to Wox plugin website, check your conenction", e);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
|
|
|
|
List<WoxPluginResult> searchedPlugins;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
|
|
|
|
|
}
|
2017-01-24 08:24:20 +08:00
|
|
|
|
catch (JsonSerializationException e)
|
2016-05-11 03:26:47 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
context.API.ShowMsg("PluginManagement.ResultForInstallPlugin: Coundn't parse api search results, Please update your Wox!");
|
|
|
|
|
Log.Exception("|PluginManagement.ResultForInstallPlugin|Coundn't parse api search results, Please update your Wox!", e);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return results;
|
|
|
|
|
}
|
2014-12-14 22:24:05 +08:00
|
|
|
|
|
2016-05-11 03:26:47 +08:00
|
|
|
|
foreach (WoxPluginResult r in searchedPlugins)
|
|
|
|
|
{
|
|
|
|
|
WoxPluginResult r1 = r;
|
|
|
|
|
results.Add(new Result
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2016-05-11 03:26:47 +08:00
|
|
|
|
Title = r.name,
|
|
|
|
|
SubTitle = r.description,
|
|
|
|
|
IcoPath = "Images\\plugin.png",
|
|
|
|
|
Action = c =>
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2016-05-11 03:26:47 +08:00
|
|
|
|
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
|
|
|
|
|
"Install plugin", MessageBoxButton.YesNo);
|
|
|
|
|
|
|
|
|
|
if (result == MessageBoxResult.Yes)
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2016-05-11 03:26:47 +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");
|
2014-07-18 20:00:55 +08:00
|
|
|
|
|
2016-05-11 03:26:47 +08:00
|
|
|
|
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-06-19 23:18:43 +08:00
|
|
|
|
Http.Download(pluginUrl, filePath);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
context.API.ShowMsg($"PluginManagement.ResultForInstallPlugin: download failed for <{r.name}>");
|
|
|
|
|
Log.Exception($"|PluginManagement.ResultForInstallPlugin|download failed for <{r.name}>", e);
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return false;
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
2016-05-11 03:26:47 +08:00
|
|
|
|
context.API.InstallPlugin(filePath);
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
2016-05-11 03:26:47 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
private List<Result> ResultForUnInstallPlugin(Query query)
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
2015-01-26 17:46:55 +08:00
|
|
|
|
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 =
|
2015-01-26 17:46:55 +08:00
|
|
|
|
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(query.SecondSearch.ToLower())).ToList();
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (PluginMetadata plugin in allInstalledPlugins)
|
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
results.Add(new Result
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
Title = plugin.Name,
|
|
|
|
|
SubTitle = plugin.Description,
|
2016-05-04 04:18:26 +08:00
|
|
|
|
IcoPath = plugin.IcoPath,
|
2014-07-18 20:00:55 +08:00
|
|
|
|
Action = e =>
|
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
UnInstallPlugin(plugin);
|
2014-07-18 20:00:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
private void UnInstallPlugin(PluginMetadata plugin)
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2015-11-26 10:04:44 +08:00
|
|
|
|
string content = $"Do you want to uninstall following plugin?{Environment.NewLine}{Environment.NewLine}" +
|
|
|
|
|
$"Name: {plugin.Name}{Environment.NewLine}" +
|
|
|
|
|
$"Version: {plugin.Version}{Environment.NewLine}" +
|
|
|
|
|
$"Author: {plugin.Author}";
|
2015-11-06 09:03:41 +08:00
|
|
|
|
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();
|
2015-11-26 10:04:44 +08:00
|
|
|
|
var result = MessageBox.Show($"You have uninstalled plugin {plugin.Name} successfully.{Environment.NewLine}" +
|
|
|
|
|
"Restart Wox to take effect?",
|
|
|
|
|
"Install plugin", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
|
|
if (result == MessageBoxResult.Yes)
|
2015-02-07 21:40:25 +08:00
|
|
|
|
{
|
2015-11-26 10:04:44 +08:00
|
|
|
|
context.API.RestarApp();
|
2015-02-07 21:40:25 +08:00
|
|
|
|
}
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 09:03:41 +08:00
|
|
|
|
private List<Result> ResultForListInstalledPlugins()
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
List<Result> results = new List<Result>();
|
2015-01-26 17:46:55 +08:00
|
|
|
|
foreach (PluginMetadata plugin in context.API.GetAllPlugins().Select(o => o.Metadata))
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2015-11-06 10:29:32 +08:00
|
|
|
|
string actionKeywordString = string.Join(" or ", plugin.ActionKeywords.ToArray());
|
2015-11-06 09:03:41 +08:00
|
|
|
|
results.Add(new Result
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
2015-11-06 09:03:41 +08:00
|
|
|
|
Title = $"{plugin.Name} - Action Keywords: {actionKeywordString}",
|
2014-07-18 20:00:55 +08:00
|
|
|
|
SubTitle = plugin.Description,
|
2016-05-04 04:18:26 +08:00
|
|
|
|
IcoPath = plugin.IcoPath
|
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 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
|
|
|
|
}
|