2010-10-15 07:10:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2011-11-13 07:27:16 +00:00
|
|
|
|
using NzbDrone.Common.Model;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
namespace NzbDrone.Common
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
public interface IConfigFileProvider
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
Guid Guid { get; }
|
|
|
|
|
int Port { get; set; }
|
|
|
|
|
bool LaunchBrowser { get; set; }
|
|
|
|
|
AuthenticationType AuthenticationType { get; set; }
|
2013-05-22 00:58:57 +00:00
|
|
|
|
string BasicAuthUsername { get; set; }
|
|
|
|
|
string BasicAuthPassword { get; set; }
|
2013-05-10 23:53:50 +00:00
|
|
|
|
int GetValueInt(string key, int defaultValue);
|
|
|
|
|
bool GetValueBoolean(string key, bool defaultValue);
|
|
|
|
|
string GetValue(string key, object defaultValue);
|
2013-05-22 04:06:25 +00:00
|
|
|
|
T GetValueEnum<T>(string key, T defaultValue);
|
2013-05-10 23:53:50 +00:00
|
|
|
|
void SetValue(string key, object value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ConfigFileProvider : IConfigFileProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly IEnvironmentProvider _environmentProvider;
|
2011-04-10 02:44:01 +00:00
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
private readonly string _configFile;
|
2013-04-16 00:08:06 +00:00
|
|
|
|
|
2013-05-10 23:53:50 +00:00
|
|
|
|
public ConfigFileProvider(IEnvironmentProvider environmentProvider)
|
2011-10-08 04:51:35 +00:00
|
|
|
|
{
|
2012-03-07 02:59:43 +00:00
|
|
|
|
_environmentProvider = environmentProvider;
|
|
|
|
|
_configFile = _environmentProvider.GetConfigPath();
|
2011-11-14 00:22:18 +00:00
|
|
|
|
|
|
|
|
|
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 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
public virtual int Port
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
get { return GetValueInt("Port", 8989); }
|
2011-11-13 07:27:16 +00:00
|
|
|
|
set { SetValue("Port", value); }
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:04 +00:00
|
|
|
|
public virtual bool LaunchBrowser
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-08 00:17:46 +00:00
|
|
|
|
get { return GetValueBoolean("LaunchBrowser", true); }
|
2011-11-13 07:27:16 +00:00
|
|
|
|
set { SetValue("LaunchBrowser", value); }
|
2011-10-07 06:36:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
public virtual AuthenticationType AuthenticationType
|
|
|
|
|
{
|
2013-05-22 00:58:57 +00:00
|
|
|
|
get { return GetValueEnum("AuthenticationType", AuthenticationType.Anonymous); }
|
|
|
|
|
set { SetValue("AuthenticationType", value); }
|
2011-10-08 00:17:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 00:58:57 +00:00
|
|
|
|
public virtual string BasicAuthUsername
|
|
|
|
|
{
|
|
|
|
|
get { return GetValue("BasicAuthUsername", ""); }
|
|
|
|
|
set { SetValue("BasicAuthUsername", value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string BasicAuthPassword
|
|
|
|
|
{
|
|
|
|
|
get { return GetValue("BasicAuthPassword", ""); }
|
|
|
|
|
set { SetValue("BasicAuthPassword", value); }
|
|
|
|
|
}
|
2011-12-14 02:31:20 +00:00
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 04:06:25 +00:00
|
|
|
|
public T GetValueEnum<T>(string key, T defaultValue)
|
2013-05-22 00:58:57 +00:00
|
|
|
|
{
|
|
|
|
|
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
public virtual string GetValue(string key, object defaultValue)
|
2011-10-07 06:36:04 +00:00
|
|
|
|
{
|
2011-11-13 07:27:16 +00:00
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var parentContainer = config;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var valueHolder = parentContainer.Descendants(key).ToList();
|
|
|
|
|
|
|
|
|
|
if (valueHolder.Count() == 1)
|
|
|
|
|
return valueHolder.First().Value;
|
|
|
|
|
|
|
|
|
|
//Save the value
|
2011-11-14 00:22:18 +00:00
|
|
|
|
SetValue(key, defaultValue);
|
2011-10-08 00:17:46 +00:00
|
|
|
|
|
|
|
|
|
//return the default value
|
|
|
|
|
return defaultValue.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
public virtual void SetValue(string key, object value)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-11-13 07:27:16 +00:00
|
|
|
|
var xDoc = XDocument.Load(_configFile);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
var keyHolder = parentContainer.Descendants(key);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
if (keyHolder.Count() != 1)
|
|
|
|
|
parentContainer.Add(new XElement(key, value));
|
2011-10-01 03:12:18 +00:00
|
|
|
|
|
2011-10-08 00:17:46 +00:00
|
|
|
|
else
|
|
|
|
|
parentContainer.Descendants(key).Single().Value = value.ToString();
|
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
xDoc.Save(_configFile);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 04:06:25 +00:00
|
|
|
|
public void SetValue(string key, Enum value)
|
2013-05-22 00:58:57 +00:00
|
|
|
|
{
|
|
|
|
|
SetValue(key, value.ToString().ToLower());
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 00:22:18 +00:00
|
|
|
|
private void CreateDefaultConfigFile()
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-11-13 07:27:16 +00: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 07:10:44 +00:00
|
|
|
|
}
|
2011-11-13 07:27:16 +00:00
|
|
|
|
}
|