From 19617e9dae733d4292e4a033b42ea98d2e16b651 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Fri, 19 Aug 2016 23:05:59 +0100 Subject: [PATCH] Refactoring start menu source 1. refactoriong start menu source 2. fix depth problems involved in last commit --- Plugins/Wox.Plugin.Program/Main.cs | 30 ++++--- Plugins/Wox.Plugin.Program/Program.cs | 1 - .../Wox.Plugin.Program/ProgramSetting.xaml | 4 +- .../ProgramSources/AppPathsProgramSource.cs | 3 +- .../CommonStartMenuProgramSource.cs | 30 ------- .../ProgramSources/FileSystemProgramSource.cs | 37 +-------- .../ProgramSources/ProgramSource.cs | 31 -------- .../ProgramSources/StartMenu.cs | 23 ++++++ .../UserStartMenuProgramSource.cs | 14 ---- .../ProgramSources/Win32.cs | 79 +++++++++++++++++++ .../Wox.Plugin.Program.csproj | 4 +- 11 files changed, 126 insertions(+), 130 deletions(-) delete mode 100644 Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs create mode 100644 Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs delete mode 100644 Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs create mode 100644 Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index b81ba5f654..3807ee4130 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -152,24 +152,28 @@ namespace Wox.Plugin.Program private static List ProgramSources() { - var fileSystemSource = new List(); + var sources = new List(); + var source1 = _settings.ProgramSources; + sources.AddRange(source1); if (_settings.EnableStartMenuSource) { - fileSystemSource.Add(new CommonStartMenuProgramSource()); - fileSystemSource.Add(new UserStartMenuProgramSource()); + var source2 = new StartMenu(); + sources.Add(source2); } - fileSystemSource.AddRange(_settings.ProgramSources); - FileChangeWatcher.AddAll(fileSystemSource, _settings.ProgramSuffixes); - foreach (var s in fileSystemSource) - { - s.Suffixes = _settings.ProgramSuffixes; - } - - var sources = new List(); - sources.AddRange(fileSystemSource); if (_settings.EnableRegistrySource) { - sources.Add(new AppPathsProgramSource()); + var source3 = new AppPathsProgramSource(); + sources.Add(source3); + } + + FileChangeWatcher.AddAll(source1, _settings.ProgramSuffixes); + foreach (var source in sources) + { + var win32 = source as Win32; + if (win32 != null) + { + win32.Suffixes = _settings.ProgramSuffixes; + } } return sources; diff --git a/Plugins/Wox.Plugin.Program/Program.cs b/Plugins/Wox.Plugin.Program/Program.cs index ff468e674e..eea6c55f17 100644 --- a/Plugins/Wox.Plugin.Program/Program.cs +++ b/Plugins/Wox.Plugin.Program/Program.cs @@ -15,6 +15,5 @@ namespace Wox.Plugin.Program public string Directory { get; set; } public string ExecutableName { get; set; } public int Score { get; set; } - public ProgramSource Source { get; set; } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml index 59f38a3ef9..760bd0779f 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml +++ b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml @@ -25,14 +25,14 @@ Drop="programSourceView_Drop" > - + - + diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/AppPathsProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/AppPathsProgramSource.cs index 03bb88c496..d4bcd54612 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/AppPathsProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/AppPathsProgramSource.cs @@ -7,7 +7,7 @@ using Microsoft.Win32; namespace Wox.Plugin.Program.ProgramSources { [Serializable] - public class AppPathsProgramSource : ProgramSource + public class AppPathsProgramSource : Win32 { public override List LoadPrograms() { @@ -57,7 +57,6 @@ namespace Wox.Plugin.Program.ProgramSources { var entry = CreateEntry(path); entry.ExecutableName = subkey; - entry.Source = this; return entry; } } diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs deleted file mode 100644 index 185330be62..0000000000 --- a/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using System.Text; - -namespace Wox.Plugin.Program.ProgramSources -{ - [Serializable] - public sealed class CommonStartMenuProgramSource : FileSystemProgramSource - { - private const int CSIDL_COMMON_PROGRAMS = 0x17; - - // todo happlebao how to pass location before loadPrograms - public CommonStartMenuProgramSource() - { - Location = getPath(); - } - - [DllImport("shell32.dll")] - private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, - bool fCreate); - - private static string getPath() - { - var commonStartMenuPath = new StringBuilder(560); - SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false); - - return commonStartMenuPath.ToString(); - } - } -} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs index c2dc3abbe2..d63b6af6b5 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs @@ -8,18 +8,16 @@ using Wox.Infrastructure.Logger; namespace Wox.Plugin.Program.ProgramSources { [Serializable] - public class FileSystemProgramSource : ProgramSource + public class FileSystemProgramSource : Win32 { public string Location { get; set; } = ""; - public int MaxDepth { get; set; } = -1; - internal string[] Suffixes { get; set; } = { "" }; public override List LoadPrograms() { if (Directory.Exists(Location) && MaxDepth >= -1) { var apps = new List(); - GetAppFromDirectory(apps, Location, 0); + GetAppFromDirectory(apps, Location, MaxDepth); return apps; } else @@ -27,36 +25,5 @@ namespace Wox.Plugin.Program.ProgramSources return new List(); } } - - private void GetAppFromDirectory(List apps, string path, int depth) - { - if (MaxDepth != depth) - { - foreach (var file in Directory.GetFiles(path)) - { - if (Suffixes.Any(o => file.EndsWith("." + o))) - { - Program p; - try - { - p = CreateEntry(file); - } - catch (Exception e) - { - var woxPluginException = new WoxPluginException("Program", - $"GetAppFromDirectory failed: {path}", e); - Log.Exception(woxPluginException); - continue; - } - p.Source = this; - apps.Add(p); - } - } - foreach (var d in Directory.GetDirectories(path)) - { - GetAppFromDirectory(apps, d, depth + 1); - } - } - } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/ProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/ProgramSource.cs index 4090bf6b3f..8a15348328 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/ProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/ProgramSource.cs @@ -8,37 +8,6 @@ namespace Wox.Plugin.Program.ProgramSources [Serializable] public abstract class ProgramSource { - public abstract List LoadPrograms(); - - protected Program CreateEntry(string file) - { - var p = new Program - { - Title = Path.GetFileNameWithoutExtension(file), - IcoPath = file, - Path = file, - Directory = Directory.GetParent(file).FullName - }; - - switch (Path.GetExtension(file).ToLower()) - { - case ".exe": - p.ExecutableName = Path.GetFileName(file); - try - { - var versionInfo = FileVersionInfo.GetVersionInfo(file); - if (!string.IsNullOrEmpty(versionInfo.FileDescription)) - { - p.Title = versionInfo.FileDescription; - } - } - catch (Exception) - { - } - break; - } - return p; - } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs b/Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs new file mode 100644 index 0000000000..f14ad16038 --- /dev/null +++ b/Plugins/Wox.Plugin.Program/ProgramSources/StartMenu.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Wox.Plugin.Program.ProgramSources +{ + [Serializable] + class StartMenu : Win32 + { + public override List LoadPrograms() + { + var directory1 = Environment.GetFolderPath(Environment.SpecialFolder.Programs); + var directory2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms); + var programs = new List(); + GetAppFromDirectory(programs, directory1, MaxDepth); + GetAppFromDirectory(programs, directory2, MaxDepth); + return programs; + } + } +} diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs deleted file mode 100644 index d6489c0f45..0000000000 --- a/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace Wox.Plugin.Program.ProgramSources -{ - - [Serializable] - public sealed class UserStartMenuProgramSource : FileSystemProgramSource - { - public UserStartMenuProgramSource() - { - Location = Environment.GetFolderPath(Environment.SpecialFolder.Programs); - } - } -} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs b/Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs new file mode 100644 index 0000000000..e23b5683b4 --- /dev/null +++ b/Plugins/Wox.Plugin.Program/ProgramSources/Win32.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Wox.Infrastructure.Exception; +using Wox.Infrastructure.Logger; + +namespace Wox.Plugin.Program.ProgramSources +{ + [Serializable] + public abstract class Win32 : ProgramSource + { + public int MaxDepth { get; set; } = -1; + public string[] Suffixes { get; set; } = { "" }; + + protected Program CreateEntry(string file) + { + var p = new Program + { + Title = Path.GetFileNameWithoutExtension(file), + IcoPath = file, + Path = file, + Directory = Directory.GetParent(file).FullName + }; + + switch (Path.GetExtension(file).ToLower()) + { + case ".exe": + p.ExecutableName = Path.GetFileName(file); + try + { + var versionInfo = FileVersionInfo.GetVersionInfo(file); + if (!string.IsNullOrEmpty(versionInfo.FileDescription)) + { + p.Title = versionInfo.FileDescription; + } + } + catch (Exception) + { + } + break; + } + return p; + } + + protected void GetAppFromDirectory(List apps, string directory, int depth) + { + if (MaxDepth == -1 || MaxDepth < depth) + { + foreach (var f in Directory.GetFiles(directory)) + { + if (Suffixes.Any(o => f.EndsWith("." + o))) + { + Program p; + try + { + p = CreateEntry(f); + } + catch (Exception e) + { + var woxPluginException = new WoxPluginException("Program", + $"GetAppFromDirectory failed: {directory}", e); + Log.Exception(woxPluginException); + continue; + } + apps.Add(p); + } + } + foreach (var d in Directory.GetDirectories(directory)) + { + GetAppFromDirectory(apps, d, depth - 1); + } + } + } + } +} diff --git a/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj b/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj index 187948deaa..d129f10f6d 100644 --- a/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj +++ b/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj @@ -71,7 +71,9 @@ AddProgramSource.xaml + + @@ -81,9 +83,7 @@ - - ProgramSuffixes.xaml