Refactoring start menu source

1. refactoriong start menu source
2. fix depth problems involved in last commit
This commit is contained in:
bao-qian 2016-08-19 23:05:59 +01:00
parent f8cc54d4f2
commit 19617e9dae
11 changed files with 126 additions and 130 deletions

View file

@ -152,24 +152,28 @@ namespace Wox.Plugin.Program
private static List<ProgramSource> ProgramSources() private static List<ProgramSource> ProgramSources()
{ {
var fileSystemSource = new List<FileSystemProgramSource>(); var sources = new List<ProgramSource>();
var source1 = _settings.ProgramSources;
sources.AddRange(source1);
if (_settings.EnableStartMenuSource) if (_settings.EnableStartMenuSource)
{ {
fileSystemSource.Add(new CommonStartMenuProgramSource()); var source2 = new StartMenu();
fileSystemSource.Add(new UserStartMenuProgramSource()); sources.Add(source2);
} }
fileSystemSource.AddRange(_settings.ProgramSources);
FileChangeWatcher.AddAll(fileSystemSource, _settings.ProgramSuffixes);
foreach (var s in fileSystemSource)
{
s.Suffixes = _settings.ProgramSuffixes;
}
var sources = new List<ProgramSource>();
sources.AddRange(fileSystemSource);
if (_settings.EnableRegistrySource) if (_settings.EnableRegistrySource)
{ {
sources.Add(new AppPathsProgramSource()); var source3 = new AppPathsProgramSource();
sources.Add(source3);
}
FileChangeWatcher.AddAll(source1, _settings.ProgramSuffixes);
foreach (var source in sources)
{
var win32 = source as Win32;
if (win32 != null)
{
win32.Suffixes = _settings.ProgramSuffixes;
}
} }
return sources; return sources;

View file

@ -15,6 +15,5 @@ namespace Wox.Plugin.Program
public string Directory { get; set; } public string Directory { get; set; }
public string ExecutableName { get; set; } public string ExecutableName { get; set; }
public int Score { get; set; } public int Score { get; set; }
public ProgramSource Source { get; set; }
} }
} }

View file

@ -25,14 +25,14 @@
Drop="programSourceView_Drop" > Drop="programSourceView_Drop" >
<ListView.View> <ListView.View>
<GridView> <GridView>
<GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="250"> <GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="450">
<GridViewColumn.CellTemplate> <GridViewColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding Location, ConverterParameter=(null), Converter={program:LocationConverter}}"/> <TextBlock Text="{Binding Location, ConverterParameter=(null), Converter={program:LocationConverter}}"/>
</DataTemplate> </DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn.CellTemplate>
</GridViewColumn> </GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="75"> <GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="130">
<GridViewColumn.CellTemplate> <GridViewColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding MaxDepth, ConverterParameter=(null)}"/> <TextBlock Text="{Binding MaxDepth, ConverterParameter=(null)}"/>

View file

@ -7,7 +7,7 @@ using Microsoft.Win32;
namespace Wox.Plugin.Program.ProgramSources namespace Wox.Plugin.Program.ProgramSources
{ {
[Serializable] [Serializable]
public class AppPathsProgramSource : ProgramSource public class AppPathsProgramSource : Win32
{ {
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
{ {
@ -57,7 +57,6 @@ namespace Wox.Plugin.Program.ProgramSources
{ {
var entry = CreateEntry(path); var entry = CreateEntry(path);
entry.ExecutableName = subkey; entry.ExecutableName = subkey;
entry.Source = this;
return entry; return entry;
} }
} }

View file

@ -1,30 +0,0 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public sealed class CommonStartMenuProgramSource : FileSystemProgramSource
{
private const int CSIDL_COMMON_PROGRAMS = 0x17;
// todo happlebao how to pass location before loadPrograms
public CommonStartMenuProgramSource()
{
Location = getPath();
}
[DllImport("shell32.dll")]
private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder,
bool fCreate);
private static string getPath()
{
var commonStartMenuPath = new StringBuilder(560);
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false);
return commonStartMenuPath.ToString();
}
}
}

View file

@ -8,18 +8,16 @@ using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program.ProgramSources namespace Wox.Plugin.Program.ProgramSources
{ {
[Serializable] [Serializable]
public class FileSystemProgramSource : ProgramSource public class FileSystemProgramSource : Win32
{ {
public string Location { get; set; } = ""; public string Location { get; set; } = "";
public int MaxDepth { get; set; } = -1;
internal string[] Suffixes { get; set; } = { "" };
public override List<Program> LoadPrograms() public override List<Program> LoadPrograms()
{ {
if (Directory.Exists(Location) && MaxDepth >= -1) if (Directory.Exists(Location) && MaxDepth >= -1)
{ {
var apps = new List<Program>(); var apps = new List<Program>();
GetAppFromDirectory(apps, Location, 0); GetAppFromDirectory(apps, Location, MaxDepth);
return apps; return apps;
} }
else else
@ -27,36 +25,5 @@ namespace Wox.Plugin.Program.ProgramSources
return new List<Program>(); return new List<Program>();
} }
} }
private void GetAppFromDirectory(List<Program> apps, string path, int depth)
{
if (MaxDepth != depth)
{
foreach (var file in Directory.GetFiles(path))
{
if (Suffixes.Any(o => file.EndsWith("." + o)))
{
Program p;
try
{
p = CreateEntry(file);
}
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program",
$"GetAppFromDirectory failed: {path}", e);
Log.Exception(woxPluginException);
continue;
}
p.Source = this;
apps.Add(p);
}
}
foreach (var d in Directory.GetDirectories(path))
{
GetAppFromDirectory(apps, d, depth + 1);
}
}
}
} }
} }

View file

@ -8,37 +8,6 @@ namespace Wox.Plugin.Program.ProgramSources
[Serializable] [Serializable]
public abstract class ProgramSource public abstract class ProgramSource
{ {
public abstract List<Program> LoadPrograms(); public abstract List<Program> LoadPrograms();
protected Program CreateEntry(string file)
{
var p = new Program
{
Title = Path.GetFileNameWithoutExtension(file),
IcoPath = file,
Path = file,
Directory = Directory.GetParent(file).FullName
};
switch (Path.GetExtension(file).ToLower())
{
case ".exe":
p.ExecutableName = Path.GetFileName(file);
try
{
var versionInfo = FileVersionInfo.GetVersionInfo(file);
if (!string.IsNullOrEmpty(versionInfo.FileDescription))
{
p.Title = versionInfo.FileDescription;
}
}
catch (Exception)
{
}
break;
}
return p;
}
} }
} }

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
class StartMenu : Win32
{
public override List<Program> LoadPrograms()
{
var directory1 = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
var directory2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);
var programs = new List<Program>();
GetAppFromDirectory(programs, directory1, MaxDepth);
GetAppFromDirectory(programs, directory2, MaxDepth);
return programs;
}
}
}

View file

@ -1,14 +0,0 @@
using System;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public sealed class UserStartMenuProgramSource : FileSystemProgramSource
{
public UserStartMenuProgramSource()
{
Location = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
}
}
}

View file

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program.ProgramSources
{
[Serializable]
public abstract class Win32 : ProgramSource
{
public int MaxDepth { get; set; } = -1;
public string[] Suffixes { get; set; } = { "" };
protected Program CreateEntry(string file)
{
var p = new Program
{
Title = Path.GetFileNameWithoutExtension(file),
IcoPath = file,
Path = file,
Directory = Directory.GetParent(file).FullName
};
switch (Path.GetExtension(file).ToLower())
{
case ".exe":
p.ExecutableName = Path.GetFileName(file);
try
{
var versionInfo = FileVersionInfo.GetVersionInfo(file);
if (!string.IsNullOrEmpty(versionInfo.FileDescription))
{
p.Title = versionInfo.FileDescription;
}
}
catch (Exception)
{
}
break;
}
return p;
}
protected void GetAppFromDirectory(List<Program> apps, string directory, int depth)
{
if (MaxDepth == -1 || MaxDepth < depth)
{
foreach (var f in Directory.GetFiles(directory))
{
if (Suffixes.Any(o => f.EndsWith("." + o)))
{
Program p;
try
{
p = CreateEntry(f);
}
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program",
$"GetAppFromDirectory failed: {directory}", e);
Log.Exception(woxPluginException);
continue;
}
apps.Add(p);
}
}
foreach (var d in Directory.GetDirectories(directory))
{
GetAppFromDirectory(apps, d, depth - 1);
}
}
}
}
}

View file

@ -71,7 +71,9 @@
<DependentUpon>AddProgramSource.xaml</DependentUpon> <DependentUpon>AddProgramSource.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="FileChangeWatcher.cs" /> <Compile Include="FileChangeWatcher.cs" />
<Compile Include="ProgramSources\StartMenu.cs" />
<Compile Include="ProgramSources\UWP.cs" /> <Compile Include="ProgramSources\UWP.cs" />
<Compile Include="ProgramSources\Win32.cs" />
<Compile Include="SuffixesConverter.cs" /> <Compile Include="SuffixesConverter.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="ProgramIndexCache.cs" /> <Compile Include="ProgramIndexCache.cs" />
@ -81,9 +83,7 @@
</Compile> </Compile>
<Compile Include="ProgramSources\ProgramSource.cs" /> <Compile Include="ProgramSources\ProgramSource.cs" />
<Compile Include="ProgramSources\AppPathsProgramSource.cs" /> <Compile Include="ProgramSources\AppPathsProgramSource.cs" />
<Compile Include="ProgramSources\CommonStartMenuProgramSource.cs" />
<Compile Include="ProgramSources\FileSystemProgramSource.cs" /> <Compile Include="ProgramSources\FileSystemProgramSource.cs" />
<Compile Include="ProgramSources\UserStartMenuProgramSource.cs" />
<Compile Include="Settings.cs" /> <Compile Include="Settings.cs" />
<Compile Include="ProgramSuffixes.xaml.cs"> <Compile Include="ProgramSuffixes.xaml.cs">
<DependentUpon>ProgramSuffixes.xaml</DependentUpon> <DependentUpon>ProgramSuffixes.xaml</DependentUpon>