Skip to content

Commit

Permalink
Add ProgramSources settings (code only, without GUI) #42
Browse files Browse the repository at this point in the history
  • Loading branch information
orzFly committed Mar 18, 2014
1 parent baa5006 commit cf0d706
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 11 deletions.
1 change: 1 addition & 0 deletions Wox.Infrastructure/CommonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private static void LoadDefaultUserSetting()
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";
}

Expand Down
18 changes: 18 additions & 0 deletions Wox.Infrastructure/UserSettings/ProgramSource.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
25 changes: 25 additions & 0 deletions Wox.Infrastructure/UserSettings/UserSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class UserSetting
public string Theme { get; set; }
public bool ReplaceWinR { get; set; }
public List<WebSearch> WebSearches { get; set; }
public List<ProgramSource> ProgramSources { get; set; }
public List<CustomPluginHotkey> CustomPluginHotkeys { get; set; }
public bool StartWoxOnSystemStartup { get; set; }

Expand Down Expand Up @@ -39,5 +40,29 @@ public List<WebSearch> LoadDefaultWebSearches()

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;
}
}
}
1 change: 1 addition & 0 deletions Wox.Infrastructure/Wox.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="IniParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UserSettings\PluginHotkey.cs" />
<Compile Include="UserSettings\ProgramSource.cs" />
<Compile Include="UserSettings\UserSelectedRecords.cs" />
<Compile Include="UserSettings\UserSetting.cs" />
<Compile Include="UserSettings\WebSearch.cs" />
Expand Down
7 changes: 7 additions & 0 deletions Wox.Plugin.System/AppPathsProgramSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

namespace Wox.Plugin.System
{
[global::System.ComponentModel.Browsable(false)]
public class AppPathsProgramSource: AbstractProgramSource
{
public AppPathsProgramSource()
{
this.BonusPoints = -10;
}

public AppPathsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this()
{
this.BonusPoints = source.BounsPoints;
}

public override List<Program> LoadPrograms()
{
var list = new List<Program>();
Expand Down
37 changes: 37 additions & 0 deletions Wox.Plugin.System/CommonStartMenuProgramSource.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
6 changes: 6 additions & 0 deletions Wox.Plugin.System/FileSystemProgramSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public FileSystemProgramSource(string baseDirectory, List<string> suffixes)
Suffixes = suffixes;
}

public FileSystemProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this(source.Location)
{
this.BonusPoints = source.BounsPoints;
}

public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
Expand Down
6 changes: 6 additions & 0 deletions Wox.Plugin.System/PortableAppsProgramSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public PortableAppsProgramSource(string baseDirectory)
BaseDirectory = baseDirectory;
}

public PortableAppsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source)
: this(source.Location)
{
this.BonusPoints = source.BounsPoints;
}

public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
Expand Down
37 changes: 26 additions & 11 deletions Wox.Plugin.System/Programs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public class Programs : BaseSystemPlugin
{
List<Program> installedList = new List<Program>();
List<IProgramSource> sources = new List<IProgramSource>();

[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<string, Type> sourceTypes = new Dictionary<string, Type>() {
{"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)},
{"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)},
{"AppPathsProgramSource", typeof(AppPathsProgramSource)},
{"PortableAppsProgramSource", typeof(PortableAppsProgramSource)},
{"FileSystemProgramSource", typeof(FileSystemProgramSource)},
};

protected override List<Result> QueryInternal(Query query)
{
Expand Down Expand Up @@ -106,13 +108,26 @@ private bool MatchProgram(Program program, FuzzyMatcher matcher)

protected override void InitInternal(PluginInitContext context)
{
sources.Add(new FileSystemProgramSource(Environment.GetFolderPath(Environment.SpecialFolder.Programs)));

StringBuilder commonStartMenuPath = new StringBuilder(560);
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false);
sources.Add(new FileSystemProgramSource(commonStartMenuPath.ToString()));
if (CommonStorage.Instance.UserSetting.ProgramSources == null)
CommonStorage.Instance.UserSetting.ProgramSources = CommonStorage.Instance.UserSetting.LoadDefaultProgramSources();

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)
{
Expand Down
22 changes: 22 additions & 0 deletions Wox.Plugin.System/UserStartMenuProgramSource.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 2 additions & 0 deletions Wox.Plugin.System/Wox.Plugin.System.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AppPathsProgramSource.cs" />
<Compile Include="CommonStartMenuProgramSource.cs" />
<Compile Include="PortableAppsProgramSource.cs" />
<Compile Include="IProgramSource.cs" />
<Compile Include="BaseSystemPlugin.cs" />
<Compile Include="BrowserBookmarks.cs" />
<Compile Include="Calculator.cs" />
<Compile Include="FileSystemProgramSource.cs" />
<Compile Include="UserStartMenuProgramSource.cs" />
<Compile Include="WebSearchPlugin.cs" />
<Compile Include="WindowsShellRun.cs" />
<Compile Include="CMD.cs" />
Expand Down

0 comments on commit cf0d706

Please sign in to comment.