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

210 lines
7.5 KiB
C#
Raw Normal View History

2015-01-26 19:06:03 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
2015-01-26 19:06:03 +08:00
using System.Diagnostics;
using System.IO;
using System.Linq;
2016-01-07 05:34:42 +08:00
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
2016-05-08 02:16:13 +08:00
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
2015-01-26 19:06:03 +08:00
using Wox.Plugin.Everything.Everything;
namespace Wox.Plugin.Everything
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
2015-01-26 19:06:03 +08:00
{
2016-01-08 09:13:36 +08:00
private readonly EverythingAPI _api = new EverythingAPI();
public const string DLL = "Everything.dll";
private PluginInitContext _context;
private Settings _settings;
private PluginJsonStorage<Settings> _storage;
public void Save()
{
_storage.Save();
}
2015-01-26 19:06:03 +08:00
public List<Result> Query(Query query)
{
var results = new List<Result>();
if (!string.IsNullOrEmpty(query.Search))
{
var keyword = query.Search;
if (_settings.MaxSearchCount <= 0)
2015-01-26 19:06:03 +08:00
{
_settings.MaxSearchCount = 50;
2015-01-26 19:06:03 +08:00
}
try
{
var searchList = _api.Search(keyword, maxCount: _settings.MaxSearchCount).ToList();
2015-01-26 19:06:03 +08:00
foreach (var s in searchList)
{
var path = s.FullPath;
string workingDir = null;
if (_settings.UseLocationAsWorkingDir)
workingDir = Path.GetDirectoryName(path);
2015-01-26 19:06:03 +08:00
Result r = new Result();
r.Title = Path.GetFileName(path);
r.SubTitle = path;
2016-05-04 06:21:03 +08:00
r.IcoPath = path;
2016-01-07 05:34:42 +08:00
r.Action = c =>
2015-01-26 19:06:03 +08:00
{
bool hide;
try
{
Process.Start(new ProcessStartInfo
{
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
});
hide = true;
}
catch (Win32Exception)
{
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
var message = "Can't open this file";
_context.API.ShowMsg(name, message, string.Empty);
hide = false;
}
return hide;
2015-01-26 19:06:03 +08:00
};
r.ContextData = s;
2015-01-26 19:06:03 +08:00
results.Add(r);
}
}
catch (IPCErrorException)
{
2016-01-07 05:34:42 +08:00
results.Add(new Result
2015-01-26 19:06:03 +08:00
{
2016-01-08 09:13:36 +08:00
Title = _context.API.GetTranslation("wox_plugin_everything_is_not_running"),
2015-01-26 19:06:03 +08:00
IcoPath = "Images\\warning.png"
});
}
catch (Exception e)
{
2016-01-07 05:34:42 +08:00
results.Add(new Result
2015-01-26 19:06:03 +08:00
{
2016-01-08 09:13:36 +08:00
Title = _context.API.GetTranslation("wox_plugin_everything_query_error"),
2015-01-26 19:06:03 +08:00
SubTitle = e.Message,
Action = _ =>
{
2016-01-07 05:34:42 +08:00
Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);
2016-01-08 09:13:36 +08:00
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_everything_copied"), null, string.Empty);
2015-01-26 19:06:03 +08:00
return false;
},
IcoPath = "Images\\error.png"
});
}
}
2016-01-08 09:13:36 +08:00
_api.Reset();
2015-01-26 19:06:03 +08:00
return results;
}
2016-01-07 05:34:42 +08:00
[DllImport("kernel32.dll")]
2015-01-26 19:06:03 +08:00
private static extern int LoadLibrary(string name);
2015-02-07 16:53:33 +08:00
private List<ContextMenu> GetDefaultContextMenu()
{
List<ContextMenu> defaultContextMenus = new List<ContextMenu>();
2016-01-07 05:34:42 +08:00
ContextMenu openFolderContextMenu = new ContextMenu
{
2016-01-08 09:13:36 +08:00
Name = _context.API.GetTranslation("wox_plugin_everything_open_containing_folder"),
Command = "explorer.exe",
Argument = " /select,\"{path}\"",
ImagePath = "Images\\folder.png"
};
2015-02-07 16:53:33 +08:00
defaultContextMenus.Add(openFolderContextMenu);
return defaultContextMenus;
}
2015-01-26 19:06:03 +08:00
public void Init(PluginInitContext context)
{
2016-01-08 09:13:36 +08:00
_context = context;
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
2015-01-26 19:06:03 +08:00
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
2016-05-08 02:16:13 +08:00
const string sdk = "EverythingSDK";
var bundledSDKDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
var sdkDirectory = Path.Combine(_storage.DirectoryPath, sdk, CpuType());
Helper.ValidateDataDirectory(bundledSDKDirectory, sdkDirectory);
2016-05-08 02:16:13 +08:00
var sdkPath = Path.Combine(sdkDirectory, DLL);
2016-07-20 09:01:47 +08:00
Constant.EverythingSDKPath = sdkPath;
2016-05-08 02:16:13 +08:00
LoadLibrary(sdkPath);
}
2016-05-08 02:16:13 +08:00
private static string CpuType()
2015-01-26 19:06:03 +08:00
{
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
2015-01-26 19:06:03 +08:00
}
2015-02-07 16:53:33 +08:00
2015-02-07 20:17:49 +08:00
public string GetTranslatedPluginTitle()
{
2016-01-08 09:13:36 +08:00
return _context.API.GetTranslation("wox_plugin_everything_plugin_name");
2015-02-07 20:17:49 +08:00
}
public string GetTranslatedPluginDescription()
{
2016-01-08 09:13:36 +08:00
return _context.API.GetTranslation("wox_plugin_everything_plugin_description");
2015-02-07 20:17:49 +08:00
}
public List<Result> LoadContextMenus(Result selectedResult)
{
SearchResult record = selectedResult.ContextData as SearchResult;
List<Result> contextMenus = new List<Result>();
if (record == null) return contextMenus;
List<ContextMenu> availableContextMenus = new List<ContextMenu>();
availableContextMenus.AddRange(GetDefaultContextMenu());
availableContextMenus.AddRange(_settings.ContextMenus);
if (record.Type == ResultType.File)
{
foreach (ContextMenu contextMenu in availableContextMenus)
{
var menu = contextMenu;
2016-01-07 05:34:42 +08:00
contextMenus.Add(new Result
{
Title = contextMenu.Name,
Action = _ =>
{
string argument = menu.Argument.Replace("{path}", record.FullPath);
try
{
Process.Start(menu.Command, argument);
}
catch
{
2016-01-08 09:13:36 +08:00
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"), record.FullPath), string.Empty, string.Empty);
return false;
}
return true;
},
IcoPath = contextMenu.ImagePath
});
}
}
return contextMenus;
}
public Control CreateSettingPanel()
{
return new EverythingSettings(_settings);
}
2015-01-26 19:06:03 +08:00
}
}