Combine FolderLinks and DirectoryIndicator into FileSystemPlugin

cc @aaroncampf
This commit is contained in:
Yeechan Lu
2014-04-13 15:38:12 +08:00
parent 36f3c293f0
commit c589978e84
10 changed files with 144 additions and 313 deletions

View File

@@ -6,10 +6,11 @@ using System.Linq;
using System.Text;
using System.Windows.Forms;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins
namespace Wox.Plugin.SystemPlugins.FileSystem
{
public class DirectoryIndicator : BaseSystemPlugin
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider
{
private PluginInitContext context;
private static List<string> driverNames = null;
@@ -17,6 +18,7 @@ namespace Wox.Plugin.SystemPlugins
protected override List<Result> QueryInternal(Query query)
{
//TODO: Consider always clearing the cache
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.RawQuery))
{
@@ -30,15 +32,49 @@ namespace Wox.Plugin.SystemPlugins
InitialDriverList();
var input = query.RawQuery.ToLower();
//if (driverNames.FirstOrDefault(x => input.StartsWith(x)) == null) return results;
if (!driverNames.Any(x => input.StartsWith(x))) return results;
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
if (Directory.Exists(query.RawQuery))
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))
{
// show all child directory
if (input.EndsWith("\\") || input.EndsWith("/"))
{
var dirInfo = new DirectoryInfo(query.RawQuery);
var dirInfo = new DirectoryInfo(currentPath);
var dirs = dirInfo.GetDirectories();
var parentDirKey = input.TrimEnd('\\', '/');
@@ -73,7 +109,7 @@ namespace Wox.Plugin.SystemPlugins
IcoPath = "Images/folder.png",
Action = (c) =>
{
Process.Start(query.RawQuery);
Process.Start(currentPath);
return true;
}
};
@@ -85,12 +121,12 @@ namespace Wox.Plugin.SystemPlugins
Result result = new Result
{
Title = "Open this directory",
SubTitle = string.Format("path: {0}", query.RawQuery),
SubTitle = string.Format("path: {0}", currentPath),
Score = 50,
IcoPath = "Images/folder.png",
Action = (c) =>
{
Process.Start(query.RawQuery);
Process.Start(currentPath);
return true;
}
};
@@ -108,7 +144,7 @@ namespace Wox.Plugin.SystemPlugins
{
var dirs = parentDirectories[parentDir];
var queryFileName = Path.GetFileName(query.RawQuery).ToLower();
var queryFileName = Path.GetFileName(currentPath).ToLower();
var fuzzy = FuzzyMatcher.Create(queryFileName);
foreach (var dir in dirs)
{
@@ -156,6 +192,12 @@ namespace Wox.Plugin.SystemPlugins
protected override void InitInternal(PluginInitContext context)
{
this.context = context;
if (UserSettingStorage.Instance.FolderLinks == null)
{
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
UserSettingStorage.Instance.Save();
}
}
public override string Name
@@ -168,6 +210,12 @@ namespace Wox.Plugin.SystemPlugins
get { return @"Images\folder.png"; }
}
public System.Windows.Controls.Control CreateSettingPanel()
{
return new FileSystemSettings();
}
public override string Description
{
get { return base.Description; }

View File

@@ -1,4 +1,4 @@
<UserControl x:Class="Wox.Plugin.SystemPlugins.Folder_Links.FolderLinksSettings"
<UserControl x:Class="Wox.Plugin.SystemPlugins.FileSystem.FileSystemSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

View File

@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins.FileSystem {
/// <summary>
/// Interaction logic for FileSystemSettings.xaml
/// </summary>
public partial class FileSystemSettings : UserControl {
public FileSystemSettings() {
InitializeComponent();
lbxFolders.ItemsSource = Wox.Infrastructure.Storage.UserSettings.UserSettingStorage.Instance.FolderLinks;
}
private void btnDelete_Click(object sender, RoutedEventArgs e) {
var selectedFolder = lbxFolders.SelectedItem as FolderLink;
if (selectedFolder != null)
{
UserSettingStorage.Instance.FolderLinks.Remove(selectedFolder);
lbxFolders.Items.Refresh();
}
else
{
MessageBox.Show("Please select a folder link!");
}
}
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
var selectedFolder = lbxFolders.SelectedItem as FolderLink;
if (selectedFolder != null)
{
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
folderBrowserDialog.SelectedPath = selectedFolder.Path;
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var link = UserSettingStorage.Instance.FolderLinks.First(x => x.Path == selectedFolder.Path);
link.Path = folderBrowserDialog.SelectedPath;
UserSettingStorage.Instance.Save();
}
lbxFolders.Items.Refresh();
}
else
{
MessageBox.Show("Please select a folder link!");
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e) {
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var newFolder = new Wox.Infrastructure.Storage.UserSettings.FolderLink() {
Path = folderBrowserDialog.SelectedPath
};
if (UserSettingStorage.Instance.FolderLinks == null) {
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
}
UserSettingStorage.Instance.FolderLinks.Add(newFolder);
UserSettingStorage.Instance.Save();
}
lbxFolders.Items.Refresh();
}
}
}

View File

@@ -1,10 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Plugin.SystemPlugins.Folder_Links {
public class FolderLink {
public string Path { get; set; }
}
}

View File

@@ -1,166 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins.Folder_Links {
public class FolderLinksPlugin : BaseSystemPlugin, ISettingProvider {
private PluginInitContext context;
//private static List<string> driverNames = null;
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
public override string Name { get { return "Folder Links"; } }
public override string Description { get { return "Adds additional folders you can search"; } }
public override string IcoPath { get { return @"Images\folder.png"; } }
protected override void InitInternal(PluginInitContext context) {
this.context = context;
if (UserSettingStorage.Instance.FolderLinks == null)
{
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
UserSettingStorage.Instance.Save();
}
}
public System.Windows.Controls.Control CreateSettingPanel() {
//return new WebSearchesSetting();
return new FolderLinksSettings();
}
protected override List<Result> QueryInternal(Query query) {
var Saved_Folders = UserSettingStorage.Instance.FolderLinks.Select(x => x.Path).ToList();
//TODO: Consider always clearing the cache
List<Result> results = new List<Result>();
if (string.IsNullOrEmpty(query.RawQuery)) {
// clear the cache
if (parentDirectories.Count > 0)
parentDirectories.Clear();
return results;
}
var input = query.RawQuery.ToLower();
var Input_Name = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
var Current_Path = Saved_Folders.FirstOrDefault(x =>
x.Split(new string[] { @"\" }, StringSplitOptions.None).Last().ToLower() == Input_Name);
foreach (var item in Saved_Folders) {
var Name = item.Split(new string[] { @"\" }, StringSplitOptions.None).Last();
if (Name.ToLower().StartsWith(input)) {
Result result = new Result {
Title = Name,
IcoPath = "Images/folder.png",
Action = (c) => {
context.ChangeQuery(item);
return false;
}
};
results.Add(result);
}
}
if (Current_Path == null) return results;
Current_Path += query.RawQuery.ToLower().Remove(0, Input_Name.Length);
if (Directory.Exists(Current_Path)) {
// show all child directory
if (input.EndsWith("\\") || input.EndsWith("/")) {
var dirInfo = new DirectoryInfo(Current_Path);
var dirs = dirInfo.GetDirectories();
var parentDirKey = input.TrimEnd('\\', '/');
if (!parentDirectories.ContainsKey(parentDirKey))
parentDirectories.Add(parentDirKey, dirs);
foreach (var dir in dirs) {
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) => {
context.ChangeQuery(dirPath);
return false;
}
};
results.Add(result);
}
if (results.Count == 0) {
Result result = new Result {
Title = "Open this directory",
SubTitle = "No files in this directory",
IcoPath = "Images/folder.png",
Action = (c) => {
Process.Start(Current_Path);
return true;
}
};
results.Add(result);
}
}
else {
Result result = new Result {
Title = "Open this directory",
SubTitle = string.Format("path: {0}", Current_Path),
Score = 50,
IcoPath = "Images/folder.png",
Action = (c) => {
Process.Start(Current_Path);
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)) {
var dirs = parentDirectories[parentDir];
var queryFileName = Path.GetFileName(Current_Path).ToLower();
var fuzzy = Wox.Infrastructure.FuzzyMatcher.Create(queryFileName);
foreach (var dir in dirs) {
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
continue;
var matchResult = fuzzy.Evaluate(dir.Name);
if (!matchResult.Success)
continue;
var dirPath = dir.FullName;
Result result = new Result {
Title = dir.Name,
IcoPath = "Images/folder.png",
Score = matchResult.Score,
Action = (c) => {
context.ChangeQuery(dirPath);
return false;
}
};
results.Add(result);
}
}
}
return results;
}
}
}

View File

@@ -1,64 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins.Folder_Links {
/// <summary>
/// Interaction logic for FolderLinksSettings.xaml
/// </summary>
public partial class FolderLinksSettings : UserControl {
public FolderLinksSettings() {
InitializeComponent();
lbxFolders.ItemsSource = Wox.Infrastructure.Storage.UserSettings.UserSettingStorage.Instance.FolderLinks;
}
private void btnDelete_Click(object sender, RoutedEventArgs e) {
}
private void btnEdit_Click(object sender, RoutedEventArgs e) {
var SelectedFolder = lbxFolders.SelectedItem as FolderLink;
var Folder = new System.Windows.Forms.FolderBrowserDialog();
Folder.SelectedPath = SelectedFolder.Path;
if (Folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var Result = UserSettingStorage.Instance.FolderLinks.First(x => x.Path == SelectedFolder.Path);
Result.Path = Folder.SelectedPath;
UserSettingStorage.Instance.Save();
}
lbxFolders.Items.Refresh();
}
private void btnAdd_Click(object sender, RoutedEventArgs e) {
var Folder = new System.Windows.Forms.FolderBrowserDialog();
if (Folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var New_Folder = new Wox.Infrastructure.Storage.UserSettings.FolderLink() {
Path = Folder.SelectedPath
};
if (UserSettingStorage.Instance.FolderLinks == null) {
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
}
UserSettingStorage.Instance.FolderLinks.Add(New_Folder);
UserSettingStorage.Instance.Save();
}
lbxFolders.Items.Refresh();
}
}
}

View File

@@ -1,32 +0,0 @@
<Window x:Class="Wox.Plugin.SystemPlugins.Folder_Links.FolderLinks_Editxaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="Folder"
Height="350"
Width="674.766"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
>
<Grid RenderTransformOrigin="0.504,0.245">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Margin="0,129,10,13" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Title:</TextBlock>
<TextBox x:Name="tbTitle" Margin="10,127.5,0,11.5" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1">
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10,27,10,109" Width="80" Height="25">Cancel</Button>
<Button x:Name="btnAdd" Margin="10,27,10,109" Width="80" Height="25" Click="btnAdd_OnClick">
<TextBlock x:Name="lblAdd">Add</TextBlock>
</Button>
</StackPanel>
</Grid>
</Window>

View File

@@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Wox.Plugin.SystemPlugins.Folder_Links {
/// <summary>
/// Interaction logic for FolderLinks_Editxaml.xaml
/// </summary>
public partial class FolderLinks_Editxaml : Window {
public FolderLinks_Editxaml() {
InitializeComponent();
}
}
}

View File

@@ -55,9 +55,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CMD\CMDStorage.cs" />
<Compile Include="Folder Links\FolderLinksPlugin.cs" />
<Compile Include="Folder Links\FolderLinksSettings.xaml.cs">
<DependentUpon>FolderLinksSettings.xaml</DependentUpon>
<Compile Include="FileSystem\FileSystemSettings.xaml.cs">
<DependentUpon>FileSystemSettings.xaml</DependentUpon>
</Compile>
<Compile Include="Program\ProgramSetting.xaml.cs">
<DependentUpon>ProgramSetting.xaml</DependentUpon>
@@ -79,7 +78,7 @@
</Compile>
<Compile Include="WebSearch\WebSearchPlugin.cs" />
<Compile Include="CMD\CMD.cs" />
<Compile Include="DirectoryIndicator.cs" />
<Compile Include="FileSystem\FileSystemPlugin.cs" />
<Compile Include="ISystemPlugin.cs" />
<Compile Include="Program\Programs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -102,7 +101,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="Folder Links\FolderLinksSettings.xaml">
<Page Include="FileSystem\FileSystemSettings.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>