forked from chocolatey/ChocolateyGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#494) Watch Chocolatey config file for changes
When Chocolatey CLI makes changes to the configuration file while Chocolatey GUI is running, Chocolatey GUI should refresh the sources so that they're accurate. This adds a FileSystemWatcher to monitor the configuration file and update the GUI if there's updates to the file.
- Loading branch information
Showing
6 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright company="Chocolatey" file="ConfigFileWatcher.cs"> | ||
// Copyright 2017 - Present Chocolatey Software, LLC | ||
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
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<ConfigFileSettings>(_configFile).GetHashCode(); | ||
} | ||
|
||
private void ConfigFileChanged(object sender, FileSystemEventArgs e) | ||
{ | ||
var currentSettingsHash = _xmlService.deserialize<ConfigFileSettings>(_configFile).GetHashCode(); | ||
if (currentSettingsHash != _lastKnownConfigFileHash) | ||
{ | ||
_eventAggregator.PublishOnUIThread(new SourcesUpdatedMessage()); | ||
_lastKnownConfigFileHash = currentSettingsHash; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright company="Chocolatey" file="IConfigFileWatcher.cs"> | ||
// Copyright 2017 - Present Chocolatey Software, LLC | ||
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace ChocolateyGui.Common.Services | ||
{ | ||
public interface IConfigFileWatcher | ||
{ | ||
} | ||
} |