diff --git a/Wox.Plugin.System/AppPathsProgramSource.cs b/Wox.Plugin.System/AppPathsProgramSource.cs new file mode 100644 index 0000000000..1a9ff6fcc1 --- /dev/null +++ b/Wox.Plugin.System/AppPathsProgramSource.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Plugin.System +{ + public class AppPathsProgramSource: AbstractProgramSource + { + public AppPathsProgramSource() + { + this.BonusPoints = -10; + } + + public override List LoadPrograms() + { + var list = new List(); + ReadAppPaths(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths", list); + ReadAppPaths(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths", list); //TODO: need test more on 64-bit + + return list; + } + + private void ReadAppPaths(string rootpath, List list) + { + using (var root = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(rootpath)) + { + if (root == null) return; + foreach (var item in root.GetSubKeyNames()) + { + using (var key = root.OpenSubKey(item)) + { + object path = key.GetValue(""); + if (path is string && global::System.IO.File.Exists((string)path)) + list.Add(CreateEntry((string)path)); + + key.Close(); + } + } + } + } + } +} diff --git a/Wox.Plugin.System/FileSystemProgramSource.cs b/Wox.Plugin.System/FileSystemProgramSource.cs new file mode 100644 index 0000000000..2960652ced --- /dev/null +++ b/Wox.Plugin.System/FileSystemProgramSource.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace Wox.Plugin.System +{ + public class FileSystemProgramSource : AbstractProgramSource + { + public string BaseDirectory; + public List Suffixes = new List() { "lnk", "exe" }; + + public FileSystemProgramSource(string baseDirectory) + { + BaseDirectory = baseDirectory; + } + + public FileSystemProgramSource(string baseDirectory, List suffixes) + : this(baseDirectory) + { + Suffixes = suffixes; + } + + public override List LoadPrograms() + { + List list = new List(); + GetAppFromDirectory(BaseDirectory, list); + + return list; + } + + private void GetAppFromDirectory(string path, List list) + { + foreach (string file in Directory.GetFiles(path)) + { + if (Suffixes.Any(o => file.EndsWith("." + o))) + { + Program p = CreateEntry(file); + list.Add(p); + } + } + + foreach (var subDirectory in Directory.GetDirectories(path)) + { + GetAppFromDirectory(subDirectory, list); + } + } + } +} diff --git a/Wox.Plugin.System/IProgramSource.cs b/Wox.Plugin.System/IProgramSource.cs new file mode 100644 index 0000000000..e243c5c5c3 --- /dev/null +++ b/Wox.Plugin.System/IProgramSource.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Plugin.System +{ + public interface IProgramSource + { + List LoadPrograms(); + int BonusPoints { get; set; } + } + + public abstract class AbstractProgramSource : IProgramSource + { + public abstract List LoadPrograms(); + + public int BonusPoints + { + get; set; + } + + protected Program CreateEntry(string file) + { + Program p = new Program() + { + Title = global::System.IO.Path.GetFileNameWithoutExtension(file), + IcoPath = file, + ExecutePath = file + }; + + return p; + } + } +} diff --git a/Wox.Plugin.System/Programs.cs b/Wox.Plugin.System/Programs.cs index 8e20825ff0..05c7f9ab2e 100644 --- a/Wox.Plugin.System/Programs.cs +++ b/Wox.Plugin.System/Programs.cs @@ -36,15 +36,13 @@ namespace Wox.Plugin.System public string IcoPath { get; set; } public string ExecutePath { get; set; } public int Score { get; set; } - + public IProgramSource Source { get; set; } } public class Programs : BaseSystemPlugin { - private List indexDirectory = new List(); - private List indexPostfix = new List { "lnk", "exe" }; - List installedList = new List(); + List sources = new List(); [DllImport("shell32.dll")] static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate); @@ -65,6 +63,7 @@ namespace Wox.Plugin.System return returnList.Select(c => new Result() { Title = c.Title, + SubTitle = c.ExecutePath, IcoPath = c.IcoPath, Score = 0, Action = (context) => @@ -105,47 +104,29 @@ namespace Wox.Plugin.System protected override void InitInternal(PluginInitContext context) { - indexDirectory.Add(Environment.GetFolderPath(Environment.SpecialFolder.Programs)); + sources.Add(new FileSystemProgramSource(Environment.GetFolderPath(Environment.SpecialFolder.Programs))); StringBuilder commonStartMenuPath = new StringBuilder(560); SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false); - indexDirectory.Add(commonStartMenuPath.ToString()); + sources.Add(new FileSystemProgramSource(commonStartMenuPath.ToString())); - GetAppFromStartMenu(); - } + sources.Add(new AppPathsProgramSource()); - private void GetAppFromStartMenu() - { - foreach (string directory in indexDirectory) + foreach (var source in sources) { - GetAppFromDirectory(directory); - } - } - - private void GetAppFromDirectory(string path) - { - foreach (string file in Directory.GetFiles(path)) - { - if (indexPostfix.Any(o => file.EndsWith("." + o))) + var list = source.LoadPrograms(); + list.ForEach(o => { - Program p = new Program() - { - Title = getAppNameFromAppPath(file), - IcoPath = file, - ExecutePath = file - }; - installedList.Add(p); - } - } - - foreach (var subDirectory in Directory.GetDirectories(path)) - { - GetAppFromDirectory(subDirectory); + o.Source = source; + }); + installedList.AddRange(list); } } private void ScoreFilter(Program p) { + p.Score += p.Source.BonusPoints; + if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start")) p.Score += 10; @@ -155,10 +136,5 @@ namespace Wox.Plugin.System if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall")) p.Score -= 20; } - - private string getAppNameFromAppPath(string app) - { - return global::System.IO.Path.GetFileNameWithoutExtension(app); - } } } diff --git a/Wox.Plugin.System/Wox.Plugin.System.csproj b/Wox.Plugin.System/Wox.Plugin.System.csproj index 6defda050e..66525c18a7 100644 --- a/Wox.Plugin.System/Wox.Plugin.System.csproj +++ b/Wox.Plugin.System/Wox.Plugin.System.csproj @@ -48,9 +48,12 @@ + + +