mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Remove unused catch and optimize code
This commit is contained in:
parent
61c1f2c620
commit
d35d7ae2d6
1 changed files with 54 additions and 65 deletions
|
|
@ -16,7 +16,6 @@ using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
|
||||||
|
|
||||||
namespace Flow.Launcher.Plugin.Program.Programs
|
namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
{
|
{
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Win32 : IProgram
|
public class Win32 : IProgram
|
||||||
{
|
{
|
||||||
|
|
@ -127,19 +126,19 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
Action = _ =>
|
Action = _ =>
|
||||||
{
|
{
|
||||||
var args = !string.IsNullOrWhiteSpace(Main._settings.CustomizedArgs)
|
var args = !string.IsNullOrWhiteSpace(Main._settings.CustomizedArgs)
|
||||||
? Main._settings.CustomizedArgs
|
? Main._settings.CustomizedArgs
|
||||||
.Replace("%s",$"\"{ParentDirectory}\"")
|
.Replace("%s", $"\"{ParentDirectory}\"")
|
||||||
.Replace("%f",$"\"{FullPath}\"")
|
.Replace("%f", $"\"{FullPath}\"")
|
||||||
: Main._settings.CustomizedExplorer==Settings.Explorer
|
: Main._settings.CustomizedExplorer == Settings.Explorer
|
||||||
? $"/select,\"{FullPath}\""
|
? $"/select,\"{FullPath}\""
|
||||||
: Settings.ExplorerArgs;
|
: Settings.ExplorerArgs;
|
||||||
|
|
||||||
Main.StartProcess(Process.Start,
|
Main.StartProcess(Process.Start,
|
||||||
new ProcessStartInfo(
|
new ProcessStartInfo(
|
||||||
!string.IsNullOrWhiteSpace(Main._settings.CustomizedExplorer)
|
!string.IsNullOrWhiteSpace(Main._settings.CustomizedExplorer)
|
||||||
? Main._settings.CustomizedExplorer
|
? Main._settings.CustomizedExplorer
|
||||||
: Settings.Explorer,
|
: Settings.Explorer,
|
||||||
args));
|
args));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
@ -150,7 +149,6 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return ExecutableName;
|
return ExecutableName;
|
||||||
|
|
@ -176,9 +174,9 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
||||||
{
|
{
|
||||||
ProgramLogger.LogException($"|Win32|Win32Program|{path}" +
|
ProgramLogger.LogException($"|Win32|Win32Program|{path}" +
|
||||||
$"|Permission denied when trying to load the program from {path}", e);
|
$"|Permission denied when trying to load the program from {path}", e);
|
||||||
|
|
||||||
return new Win32() { Valid = false, Enabled = false };
|
return new Win32() {Valid = false, Enabled = false};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,13 +214,15 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
catch (COMException e)
|
catch (COMException e)
|
||||||
{
|
{
|
||||||
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
|
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
|
||||||
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
|
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
|
||||||
"|Error caused likely due to trying to get the description of the program", e);
|
"|Error caused likely due to trying to get the description of the program",
|
||||||
|
e);
|
||||||
|
|
||||||
program.Valid = false;
|
program.Valid = false;
|
||||||
return program;
|
return program;
|
||||||
|
|
@ -252,37 +252,22 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
||||||
{
|
{
|
||||||
ProgramLogger.LogException($"|Win32|ExeProgram|{path}" +
|
ProgramLogger.LogException($"|Win32|ExeProgram|{path}" +
|
||||||
$"|Permission denied when trying to load the program from {path}", e);
|
$"|Permission denied when trying to load the program from {path}", e);
|
||||||
|
|
||||||
return new Win32() { Valid = false, Enabled = false };
|
return new Win32() {Valid = false, Enabled = false};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
|
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(directory))
|
if (!Directory.Exists(directory))
|
||||||
return new string[] { };
|
return Enumerable.Empty<string>();
|
||||||
try
|
|
||||||
{
|
|
||||||
var paths = Directory.EnumerateFiles(directory, "*", new EnumerationOptions
|
|
||||||
{
|
|
||||||
IgnoreInaccessible = true,
|
|
||||||
RecurseSubdirectories = true
|
|
||||||
})
|
|
||||||
.Where(x => suffixes.Contains(Extension(x)));
|
|
||||||
|
|
||||||
return paths;
|
return Directory.EnumerateFiles(directory, "*", new EnumerationOptions
|
||||||
}
|
|
||||||
catch (DirectoryNotFoundException e)
|
|
||||||
{
|
{
|
||||||
ProgramLogger.LogException($"Directory not found {directory}", e);
|
IgnoreInaccessible = true,
|
||||||
return new string[] { };
|
RecurseSubdirectories = true
|
||||||
}
|
}).Where(x => suffixes.Contains(Extension(x)));
|
||||||
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
|
||||||
{
|
|
||||||
ProgramLogger.LogException($"Permission denied {directory}", e);
|
|
||||||
return new string[] { };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string Extension(string path)
|
private static string Extension(string path)
|
||||||
|
|
@ -328,13 +313,13 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
var toFilter = paths1.Concat(paths2);
|
var toFilter = paths1.Concat(paths2);
|
||||||
|
|
||||||
var programs = toFilter
|
var programs = toFilter
|
||||||
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1))
|
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1))
|
||||||
.Distinct()
|
.Distinct()
|
||||||
.Select(x => Extension(x) switch
|
.Select(x => Extension(x) switch
|
||||||
{
|
{
|
||||||
ShortcutExtension => LnkProgram(x),
|
ShortcutExtension => LnkProgram(x),
|
||||||
_ => Win32Program(x)
|
_ => Win32Program(x)
|
||||||
}).Where(x => x.Valid);
|
}).Where(x => x.Valid);
|
||||||
return programs;
|
return programs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -351,6 +336,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
{
|
{
|
||||||
programs = programs.Concat(GetProgramsFromRegistry(rootMachine));
|
programs = programs.Concat(GetProgramsFromRegistry(rootMachine));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rootUser != null)
|
if (rootUser != null)
|
||||||
{
|
{
|
||||||
programs = programs.Concat(GetProgramsFromRegistry(rootUser));
|
programs = programs.Concat(GetProgramsFromRegistry(rootUser));
|
||||||
|
|
@ -360,7 +346,9 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
var disabledProgramsList = Main._settings.DisabledProgramSources;
|
var disabledProgramsList = Main._settings.DisabledProgramSources;
|
||||||
var toFilter = programs.Where(p => suffixes.Contains(Extension(p.ExecutableName)));
|
var toFilter = programs.Where(p => suffixes.Contains(Extension(p.ExecutableName)));
|
||||||
|
|
||||||
var filtered = toFilter.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier)).Select(t1 => t1);
|
var filtered = toFilter
|
||||||
|
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
|
||||||
|
.Select(t1 => t1);
|
||||||
|
|
||||||
return filtered.ToList();
|
return filtered.ToList();
|
||||||
}
|
}
|
||||||
|
|
@ -368,10 +356,10 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
private static IEnumerable<Win32> GetProgramsFromRegistry(RegistryKey root)
|
private static IEnumerable<Win32> GetProgramsFromRegistry(RegistryKey root)
|
||||||
{
|
{
|
||||||
return root
|
return root
|
||||||
.GetSubKeyNames()
|
.GetSubKeyNames()
|
||||||
.Select(x => GetProgramPathFromRegistrySubKeys(root, x))
|
.Select(x => GetProgramPathFromRegistrySubKeys(root, x))
|
||||||
.Distinct()
|
.Distinct()
|
||||||
.Select(x => GetProgramFromPath(x));
|
.Select(GetProgramFromPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetProgramPathFromRegistrySubKeys(RegistryKey root, string subkey)
|
private static string GetProgramPathFromRegistrySubKeys(RegistryKey root, string subkey)
|
||||||
|
|
@ -397,7 +385,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
|
||||||
{
|
{
|
||||||
ProgramLogger.LogException($"|Win32|GetProgramPathFromRegistrySubKeys|{path}" +
|
ProgramLogger.LogException($"|Win32|GetProgramPathFromRegistrySubKeys|{path}" +
|
||||||
$"|Permission denied when trying to load the program from {path}", e);
|
$"|Permission denied when trying to load the program from {path}", e);
|
||||||
|
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
@ -419,13 +407,13 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Win32ComparatorBasedonDescription : IEqualityComparer<Win32>
|
private class Win32ComparatorWithDescription : IEqualityComparer<Win32>
|
||||||
{
|
{
|
||||||
public readonly static Win32ComparatorBasedonDescription Default = new Win32ComparatorBasedonDescription();
|
public static readonly Win32ComparatorWithDescription Default = new Win32ComparatorWithDescription();
|
||||||
|
|
||||||
public bool Equals(Win32 x, Win32 y)
|
public bool Equals(Win32 x, Win32 y)
|
||||||
{
|
{
|
||||||
return x.Description == y.Description;
|
return x?.Description == y?.Description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetHashCode(Win32 obj)
|
public int GetHashCode(Win32 obj)
|
||||||
|
|
@ -435,16 +423,16 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Win32[] ProgramsHasher(IEnumerable<Win32> programs)
|
private static Win32[] ProgramsHasher(IEnumerable<Win32> programs)
|
||||||
=> programs.GroupBy(p => p.FullPath.ToLower())
|
{
|
||||||
.SelectMany(g =>
|
return programs.GroupBy(p => p.FullPath.ToLower())
|
||||||
{
|
.SelectMany(g =>
|
||||||
var tempList = g.ToList();
|
{
|
||||||
if (tempList.Count() > 1)
|
if (g.Count() > 1)
|
||||||
return g.Where(p => !string.IsNullOrEmpty(p.Description))
|
return g.Where(p => !string.IsNullOrEmpty(p.Description))
|
||||||
.Distinct(Win32ComparatorBasedonDescription.Default);
|
.Distinct(Win32ComparatorWithDescription.Default);
|
||||||
else
|
return g;
|
||||||
return g.Take(1);
|
}).ToArray();
|
||||||
}).ToArray();
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Win32[] All(Settings settings)
|
public static Win32[] All(Settings settings)
|
||||||
|
|
@ -467,6 +455,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
var startMenu = StartMenuPrograms(settings.ProgramSuffixes);
|
var startMenu = StartMenuPrograms(settings.ProgramSuffixes);
|
||||||
programs = programs.Concat(startMenu);
|
programs = programs.Concat(startMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ProgramsHasher(programs);
|
return ProgramsHasher(programs);
|
||||||
}
|
}
|
||||||
#if DEBUG //This is to make developer aware of any unhandled exception and add in handling.
|
#if DEBUG //This is to make developer aware of any unhandled exception and add in handling.
|
||||||
|
|
@ -486,4 +475,4 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue