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

237 lines
8.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
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;
using Wox.Infrastructure.Storage;
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;
private readonly Settings _settings;
2016-04-27 05:45:31 +08:00
private readonly PluginJsonStorage<Settings> _storage;
2016-05-07 10:55:09 +08:00
public Main()
{
2016-04-27 05:45:31 +08:00
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
2016-06-16 09:59:31 +08:00
InitialDriverList();
}
public void Save()
{
_storage.Save();
}
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
}
public void Init(PluginInitContext context)
2014-07-19 10:12:11 +08:00
{
2016-06-16 09:59:31 +08:00
_context = context;
2014-07-19 10:12:11 +08:00
}
public List<Result> Query(Query query)
2014-07-19 10:12:11 +08:00
{
2016-05-21 02:19:32 +08:00
string search = query.Search.ToLower();
2014-07-19 10:12:11 +08:00
List<FolderLink> userFolderLinks = _settings.FolderLinks.Where(
2016-05-21 02:19:32 +08:00
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase)).ToList();
2014-07-19 10:12:11 +08:00
List<Result> results =
userFolderLinks.Select(
item => new Result()
2014-07-19 10:12:11 +08:00
{
Title = item.Nickname,
2016-05-21 02:23:57 +08:00
IcoPath = item.Path,
SubTitle = "Ctrl + Enter to open the directory",
2019-12-03 22:25:19 +08:00
TitleHighlightData = StringMatcher.FuzzySearch(item.Nickname, search).MatchData,
2014-07-19 10:12:11 +08:00
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(item.Path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + item.Path);
return false;
}
}
2016-06-16 09:59:31 +08:00
_context.API.ChangeQuery($"{query.ActionKeyword} {item.Path}{(item.Path.EndsWith("\\") ? "" : "\\")}");
2014-07-19 10:12:11 +08:00
return false;
2015-02-04 23:16:41 +08:00
},
2016-05-05 23:30:56 +08:00
ContextData = item,
2014-07-19 10:12:11 +08:00
}).ToList();
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;
2015-08-23 15:40:18 +08:00
//if (!input.EndsWith("\\"))
//{
// //"c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive"
// input = input + "\\";
//}
2016-05-21 02:19:32 +08:00
results.AddRange(QueryInternal_Directory_Exists(query));
2014-07-19 10:12:11 +08:00
2016-05-05 23:30:56 +08:00
// todo temp hack for scores
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
}
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>();
2014-07-19 10:12:11 +08:00
DriveInfo[] allDrives = DriveInfo.GetDrives();
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
}
}
}
2016-05-21 02:19:32 +08:00
private List<Result> QueryInternal_Directory_Exists(Query query)
2014-07-19 10:12:11 +08:00
{
2016-05-21 02:19:32 +08:00
var search = query.Search.ToLower();
2014-07-19 10:12:11 +08:00
var results = new List<Result>();
2016-05-21 02:19:32 +08:00
2015-08-23 15:40:18 +08:00
string incompleteName = "";
2016-05-21 02:19:32 +08:00
if (!Directory.Exists(search + "\\"))
2015-08-23 15:40:18 +08:00
{
//if the last component of the path is incomplete,
//then make auto complete for it.
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
{
2016-05-21 02:19:32 +08:00
incompleteName = search.Substring(index + 1);
2015-08-23 15:40:18 +08:00
incompleteName = incompleteName.ToLower();
2016-05-21 02:19:32 +08:00
search = search.Substring(0, index + 1);
if (!Directory.Exists(search))
2015-08-23 15:40:18 +08:00
return results;
}
else
return results;
}
else
{
2016-05-21 02:19:32 +08:00
if (!search.EndsWith("\\"))
search += "\\";
2015-08-23 15:40:18 +08:00
}
2014-07-19 10:12:11 +08:00
2015-08-23 15:40:18 +08:00
string firstResult = "Open current directory";
if (incompleteName.Length > 0)
2016-05-21 02:19:32 +08:00
firstResult = "Open " + search;
results.Add(new Result
2014-07-19 10:12:11 +08:00
{
Title = firstResult,
2016-05-21 02:23:57 +08:00
IcoPath = search,
2014-07-19 10:12:11 +08:00
Score = 10000,
Action = c =>
{
2016-05-21 02:19:32 +08:00
Process.Start(search);
2014-07-19 10:12:11 +08:00
return true;
}
});
2019-12-03 22:25:19 +08:00
string searchNickname = new FolderLink { Path = query.Search }.Nickname;
2014-07-19 10:12:11 +08:00
//Add children directories
2016-05-21 02:19:32 +08:00
DirectoryInfo[] dirs = new DirectoryInfo(search).GetDirectories();
2014-07-19 10:12:11 +08:00
foreach (DirectoryInfo dir in dirs)
{
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
2015-08-23 15:40:18 +08:00
if (incompleteName.Length != 0 && !dir.Name.ToLower().StartsWith(incompleteName))
continue;
2014-07-19 10:12:11 +08:00
DirectoryInfo dirCopy = dir;
var result = new Result
2014-07-19 10:12:11 +08:00
{
Title = dir.Name,
2016-05-21 02:23:57 +08:00
IcoPath = dir.FullName,
SubTitle = "Ctrl + Enter to open the directory",
2019-12-03 22:25:19 +08:00
TitleHighlightData = StringMatcher.FuzzySearch(dir.Name, searchNickname).MatchData,
2014-07-19 10:12:11 +08:00
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(dirCopy.FullName);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + dirCopy.FullName);
return false;
}
}
2016-06-16 09:59:31 +08:00
_context.API.ChangeQuery($"{query.ActionKeyword} {dirCopy.FullName}\\");
2014-07-19 10:12:11 +08:00
return false;
}
};
results.Add(result);
}
//Add children files
2016-05-21 02:19:32 +08:00
FileInfo[] files = new DirectoryInfo(search).GetFiles();
2014-07-19 10:12:11 +08:00
foreach (FileInfo file in files)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
2015-08-23 15:40:18 +08:00
if (incompleteName.Length != 0 && !file.Name.ToLower().StartsWith(incompleteName))
continue;
2014-07-19 10:12:11 +08:00
string filePath = file.FullName;
var result = new Result
2014-07-19 10:12:11 +08:00
{
Title = Path.GetFileName(filePath),
2016-05-21 02:23:57 +08:00
IcoPath = filePath,
2019-12-03 22:25:19 +08:00
TitleHighlightData = StringMatcher.FuzzySearch(file.Name, searchNickname).MatchData,
2014-07-19 10:12:11 +08:00
Action = c =>
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
}
};
results.Add(result);
}
return results;
}
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
}
}