From f0f706b32cb2625f9fdc504a29afb800564ef3c2 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 30 Sep 2011 20:12:18 -0700 Subject: [PATCH] ConfigFile for NzbDrone.exe is now stored under App_Data for NzbDrone.Web. - This will be to provide the users a way to edit Port and set whether they want their default browser to open on startup, all form the WebUI (and not be overwritten on upgrades). --- App_Data/Config.xml | 5 ++ NzbDrone.Core.Test/App_Data/Config.xml | 5 ++ NzbDrone.Core.Test/ConfigFileProviderTest.cs | 84 +++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 5 ++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../Providers/Core/ConfigFileProvider.cs | 53 ++++++++++++ NzbDrone/Config.cs | 70 +++++++++++++++- NzbDrone/Program.cs | 3 +- NzbDrone/app.config | 3 - 9 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 App_Data/Config.xml create mode 100644 NzbDrone.Core.Test/App_Data/Config.xml create mode 100644 NzbDrone.Core.Test/ConfigFileProviderTest.cs create mode 100644 NzbDrone.Core/Providers/Core/ConfigFileProvider.cs diff --git a/App_Data/Config.xml b/App_Data/Config.xml new file mode 100644 index 000000000..8bb6fbd51 --- /dev/null +++ b/App_Data/Config.xml @@ -0,0 +1,5 @@ + + + 8989 + true + \ No newline at end of file diff --git a/NzbDrone.Core.Test/App_Data/Config.xml b/NzbDrone.Core.Test/App_Data/Config.xml new file mode 100644 index 000000000..c4328bdeb --- /dev/null +++ b/NzbDrone.Core.Test/App_Data/Config.xml @@ -0,0 +1,5 @@ + + + 8989 + true + \ No newline at end of file diff --git a/NzbDrone.Core.Test/ConfigFileProviderTest.cs b/NzbDrone.Core.Test/ConfigFileProviderTest.cs new file mode 100644 index 000000000..7c6496aba --- /dev/null +++ b/NzbDrone.Core.Test/ConfigFileProviderTest.cs @@ -0,0 +1,84 @@ +using AutoMoq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Providers.Core; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test +{ + [TestFixture] + // ReSharper disable InconsistentNaming + public class ConfigFileProviderTest : TestBase + { + [Test] + public void GetValue_Success() + { + const string key = "Port"; + const string value = "8989"; + + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().GetValue(key); + + //Assert + result.Should().Be(value); + } + + [Test] + public void GetInt_Success() + { + const string key = "Port"; + const int value = 8989; + + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().GetValueInt(key); + + //Assert + result.Should().Be(value); + } + + [Test] + public void GetBool_Success() + { + const string key = "LaunchBrowser"; + + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().GetValueBoolean(key); + + //Assert + result.Should().BeTrue(); + } + + [Test] + public void GetLaunchBrowser_Success() + { + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().LaunchBrowser; + + //Assert + result.Should().Be(true); + } + + [Test] + public void GetPort_Success() + { + const int value = 8989; + + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().Port; + + //Assert + result.Should().Be(value); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index a63ff4f5d..25cc2fc37 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -90,6 +90,7 @@ + @@ -153,6 +154,10 @@ + + Always + + Always diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 88f60d607..ea2d903c2 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -203,6 +203,7 @@ + diff --git a/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs b/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs new file mode 100644 index 000000000..a7daa4bc6 --- /dev/null +++ b/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Xml.Linq; + +namespace NzbDrone.Core.Providers.Core +{ + public class ConfigFileProvider + { + public string ConfigFile + { + get { return Path.Combine(CentralDispatch.AppPath, "App_Data", "Config.xml"); } + } + + public virtual int Port + { + get { return GetValueInt("Port"); } + } + + public virtual bool LaunchBrowser + { + get { return GetValueBoolean("LaunchBrowser"); } + } + + public virtual string GetValue(string key, string parent = null) + { + var xDoc = XDocument.Load(ConfigFile); + var config = xDoc.Descendants("Config").Single(); + + var parentContainer = config; + + if (parent != null) + parentContainer = config.Descendants(parent).Single(); + + var value = parentContainer.Descendants(key).Single().Value; + + return value; + } + + public virtual int GetValueInt(string key, string parent = null) + { + return Convert.ToInt32(GetValue(key, parent)); + } + + public virtual bool GetValueBoolean(string key, string parent = null) + { + return Convert.ToBoolean(GetValue(key, parent)); + } + } +} diff --git a/NzbDrone/Config.cs b/NzbDrone/Config.cs index 88cb7e566..71141f3cd 100644 --- a/NzbDrone/Config.cs +++ b/NzbDrone/Config.cs @@ -1,7 +1,9 @@ using System; using System.Configuration; using System.IO; +using System.Linq; using System.Reflection; +using System.Xml.Linq; using NLog; using NLog.Config; @@ -34,13 +36,77 @@ internal static string ProjectRoot internal static int Port { - get { return Convert.ToInt32(ConfigurationManager.AppSettings.Get("port")); } + get { return GetValueInt("Port"); } + } + + internal static bool LaunchBrowser + { + get { return GetValueBoolean("LaunchBrowser"); } + } + + internal static string AppDataDirectory + { + get { return Path.Combine(ProjectRoot, "NzbDrone.Web", "App_Data"); } + } + + internal static string ConfigFile + { + get { return Path.Combine(AppDataDirectory, "Config.xml"); } } internal static void ConfigureNlog() { LogManager.Configuration = new XmlLoggingConfiguration( - Path.Combine(ProjectRoot, "NZBDrone.Web\\log.config"), false); + Path.Combine(ProjectRoot, "NzbDrone.Web\\log.config"), false); + } + + internal static void CreateDefaultConfigFile() + { + //Create the config file here + Directory.CreateDirectory(AppDataDirectory); + + if (!File.Exists(ConfigFile)) + { + WriteDefaultConfig(); + } + } + + internal static void WriteDefaultConfig() + { + var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); + + xDoc.Add(new XElement("Config", + new XElement("Port", 8989), + new XElement("LaunchBrowser", true) + ) + ); + + xDoc.Save(ConfigFile); + } + + private static string GetValue(string key, string parent = null) + { + var xDoc = XDocument.Load(ConfigFile); + var config = xDoc.Descendants("Config").Single(); + + var parentContainer = config; + + if (parent != null) + parentContainer = config.Descendants(parent).Single(); + + var value = parentContainer.Descendants(key).Single().Value; + + return value; + } + + private static int GetValueInt(string key, string parent = null) + { + return Convert.ToInt32(GetValue(key, parent)); + } + + private static bool GetValueBoolean(string key, string parent = null) + { + return Convert.ToBoolean(GetValue(key, parent)); } } } \ No newline at end of file diff --git a/NzbDrone/Program.cs b/NzbDrone/Program.cs index 697065fb7..3200a24cd 100644 --- a/NzbDrone/Program.cs +++ b/NzbDrone/Program.cs @@ -16,6 +16,7 @@ private static void Main() try { Config.ConfigureNlog(); + Config.CreateDefaultConfigFile(); Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", Config.ProjectRoot); Thread.CurrentThread.Name = "Host"; @@ -39,7 +40,7 @@ private static void Main() Attach(); #endif - if (!Environment.UserInteractive) + if (!Environment.UserInteractive || !Config.LaunchBrowser) { try { diff --git a/NzbDrone/app.config b/NzbDrone/app.config index 81d395705..edeb0a26d 100644 --- a/NzbDrone/app.config +++ b/NzbDrone/app.config @@ -3,7 +3,4 @@ - - - \ No newline at end of file