PowerToys/Wox.Plugin.System/Programs.cs

150 lines
5.9 KiB
C#
Raw Normal View History

using System;
2014-01-04 20:26:13 +08:00
using System.Collections.Generic;
using System.ComponentModel;
2014-01-04 20:26:13 +08:00
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
2014-01-04 20:26:13 +08:00
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using Wox.Infrastructure;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
2014-03-21 04:31:42 +08:00
using Wox.Plugin.System.ProgramSources;
2014-01-04 20:26:13 +08:00
2014-01-29 18:33:24 +08:00
namespace Wox.Plugin.System
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;
2014-03-18 13:17:09 +08:00
string pinyin = ChineseToPinYin.ToPinYin(m_Title);
PinyinTitle = pinyin.Replace(" ", "").ToLower();
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
}
2014-01-15 22:45:02 +08:00
public class Programs : BaseSystemPlugin
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)},
};
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
{
2014-01-05 17:56:02 +08:00
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.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
{
context.HideApp();
context.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
}
2014-01-04 20:26:13 +08:00
}
}