Radarr/src/NzbDrone.Core/Configuration/ConfigService.cs

299 lines
8.4 KiB
C#
Raw Normal View History

2010-09-23 03:19:47 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2010-10-05 06:21:18 +00:00
using NLog;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Messaging.Events;
2013-09-11 06:33:47 +00:00
2010-09-23 03:19:47 +00:00
2013-02-24 06:48:52 +00:00
namespace NzbDrone.Core.Configuration
2010-09-23 03:19:47 +00:00
{
public enum ConfigKey
{
DownloadedEpisodesFolder
}
2013-02-24 06:48:52 +00:00
public class ConfigService : IConfigService
2010-09-23 03:19:47 +00:00
{
2013-02-24 06:48:52 +00:00
private readonly IConfigRepository _repository;
private readonly IEventAggregator _eventAggregator;
2013-02-24 06:48:52 +00:00
private readonly Logger _logger;
private static Dictionary<string, string> _cache;
public ConfigService(IConfigRepository repository, IEventAggregator eventAggregator, Logger logger)
2010-09-23 03:19:47 +00:00
{
2013-02-24 06:48:52 +00:00
_repository = repository;
_eventAggregator = eventAggregator;
2013-02-24 06:48:52 +00:00
_logger = logger;
_cache = new Dictionary<string, string>();
2010-09-23 03:19:47 +00:00
}
public IEnumerable<Config> All()
2010-09-23 03:19:47 +00:00
{
2013-02-24 06:48:52 +00:00
return _repository.All();
}
2010-09-24 05:21:45 +00:00
public Dictionary<String, Object> AllWithDefaults()
{
2013-03-06 18:41:13 +00:00
var dict = new Dictionary<String, Object>(StringComparer.InvariantCultureIgnoreCase);
var type = GetType();
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
var value = propertyInfo.GetValue(this, null);
dict.Add(propertyInfo.Name, value);
}
return dict;
}
public void SaveConfigDictionary(Dictionary<string, object> configValues)
{
var allWithDefaults = AllWithDefaults();
foreach (var configValue in configValues)
{
object currentValue;
allWithDefaults.TryGetValue(configValue.Key, out currentValue);
if (currentValue == null) continue;
var equal = configValue.Value.ToString().Equals(currentValue.ToString());
if (!equal)
SetValue(configValue.Key, configValue.Value.ToString());
}
_eventAggregator.PublishEvent(new ConfigSavedEvent());
}
2013-05-15 02:57:57 +00:00
public String DownloadedEpisodesFolder
{
get { return GetValue(ConfigKey.DownloadedEpisodesFolder.ToString()); }
set { SetValue(ConfigKey.DownloadedEpisodesFolder.ToString(), value); }
}
public bool AutoUnmonitorPreviouslyDownloadedEpisodes
{
get { return GetValueBoolean("AutoUnmonitorPreviouslyDownloadedEpisodes"); }
set { SetValue("AutoUnmonitorPreviouslyDownloadedEpisodes", value); }
}
public int Retention
{
get { return GetValueInt("Retention", 0); }
set { SetValue("Retention", value); }
}
public string RecycleBin
{
get { return GetValue("RecycleBin", String.Empty); }
set { SetValue("RecycleBin", value); }
}
public string ReleaseRestrictions
2013-03-18 15:25:36 +00:00
{
get { return GetValue("ReleaseRestrictions", String.Empty).Trim('\r', '\n'); }
set { SetValue("ReleaseRestrictions", value.Trim('\r', '\n')); }
2013-04-17 03:52:43 +00:00
}
public Int32 RssSyncInterval
{
get { return GetValueInt("RssSyncInterval", 15); }
set { SetValue("RssSyncInterval", value); }
}
public Boolean AutoDownloadPropers
{
get { return GetValueBoolean("AutoDownloadPropers", true); }
set { SetValue("AutoDownloadPropers", value); }
}
public Boolean AutoRedownloadFailed
{
get { return GetValueBoolean("AutoRedownloadFailed", true); }
set { SetValue("AutoRedownloadFailed", value); }
}
public Boolean RemoveFailedDownloads
{
get { return GetValueBoolean("RemoveFailedDownloads", true); }
set { SetValue("RemoveFailedDownloads", value); }
}
public Boolean EnableFailedDownloadHandling
{
get { return GetValueBoolean("EnableFailedDownloadHandling", true); }
set { SetValue("EnableFailedDownloadHandling", value); }
}
public Boolean CreateEmptySeriesFolders
{
get { return GetValueBoolean("CreateEmptySeriesFolders", false); }
set { SetValue("CreateEmptySeriesFolders", value); }
}
public FileDateType FileDate
{
get { return GetValueEnum("FileDate", FileDateType.None); }
set { SetValue("FileDate", value); }
}
public String DownloadClientWorkingFolders
{
get { return GetValue("DownloadClientWorkingFolders", "_UNPACK_|_FAILED_"); }
set { SetValue("DownloadClientWorkingFolders", value); }
}
public Int32 DownloadedEpisodesScanInterval
{
get { return GetValueInt("DownloadedEpisodesScanInterval", 1); }
set { SetValue("DownloadedEpisodesScanInterval", value); }
}
2014-01-26 08:57:14 +00:00
public Boolean SetPermissionsLinux
{
get { return GetValueBoolean("SetPermissionsLinux", false); }
set { SetValue("SetPermissionsLinux", value); }
}
public String FileChmod
{
2014-01-26 08:57:14 +00:00
get { return GetValue("FileChmod", "0644"); }
set { SetValue("FileChmod", value); }
}
public String FolderChmod
{
get { return GetValue("FolderChmod", "0755"); }
set { SetValue("FolderChmod", value); }
}
public String ChownUser
{
2014-01-31 16:17:50 +00:00
get { return GetValue("ChownUser", ""); }
2014-01-31 16:17:50 +00:00
set { SetValue("ChownUser", value); }
}
public String ChownGroup
{
2014-01-31 16:17:50 +00:00
get { return GetValue("ChownGroup", ""); }
2014-01-31 16:17:50 +00:00
set { SetValue("ChownGroup", value); }
}
private string GetValue(string key)
{
2011-06-17 02:27:10 +00:00
return GetValue(key, String.Empty);
2010-09-23 03:19:47 +00:00
}
private bool GetValueBoolean(string key, bool defaultValue = false)
{
2011-06-17 02:27:10 +00:00
return Convert.ToBoolean(GetValue(key, defaultValue));
}
private int GetValueInt(string key, int defaultValue = 0)
{
2012-12-31 21:09:43 +00:00
return Convert.ToInt32(GetValue(key, defaultValue));
}
2013-05-14 05:22:51 +00:00
public T GetValueEnum<T>(string key, T defaultValue)
{
return (T)Enum.Parse(typeof(T), GetValue(key, defaultValue), true);
}
public string GetValue(string key, object defaultValue, bool persist = false)
2010-09-23 03:19:47 +00:00
{
key = key.ToLowerInvariant();
Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
EnsureCache();
string dbValue;
2010-09-23 03:19:47 +00:00
2013-02-24 06:48:52 +00:00
if (_cache.TryGetValue(key, out dbValue) && dbValue != null && !String.IsNullOrEmpty(dbValue))
return dbValue;
2010-09-23 03:19:47 +00:00
2013-02-24 06:48:52 +00:00
_logger.Trace("Unable to find config key '{0}' defaultValue:'{1}'", key, defaultValue);
2012-01-25 03:09:49 +00:00
if (persist)
2013-02-24 06:48:52 +00:00
{
2012-01-25 03:09:49 +00:00
SetValue(key, defaultValue.ToString());
2013-02-24 06:48:52 +00:00
}
return defaultValue.ToString();
2010-09-23 03:19:47 +00:00
}
private void SetValue(string key, Boolean value)
{
SetValue(key, value.ToString());
}
private void SetValue(string key, int value)
{
SetValue(key, value.ToString());
}
public void SetValue(string key, string value)
2010-09-23 03:19:47 +00:00
{
2013-03-06 18:41:13 +00:00
key = key.ToLowerInvariant();
2010-09-24 05:37:48 +00:00
2014-03-13 20:12:42 +00:00
_logger.Trace("Writing Setting to database. Key:'{0}' Value:'{1}'", key, value);
2010-09-23 03:19:47 +00:00
2013-02-24 06:48:52 +00:00
var dbValue = _repository.Get(key);
2010-09-28 06:09:24 +00:00
if (dbValue == null)
{
2013-02-24 06:48:52 +00:00
_repository.Insert(new Config { Key = key, Value = value });
2010-09-28 06:09:24 +00:00
}
else
{
dbValue.Value = value;
2013-02-24 06:48:52 +00:00
_repository.Update(dbValue);
}
ClearCache();
}
2013-05-14 05:22:51 +00:00
public void SetValue(string key, Enum value)
{
SetValue(key, value.ToString().ToLower());
}
private void EnsureCache()
{
2013-02-24 06:48:52 +00:00
lock (_cache)
{
2013-02-24 06:48:52 +00:00
if (!_cache.Any())
2011-06-23 06:56:17 +00:00
{
_cache = All().ToDictionary(c => c.Key.ToLower(), c => c.Value);
2011-06-23 06:56:17 +00:00
}
}
2010-09-23 03:19:47 +00:00
}
public static void ClearCache()
{
2013-02-24 06:48:52 +00:00
lock (_cache)
{
2013-02-24 06:48:52 +00:00
_cache = new Dictionary<string, string>();
}
}
2010-09-23 03:19:47 +00:00
}
}