PowerToys/Wox.Plugin.System/Programs.cs

150 lines
5.1 KiB
C#
Raw Normal View History

using System;
2014-01-04 13:26:13 +01:00
using System.Collections.Generic;
using System.ComponentModel;
2014-01-04 13:26:13 +01:00
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
2014-01-04 13:26:13 +01:00
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using Wox.Infrastructure;
2014-01-04 13:26:13 +01:00
2014-01-29 11:33:24 +01:00
namespace Wox.Plugin.System
2014-01-04 13:26:13 +01:00
{
public class Program
{
public string Title { get; set; }
public string IcoPath { get; set; }
public string ExecutePath { get; set; }
2014-01-05 10:56:02 +01:00
public int Score { get; set; }
2014-01-04 13:26:13 +01:00
}
2014-01-15 15:45:02 +01:00
public class Programs : BaseSystemPlugin
2014-01-04 13:26:13 +01:00
{
2014-01-05 10:56:02 +01:00
private List<string> indexDirectory = new List<string>();
private List<string> indexPostfix = new List<string> { "lnk", "exe" };
2014-01-04 13:26:13 +01: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 15:45:02 +01:00
protected override List<Result> QueryInternal(Query query)
2014-01-04 13:26:13 +01:00
{
2014-01-05 10:56:02 +01:00
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
2014-01-04 13:26:13 +01:00
var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
List<Program> returnList = installedList.Where(o => MatchProgram(o, fuzzyMather)).ToList();
2014-01-05 10:56:02 +01:00
returnList.ForEach(ScoreFilter);
2014-03-02 04:29:14 +01: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();
returnList.ForEach(o=>o.Score = 0);
2014-01-05 10:56:02 +01:00
return returnList.Select(c => new Result()
2014-01-04 13:26:13 +01:00
{
Title = c.Title,
IcoPath = c.IcoPath,
2014-01-05 10:56:02 +01:00
Score = c.Score,
Action = (context) =>
2014-01-04 13:26:13 +01: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 13:26:13 +01:00
}
2014-02-28 16:21:01 +01:00
return true;
2014-01-04 13:26:13 +01:00
}
}).ToList();
}
private bool MatchProgram(Program program, FuzzyMatcher matcher)
2014-01-05 10:56:02 +01:00
{
program.Score = matcher.Score(program.Title);
if (program.Score > 0) return true;
program.Score = matcher.Score(ChineseToPinYin.ToPinYin(program.Title).Replace(" ", ""));
if (program.Score > 0) return true;
2014-01-05 10:56:02 +01:00
return false;
}
2014-01-15 15:45:02 +01:00
protected override void InitInternal(PluginInitContext context)
2014-01-04 13:26:13 +01:00
{
2014-01-05 10:56:02 +01: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 10:56:02 +01:00
2014-01-04 13:26:13 +01:00
GetAppFromStartMenu();
}
private void GetAppFromStartMenu()
{
2014-01-05 10:56:02 +01:00
foreach (string directory in indexDirectory)
2014-01-04 13:26:13 +01:00
{
2014-01-05 10:56:02 +01:00
GetAppFromDirectory(directory);
2014-01-04 13:26:13 +01:00
}
}
private void GetAppFromDirectory(string path)
{
foreach (string file in Directory.GetFiles(path))
{
2014-01-05 10:56:02 +01:00
if (indexPostfix.Any(o => file.EndsWith("." + o)))
2014-01-04 13:26:13 +01: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 10:56:02 +01:00
private void ScoreFilter(Program p)
{
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start"))
{
2014-03-02 04:29:14 +01:00
p.Score += 1;
2014-01-05 10:56:02 +01:00
}
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall"))
{
2014-03-02 04:29:14 +01:00
p.Score -= 1;
2014-01-05 10:56:02 +01:00
}
}
2014-01-04 13:26:13 +01:00
private string getAppNameFromAppPath(string app)
{
string temp = app.Substring(app.LastIndexOf('\\') + 1);
string name = temp.Substring(0, temp.LastIndexOf('.'));
return name;
}
}
}