diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index 8350fce8a6..129799d49c 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -135,30 +135,6 @@ namespace Wox.Infrastructure.Storage.UserSettings return webSearches; } - public List LoadDefaultProgramSources() - { - var list = new List(); - 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 { get { return "config"; } @@ -169,7 +145,7 @@ namespace Wox.Infrastructure.Storage.UserSettings Theme = "Dark"; ReplaceWinR = true; WebSearches = LoadDefaultWebSearches(); - ProgramSources = LoadDefaultProgramSources(); + ProgramSources = new List(); CustomizedPluginConfigs = new List(); Hotkey = "Alt + Space"; QueryBoxFont = FontFamily.GenericSansSerif.Name; diff --git a/Wox.Plugin.SystemPlugins/Program/IProgramSource.cs b/Wox.Plugin.SystemPlugins/Program/IProgramSource.cs index 4efcb1e409..44eb232e6f 100644 --- a/Wox.Plugin.SystemPlugins/Program/IProgramSource.cs +++ b/Wox.Plugin.SystemPlugins/Program/IProgramSource.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; namespace Wox.Plugin.SystemPlugins.Program { @@ -19,23 +20,26 @@ namespace Wox.Plugin.SystemPlugins.Program 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, ExecutePath = file }; - switch (global::System.IO.Path.GetExtension(file).ToLower()) + switch (Path.GetExtension(file).ToLower()) { case ".exe": p.ExecuteName = global::System.IO.Path.GetFileName(file); try { 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) { } break; diff --git a/Wox.Plugin.SystemPlugins/Program/Program.cs b/Wox.Plugin.SystemPlugins/Program/Program.cs new file mode 100644 index 0000000000..8148e3cbbb --- /dev/null +++ b/Wox.Plugin.SystemPlugins/Program/Program.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml b/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml index f8d7623d0b..2a66b922f2 100644 --- a/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml +++ b/Wox.Plugin.SystemPlugins/Program/ProgramSetting.xaml @@ -5,8 +5,11 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:infrastructure="clr-namespace:Wox.Infrastructure;assembly=Wox.Infrastructure" mc:Ignorable="d" - d:DesignHeight="300" d:DesignWidth="300"> + d:DesignHeight="300" d:DesignWidth="600"> + + + @@ -22,35 +25,19 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Wox.Plugin.SystemPlugins/Program/ProgramSourceSetting.xaml.cs b/Wox.Plugin.SystemPlugins/Program/ProgramSourceSetting.xaml.cs deleted file mode 100644 index d38a97bca5..0000000000 --- a/Wox.Plugin.SystemPlugins/Program/ProgramSourceSetting.xaml.cs +++ /dev/null @@ -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; - } - } -} diff --git a/Wox.Plugin.SystemPlugins/Program/ProgramSources/PortableAppsProgramSource.cs b/Wox.Plugin.SystemPlugins/Program/ProgramSources/PortableAppsProgramSource.cs deleted file mode 100644 index 284f99e98f..0000000000 --- a/Wox.Plugin.SystemPlugins/Program/ProgramSources/PortableAppsProgramSource.cs +++ /dev/null @@ -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 LoadPrograms() - { - List list = new List(); - 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; - } - } -} diff --git a/Wox.Plugin.SystemPlugins/Program/Programs.cs b/Wox.Plugin.SystemPlugins/Program/Programs.cs index c93d2e71df..5665d9ae72 100644 --- a/Wox.Plugin.SystemPlugins/Program/Programs.cs +++ b/Wox.Plugin.SystemPlugins/Program/Programs.cs @@ -1,51 +1,24 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using Wox.Infrastructure; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin.SystemPlugins.Program.ProgramSources; 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 { - List installedList = new List(); - List sources = new List(); - public static Dictionary SourceTypes = new Dictionary() { + public static bool Initing = false; + private static object lockObject = new object(); + private static List programs = new List(); + private static List sources = new List(); + private static Dictionary SourceTypes = new Dictionary() { {"FileSystemProgramSource", typeof(FileSystemProgramSource)}, - {"PortableAppsProgramSource", typeof(PortableAppsProgramSource)}, {"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)}, {"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)}, {"AppPathsProgramSource", typeof(AppPathsProgramSource)}, - //{"FileSystemFolderSourceShallow", typeof(FileSystemFolderSourceShallow)}, }; private PluginInitContext context; @@ -54,10 +27,8 @@ namespace Wox.Plugin.SystemPlugins.Program if (query.RawQuery.Trim().Length <= 1) return new List(); var fuzzyMather = FuzzyMatcher.Create(query.RawQuery); - List returnList = installedList.Where(o => MatchProgram(o, fuzzyMather)).ToList(); + List returnList = programs.Where(o => MatchProgram(o, fuzzyMather)).ToList(); 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(); return returnList.Select(c => new Result() @@ -88,41 +59,83 @@ namespace Wox.Plugin.SystemPlugins.Program protected override void InitInternal(PluginInitContext context) { this.context = context; + LoadPrograms(); + } - if (UserSettingStorage.Instance.ProgramSources == null) - UserSettingStorage.Instance.ProgramSources = UserSettingStorage.Instance.LoadDefaultProgramSources(); - - UserSettingStorage.Instance.ProgramSources.ForEach(source => + public static void LoadPrograms() + { + lock (lockObject) { - if (source.Enabled) + Initing = true; + + List programSources = new List(); + 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; if (SourceTypes.TryGetValue(source.Type, out sourceClass)) { - sources.Add(sourceClass.GetConstructor( - new Type[] { typeof(ProgramSource) } - ).Invoke(new object[] { source }) as IProgramSource); + ConstructorInfo constructorInfo = sourceClass.GetConstructor(new[] { typeof(ProgramSource) }); + if (constructorInfo != null) + { + 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 - installedList = installedList.GroupBy(x => new { x.ExecutePath, x.ExecuteName }) - .Select(g => g.First()).ToList(); + var tempPrograms = new List(); + foreach (var source in sources) + { + 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; + } + } + + /// + /// Load program sources that wox always provide + /// + private static List LoadDeaultProgramSources() + { + var list = new List(); + 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) @@ -139,7 +152,6 @@ namespace Wox.Plugin.SystemPlugins.Program p.Score -= 20; } - public override string ID { get { return "791FC278BA414111B8D1886DFE447410"; } diff --git a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj index fbceb2471d..ca1cfab4f2 100644 --- a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj +++ b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj @@ -65,15 +65,12 @@ FolderPluginSettings.xaml + ProgramSetting.xaml - - ProgramSourceSetting.xaml - - @@ -125,10 +122,6 @@ Designer MSBuild:Compile - - MSBuild:Compile - Designer - Designer MSBuild:Compile