From c589978e84fbfe75b0c1c95b08c5e78271a8fb86 Mon Sep 17 00:00:00 2001 From: Yeechan Lu Date: Sun, 13 Apr 2014 15:38:12 +0800 Subject: [PATCH] Combine FolderLinks and DirectoryIndicator into FileSystemPlugin cc @aaroncampf --- .../Storage/UserSettings/FolderLink.cs | 9 +- .../FileSystemPlugin.cs} | 68 +++++-- .../FileSystemSettings.xaml} | 2 +- .../FileSystem/FileSystemSettings.xaml.cs | 73 ++++++++ .../Folder Links/FolderLink.cs | 10 -- .../Folder Links/FolderLinksPlugin.cs | 166 ------------------ .../Folder Links/FolderLinksSettings.xaml.cs | 64 ------- .../Folder Links/FolderLinks_Edit.xaml | 32 ---- .../Folder Links/FolderLinks_Edit.xaml.cs | 24 --- .../Wox.Plugin.SystemPlugins.csproj | 9 +- 10 files changed, 144 insertions(+), 313 deletions(-) rename Wox.Plugin.SystemPlugins/{DirectoryIndicator.cs => FileSystem/FileSystemPlugin.cs} (70%) rename Wox.Plugin.SystemPlugins/{Folder Links/FolderLinksSettings.xaml => FileSystem/FileSystemSettings.xaml} (93%) create mode 100644 Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml.cs delete mode 100644 Wox.Plugin.SystemPlugins/Folder Links/FolderLink.cs delete mode 100644 Wox.Plugin.SystemPlugins/Folder Links/FolderLinksPlugin.cs delete mode 100644 Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml.cs delete mode 100644 Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml delete mode 100644 Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml.cs diff --git a/Wox.Infrastructure/Storage/UserSettings/FolderLink.cs b/Wox.Infrastructure/Storage/UserSettings/FolderLink.cs index 2479443ed2..a311517500 100644 --- a/Wox.Infrastructure/Storage/UserSettings/FolderLink.cs +++ b/Wox.Infrastructure/Storage/UserSettings/FolderLink.cs @@ -2,9 +2,16 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Newtonsoft.Json; namespace Wox.Infrastructure.Storage.UserSettings { public class FolderLink { + [JsonProperty] public string Path { get; set; } - } + + public string Nickname + { + get { return Path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); } + } + } } diff --git a/Wox.Plugin.SystemPlugins/DirectoryIndicator.cs b/Wox.Plugin.SystemPlugins/FileSystem/FileSystemPlugin.cs similarity index 70% rename from Wox.Plugin.SystemPlugins/DirectoryIndicator.cs rename to Wox.Plugin.SystemPlugins/FileSystem/FileSystemPlugin.cs index da3c398eee..46bc2e5089 100644 --- a/Wox.Plugin.SystemPlugins/DirectoryIndicator.cs +++ b/Wox.Plugin.SystemPlugins/FileSystem/FileSystemPlugin.cs @@ -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 driverNames = null; @@ -17,6 +18,7 @@ namespace Wox.Plugin.SystemPlugins protected override List QueryInternal(Query query) { + //TODO: Consider always clearing the cache List results = new List(); 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(); + 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; } diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml b/Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml similarity index 93% rename from Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml rename to Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml index 8817bf3a64..2eb0888e18 100644 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml +++ b/Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml @@ -1,4 +1,4 @@ - + /// Interaction logic for FileSystemSettings.xaml + /// + 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(); + } + + UserSettingStorage.Instance.FolderLinks.Add(newFolder); + UserSettingStorage.Instance.Save(); + } + + lbxFolders.Items.Refresh(); + } + } +} diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLink.cs b/Wox.Plugin.SystemPlugins/Folder Links/FolderLink.cs deleted file mode 100644 index a61a18cb0b..0000000000 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLink.cs +++ /dev/null @@ -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; } - } -} diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksPlugin.cs b/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksPlugin.cs deleted file mode 100644 index c97216589e..0000000000 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksPlugin.cs +++ /dev/null @@ -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 driverNames = null; - private static Dictionary parentDirectories = new Dictionary(); - - 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(); - UserSettingStorage.Instance.Save(); - } - } - - public System.Windows.Controls.Control CreateSettingPanel() { - //return new WebSearchesSetting(); - return new FolderLinksSettings(); - } - - protected override List QueryInternal(Query query) { - var Saved_Folders = UserSettingStorage.Instance.FolderLinks.Select(x => x.Path).ToList(); - - //TODO: Consider always clearing the cache - List results = new List(); - 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; - } - - - } -} diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml.cs b/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml.cs deleted file mode 100644 index 210a9127e2..0000000000 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinksSettings.xaml.cs +++ /dev/null @@ -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 { - /// - /// Interaction logic for FolderLinksSettings.xaml - /// - 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(); - } - - UserSettingStorage.Instance.FolderLinks.Add(New_Folder); - UserSettingStorage.Instance.Save(); - } - - lbxFolders.Items.Refresh(); - } - } -} diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml b/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml deleted file mode 100644 index 0a11d5b599..0000000000 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - Title: - - - - - - - diff --git a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml.cs b/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml.cs deleted file mode 100644 index 869f8825a7..0000000000 --- a/Wox.Plugin.SystemPlugins/Folder Links/FolderLinks_Edit.xaml.cs +++ /dev/null @@ -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 { - /// - /// Interaction logic for FolderLinks_Editxaml.xaml - /// - public partial class FolderLinks_Editxaml : Window { - public FolderLinks_Editxaml() { - InitializeComponent(); - } - } -} diff --git a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj index 90eabc7c28..89b0103ed7 100644 --- a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj +++ b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj @@ -55,9 +55,8 @@ - - - FolderLinksSettings.xaml + + FileSystemSettings.xaml ProgramSetting.xaml @@ -79,7 +78,7 @@ - + @@ -102,7 +101,7 @@ - + Designer MSBuild:Compile