From cf0d7067eb935ec260e26baeb29aae661a6fbfd3 Mon Sep 17 00:00:00 2001 From: Yeechan Lu Date: Wed, 19 Mar 2014 04:05:27 +0800 Subject: [PATCH] Add ProgramSources settings (code only, without GUI) #42 --- Wox.Infrastructure/CommonStorage.cs | 1 + .../UserSettings/ProgramSource.cs | 18 +++++++++ .../UserSettings/UserSetting.cs | 25 +++++++++++++ Wox.Infrastructure/Wox.Infrastructure.csproj | 1 + Wox.Plugin.System/AppPathsProgramSource.cs | 7 ++++ .../CommonStartMenuProgramSource.cs | 37 +++++++++++++++++++ Wox.Plugin.System/FileSystemProgramSource.cs | 6 +++ .../PortableAppsProgramSource.cs | 6 +++ Wox.Plugin.System/Programs.cs | 37 +++++++++++++------ .../UserStartMenuProgramSource.cs | 22 +++++++++++ Wox.Plugin.System/Wox.Plugin.System.csproj | 2 + 11 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 Wox.Infrastructure/UserSettings/ProgramSource.cs create mode 100644 Wox.Plugin.System/CommonStartMenuProgramSource.cs create mode 100644 Wox.Plugin.System/UserStartMenuProgramSource.cs diff --git a/Wox.Infrastructure/CommonStorage.cs b/Wox.Infrastructure/CommonStorage.cs index 8d3930e7a6..e8230ee32c 100644 --- a/Wox.Infrastructure/CommonStorage.cs +++ b/Wox.Infrastructure/CommonStorage.cs @@ -63,6 +63,7 @@ namespace Wox.Infrastructure Instance.UserSetting.Theme = "Dark"; Instance.UserSetting.ReplaceWinR = true; Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches(); + Instance.UserSetting.ProgramSources = Instance.UserSetting.LoadDefaultProgramSources(); Instance.UserSetting.Hotkey = "Win + W"; } diff --git a/Wox.Infrastructure/UserSettings/ProgramSource.cs b/Wox.Infrastructure/UserSettings/ProgramSource.cs new file mode 100644 index 0000000000..972c25b893 --- /dev/null +++ b/Wox.Infrastructure/UserSettings/ProgramSource.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Infrastructure.UserSettings +{ + [Serializable] + public class ProgramSource + { + public string Location { get; set; } + public string Assembly { get; set; } + public string Type { get; set; } + public int BounsPoints { get; set; } + public bool Enabled { get; set; } + public Dictionary Meta { get; set; } + } +} diff --git a/Wox.Infrastructure/UserSettings/UserSetting.cs b/Wox.Infrastructure/UserSettings/UserSetting.cs index c2309bae82..86c1eadfc3 100644 --- a/Wox.Infrastructure/UserSettings/UserSetting.cs +++ b/Wox.Infrastructure/UserSettings/UserSetting.cs @@ -9,6 +9,7 @@ namespace Wox.Infrastructure.UserSettings public string Theme { get; set; } public bool ReplaceWinR { get; set; } public List WebSearches { get; set; } + public List ProgramSources { get; set; } public List CustomPluginHotkeys { get; set; } public bool StartWoxOnSystemStartup { get; set; } @@ -39,5 +40,29 @@ namespace Wox.Infrastructure.UserSettings return webSearches; } + + public List LoadDefaultProgramSources() + { + var list = new List(); + list.Add(new ProgramSource() + { + BounsPoints = 0, + Enabled = true, + Type = "CommonStartMenuProgramSource" + }); + list.Add(new ProgramSource() + { + BounsPoints = 0, + Enabled = true, + Type = "UserStartMenuProgramSource" + }); + list.Add(new ProgramSource() + { + BounsPoints = -10, + Enabled = true, + Type = "AppPathsProgramSource" + }); + return list; + } } } diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index 06326b2f7a..4aab17fbe5 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -56,6 +56,7 @@ + diff --git a/Wox.Plugin.System/AppPathsProgramSource.cs b/Wox.Plugin.System/AppPathsProgramSource.cs index 1a9ff6fcc1..35b47ed0b8 100644 --- a/Wox.Plugin.System/AppPathsProgramSource.cs +++ b/Wox.Plugin.System/AppPathsProgramSource.cs @@ -5,6 +5,7 @@ using System.Text; namespace Wox.Plugin.System { + [global::System.ComponentModel.Browsable(false)] public class AppPathsProgramSource: AbstractProgramSource { public AppPathsProgramSource() @@ -12,6 +13,12 @@ namespace Wox.Plugin.System this.BonusPoints = -10; } + public AppPathsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + : this() + { + this.BonusPoints = source.BounsPoints; + } + public override List LoadPrograms() { var list = new List(); diff --git a/Wox.Plugin.System/CommonStartMenuProgramSource.cs b/Wox.Plugin.System/CommonStartMenuProgramSource.cs new file mode 100644 index 0000000000..dd973f4b31 --- /dev/null +++ b/Wox.Plugin.System/CommonStartMenuProgramSource.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace Wox.Plugin.System +{ + [global::System.ComponentModel.Browsable(false)] + public class CommonStartMenuProgramSource : FileSystemProgramSource + { + [DllImport("shell32.dll")] + static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate); + const int CSIDL_COMMON_STARTMENU = 0x16; // \Windows\Start Menu\Programs + const int CSIDL_COMMON_PROGRAMS = 0x17; + + private static string getPath() + { + StringBuilder commonStartMenuPath = new StringBuilder(560); + SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false); + + return commonStartMenuPath.ToString(); + } + + public CommonStartMenuProgramSource() + : base(getPath()) + { + } + + public CommonStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + : this() + { + this.BonusPoints = source.BounsPoints; + } + + } +} diff --git a/Wox.Plugin.System/FileSystemProgramSource.cs b/Wox.Plugin.System/FileSystemProgramSource.cs index 2960652ced..0ecd03fc15 100644 --- a/Wox.Plugin.System/FileSystemProgramSource.cs +++ b/Wox.Plugin.System/FileSystemProgramSource.cs @@ -22,6 +22,12 @@ namespace Wox.Plugin.System Suffixes = suffixes; } + public FileSystemProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + : this(source.Location) + { + this.BonusPoints = source.BounsPoints; + } + public override List LoadPrograms() { List list = new List(); diff --git a/Wox.Plugin.System/PortableAppsProgramSource.cs b/Wox.Plugin.System/PortableAppsProgramSource.cs index 7da206c393..17be681ffe 100644 --- a/Wox.Plugin.System/PortableAppsProgramSource.cs +++ b/Wox.Plugin.System/PortableAppsProgramSource.cs @@ -15,6 +15,12 @@ namespace Wox.Plugin.System BaseDirectory = baseDirectory; } + public PortableAppsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + : this(source.Location) + { + this.BonusPoints = source.BounsPoints; + } + public override List LoadPrograms() { List list = new List(); diff --git a/Wox.Plugin.System/Programs.cs b/Wox.Plugin.System/Programs.cs index 943273db55..8188880671 100644 --- a/Wox.Plugin.System/Programs.cs +++ b/Wox.Plugin.System/Programs.cs @@ -44,11 +44,13 @@ namespace Wox.Plugin.System { List installedList = new List(); List sources = new List(); - - [DllImport("shell32.dll")] - static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate); - const int CSIDL_COMMON_STARTMENU = 0x16; // \Windows\Start Menu\Programs - const int CSIDL_COMMON_PROGRAMS = 0x17; + Dictionary sourceTypes = new Dictionary() { + {"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)}, + {"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)}, + {"AppPathsProgramSource", typeof(AppPathsProgramSource)}, + {"PortableAppsProgramSource", typeof(PortableAppsProgramSource)}, + {"FileSystemProgramSource", typeof(FileSystemProgramSource)}, + }; protected override List QueryInternal(Query query) { @@ -106,13 +108,26 @@ namespace Wox.Plugin.System protected override void InitInternal(PluginInitContext context) { - sources.Add(new FileSystemProgramSource(Environment.GetFolderPath(Environment.SpecialFolder.Programs))); + if (CommonStorage.Instance.UserSetting.ProgramSources == null) + CommonStorage.Instance.UserSetting.ProgramSources = CommonStorage.Instance.UserSetting.LoadDefaultProgramSources(); - StringBuilder commonStartMenuPath = new StringBuilder(560); - SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false); - sources.Add(new FileSystemProgramSource(commonStartMenuPath.ToString())); - - sources.Add(new AppPathsProgramSource()); + CommonStorage.Instance.UserSetting.ProgramSources.ForEach(source => + { + if (source.Enabled) + { + Type sourceClass; + if (sourceTypes.TryGetValue(source.Type, out sourceClass)) + { + sources.Add(sourceClass.GetConstructor( + new Type[] { typeof(Wox.Infrastructure.UserSettings.ProgramSource) } + ).Invoke(new object[] { source }) as IProgramSource); + } + else + { + // TODO: invalid class + } + } + }); foreach (var source in sources) { diff --git a/Wox.Plugin.System/UserStartMenuProgramSource.cs b/Wox.Plugin.System/UserStartMenuProgramSource.cs new file mode 100644 index 0000000000..ae828ee138 --- /dev/null +++ b/Wox.Plugin.System/UserStartMenuProgramSource.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Plugin.System +{ + [global::System.ComponentModel.Browsable(false)] + public class UserStartMenuProgramSource : FileSystemProgramSource + { + public UserStartMenuProgramSource() + : base(Environment.GetFolderPath(Environment.SpecialFolder.Programs)) + { + } + + public UserStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) + : this() + { + this.BonusPoints = source.BounsPoints; + } + } +} diff --git a/Wox.Plugin.System/Wox.Plugin.System.csproj b/Wox.Plugin.System/Wox.Plugin.System.csproj index a98c52d3da..d6984b650e 100644 --- a/Wox.Plugin.System/Wox.Plugin.System.csproj +++ b/Wox.Plugin.System/Wox.Plugin.System.csproj @@ -52,12 +52,14 @@ + +