2014-02-08 14:37:40 +08:00
|
|
|
using System;
|
2014-01-04 20:26:13 +08:00
|
|
|
using System.Collections.Generic;
|
2014-02-08 14:37:40 +08:00
|
|
|
using System.ComponentModel;
|
2014-01-04 20:26:13 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2014-01-06 21:25:24 +08:00
|
|
|
using System.Runtime.InteropServices;
|
2014-01-04 20:26:13 +08:00
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using Microsoft.Win32;
|
2014-02-06 22:22:02 +08:00
|
|
|
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);
|
2014-03-17 03:37:52 +08:00
|
|
|
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;
|
2014-03-17 03:37:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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; }
|
2014-03-19 03:24:15 +08:00
|
|
|
public string ExecuteName { get; set; }
|
2014-01-05 17:56:02 +08:00
|
|
|
public int Score { get; set; }
|
2014-03-19 02:06:51 +08:00
|
|
|
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>();
|
2014-03-19 02:06:51 +08:00
|
|
|
List<IProgramSource> sources = new List<IProgramSource>();
|
2014-03-19 04:05:27 +08:00
|
|
|
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-06 21:25:24 +08:00
|
|
|
|
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
|
|
|
|
2014-02-09 00:33:10 +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,
|
2014-03-19 02:06:51 +08:00
|
|
|
SubTitle = c.ExecutePath,
|
2014-01-04 20:26:13 +08:00
|
|
|
IcoPath = c.IcoPath,
|
2014-03-17 03:37:52 +08:00
|
|
|
Score = 0,
|
2014-02-09 20:55:18 +08:00
|
|
|
Action = (context) =>
|
2014-01-04 20:26:13 +08:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(c.ExecutePath))
|
|
|
|
{
|
|
|
|
MessageBox.Show("couldn't start" + c.Title);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-08 14:37:40 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-02-09 00:33:10 +08:00
|
|
|
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;
|
2014-03-17 03:37:52 +08:00
|
|
|
if ((program.Score = matcher.Score(program.Title)) > 0) return true;
|
|
|
|
if ((program.Score = matcher.Score(program.PinyinTitle)) > 0) return true;
|
2014-03-19 03:24:15 +08:00
|
|
|
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
|
|
|
{
|
2014-03-19 04:05:27 +08:00
|
|
|
if (CommonStorage.Instance.UserSetting.ProgramSources == null)
|
|
|
|
CommonStorage.Instance.UserSetting.ProgramSources = CommonStorage.Instance.UserSetting.LoadDefaultProgramSources();
|
2014-01-05 17:56:02 +08:00
|
|
|
|
2014-03-19 04:05:27 +08:00
|
|
|
CommonStorage.Instance.UserSetting.ProgramSources.ForEach(source =>
|
|
|
|
{
|
|
|
|
if (source.Enabled)
|
|
|
|
{
|
|
|
|
Type sourceClass;
|
|
|
|
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
|
|
|
|
2014-03-19 02:06:51 +08:00
|
|
|
foreach (var source in sources)
|
2014-01-04 20:26:13 +08:00
|
|
|
{
|
2014-03-19 02:06:51 +08:00
|
|
|
var list = source.LoadPrograms();
|
|
|
|
list.ForEach(o =>
|
2014-01-04 20:26:13 +08:00
|
|
|
{
|
2014-03-19 02:06:51 +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)
|
|
|
|
{
|
2014-03-19 02:06:51 +08:00
|
|
|
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
|
|
|
}
|
2014-02-08 14:37:40 +08:00
|
|
|
}
|