PowerToys/Wox.Plugin.System/Programs.cs

159 lines
5.2 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
{
private string m_Title;
public string Title
{
get
{
return m_Title;
}
set
{
m_Title = value;
PinyinTitle = ChineseToPinYin.ToPinYin(m_Title).Replace(" ", "").ToLower();
}
}
public string PinyinTitle { get; private set; }
2014-01-04 20:26:13 +08:00
public string IcoPath { get; set; }
public string ExecutePath { get; set; }
2014-01-05 17:56:02 +08:00
public int Score { 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
{
2014-01-05 17:56:02 +08:00
private List<string> indexDirectory = new List<string>();
private List<string> indexPostfix = new List<string> { "lnk", "exe" };
2014-01-04 20:26:13 +08:00
List<Program> installedList = new List<Program>();
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,[Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_COMMON_STARTMENU = 0x16; // \Windows\Start Menu\Programs
const int CSIDL_COMMON_PROGRAMS = 0x17;
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,
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
{
if ((program.Score = matcher.Score(program.Title)) > 0) return true;
if ((program.Score = matcher.Score(program.PinyinTitle)) > 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-01-05 17:56:02 +08:00
indexDirectory.Add(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
StringBuilder commonStartMenuPath = new StringBuilder(560);
SHGetSpecialFolderPath(IntPtr.Zero, commonStartMenuPath, CSIDL_COMMON_PROGRAMS, false);
indexDirectory.Add(commonStartMenuPath.ToString());
2014-01-05 17:56:02 +08:00
2014-01-04 20:26:13 +08:00
GetAppFromStartMenu();
}
private void GetAppFromStartMenu()
{
2014-01-05 17:56:02 +08:00
foreach (string directory in indexDirectory)
2014-01-04 20:26:13 +08:00
{
2014-01-05 17:56:02 +08:00
GetAppFromDirectory(directory);
2014-01-04 20:26:13 +08:00
}
}
private void GetAppFromDirectory(string path)
{
foreach (string file in Directory.GetFiles(path))
{
2014-01-05 17:56:02 +08:00
if (indexPostfix.Any(o => file.EndsWith("." + o)))
2014-01-04 20:26:13 +08:00
{
Program p = new Program()
{
Title = getAppNameFromAppPath(file),
IcoPath = file,
ExecutePath = file
};
installedList.Add(p);
}
}
foreach (var subDirectory in Directory.GetDirectories(path))
{
GetAppFromDirectory(subDirectory);
}
}
2014-01-05 17:56:02 +08:00
private void ScoreFilter(Program p)
{
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
{
2014-03-02 11:29:14 +08:00
p.Score += 1;
2014-01-05 17:56:02 +08:00
}
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
{
2014-03-02 11:29:14 +08:00
p.Score -= 1;
2014-01-05 17:56:02 +08:00
}
}
2014-01-04 20:26:13 +08:00
private string getAppNameFromAppPath(string app)
{
return global::System.IO.Path.GetFileNameWithoutExtension(app);
2014-01-04 20:26:13 +08:00
}
}
}