2016-08-20 06:05:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-08-22 09:21:22 +08:00
|
|
|
|
using System.ComponentModel;
|
2016-08-20 06:05:59 +08:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-11-29 09:55:25 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2017-03-02 07:21:34 +08:00
|
|
|
|
using System.Security;
|
2016-08-21 01:18:41 +08:00
|
|
|
|
using System.Text;
|
2016-08-21 00:08:16 +08:00
|
|
|
|
using Microsoft.Win32;
|
2016-08-21 01:18:41 +08:00
|
|
|
|
using Shell;
|
2016-08-22 09:21:22 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2016-08-20 06:05:59 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
|
2016-08-20 08:17:28 +08:00
|
|
|
|
namespace Wox.Plugin.Program.Programs
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2016-08-22 09:21:22 +08:00
|
|
|
|
public class Win32 : IProgram
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 01:50:14 +08:00
|
|
|
|
public string Name { get; set; }
|
2016-08-20 08:17:28 +08:00
|
|
|
|
public string IcoPath { get; set; }
|
2016-08-21 00:08:16 +08:00
|
|
|
|
public string FullPath { get; set; }
|
|
|
|
|
public string ParentDirectory { get; set; }
|
2016-08-20 08:17:28 +08:00
|
|
|
|
public string ExecutableName { get; set; }
|
2016-08-21 01:50:14 +08:00
|
|
|
|
public string Description { get; set; }
|
2016-09-06 01:08:30 +08:00
|
|
|
|
public bool Valid { get; set; }
|
2016-08-20 06:05:59 +08:00
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
|
private const string ShortcutExtension = "lnk";
|
|
|
|
|
private const string ApplicationReferenceExtension = "appref-ms";
|
|
|
|
|
private const string ExeExtension = "exe";
|
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
private int Score(string query)
|
|
|
|
|
{
|
|
|
|
|
var score1 = StringMatcher.Score(Name, query);
|
|
|
|
|
var score2 = StringMatcher.ScoreForPinyin(Name, query);
|
|
|
|
|
var score3 = StringMatcher.Score(Description, query);
|
|
|
|
|
var score4 = StringMatcher.ScoreForPinyin(Description, query);
|
|
|
|
|
var score5 = StringMatcher.Score(ExecutableName, query);
|
|
|
|
|
var score = new[] { score1, score2, score3, score4, score5 }.Max();
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Result Result(string query, IPublicAPI api)
|
|
|
|
|
{
|
|
|
|
|
var result = new Result
|
|
|
|
|
{
|
|
|
|
|
SubTitle = FullPath,
|
|
|
|
|
IcoPath = IcoPath,
|
|
|
|
|
Score = Score(query),
|
|
|
|
|
ContextData = this,
|
|
|
|
|
Action = e =>
|
|
|
|
|
{
|
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = FullPath,
|
|
|
|
|
WorkingDirectory = ParentDirectory
|
|
|
|
|
};
|
|
|
|
|
var hide = Main.StartProcess(info);
|
|
|
|
|
return hide;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (Description.Length >= Name.Length &&
|
|
|
|
|
Description.Substring(0, Name.Length) == Name)
|
|
|
|
|
{
|
|
|
|
|
result.Title = Description;
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(Description))
|
|
|
|
|
{
|
|
|
|
|
result.Title = $"{Name}: {Description}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Title = Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> ContextMenus(IPublicAPI api)
|
|
|
|
|
{
|
|
|
|
|
var contextMenus = new List<Result>
|
|
|
|
|
{
|
|
|
|
|
new Result
|
|
|
|
|
{
|
|
|
|
|
Title = api.GetTranslation("wox_plugin_program_run_as_administrator"),
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = FullPath,
|
|
|
|
|
WorkingDirectory = ParentDirectory,
|
|
|
|
|
Verb = "runas"
|
|
|
|
|
};
|
|
|
|
|
var hide = Main.StartProcess(info);
|
|
|
|
|
return hide;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = "Images/cmd.png"
|
|
|
|
|
},
|
|
|
|
|
new Result
|
|
|
|
|
{
|
|
|
|
|
Title = api.GetTranslation("wox_plugin_program_open_containing_folder"),
|
|
|
|
|
Action = _ =>
|
|
|
|
|
{
|
|
|
|
|
var hide = Main.StartProcess(new ProcessStartInfo(ParentDirectory));
|
|
|
|
|
return hide;
|
|
|
|
|
},
|
|
|
|
|
IcoPath = "Images/folder.png"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return contextMenus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return ExecutableName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Win32 Win32Program(string path)
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-20 08:17:28 +08:00
|
|
|
|
var p = new Win32
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 01:50:14 +08:00
|
|
|
|
Name = Path.GetFileNameWithoutExtension(path),
|
2016-08-21 00:08:16 +08:00
|
|
|
|
IcoPath = path,
|
|
|
|
|
FullPath = path,
|
|
|
|
|
ParentDirectory = Directory.GetParent(path).FullName,
|
2016-09-06 01:08:30 +08:00
|
|
|
|
Description = string.Empty,
|
|
|
|
|
Valid = true
|
2016-08-20 06:05:59 +08:00
|
|
|
|
};
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Win32 LnkProgram(string path)
|
|
|
|
|
{
|
|
|
|
|
var program = Win32Program(path);
|
|
|
|
|
try
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 01:18:41 +08:00
|
|
|
|
var link = new ShellLink();
|
|
|
|
|
const uint STGM_READ = 0;
|
2017-01-24 08:24:20 +08:00
|
|
|
|
((IPersistFile)link).Load(path, STGM_READ);
|
2016-08-21 01:18:41 +08:00
|
|
|
|
var hwnd = new _RemotableHandle();
|
|
|
|
|
link.Resolve(ref hwnd, 0);
|
|
|
|
|
|
|
|
|
|
const int MAX_PATH = 260;
|
|
|
|
|
StringBuilder buffer = new StringBuilder(MAX_PATH);
|
2016-09-06 01:08:30 +08:00
|
|
|
|
|
|
|
|
|
var data = new _WIN32_FIND_DATAW();
|
|
|
|
|
const uint SLGP_SHORTPATH = 1;
|
|
|
|
|
link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH);
|
|
|
|
|
var target = buffer.ToString();
|
2016-11-29 09:55:25 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(target))
|
2016-08-21 00:08:16 +08:00
|
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
|
var extension = Extension(target);
|
|
|
|
|
if (extension == ExeExtension && File.Exists(target))
|
2016-09-06 01:08:30 +08:00
|
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
|
buffer = new StringBuilder(MAX_PATH);
|
|
|
|
|
link.GetDescription(buffer, MAX_PATH);
|
|
|
|
|
var description = buffer.ToString();
|
|
|
|
|
if (!string.IsNullOrEmpty(description))
|
|
|
|
|
{
|
|
|
|
|
program.Description = description;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-11-29 09:55:25 +08:00
|
|
|
|
var info = FileVersionInfo.GetVersionInfo(target);
|
|
|
|
|
if (!string.IsNullOrEmpty(info.FileDescription))
|
|
|
|
|
{
|
|
|
|
|
program.Description = info.FileDescription;
|
|
|
|
|
}
|
2016-08-20 06:05:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
}
|
|
|
|
|
return program;
|
|
|
|
|
}
|
2016-11-29 09:55:25 +08:00
|
|
|
|
catch (COMException e)
|
|
|
|
|
{
|
|
|
|
|
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|Win32.LnkProgram|COMException when parsing shortcut <{path}> with HResult <{e.HResult}>", e);
|
2016-11-29 09:55:25 +08:00
|
|
|
|
program.Valid = false;
|
|
|
|
|
return program;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2016-08-21 00:08:16 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|Win32.LnkProgram|Exception when parsing shortcut <{path}>", e);
|
2016-11-29 09:55:25 +08:00
|
|
|
|
program.Valid = false;
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return program;
|
2016-08-20 06:05:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
|
private static Win32 ExeProgram(string path)
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
var program = Win32Program(path);
|
2016-08-21 01:18:41 +08:00
|
|
|
|
var info = FileVersionInfo.GetVersionInfo(path);
|
|
|
|
|
if (!string.IsNullOrEmpty(info.FileDescription))
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 01:50:14 +08:00
|
|
|
|
program.Description = info.FileDescription;
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return program;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
|
|
|
|
|
{
|
2017-03-08 09:44:04 +08:00
|
|
|
|
if (!Directory.Exists(directory))
|
|
|
|
|
return new string[] { };
|
|
|
|
|
|
|
|
|
|
var ds = Directory.GetDirectories(directory);
|
|
|
|
|
|
|
|
|
|
var paths = ds.SelectMany(d =>
|
2016-08-21 00:08:16 +08:00
|
|
|
|
{
|
2017-03-02 07:21:34 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-08 09:44:04 +08:00
|
|
|
|
var paths_for_suffixes = suffixes.SelectMany(s =>
|
|
|
|
|
{
|
|
|
|
|
var pattern = $"*.{s}";
|
|
|
|
|
var ps = Directory.EnumerateFiles(d, pattern, SearchOption.AllDirectories);
|
|
|
|
|
return ps;
|
|
|
|
|
});
|
|
|
|
|
return paths_for_suffixes;
|
2017-03-02 07:21:34 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
|
|
|
{
|
2017-03-08 09:44:04 +08:00
|
|
|
|
Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e);
|
|
|
|
|
return new List<string>();
|
2017-03-02 07:21:34 +08:00
|
|
|
|
}
|
2017-03-08 09:44:04 +08:00
|
|
|
|
});
|
|
|
|
|
return paths;
|
2016-08-21 00:08:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Extension(string path)
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(path)?.ToLower();
|
|
|
|
|
if (!string.IsNullOrEmpty(extension))
|
2016-08-20 08:17:28 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return extension.Substring(1);
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return string.Empty;
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
}
|
2016-08-20 08:17:28 +08:00
|
|
|
|
|
2016-08-21 00:08:16 +08:00
|
|
|
|
private static ParallelQuery<Win32> UnregisteredPrograms(List<Settings.ProgramSource> sources, string[] suffixes)
|
|
|
|
|
{
|
|
|
|
|
var paths = sources.Where(s => Directory.Exists(s.Location))
|
|
|
|
|
.SelectMany(s => ProgramPaths(s.Location, suffixes))
|
|
|
|
|
.ToArray();
|
|
|
|
|
var programs1 = paths.AsParallel().Where(p => Extension(p) == ExeExtension).Select(ExeProgram);
|
|
|
|
|
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(ExeProgram);
|
|
|
|
|
var programs3 = from p in paths.AsParallel()
|
|
|
|
|
let e = Extension(p)
|
|
|
|
|
where e != ShortcutExtension && e != ExeExtension
|
|
|
|
|
select Win32Program(p);
|
|
|
|
|
return programs1.Concat(programs2).Concat(programs3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
|
|
|
|
|
{
|
|
|
|
|
var directory1 = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
|
|
|
|
|
var directory2 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms);
|
|
|
|
|
var paths1 = ProgramPaths(directory1, suffixes);
|
|
|
|
|
var paths2 = ProgramPaths(directory2, suffixes);
|
|
|
|
|
var paths = paths1.Concat(paths2).ToArray();
|
|
|
|
|
var programs1 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
|
|
|
|
|
var programs2 = paths.AsParallel().Where(p => Extension(p) == ApplicationReferenceExtension).Select(Win32Program);
|
2016-09-06 01:08:30 +08:00
|
|
|
|
var programs = programs1.Concat(programs2).Where(p => p.Valid);
|
|
|
|
|
return programs;
|
2016-08-21 00:08:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ParallelQuery<Win32> AppPathsPrograms(string[] suffixes)
|
|
|
|
|
{
|
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
|
|
|
|
|
const string appPaths = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
|
|
|
|
|
var programs = new List<Win32>();
|
|
|
|
|
using (var root = Registry.LocalMachine.OpenSubKey(appPaths))
|
2016-08-20 08:17:28 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
if (root != null)
|
2016-08-20 06:05:59 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
using (var root = Registry.CurrentUser.OpenSubKey(appPaths))
|
|
|
|
|
{
|
|
|
|
|
if (root != null)
|
|
|
|
|
{
|
|
|
|
|
programs.AddRange(ProgramsFromRegistryKey(root));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var filtered = programs.AsParallel().Where(p => suffixes.Contains(Extension(p.ExecutableName)));
|
|
|
|
|
return filtered;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Win32> ProgramsFromRegistryKey(RegistryKey root)
|
|
|
|
|
{
|
|
|
|
|
var programs = root.GetSubKeyNames()
|
|
|
|
|
.Select(subkey => ProgramFromRegistrySubkey(root, subkey))
|
2016-08-21 01:50:14 +08:00
|
|
|
|
.Where(p => !string.IsNullOrEmpty(p.Name));
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return programs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Win32 ProgramFromRegistrySubkey(RegistryKey root, string subkey)
|
|
|
|
|
{
|
|
|
|
|
using (var key = root.OpenSubKey(subkey))
|
|
|
|
|
{
|
|
|
|
|
if (key != null)
|
|
|
|
|
{
|
|
|
|
|
var defaultValue = string.Empty;
|
|
|
|
|
var path = key.GetValue(defaultValue) as string;
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2016-08-20 08:17:28 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
// fix path like this: ""\"C:\\folder\\executable.exe\""
|
2017-01-24 08:24:20 +08:00
|
|
|
|
path = path.Trim('"', ' ');
|
2016-08-21 00:08:16 +08:00
|
|
|
|
path = Environment.ExpandEnvironmentVariables(path);
|
|
|
|
|
|
2016-08-21 01:19:49 +08:00
|
|
|
|
if (File.Exists(path))
|
2016-08-21 00:08:16 +08:00
|
|
|
|
{
|
|
|
|
|
var entry = Win32Program(path);
|
|
|
|
|
entry.ExecutableName = subkey;
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
2017-03-02 07:21:34 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Win32();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Win32();
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-20 06:05:59 +08:00
|
|
|
|
}
|
2017-03-02 07:21:34 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new Win32();
|
|
|
|
|
}
|
2016-08-20 08:17:28 +08:00
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
//private static Win32 ScoreFilter(Win32 p)
|
|
|
|
|
//{
|
|
|
|
|
// var start = new[] { "启动", "start" };
|
|
|
|
|
// var doc = new[] { "帮助", "help", "文档", "documentation" };
|
|
|
|
|
// var uninstall = new[] { "卸载", "uninstall" };
|
2016-08-21 00:08:16 +08:00
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
// var contained = start.Any(s => p.Name.ToLower().Contains(s));
|
|
|
|
|
// if (contained)
|
|
|
|
|
// {
|
|
|
|
|
// p.Score += 10;
|
|
|
|
|
// }
|
|
|
|
|
// contained = doc.Any(d => p.Name.ToLower().Contains(d));
|
|
|
|
|
// if (contained)
|
|
|
|
|
// {
|
|
|
|
|
// p.Score -= 10;
|
|
|
|
|
// }
|
|
|
|
|
// contained = uninstall.Any(u => p.Name.ToLower().Contains(u));
|
|
|
|
|
// if (contained)
|
|
|
|
|
// {
|
|
|
|
|
// p.Score -= 20;
|
|
|
|
|
// }
|
2016-08-21 00:08:16 +08:00
|
|
|
|
|
2016-08-22 09:21:22 +08:00
|
|
|
|
// return p;
|
|
|
|
|
//}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
|
|
|
|
|
public static Win32[] All(Settings settings)
|
|
|
|
|
{
|
|
|
|
|
ParallelQuery<Win32> programs = new List<Win32>().AsParallel();
|
|
|
|
|
if (settings.EnableRegistrySource)
|
|
|
|
|
{
|
|
|
|
|
var appPaths = AppPathsPrograms(settings.ProgramSuffixes);
|
|
|
|
|
programs = programs.Concat(appPaths);
|
|
|
|
|
}
|
|
|
|
|
if (settings.EnableStartMenuSource)
|
2016-08-20 08:17:28 +08:00
|
|
|
|
{
|
2016-08-21 00:08:16 +08:00
|
|
|
|
var startMenu = StartMenuPrograms(settings.ProgramSuffixes);
|
|
|
|
|
programs = programs.Concat(startMenu);
|
2016-08-20 06:05:59 +08:00
|
|
|
|
}
|
2016-08-21 00:08:16 +08:00
|
|
|
|
var unregistered = UnregisteredPrograms(settings.ProgramSources, settings.ProgramSuffixes);
|
2016-08-22 09:21:22 +08:00
|
|
|
|
programs = programs.Concat(unregistered);
|
|
|
|
|
//.Select(ScoreFilter);
|
2016-08-21 00:08:16 +08:00
|
|
|
|
return programs.ToArray();
|
2016-08-20 06:05:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|