Add ProgramSources settings (code only, without GUI) #42

This commit is contained in:
Yeechan Lu
2014-03-19 04:05:27 +08:00
parent baa50069cf
commit cf0d7067eb
11 changed files with 151 additions and 11 deletions

View File

@@ -63,6 +63,7 @@ namespace Wox.Infrastructure
Instance.UserSetting.Theme = "Dark"; Instance.UserSetting.Theme = "Dark";
Instance.UserSetting.ReplaceWinR = true; Instance.UserSetting.ReplaceWinR = true;
Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches(); Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches();
Instance.UserSetting.ProgramSources = Instance.UserSetting.LoadDefaultProgramSources();
Instance.UserSetting.Hotkey = "Win + W"; Instance.UserSetting.Hotkey = "Win + W";
} }

View File

@@ -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<string, string> Meta { get; set; }
}
}

View File

@@ -9,6 +9,7 @@ namespace Wox.Infrastructure.UserSettings
public string Theme { get; set; } public string Theme { get; set; }
public bool ReplaceWinR { get; set; } public bool ReplaceWinR { get; set; }
public List<WebSearch> WebSearches { get; set; } public List<WebSearch> WebSearches { get; set; }
public List<ProgramSource> ProgramSources { get; set; }
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; } public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; }
public bool StartWoxOnSystemStartup { get; set; } public bool StartWoxOnSystemStartup { get; set; }
@@ -39,5 +40,29 @@ namespace Wox.Infrastructure.UserSettings
return webSearches; return webSearches;
} }
public List<ProgramSource> LoadDefaultProgramSources()
{
var list = new List<ProgramSource>();
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;
}
} }
} }

View File

@@ -56,6 +56,7 @@
<Compile Include="IniParser.cs" /> <Compile Include="IniParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UserSettings\PluginHotkey.cs" /> <Compile Include="UserSettings\PluginHotkey.cs" />
<Compile Include="UserSettings\ProgramSource.cs" />
<Compile Include="UserSettings\UserSelectedRecords.cs" /> <Compile Include="UserSettings\UserSelectedRecords.cs" />
<Compile Include="UserSettings\UserSetting.cs" /> <Compile Include="UserSettings\UserSetting.cs" />
<Compile Include="UserSettings\WebSearch.cs" /> <Compile Include="UserSettings\WebSearch.cs" />

View File

@@ -5,6 +5,7 @@ using System.Text;
namespace Wox.Plugin.System namespace Wox.Plugin.System
{ {
[global::System.ComponentModel.Browsable(false)]
public class AppPathsProgramSource: AbstractProgramSource public class AppPathsProgramSource: AbstractProgramSource
{ {
public AppPathsProgramSource() public AppPathsProgramSource()
@@ -12,6 +13,12 @@ namespace Wox.Plugin.System
this.BonusPoints = -10; this.BonusPoints = -10;
} }
public AppPathsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this()
{
this.BonusPoints = source.BounsPoints;
}
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
{ {
var list = new List<Program>(); var list = new List<Program>();

View File

@@ -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;
}
}
}

View File

@@ -22,6 +22,12 @@ namespace Wox.Plugin.System
Suffixes = suffixes; Suffixes = suffixes;
} }
public FileSystemProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this(source.Location)
{
this.BonusPoints = source.BounsPoints;
}
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
{ {
List<Program> list = new List<Program>(); List<Program> list = new List<Program>();

View File

@@ -15,6 +15,12 @@ namespace Wox.Plugin.System
BaseDirectory = baseDirectory; BaseDirectory = baseDirectory;
} }
public PortableAppsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this(source.Location)
{
this.BonusPoints = source.BounsPoints;
}
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
{ {
List<Program> list = new List<Program>(); List<Program> list = new List<Program>();

View File

@@ -44,11 +44,13 @@ namespace Wox.Plugin.System
{ {
List<Program> installedList = new List<Program>(); List<Program> installedList = new List<Program>();
List<IProgramSource> sources = new List<IProgramSource>(); List<IProgramSource> sources = new List<IProgramSource>();
Dictionary<string, Type> sourceTypes = new Dictionary<string, Type>() {
[DllImport("shell32.dll")] {"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)},
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate); {"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)},
const int CSIDL_COMMON_STARTMENU = 0x16; // \Windows\Start Menu\Programs {"AppPathsProgramSource", typeof(AppPathsProgramSource)},
const int CSIDL_COMMON_PROGRAMS = 0x17; {"PortableAppsProgramSource", typeof(PortableAppsProgramSource)},
{"FileSystemProgramSource", typeof(FileSystemProgramSource)},
};
protected override List<Result> QueryInternal(Query query) protected override List<Result> QueryInternal(Query query)
{ {
@@ -106,13 +108,26 @@ namespace Wox.Plugin.System
protected override void InitInternal(PluginInitContext context) 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); CommonStorage.Instance.UserSetting.ProgramSources.ForEach(source =>
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false); {
sources.Add(new FileSystemProgramSource(commonStartMenuPath.ToString())); if (source.Enabled)
{
sources.Add(new AppPathsProgramSource()); 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) foreach (var source in sources)
{ {

View File

@@ -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;
}
}
}

View File

@@ -52,12 +52,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AppPathsProgramSource.cs" /> <Compile Include="AppPathsProgramSource.cs" />
<Compile Include="CommonStartMenuProgramSource.cs" />
<Compile Include="PortableAppsProgramSource.cs" /> <Compile Include="PortableAppsProgramSource.cs" />
<Compile Include="IProgramSource.cs" /> <Compile Include="IProgramSource.cs" />
<Compile Include="BaseSystemPlugin.cs" /> <Compile Include="BaseSystemPlugin.cs" />
<Compile Include="BrowserBookmarks.cs" /> <Compile Include="BrowserBookmarks.cs" />
<Compile Include="Calculator.cs" /> <Compile Include="Calculator.cs" />
<Compile Include="FileSystemProgramSource.cs" /> <Compile Include="FileSystemProgramSource.cs" />
<Compile Include="UserStartMenuProgramSource.cs" />
<Compile Include="WebSearchPlugin.cs" /> <Compile Include="WebSearchPlugin.cs" />
<Compile Include="WindowsShellRun.cs" /> <Compile Include="WindowsShellRun.cs" />
<Compile Include="CMD.cs" /> <Compile Include="CMD.cs" />