2015-01-26 19:06:03 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-03-27 09:49:05 +08:00
|
|
|
|
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;
|
2015-02-08 16:27:41 +08:00
|
|
|
|
using System.ServiceProcess;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
using System.Windows;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
using Wox.Plugin.Everything.Everything;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Plugin.Everything
|
|
|
|
|
{
|
2015-02-08 16:27:41 +08:00
|
|
|
|
public class Main : IPlugin, IPluginI18n, IContextMenu
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
private readonly EverythingAPI _api = new EverythingAPI();
|
|
|
|
|
private static readonly List<string> ImageExts = new List<string> { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
|
|
|
|
|
private static readonly List<string> ExecutableExts = new List<string> { ".exe" };
|
2016-03-29 11:33:01 +08:00
|
|
|
|
private const string PortableEverything = "PortableEverything";
|
|
|
|
|
private const string EverythingProcessName = "Everything";
|
|
|
|
|
|
|
|
|
|
private PluginInitContext _context;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
private ContextMenuStorage _settings = ContextMenuStorage.Instance;
|
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;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
if (_settings.MaxSearchCount <= 0)
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings.MaxSearchCount = 50;
|
|
|
|
|
_settings.Save();
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 17:09:14 +08:00
|
|
|
|
if (keyword == "uninstalleverything")
|
|
|
|
|
{
|
|
|
|
|
Result r = new Result();
|
|
|
|
|
r.Title = "Uninstall Everything";
|
|
|
|
|
r.SubTitle = "You need to uninstall everything service if you can not move/delete wox folder";
|
|
|
|
|
r.IcoPath = "Images\\find.png";
|
2016-01-07 05:34:42 +08:00
|
|
|
|
r.Action = c =>
|
2015-02-08 17:09:14 +08:00
|
|
|
|
{
|
|
|
|
|
UnInstallEverything();
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
r.Score = 2000;
|
|
|
|
|
results.Add(r);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 19:06:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
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;
|
|
|
|
|
Result r = new Result();
|
|
|
|
|
r.Title = Path.GetFileName(path);
|
|
|
|
|
r.SubTitle = path;
|
|
|
|
|
r.IcoPath = GetIconPath(s);
|
2016-01-07 05:34:42 +08:00
|
|
|
|
r.Action = c =>
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
2016-03-27 09:49:05 +08:00
|
|
|
|
bool hide;
|
|
|
|
|
try
|
2016-01-04 07:18:51 +08:00
|
|
|
|
{
|
2016-03-27 09:49:05 +08:00
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = path,
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
};
|
2015-02-07 23:49:46 +08:00
|
|
|
|
r.ContextData = s;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
results.Add(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IPCErrorException)
|
|
|
|
|
{
|
|
|
|
|
StartEverything();
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetIconPath(SearchResult s)
|
|
|
|
|
{
|
|
|
|
|
var ext = Path.GetExtension(s.FullPath);
|
|
|
|
|
if (s.Type == ResultType.Folder)
|
|
|
|
|
{
|
|
|
|
|
return "Images\\folder.png";
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(ext))
|
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
if (ImageExts.Contains(ext.ToLower()))
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
return "Images\\image.png";
|
|
|
|
|
}
|
2016-01-08 09:13:36 +08:00
|
|
|
|
else if (ExecutableExts.Contains(ext.ToLower()))
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
|
|
|
|
return s.FullPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "Images\\file.png";
|
|
|
|
|
}
|
|
|
|
|
|
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-04 07:18:51 +08:00
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
Name = _context.API.GetTranslation("wox_plugin_everything_open_containing_folder"),
|
2016-01-04 07:18:51 +08:00
|
|
|
|
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;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings.API = context.API;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
|
2016-03-29 11:33:01 +08:00
|
|
|
|
LoadLibrary(Path.Combine(context.CurrentPluginMetadata.PluginDirectory,
|
|
|
|
|
PortableEverything, GetCpuType(), "Everything.dll"));
|
2015-01-26 19:06:03 +08:00
|
|
|
|
|
|
|
|
|
StartEverything();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-29 11:33:01 +08:00
|
|
|
|
private string GetCpuType()
|
|
|
|
|
{
|
|
|
|
|
return (IntPtr.Size == 4) ? "x86" : "x64";
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 19:06:03 +08:00
|
|
|
|
private void StartEverything()
|
|
|
|
|
{
|
2015-02-08 17:09:14 +08:00
|
|
|
|
if (!CheckEverythingServiceRunning())
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
2015-02-08 16:27:41 +08:00
|
|
|
|
if (InstallAndRunEverythingService())
|
2015-02-07 16:53:33 +08:00
|
|
|
|
{
|
2015-02-08 16:27:41 +08:00
|
|
|
|
StartEverythingClient();
|
2015-02-07 16:53:33 +08:00
|
|
|
|
}
|
2015-02-08 16:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StartEverythingClient();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool InstallAndRunEverythingService()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process p = new Process();
|
|
|
|
|
p.StartInfo.Verb = "runas";
|
|
|
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
|
|
p.StartInfo.FileName = GetEverythingPath();
|
|
|
|
|
p.StartInfo.UseShellExecute = true;
|
|
|
|
|
p.StartInfo.Arguments = "-install-service";
|
|
|
|
|
p.Start();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 17:09:14 +08:00
|
|
|
|
private bool UnInstallEverything()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process p = new Process();
|
|
|
|
|
p.StartInfo.Verb = "runas";
|
|
|
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
|
|
p.StartInfo.FileName = GetEverythingPath();
|
|
|
|
|
p.StartInfo.UseShellExecute = true;
|
|
|
|
|
p.StartInfo.Arguments = "-uninstall-service";
|
|
|
|
|
p.Start();
|
|
|
|
|
|
2016-03-29 11:33:01 +08:00
|
|
|
|
Process[] proc = Process.GetProcessesByName(EverythingProcessName);
|
2015-02-08 17:09:14 +08:00
|
|
|
|
foreach (Process process in proc)
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 16:27:41 +08:00
|
|
|
|
private void StartEverythingClient()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Process p = new Process();
|
|
|
|
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
|
|
p.StartInfo.FileName = GetEverythingPath();
|
|
|
|
|
p.StartInfo.UseShellExecute = true;
|
|
|
|
|
p.StartInfo.Arguments = "-startup";
|
|
|
|
|
p.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
_context.API.ShowMsg("Start Everything failed");
|
2015-02-08 16:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 17:09:14 +08:00
|
|
|
|
private bool CheckEverythingServiceRunning()
|
2015-02-08 16:27:41 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-29 11:33:01 +08:00
|
|
|
|
ServiceController sc = new ServiceController(EverythingProcessName);
|
2015-02-08 16:27:41 +08:00
|
|
|
|
return sc.Status == ServiceControllerStatus.Running;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2015-02-07 16:53:33 +08:00
|
|
|
|
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
2015-02-08 16:27:41 +08:00
|
|
|
|
return false;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckEverythingIsRunning()
|
|
|
|
|
{
|
2016-03-29 11:33:01 +08:00
|
|
|
|
return Process.GetProcessesByName(EverythingProcessName).Length > 0;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetEverythingPath()
|
|
|
|
|
{
|
2016-01-08 09:13:36 +08:00
|
|
|
|
string directory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory,
|
2016-03-29 11:33:01 +08:00
|
|
|
|
PortableEverything, GetCpuType(),
|
2016-01-08 09:13:36 +08:00
|
|
|
|
"Everything.exe");
|
|
|
|
|
return directory;
|
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
|
|
|
|
}
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
|
|
|
|
public List<Result> LoadContextMenus(Result selectedResult)
|
|
|
|
|
{
|
|
|
|
|
SearchResult record = selectedResult.ContextData as SearchResult;
|
|
|
|
|
List<Result> contextMenus = new List<Result>();
|
2015-02-08 16:27:41 +08:00
|
|
|
|
if (record == null) return contextMenus;
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
|
|
|
|
List<ContextMenu> availableContextMenus = new List<ContextMenu>();
|
|
|
|
|
availableContextMenus.AddRange(GetDefaultContextMenu());
|
2016-03-28 10:09:57 +08:00
|
|
|
|
availableContextMenus.AddRange(_settings.ContextMenus);
|
2015-02-07 23:49:46 +08:00
|
|
|
|
|
|
|
|
|
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
|
2015-02-07 23:49:46 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
2015-02-07 23:49:46 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = contextMenu.ImagePath
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return contextMenus;
|
|
|
|
|
}
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|