mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Refactor to support multiple program sources #42
App Paths is supported now #41
This commit is contained in:
43
Wox.Plugin.System/AppPathsProgramSource.cs
Normal file
43
Wox.Plugin.System/AppPathsProgramSource.cs
Normal file
@@ -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<Program> LoadPrograms()
|
||||||
|
{
|
||||||
|
var list = new List<Program>();
|
||||||
|
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<Program> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
Wox.Plugin.System/FileSystemProgramSource.cs
Normal file
50
Wox.Plugin.System/FileSystemProgramSource.cs
Normal file
@@ -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<string> Suffixes = new List<string>() { "lnk", "exe" };
|
||||||
|
|
||||||
|
public FileSystemProgramSource(string baseDirectory)
|
||||||
|
{
|
||||||
|
BaseDirectory = baseDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileSystemProgramSource(string baseDirectory, List<string> suffixes)
|
||||||
|
: this(baseDirectory)
|
||||||
|
{
|
||||||
|
Suffixes = suffixes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override List<Program> LoadPrograms()
|
||||||
|
{
|
||||||
|
List<Program> list = new List<Program>();
|
||||||
|
GetAppFromDirectory(BaseDirectory, list);
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GetAppFromDirectory(string path, List<Program> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
Wox.Plugin.System/IProgramSource.cs
Normal file
35
Wox.Plugin.System/IProgramSource.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Wox.Plugin.System
|
||||||
|
{
|
||||||
|
public interface IProgramSource
|
||||||
|
{
|
||||||
|
List<Program> LoadPrograms();
|
||||||
|
int BonusPoints { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class AbstractProgramSource : IProgramSource
|
||||||
|
{
|
||||||
|
public abstract List<Program> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,15 +36,13 @@ namespace Wox.Plugin.System
|
|||||||
public string IcoPath { get; set; }
|
public string IcoPath { get; set; }
|
||||||
public string ExecutePath { get; set; }
|
public string ExecutePath { get; set; }
|
||||||
public int Score { get; set; }
|
public int Score { get; set; }
|
||||||
|
public IProgramSource Source { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Programs : BaseSystemPlugin
|
public class Programs : BaseSystemPlugin
|
||||||
{
|
{
|
||||||
private List<string> indexDirectory = new List<string>();
|
|
||||||
private List<string> indexPostfix = new List<string> { "lnk", "exe" };
|
|
||||||
|
|
||||||
List<Program> installedList = new List<Program>();
|
List<Program> installedList = new List<Program>();
|
||||||
|
List<IProgramSource> sources = new List<IProgramSource>();
|
||||||
|
|
||||||
[DllImport("shell32.dll")]
|
[DllImport("shell32.dll")]
|
||||||
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate);
|
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()
|
return returnList.Select(c => new Result()
|
||||||
{
|
{
|
||||||
Title = c.Title,
|
Title = c.Title,
|
||||||
|
SubTitle = c.ExecutePath,
|
||||||
IcoPath = c.IcoPath,
|
IcoPath = c.IcoPath,
|
||||||
Score = 0,
|
Score = 0,
|
||||||
Action = (context) =>
|
Action = (context) =>
|
||||||
@@ -105,47 +104,29 @@ namespace Wox.Plugin.System
|
|||||||
|
|
||||||
protected override void InitInternal(PluginInitContext context)
|
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);
|
StringBuilder commonStartMenuPath = new StringBuilder(560);
|
||||||
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false);
|
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 (var source in sources)
|
||||||
{
|
|
||||||
foreach (string directory in indexDirectory)
|
|
||||||
{
|
{
|
||||||
GetAppFromDirectory(directory);
|
var list = source.LoadPrograms();
|
||||||
}
|
list.ForEach(o =>
|
||||||
}
|
|
||||||
|
|
||||||
private void GetAppFromDirectory(string path)
|
|
||||||
{
|
|
||||||
foreach (string file in Directory.GetFiles(path))
|
|
||||||
{
|
|
||||||
if (indexPostfix.Any(o => file.EndsWith("." + o)))
|
|
||||||
{
|
{
|
||||||
Program p = new Program()
|
o.Source = source;
|
||||||
{
|
});
|
||||||
Title = getAppNameFromAppPath(file),
|
installedList.AddRange(list);
|
||||||
IcoPath = file,
|
|
||||||
ExecutePath = file
|
|
||||||
};
|
|
||||||
installedList.Add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var subDirectory in Directory.GetDirectories(path))
|
|
||||||
{
|
|
||||||
GetAppFromDirectory(subDirectory);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ScoreFilter(Program p)
|
private void ScoreFilter(Program p)
|
||||||
{
|
{
|
||||||
|
p.Score += p.Source.BonusPoints;
|
||||||
|
|
||||||
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
|
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
|
||||||
p.Score += 10;
|
p.Score += 10;
|
||||||
|
|
||||||
@@ -155,10 +136,5 @@ namespace Wox.Plugin.System
|
|||||||
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
|
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
|
||||||
p.Score -= 20;
|
p.Score -= 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string getAppNameFromAppPath(string app)
|
|
||||||
{
|
|
||||||
return global::System.IO.Path.GetFileNameWithoutExtension(app);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,9 +48,12 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="AppPathsProgramSource.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="WebSearchPlugin.cs" />
|
<Compile Include="WebSearchPlugin.cs" />
|
||||||
<Compile Include="WindowsShellRun.cs" />
|
<Compile Include="WindowsShellRun.cs" />
|
||||||
<Compile Include="CMD.cs" />
|
<Compile Include="CMD.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user