diff --git a/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs b/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs index 3e09d62b84..6027621de4 100644 --- a/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs +++ b/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs @@ -1,6 +1,6 @@ using System.Windows; using System.Windows.Forms; -using Wox.Plugin.Program.ProgramSources; +using Wox.Plugin.Program.Programs; namespace Wox.Plugin.Program { @@ -9,7 +9,7 @@ namespace Wox.Plugin.Program /// public partial class AddProgramSource { - private UnregisteredPrograms _editing; + private Settings.ProgramSource _editing; private Settings _settings; public AddProgramSource(Settings settings) @@ -19,7 +19,7 @@ namespace Wox.Plugin.Program Directory.Focus(); } - public AddProgramSource(UnregisteredPrograms edit, Settings settings) + public AddProgramSource(Settings.ProgramSource edit, Settings settings) { _editing = edit; _settings = settings; @@ -49,7 +49,7 @@ namespace Wox.Plugin.Program if(_editing == null) { - var source = new UnregisteredPrograms + var source = new Settings.ProgramSource { Location = Directory.Text, MaxDepth = max diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index d5d249e1cf..49669fa812 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -7,14 +7,14 @@ using System.Windows.Controls; using Wox.Infrastructure; using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage; -using Wox.Plugin.Program.ProgramSources; +using Wox.Plugin.Program.Programs; using Stopwatch = Wox.Infrastructure.Stopwatch; namespace Wox.Plugin.Program { public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable { - private static List _programs = new List(); + private static List _programs = new List(); private static List _uwps = new List(); private PluginInitContext _context; @@ -59,12 +59,12 @@ namespace Wox.Plugin.Program return result; } - public Result ResultFromProgram(Program p) + public Result ResultFromProgram(Win32 p) { var result = new Result { Title = p.Title, - SubTitle = p.Path, + SubTitle = p.ExecutablePath, IcoPath = p.IcoPath, Score = p.Score, ContextData = p, @@ -72,7 +72,7 @@ namespace Wox.Plugin.Program { var info = new ProcessStartInfo { - FileName = p.Path, + FileName = p.ExecutablePath, WorkingDirectory = p.Directory }; var hide = StartProcess(info); @@ -101,7 +101,7 @@ namespace Wox.Plugin.Program } - private int Score(Program program, string query) + private int Score(Win32 program, string query) { var score1 = StringMatcher.Score(program.Title, query); var score2 = StringMatcher.ScoreForPinyin(program.Title, query); @@ -128,59 +128,27 @@ namespace Wox.Plugin.Program public static void IndexPrograms() { - var sources = ProgramSources(); - - - var programs = sources.AsParallel() - .SelectMany(s => s.LoadPrograms()) - // filter duplicate program - .GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName }) - .Select(g => g.First()); - programs = programs.Select(ScoreFilter); - - _programs = programs.ToList(); - _cache.Programs = _programs; - - var windows10 = new Version(10, 0); - var support = Environment.OSVersion.Version.Major >= windows10.Major; - if (support) - { - _uwps = UWP.All(); - } + _cache.Programs = AllWin32Programs(); + _uwps = UWP.All(); } - private static List ProgramSources() + private static List AllWin32Programs() { - var sources = new List(); - var source1 = _settings.ProgramSources; - sources.AddRange(source1); - if (_settings.EnableStartMenuSource) - { - var source2 = new StartMenu(); - sources.Add(source2); - } - if (_settings.EnableRegistrySource) - { - var source3 = new AppPathsPrograms(); - sources.Add(source3); - } + var appPaths = AppPathsPrograms.All(); + var startMenu = StartMenu.All(_settings.ProgramSuffixes); + var unregistered = UnregisteredPrograms.All(_settings.ProgramSources, _settings.ProgramSuffixes); + var programs = appPaths.Concat(startMenu).Concat(unregistered); - FileChangeWatcher.AddAll(source1, _settings.ProgramSuffixes); - foreach (var source in sources) - { - var win32 = source as Win32; - if (win32 != null) - { - win32.Suffixes = _settings.ProgramSuffixes; - } - } + programs = programs.AsParallel() + // filter duplicate program + .GroupBy(x => new { ExecutePath = x.ExecutablePath, ExecuteName = x.ExecutableName }) + .Select(g => g.First()) + .Select(ScoreFilter); - return sources; + return programs.ToList(); } - - - private static Program ScoreFilter(Program p) + private static Win32 ScoreFilter(Win32 p) { var start = new[] { "启动", "start" }; var doc = new[] { "帮助", "help", "文档", "documentation" }; @@ -222,7 +190,7 @@ namespace Wox.Plugin.Program public List LoadContextMenus(Result selectedResult) { - Program p = selectedResult.ContextData as Program; + Win32 p = selectedResult.ContextData as Win32; if (p != null) { List contextMenus = new List @@ -234,7 +202,7 @@ namespace Wox.Plugin.Program { var info = new ProcessStartInfo { - FileName = p.Path, + FileName = p.ExecutablePath, WorkingDirectory = p.Directory, Verb = "runas" }; diff --git a/Plugins/Wox.Plugin.Program/ProgramIndexCache.cs b/Plugins/Wox.Plugin.Program/ProgramIndexCache.cs index 11c0c41163..480f7d66d6 100644 --- a/Plugins/Wox.Plugin.Program/ProgramIndexCache.cs +++ b/Plugins/Wox.Plugin.Program/ProgramIndexCache.cs @@ -1,11 +1,12 @@ using System; using System.Collections.Generic; +using Wox.Plugin.Program.Programs; namespace Wox.Plugin.Program { [Serializable] public class ProgramIndexCache { - public List Programs = new List(); + public List Programs = new List(); } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs index 163d12f128..5bbb90e9a6 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using Wox.Plugin.Program.ProgramSources; +using Wox.Plugin.Program.Programs; namespace Wox.Plugin.Program { @@ -51,7 +51,7 @@ namespace Wox.Plugin.Program private void btnDeleteProgramSource_OnClick(object sender, RoutedEventArgs e) { - var selectedProgramSource = programSourceView.SelectedItem as UnregisteredPrograms; + var selectedProgramSource = programSourceView.SelectedItem as Settings.ProgramSource; if (selectedProgramSource != null) { string msg = string.Format(context.API.GetTranslation("wox_plugin_program_delete_program_source"), selectedProgramSource.Location); @@ -71,7 +71,7 @@ namespace Wox.Plugin.Program private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e) { - var selectedProgramSource = programSourceView.SelectedItem as UnregisteredPrograms; + var selectedProgramSource = programSourceView.SelectedItem as Settings.ProgramSource; if (selectedProgramSource != null) { var add = new AddProgramSource(selectedProgramSource, _settings); @@ -120,7 +120,7 @@ namespace Wox.Plugin.Program { if (Directory.Exists(s)) { - _settings.ProgramSources.Add(new UnregisteredPrograms + _settings.ProgramSources.Add(new Settings.ProgramSource { Location = s }); diff --git a/Plugins/Wox.Plugin.Program/Programs/AppPathsPrograms.cs b/Plugins/Wox.Plugin.Program/Programs/AppPathsPrograms.cs index 6f37847dd7..c7af623356 100644 --- a/Plugins/Wox.Plugin.Program/Programs/AppPathsPrograms.cs +++ b/Plugins/Wox.Plugin.Program/Programs/AppPathsPrograms.cs @@ -4,15 +4,15 @@ using System.IO; using System.Linq; using Microsoft.Win32; -namespace Wox.Plugin.Program.ProgramSources +namespace Wox.Plugin.Program.Programs { [Serializable] public class AppPathsPrograms : Win32 { - public override List LoadPrograms() + public static IEnumerable All() { // https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121 - var programs = new List(); + var programs = new List(); const string appPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"; using (var root = Registry.LocalMachine.OpenSubKey(appPaths)) { @@ -31,7 +31,7 @@ namespace Wox.Plugin.Program.ProgramSources return programs; } - private IEnumerable ProgramsFromRegistryKey(RegistryKey root) + private static IEnumerable ProgramsFromRegistryKey(RegistryKey root) { var programs = root.GetSubKeyNames() .Select(subkey => ProgramFromRegistrySubkey(root, subkey)) @@ -39,7 +39,7 @@ namespace Wox.Plugin.Program.ProgramSources return programs; } - private Program ProgramFromRegistrySubkey(RegistryKey root, string subkey) + private static Win32 ProgramFromRegistrySubkey(RegistryKey root, string subkey) { using (var key = root.OpenSubKey(subkey)) { @@ -62,7 +62,7 @@ namespace Wox.Plugin.Program.ProgramSources } } } - return new Program(); + return new Win32(); } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/Programs/Program.cs b/Plugins/Wox.Plugin.Program/Programs/Program.cs deleted file mode 100644 index 721e2ec717..0000000000 --- a/Plugins/Wox.Plugin.Program/Programs/Program.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Wox.Plugin.Program -{ - [Serializable] - public class Program - { - public string Title { get; set; } - public string IcoPath { get; set; } - public string Path { get; set; } - public string Directory { get; set; } - public string ExecutableName { get; set; } - public int Score { get; set; } - } -} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/Programs/ProgramSource.cs b/Plugins/Wox.Plugin.Program/Programs/ProgramSource.cs deleted file mode 100644 index 2a6c6a43b8..0000000000 --- a/Plugins/Wox.Plugin.Program/Programs/ProgramSource.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Wox.Plugin.Program.ProgramSources -{ - [Serializable] - public abstract class ProgramSource - { - public abstract List LoadPrograms(); - } -} \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/Programs/StartMenu.cs b/Plugins/Wox.Plugin.Program/Programs/StartMenu.cs index 2ee63ac2d4..e8a364cffc 100644 --- a/Plugins/Wox.Plugin.Program/Programs/StartMenu.cs +++ b/Plugins/Wox.Plugin.Program/Programs/StartMenu.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Generic; -namespace Wox.Plugin.Program.ProgramSources +namespace Wox.Plugin.Program.Programs { [Serializable] class StartMenu : Win32 { - public override List LoadPrograms() + public static IEnumerable All(string[] suffixes) { 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); + var programs = new List(); + GetAppFromDirectory(programs, directory1, -1, suffixes); + GetAppFromDirectory(programs, directory2, -1, suffixes); return programs; } } diff --git a/Plugins/Wox.Plugin.Program/Programs/UWP.cs b/Plugins/Wox.Plugin.Program/Programs/UWP.cs index 11c9cc818e..fd7c3a52b6 100644 --- a/Plugins/Wox.Plugin.Program/Programs/UWP.cs +++ b/Plugins/Wox.Plugin.Program/Programs/UWP.cs @@ -14,11 +14,12 @@ using Windows.Management.Deployment; using Windows.Storage.Streams; using AppxPackaing; using Shell; +using Wox.Infrastructure; using Wox.Infrastructure.Logger; using IStream = AppxPackaing.IStream; using Rect = System.Windows.Rect; -namespace Wox.Plugin.Program.ProgramSources +namespace Wox.Plugin.Program.Programs { public class UWP { @@ -119,26 +120,35 @@ namespace Wox.Plugin.Program.ProgramSources public static List All() { - var packages = CurrentUserPackages(); - var uwps = new List(); - Parallel.ForEach(packages, p => + var windows10 = new Version(10, 0); + var support = Environment.OSVersion.Version.Major >= windows10.Major; + if (support) { - try + var packages = CurrentUserPackages(); + var uwps = new List(); + Parallel.ForEach(packages, p => { - var u = new UWP(p); - if (u.Apps.Length > 0) + try { - uwps.Add(u); + var u = new UWP(p); + if (u.Apps.Length > 0) + { + uwps.Add(u); + } } - } - catch (Exception e) - { - // if there are errors, just ignore it and continue - var message = $"Can't parse {p.Id.Name}: {e.Message}"; - Log.Error(message); - } - }); - return uwps; + catch (Exception e) + { + // if there are errors, just ignore it and continue + var message = $"Can't parse {p.Id.Name}: {e.Message}"; + Log.Error(message); + } + }); + return uwps; + } + else + { + return new List(); + } } private static IEnumerable CurrentUserPackages() @@ -244,8 +254,7 @@ namespace Wox.Plugin.Program.ProgramSources } else { - // todo: replaced with wox error logo - return new BitmapImage(); + return new BitmapImage(new Uri(Constant.ErrorIcon)); } } @@ -310,8 +319,7 @@ namespace Wox.Plugin.Program.ProgramSources } else { - var bitmap = new BitmapImage(); - return bitmap; + return new BitmapImage(new Uri(Constant.ErrorIcon)); } } diff --git a/Plugins/Wox.Plugin.Program/Programs/UnregisteredPrograms.cs b/Plugins/Wox.Plugin.Program/Programs/UnregisteredPrograms.cs index 1418cea5ba..58a629d807 100644 --- a/Plugins/Wox.Plugin.Program/Programs/UnregisteredPrograms.cs +++ b/Plugins/Wox.Plugin.Program/Programs/UnregisteredPrograms.cs @@ -2,25 +2,22 @@ using System.Collections.Generic; using System.IO; -namespace Wox.Plugin.Program.ProgramSources +namespace Wox.Plugin.Program.Programs { [Serializable] public class UnregisteredPrograms : Win32 { - public string Location { get; set; } = ""; - - public override List LoadPrograms() + public static List All(List sources, string[] suffixes) { - if (Directory.Exists(Location) && MaxDepth >= -1) + List programs = new List(); + foreach (var source in sources) { - var apps = new List(); - GetAppFromDirectory(apps, Location, MaxDepth); - return apps; - } - else - { - return new List(); + if (System.IO.Directory.Exists(source.Location) && source.MaxDepth >= -1) + { + GetAppFromDirectory(programs, source.Location, source.MaxDepth, suffixes); + } } + return programs; } } } \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index cbd89f7103..8533a36b0c 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -6,22 +6,26 @@ using System.Linq; using Wox.Infrastructure.Exception; using Wox.Infrastructure.Logger; -namespace Wox.Plugin.Program.ProgramSources +namespace Wox.Plugin.Program.Programs { [Serializable] - public abstract class Win32 : ProgramSource + public class Win32 { - public int MaxDepth { get; set; } = -1; - public string[] Suffixes { get; set; } = { "" }; + public string Title { get; set; } + public string IcoPath { get; set; } + public string ExecutablePath { get; set; } + public string Directory { get; set; } + public string ExecutableName { get; set; } + public int Score { get; set; } - protected Program CreateEntry(string file) + protected static Win32 CreateEntry(string file) { - var p = new Program + var p = new Win32 { Title = Path.GetFileNameWithoutExtension(file), IcoPath = file, - Path = file, - Directory = Directory.GetParent(file).FullName + ExecutablePath = file, + Directory = System.IO.Directory.GetParent(file).FullName }; switch (Path.GetExtension(file).ToLower()) @@ -44,33 +48,42 @@ namespace Wox.Plugin.Program.ProgramSources return p; } - protected void GetAppFromDirectory(List apps, string directory, int depth) + protected static void GetAppFromDirectory(List apps, string directory, int depth, string[] suffixes) { - if (MaxDepth == -1 || MaxDepth < depth) + if (depth == -1) { - foreach (var f in Directory.GetFiles(directory)) + } + else if (depth > 0) + { + depth = depth - 1; + } + else + { + return; + } + + foreach (var f in System.IO.Directory.GetFiles(directory)) + { + if (suffixes.Any(o => f.EndsWith("." + o))) { - if (Suffixes.Any(o => f.EndsWith("." + o))) + Win32 p; + try { - 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); + 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); - } + } + foreach (var d in System.IO.Directory.GetDirectories(directory)) + { + GetAppFromDirectory(apps, d, depth, suffixes); } } } diff --git a/Plugins/Wox.Plugin.Program/Settings.cs b/Plugins/Wox.Plugin.Program/Settings.cs index 6eb0754fcc..fb1dabbf32 100644 --- a/Plugins/Wox.Plugin.Program/Settings.cs +++ b/Plugins/Wox.Plugin.Program/Settings.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; -using Wox.Plugin.Program.ProgramSources; +using Wox.Plugin.Program.Programs; namespace Wox.Plugin.Program { public class Settings { - public List ProgramSources { get; set; } = new List(); + public List ProgramSources { get; set; } = new List(); public string[] ProgramSuffixes { get; set; } = {"bat", "appref-ms", "exe", "lnk"}; public bool EnableStartMenuSource { get; set; } = true; @@ -13,5 +13,11 @@ namespace Wox.Plugin.Program public bool EnableRegistrySource { get; set; } = true; internal const char SuffixSeperator = ';'; + + public class ProgramSource + { + public string Location { get; set; } + public int MaxDepth { get; set; } + } } } diff --git a/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj b/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj index 7963513395..6c7ac1f2f6 100644 --- a/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj +++ b/Plugins/Wox.Plugin.Program/Wox.Plugin.Program.csproj @@ -75,13 +75,11 @@ - ProgramSetting.xaml -