2021-11-13 17:11:08 +01:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-04-03 14:58:59 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-11-13 17:11:08 +01:00
|
|
|
|
using System.Configuration;
|
2022-04-03 14:58:59 +02:00
|
|
|
|
using System.Threading;
|
2021-11-13 17:11:08 +01:00
|
|
|
|
|
|
|
|
|
namespace com.muldersoft.slunkcrypt.gui.utils
|
|
|
|
|
{
|
|
|
|
|
class ApplicationConfig
|
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
private readonly ConcurrentDictionary<string, int> m_cache = new ConcurrentDictionary<string, int>();
|
2021-11-13 17:11:08 +01:00
|
|
|
|
|
|
|
|
|
public bool DisableBusyIndicator
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
return ComputeIfAbsent("DisableBusyIndicator", (key) => AppConfHelper.GetConfigValueAsBool(key).GetValueOrDefault(false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool KeepIncompleteFiles
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ComputeIfAbsent("KeepIncompleteFiles", (key) => AppConfHelper.GetConfigValueAsBool(key).GetValueOrDefault(false));
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 14:58:59 +02:00
|
|
|
|
public int ThreadCount
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
get
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
return ComputeIfAbsent("ThreadCount", (key) => AppConfHelper.GetConfigValueAsInt32(key).GetValueOrDefault(0));
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 14:58:59 +02:00
|
|
|
|
// =============================================================================
|
|
|
|
|
// Internal methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
protected int ComputeIfAbsent(string name, Func<string, int> valueFactory)
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
return m_cache.GetOrAdd(name, valueFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool ComputeIfAbsent(string name, Func<string, bool> valueFactory)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBoolean(m_cache.GetOrAdd(name, (key) => Convert.ToInt32(valueFactory(key))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Helper class
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
private static class AppConfHelper
|
|
|
|
|
{
|
|
|
|
|
private static readonly Lazy<KeyValueConfigurationCollection> m_settings = new Lazy<KeyValueConfigurationCollection>(InitializeSettings, LazyThreadSafetyMode.ExecutionAndPublication);
|
|
|
|
|
|
|
|
|
|
private static volatile object m_syncRoot;
|
|
|
|
|
|
|
|
|
|
public static string GetConfigValue(string name)
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
KeyValueConfigurationCollection settings = m_settings.Value;
|
|
|
|
|
if (!ReferenceEquals(settings, null))
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
lock (settings.SyncRoot ?? SyncRootInstance)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
KeyValueConfigurationElement element = settings[name];
|
|
|
|
|
if (!ReferenceEquals(element, null))
|
|
|
|
|
{
|
|
|
|
|
string value = element.Value;
|
|
|
|
|
return string.IsNullOrWhiteSpace(value) ? string.Empty : value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
2022-04-03 14:58:59 +02:00
|
|
|
|
return string.Empty;
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 14:58:59 +02:00
|
|
|
|
public static bool? GetConfigValueAsBool(string name)
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
string value;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(value = GetConfigValue(name)))
|
|
|
|
|
{
|
|
|
|
|
bool result;
|
|
|
|
|
if (bool.TryParse(value.Trim(), out result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
2022-04-03 14:58:59 +02:00
|
|
|
|
|
|
|
|
|
public static int? GetConfigValueAsInt32(string name)
|
2021-11-13 17:11:08 +01:00
|
|
|
|
{
|
2022-04-03 14:58:59 +02:00
|
|
|
|
string value;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(value = GetConfigValue(name)))
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
if (int.TryParse(value.Trim(), out result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-13 17:11:08 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-04-03 14:58:59 +02:00
|
|
|
|
|
|
|
|
|
private static KeyValueConfigurationCollection InitializeSettings()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
|
|
|
|
return configuration.AppSettings.Settings;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static object SyncRootInstance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (m_syncRoot == null)
|
|
|
|
|
{
|
|
|
|
|
Interlocked.CompareExchange<object>(ref m_syncRoot, new object(), null);
|
|
|
|
|
}
|
|
|
|
|
return m_syncRoot;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-13 17:11:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|