1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Common/IConfigFileProvider.cs

131 lines
3.6 KiB
C#
Raw Normal View History

2010-10-15 09:10:44 +02:00
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
2011-11-13 08:27:16 +01:00
using NzbDrone.Common.Model;
2010-10-15 09:10:44 +02:00
2011-11-13 08:27:16 +01:00
namespace NzbDrone.Common
2010-10-15 09:10:44 +02:00
{
2013-05-11 01:53:50 +02:00
public interface IConfigFileProvider
2010-10-15 09:10:44 +02:00
{
2013-05-11 01:53:50 +02:00
Guid Guid { get; }
int Port { get; set; }
bool LaunchBrowser { get; set; }
AuthenticationType AuthenticationType { get; set; }
int GetValueInt(string key, int defaultValue);
bool GetValueBoolean(string key, bool defaultValue);
string GetValue(string key, object defaultValue);
void SetValue(string key, object value);
}
public class ConfigFileProvider : IConfigFileProvider
{
private readonly IEnvironmentProvider _environmentProvider;
2011-04-10 04:44:01 +02:00
2011-11-13 08:27:16 +01:00
private readonly string _configFile;
2013-04-16 02:08:06 +02:00
2013-05-11 01:53:50 +02:00
public ConfigFileProvider(IEnvironmentProvider environmentProvider)
{
_environmentProvider = environmentProvider;
_configFile = _environmentProvider.GetConfigPath();
CreateDefaultConfigFile();
}
public virtual Guid Guid
{
get
{
var key = "Guid";
if (string.IsNullOrWhiteSpace(GetValue(key, string.Empty)))
{
SetValue(key, Guid.NewGuid().ToString());
}
return Guid.Parse(GetValue(key, string.Empty));
}
2010-10-15 09:10:44 +02:00
}
2011-11-13 08:27:16 +01:00
public virtual int Port
2010-10-15 09:10:44 +02:00
{
get { return GetValueInt("Port", 8989); }
2011-11-13 08:27:16 +01:00
set { SetValue("Port", value); }
}
2011-10-07 08:36:04 +02:00
public virtual bool LaunchBrowser
{
get { return GetValueBoolean("LaunchBrowser", true); }
2011-11-13 08:27:16 +01:00
set { SetValue("LaunchBrowser", value); }
2011-10-07 08:36:04 +02:00
}
public virtual AuthenticationType AuthenticationType
{
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
2011-11-13 08:27:16 +01:00
set { SetValue("AuthenticationType", (int)value); }
}
public virtual int GetValueInt(string key, int defaultValue)
{
return Convert.ToInt32(GetValue(key, defaultValue));
}
public virtual bool GetValueBoolean(string key, bool defaultValue)
{
return Convert.ToBoolean(GetValue(key, defaultValue));
}
public virtual string GetValue(string key, object defaultValue)
2011-10-07 08:36:04 +02:00
{
2011-11-13 08:27:16 +01:00
var xDoc = XDocument.Load(_configFile);
var config = xDoc.Descendants("Config").Single();
var parentContainer = config;
var valueHolder = parentContainer.Descendants(key).ToList();
if (valueHolder.Count() == 1)
return valueHolder.First().Value;
//Save the value
SetValue(key, defaultValue);
//return the default value
return defaultValue.ToString();
}
public virtual void SetValue(string key, object value)
{
2011-11-13 08:27:16 +01:00
var xDoc = XDocument.Load(_configFile);
var config = xDoc.Descendants("Config").Single();
var parentContainer = config;
var keyHolder = parentContainer.Descendants(key);
if (keyHolder.Count() != 1)
parentContainer.Add(new XElement(key, value));
else
parentContainer.Descendants(key).Single().Value = value.ToString();
2011-11-13 08:27:16 +01:00
xDoc.Save(_configFile);
}
private void CreateDefaultConfigFile()
{
2011-11-13 08:27:16 +01:00
if (!File.Exists(_configFile))
{
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
xDoc.Add(new XElement("Config"));
xDoc.Save(_configFile);
}
}
2010-10-15 09:10:44 +02:00
}
2011-11-13 08:27:16 +01:00
}