2019-12-09 05:27:50 +08:00
|
|
|
using System;
|
2014-01-06 21:25:24 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2015-01-03 15:20:34 +08:00
|
|
|
using System.Windows;
|
2015-10-31 07:17:34 +08:00
|
|
|
using System.Windows.Controls;
|
2019-12-03 22:25:19 +08:00
|
|
|
using Wox.Infrastructure;
|
2016-04-21 08:53:21 +08:00
|
|
|
using Wox.Infrastructure.Storage;
|
2014-01-06 21:25:24 +08:00
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
namespace Wox.Plugin.Folder
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2016-05-07 10:55:09 +08:00
|
|
|
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
private static List<string> _driverNames;
|
|
|
|
private PluginInitContext _context;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
|
|
|
private readonly Settings _settings;
|
2016-04-27 05:45:31 +08:00
|
|
|
private readonly PluginJsonStorage<Settings> _storage;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
2016-05-07 10:55:09 +08:00
|
|
|
public Main()
|
2016-04-21 08:53:21 +08:00
|
|
|
{
|
2016-04-27 05:45:31 +08:00
|
|
|
_storage = new PluginJsonStorage<Settings>();
|
2016-04-21 08:53:21 +08:00
|
|
|
_settings = _storage.Load();
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:37:01 +08:00
|
|
|
public void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
{
|
|
|
|
_storage.Save();
|
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
|
2014-07-19 10:12:11 +08:00
|
|
|
public Control CreateSettingPanel()
|
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
return new FileSystemSettings(_context.API, _settings);
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public void Init(PluginInitContext context)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
_context = context;
|
2019-12-07 18:44:58 +08:00
|
|
|
InitialDriverList();
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
public List<Result> Query(Query query)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
var results = GetUserFolderResults(query);
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
string search = query.Search.ToLower();
|
2016-06-16 09:59:31 +08:00
|
|
|
if (_driverNames != null && !_driverNames.Any(search.StartsWith))
|
2014-07-19 10:12:11 +08:00
|
|
|
return results;
|
|
|
|
|
2016-05-21 02:19:32 +08:00
|
|
|
results.AddRange(QueryInternal_Directory_Exists(query));
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
// todo why was this hack here?
|
2016-05-05 23:30:56 +08:00
|
|
|
foreach (var result in results)
|
|
|
|
{
|
|
|
|
result.Score += 10;
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:12:11 +08:00
|
|
|
return results;
|
2016-05-21 02:19:32 +08:00
|
|
|
}
|
2019-12-07 18:44:58 +08:00
|
|
|
|
2019-12-11 23:50:17 +08:00
|
|
|
private Result CreateFolderResult(string title, string path, Query query)
|
2019-12-07 18:44:58 +08:00
|
|
|
{
|
|
|
|
return new Result
|
|
|
|
{
|
|
|
|
Title = title,
|
|
|
|
IcoPath = path,
|
|
|
|
SubTitle = "Ctrl + Enter to open the directory",
|
2019-12-11 23:50:17 +08:00
|
|
|
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
|
2019-12-07 18:44:58 +08:00
|
|
|
Action = c =>
|
|
|
|
{
|
|
|
|
if (c.SpecialKeyState.CtrlPressed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Process.Start(path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string changeTo = path.EndsWith("\\") ? path : path + "\\";
|
2019-12-11 23:50:17 +08:00
|
|
|
_context.API.ChangeQuery(string.IsNullOrEmpty(query.ActionKeyword) ?
|
|
|
|
changeTo :
|
|
|
|
query.ActionKeyword + " " + changeTo);
|
2019-12-07 18:44:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Result> GetUserFolderResults(Query query)
|
|
|
|
{
|
|
|
|
string search = query.Search.ToLower();
|
|
|
|
var userFolderLinks = _settings.FolderLinks.Where(
|
|
|
|
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase));
|
2019-12-11 23:50:17 +08:00
|
|
|
var results = userFolderLinks.Select(item =>
|
|
|
|
CreateFolderResult(item.Nickname, item.Path, query)).ToList();
|
2019-12-07 18:44:58 +08:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2016-05-21 02:19:32 +08:00
|
|
|
private void InitialDriverList()
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
if (_driverNames == null)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
_driverNames = new List<string>();
|
2019-12-07 18:44:58 +08:00
|
|
|
var allDrives = DriveInfo.GetDrives();
|
2014-07-19 10:12:11 +08:00
|
|
|
foreach (DriveInfo driver in allDrives)
|
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
_driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
private static readonly char[] _specialSearchChars = new char[]
|
|
|
|
{
|
|
|
|
'?', '*', '>'
|
|
|
|
};
|
|
|
|
|
2016-05-21 02:19:32 +08:00
|
|
|
private List<Result> QueryInternal_Directory_Exists(Query query)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-09 18:27:33 +08:00
|
|
|
var search = query.Search;
|
2014-07-19 10:12:11 +08:00
|
|
|
var results = new List<Result>();
|
2019-12-07 18:44:58 +08:00
|
|
|
var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0;
|
2015-08-23 15:40:18 +08:00
|
|
|
string incompleteName = "";
|
2019-12-07 18:44:58 +08:00
|
|
|
if (hasSpecial || !Directory.Exists(search + "\\"))
|
2015-08-23 15:40:18 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user
|
|
|
|
// find the right folder.
|
2016-05-21 02:19:32 +08:00
|
|
|
int index = search.LastIndexOf('\\');
|
|
|
|
if (index > 0 && index < (search.Length - 1))
|
2015-08-23 15:40:18 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
incompleteName = search.Substring(index + 1).ToLower();
|
2016-05-21 02:19:32 +08:00
|
|
|
search = search.Substring(0, index + 1);
|
|
|
|
if (!Directory.Exists(search))
|
2019-12-11 23:50:17 +08:00
|
|
|
{
|
2015-08-23 15:40:18 +08:00
|
|
|
return results;
|
2019-12-11 23:50:17 +08:00
|
|
|
}
|
2015-08-23 15:40:18 +08:00
|
|
|
}
|
|
|
|
else
|
2019-12-11 23:50:17 +08:00
|
|
|
{
|
2015-08-23 15:40:18 +08:00
|
|
|
return results;
|
2019-12-11 23:50:17 +08:00
|
|
|
}
|
2015-08-23 15:40:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-11 23:50:17 +08:00
|
|
|
// folder exist, add \ at the end of doesn't exist
|
2016-05-21 02:19:32 +08:00
|
|
|
if (!search.EndsWith("\\"))
|
2019-12-11 23:50:17 +08:00
|
|
|
{
|
2016-05-21 02:19:32 +08:00
|
|
|
search += "\\";
|
2019-12-11 23:50:17 +08:00
|
|
|
}
|
2015-08-23 15:40:18 +08:00
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
results.Add(CreateOpenCurrentFolderResult(incompleteName, search));
|
|
|
|
|
2019-12-11 23:50:17 +08:00
|
|
|
var searchOption = SearchOption.TopDirectoryOnly;
|
2019-12-07 18:44:58 +08:00
|
|
|
incompleteName += "*";
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-11 23:50:17 +08:00
|
|
|
// give the ability to search all folder when starting with >
|
|
|
|
if (incompleteName.StartsWith(">"))
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
searchOption = SearchOption.AllDirectories;
|
|
|
|
incompleteName = incompleteName.Substring(1);
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-03 22:25:19 +08:00
|
|
|
string searchNickname = new FolderLink { Path = query.Search }.Nickname;
|
2019-12-09 05:27:50 +08:00
|
|
|
try
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-09 05:27:50 +08:00
|
|
|
// search folder and add results
|
2019-12-11 23:50:17 +08:00
|
|
|
var directoryInfo = new DirectoryInfo(search);
|
2019-12-09 05:27:50 +08:00
|
|
|
var fileSystemInfos = directoryInfo.GetFileSystemInfos(incompleteName, searchOption);
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-09 05:27:50 +08:00
|
|
|
foreach (var fileSystemInfo in fileSystemInfos)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-09 05:27:50 +08:00
|
|
|
if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-09 05:27:50 +08:00
|
|
|
var result =
|
|
|
|
fileSystemInfo is DirectoryInfo
|
2019-12-11 23:50:17 +08:00
|
|
|
? CreateFolderResult(fileSystemInfo.Name, fileSystemInfo.FullName, query)
|
|
|
|
: CreateFileResult(fileSystemInfo.FullName, query);
|
2019-12-09 05:27:50 +08:00
|
|
|
results.Add(result);
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
2019-12-11 23:50:17 +08:00
|
|
|
catch (Exception e)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-09 05:27:50 +08:00
|
|
|
if (e is UnauthorizedAccessException || e is ArgumentException)
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-09 05:27:50 +08:00
|
|
|
results.Add(new Result { Title = e.Message, Score = 501 });
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-09 05:27:50 +08:00
|
|
|
return results;
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-09 05:27:50 +08:00
|
|
|
throw;
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
2015-01-07 18:59:55 +08:00
|
|
|
|
2019-12-11 23:50:17 +08:00
|
|
|
private static Result CreateFileResult(string filePath, Query query)
|
2019-12-07 18:44:58 +08:00
|
|
|
{
|
|
|
|
var result = new Result
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
Title = Path.GetFileName(filePath),
|
|
|
|
IcoPath = filePath,
|
2019-12-11 23:50:17 +08:00
|
|
|
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData,
|
2019-12-07 18:44:58 +08:00
|
|
|
Action = c =>
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
try
|
2014-07-19 10:12:11 +08:00
|
|
|
{
|
2019-12-07 18:44:58 +08:00
|
|
|
Process.Start(filePath);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + filePath);
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
private static Result CreateOpenCurrentFolderResult(string incompleteName, string search)
|
|
|
|
{
|
2019-12-09 18:27:33 +08:00
|
|
|
var firstResult = "Open current directory";
|
2019-12-07 18:44:58 +08:00
|
|
|
if (incompleteName.Length > 0)
|
|
|
|
firstResult = "Open " + search;
|
2019-12-09 18:27:33 +08:00
|
|
|
|
|
|
|
var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last();
|
|
|
|
|
2019-12-07 18:44:58 +08:00
|
|
|
return new Result
|
|
|
|
{
|
|
|
|
Title = firstResult,
|
2019-12-09 18:27:33 +08:00
|
|
|
SubTitle = $"Use > to search files and subfolders within {folderName}, " +
|
|
|
|
$"* to search for file extensions in {folderName} or both >* to combine the search",
|
2019-12-07 18:44:58 +08:00
|
|
|
IcoPath = search,
|
2019-12-09 05:29:20 +08:00
|
|
|
Score = 500,
|
2019-12-07 18:44:58 +08:00
|
|
|
Action = c =>
|
|
|
|
{
|
|
|
|
Process.Start(search);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
2015-01-07 18:59:55 +08:00
|
|
|
|
2015-02-07 20:17:49 +08:00
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
return _context.API.GetTranslation("wox_plugin_folder_plugin_name");
|
2015-02-07 20:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
{
|
2016-06-16 09:59:31 +08:00
|
|
|
return _context.API.GetTranslation("wox_plugin_folder_plugin_description");
|
2015-02-07 20:17:49 +08:00
|
|
|
}
|
2014-07-19 10:12:11 +08:00
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
}
|