using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using Wox.Infrastructure; using Wox.Infrastructure.Storage; using Wox.Plugin.Everything.Everything; namespace Wox.Plugin.Everything { public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable { private readonly EverythingAPI _api = new EverythingAPI(); public const string DLL = "Everything.dll"; private PluginInitContext _context; private Settings _settings; private PluginJsonStorage _storage; public void Save() { _storage.Save(); } public List Query(Query query) { var results = new List(); if (!string.IsNullOrEmpty(query.Search)) { var keyword = query.Search; if (_settings.MaxSearchCount <= 0) { _settings.MaxSearchCount = 50; } try { var searchList = _api.Search(keyword, maxCount: _settings.MaxSearchCount).ToList(); foreach (var s in searchList) { var path = s.FullPath; string workingDir = null; if (_settings.UseLocationAsWorkingDir) workingDir = Path.GetDirectoryName(path); Result r = new Result(); r.Title = Path.GetFileName(path); r.SubTitle = path; r.IcoPath = path; r.Action = c => { 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; }; r.ContextData = s; results.Add(r); } } catch (IPCErrorException) { results.Add(new Result { Title = _context.API.GetTranslation("wox_plugin_everything_is_not_running"), IcoPath = "Images\\warning.png" }); } catch (Exception e) { results.Add(new Result { Title = _context.API.GetTranslation("wox_plugin_everything_query_error"), SubTitle = e.Message, Action = _ => { Clipboard.SetText(e.Message + "\r\n" + e.StackTrace); _context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_everything_copied"), null, string.Empty); return false; }, IcoPath = "Images\\error.png" }); } } _api.Reset(); return results; } [DllImport("kernel32.dll")] private static extern int LoadLibrary(string name); private List GetDefaultContextMenu() { List defaultContextMenus = new List(); ContextMenu openFolderContextMenu = new ContextMenu { Name = _context.API.GetTranslation("wox_plugin_everything_open_containing_folder"), Command = "explorer.exe", Argument = " /select,\"{path}\"", ImagePath = "Images\\folder.png" }; defaultContextMenus.Add(openFolderContextMenu); return defaultContextMenus; } public void Init(PluginInitContext context) { _context = context; _storage = new PluginJsonStorage(); _settings = _storage.Load(); var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory; const string sdk = "EverythingSDK"; var bundledSDKDirectory = Path.Combine(pluginDirectory, sdk, CpuType()); var sdkDirectory = Path.Combine(_storage.DirectoryPath, sdk, CpuType()); Helper.ValidateDataDirectory(bundledSDKDirectory, sdkDirectory); var sdkPath = Path.Combine(sdkDirectory, DLL); Constant.EverythingSDKPath = sdkPath; LoadLibrary(sdkPath); } private static string CpuType() { return Environment.Is64BitOperatingSystem ? "x64" : "x86"; } public string GetTranslatedPluginTitle() { return _context.API.GetTranslation("wox_plugin_everything_plugin_name"); } public string GetTranslatedPluginDescription() { return _context.API.GetTranslation("wox_plugin_everything_plugin_description"); } public List LoadContextMenus(Result selectedResult) { SearchResult record = selectedResult.ContextData as SearchResult; List contextMenus = new List(); if (record == null) return contextMenus; List availableContextMenus = new List(); availableContextMenus.AddRange(GetDefaultContextMenu()); availableContextMenus.AddRange(_settings.ContextMenus); if (record.Type == ResultType.File) { foreach (ContextMenu contextMenu in availableContextMenus) { var menu = contextMenu; contextMenus.Add(new Result { Title = contextMenu.Name, Action = _ => { string argument = menu.Argument.Replace("{path}", record.FullPath); try { Process.Start(menu.Command, argument); } catch { _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); } } }