Refactoring Programs system plugin and adding re-index function when adding new location.

This commit is contained in:
qianlifeng
2014-08-10 22:22:54 +08:00
parent 961bfcc481
commit 20ffcfb91e
10 changed files with 171 additions and 431 deletions

View File

@@ -135,30 +135,6 @@ namespace Wox.Infrastructure.Storage.UserSettings
return webSearches; return webSearches;
} }
public List<ProgramSource> LoadDefaultProgramSources()
{
var list = new List<ProgramSource>();
list.Add(new ProgramSource()
{
BonusPoints = 0,
Enabled = true,
Type = "CommonStartMenuProgramSource"
});
list.Add(new ProgramSource()
{
BonusPoints = 0,
Enabled = true,
Type = "UserStartMenuProgramSource"
});
list.Add(new ProgramSource()
{
BonusPoints = -10,
Enabled = true,
Type = "AppPathsProgramSource"
});
return list;
}
protected override string ConfigName protected override string ConfigName
{ {
get { return "config"; } get { return "config"; }
@@ -169,7 +145,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
Theme = "Dark"; Theme = "Dark";
ReplaceWinR = true; ReplaceWinR = true;
WebSearches = LoadDefaultWebSearches(); WebSearches = LoadDefaultWebSearches();
ProgramSources = LoadDefaultProgramSources(); ProgramSources = new List<ProgramSource>();
CustomizedPluginConfigs = new List<CustomizedPluginConfig>(); CustomizedPluginConfigs = new List<CustomizedPluginConfig>();
Hotkey = "Alt + Space"; Hotkey = "Alt + Space";
QueryBoxFont = FontFamily.GenericSansSerif.Name; QueryBoxFont = FontFamily.GenericSansSerif.Name;

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
namespace Wox.Plugin.SystemPlugins.Program namespace Wox.Plugin.SystemPlugins.Program
{ {
@@ -19,23 +20,26 @@ namespace Wox.Plugin.SystemPlugins.Program
get; set; get; set;
} }
protected SystemPlugins.Program.Program CreateEntry(string file) protected Program CreateEntry(string file)
{ {
SystemPlugins.Program.Program p = new SystemPlugins.Program.Program() var p = new Program()
{ {
Title = global::System.IO.Path.GetFileNameWithoutExtension(file), Title = Path.GetFileNameWithoutExtension(file),
IcoPath = file, IcoPath = file,
ExecutePath = file ExecutePath = file
}; };
switch (global::System.IO.Path.GetExtension(file).ToLower()) switch (Path.GetExtension(file).ToLower())
{ {
case ".exe": case ".exe":
p.ExecuteName = global::System.IO.Path.GetFileName(file); p.ExecuteName = global::System.IO.Path.GetFileName(file);
try try
{ {
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(file); FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(file);
if (versionInfo.FileDescription != null && versionInfo.FileDescription != string.Empty) p.Title = versionInfo.FileDescription; if (!string.IsNullOrEmpty(versionInfo.FileDescription))
{
p.Title = versionInfo.FileDescription;
}
} }
catch (Exception) { } catch (Exception) { }
break; break;

View File

@@ -0,0 +1,32 @@
using Wox.Infrastructure;
namespace Wox.Plugin.SystemPlugins.Program
{
public class Program
{
private static readonly global::System.Text.RegularExpressions.Regex AbbrRegexp = new global::System.Text.RegularExpressions.Regex("[^A-Z0-9]", global::System.Text.RegularExpressions.RegexOptions.Compiled);
private string m_Title;
public string Title
{
get
{
return m_Title;
}
set
{
m_Title = value;
string pinyin = m_Title.Unidecode();
PinyinTitle = pinyin;
AbbrTitle = AbbrRegexp.Replace(global::System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(pinyin), "");
if (AbbrTitle.Length < 2) AbbrTitle = null;
}
}
public string PinyinTitle { get; private set; }
public string AbbrTitle { get; private set; }
public string IcoPath { get; set; }
public string ExecutePath { get; set; }
public string ExecuteName { get; set; }
public int Score { get; set; }
public IProgramSource Source { get; set; }
}
}

View File

@@ -5,8 +5,11 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:infrastructure="clr-namespace:Wox.Infrastructure;assembly=Wox.Infrastructure" xmlns:infrastructure="clr-namespace:Wox.Infrastructure;assembly=Wox.Infrastructure"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"> d:DesignHeight="300" d:DesignWidth="600">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</UserControl.Resources>
<Grid Margin="10"> <Grid Margin="10">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
@@ -22,35 +25,19 @@
</DataTemplate> </DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn.CellTemplate>
</GridViewColumn> </GridViewColumn>
<GridViewColumn Header="Type" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Bonus Points" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BonusPoints}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Enabled" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Enabled}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView> </GridView>
</ListView.View> </ListView.View>
</ListView> </ListView>
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal"> <DockPanel Grid.Row="1">
<Button x:Name="btnDeleteProgramSource" Click="btnDeleteProgramSource_OnClick" Width="100" Margin="10" Content="Delete"/> <StackPanel x:Name="indexingPanel" Visibility="Hidden" HorizontalAlignment="Left" Orientation="Horizontal">
<Button x:Name="btnEditProgramSource" Click="btnEditProgramSource_OnClick" Width="100" Margin="10" Content="Edit"/> <ProgressBar x:Name="progressBarIndexing" Height="20" Width="80" Minimum="0" Maximum="100" IsIndeterminate="True"></ProgressBar>
<Button x:Name="btnAddProgramSource" Click="btnAddProgramSource_OnClick" Width="100" Margin="10" Content="Add"/> <TextBlock Margin="10 0 0 0" Height="20" HorizontalAlignment="Center">Indexing</TextBlock>
</StackPanel> </StackPanel>
<TextBlock Grid.Row="1" Margin="10" HorizontalAlignment="Left" Text="* Restart required" VerticalAlignment="Center" /> <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<Button x:Name="btnDeleteProgramSource" Click="btnDeleteProgramSource_OnClick" Width="100" Margin="10" Content="Delete"/>
<Button x:Name="btnEditProgramSource" Click="btnEditProgramSource_OnClick" Width="100" Margin="10" Content="Edit"/>
<Button x:Name="btnAddProgramSource" Click="btnAddProgramSource_OnClick" Width="100" Margin="10" Content="Add"/>
</StackPanel>
</DockPanel>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -1,4 +1,6 @@
using System.Windows; using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
@@ -20,16 +22,33 @@ namespace Wox.Plugin.SystemPlugins.Program
programSourceView.ItemsSource = UserSettingStorage.Instance.ProgramSources; programSourceView.ItemsSource = UserSettingStorage.Instance.ProgramSources;
} }
public void ReloadProgramSourceView() private void ReIndexing()
{ {
programSourceView.Items.Refresh(); programSourceView.Items.Refresh();
ThreadPool.QueueUserWorkItem(t =>
{
Dispatcher.Invoke(new Action(() => { indexingPanel.Visibility = Visibility.Visible; }));
Programs.LoadPrograms();
Dispatcher.Invoke(new Action(() => { indexingPanel.Visibility = Visibility.Hidden; }));
});
} }
private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e) private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e)
{ {
ProgramSourceSetting programSource = new ProgramSourceSetting(this); var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
programSource.ShowDialog(); if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = folderBrowserDialog.SelectedPath;
UserSettingStorage.Instance.ProgramSources.Add(new ProgramSource()
{
Location = path,
Type = "FileSystemProgramSource",
Enabled = true
});
UserSettingStorage.Instance.Save();
ReIndexing();
}
} }
private void btnDeleteProgramSource_OnClick(object sender, RoutedEventArgs e) private void btnDeleteProgramSource_OnClick(object sender, RoutedEventArgs e)
@@ -37,11 +56,12 @@ namespace Wox.Plugin.SystemPlugins.Program
ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource; ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null) if (selectedProgramSource != null)
{ {
if (MessageBox.Show("Are your sure to delete " + selectedProgramSource.ToString(), "Delete ProgramSource", if (MessageBox.Show("Are your sure to delete " + selectedProgramSource.Location, "Delete ProgramSource",
MessageBoxButton.YesNo) == MessageBoxResult.Yes) MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{ {
UserSettingStorage.Instance.ProgramSources.Remove(selectedProgramSource); UserSettingStorage.Instance.ProgramSources.Remove(selectedProgramSource);
programSourceView.Items.Refresh(); UserSettingStorage.Instance.Save();
ReIndexing();
} }
} }
else else
@@ -55,15 +75,20 @@ namespace Wox.Plugin.SystemPlugins.Program
ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource; ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource;
if (selectedProgramSource != null) if (selectedProgramSource != null)
{ {
ProgramSourceSetting programSource = new ProgramSourceSetting(this); //todo: update
programSource.UpdateItem(selectedProgramSource); var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
programSource.ShowDialog(); if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = folderBrowserDialog.SelectedPath;
selectedProgramSource.Location = path;
UserSettingStorage.Instance.Save();
ReIndexing();
}
} }
else else
{ {
MessageBox.Show("Please select a program source"); MessageBox.Show("Please select a program source");
} }
} }
} }
} }

View File

@@ -1,39 +0,0 @@
<Window x:Class="Wox.Plugin.SystemPlugins.Program.ProgramSourceSetting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Title="ProgramSourceSetting" Height="350" Width="674.766">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Type:</TextBlock>
<ComboBox x:Name="cbType" Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" ItemsSource="{Binding}" SelectionChanged="cbType_SelectionChanged"></ComboBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Location:</TextBlock>
<TextBox x:Name="tbLocation" Margin="10" Grid.Row="1" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">BonusPoints:</TextBlock>
<TextBox x:Name="tbBonusPoints" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">Enable:</TextBlock>
<CheckBox x:Name="cbEnable" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1">
<Button x:Name="btnCancel" Click="BtnCancel_OnClick" Margin="10 0 10 0" Width="80" Height="25">Cancel</Button>
<Button x:Name="btnAdd" Margin="10 0 10 0" Width="80" Height="25" Click="btnAdd_OnClick">
<TextBlock x:Name="lblAdd">Add</TextBlock>
</Button>
</StackPanel>
</Grid>
</Window>

View File

@@ -1,119 +0,0 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wox.Infrastructure.Storage.UserSettings;
using MessageBox = System.Windows.MessageBox;
namespace Wox.Plugin.SystemPlugins.Program
{
public partial class ProgramSourceSetting : Window
{
private ProgramSetting settingWindow;
private bool update;
private ProgramSource updateProgramSource;
public ProgramSourceSetting(ProgramSetting settingWidow)
{
this.settingWindow = settingWidow;
InitializeComponent();
this.cbType.ItemsSource = Programs.SourceTypes.Select(o => o.Key).ToList();
}
public void UpdateItem(ProgramSource programSource)
{
updateProgramSource = UserSettingStorage.Instance.ProgramSources.FirstOrDefault(o => o == programSource);
if (updateProgramSource == null)
{
MessageBox.Show("Invalid program source");
Close();
return;
}
update = true;
lblAdd.Text = "Update";
cbEnable.IsChecked = programSource.Enabled;
cbType.SelectedItem = programSource.Type;
cbType.IsEnabled = false;
tbLocation.Text = programSource.Location;
tbBonusPoints.Text = programSource.BonusPoints.ToString();
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
{
string location = tbLocation.Text;
if (this.tbLocation.IsEnabled == true && string.IsNullOrEmpty(location))
{
MessageBox.Show("Please input Type field");
return;
}
string type = cbType.SelectedItem as string;
if (string.IsNullOrEmpty(type))
{
MessageBox.Show("Please input Type field");
return;
}
int bonusPoint = 0;
int.TryParse(this.tbBonusPoints.Text, out bonusPoint);
if (!update)
{
ProgramSource p = new ProgramSource()
{
Location = this.tbLocation.IsEnabled ? location : null,
Enabled = cbEnable.IsChecked ?? false,
Type = type,
BonusPoints = bonusPoint
};
if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == p.ToString() && o != p))
{
MessageBox.Show("Program source already exists!");
return;
}
UserSettingStorage.Instance.ProgramSources.Add(p);
MessageBox.Show(string.Format("Add {0} program source successfully!", p.ToString()));
}
else
{
if (UserSettingStorage.Instance.ProgramSources.Exists(o => o.ToString() == updateProgramSource.ToString() && o != updateProgramSource))
{
MessageBox.Show("Program source already exists!");
return;
}
updateProgramSource.Location = this.tbLocation.IsEnabled ? location : null;
updateProgramSource.Type = type;
updateProgramSource.Enabled = cbEnable.IsChecked ?? false;
updateProgramSource.BonusPoints = bonusPoint;
MessageBox.Show(string.Format("Update {0} program source successfully!", updateProgramSource.ToString()));
}
UserSettingStorage.Instance.Save();
settingWindow.ReloadProgramSourceView();
Close();
}
private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string item = cbType.SelectedItem as String;
Type type;
if (item != null && Programs.SourceTypes.TryGetValue(item, out type))
{
var attrs = type.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (attrs.Length > 0 && (attrs[0] as BrowsableAttribute).Browsable == false)
{
this.tbLocation.IsEnabled = false;
return;
}
}
this.tbLocation.IsEnabled = true;
}
}
}

View File

@@ -1,131 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Plugin.SystemPlugins.Program.ProgramSources
{
public class PortableAppsProgramSource : AbstractProgramSource
{
public string BaseDirectory;
public PortableAppsProgramSource(string baseDirectory)
{
BaseDirectory = baseDirectory;
}
public PortableAppsProgramSource(ProgramSource source)
: this(source.Location)
{
this.BonusPoints = source.BonusPoints;
}
public override List<Program> LoadPrograms()
{
List<Program> list = new List<Program>();
var ini = new IniParser.Parser.IniDataParser();
ini.Configuration.AllowDuplicateKeys = true;
string menuSettingsPath = Path.Combine(BaseDirectory, @"PortableApps.com\Data\PortableAppsMenu.ini");
IniParser.Model.KeyDataCollection appsRenamed = null, appsRecategorized = null, appsHidden = null;
if (File.Exists(menuSettingsPath))
{
var menuSettings = ini.Parse(File.ReadAllText(menuSettingsPath, Encoding.Default));
appsRenamed = menuSettings["AppsRenamed"];
appsRecategorized = menuSettings["AppsRecategorized"];
appsHidden = menuSettings["AppsHidden"];
}
if (appsRenamed == null) appsRenamed = new IniParser.Model.KeyDataCollection();
if (appsRecategorized == null) appsRecategorized = new IniParser.Model.KeyDataCollection();
if (appsHidden == null) appsHidden = new IniParser.Model.KeyDataCollection();
foreach (var appDir in Directory.GetDirectories(BaseDirectory))
{
var appDirName = Path.GetDirectoryName(appDir);
var appInfoPath = Path.Combine(appDir, @"App\AppInfo\appinfo.ini");
var appInfoValid = false;
if (File.Exists(appInfoPath))
{
var appInfo = ini.Parse(File.ReadAllText(appInfoPath, Encoding.Default));
var appName = appInfo["Details"]["Name"] ?? appDirName;
var control = appInfo["Control"];
int count;
if (Int32.TryParse(control["Icons"], out count))
{
appInfoValid = true;
for (int i = 1; i <= count; i++)
{
string cmdline, name, icon;
cmdline = control[String.Format("Start{0}", i)];
name = control[String.Format("Name{0}", i)];
icon = control[String.Format("ExtractIcon{0}", i)];
if (i == 1)
{
if (cmdline == null) cmdline = control["Start"];
if (cmdline == null) continue;
if (name == null) name = appName;
if (icon == null) icon = control["ExtractIcon"];
if (icon == null && !File.Exists(icon = Path.Combine(appDir, @"App\AppInfo\appicon.ico"))) icon = null;
}
if (cmdline == null) continue;
if (name == null) name = String.Format("{0} #{1}", appName, i);
if (icon == null) icon = Path.Combine(appDir, String.Format(@"App\AppInfo\appicon{0}.ico", i));
cmdline = Path.Combine(appDir, cmdline);
var menuKey = (appDirName + @"\" + cmdline).ToLower();
var renamed = appsRenamed[menuKey];
if (renamed != null)
name = renamed;
var hidden = appsHidden[menuKey] == "true";
if (!hidden)
{
Program p = new Program()
{
Title = name,
IcoPath = icon,
ExecutePath = cmdline
};
list.Add(p);
}
}
}
}
if (!appInfoValid)
{
foreach (var item in Directory.GetFiles(appDir, "*.exe", SearchOption.TopDirectoryOnly))
{
var menuKey = Path.GetFullPath(item).Substring(Path.GetFullPath(BaseDirectory).Length + 1).ToLower();
if (appsHidden[menuKey] != "true")
{
var p = CreateEntry(item);
var renamed = appsRenamed[menuKey];
if (renamed != null)
p.Title = renamed;
list.Add(p);
}
}
}
}
return list;
}
public override string ToString()
{
return typeof(PortableAppsProgramSource).Name + ":" + this.BaseDirectory;
}
}
}

View File

@@ -1,51 +1,24 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage.UserSettings; using Wox.Infrastructure.Storage.UserSettings;
using Wox.Plugin.SystemPlugins.Program.ProgramSources; using Wox.Plugin.SystemPlugins.Program.ProgramSources;
namespace Wox.Plugin.SystemPlugins.Program namespace Wox.Plugin.SystemPlugins.Program
{ {
public class Program
{
private static readonly global::System.Text.RegularExpressions.Regex AbbrRegexp = new global::System.Text.RegularExpressions.Regex("[^A-Z0-9]", global::System.Text.RegularExpressions.RegexOptions.Compiled);
private string m_Title;
public string Title
{
get
{
return m_Title;
}
set
{
m_Title = value;
string pinyin = m_Title.Unidecode();
PinyinTitle = pinyin;
AbbrTitle = AbbrRegexp.Replace(global::System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(pinyin), "");
if (AbbrTitle.Length < 2) AbbrTitle = null;
}
}
public string PinyinTitle { get; private set; }
public string AbbrTitle { get; private set; }
public string IcoPath { get; set; }
public string ExecutePath { get; set; }
public string ExecuteName { get; set; }
public int Score { get; set; }
public IProgramSource Source { get; set; }
}
public class Programs : BaseSystemPlugin, ISettingProvider public class Programs : BaseSystemPlugin, ISettingProvider
{ {
List<Program> installedList = new List<Program>(); public static bool Initing = false;
List<IProgramSource> sources = new List<IProgramSource>(); private static object lockObject = new object();
public static Dictionary<string, Type> SourceTypes = new Dictionary<string, Type>() { private static List<Program> programs = new List<Program>();
private static List<IProgramSource> sources = new List<IProgramSource>();
private static Dictionary<string, Type> SourceTypes = new Dictionary<string, Type>() {
{"FileSystemProgramSource", typeof(FileSystemProgramSource)}, {"FileSystemProgramSource", typeof(FileSystemProgramSource)},
{"PortableAppsProgramSource", typeof(PortableAppsProgramSource)},
{"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)}, {"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)},
{"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)}, {"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)},
{"AppPathsProgramSource", typeof(AppPathsProgramSource)}, {"AppPathsProgramSource", typeof(AppPathsProgramSource)},
//{"FileSystemFolderSourceShallow", typeof(FileSystemFolderSourceShallow)},
}; };
private PluginInitContext context; private PluginInitContext context;
@@ -54,10 +27,8 @@ namespace Wox.Plugin.SystemPlugins.Program
if (query.RawQuery.Trim().Length <= 1) return new List<Result>(); if (query.RawQuery.Trim().Length <= 1) return new List<Result>();
var fuzzyMather = FuzzyMatcher.Create(query.RawQuery); var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
List<Program> returnList = installedList.Where(o => MatchProgram(o, fuzzyMather)).ToList(); List<Program> returnList = programs.Where(o => MatchProgram(o, fuzzyMather)).ToList();
returnList.ForEach(ScoreFilter); returnList.ForEach(ScoreFilter);
//return ordered list instead of return the score, because programs scores will affect other
//plugins, the weight of program should be less than the plugins when they showed at the same time.
returnList = returnList.OrderByDescending(o => o.Score).ToList(); returnList = returnList.OrderByDescending(o => o.Score).ToList();
return returnList.Select(c => new Result() return returnList.Select(c => new Result()
@@ -88,41 +59,83 @@ namespace Wox.Plugin.SystemPlugins.Program
protected override void InitInternal(PluginInitContext context) protected override void InitInternal(PluginInitContext context)
{ {
this.context = context; this.context = context;
LoadPrograms();
}
if (UserSettingStorage.Instance.ProgramSources == null) public static void LoadPrograms()
UserSettingStorage.Instance.ProgramSources = UserSettingStorage.Instance.LoadDefaultProgramSources(); {
lock (lockObject)
UserSettingStorage.Instance.ProgramSources.ForEach(source =>
{ {
if (source.Enabled) Initing = true;
List<ProgramSource> programSources = new List<ProgramSource>();
programSources.AddRange(LoadDeaultProgramSources());
if (UserSettingStorage.Instance.ProgramSources != null &&
UserSettingStorage.Instance.ProgramSources.Count(o => o.Enabled) > 0)
{
programSources.AddRange(UserSettingStorage.Instance.ProgramSources.Where(o => o.Enabled));
}
programSources.ForEach(source =>
{ {
Type sourceClass; Type sourceClass;
if (SourceTypes.TryGetValue(source.Type, out sourceClass)) if (SourceTypes.TryGetValue(source.Type, out sourceClass))
{ {
sources.Add(sourceClass.GetConstructor( ConstructorInfo constructorInfo = sourceClass.GetConstructor(new[] { typeof(ProgramSource) });
new Type[] { typeof(ProgramSource) } if (constructorInfo != null)
).Invoke(new object[] { source }) as IProgramSource); {
IProgramSource programSource =
constructorInfo.Invoke(new object[] { source }) as IProgramSource;
sources.Add(programSource);
}
} }
else
{
// TODO: invalid class
}
}
});
foreach (var source in sources)
{
var list = source.LoadPrograms();
list.ForEach(o =>
{
o.Source = source;
}); });
installedList.AddRange(list);
}
// filter duplicate program var tempPrograms = new List<Program>();
installedList = installedList.GroupBy(x => new { x.ExecutePath, x.ExecuteName }) foreach (var source in sources)
.Select(g => g.First()).ToList(); {
var list = source.LoadPrograms();
list.ForEach(o =>
{
o.Source = source;
});
tempPrograms.AddRange(list);
}
// filter duplicate program
tempPrograms = tempPrograms.GroupBy(x => new { x.ExecutePath, x.ExecuteName })
.Select(g => g.First()).ToList();
programs = tempPrograms;
Initing = false;
}
}
/// <summary>
/// Load program sources that wox always provide
/// </summary>
private static List<ProgramSource> LoadDeaultProgramSources()
{
var list = new List<ProgramSource>();
list.Add(new ProgramSource()
{
BonusPoints = 0,
Enabled = true,
Type = "CommonStartMenuProgramSource"
});
list.Add(new ProgramSource()
{
BonusPoints = 0,
Enabled = true,
Type = "UserStartMenuProgramSource"
});
list.Add(new ProgramSource()
{
BonusPoints = -10,
Enabled = true,
Type = "AppPathsProgramSource"
});
return list;
} }
private void ScoreFilter(Program p) private void ScoreFilter(Program p)
@@ -139,7 +152,6 @@ namespace Wox.Plugin.SystemPlugins.Program
p.Score -= 20; p.Score -= 20;
} }
public override string ID public override string ID
{ {
get { return "791FC278BA414111B8D1886DFE447410"; } get { return "791FC278BA414111B8D1886DFE447410"; }

View File

@@ -65,15 +65,12 @@
<Compile Include="Folder\FolderPluginSettings.xaml.cs"> <Compile Include="Folder\FolderPluginSettings.xaml.cs">
<DependentUpon>FolderPluginSettings.xaml</DependentUpon> <DependentUpon>FolderPluginSettings.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Program\Program.cs" />
<Compile Include="Program\ProgramSetting.xaml.cs"> <Compile Include="Program\ProgramSetting.xaml.cs">
<DependentUpon>ProgramSetting.xaml</DependentUpon> <DependentUpon>ProgramSetting.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Program\ProgramSourceSetting.xaml.cs">
<DependentUpon>ProgramSourceSetting.xaml</DependentUpon>
</Compile>
<Compile Include="Program\ProgramSources\AppPathsProgramSource.cs" /> <Compile Include="Program\ProgramSources\AppPathsProgramSource.cs" />
<Compile Include="Program\ProgramSources\CommonStartMenuProgramSource.cs" /> <Compile Include="Program\ProgramSources\CommonStartMenuProgramSource.cs" />
<Compile Include="Program\ProgramSources\PortableAppsProgramSource.cs" />
<Compile Include="Program\IProgramSource.cs" /> <Compile Include="Program\IProgramSource.cs" />
<Compile Include="BaseSystemPlugin.cs" /> <Compile Include="BaseSystemPlugin.cs" />
<Compile Include="Calculator.cs" /> <Compile Include="Calculator.cs" />
@@ -125,10 +122,6 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Program\ProgramSourceSetting.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Sys\SysSettings.xaml"> <Page Include="Sys\SysSettings.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>