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;
|
|
|
|
|
using System.Windows;
|
2016-05-31 03:07:20 +08:00
|
|
|
|
using System.Windows.Controls;
|
2016-05-08 02:16:13 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2015-01-26 19:06:03 +08:00
|
|
|
|
using Wox.Plugin.Everything.Everything;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Plugin.Everything
|
|
|
|
|
{
|
2016-05-31 03:07:20 +08:00
|
|
|
|
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();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
2016-05-04 09:26:19 +08:00
|
|
|
|
public const string DLL = "Everything.dll";
|
2016-03-29 11:33:01 +08:00
|
|
|
|
|
|
|
|
|
private PluginInitContext _context;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
2016-05-04 09:26:19 +08:00
|
|
|
|
private Settings _settings;
|
|
|
|
|
private PluginJsonStorage<Settings> _storage;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
2016-05-03 05:37:01 +08:00
|
|
|
|
public void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
|
|
|
|
_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;
|
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;
|
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;
|
2016-05-31 03:07:20 +08:00
|
|
|
|
|
|
|
|
|
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;
|
2019-12-03 22:25:19 +08:00
|
|
|
|
r.TitleHighlightData = StringMatcher.FuzzySearch(keyword, Path.GetFileName(path)).MatchData;
|
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,
|
2016-05-31 03:07:20 +08:00
|
|
|
|
UseShellExecute = true,
|
|
|
|
|
WorkingDirectory = workingDir
|
2016-03-27 09:49:05 +08:00
|
|
|
|
});
|
|
|
|
|
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;
|
2019-12-03 22:25:19 +08:00
|
|
|
|
r.SubTitleHighlightData = StringMatcher.FuzzySearch(keyword, path).MatchData;
|
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-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);
|
2018-12-25 13:17:35 +08:00
|
|
|
|
|
|
|
|
|
string editorPath = string.IsNullOrEmpty(_settings.EditorPath) ? "notepad.exe" : _settings.EditorPath;
|
|
|
|
|
|
|
|
|
|
ContextMenu openWithEditorContextMenu = new ContextMenu
|
|
|
|
|
{
|
|
|
|
|
Name = string.Format(_context.API.GetTranslation("wox_plugin_everything_open_with_editor"), Path.GetFileNameWithoutExtension(editorPath)),
|
|
|
|
|
Command = editorPath,
|
|
|
|
|
Argument = " \"{path}\"",
|
|
|
|
|
ImagePath = editorPath
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
defaultContextMenus.Add(openWithEditorContextMenu);
|
|
|
|
|
|
2015-02-07 16:53:33 +08:00
|
|
|
|
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-05-04 09:26:19 +08:00
|
|
|
|
_storage = new PluginJsonStorage<Settings>();
|
|
|
|
|
_settings = _storage.Load();
|
2015-01-26 19:06:03 +08:00
|
|
|
|
|
2016-04-21 08:53:21 +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-03-29 11:33:01 +08:00
|
|
|
|
|
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);
|
2015-02-08 16:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 02:16:13 +08:00
|
|
|
|
private static string CpuType()
|
2015-01-26 19:06:03 +08:00
|
|
|
|
{
|
2016-05-04 09:26:19 +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
|
|
|
|
}
|
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
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-25 15:11:24 +08:00
|
|
|
|
var icoPath = (record.Type == ResultType.File) ? "Images\\file.png" : "Images\\folder.png";
|
|
|
|
|
contextMenus.Add(new Result
|
|
|
|
|
{
|
|
|
|
|
Title = _context.API.GetTranslation("wox_plugin_everything_copy_path"),
|
|
|
|
|
Action = (context) =>
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText(record.FullPath);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = icoPath
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
contextMenus.Add(new Result
|
|
|
|
|
{
|
|
|
|
|
Title = _context.API.GetTranslation("wox_plugin_everything_copy"),
|
|
|
|
|
Action = (context) =>
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = icoPath
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
|
|
|
|
|
contextMenus.Add(new Result
|
|
|
|
|
{
|
|
|
|
|
Title = _context.API.GetTranslation("wox_plugin_everything_delete"),
|
|
|
|
|
Action = (context) =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (record.Type == ResultType.File)
|
|
|
|
|
System.IO.File.Delete(record.FullPath);
|
|
|
|
|
else
|
|
|
|
|
System.IO.Directory.Delete(record.FullPath);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = icoPath
|
|
|
|
|
});
|
|
|
|
|
|
2015-02-07 23:49:46 +08:00
|
|
|
|
return contextMenus;
|
|
|
|
|
}
|
2016-05-31 03:07:20 +08:00
|
|
|
|
|
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
{
|
|
|
|
|
return new EverythingSettings(_settings);
|
|
|
|
|
}
|
2015-01-26 19:06:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|