2011-10-01 05:12:18 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Linq;
|
2011-10-07 06:36:47 +02:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Core
|
|
|
|
|
{
|
|
|
|
|
public class ConfigFileProvider
|
|
|
|
|
{
|
2011-10-21 07:04:26 +02:00
|
|
|
|
private string _configFile = Path.Combine(new EnviromentProvider().AppPath, "App_Data", "Config.xml");
|
2011-10-07 06:36:47 +02:00
|
|
|
|
|
2011-10-01 05:12:18 +02:00
|
|
|
|
public string ConfigFile
|
|
|
|
|
{
|
2011-10-07 06:36:47 +02:00
|
|
|
|
get { return _configFile; }
|
|
|
|
|
set { _configFile = value; }
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
2011-10-21 07:04:26 +02:00
|
|
|
|
|
2011-10-01 05:12:18 +02:00
|
|
|
|
public virtual int Port
|
|
|
|
|
{
|
2011-10-07 06:36:47 +02:00
|
|
|
|
get { return GetValueInt("Port", 8989); }
|
2011-10-01 09:04:06 +02:00
|
|
|
|
set { SetValue("Port", value); }
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool LaunchBrowser
|
|
|
|
|
{
|
2011-10-07 06:36:47 +02:00
|
|
|
|
get { return GetValueBoolean("LaunchBrowser", true); }
|
2011-10-01 09:04:06 +02:00
|
|
|
|
set { SetValue("LaunchBrowser", value); }
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
public virtual AuthenticationType AuthenticationType
|
|
|
|
|
{
|
|
|
|
|
get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); }
|
|
|
|
|
set { SetValue("AuthenticationType", (int)value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string GetValue(string key, object defaultValue, string parent = null)
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
2011-10-01 09:04:06 +02:00
|
|
|
|
if (!String.IsNullOrEmpty(parent))
|
2011-10-07 06:36:47 +02:00
|
|
|
|
{
|
|
|
|
|
//Add the parent
|
|
|
|
|
if (config.Descendants(parent).Count() != 1)
|
|
|
|
|
{
|
|
|
|
|
SetValue(key, defaultValue, parent);
|
|
|
|
|
|
|
|
|
|
//Reload the configFile
|
|
|
|
|
xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
config = xDoc.Descendants("Config").Single();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-01 05:12:18 +02:00
|
|
|
|
parentContainer = config.Descendants(parent).Single();
|
2011-10-07 06:36:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var valueHolder = parentContainer.Descendants(key).ToList();
|
2011-10-21 07:04:26 +02:00
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
if (valueHolder.Count() == 1)
|
|
|
|
|
return valueHolder.First().Value;
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
//Save the value
|
|
|
|
|
SetValue(key, defaultValue, parent);
|
2011-10-01 05:12:18 +02:00
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
//return the default value
|
|
|
|
|
return defaultValue.ToString();
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
public virtual int GetValueInt(string key, int defaultValue, string parent = null)
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
2011-10-07 06:36:47 +02:00
|
|
|
|
return Convert.ToInt32(GetValue(key, defaultValue, parent));
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
public virtual bool GetValueBoolean(string key, bool defaultValue, string parent = null)
|
2011-10-01 05:12:18 +02:00
|
|
|
|
{
|
2011-10-07 06:36:47 +02:00
|
|
|
|
return Convert.ToBoolean(GetValue(key, defaultValue, parent));
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
2011-10-01 09:04:06 +02:00
|
|
|
|
|
|
|
|
|
public virtual void SetValue(string key, object value, string parent = null)
|
|
|
|
|
{
|
|
|
|
|
var xDoc = XDocument.Load(ConfigFile);
|
|
|
|
|
var config = xDoc.Descendants("Config").Single();
|
|
|
|
|
|
|
|
|
|
var parentContainer = config;
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(parent))
|
2011-10-07 06:36:47 +02:00
|
|
|
|
{
|
|
|
|
|
//Add the parent container if it doesn't already exist
|
|
|
|
|
if (config.Descendants(parent).Count() != 1)
|
|
|
|
|
{
|
|
|
|
|
config.Add(new XElement(parent));
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-01 09:04:06 +02:00
|
|
|
|
parentContainer = config.Descendants(parent).Single();
|
2011-10-07 06:36:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var keyHolder = parentContainer.Descendants(key);
|
|
|
|
|
|
|
|
|
|
if (keyHolder.Count() != 1)
|
|
|
|
|
parentContainer.Add(new XElement(key, value));
|
2011-10-01 09:04:06 +02:00
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
else
|
|
|
|
|
parentContainer.Descendants(key).Single().Value = value.ToString();
|
2011-10-01 09:04:06 +02:00
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CreateDefaultConfigFile()
|
|
|
|
|
{
|
|
|
|
|
//Create the config file here
|
2011-10-21 07:04:26 +02:00
|
|
|
|
Directory.CreateDirectory(Path.Combine(new EnviromentProvider().AppPath, "App_Data"));
|
2011-10-01 09:04:06 +02:00
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigFile))
|
|
|
|
|
{
|
|
|
|
|
WriteDefaultConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void WriteDefaultConfig()
|
|
|
|
|
{
|
|
|
|
|
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
|
|
|
|
2011-10-07 06:36:47 +02:00
|
|
|
|
xDoc.Add(new XElement("Config"));
|
2011-10-01 09:04:06 +02:00
|
|
|
|
|
|
|
|
|
xDoc.Save(ConfigFile);
|
|
|
|
|
}
|
2011-10-01 05:12:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|