You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/NzbDrone.Core/Providers/ConfigProvider.cs

86 lines
2.1 KiB

14 years ago
using System;
using log4net;
using NzbDrone.Core.Repository;
using SubSonic.Repository;
namespace NzbDrone.Core.Providers
14 years ago
{
public class ConfigProvider : IConfigProvider
14 years ago
{
private const string SERIES_ROOTS = "SeriesRoots";
14 years ago
private readonly ILog _logger;
private readonly IRepository _sonicRepo;
public ConfigProvider(ILog logger, IRepository dataRepository)
14 years ago
{
_logger = logger;
14 years ago
_sonicRepo = dataRepository;
}
private string GetValue(string key)
14 years ago
{
return GetValue(key, String.Empty, false);
14 years ago
}
public String SeriesRoot
14 years ago
{
get
{
return GetValue(SERIES_ROOTS);
}
set
{
SetValue(SERIES_ROOTS, value);
}
14 years ago
}
public string GetValue(string key, object defaultValue, bool makePermanent)
14 years ago
{
string value;
var dbValue = _sonicRepo.Single<Config>(key);
if (dbValue != null && !String.IsNullOrEmpty(dbValue.Value))
{
return dbValue.Value;
}
_logger.WarnFormat("Unable to find config key '{0}' defaultValue:'{1}'", key, defaultValue);
if (makePermanent)
{
SetValue(key, defaultValue.ToString());
}
value = defaultValue.ToString();
return value;
}
public void SetValue(string key, string value)
14 years ago
{
if (String.IsNullOrEmpty(key)) throw new ArgumentOutOfRangeException("key");
if (value == null) throw new ArgumentNullException("key");
14 years ago
_logger.DebugFormat("Writing Setting to file. Key:'{0}' Value:'{1}'", key, value);
var dbValue = _sonicRepo.Single<Config>(key);
if (dbValue == null)
{
_sonicRepo.Add(new Config { Key = key, Value = value });
}
else
{
dbValue.Value = value;
_sonicRepo.Update(dbValue);
}
14 years ago
}
}
}