mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-13 19:19:23 +08:00
Cleaned FileSystemPlugin + Enhanced
THIS NEEDS TESTING!
This commit is contained in:
parent
498634fd13
commit
81467a86cc
@ -8,319 +8,246 @@ using System.Threading;
|
|||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Wox.Plugin.PluginManagement
|
namespace Wox.Plugin.PluginManagement {
|
||||||
{
|
public class WoxPlugin {
|
||||||
public class WoxPlugin
|
public int apiVersion { get; set; }
|
||||||
{
|
public List<WoxPluginResult> result { get; set; }
|
||||||
public int apiVersion { get; set; }
|
}
|
||||||
public List<WoxPluginResult> result { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class WoxPluginResult
|
public class WoxPluginResult {
|
||||||
{
|
public string actionkeyword;
|
||||||
public string actionkeyword;
|
public string download;
|
||||||
public string download;
|
public string author;
|
||||||
public string author;
|
public string description;
|
||||||
public string description;
|
public string id;
|
||||||
public string id;
|
public int star;
|
||||||
public int star;
|
public string name;
|
||||||
public string name;
|
public string version;
|
||||||
public string version;
|
public string website;
|
||||||
public string website;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public class Main : IPlugin
|
public class Main : IPlugin {
|
||||||
{
|
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
||||||
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
private static string PluginConfigName = "plugin.json";
|
||||||
private static string PluginConfigName = "plugin.json";
|
private static string pluginSearchUrl = "http://www.getwox.com/api/plugin/search/";
|
||||||
private static string pluginSearchUrl = "http://www.getwox.com/api/plugin/search/";
|
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 (query.ActionParameters.Count == 0) {
|
||||||
if (query.ActionParameters.Count == 0)
|
results.Add(new Result("wpm install <pluginName>", "Images\\plugin.png", "search and install wox plugins") {
|
||||||
{
|
Action = e => {
|
||||||
results.Add(new Result()
|
context.ChangeQuery("wpm install ");
|
||||||
{
|
return false;
|
||||||
Title = "wpm install <pluginName>",
|
}
|
||||||
SubTitle = "search and install wox plugins",
|
});
|
||||||
IcoPath = "Images\\plugin.png",
|
results.Add(new Result("wpm uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin") {
|
||||||
Action = e =>
|
Action = e => {
|
||||||
{
|
context.ChangeQuery("wpm uninstall ");
|
||||||
context.ChangeQuery("wpm install ");
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
});
|
||||||
});
|
results.Add(new Result("wpm list", "Images\\plugin.png", "list plugins installed") {
|
||||||
results.Add(new Result()
|
Action = e => {
|
||||||
{
|
context.ChangeQuery("wpm list");
|
||||||
Title = "wpm uninstall <pluginName>",
|
return false;
|
||||||
SubTitle = "uninstall plugin",
|
}
|
||||||
IcoPath = "Images\\plugin.png",
|
});
|
||||||
Action = e =>
|
return results;
|
||||||
{
|
}
|
||||||
context.ChangeQuery("wpm uninstall ");
|
|
||||||
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)
|
if (query.ActionParameters.Count > 0) {
|
||||||
{
|
bool hit = false;
|
||||||
bool hit = false;
|
switch (query.ActionParameters[0].ToLower()) {
|
||||||
switch (query.ActionParameters[0].ToLower())
|
case "list":
|
||||||
{
|
hit = true;
|
||||||
case "list":
|
results = ListInstalledPlugins();
|
||||||
hit = true;
|
break;
|
||||||
results = ListInstalledPlugins();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "uninstall":
|
case "uninstall":
|
||||||
hit = true;
|
hit = true;
|
||||||
results = ListUnInstalledPlugins(query);
|
results = ListUnInstalledPlugins(query);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "install":
|
case "install":
|
||||||
hit = true;
|
hit = true;
|
||||||
if (query.ActionParameters.Count > 1)
|
if (query.ActionParameters.Count > 1) {
|
||||||
{
|
results = InstallPlugin(query);
|
||||||
results = InstallPlugin(query);
|
}
|
||||||
}
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!hit)
|
if (!hit) {
|
||||||
{
|
if ("install".Contains(query.ActionParameters[0].ToLower())) {
|
||||||
if ("install".Contains(query.ActionParameters[0].ToLower()))
|
results.Add(new Result("wpm install <pluginName>", "Images\\plugin.png", "search and install wox plugins") {
|
||||||
{
|
Action = e => {
|
||||||
results.Add(new Result()
|
context.ChangeQuery("wpm install ");
|
||||||
{
|
return false;
|
||||||
Title = "wpm install <pluginName>",
|
}
|
||||||
SubTitle = "search and install wox plugins",
|
});
|
||||||
IcoPath = "Images\\plugin.png",
|
}
|
||||||
Action = e =>
|
if ("uninstall".Contains(query.ActionParameters[0].ToLower())) {
|
||||||
{
|
results.Add(new Result("wpm uninstall <pluginName>", "Images\\plugin.png", "uninstall plugin") {
|
||||||
context.ChangeQuery("wpm install ");
|
Action = e => {
|
||||||
return false;
|
context.ChangeQuery("wpm uninstall ");
|
||||||
}
|
return false;
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
if ("uninstall".Contains(query.ActionParameters[0].ToLower()))
|
}
|
||||||
{
|
if ("list".Contains(query.ActionParameters[0].ToLower())) {
|
||||||
results.Add(new Result()
|
results.Add(new Result("wpm list", "Images\\plugin.png", "list plugins installed") {
|
||||||
{
|
Action = e => {
|
||||||
Title = "wpm uninstall <pluginName>",
|
context.ChangeQuery("wpm list");
|
||||||
SubTitle = "uninstall plugin",
|
return false;
|
||||||
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;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Result> InstallPlugin(Query query)
|
private List<Result> InstallPlugin(Query query) {
|
||||||
{
|
List<Result> results = new List<Result>();
|
||||||
List<Result> results = new List<Result>();
|
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + query.ActionParameters[1], null, null, null);
|
||||||
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + query.ActionParameters[1], null, null, null);
|
Stream s = response.GetResponseStream();
|
||||||
Stream s = response.GetResponseStream();
|
if (s != null) {
|
||||||
if (s != null)
|
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
||||||
{
|
string json = reader.ReadToEnd();
|
||||||
StreamReader reader = new StreamReader(s, Encoding.UTF8);
|
WoxPlugin o = JsonConvert.DeserializeObject<WoxPlugin>(json);
|
||||||
string json = reader.ReadToEnd();
|
foreach (WoxPluginResult r in o.result) {
|
||||||
WoxPlugin o = JsonConvert.DeserializeObject<WoxPlugin>(json);
|
WoxPluginResult r1 = r;
|
||||||
foreach (WoxPluginResult r in o.result)
|
results.Add(new Result() {
|
||||||
{
|
Title = r.name,
|
||||||
WoxPluginResult r1 = r;
|
SubTitle = r.description,
|
||||||
results.Add(new Result()
|
IcoPath = "Images\\plugin.png",
|
||||||
{
|
Action = e => {
|
||||||
Title = r.name,
|
DialogResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
|
||||||
SubTitle = r.description,
|
"Install plugin", MessageBoxButtons.YesNo);
|
||||||
IcoPath = "Images\\plugin.png",
|
|
||||||
Action = e =>
|
|
||||||
{
|
|
||||||
DialogResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
|
|
||||||
"Install plugin", MessageBoxButtons.YesNo);
|
|
||||||
|
|
||||||
if (result == DialogResult.Yes)
|
if (result == DialogResult.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);
|
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
|
||||||
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
|
|
||||||
|
|
||||||
context.StartLoadingBar();
|
context.StartLoadingBar();
|
||||||
ThreadPool.QueueUserWorkItem(delegate
|
ThreadPool.QueueUserWorkItem(delegate {
|
||||||
{
|
using (WebClient Client = new WebClient()) {
|
||||||
using (WebClient Client = new WebClient())
|
try {
|
||||||
{
|
Client.DownloadFile(r1.download, filePath);
|
||||||
try
|
context.InstallPlugin(filePath);
|
||||||
{
|
context.ReloadPlugins();
|
||||||
Client.DownloadFile(r1.download, filePath);
|
}
|
||||||
context.InstallPlugin(filePath);
|
catch (Exception exception) {
|
||||||
context.ReloadPlugins();
|
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
finally {
|
||||||
{
|
context.StopLoadingBar();
|
||||||
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
|
}
|
||||||
}
|
}
|
||||||
finally
|
});
|
||||||
{
|
}
|
||||||
context.StopLoadingBar();
|
return false;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return results;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Result> ListUnInstalledPlugins(Query query)
|
private List<Result> ListUnInstalledPlugins(Query query) {
|
||||||
{
|
List<Result> results = new List<Result>();
|
||||||
List<Result> results = new List<Result>();
|
List<PluginMetadata> allInstalledPlugins = ParseThirdPartyPlugins();
|
||||||
List<PluginMetadata> allInstalledPlugins = ParseThirdPartyPlugins();
|
if (query.ActionParameters.Count > 1) {
|
||||||
if (query.ActionParameters.Count > 1)
|
string pluginName = query.ActionParameters[1];
|
||||||
{
|
allInstalledPlugins =
|
||||||
string pluginName = query.ActionParameters[1];
|
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(pluginName.ToLower())).ToList();
|
||||||
allInstalledPlugins =
|
}
|
||||||
allInstalledPlugins.Where(o => o.Name.ToLower().Contains(pluginName.ToLower())).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (PluginMetadata plugin in allInstalledPlugins)
|
foreach (PluginMetadata plugin in allInstalledPlugins) {
|
||||||
{
|
results.Add(new Result() {
|
||||||
results.Add(new Result()
|
Title = plugin.Name,
|
||||||
{
|
SubTitle = plugin.Description,
|
||||||
Title = plugin.Name,
|
IcoPath = "Images\\plugin.png",
|
||||||
SubTitle = plugin.Description,
|
Action = e => {
|
||||||
IcoPath = "Images\\plugin.png",
|
UnInstalledPlugins(plugin);
|
||||||
Action = e =>
|
return false;
|
||||||
{
|
}
|
||||||
UnInstalledPlugins(plugin);
|
});
|
||||||
return false;
|
}
|
||||||
}
|
return results;
|
||||||
});
|
}
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UnInstalledPlugins(PluginMetadata plugin)
|
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) {
|
||||||
if (MessageBox.Show(content, "Wox", MessageBoxButtons.YesNo) == DialogResult.Yes)
|
File.Create(Path.Combine(plugin.PluginDirecotry, "NeedDelete.txt")).Close();
|
||||||
{
|
MessageBox.Show("This plugin has been removed, restart Wox to take effect");
|
||||||
File.Create(Path.Combine(plugin.PluginDirecotry, "NeedDelete.txt")).Close();
|
}
|
||||||
MessageBox.Show("This plugin has been removed, restart Wox to take effect");
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Result> ListInstalledPlugins()
|
private List<Result> ListInstalledPlugins() {
|
||||||
{
|
List<Result> results = new List<Result>();
|
||||||
List<Result> results = new List<Result>();
|
foreach (PluginMetadata plugin in ParseThirdPartyPlugins()) {
|
||||||
foreach (PluginMetadata plugin in ParseThirdPartyPlugins())
|
results.Add(new Result() {
|
||||||
{
|
Title = plugin.Name + " - " + plugin.ActionKeyword,
|
||||||
results.Add(new Result()
|
SubTitle = plugin.Description,
|
||||||
{
|
IcoPath = "Images\\plugin.png"
|
||||||
Title = plugin.Name + " - " + plugin.ActionKeyword,
|
});
|
||||||
SubTitle = plugin.Description,
|
}
|
||||||
IcoPath = "Images\\plugin.png"
|
return results;
|
||||||
});
|
}
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<PluginMetadata> ParseThirdPartyPlugins()
|
private static List<PluginMetadata> ParseThirdPartyPlugins() {
|
||||||
{
|
List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||||
List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
if (!Directory.Exists(PluginPath))
|
||||||
if (!Directory.Exists(PluginPath))
|
Directory.CreateDirectory(PluginPath);
|
||||||
Directory.CreateDirectory(PluginPath);
|
|
||||||
|
|
||||||
string[] directories = Directory.GetDirectories(PluginPath);
|
string[] directories = Directory.GetDirectories(PluginPath);
|
||||||
foreach (string directory in directories)
|
foreach (string directory in directories) {
|
||||||
{
|
PluginMetadata metadata = GetMetadataFromJson(directory);
|
||||||
PluginMetadata metadata = GetMetadataFromJson(directory);
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return pluginMetadatas;
|
return pluginMetadatas;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
private static PluginMetadata GetMetadataFromJson(string pluginDirectory) {
|
||||||
{
|
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
PluginMetadata metadata;
|
||||||
PluginMetadata metadata;
|
|
||||||
|
|
||||||
if (!File.Exists(configPath))
|
if (!File.Exists(configPath)) {
|
||||||
{
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
metadata.PluginType = PluginType.ThirdParty;
|
||||||
metadata.PluginType = PluginType.ThirdParty;
|
metadata.PluginDirecotry = pluginDirectory;
|
||||||
metadata.PluginDirecotry = pluginDirectory;
|
}
|
||||||
}
|
catch (Exception) {
|
||||||
catch (Exception)
|
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
||||||
{
|
return null;
|
||||||
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
if (!AllowedLanguage.IsAllowed(metadata.Language)) {
|
||||||
{
|
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
|
||||||
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
|
metadata.Language);
|
||||||
metadata.Language);
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
if (!File.Exists(metadata.ExecuteFilePath)) {
|
||||||
if (!File.Exists(metadata.ExecuteFilePath))
|
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
|
||||||
{
|
metadata.ExecuteFilePath);
|
||||||
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
|
return null;
|
||||||
metadata.ExecuteFilePath);
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return metadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(PluginInitContext context)
|
public void Init(PluginInitContext context) {
|
||||||
{
|
this.context = context;
|
||||||
this.context = context;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,8 @@ namespace Python.Runtime {
|
|||||||
PyTrue = Runtime.PyObject_GetAttrString(op, "True");
|
PyTrue = Runtime.PyObject_GetAttrString(op, "True");
|
||||||
PyFalse = Runtime.PyObject_GetAttrString(op, "False");
|
PyFalse = Runtime.PyObject_GetAttrString(op, "False");
|
||||||
|
|
||||||
PyBoolType = Runtime.PyObject_Type(PyTrue);
|
PyBoolType = Runtime.PyObject_Type(PyTrue);> Python.Runtime.Runtime.Initialize() C#
|
||||||
|
|
||||||
PyNoneType = Runtime.PyObject_Type(PyNone);
|
PyNoneType = Runtime.PyObject_Type(PyNone);
|
||||||
PyTypeType = Runtime.PyObject_Type(PyNoneType);
|
PyTypeType = Runtime.PyObject_Type(PyNoneType);
|
||||||
|
|
||||||
|
@ -3,139 +3,82 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Wox.Infrastructure;
|
using Wox.Infrastructure;
|
||||||
using Wox.Infrastructure.Storage.UserSettings;
|
using Wox.Infrastructure.Storage.UserSettings;
|
||||||
|
|
||||||
namespace Wox.Plugin.SystemPlugins.FileSystem {
|
namespace Wox.Plugin.SystemPlugins.FileSystem {
|
||||||
|
|
||||||
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider {
|
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider {
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
|
||||||
private PluginInitContext context;
|
private PluginInitContext context;
|
||||||
private static List<string> driverNames = null;
|
private static List<string> driverNames = null;
|
||||||
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
||||||
|
public override string Description { get { return base.Description; } }
|
||||||
|
public override string Name { get { return "File System"; } }
|
||||||
|
public override string IcoPath { get { return @"Images\folder.png"; } }
|
||||||
|
|
||||||
|
#endregion Properties
|
||||||
|
|
||||||
|
#region Misc
|
||||||
|
|
||||||
|
protected override void InitInternal(PluginInitContext context) {
|
||||||
|
this.context = context;
|
||||||
|
|
||||||
|
if (UserSettingStorage.Instance.FolderLinks == null) {
|
||||||
|
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
||||||
|
UserSettingStorage.Instance.Save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public System.Windows.Controls.Control CreateSettingPanel() {
|
||||||
|
return new FileSystemSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Misc
|
||||||
|
|
||||||
protected override List<Result> QueryInternal(Query query) {
|
protected override List<Result> QueryInternal(Query query) {
|
||||||
|
|
||||||
|
#region 1
|
||||||
|
|
||||||
//TODO: Consider always clearing the cache
|
//TODO: Consider always clearing the cache
|
||||||
List<Result> results = new List<Result>();
|
List<Result> results = new List<Result>();
|
||||||
if (string.IsNullOrEmpty(query.RawQuery)) {
|
//if (string.IsNullOrEmpty(query.RawQuery)) {
|
||||||
// clear the cache
|
// // clear the cache
|
||||||
if (parentDirectories.Count > 0)
|
// if (parentDirectories.Count > 0) parentDirectories.Clear();
|
||||||
parentDirectories.Clear();
|
// return results;
|
||||||
|
//}
|
||||||
|
|
||||||
return results;
|
#endregion 1
|
||||||
}
|
|
||||||
|
|
||||||
InitialDriverList();
|
|
||||||
|
|
||||||
var input = query.RawQuery.ToLower();
|
var input = query.RawQuery.ToLower();
|
||||||
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
||||||
|
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x => x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
var currentPath = link == null ? input : link.Path + input.Remove(0, inputName.Length);
|
||||||
|
|
||||||
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
|
InitialDriverList();
|
||||||
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
|
||||||
var currentPath = link != null ? link.Path : null;
|
|
||||||
|
|
||||||
foreach (var item in UserSettingStorage.Instance.FolderLinks) {
|
foreach (var item in UserSettingStorage.Instance.FolderLinks.Where(x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase))) {
|
||||||
var Name = item.Nickname;
|
//if (item.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase)) { //&& item.Nickname.Length != input.Length) {
|
||||||
|
results.Add(new Result(item.Nickname, "Images/folder.png") {
|
||||||
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length) {
|
Action = (c) => {
|
||||||
Result result = new Result {
|
context.ChangeQuery(item.Nickname);
|
||||||
Title = Name,
|
return false;
|
||||||
IcoPath = "Images/folder.png",
|
}
|
||||||
Action = (c) => {
|
});
|
||||||
context.ChangeQuery(item.Nickname);
|
//}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
results.Add(result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentPath == null) {
|
if (link == null && !driverNames.Any(input.StartsWith))
|
||||||
if (!driverNames.Any(input.StartsWith))
|
return results;
|
||||||
return results;
|
|
||||||
|
|
||||||
currentPath = input;
|
QueryInternal_Directory_Exists(currentPath, input, results);
|
||||||
}
|
|
||||||
else
|
|
||||||
currentPath += input.Remove(0, inputName.Length);
|
|
||||||
|
|
||||||
if (Directory.Exists(currentPath)) {
|
return results;
|
||||||
// show all child directory
|
|
||||||
if (input.EndsWith("\\") || input.EndsWith("/")) {
|
|
||||||
var dirInfo = new DirectoryInfo(currentPath);
|
|
||||||
var dirs = dirInfo.GetDirectories();
|
|
||||||
|
|
||||||
var parentDirKey = input.TrimEnd('\\', '/');
|
|
||||||
if (!parentDirectories.ContainsKey(parentDirKey))
|
|
||||||
parentDirectories.Add(parentDirKey, dirs);
|
|
||||||
|
|
||||||
foreach (var dir in dirs) {
|
|
||||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var dirPath = dir.FullName;
|
|
||||||
Result result = new Result {
|
|
||||||
Title = dir.Name,
|
|
||||||
IcoPath = "Images/folder.png",
|
|
||||||
Action = (c) => {
|
|
||||||
context.ChangeQuery(dirPath);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
results.Add(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (results.Count == 0) {
|
|
||||||
Result result = new Result {
|
|
||||||
Title = "Open this directory",
|
|
||||||
SubTitle = "No files in this directory",
|
|
||||||
IcoPath = "Images/folder.png",
|
|
||||||
Action = (c) => {
|
|
||||||
Process.Start(currentPath);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
results.Add(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var dir in dirInfo.GetFiles()) {
|
|
||||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var dirPath = dir.FullName;
|
|
||||||
Result result = new Result {
|
|
||||||
Title = global::System.IO.Path.GetFileNameWithoutExtension(dirPath),
|
|
||||||
IcoPath = dirPath,
|
|
||||||
Action = (c) => {
|
|
||||||
try {
|
|
||||||
Process.Start(dirPath);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
MessageBox.Show(ex.Message, "Could not start " + dir.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
results.Add(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Result result = new Result {
|
|
||||||
Title = "Open this directory",
|
|
||||||
SubTitle = string.Format("path: {0}", currentPath),
|
|
||||||
Score = 50,
|
|
||||||
IcoPath = "Images/folder.png",
|
|
||||||
Action = (c) => {
|
|
||||||
Process.Start(currentPath);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
results.Add(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/*
|
||||||
// change to search in current directory
|
// change to search in current directory
|
||||||
string parentDir = null;
|
string parentDir = null;
|
||||||
try {
|
try {
|
||||||
@ -143,38 +86,37 @@ namespace Wox.Plugin.SystemPlugins.FileSystem {
|
|||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return results;
|
||||||
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0) {
|
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0) {
|
||||||
parentDir = parentDir.TrimEnd('\\', '/');
|
parentDir = parentDir.TrimEnd('\\', '/');
|
||||||
if (parentDirectories.ContainsKey(parentDir)) {
|
//TODO: Why are we doing the following check
|
||||||
|
|
||||||
var dirs = parentDirectories[parentDir];
|
//FUCK THIS CODE o.O!!!!!!!
|
||||||
var queryFileName = Path.GetFileName(currentPath).ToLower();
|
|
||||||
var fuzzy = FuzzyMatcher.Create(queryFileName);
|
if (parentDirectories.ContainsKey(parentDir)) {
|
||||||
foreach (var dir in dirs) {
|
var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower());
|
||||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
foreach (var dir in parentDirectories[parentDir]) {
|
||||||
continue;
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
||||||
|
|
||||||
var matchResult = fuzzy.Evaluate(dir.Name);
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||||
if (!matchResult.Success)
|
if (!matchResult.Success) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
var dirPath = dir.FullName;
|
results.Add(new Result(dir.Name, "Images/folder.png") {
|
||||||
Result result = new Result {
|
|
||||||
Title = dir.Name,
|
|
||||||
IcoPath = "Images/folder.png",
|
|
||||||
Score = matchResult.Score,
|
Score = matchResult.Score,
|
||||||
Action = (c) => {
|
Action = (c) => {
|
||||||
context.ChangeQuery(dirPath);
|
context.ChangeQuery(dir.FullName);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
results.Add(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitialDriverList() {
|
private void InitialDriverList() {
|
||||||
@ -187,30 +129,103 @@ namespace Wox.Plugin.SystemPlugins.FileSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void InitInternal(PluginInitContext context) {
|
private void QueryInternal_Directory_Exists(string currentPath, string input, List<Result> results) {
|
||||||
this.context = context;
|
string path = Directory.Exists(currentPath) ? new DirectoryInfo(currentPath).FullName : Path.GetDirectoryName(input);
|
||||||
|
|
||||||
if (UserSettingStorage.Instance.FolderLinks == null) {
|
if (path != null) {
|
||||||
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
var dirs = new DirectoryInfo(path).GetDirectories();
|
||||||
UserSettingStorage.Instance.Save();
|
|
||||||
|
var parentDirKey = input.TrimEnd('\\', '/');
|
||||||
|
if (!parentDirectories.ContainsKey(parentDirKey)) parentDirectories.Add(parentDirKey, dirs);
|
||||||
|
|
||||||
|
var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower());
|
||||||
|
foreach (var dir in dirs) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) {
|
||||||
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
||||||
|
|
||||||
|
var result = new Result(dir.Name, "Images/folder.png") {
|
||||||
|
Action = (c) => {
|
||||||
|
context.ChangeQuery(dir.FullName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Path.GetFileName(currentPath).ToLower() != "") {
|
||||||
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||||
|
result.Score = matchResult.Score;
|
||||||
|
if (!matchResult.Success) continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
results.Add(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (results.Count == 0) {
|
||||||
|
// results.Add(new Result("Open this directory", "Images/folder.png", "No files in this directory") {
|
||||||
|
// Action = (c) => {
|
||||||
|
// Process.Start(currentPath);
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//results.Add(new Result("Open this directory", "Images/folder.png", string.Format("path: {0}", currentPath)) {
|
||||||
|
// Score = 50,
|
||||||
|
// Action = (c) => {
|
||||||
|
// Process.Start(currentPath);
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************/
|
||||||
|
/**************************************************************/
|
||||||
|
|
||||||
|
if (results.Count == 0) {
|
||||||
|
results.Add(new Result("Open this directory", "Images/folder.png", "No files in this directory") {
|
||||||
|
Action = (c) => {
|
||||||
|
Process.Start(currentPath);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************/
|
||||||
|
/**************************************************************/
|
||||||
|
|
||||||
|
var Folder = Path.GetDirectoryName(currentPath);
|
||||||
|
if (Folder != null) {
|
||||||
|
var dirInfo1 = new DirectoryInfo(Folder);
|
||||||
|
var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower());
|
||||||
|
foreach (var dir in dirInfo1.GetFiles()) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) {
|
||||||
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
||||||
|
|
||||||
|
var dirPath = dir.FullName;
|
||||||
|
Result result = new Result(System.IO.Path.GetFileNameWithoutExtension(dirPath), dirPath) {
|
||||||
|
Action = (c) => {
|
||||||
|
try {
|
||||||
|
Process.Start(dirPath);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
MessageBox.Show(ex.Message, "Could not start " + dir.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Path.GetFileName(currentPath).ToLower() != "") {
|
||||||
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||||
|
result.Score = matchResult.Score;
|
||||||
|
if (!matchResult.Success) continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
results.Add(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Name {
|
|
||||||
get { return "File System"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string IcoPath {
|
|
||||||
get { return @"Images\folder.png"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public System.Windows.Controls.Control CreateSettingPanel() {
|
|
||||||
return new FileSystemSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string Description {
|
|
||||||
get { return base.Description; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,47 +2,56 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Wox.Plugin
|
namespace Wox.Plugin {
|
||||||
{
|
public class Result {
|
||||||
public class Result
|
public string Title { get; set; }
|
||||||
{
|
public string SubTitle { get; set; }
|
||||||
public string Title { get; set; }
|
public string IcoPath { get; set; }
|
||||||
public string SubTitle { get; set; }
|
|
||||||
public string IcoPath { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// return true to hide wox after select result
|
/// return true to hide wox after select result
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<ActionContext,bool> Action { get; set; }
|
public Func<ActionContext, bool> Action { get; set; }
|
||||||
public int Score { get; set; }
|
public int Score { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Auto add scores for MRU items
|
/// Auto add scores for MRU items
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AutoAjustScore { get; set; }
|
public bool AutoAjustScore { get; set; }
|
||||||
|
|
||||||
//todo: this should be controlled by system, not visible to users
|
//todo: this should be controlled by system, not visible to users
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Only resulsts that originQuery match with curren query will be displayed in the panel
|
/// Only resulsts that originQuery match with curren query will be displayed in the panel
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Query OriginQuery { get; set; }
|
public Query OriginQuery { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Don't set this property if you are developing a plugin
|
/// Don't set this property if you are developing a plugin
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string PluginDirectory { get; set; }
|
public string PluginDirectory { get; set; }
|
||||||
|
|
||||||
public new bool Equals(object obj)
|
public new bool Equals(object obj) {
|
||||||
{
|
if (obj == null || !(obj is Result)) return false;
|
||||||
if (obj == null || !(obj is Result)) return false;
|
|
||||||
|
|
||||||
Result r = (Result)obj;
|
Result r = (Result)obj;
|
||||||
return r.Title == Title && r.SubTitle == SubTitle;
|
return r.Title == Title && r.SubTitle == SubTitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString() {
|
||||||
{
|
return Title + SubTitle;
|
||||||
return Title + SubTitle;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
public Result() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result(string Title = null, string IcoPath = null, string SubTitle = null) {
|
||||||
|
this.Title = Title;
|
||||||
|
this.IcoPath = IcoPath;
|
||||||
|
this.SubTitle = SubTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
172
Wox/App.xaml.cs
172
Wox/App.xaml.cs
@ -18,114 +18,98 @@ using MessageBoxOptions = System.Windows.Forms.MessageBoxOptions;
|
|||||||
using StartupEventArgs = System.Windows.StartupEventArgs;
|
using StartupEventArgs = System.Windows.StartupEventArgs;
|
||||||
using UnhandledExceptionEventArgs = System.UnhandledExceptionEventArgs;
|
using UnhandledExceptionEventArgs = System.UnhandledExceptionEventArgs;
|
||||||
|
|
||||||
namespace Wox
|
namespace Wox {
|
||||||
{
|
public static class EntryPoint {
|
||||||
public static class EntryPoint
|
[STAThread]
|
||||||
{
|
public static void Main(string[] args) {
|
||||||
[STAThread]
|
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
|
||||||
public static void Main(string[] args)
|
System.Windows.Forms.Application.ThreadException += ErrorReporting.ThreadException;
|
||||||
{
|
|
||||||
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
|
|
||||||
System.Windows.Forms.Application.ThreadException += ErrorReporting.ThreadException;
|
|
||||||
|
|
||||||
// don't combine Main and Entry since Microsoft.VisualBasic may be unable to load
|
|
||||||
// seperating them into two methods can make error reporting have the chance to catch exception
|
|
||||||
Entry(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// don't combine Main and Entry since Microsoft.VisualBasic may be unable to load
|
||||||
private static void Entry(string[] args){
|
// seperating them into two methods can make error reporting have the chance to catch exception
|
||||||
SingleInstanceManager manager = new SingleInstanceManager();
|
Entry(args);
|
||||||
manager.Run(args);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Using VB bits to detect single instances and process accordingly:
|
|
||||||
// * OnStartup is fired when the first instance loads
|
|
||||||
// * OnStartupNextInstance is fired when the application is re-run again
|
|
||||||
// NOTE: it is redirected to this instance thanks to IsSingleInstance
|
|
||||||
public class SingleInstanceManager : WindowsFormsApplicationBase
|
|
||||||
{
|
|
||||||
App app;
|
|
||||||
|
|
||||||
public SingleInstanceManager()
|
private static void Entry(string[] args) {
|
||||||
{
|
SingleInstanceManager manager = new SingleInstanceManager();
|
||||||
this.IsSingleInstance = true;
|
manager.Run(args);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
|
// Using VB bits to detect single instances and process accordingly:
|
||||||
{
|
// * OnStartup is fired when the first instance loads
|
||||||
// First time app is launched
|
// * OnStartupNextInstance is fired when the application is re-run again
|
||||||
app = new App();
|
// NOTE: it is redirected to this instance thanks to IsSingleInstance
|
||||||
|
[System.Diagnostics.DebuggerStepThrough]
|
||||||
|
public class SingleInstanceManager : WindowsFormsApplicationBase {
|
||||||
|
App app;
|
||||||
|
|
||||||
|
public SingleInstanceManager() {
|
||||||
|
this.IsSingleInstance = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e) {
|
||||||
|
// First time app is launched
|
||||||
|
app = new App();
|
||||||
//app.InitializeComponent();
|
//app.InitializeComponent();
|
||||||
app.Run();
|
app.Run();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
|
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
|
||||||
{
|
// Subsequent launches
|
||||||
// Subsequent launches
|
base.OnStartupNextInstance(eventArgs);
|
||||||
base.OnStartupNextInstance(eventArgs);
|
app.Activate(eventArgs.CommandLine.ToArray());
|
||||||
app.Activate(eventArgs.CommandLine.ToArray());
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public partial class App : Application
|
public partial class App : Application {
|
||||||
{
|
|
||||||
|
|
||||||
private static MainWindow window;
|
private static MainWindow window;
|
||||||
|
|
||||||
public static MainWindow Window
|
public static MainWindow Window {
|
||||||
{
|
get {
|
||||||
get
|
return window;
|
||||||
{
|
}
|
||||||
return window;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnStartup(StartupEventArgs e)
|
protected override void OnStartup(StartupEventArgs e) {
|
||||||
{
|
this.DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
||||||
this.DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
|
||||||
|
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
|
|
||||||
//for install plugin command when wox didn't start up
|
//for install plugin command when wox didn't start up
|
||||||
//we shouldn't init MainWindow, just intall plugin and exit.
|
//we shouldn't init MainWindow, just intall plugin and exit.
|
||||||
if (e.Args.Length > 0 && e.Args[0].ToLower() == "installplugin")
|
if (e.Args.Length > 0 && e.Args[0].ToLower() == "installplugin") {
|
||||||
{
|
var path = e.Args[1];
|
||||||
var path = e.Args[1];
|
if (!File.Exists(path)) {
|
||||||
if (!File.Exists(path))
|
MessageBox.Show("Plugin " + path + " didn't exist");
|
||||||
{
|
return;
|
||||||
MessageBox.Show("Plugin " + path + " didn't exist");
|
}
|
||||||
return;
|
PluginInstaller.Install(path);
|
||||||
}
|
Environment.Exit(0);
|
||||||
PluginInstaller.Install(path);
|
return;
|
||||||
Environment.Exit(0);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.Args.Length > 0 && e.Args[0].ToLower() == "plugindebugger")
|
if (e.Args.Length > 0 && e.Args[0].ToLower() == "plugindebugger") {
|
||||||
{
|
var path = e.Args[1];
|
||||||
var path = e.Args[1];
|
PluginLoader.Plugins.ActivatePluginDebugger(path);
|
||||||
PluginLoader.Plugins.ActivatePluginDebugger(path);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
window = new MainWindow();
|
window = new MainWindow();
|
||||||
if (e.Args.Length == 0 || e.Args[0].ToLower() != "hidestart")
|
if (e.Args.Length == 0 || e.Args[0].ToLower() != "hidestart") {
|
||||||
{
|
window.ShowApp();
|
||||||
window.ShowApp();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
window.ParseArgs(e.Args);
|
window.ParseArgs(e.Args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Activate(string[] args)
|
public void Activate(string[] args) {
|
||||||
{
|
if (args.Length == 0 || args[0].ToLower() != "hidestart") {
|
||||||
if (args.Length == 0 || args[0].ToLower() != "hidestart")
|
window.ShowApp();
|
||||||
{
|
}
|
||||||
window.ShowApp();
|
window.ParseArgs(args);
|
||||||
}
|
}
|
||||||
window.ParseArgs(args);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,117 +9,101 @@ using Wox.Helper;
|
|||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
using Wox.Plugin.SystemPlugins;
|
using Wox.Plugin.SystemPlugins;
|
||||||
|
|
||||||
namespace Wox.PluginLoader
|
namespace Wox.PluginLoader {
|
||||||
{
|
public abstract class BasePluginLoader {
|
||||||
public abstract class BasePluginLoader
|
private static string PluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins");
|
||||||
{
|
private static string PluginConfigName = "plugin.json";
|
||||||
private static string PluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Plugins");
|
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||||
private static string PluginConfigName = "plugin.json";
|
public abstract List<PluginPair> LoadPlugin();
|
||||||
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
|
||||||
public abstract List<PluginPair> LoadPlugin();
|
|
||||||
|
|
||||||
public static void ParsePluginsConfig()
|
public static void ParsePluginsConfig() {
|
||||||
{
|
pluginMetadatas.Clear();
|
||||||
pluginMetadatas.Clear();
|
ParseSystemPlugins();
|
||||||
ParseSystemPlugins();
|
ParseThirdPartyPlugins();
|
||||||
ParseThirdPartyPlugins();
|
|
||||||
|
|
||||||
if (Plugins.DebuggerMode != null)
|
if (Plugins.DebuggerMode != null) {
|
||||||
{
|
PluginMetadata metadata = GetMetadataFromJson(Plugins.DebuggerMode);
|
||||||
PluginMetadata metadata = GetMetadataFromJson(Plugins.DebuggerMode);
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void ParseSystemPlugins()
|
private static void ParseSystemPlugins() {
|
||||||
{
|
pluginMetadatas.Add(new PluginMetadata() {
|
||||||
PluginMetadata metadata = new PluginMetadata();
|
Name = "System Plugins",
|
||||||
metadata.Name = "System Plugins";
|
Author = "System",
|
||||||
metadata.Author = "System";
|
Description = "system plugins collection",
|
||||||
metadata.Description = "system plugins collection";
|
Language = AllowedLanguage.CSharp,
|
||||||
metadata.Language = AllowedLanguage.CSharp;
|
Version = "1.0",
|
||||||
metadata.Version = "1.0";
|
PluginType = PluginType.System,
|
||||||
metadata.PluginType = PluginType.System;
|
ActionKeyword = "*",
|
||||||
metadata.ActionKeyword = "*";
|
ExecuteFileName = "Wox.Plugin.SystemPlugins.dll",
|
||||||
metadata.ExecuteFileName = "Wox.Plugin.SystemPlugins.dll";
|
PluginDirecotry = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)
|
||||||
metadata.PluginDirecotry = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
|
});
|
||||||
pluginMetadatas.Add(metadata);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void ParseThirdPartyPlugins()
|
private static void ParseThirdPartyPlugins() {
|
||||||
{
|
if (!Directory.Exists(PluginPath))
|
||||||
if (!Directory.Exists(PluginPath))
|
Directory.CreateDirectory(PluginPath);
|
||||||
Directory.CreateDirectory(PluginPath);
|
|
||||||
|
|
||||||
string[] directories = Directory.GetDirectories(PluginPath);
|
string[] directories = Directory.GetDirectories(PluginPath);
|
||||||
foreach (string directory in directories)
|
foreach (string directory in directories) {
|
||||||
{
|
if (File.Exists((Path.Combine(directory, "NeedDelete.txt")))) {
|
||||||
if (File.Exists((Path.Combine(directory, "NeedDelete.txt"))))
|
Directory.Delete(directory, true);
|
||||||
{
|
continue;
|
||||||
Directory.Delete(directory,true);
|
}
|
||||||
continue;
|
PluginMetadata metadata = GetMetadataFromJson(directory);
|
||||||
}
|
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||||
PluginMetadata metadata = GetMetadataFromJson(directory);
|
}
|
||||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
private static PluginMetadata GetMetadataFromJson(string pluginDirectory) {
|
||||||
{
|
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
PluginMetadata metadata;
|
||||||
PluginMetadata metadata;
|
|
||||||
|
|
||||||
if (!File.Exists(configPath))
|
if (!File.Exists(configPath)) {
|
||||||
{
|
Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
||||||
Log.Warn(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
metadata.PluginType = PluginType.ThirdParty;
|
||||||
metadata.PluginType = PluginType.ThirdParty;
|
metadata.PluginDirecotry = pluginDirectory;
|
||||||
metadata.PluginDirecotry = pluginDirectory;
|
}
|
||||||
}
|
catch (Exception) {
|
||||||
catch (Exception)
|
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
||||||
{
|
Log.Warn(error);
|
||||||
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
|
||||||
Log.Warn(error);
|
|
||||||
#if (DEBUG)
|
#if (DEBUG)
|
||||||
{
|
{
|
||||||
throw new WoxException(error);
|
throw new WoxException(error);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
if (!AllowedLanguage.IsAllowed(metadata.Language)) {
|
||||||
{
|
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath, metadata.Language);
|
||||||
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
|
Log.Warn(error);
|
||||||
metadata.Language);
|
|
||||||
Log.Warn(error);
|
|
||||||
#if (DEBUG)
|
#if (DEBUG)
|
||||||
{
|
{
|
||||||
throw new WoxException(error);
|
throw new WoxException(error);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!File.Exists(metadata.ExecuteFilePath))
|
if (!File.Exists(metadata.ExecuteFilePath)) {
|
||||||
{
|
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath, metadata.ExecuteFilePath);
|
||||||
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
|
Log.Warn(error);
|
||||||
metadata.ExecuteFilePath);
|
|
||||||
Log.Warn(error);
|
|
||||||
#if (DEBUG)
|
#if (DEBUG)
|
||||||
{
|
{
|
||||||
throw new WoxException(error);
|
throw new WoxException(error);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +1,53 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
|
||||||
using Wox.Helper;
|
using Wox.Helper;
|
||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
using Wox.Plugin.SystemPlugins;
|
using Wox.Plugin.SystemPlugins;
|
||||||
|
|
||||||
namespace Wox.PluginLoader
|
namespace Wox.PluginLoader {
|
||||||
{
|
|
||||||
public class CSharpPluginLoader : BasePluginLoader
|
|
||||||
{
|
|
||||||
public override List<PluginPair> LoadPlugin()
|
|
||||||
{
|
|
||||||
List<PluginPair> plugins = new List<PluginPair>();
|
|
||||||
|
|
||||||
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
public class CSharpPluginLoader : BasePluginLoader {
|
||||||
foreach (PluginMetadata metadata in metadatas)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
|
||||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
|
|
||||||
if (types.Count == 0)
|
|
||||||
{
|
|
||||||
Log.Warn(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
|
|
||||||
metadata.Name));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Type type in types)
|
public override List<PluginPair> LoadPlugin() {
|
||||||
{
|
List<PluginPair> plugins = new List<PluginPair>();
|
||||||
PluginPair pair = new PluginPair()
|
|
||||||
{
|
|
||||||
Plugin = Activator.CreateInstance(type) as IPlugin,
|
|
||||||
Metadata = metadata
|
|
||||||
};
|
|
||||||
|
|
||||||
var sys = pair.Plugin as BaseSystemPlugin;
|
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||||
if (sys != null)
|
foreach (PluginMetadata metadata in metadatas) {
|
||||||
{
|
try {
|
||||||
sys.PluginDirectory = metadata.PluginDirecotry;
|
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||||
}
|
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
|
||||||
|
if (types.Count == 0) {
|
||||||
|
Log.Warn(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin", metadata.Name));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
plugins.Add(pair);
|
foreach (Type type in types) {
|
||||||
}
|
PluginPair pair = new PluginPair() {
|
||||||
}
|
Plugin = Activator.CreateInstance(type) as IPlugin,
|
||||||
catch (Exception e)
|
Metadata = metadata
|
||||||
{
|
};
|
||||||
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
|
|
||||||
|
var sys = pair.Plugin as BaseSystemPlugin;
|
||||||
|
if (sys != null) {
|
||||||
|
sys.PluginDirectory = metadata.PluginDirecotry;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins.Add(pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||||
#if (DEBUG)
|
#if (DEBUG)
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
return plugins;
|
||||||
|
}
|
||||||
return plugins;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
@ -24,7 +24,7 @@ namespace Wox.PluginLoader {
|
|||||||
initializing = new ManualResetEvent(false);
|
initializing = new ManualResetEvent(false);
|
||||||
plugins.Clear();
|
plugins.Clear();
|
||||||
BasePluginLoader.ParsePluginsConfig();
|
BasePluginLoader.ParsePluginsConfig();
|
||||||
|
|
||||||
if (UserSettingStorage.Instance.EnablePythonPlugins) {
|
if (UserSettingStorage.Instance.EnablePythonPlugins) {
|
||||||
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
|
Loading…
Reference in New Issue
Block a user