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

315 lines
11 KiB
C#
Raw Normal View History

2015-01-26 19:06:03 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
2015-10-31 07:17:34 +08:00
using System.Reflection;
using System.ServiceProcess;
2015-01-26 19:06:03 +08:00
using Wox.Infrastructure;
using Wox.Plugin.Everything.Everything;
using Wox.Plugin.Features;
2015-01-26 19:06:03 +08:00
namespace Wox.Plugin.Everything
{
public class Main : IPlugin, IPluginI18n, IContextMenu
2015-01-26 19:06:03 +08:00
{
PluginInitContext context;
EverythingAPI api = new EverythingAPI();
private static List<string> imageExts = new List<string>() { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
private static List<string> executableExts = new List<string>() { ".exe" };
public List<Result> Query(Query query)
{
var results = new List<Result>();
if (!string.IsNullOrEmpty(query.Search))
{
var keyword = query.Search;
if (ContextMenuStorage.Instance.MaxSearchCount <= 0)
{
ContextMenuStorage.Instance.MaxSearchCount = 50;
2015-01-26 19:06:03 +08:00
ContextMenuStorage.Instance.Save();
}
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";
r.Action = (c) =>
{
UnInstallEverything();
return true;
};
r.Score = 2000;
results.Add(r);
}
2015-01-26 19:06:03 +08:00
try
{
var searchList = api.Search(keyword, maxCount: ContextMenuStorage.Instance.MaxSearchCount).ToList();
var fuzzyMather = FuzzyMatcher.Create(keyword);
searchList.Sort(
(x, y) =>
fuzzyMather.Evaluate(Path.GetFileName(y.FullPath)).Score -
fuzzyMather.Evaluate(Path.GetFileName(x.FullPath)).Score);
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);
r.Action = (c) =>
{
context.API.HideApp();
context.API.ShellRun(path);
return true;
};
r.ContextData = s;
2015-01-26 19:06:03 +08:00
results.Add(r);
}
}
catch (IPCErrorException)
{
StartEverything();
results.Add(new Result()
{
2015-02-07 16:53:33 +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)
{
results.Add(new Result()
{
2015-02-07 16:53:33 +08:00
Title = context.API.GetTranslation("wox_plugin_everything_query_error"),
2015-01-26 19:06:03 +08:00
SubTitle = e.Message,
Action = _ =>
{
System.Windows.Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);
2015-02-07 16:53:33 +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"
});
}
}
api.Reset();
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))
{
if (imageExts.Contains(ext.ToLower()))
{
return "Images\\image.png";
}
else if (executableExts.Contains(ext.ToLower()))
{
return s.FullPath;
}
}
return "Images\\file.png";
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
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>();
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;
}
2015-01-26 19:06:03 +08:00
public void Init(PluginInitContext context)
{
this.context = context;
2015-02-07 16:53:33 +08:00
ContextMenuStorage.Instance.API = context.API;
2015-01-26 19:06:03 +08:00
LoadLibrary(Path.Combine(
Path.Combine(context.CurrentPluginMetadata.PluginDirectory, (IntPtr.Size == 4) ? "x86" : "x64"),
"Everything.dll"
));
StartEverything();
}
private void StartEverything()
{
if (!CheckEverythingServiceRunning())
2015-01-26 19:06:03 +08:00
{
if (InstallAndRunEverythingService())
2015-02-07 16:53:33 +08:00
{
StartEverythingClient();
2015-02-07 16:53:33 +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;
}
}
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();
Process[] proc = Process.GetProcessesByName("Everything");
foreach (Process process in proc)
{
process.Kill();
}
return true;
}
catch (Exception e)
{
return false;
}
}
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)
{
context.API.ShowMsg("Start Everything failed");
}
}
private bool CheckEverythingServiceRunning()
{
try
{
ServiceController sc = new ServiceController("Everything");
return sc.Status == ServiceControllerStatus.Running;
}
catch
{
2015-02-07 16:53:33 +08:00
2015-01-26 19:06:03 +08:00
}
return false;
2015-01-26 19:06:03 +08:00
}
private bool CheckEverythingIsRunning()
{
return Process.GetProcessesByName("Everything").Length > 0;
}
private string GetEverythingPath()
{
string everythingFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "PortableEverything");
return Path.Combine(everythingFolder, "Everything.exe");
}
2015-02-07 16:53:33 +08:00
public string GetLanguagesFolder()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
}
2015-02-07 20:17:49 +08:00
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<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(ContextMenuStorage.Instance.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;
}
2015-01-26 19:06:03 +08:00
}
}