From b71518a803030b0b012d70ce7b7734d5bee769bf Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 24 Aug 2013 23:05:49 -0700 Subject: [PATCH] Old config file values are removed on app start --- .../Configuration/ConfigFileProvider.cs | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/NzbDrone.Core/Configuration/ConfigFileProvider.cs index f588e2272..fa694700d 100644 --- a/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -8,10 +8,11 @@ using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Core.Configuration.Events; +using NzbDrone.Core.Lifecycle; namespace NzbDrone.Core.Configuration { - public interface IConfigFileProvider + public interface IConfigFileProvider : IHandleAsync { Dictionary GetConfigDictionary(); void SaveConfigDictionary(Dictionary configValues); @@ -27,6 +28,8 @@ public interface IConfigFileProvider public class ConfigFileProvider : IConfigFileProvider { + private const string CONFIG_ELEMENT_NAME = "Config"; + private readonly IAppFolderInfo _appFolderInfo; private readonly IMessageAggregator _messageAggregator; private readonly ICached _cache; @@ -138,7 +141,7 @@ public string GetValue(string key, object defaultValue) EnsureDefaultConfigFile(); var xDoc = XDocument.Load(_configFile); - var config = xDoc.Descendants("Config").Single(); + var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); var parentContainer = config; @@ -160,7 +163,7 @@ public void SetValue(string key, object value) EnsureDefaultConfigFile(); var xDoc = XDocument.Load(_configFile); - var config = xDoc.Descendants("Config").Single(); + var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); var parentContainer = config; @@ -191,9 +194,37 @@ private void EnsureDefaultConfigFile() if (!File.Exists(_configFile)) { var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); - xDoc.Add(new XElement("Config")); + xDoc.Add(new XElement(CONFIG_ELEMENT_NAME)); xDoc.Save(_configFile); } } + + private void DeleteOldValues() + { + EnsureDefaultConfigFile(); + + var xDoc = XDocument.Load(_configFile); + var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); + + var type = GetType(); + var properties = type.GetProperties(); + + foreach (var configValue in config.Descendants().ToList()) + { + var name = configValue.Name.LocalName; + + if (!properties.Any(p => p.Name == name)) + { + config.Descendants(name).Remove(); + } + } + + xDoc.Save(_configFile); + } + + public void HandleAsync(ApplicationStartedEvent message) + { + DeleteOldValues(); + } } }