Skip to content

Commit

Permalink
Deduping results for program plugin (#2375)
Browse files Browse the repository at this point in the history
* Removing description from title

* adjusting subtitle

* removing accidently paste

* removing desc for uwp apps

* Getting dups removed from list if LNK exists

* adjusting subtitle

* removing accidently paste

* Getting dups removed from list if LNK exists

* changed to normal forloop
  • Loading branch information
crutkas authored Apr 24, 2020
1 parent 8a2fd5b commit 08a7394
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public Result Result(string query, IPublicAPI api)

var result = new Result
{
SubTitle = "UWP Application",
SubTitle = "UWP application",
Icon = Logo,
Score = score,
ContextData = this,
Expand Down
32 changes: 24 additions & 8 deletions src/modules/launcher/Plugins/Wox.Plugin.Program/Programs/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Result Result(string query, IPublicAPI api)

var result = new Result
{
SubTitle = "Win32 Application",
SubTitle = "Win32 application",
IcoPath = IcoPath,
Score = score,
ContextData = this,
Expand Down Expand Up @@ -148,8 +148,9 @@ private static Win32 Win32Program(string path)
var p = new Win32
{
Name = Path.GetFileNameWithoutExtension(path),
ExecutableName = Path.GetFileName(path),
IcoPath = path,
FullPath = path,
FullPath = path.ToLower(),
UniqueIdentifier = path,
ParentDirectory = Directory.GetParent(path).FullName,
Description = string.Empty,
Expand Down Expand Up @@ -191,7 +192,8 @@ private static Win32 LnkProgram(string path)
if (extension == ExeExtension && File.Exists(target))
{
program.LnkResolvedPath = program.FullPath;
program.FullPath = target;
program.FullPath = Path.GetFullPath(target).ToLower();
program.ExecutableName = Path.GetFileName(target);

buffer = new StringBuilder(MAX_PATH);
link.GetDescription(buffer, MAX_PATH);
Expand Down Expand Up @@ -239,10 +241,12 @@ private static Win32 ExeProgram(string path)
{
var program = Win32Program(path);
var info = FileVersionInfo.GetVersionInfo(path);

if (!string.IsNullOrEmpty(info.FileDescription))
{
program.Description = info.FileDescription;
}

return program;
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
Expand All @@ -257,10 +261,14 @@ private static Win32 ExeProgram(string path)
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
{
if (!Directory.Exists(directory))
{
return new string[] { };
}

var files = new List<string>();
var folderQueue = new Queue<string>();
folderQueue.Enqueue(directory);

do
{
var currentDirectory = folderQueue.Dequeue();
Expand Down Expand Up @@ -298,12 +306,14 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
$"|Permission denied when trying to load programs from {currentDirectory}", e);
}
} while (folderQueue.Any());

return files;
}

private static string Extension(string path)
{
var extension = Path.GetExtension(path)?.ToLower();

if (!string.IsNullOrEmpty(extension))
{
return extension.Substring(1);
Expand Down Expand Up @@ -353,11 +363,8 @@ private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)

var programs1 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
var programs2 = paths.AsParallel().Where(p => Extension(p) == ApplicationReferenceExtension).Select(Win32Program);
var allValidPrograms = programs1.Concat(programs2).Where(p => p.Valid);
//var programsWithLnk = allValidPrograms.Where(x => !string.IsNullOrEmpty(x.LnkResolvedPath));


return allValidPrograms;
return programs1.Concat(programs2).Where(p => p.Valid);
}

private static ParallelQuery<Win32> AppPathsPrograms(string[] suffixes)
Expand Down Expand Up @@ -450,6 +457,7 @@ public static Win32[] All(Settings settings)

var unregistered = UnregisteredPrograms(settings.ProgramSources, settings.ProgramSuffixes);
programs = programs.Concat(unregistered);

if (settings.EnableRegistrySource)
{
var appPaths = AppPathsPrograms(settings.ProgramSuffixes);
Expand All @@ -462,7 +470,15 @@ public static Win32[] All(Settings settings)
programs = programs.Concat(startMenu);
}

return programs.ToArray();
var programsWithoutLnk = programs.Where(x => string.IsNullOrEmpty(x.LnkResolvedPath));
var programsAsList = programs.ToList();

foreach(var app in programsWithoutLnk)
{
programsAsList.RemoveAll(x => (x.FullPath == app.FullPath) && string.IsNullOrEmpty(x.LnkResolvedPath));
}

return programsAsList.ToArray();
}
#if DEBUG //This is to make developer aware of any unhandled exception and add in handling.
catch (Exception e)
Expand Down

0 comments on commit 08a7394

Please sign in to comment.