PowerToys/Wox.Plugin.System/Programs.cs

158 lines
6.1 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-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>() {
{"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)},
{"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)},
{"AppPathsProgramSource", typeof(AppPathsProgramSource)},
{"PortableAppsProgramSource", typeof(PortableAppsProgramSource)},
{"FileSystemProgramSource", typeof(FileSystemProgramSource)},
};
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 = (context) =>
2014-01-04 20:26:13 +08:00
{
if (string.IsNullOrEmpty(c.ExecutePath))
{
MessageBox.Show("couldn't start" + c.Title);
}
else
{
try
{
Process.Start(c.ExecutePath);
}
catch (Win32Exception)
{
//Do nothing.
//It may be caused if UAC blocks the program.
}
catch (Exception e)
{
throw e;
}
2014-01-04 20:26:13 +08:00
}
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-18 13:17:09 +08:00
if (program.AbbrTitle != null && (program.Score = matcher.Score(program.AbbrTitle)) > 0) return true;
if ((program.Score = matcher.Score(program.Title)) > 0) return true;
if ((program.Score = matcher.Score(program.PinyinTitle)) > 0) return true;
if (program.ExecuteName != null && (program.Score = matcher.Score(program.ExecuteName)) > 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
{
if (CommonStorage.Instance.UserSetting.ProgramSources == null)
CommonStorage.Instance.UserSetting.ProgramSources = CommonStorage.Instance.UserSetting.LoadDefaultProgramSources();
2014-01-05 17:56:02 +08:00
CommonStorage.Instance.UserSetting.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(
new Type[] { typeof(Wox.Infrastructure.UserSettings.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-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
}
}