PowerToys/Wox.Plugin.SystemPlugins/Program/Programs.cs

173 lines
6.4 KiB
C#
Raw Normal View History

using System;
2014-01-04 20:26:13 +08:00
using System.Collections.Generic;
using System.Linq;
using Wox.Infrastructure;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage.UserSettings;
2014-03-30 11:16:44 +08:00
using Wox.Plugin.SystemPlugins.Program.ProgramSources;
2014-01-04 20:26:13 +08:00
2014-03-30 11:16:44 +08:00
namespace Wox.Plugin.SystemPlugins.Program
2014-01-04 20:26:13 +08:00
{
public class Program
{
2014-03-18 13:17:09 +08:00
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;
2014-03-18 13:17:09 +08:00
AbbrTitle = AbbrRegexp.Replace(global::System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(pinyin), "");
if (AbbrTitle.Length < 2) AbbrTitle = null;
}
}
public string PinyinTitle { get; private set; }
2014-03-18 13:17:09 +08:00
public string AbbrTitle { get; private set; }
2014-01-04 20:26:13 +08:00
public string IcoPath { get; set; }
public string ExecutePath { get; set; }
public string ExecuteName { get; set; }
2014-01-05 17:56:02 +08:00
public int Score { get; set; }
public IProgramSource Source { get; set; }
2014-01-04 20:26:13 +08:00
}
public class Programs : BaseSystemPlugin, ISettingProvider
2014-01-04 20:26:13 +08:00
{
List<Program> installedList = new List<Program>();
List<IProgramSource> sources = new List<IProgramSource>();
2014-03-19 20:16:20 +08:00
public static Dictionary<string, Type> SourceTypes = new Dictionary<string, Type>() {
2014-03-21 04:31:42 +08:00
{"FileSystemProgramSource", typeof(FileSystemProgramSource)},
{"PortableAppsProgramSource", typeof(PortableAppsProgramSource)},
{"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)},
{"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)},
{"AppPathsProgramSource", typeof(AppPathsProgramSource)},
2014-04-02 02:50:01 +08:00
//{"FileSystemFolderSourceShallow", typeof(FileSystemFolderSourceShallow)},
};
private PluginInitContext context;
2014-01-15 22:45:02 +08:00
protected override List<Result> QueryInternal(Query query)
2014-01-04 20:26:13 +08:00
{
if (query.RawQuery.Trim().Length <= 1) return new List<Result>();
2014-01-04 20:26:13 +08:00
var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
List<Program> returnList = installedList.Where(o => MatchProgram(o, fuzzyMather)).ToList();
2014-01-05 17:56:02 +08:00
returnList.ForEach(ScoreFilter);
2014-03-02 11:29:14 +08:00
//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();
2014-01-05 17:56:02 +08:00
return returnList.Select(c => new Result()
2014-01-04 20:26:13 +08:00
{
Title = c.Title,
SubTitle = c.ExecutePath,
2014-01-04 20:26:13 +08:00
IcoPath = c.IcoPath,
Score = 0,
Action = (e) =>
2014-01-04 20:26:13 +08:00
{
2014-07-21 19:48:17 +08:00
context.API.HideApp();
context.API.ShellRun(c.ExecutePath);
2014-02-28 23:21:01 +08:00
return true;
2014-01-04 20:26:13 +08:00
}
}).ToList();
}
private bool MatchProgram(Program program, FuzzyMatcher matcher)
2014-01-05 17:56:02 +08:00
{
2014-03-24 21:14:10 +08:00
if (program.AbbrTitle != null && (program.Score = matcher.Evaluate(program.AbbrTitle).Score) > 0) return true;
if ((program.Score = matcher.Evaluate(program.Title).Score) > 0) return true;
if ((program.Score = matcher.Evaluate(program.PinyinTitle).Score) > 0) return true;
if (program.ExecuteName != null && (program.Score = matcher.Evaluate(program.ExecuteName).Score) > 0) return true;
2014-01-05 17:56:02 +08:00
return false;
}
2014-01-15 22:45:02 +08:00
protected override void InitInternal(PluginInitContext context)
2014-01-04 20:26:13 +08:00
{
this.context = context;
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.ProgramSources == null)
UserSettingStorage.Instance.ProgramSources = UserSettingStorage.Instance.LoadDefaultProgramSources();
2014-01-05 17:56:02 +08:00
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ProgramSources.ForEach(source =>
{
if (source.Enabled)
{
Type sourceClass;
2014-03-19 20:16:20 +08:00
if (SourceTypes.TryGetValue(source.Type, out sourceClass))
{
sources.Add(sourceClass.GetConstructor(
2014-03-23 16:17:41 +08:00
new Type[] { typeof(ProgramSource) }
).Invoke(new object[] { source }) as IProgramSource);
}
else
{
// TODO: invalid class
}
}
});
2014-01-04 20:26:13 +08:00
foreach (var source in sources)
2014-01-04 20:26:13 +08:00
{
var list = source.LoadPrograms();
list.ForEach(o =>
2014-01-04 20:26:13 +08:00
{
o.Source = source;
});
installedList.AddRange(list);
2014-01-04 20:26:13 +08:00
}
2014-03-22 13:50:20 +08:00
// filter duplicate program
installedList = installedList.GroupBy(x => new { x.ExecutePath, x.ExecuteName })
.Select(g => g.First()).ToList();
2014-01-04 20:26:13 +08:00
}
2014-01-05 17:56:02 +08:00
private void ScoreFilter(Program p)
{
p.Score += p.Source.BonusPoints;
2014-01-05 17:56:02 +08:00
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
2014-03-18 13:17:09 +08:00
p.Score += 10;
if (p.Title.Contains("帮助") || p.Title.ToLower().Contains("help") || p.Title.Contains("文档") || p.Title.ToLower().Contains("documentation"))
p.Score -= 10;
2014-01-05 17:56:02 +08:00
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
2014-03-18 13:17:09 +08:00
p.Score -= 20;
2014-01-05 17:56:02 +08:00
}
public override string ID
{
get { return "791FC278BA414111B8D1886DFE447410"; }
}
public override string Name
{
get { return "Programs"; }
}
public override string IcoPath
{
2014-06-30 21:31:13 +08:00
get { return @"Images\program.png"; }
}
public override string Description
{
2014-06-30 21:31:13 +08:00
get { return "Provide searching programs in your computer."; }
}
#region ISettingProvider Members
public System.Windows.Controls.Control CreateSettingPanel()
{
return new ProgramSetting();
}
#endregion
2014-01-04 20:26:13 +08:00
}
}