2014-01-06 21:25:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2014-02-09 20:55:18 +08:00
|
|
|
|
using System.Windows.Forms;
|
2014-03-24 21:14:56 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-04-13 15:38:12 +08:00
|
|
|
|
using Wox.Infrastructure.Storage.UserSettings;
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
2014-06-30 21:31:13 +08:00
|
|
|
|
namespace Wox.Plugin.SystemPlugins.Folder {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
2014-06-30 21:31:13 +08:00
|
|
|
|
public class FolderPlugin : BaseSystemPlugin, ISettingProvider {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2014-05-09 08:38:51 +08:00
|
|
|
|
private PluginInitContext context;
|
|
|
|
|
private static List<string> driverNames = null;
|
|
|
|
|
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
2014-06-30 21:31:13 +08:00
|
|
|
|
public override string Description {
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "Provide opening folder from wox directorily. You can add your favorite folders.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name { get { return "Folder"; } }
|
2014-05-10 09:24:16 +08:00
|
|
|
|
public override string IcoPath { get { return @"Images\folder.png"; } }
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
#endregion Properties
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
#region Misc
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
protected override void InitInternal(PluginInitContext context) {
|
|
|
|
|
this.context = context;
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
if (UserSettingStorage.Instance.FolderLinks == null) {
|
|
|
|
|
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
|
|
|
|
UserSettingStorage.Instance.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
public System.Windows.Controls.Control CreateSettingPanel() {
|
|
|
|
|
return new FileSystemSettings();
|
|
|
|
|
}
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
#endregion Misc
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
protected override List<Result> QueryInternal(Query query) {
|
2014-05-10 12:21:55 +08:00
|
|
|
|
var results = new List<Result>();
|
|
|
|
|
var input = query.RawQuery.ToLower();
|
|
|
|
|
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
|
|
|
|
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x => x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
var currentPath = link == null ? input : link.Path + input.Remove(0, inputName.Length);
|
|
|
|
|
InitialDriverList();
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
foreach (var item in UserSettingStorage.Instance.FolderLinks.Where(x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase))) {
|
|
|
|
|
results.Add(new Result(item.Nickname, "Images/folder.png") {
|
|
|
|
|
Action = (c) => {
|
|
|
|
|
context.ChangeQuery(item.Nickname);
|
|
|
|
|
return false;
|
2014-05-09 08:38:51 +08:00
|
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
|
});
|
2014-05-09 08:38:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
if (link == null && !driverNames.Any(input.StartsWith))
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
|
|
QueryInternal_Directory_Exists(currentPath, input, results);
|
|
|
|
|
|
|
|
|
|
return results;
|
2014-05-09 08:38:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitialDriverList() {
|
|
|
|
|
if (driverNames == null) {
|
|
|
|
|
driverNames = new List<string>();
|
|
|
|
|
var allDrives = DriveInfo.GetDrives();
|
|
|
|
|
foreach (var driver in allDrives) {
|
|
|
|
|
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
private void QueryInternal_Directory_Exists(string currentPath, string input, List<Result> results) {
|
|
|
|
|
string path = Directory.Exists(currentPath) ? new DirectoryInfo(currentPath).FullName : Path.GetDirectoryName(input);
|
2014-05-21 07:45:11 +08:00
|
|
|
|
if (!System.IO.Directory.Exists(path)) return;
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
results.Add(new Result("Open this directory", "Images/folder.png") {
|
|
|
|
|
Score = 100000,
|
|
|
|
|
Action = (c) => {
|
|
|
|
|
if (Directory.Exists(currentPath)) {
|
|
|
|
|
Process.Start(currentPath);
|
|
|
|
|
}
|
|
|
|
|
else if (currentPath.Contains("\\")) {
|
|
|
|
|
var index = currentPath.LastIndexOf("\\");
|
|
|
|
|
Process.Start(currentPath.Remove(index) + "\\");
|
|
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
//if (System.IO.Directory.Exists(input)) {
|
|
|
|
|
var dirs = new DirectoryInfo(path).GetDirectories();
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
var parentDirKey = input.TrimEnd('\\', '/');
|
|
|
|
|
if (!parentDirectories.ContainsKey(parentDirKey)) parentDirectories.Add(parentDirKey, dirs);
|
2014-05-10 09:24:16 +08:00
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower());
|
|
|
|
|
foreach (var dir in dirs) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) {
|
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
var result = new Result(dir.Name, "Images/folder.png") {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
Action = (c) => {
|
2014-05-21 07:45:11 +08:00
|
|
|
|
//context.ChangeQuery(dir.FullName);
|
|
|
|
|
context.ChangeQuery(input + dir.Name + "\\");
|
|
|
|
|
return false;
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|
2014-05-21 07:45:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (Path.GetFileName(currentPath).ToLower() != "") {
|
|
|
|
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
|
|
|
|
result.Score = matchResult.Score;
|
|
|
|
|
if (!matchResult.Success) continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.Add(result);
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|
2014-05-21 07:45:11 +08:00
|
|
|
|
//}
|
2014-05-09 08:38:51 +08:00
|
|
|
|
|
2014-05-10 09:24:16 +08:00
|
|
|
|
var Folder = Path.GetDirectoryName(currentPath);
|
|
|
|
|
if (Folder != null) {
|
2014-05-21 07:45:11 +08:00
|
|
|
|
|
|
|
|
|
//var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower());
|
|
|
|
|
foreach (var dir in new DirectoryInfo(Folder).GetFiles()) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
|
|
|
|
|
|
|
|
|
|
var dirPath = dir.FullName;
|
2014-05-21 07:45:11 +08:00
|
|
|
|
Result result = new Result(Path.GetFileNameWithoutExtension(dirPath), dirPath) {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
Action = (c) => {
|
|
|
|
|
try {
|
|
|
|
|
Process.Start(dirPath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
MessageBox.Show(ex.Message, "Could not start " + dir.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-21 07:45:11 +08:00
|
|
|
|
if (Path.GetFileName(currentPath) != "") {
|
2014-05-10 09:24:16 +08:00
|
|
|
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
|
|
|
|
result.Score = matchResult.Score;
|
|
|
|
|
if (!matchResult.Success) continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 08:38:51 +08:00
|
|
|
|
}
|
2014-05-10 09:24:16 +08:00
|
|
|
|
}
|