Refactor to support multiple program sources #42

App Paths is supported now #41
This commit is contained in:
Yeechan Lu
2014-03-19 02:06:51 +08:00
parent 47982d092b
commit 07c13d84be
5 changed files with 145 additions and 38 deletions

View 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();
}
}
}
}
}
}

View 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);
}
}
}
}

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

View File

@@ -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<string> indexDirectory = new List<string>();
private List<string> indexPostfix = new List<string> { "lnk", "exe" };
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);
@@ -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 (var source in sources)
{
foreach (string directory in indexDirectory)
var list = source.LoadPrograms();
list.ForEach(o =>
{
GetAppFromDirectory(directory);
}
}
private void GetAppFromDirectory(string path)
{
foreach (string file in Directory.GetFiles(path))
{
if (indexPostfix.Any(o => file.EndsWith("." + 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);
}
}
}

View File

@@ -48,9 +48,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppPathsProgramSource.cs" />
<Compile Include="IProgramSource.cs" />
<Compile Include="BaseSystemPlugin.cs" />
<Compile Include="BrowserBookmarks.cs" />
<Compile Include="Calculator.cs" />
<Compile Include="FileSystemProgramSource.cs" />
<Compile Include="WebSearchPlugin.cs" />
<Compile Include="WindowsShellRun.cs" />
<Compile Include="CMD.cs" />