2010-10-15 07:10:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
using System.Linq;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
using System.Reflection;
|
2011-10-01 03:12:18 +00:00
|
|
|
|
using System.Xml.Linq;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
namespace NzbDrone.Providers
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal class ConfigProvider
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual string ApplicationRoot
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2011-10-07 01:30:44 +00:00
|
|
|
|
var appDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
while (appDir.GetDirectories("iisexpress").Length == 0)
|
|
|
|
|
{
|
|
|
|
|
if (appDir.Parent == null) throw new ApplicationException("Can't fine IISExpress folder.");
|
|
|
|
|
appDir = appDir.Parent;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
return appDir.FullName;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual int Port
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-10-01 03:12:18 +00:00
|
|
|
|
get { return GetValueInt("Port"); }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual bool LaunchBrowser
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
get { return GetValueBoolean("LaunchBrowser"); }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual string AppDataDirectory
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
2011-10-07 01:30:44 +00:00
|
|
|
|
get { return Path.Combine(ApplicationRoot, "NzbDrone.Web", "App_Data"); }
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual string ConfigFile
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(AppDataDirectory, "Config.xml"); }
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual string IISFolder
|
|
|
|
|
{
|
|
|
|
|
get { return Path.Combine(ApplicationRoot, @"IISExpress\"); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal virtual void ConfigureNlog()
|
2011-04-10 02:44:01 +00:00
|
|
|
|
{
|
|
|
|
|
LogManager.Configuration = new XmlLoggingConfiguration(
|
2011-10-07 01:30:44 +00:00
|
|
|
|
Path.Combine(ApplicationRoot, "NzbDrone.Web\\log.config"), false);
|
2011-10-01 03:12:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual void CreateDefaultConfigFile()
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
//Create the config file here
|
|
|
|
|
Directory.CreateDirectory(AppDataDirectory);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigFile))
|
|
|
|
|
{
|
|
|
|
|
WriteDefaultConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
internal virtual void WriteDefaultConfig()
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
private string GetValue(string key, string parent = null)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
private int GetValueInt(string key, string parent = null)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToInt32(GetValue(key, parent));
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 01:30:44 +00:00
|
|
|
|
private bool GetValueBoolean(string key, string parent = null)
|
2011-10-01 03:12:18 +00:00
|
|
|
|
{
|
|
|
|
|
return Convert.ToBoolean(GetValue(key, parent));
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|