2014-01-06 21:25:24 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
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-04-13 15:38:12 +08:00
|
|
|
|
namespace Wox.Plugin.SystemPlugins.FileSystem
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-04-13 15:38:12 +08:00
|
|
|
|
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-03-24 00:51:07 +08:00
|
|
|
|
private PluginInitContext context;
|
2014-03-22 16:25:22 +08:00
|
|
|
|
private static List<string> driverNames = null;
|
|
|
|
|
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
|
|
|
|
|
2014-01-15 22:45:02 +08:00
|
|
|
|
protected override List<Result> QueryInternal(Query query)
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-04-13 15:38:12 +08:00
|
|
|
|
//TODO: Consider always clearing the cache
|
2014-01-06 21:25:24 +08:00
|
|
|
|
List<Result> results = new List<Result>();
|
2014-03-22 16:25:22 +08:00
|
|
|
|
if (string.IsNullOrEmpty(query.RawQuery))
|
|
|
|
|
{
|
|
|
|
|
// clear the cache
|
|
|
|
|
if (parentDirectories.Count > 0)
|
|
|
|
|
parentDirectories.Clear();
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:51:07 +08:00
|
|
|
|
InitialDriverList();
|
|
|
|
|
|
2014-03-22 16:25:22 +08:00
|
|
|
|
var input = query.RawQuery.ToLower();
|
2014-04-13 15:38:12 +08:00
|
|
|
|
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
2014-04-13 15:38:12 +08:00
|
|
|
|
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
|
|
|
|
|
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
var currentPath = link != null ? link.Path : null;
|
|
|
|
|
|
|
|
|
|
foreach (var item in UserSettingStorage.Instance.FolderLinks)
|
|
|
|
|
{
|
|
|
|
|
var Name = item.Nickname;
|
|
|
|
|
|
|
|
|
|
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length)
|
|
|
|
|
{
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
|
|
|
|
Title = Name,
|
|
|
|
|
IcoPath = "Images/folder.png",
|
|
|
|
|
Action = (c) =>
|
|
|
|
|
{
|
|
|
|
|
context.ChangeQuery(item.Nickname);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentPath == null)
|
|
|
|
|
{
|
|
|
|
|
if (!driverNames.Any(input.StartsWith))
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
|
|
currentPath = input;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
currentPath += input.Remove(0, inputName.Length);
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(currentPath))
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-03-22 16:25:22 +08:00
|
|
|
|
// show all child directory
|
|
|
|
|
if (input.EndsWith("\\") || input.EndsWith("/"))
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-04-13 15:38:12 +08:00
|
|
|
|
var dirInfo = new DirectoryInfo(currentPath);
|
2014-03-22 16:25:22 +08:00
|
|
|
|
var dirs = dirInfo.GetDirectories();
|
|
|
|
|
|
|
|
|
|
var parentDirKey = input.TrimEnd('\\', '/');
|
|
|
|
|
if (!parentDirectories.ContainsKey(parentDirKey))
|
|
|
|
|
parentDirectories.Add(parentDirKey, dirs);
|
|
|
|
|
|
|
|
|
|
foreach (var dir in dirs)
|
2014-02-09 20:55:18 +08:00
|
|
|
|
{
|
2014-03-22 16:25:22 +08:00
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var dirPath = dir.FullName;
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
|
|
|
|
Title = dir.Name,
|
|
|
|
|
IcoPath = "Images/folder.png",
|
|
|
|
|
Action = (c) =>
|
|
|
|
|
{
|
2014-03-24 00:51:07 +08:00
|
|
|
|
context.ChangeQuery(dirPath);
|
|
|
|
|
return false;
|
2014-03-22 16:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (results.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
2014-03-24 00:51:07 +08:00
|
|
|
|
Title = "Open this directory",
|
|
|
|
|
SubTitle = "No files in this directory",
|
2014-03-22 16:25:22 +08:00
|
|
|
|
IcoPath = "Images/folder.png",
|
2014-03-24 00:51:07 +08:00
|
|
|
|
Action = (c) =>
|
|
|
|
|
{
|
2014-04-13 15:38:12 +08:00
|
|
|
|
Process.Start(currentPath);
|
2014-03-24 00:51:07 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-03-22 16:25:22 +08:00
|
|
|
|
};
|
|
|
|
|
results.Add(result);
|
2014-02-09 20:55:18 +08:00
|
|
|
|
}
|
2014-03-22 16:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
|
|
|
|
Title = "Open this directory",
|
2014-04-13 15:38:12 +08:00
|
|
|
|
SubTitle = string.Format("path: {0}", currentPath),
|
2014-03-22 16:25:22 +08:00
|
|
|
|
Score = 50,
|
|
|
|
|
IcoPath = "Images/folder.png",
|
|
|
|
|
Action = (c) =>
|
|
|
|
|
{
|
2014-04-13 15:38:12 +08:00
|
|
|
|
Process.Start(currentPath);
|
2014-03-22 16:25:22 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// change to search in current directory
|
|
|
|
|
var parentDir = Path.GetDirectoryName(input);
|
|
|
|
|
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
parentDir = parentDir.TrimEnd('\\', '/');
|
|
|
|
|
if (parentDirectories.ContainsKey(parentDir))
|
|
|
|
|
{
|
2014-03-24 21:14:56 +08:00
|
|
|
|
|
2014-03-22 16:25:22 +08:00
|
|
|
|
var dirs = parentDirectories[parentDir];
|
2014-04-13 15:38:12 +08:00
|
|
|
|
var queryFileName = Path.GetFileName(currentPath).ToLower();
|
2014-03-24 21:14:56 +08:00
|
|
|
|
var fuzzy = FuzzyMatcher.Create(queryFileName);
|
2014-03-22 16:25:22 +08:00
|
|
|
|
foreach (var dir in dirs)
|
|
|
|
|
{
|
|
|
|
|
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
|
|
|
continue;
|
|
|
|
|
|
2014-03-24 21:14:56 +08:00
|
|
|
|
var matchResult = fuzzy.Evaluate(dir.Name);
|
|
|
|
|
if (!matchResult.Success)
|
2014-03-22 16:25:22 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var dirPath = dir.FullName;
|
|
|
|
|
Result result = new Result
|
|
|
|
|
{
|
|
|
|
|
Title = dir.Name,
|
|
|
|
|
IcoPath = "Images/folder.png",
|
2014-03-24 21:14:56 +08:00
|
|
|
|
Score = matchResult.Score,
|
2014-03-22 16:25:22 +08:00
|
|
|
|
Action = (c) =>
|
|
|
|
|
{
|
2014-03-24 00:51:07 +08:00
|
|
|
|
context.ChangeQuery(dirPath);
|
|
|
|
|
return false;
|
2014-03-22 16:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
results.Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-06 21:25:24 +08:00
|
|
|
|
}
|
2014-03-22 16:25:22 +08:00
|
|
|
|
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:51:07 +08:00
|
|
|
|
private void InitialDriverList()
|
2014-01-06 21:25:24 +08:00
|
|
|
|
{
|
2014-03-22 16:25:22 +08:00
|
|
|
|
if (driverNames == null)
|
|
|
|
|
{
|
|
|
|
|
driverNames = new List<string>();
|
|
|
|
|
var allDrives = DriveInfo.GetDrives();
|
|
|
|
|
foreach (var driver in allDrives)
|
|
|
|
|
{
|
|
|
|
|
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-06 21:25:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:51:07 +08:00
|
|
|
|
protected override void InitInternal(PluginInitContext context)
|
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
2014-04-13 15:38:12 +08:00
|
|
|
|
|
|
|
|
|
if (UserSettingStorage.Instance.FolderLinks == null)
|
|
|
|
|
{
|
|
|
|
|
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
|
|
|
|
UserSettingStorage.Instance.Save();
|
|
|
|
|
}
|
2014-03-24 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 22:42:28 +08:00
|
|
|
|
public override string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "File System"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string IcoPath
|
|
|
|
|
{
|
|
|
|
|
get { return @"Images\folder.png"; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-13 15:38:12 +08:00
|
|
|
|
|
|
|
|
|
public System.Windows.Controls.Control CreateSettingPanel()
|
|
|
|
|
{
|
|
|
|
|
return new FileSystemSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 22:42:28 +08:00
|
|
|
|
public override string Description
|
|
|
|
|
{
|
|
|
|
|
get { return base.Description; }
|
|
|
|
|
}
|
2014-01-06 21:25:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|