diff --git a/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs b/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs index 86c62c5c6..d786f095b 100644 --- a/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs +++ b/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs @@ -121,6 +121,7 @@ protected override async void OnStartup(object sender, StartupEventArgs e) var packageService = Container.Resolve(); var features = await packageService.GetFeatures(); + var fileSystemWatcher = Container.Resolve(); var backgroundFeature = features.FirstOrDefault(feature => string.Equals(feature.Name, "useBackgroundService", StringComparison.OrdinalIgnoreCase)); var elevationProvider = Elevation.Instance; diff --git a/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj b/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj index 016ce723e..852f7ac20 100644 --- a/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj +++ b/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj @@ -1,4 +1,4 @@ - + @@ -229,6 +229,7 @@ + diff --git a/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs b/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs new file mode 100644 index 000000000..b34c20a39 --- /dev/null +++ b/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs @@ -0,0 +1,53 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright 2017 - Present Chocolatey Software, LLC +// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC +// +// -------------------------------------------------------------------------------------------------------------------- + +using System.IO; +using Caliburn.Micro; +using chocolatey.infrastructure.app.configuration; +using chocolatey.infrastructure.services; +using ChocolateyGui.Common.Models.Messages; +using ChocolateyGui.Common.Services; +using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem; + +namespace ChocolateyGui.Common.Windows.Services +{ + public class ConfigFileWatcher : IConfigFileWatcher + { + private readonly IFileSystem _fileSystem; + private readonly IEventAggregator _eventAggregator; + private readonly FileSystemWatcher _fileSystemWatcher; + private readonly IXmlService _xmlService; + private readonly string _configFile = chocolatey.infrastructure.app.ApplicationParameters.GlobalConfigFileLocation; + private int _lastKnownConfigFileHash; + + public ConfigFileWatcher(IFileSystem fileSystem, IEventAggregator eventAggregator, IXmlService xmlService) + { + _fileSystem = fileSystem; + _eventAggregator = eventAggregator; + _xmlService = xmlService; + _fileSystemWatcher = new FileSystemWatcher + { + Path = _fileSystem.get_directory_name(_configFile), + Filter = _fileSystem.get_file_name(_configFile), + NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName, + }; + _fileSystemWatcher.Changed += ConfigFileChanged; + _fileSystemWatcher.EnableRaisingEvents = true; + _lastKnownConfigFileHash = _xmlService.deserialize(_configFile).GetHashCode(); + } + + private void ConfigFileChanged(object sender, FileSystemEventArgs e) + { + var currentSettingsHash = _xmlService.deserialize(_configFile).GetHashCode(); + if (currentSettingsHash != _lastKnownConfigFileHash) + { + _eventAggregator.PublishOnUIThread(new SourcesUpdatedMessage()); + _lastKnownConfigFileHash = currentSettingsHash; + } + } + } +} \ No newline at end of file diff --git a/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs b/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs index e19bcf522..d80cca943 100644 --- a/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs +++ b/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs @@ -52,6 +52,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); // Register ViewModels builder.RegisterAssemblyTypes(viewModelAssembly) diff --git a/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj b/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj index 6630edf82..1968c7bce 100644 --- a/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj +++ b/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj @@ -1,4 +1,4 @@ - + @@ -185,6 +185,7 @@ + diff --git a/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs b/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs new file mode 100644 index 000000000..46a0eeda0 --- /dev/null +++ b/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs @@ -0,0 +1,13 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright 2017 - Present Chocolatey Software, LLC +// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ChocolateyGui.Common.Services +{ + public interface IConfigFileWatcher + { + } +} \ No newline at end of file