mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960)
* Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
This commit is contained in:
@@ -11,7 +11,6 @@ using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin;
|
||||
using Microsoft.Plugin.Program.Views;
|
||||
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
using Windows.ApplicationModel;
|
||||
using Microsoft.Plugin.Program.Storage;
|
||||
@@ -22,36 +21,39 @@ namespace Microsoft.Plugin.Program
|
||||
public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IReloadable, IDisposable
|
||||
{
|
||||
private static readonly object IndexLock = new object();
|
||||
internal static Programs.Win32[] _win32s { get; set; }
|
||||
internal static Settings _settings { get; set; }
|
||||
|
||||
private static bool IsStartupIndexProgramsRequired => _settings.LastIndexTime.AddDays(3) < DateTime.Today;
|
||||
|
||||
private static PluginInitContext _context;
|
||||
|
||||
private static BinaryStorage<Programs.Win32[]> _win32Storage;
|
||||
private readonly PluginJsonStorage<Settings> _settingsStorage;
|
||||
private bool _disposed = false;
|
||||
private PackageRepository _packageRepository = new PackageRepository(new PackageCatalogWrapper(), new BinaryStorage<IList<UWP.Application>>("UWP"));
|
||||
private static Win32ProgramFileSystemWatchers _win32ProgramRepositoryHelper;
|
||||
private static Win32ProgramRepository _win32ProgramRepository;
|
||||
|
||||
public Main()
|
||||
{
|
||||
_settingsStorage = new PluginJsonStorage<Settings>();
|
||||
_settings = _settingsStorage.Load();
|
||||
// This helper class initializes the file system watchers based on the locations to watch
|
||||
_win32ProgramRepositoryHelper = new Win32ProgramFileSystemWatchers();
|
||||
|
||||
// Initialize the Win32ProgramRepository with the settings object
|
||||
_win32ProgramRepository = new Win32ProgramRepository(_win32ProgramRepositoryHelper._fileSystemWatchers.Cast<IFileSystemWatcherWrapper>().ToList(), new BinaryStorage<IList<Programs.Win32>>("Win32"), _settings, _win32ProgramRepositoryHelper._pathsToWatch);
|
||||
|
||||
Stopwatch.Normal("|Microsoft.Plugin.Program.Main|Preload programs cost", () =>
|
||||
{
|
||||
_win32Storage = new BinaryStorage<Programs.Win32[]>("Win32");
|
||||
_win32s = _win32Storage.TryLoad(new Programs.Win32[] { });
|
||||
|
||||
_win32ProgramRepository.Load();
|
||||
_packageRepository.Load();
|
||||
});
|
||||
Log.Info($"|Microsoft.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
|
||||
Log.Info($"|Microsoft.Plugin.Program.Main|Number of preload win32 programs <{_win32ProgramRepository.Count()}>");
|
||||
|
||||
var a = Task.Run(() =>
|
||||
{
|
||||
if (IsStartupIndexProgramsRequired || !_win32s.Any())
|
||||
Stopwatch.Normal("|Microsoft.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
|
||||
if (IsStartupIndexProgramsRequired || !_win32ProgramRepository.Any())
|
||||
Stopwatch.Normal("|Microsoft.Plugin.Program.Main|Win32Program index cost", _win32ProgramRepository.IndexPrograms);
|
||||
});
|
||||
|
||||
var b = Task.Run(() =>
|
||||
@@ -64,26 +66,19 @@ namespace Microsoft.Plugin.Program
|
||||
Task.WaitAll(a, b);
|
||||
|
||||
_settings.LastIndexTime = DateTime.Today;
|
||||
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_settingsStorage.Save();
|
||||
_win32Storage.Save(_win32s);
|
||||
_win32ProgramRepository.Save();
|
||||
_packageRepository.Save();
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
Programs.Win32[] win32;
|
||||
|
||||
lock (IndexLock)
|
||||
{
|
||||
// just take the reference inside the lock to eliminate query time issues.
|
||||
win32 = _win32s;
|
||||
}
|
||||
|
||||
var results1 = win32.AsParallel()
|
||||
var results1 = _win32ProgramRepository.AsParallel()
|
||||
.Where(p => p.Enabled)
|
||||
.Select(p => p.Result(query.Search, _context.API));
|
||||
|
||||
@@ -115,20 +110,9 @@ namespace Microsoft.Plugin.Program
|
||||
}
|
||||
}
|
||||
|
||||
public static void IndexWin32Programs()
|
||||
{
|
||||
var win32S = Programs.Win32.All(_settings);
|
||||
lock (IndexLock)
|
||||
{
|
||||
_win32s = win32S;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void IndexPrograms()
|
||||
{
|
||||
var t1 = Task.Run(() => IndexWin32Programs());
|
||||
var t1 = Task.Run(() => _win32ProgramRepository.IndexPrograms());
|
||||
var t2 = Task.Run(() => _packageRepository.IndexPrograms());
|
||||
|
||||
Task.WaitAll(t1, t2);
|
||||
@@ -194,10 +178,10 @@ namespace Microsoft.Plugin.Program
|
||||
if (disposing)
|
||||
{
|
||||
_context.API.ThemeChanged -= OnThemeChanged;
|
||||
_win32ProgramRepositoryHelper.Dispose();
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -125,9 +125,11 @@
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>Microsoft.Plugin.Program.UnitTests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
public interface IShellLinkHelper
|
||||
{
|
||||
string RetrieveTargetPath(string path);
|
||||
string description { get; set; }
|
||||
string Arguments { get; set; }
|
||||
bool hasArguments { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,11 @@ using System.IO;
|
||||
using Accessibility;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Security.Policy;
|
||||
using Microsoft.Plugin.Program.Logger;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
class ShellLinkHelper
|
||||
public class ShellLinkHelper : IShellLinkHelper
|
||||
{
|
||||
[Flags()]
|
||||
public enum SLGP_FLAGS
|
||||
@@ -99,19 +100,29 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
}
|
||||
|
||||
// To initialize the app description
|
||||
public String description = String.Empty;
|
||||
// Contains the description of the app
|
||||
public string description { get; set; } = String.Empty;
|
||||
|
||||
// Sets to true if the program takes in arguments
|
||||
public String Arguments = String.Empty;
|
||||
public bool hasArguments = false;
|
||||
// Contains the arguments to the app
|
||||
public string Arguments { get; set; } = String.Empty;
|
||||
public bool hasArguments { get; set; } = false;
|
||||
|
||||
// Retrieve the target path using Shell Link
|
||||
public string retrieveTargetPath(string path)
|
||||
public string RetrieveTargetPath(string path)
|
||||
{
|
||||
var link = new ShellLink();
|
||||
const int STGM_READ = 0;
|
||||
((IPersistFile)link).Load(path, STGM_READ);
|
||||
|
||||
try
|
||||
{
|
||||
((IPersistFile)link).Load(path, STGM_READ);
|
||||
}
|
||||
catch(System.IO.FileNotFoundException ex)
|
||||
{
|
||||
ProgramLogger.LogException($"|Win32| ShellLinkHelper.retrieveTargetPath | {path} | Path could not be retrieved", ex);
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
var hwnd = new _RemotableHandle();
|
||||
((IShellLinkW)link).Resolve(ref hwnd, 0);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.Windows.Input;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.FileSystemHelper;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
@@ -36,6 +37,10 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
public string Arguments { get; set; } = String.Empty;
|
||||
public string Location => ParentDirectory;
|
||||
public uint AppType { get; set; }
|
||||
// Wrappers for File Operations
|
||||
public static IFileVersionInfoWrapper _fileVersionInfoWrapper = new FileVersionInfoWrapper();
|
||||
public static IFileWrapper _fileWrapper = new FileWrapper();
|
||||
public static IShellLinkHelper _helper = new ShellLinkHelper();
|
||||
|
||||
private const string ShortcutExtension = "lnk";
|
||||
private const string ApplicationReferenceExtension = "appref-ms";
|
||||
@@ -313,7 +318,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
// This function filters Internet Shortcut programs
|
||||
private static Win32 InternetShortcutProgram(string path)
|
||||
{
|
||||
string[] lines = System.IO.File.ReadAllLines(path);
|
||||
string[] lines = _fileWrapper.ReadAllLines(path);
|
||||
string appName = string.Empty;
|
||||
string iconPath = string.Empty;
|
||||
string urlPath = string.Empty;
|
||||
@@ -382,8 +387,8 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
const int MAX_PATH = 260;
|
||||
StringBuilder buffer = new StringBuilder(MAX_PATH);
|
||||
ShellLinkHelper _helper = new ShellLinkHelper();
|
||||
string target = _helper.retrieveTargetPath(path);
|
||||
|
||||
string target = _helper.RetrieveTargetPath(path);
|
||||
|
||||
if (!string.IsNullOrEmpty(target))
|
||||
{
|
||||
@@ -403,8 +408,8 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
}
|
||||
else
|
||||
{
|
||||
var info = FileVersionInfo.GetVersionInfo(target);
|
||||
if (!string.IsNullOrEmpty(info.FileDescription))
|
||||
var info = _fileVersionInfoWrapper.GetVersionInfo(target);
|
||||
if (!string.IsNullOrEmpty(info?.FileDescription))
|
||||
{
|
||||
program.Description = info.FileDescription;
|
||||
}
|
||||
@@ -439,9 +444,9 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
try
|
||||
{
|
||||
var program = Win32Program(path);
|
||||
var info = FileVersionInfo.GetVersionInfo(path);
|
||||
var info = _fileVersionInfoWrapper.GetVersionInfo(path);
|
||||
|
||||
if (!string.IsNullOrEmpty(info.FileDescription))
|
||||
if (!string.IsNullOrEmpty(info?.FileDescription))
|
||||
{
|
||||
program.Description = info.FileDescription;
|
||||
}
|
||||
@@ -457,6 +462,46 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
}
|
||||
}
|
||||
|
||||
// Function to get the Win32 application, given the path to the application
|
||||
public static Win32 GetAppFromPath(string path)
|
||||
{
|
||||
Win32 app = null;
|
||||
const string exeExtension = ".exe";
|
||||
const string lnkExtension = ".lnk";
|
||||
const string urlExtenion = ".url";
|
||||
const string apprefExtension = ".appref-ms";
|
||||
|
||||
string extension = Path.GetExtension(path);
|
||||
|
||||
if(extension.Equals(exeExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = ExeProgram(path);
|
||||
}
|
||||
else if(extension.Equals(lnkExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = LnkProgram(path);
|
||||
}
|
||||
else if(extension.Equals(apprefExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = Win32Program(path);
|
||||
}
|
||||
else if(extension.Equals(urlExtenion, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = InternetShortcutProgram(path);
|
||||
}
|
||||
|
||||
// if the app is valid, only then return the application, else return null
|
||||
if(app?.Valid ?? false)
|
||||
{
|
||||
return app;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes, bool recursiveSearch = true)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
@@ -609,8 +654,11 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
var programs1 = paths.AsParallel().Where(p => Extension(p).Equals(ShortcutExtension, StringComparison.OrdinalIgnoreCase)).Select(LnkProgram);
|
||||
var programs2 = paths.AsParallel().Where(p => Extension(p).Equals(ApplicationReferenceExtension, StringComparison.OrdinalIgnoreCase)).Select(Win32Program);
|
||||
var programs3 = paths.AsParallel().Where(p => Extension(p).Equals(InternetShortcutExtension, StringComparison.OrdinalIgnoreCase)).Select(InternetShortcutProgram);
|
||||
var programs4 = paths.AsParallel().Where(p => Extension(p).Equals(ExeExtension, StringComparison.OrdinalIgnoreCase)).Select(ExeProgram);
|
||||
|
||||
return programs1.Concat(programs2).Where(p => p.Valid).Concat(programs3).Where(p => p.Valid);
|
||||
return programs1.Concat(programs2).Where(p => p.Valid)
|
||||
.Concat(programs3).Where(p => p.Valid)
|
||||
.Concat(programs4).Where(p => p.Valid);
|
||||
}
|
||||
|
||||
private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
|
||||
@@ -712,6 +760,19 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
return entry;
|
||||
}
|
||||
|
||||
// Overriding the object.GetHashCode() function to aid in removing duplicates while adding and removing apps from the concurrent dictionary storage
|
||||
public override int GetHashCode()
|
||||
{
|
||||
removeDuplicatesComparer _removeDuplicatesHelper = new removeDuplicatesComparer();
|
||||
return _removeDuplicatesHelper.GetHashCode(this);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
removeDuplicatesComparer _removeDuplicatesHelper = new removeDuplicatesComparer();
|
||||
return obj is Win32 win && _removeDuplicatesHelper.Equals(this, win);
|
||||
}
|
||||
|
||||
public class removeDuplicatesComparer : IEqualityComparer<Win32>
|
||||
{
|
||||
public bool Equals(Win32 app1, Win32 app2)
|
||||
@@ -802,6 +863,6 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
return new Win32[0];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Wox.Infrastructure.Storage;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Storage
|
||||
{
|
||||
internal class Win32ProgramFileSystemWatchers : IDisposable
|
||||
{
|
||||
|
||||
public readonly string[] _pathsToWatch;
|
||||
public List<FileSystemWatcherWrapper> _fileSystemWatchers;
|
||||
private bool _disposed = false;
|
||||
|
||||
// This class contains the list of directories to watch and initializes the File System Watchers
|
||||
public Win32ProgramFileSystemWatchers()
|
||||
{
|
||||
_pathsToWatch = GetPathsToWatch();
|
||||
SetFileSystemWatchers();
|
||||
}
|
||||
|
||||
// Returns an array of paths to be watched
|
||||
private string[] GetPathsToWatch()
|
||||
{
|
||||
string[] paths = new string[]
|
||||
{
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.Programs),
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms),
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
|
||||
};
|
||||
return paths;
|
||||
}
|
||||
|
||||
// Initializes the FileSystemWatchers
|
||||
private void SetFileSystemWatchers()
|
||||
{
|
||||
_fileSystemWatchers = new List<FileSystemWatcherWrapper>();
|
||||
for (int index = 0; index < _pathsToWatch.Length; index++)
|
||||
{
|
||||
_fileSystemWatchers.Add(new FileSystemWatcherWrapper());
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
for(int index = 0; index < _pathsToWatch.Length; index++)
|
||||
{
|
||||
_fileSystemWatchers[index].Dispose();
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.Plugin.Program.Programs;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Storage
|
||||
{
|
||||
using Win32 = Programs.Win32;
|
||||
internal class Win32ProgramRepository : ListRepository<Programs.Win32>, IProgramRepository
|
||||
{
|
||||
private IStorage<IList<Programs.Win32>> _storage;
|
||||
private Settings _settings;
|
||||
private IList<IFileSystemWatcherWrapper> _fileSystemWatcherHelpers;
|
||||
private string[] _pathsToWatch;
|
||||
private int _numberOfPathsToWatch;
|
||||
private Collection<string> extensionsToWatch = new Collection<string>{ "*.exe", "*.lnk", "*.appref-ms", "*.url" };
|
||||
private readonly string lnkExtension = ".lnk";
|
||||
private readonly string urlExtension = ".url";
|
||||
|
||||
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, IStorage<IList<Win32>> storage, Settings settings, string[] pathsToWatch)
|
||||
{
|
||||
this._fileSystemWatcherHelpers = fileSystemWatcherHelpers;
|
||||
this._storage = storage ?? throw new ArgumentNullException("storage", "Win32ProgramRepository requires an initialized storage interface");
|
||||
this._settings = settings ?? throw new ArgumentNullException("settings", "Win32ProgramRepository requires an initialized settings object");
|
||||
this._pathsToWatch = pathsToWatch;
|
||||
this._numberOfPathsToWatch = pathsToWatch.Count();
|
||||
InitializeFileSystemWatchers();
|
||||
}
|
||||
|
||||
private void InitializeFileSystemWatchers()
|
||||
{
|
||||
for(int index = 0; index < _numberOfPathsToWatch; index++)
|
||||
{
|
||||
// To set the paths to monitor
|
||||
_fileSystemWatcherHelpers[index].Path = _pathsToWatch[index];
|
||||
|
||||
// to be notified when there is a change to a file
|
||||
_fileSystemWatcherHelpers[index].NotifyFilter = NotifyFilters.FileName;
|
||||
|
||||
// filtering the app types that we want to monitor
|
||||
_fileSystemWatcherHelpers[index].Filters = extensionsToWatch;
|
||||
|
||||
// Registering the event handlers
|
||||
_fileSystemWatcherHelpers[index].Created += OnAppCreated;
|
||||
_fileSystemWatcherHelpers[index].Deleted += OnAppDeleted;
|
||||
_fileSystemWatcherHelpers[index].Renamed += OnAppRenamed;
|
||||
|
||||
// Enable the file system watcher
|
||||
_fileSystemWatcherHelpers[index].EnableRaisingEvents = true;
|
||||
|
||||
// Enable it to search in sub folders as well
|
||||
_fileSystemWatcherHelpers[index].IncludeSubdirectories = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAppRenamed(object sender, RenamedEventArgs e)
|
||||
{
|
||||
string oldPath = e.OldFullPath;
|
||||
string newPath = e.FullPath;
|
||||
|
||||
string extension = Path.GetExtension(newPath);
|
||||
Programs.Win32 newApp = Programs.Win32.GetAppFromPath(newPath);
|
||||
Programs.Win32 oldApp = null;
|
||||
|
||||
// Once the shortcut application is renamed, the old app does not exist and therefore when we try to get the FullPath we get the lnk path instead of the exe path
|
||||
// This changes the hashCode() of the old application.
|
||||
// Therefore, instead of retrieving the old app using the GetAppFromPath(), we construct the application ourself
|
||||
// This situation is not encountered for other application types because the fullPath is the path itself, instead of being computed by using the path to the app.
|
||||
try
|
||||
{
|
||||
if (extension.Equals(lnkExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
oldApp = new Win32() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = newApp.ExecutableName, FullPath = newApp.FullPath };
|
||||
}
|
||||
else if(extension.Equals(urlExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
oldApp = new Win32() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath };
|
||||
}
|
||||
else
|
||||
{
|
||||
oldApp = Win32.GetAppFromPath(oldPath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Info($"|Win32ProgramRepository|OnAppRenamed-{extension}Program|{oldPath}|Unable to create program from {oldPath}| {ex.Message}");
|
||||
}
|
||||
|
||||
|
||||
// To remove the old app which has been renamed and to add the new application.
|
||||
if (oldApp != null)
|
||||
{
|
||||
Remove(oldApp);
|
||||
}
|
||||
|
||||
if (newApp != null)
|
||||
{
|
||||
Add(newApp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAppDeleted(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath;
|
||||
string extension = Path.GetExtension(path);
|
||||
Programs.Win32 app = null;
|
||||
|
||||
try
|
||||
{
|
||||
// To mitigate the issue of not having a FullPath for a shortcut app, we iterate through the items and find the app with the same hashcode.
|
||||
if (extension.Equals(lnkExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = GetAppWithSameLnkResolvedPath(path);
|
||||
}
|
||||
else if (extension.Equals(urlExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
app = GetAppWithSameNameAndExecutable(Path.GetFileNameWithoutExtension(path), Path.GetFileName(path));
|
||||
}
|
||||
else
|
||||
{
|
||||
app = Programs.Win32.GetAppFromPath(path);
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Log.Info($"|Win32ProgramRepository|OnAppDeleted-{extension}Program|{path}|Unable to create program from {path}| {ex.Message}");
|
||||
}
|
||||
|
||||
if (app != null)
|
||||
{
|
||||
Remove(app);
|
||||
}
|
||||
}
|
||||
|
||||
// When a URL application is deleted, we can no longer get the HashCode directly from the path because the FullPath a Url app is the URL obtained from reading the file
|
||||
private Win32 GetAppWithSameNameAndExecutable(string name, string executableName)
|
||||
{
|
||||
foreach (Win32 app in Items)
|
||||
{
|
||||
if (name.Equals(app.Name, StringComparison.OrdinalIgnoreCase) && executableName.Equals(app.ExecutableName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return app;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// To mitigate the issue faced (as stated above) when a shortcut application is renamed, the Exe FullPath and executable name must be obtained.
|
||||
// Unlike the rename event args, since we do not have a newPath, we iterate through all the programs and find the one with the same LnkResolved path.
|
||||
private Programs.Win32 GetAppWithSameLnkResolvedPath(string lnkResolvedPath)
|
||||
{
|
||||
foreach(Programs.Win32 app in Items)
|
||||
{
|
||||
if (lnkResolvedPath.ToLower().Equals(app.LnkResolvedPath))
|
||||
{
|
||||
return app;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnAppCreated(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
string path = e.FullPath;
|
||||
Programs.Win32 app = Programs.Win32.GetAppFromPath(path);
|
||||
if (app != null)
|
||||
{
|
||||
Add(app);
|
||||
}
|
||||
}
|
||||
|
||||
public void IndexPrograms()
|
||||
{
|
||||
var applications = Programs.Win32.All(_settings);
|
||||
Set(applications);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_storage.Save(Items);
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
var items = _storage.TryLoad(new Programs.Win32[] { });
|
||||
Set(items);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace Microsoft.Plugin.Program.Views.Commands
|
||||
|
||||
internal static void LoadAllApplications(this List<ProgramSource> list)
|
||||
{
|
||||
Main._win32s
|
||||
/*Main._win32s
|
||||
.Where(t1 => !ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
|
||||
.ToList()
|
||||
.ForEach(t1 => ProgramSetting.ProgramSettingDisplayList
|
||||
@@ -60,7 +60,7 @@ namespace Microsoft.Plugin.Program.Views.Commands
|
||||
UniqueIdentifier = t1.UniqueIdentifier,
|
||||
Enabled = t1.Enabled
|
||||
}
|
||||
));
|
||||
));*/
|
||||
}
|
||||
|
||||
internal static void SetProgramSourcesStatus(this List<ProgramSource> list, List<ProgramSource> selectedProgramSourcesToDisable, bool status)
|
||||
@@ -70,10 +70,10 @@ namespace Microsoft.Plugin.Program.Views.Commands
|
||||
.ToList()
|
||||
.ForEach(t1 => t1.Enabled = status);
|
||||
|
||||
Main._win32s
|
||||
/*Main._win32s
|
||||
.Where(t1 => selectedProgramSourcesToDisable.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier && t1.Enabled != status))
|
||||
.ToList()
|
||||
.ForEach(t1 => t1.Enabled = status);
|
||||
.ForEach(t1 => t1.Enabled = status);*/
|
||||
}
|
||||
|
||||
internal static void StoreDisabledInSettings(this List<ProgramSource> list)
|
||||
@@ -114,8 +114,7 @@ namespace Microsoft.Plugin.Program.Views.Commands
|
||||
|
||||
internal static bool IsReindexRequired(this List<ProgramSource> selectedItems)
|
||||
{
|
||||
if (selectedItems.Where(t1 => t1.Enabled).Count() > 0
|
||||
&& selectedItems.Where(t1 => t1.Enabled && !Main._win32s.Any(x => t1.UniqueIdentifier == x.UniqueIdentifier)).Count() > 0)
|
||||
if (selectedItems.Where(t1 => t1.Enabled).Count() > 0)
|
||||
return true;
|
||||
|
||||
// ProgramSources holds list of user added directories,
|
||||
|
||||
Reference in New Issue
Block a user