Remove suffixes from model

This commit is contained in:
bao-qian
2016-08-19 20:34:20 +01:00
parent fb68f6eb60
commit 8764aa9829
9 changed files with 60 additions and 57 deletions

View File

@@ -14,10 +14,6 @@
<TextBox Name="Directory" VerticalAlignment="Center" Width="238" Margin="0,7" />
<Button Name="BrowseButton" Content="{DynamicResource wox_plugin_program_browse}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="BrowseButton_Click" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="{DynamicResource wox_plugin_program_file_suffixes}" />
<TextBox x:Name="Suffixes" VerticalAlignment="Center" Width="297" Margin="0,7" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="{DynamicResource wox_plugin_program_max_search_depth}" />
<TextBox Name="MaxDepth" Text="-1" VerticalAlignment="Center" Width="146" Margin="0,7" />

View File

@@ -17,7 +17,6 @@ namespace Wox.Plugin.Program
{
InitializeComponent();
_settings = settings;
Suffixes.Text = string.Join(";", settings.ProgramSuffixes);
Directory.Focus();
}
@@ -29,7 +28,6 @@ namespace Wox.Plugin.Program
InitializeComponent();
Directory.Text = _editing.Location;
MaxDepth.Text = _editing.MaxDepth.ToString();
Suffixes.Text = string.Join(";", _editing.Suffixes);
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
@@ -56,7 +54,6 @@ namespace Wox.Plugin.Program
{
Location = Directory.Text,
MaxDepth = max,
Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator),
};
_settings.ProgramSources.Add(source);
}
@@ -64,7 +61,6 @@ namespace Wox.Plugin.Program
{
_editing.Location = Directory.Text;
_editing.MaxDepth = max;
_editing.Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator);
}
DialogResult = true;

View File

@@ -2,12 +2,24 @@
using System.IO;
using System.Threading.Tasks;
using Wox.Infrastructure.Logger;
using Wox.Plugin.Program.ProgramSources;
namespace Wox.Plugin.Program
{
internal static class FileChangeWatcher
{
private static readonly List<string> WatchedPath = new List<string>();
// todo remove previous watcher events
public static void AddAll(List<FileSystemProgramSource> sources, string[] suffixes)
{
foreach (var s in sources)
{
if (Directory.Exists(s.Location))
{
AddWatch(s.Location, suffixes);
}
}
}
public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true)
{

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using Wox.Infrastructure;
@@ -130,6 +131,7 @@ namespace Wox.Plugin.Program
{
var sources = ProgramSources();
var programs = sources.AsParallel()
.SelectMany(s => s.LoadPrograms())
// filter duplicate program
@@ -150,20 +152,31 @@ namespace Wox.Plugin.Program
private static List<ProgramSource> ProgramSources()
{
var sources = new List<ProgramSource>();
var fileSystemSource = new List<FileSystemProgramSource>();
if (_settings.EnableStartMenuSource)
{
sources.Add(new CommonStartMenuProgramSource());
sources.Add(new UserStartMenuProgramSource());
fileSystemSource.Add(new CommonStartMenuProgramSource());
fileSystemSource.Add(new UserStartMenuProgramSource());
}
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)
{
sources.Add(new AppPathsProgramSource());
}
sources.AddRange(_settings.ProgramSources);
return sources;
}
private static Program ScoreFilter(Program p)
{
var start = new[] { "启动", "start" };

View File

@@ -32,13 +32,6 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_program_suffixes_header}" Width="220">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Suffixes, ConverterParameter=(null), Converter={program:SuffixesConvert}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="75">
<GridViewColumn.CellTemplate>
<DataTemplate>

View File

@@ -11,56 +11,52 @@ namespace Wox.Plugin.Program.ProgramSources
public class FileSystemProgramSource : ProgramSource
{
public string Location { get; set; } = "";
public int MaxDepth { get; set; } = -1;
internal string[] Suffixes { get; set; } = { "" };
public override List<Program> LoadPrograms()
{
var list = new List<Program>();
if (Directory.Exists(Location))
if (Directory.Exists(Location) && MaxDepth >= -1)
{
GetAppFromDirectory(Location, list);
FileChangeWatcher.AddWatch(Location, Suffixes);
var apps = new List<Program>();
GetAppFromDirectory(apps, Location, 0);
return apps;
}
else
{
return new List<Program>();
}
return list;
}
private void GetAppFromDirectory(string path, List<Program> list)
private void GetAppFromDirectory(List<Program> apps, string path, int depth)
{
GetAppFromDirectory(path, list, 0);
}
private void GetAppFromDirectory(string path, List<Program> list, int depth)
{
if (MaxDepth != -1 && depth > MaxDepth)
{
return;
}
try
if (MaxDepth != depth)
{
foreach (var file in Directory.GetFiles(path))
{
if (Suffixes.Any(o => file.EndsWith("." + o)))
{
var p = CreateEntry(file);
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;
list.Add(p);
apps.Add(p);
}
}
foreach (var subDirectory in Directory.GetDirectories(path))
foreach (var d in Directory.GetDirectories(path))
{
GetAppFromDirectory(subDirectory, list, depth + 1);
GetAppFromDirectory(apps, d, depth + 1);
}
}
catch (Exception e)
{
var woxPluginException = new WoxPluginException("Program", $"GetAppFromDirectory failed: {path}", e);
Log.Exception(woxPluginException);
}
}
public override string ToString()
{
var display = GetType().Name + Location;
return display;
}
}
}

View File

@@ -8,11 +8,6 @@ namespace Wox.Plugin.Program.ProgramSources
[Serializable]
public abstract class ProgramSource
{
public const char SuffixSeperator = ';';
// happlebao todo: temp hack for program suffixes
public string[] Suffixes { get; set; } = {"bat", "appref-ms", "exe", "lnk"};
public int MaxDepth { get; set; } = -1;
public abstract List<Program> LoadPrograms();

View File

@@ -17,7 +17,7 @@ namespace Wox.Plugin.Program
this.context = context;
InitializeComponent();
_settings = settings;
tbSuffixes.Text = string.Join(ProgramSource.SuffixSeperator.ToString(), _settings.ProgramSuffixes);
tbSuffixes.Text = string.Join(Settings.SuffixSeperator.ToString(), _settings.ProgramSuffixes);
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
@@ -29,7 +29,7 @@ namespace Wox.Plugin.Program
return;
}
_settings.ProgramSuffixes = tbSuffixes.Text.Split(ProgramSource.SuffixSeperator);
_settings.ProgramSuffixes = tbSuffixes.Text.Split(Settings.SuffixSeperator);
string msg = context.API.GetTranslation("wox_plugin_program_update_file_suffixes");
MessageBox.Show(msg);
}

View File

@@ -12,5 +12,7 @@ namespace Wox.Plugin.Program
public bool EnableStartMenuSource { get; set; } = true;
public bool EnableRegistrySource { get; set; } = true;
internal const char SuffixSeperator = ';';
}
}