Merge pull request #278 from dstiert/program-source-details

Improve configurability of Programs plguin
This commit is contained in:
qianlifeng 2015-05-03 14:25:46 +08:00
commit dcd20f42b9
12 changed files with 419 additions and 234 deletions

View file

@ -0,0 +1,27 @@
<Window x:Class="Wox.Plugin.Program.AddProgramSource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Width="400"
Height="180"
WindowStartupLocation="CenterScreen"
d:DesignHeight="400" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="{DynamicResource wox_plugin_program_directory}"/>
<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" />
</StackPanel>
<Button VerticalAlignment="Center" HorizontalAlignment="Right" Margin="10" Height="20" Width="70" Click="ButtonAdd_OnClick" Content="{DynamicResource wox_plugin_program_update}" />
</StackPanel>
</Window>

View file

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Wox.Plugin.Program
{
/// <summary>
/// Interaction logic for AddProgramSource.xaml
/// </summary>
public partial class AddProgramSource
{
private ProgramSource _editing;
public AddProgramSource()
{
InitializeComponent();
}
public AddProgramSource(ProgramSource edit) : this()
{
this._editing = edit;
this.Directory.Text = this._editing.Location;
this.MaxDepth.Text = this._editing.MaxDepth.ToString();
this.Suffixes.Text = this._editing.Suffixes;
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
this.Directory.Text = dialog.SelectedPath;
}
}
private void ButtonAdd_OnClick(object sender, RoutedEventArgs e)
{
int max;
if(!int.TryParse(this.MaxDepth.Text, out max))
{
max = -1;
}
if(this._editing == null)
{
ProgramStorage.Instance.ProgramSources.Add(new ProgramSource()
{
Location = this.Directory.Text,
MaxDepth = max,
Suffixes = this.Suffixes.Text,
Type = "FileSystemProgramSource",
Enabled = true
});
}
else
{
this._editing.Location = this.Directory.Text;
this._editing.MaxDepth = max;
this._editing.Suffixes = this.Suffixes.Text;
}
ProgramStorage.Instance.Save();
this.DialogResult = true;
this.Close();
}
}
}

View file

@ -10,6 +10,15 @@
<system:String x:Key="wox_plugin_program_suffixes">Index file suffixes</system:String> <system:String x:Key="wox_plugin_program_suffixes">Index file suffixes</system:String>
<system:String x:Key="wox_plugin_program_reindex">Reindex</system:String> <system:String x:Key="wox_plugin_program_reindex">Reindex</system:String>
<system:String x:Key="wox_plugin_program_indexing">Indexing</system:String> <system:String x:Key="wox_plugin_program_indexing">Indexing</system:String>
<system:String x:Key="wox_plugin_program_index_start">Index Start Menu</system:String>
<system:String x:Key="wox_plugin_program_index_registry">Index Registry</system:String>
<system:String x:Key="wox_plugin_program_suffixes_header">Suffixes</system:String>
<system:String x:Key="wox_plugin_program_max_depth_header">Max Depth</system:String>
<system:String x:Key="wox_plugin_program_directory">Directory:</system:String>
<system:String x:Key="wox_plugin_program_browse">Browse</system:String>
<system:String x:Key="wox_plugin_program_file_suffixes">File Suffixes:</system:String>
<system:String x:Key="wox_plugin_program_max_search_depth">Maximum Search Depth (-1 is unlimited):</system:String>
<system:String x:Key="wox_plugin_program_pls_select_program_source">Please select a program source</system:String> <system:String x:Key="wox_plugin_program_pls_select_program_source">Please select a program source</system:String>
<system:String x:Key="wox_plugin_program_delete_program_source">Are your sure to delete {0}?</system:String> <system:String x:Key="wox_plugin_program_delete_program_source">Are your sure to delete {0}?</system:String>

View file

@ -10,7 +10,15 @@
<system:String x:Key="wox_plugin_program_suffixes">索引文件后缀</system:String> <system:String x:Key="wox_plugin_program_suffixes">索引文件后缀</system:String>
<system:String x:Key="wox_plugin_program_reindex">重新索引</system:String> <system:String x:Key="wox_plugin_program_reindex">重新索引</system:String>
<system:String x:Key="wox_plugin_program_indexing">索引中</system:String> <system:String x:Key="wox_plugin_program_indexing">索引中</system:String>
<system:String x:Key="wox_plugin_program_index_start">指数开始菜单</system:String>
<system:String x:Key="wox_plugin_program_index_registry">指数注册</system:String>
<system:String x:Key="wox_plugin_program_suffixes_header">后缀</system:String>
<system:String x:Key="wox_plugin_program_max_depth_header">最大深度</system:String>
<system:String x:Key="wox_plugin_program_directory">目录</system:String>
<system:String x:Key="wox_plugin_program_browse">浏览</system:String>
<system:String x:Key="wox_plugin_program_file_suffixes">文件后缀</system:String>
<system:String x:Key="wox_plugin_program_max_search_depth">最大搜索深度(-1是无限的</system:String>
<system:String x:Key="wox_plugin_program_pls_select_program_source">请先选择一项</system:String> <system:String x:Key="wox_plugin_program_pls_select_program_source">请先选择一项</system:String>
<system:String x:Key="wox_plugin_program_delete_program_source">你确定要删除{0}吗?</system:String> <system:String x:Key="wox_plugin_program_delete_program_source">你确定要删除{0}吗?</system:String>

View file

@ -10,7 +10,15 @@
<system:String x:Key="wox_plugin_program_suffixes">索引文件後綴</system:String> <system:String x:Key="wox_plugin_program_suffixes">索引文件後綴</system:String>
<system:String x:Key="wox_plugin_program_reindex">重新索引</system:String> <system:String x:Key="wox_plugin_program_reindex">重新索引</system:String>
<system:String x:Key="wox_plugin_program_indexing">索引中</system:String> <system:String x:Key="wox_plugin_program_indexing">索引中</system:String>
<system:String x:Key="wox_plugin_program_index_start">指数开始菜单</system:String>
<system:String x:Key="wox_plugin_program_index_registry">指数注册</system:String>
<system:String x:Key="wox_plugin_program_suffixes_header">后缀</system:String>
<system:String x:Key="wox_plugin_program_max_depth_header">最大深度</system:String>
<system:String x:Key="wox_plugin_program_directory">目录</system:String>
<system:String x:Key="wox_plugin_program_browse">浏览</system:String>
<system:String x:Key="wox_plugin_program_file_suffixes">文件后缀</system:String>
<system:String x:Key="wox_plugin_program_max_search_depth">最大搜索深度(-1是无限的</system:String>
<system:String x:Key="wox_plugin_program_pls_select_program_source">請先選擇一項</system:String> <system:String x:Key="wox_plugin_program_pls_select_program_source">請先選擇一項</system:String>
<system:String x:Key="wox_plugin_program_delete_program_source">你確定要刪除{0}嗎?</system:String> <system:String x:Key="wox_plugin_program_delete_program_source">你確定要刪除{0}嗎?</system:String>

View file

@ -12,7 +12,11 @@
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
<RowDefinition Height="50"/> <RowDefinition Height="50"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right"> <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Stretch">
<StackPanel Orientation="Vertical" Width="310">
<CheckBox Name="StartMenuEnabled" Click="StartMenuEnabled_Click" Margin="5" Content="{DynamicResource wox_plugin_program_index_start}"></CheckBox>
<CheckBox Name="RegistryEnabled" Click="RegistryEnabled_Click" Margin="5" Content="{DynamicResource wox_plugin_program_index_registry}"></CheckBox>
</StackPanel>
<Button Height="30" HorizontalAlignment="Right" x:Name="btnProgramSuffixes" Width="130" Click="BtnProgramSuffixes_OnClick" Content="{DynamicResource wox_plugin_program_suffixes}"></Button> <Button Height="30" HorizontalAlignment="Right" x:Name="btnProgramSuffixes" Width="130" Click="BtnProgramSuffixes_OnClick" Content="{DynamicResource wox_plugin_program_suffixes}"></Button>
<Button Height="30" HorizontalAlignment="Right" Margin="10 0 0 0" x:Name="btnReindex" Width="100" Click="btnReindex_Click" Content="{DynamicResource wox_plugin_program_reindex}"></Button> <Button Height="30" HorizontalAlignment="Right" Margin="10 0 0 0" x:Name="btnReindex" Width="100" Click="btnReindex_Click" Content="{DynamicResource wox_plugin_program_reindex}"></Button>
</StackPanel> </StackPanel>
@ -21,13 +25,27 @@
Drop="programSourceView_Drop" > Drop="programSourceView_Drop" >
<ListView.View> <ListView.View>
<GridView> <GridView>
<GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="400"> <GridViewColumn Header="{DynamicResource wox_plugin_program_location}" Width="250">
<GridViewColumn.CellTemplate> <GridViewColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding Location, ConverterParameter=(null), Converter={program:StringEmptyConverter}}"/> <TextBlock Text="{Binding Location, ConverterParameter=(null), Converter={program:StringEmptyConverter}}"/>
</DataTemplate> </DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn.CellTemplate>
</GridViewColumn> </GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_program_suffixes_header}" Width="220">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Suffixes, ConverterParameter=(null), Converter={program:StringEmptyConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_program_max_depth_header}" Width="75">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MaxDepth, ConverterParameter=(null)}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView> </GridView>
</ListView.View> </ListView.View>
</ListView> </ListView>

View file

@ -22,6 +22,8 @@ namespace Wox.Plugin.Program
private void Setting_Loaded(object sender, RoutedEventArgs e) private void Setting_Loaded(object sender, RoutedEventArgs e)
{ {
programSourceView.ItemsSource = ProgramStorage.Instance.ProgramSources; programSourceView.ItemsSource = ProgramStorage.Instance.ProgramSources;
StartMenuEnabled.IsChecked = ProgramStorage.Instance.EnableStartMenuSource;
RegistryEnabled.IsChecked = ProgramStorage.Instance.EnableRegistrySource;
} }
private void ReIndexing() private void ReIndexing()
@ -37,19 +39,10 @@ namespace Wox.Plugin.Program
private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e) private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e)
{ {
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); var add = new AddProgramSource();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) if(add.ShowDialog() ?? false)
{ {
string path = folderBrowserDialog.SelectedPath; this.ReIndexing();
ProgramStorage.Instance.ProgramSources.Add(new ProgramSource()
{
Location = path,
Type = "FileSystemProgramSource",
Enabled = true
});
ProgramStorage.Instance.Save();
ReIndexing();
} }
} }
@ -79,14 +72,10 @@ namespace Wox.Plugin.Program
ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource; ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null) if (selectedProgramSource != null)
{ {
//todo: update var add = new AddProgramSource(selectedProgramSource);
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); if (add.ShowDialog() ?? false)
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{ {
string path = folderBrowserDialog.SelectedPath; this.ReIndexing();
selectedProgramSource.Location = path;
ProgramStorage.Instance.Save();
ReIndexing();
} }
} }
else else
@ -142,5 +131,19 @@ namespace Wox.Plugin.Program
} }
} }
} }
private void StartMenuEnabled_Click(object sender, RoutedEventArgs e)
{
ProgramStorage.Instance.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false;
ProgramStorage.Instance.Save();
ReIndexing();
}
private void RegistryEnabled_Click(object sender, RoutedEventArgs e)
{
ProgramStorage.Instance.EnableRegistrySource = RegistryEnabled.IsChecked ?? false;
ProgramStorage.Instance.Save();
ReIndexing();
}
} }
} }

View file

@ -10,6 +10,8 @@ namespace Wox.Plugin.Program
public string Type { get; set; } public string Type { get; set; }
public int BonusPoints { get; set; } public int BonusPoints { get; set; }
public bool Enabled { get; set; } public bool Enabled { get; set; }
public string Suffixes { get; set; }
public int MaxDepth { get; set; }
public Dictionary<string, string> Meta { get; set; } public Dictionary<string, string> Meta { get; set; }
public override string ToString() public override string ToString()

View file

@ -10,14 +10,21 @@ namespace Wox.Plugin.Program.ProgramSources
public class FileSystemProgramSource : AbstractProgramSource public class FileSystemProgramSource : AbstractProgramSource
{ {
private string baseDirectory; private string baseDirectory;
private int maxDepth;
private string suffixes;
public FileSystemProgramSource(string baseDirectory) public FileSystemProgramSource(string baseDirectory, int maxDepth, string suffixes)
{ {
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.maxDepth = maxDepth;
this.suffixes = suffixes;
} }
public FileSystemProgramSource(string baseDirectory)
: this(baseDirectory, -1, "") {}
public FileSystemProgramSource(ProgramSource source) public FileSystemProgramSource(ProgramSource source)
: this(source.Location) : this(source.Location, source.MaxDepth, source.Suffixes)
{ {
this.BonusPoints = source.BonusPoints; this.BonusPoints = source.BonusPoints;
} }
@ -35,11 +42,21 @@ namespace Wox.Plugin.Program.ProgramSources
private void GetAppFromDirectory(string path, List<Program> list) private void GetAppFromDirectory(string path, List<Program> list)
{ {
GetAppFromDirectory(path, list, 0);
}
private void GetAppFromDirectory(string path, List<Program> list, int depth)
{
if(maxDepth != -1 && depth > maxDepth)
{
return;
}
try try
{ {
foreach (string file in Directory.GetFiles(path)) foreach (string file in Directory.GetFiles(path))
{ {
if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o))) if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)) ||
suffixes.Split(';').Any(o => file.EndsWith("." + o)))
{ {
Program p = CreateEntry(file); Program p = CreateEntry(file);
list.Add(p); list.Add(p);
@ -48,7 +65,7 @@ namespace Wox.Plugin.Program.ProgramSources
foreach (var subDirectory in Directory.GetDirectories(path)) foreach (var subDirectory in Directory.GetDirectories(path))
{ {
GetAppFromDirectory(subDirectory, list); GetAppFromDirectory(subDirectory, list, depth + 1);
} }
} }
catch (Exception e) catch (Exception e)

View file

@ -6,6 +6,7 @@ using System.Reflection;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using System.ComponentModel;
namespace Wox.Plugin.Program namespace Wox.Plugin.Program
{ {
@ -17,6 +18,14 @@ namespace Wox.Plugin.Program
[JsonProperty] [JsonProperty]
public string ProgramSuffixes { get; set; } public string ProgramSuffixes { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[DefaultValue(true)]
public bool EnableStartMenuSource { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[DefaultValue(true)]
public bool EnableRegistrySource { get; set; }
protected override string ConfigFolder protected override string ConfigFolder
{ {
get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); }
@ -25,6 +34,8 @@ namespace Wox.Plugin.Program
protected override ProgramStorage LoadDefault() protected override ProgramStorage LoadDefault()
{ {
ProgramSources = new List<ProgramSource>(); ProgramSources = new List<ProgramSource>();
EnableStartMenuSource = true;
EnableRegistrySource = true;
return this; return this;
} }

View file

@ -28,6 +28,7 @@ namespace Wox.Plugin.Program
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
var fuzzyMather = FuzzyMatcher.Create(query.Search); var fuzzyMather = FuzzyMatcher.Create(query.Search);
List<Program> returnList = programs.Where(o => MatchProgram(o, fuzzyMather)).ToList(); List<Program> returnList = programs.Where(o => MatchProgram(o, fuzzyMather)).ToList();
returnList.ForEach(ScoreFilter); returnList.ForEach(ScoreFilter);
@ -97,11 +98,11 @@ namespace Wox.Plugin.Program
if (ProgramStorage.Instance.ProgramSources != null && if (ProgramStorage.Instance.ProgramSources != null &&
ProgramStorage.Instance.ProgramSources.Count(o => o.Enabled) > 0) ProgramStorage.Instance.ProgramSources.Count(o => o.Enabled) > 0)
{ {
programSources.AddRange(ProgramStorage.Instance.ProgramSources.Where(o => o.Enabled)); programSources.AddRange(ProgramStorage.Instance.ProgramSources);
} }
sources.Clear(); sources.Clear();
programSources.ForEach(source => foreach(var source in programSources.Where(o => o.Enabled))
{ {
Type sourceClass; Type sourceClass;
if (SourceTypes.TryGetValue(source.Type, out sourceClass)) if (SourceTypes.TryGetValue(source.Type, out sourceClass))
@ -114,7 +115,7 @@ namespace Wox.Plugin.Program
sources.Add(programSource); sources.Add(programSource);
} }
} }
}); }
var tempPrograms = new List<Program>(); var tempPrograms = new List<Program>();
foreach (var source in sources) foreach (var source in sources)
@ -145,19 +146,19 @@ namespace Wox.Plugin.Program
list.Add(new ProgramSource() list.Add(new ProgramSource()
{ {
BonusPoints = 0, BonusPoints = 0,
Enabled = true, Enabled = ProgramStorage.Instance.EnableStartMenuSource,
Type = "CommonStartMenuProgramSource" Type = "CommonStartMenuProgramSource"
}); });
list.Add(new ProgramSource() list.Add(new ProgramSource()
{ {
BonusPoints = 0, BonusPoints = 0,
Enabled = true, Enabled = ProgramStorage.Instance.EnableStartMenuSource,
Type = "UserStartMenuProgramSource" Type = "UserStartMenuProgramSource"
}); });
list.Add(new ProgramSource() list.Add(new ProgramSource()
{ {
BonusPoints = -10, BonusPoints = -10,
Enabled = true, Enabled = ProgramStorage.Instance.EnableRegistrySource,
Type = "AppPathsProgramSource" Type = "AppPathsProgramSource"
}); });
return list; return list;

View file

@ -44,7 +44,6 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
@ -52,6 +51,10 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Include="AddProgramSource.xaml" />
<Compile Include="AddProgramSource.xaml.cs">
<DependentUpon>AddProgramSource.xaml</DependentUpon>
</Compile>
<Compile Include="FileChangeWatcher.cs" /> <Compile Include="FileChangeWatcher.cs" />
<Compile Include="IProgramSource.cs" /> <Compile Include="IProgramSource.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />