2014-07-28 17:18:18 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-08-15 12:31:33 +00:00
|
|
|
|
namespace RainmeterStudio.Business
|
2014-07-28 17:18:18 +00:00
|
|
|
|
{
|
|
|
|
|
public static class SettingsProvider
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to retrieve the setting of type T, where T is class
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Any class type</typeparam>
|
|
|
|
|
/// <param name="name">Name of setting</param>
|
|
|
|
|
/// <returns>Retrieved setting, or null if not found</returns>
|
|
|
|
|
public static T GetSetting<T> (string name) where T : class
|
|
|
|
|
{
|
|
|
|
|
var property = Properties.Settings.Default.Properties
|
|
|
|
|
.OfType<SettingsProperty>()
|
|
|
|
|
.FirstOrDefault(x => String.Equals(x.Name, name));
|
|
|
|
|
|
|
|
|
|
return (property == null) ? null : (property.DefaultValue as T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to retrieve the setting of type T
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Any type</typeparam>
|
|
|
|
|
/// <param name="name">Name of setting</param>
|
|
|
|
|
/// <param name="value">Output value</param>
|
|
|
|
|
/// <returns>True if attempt was successful</returns>
|
|
|
|
|
public static bool TryGetSetting<T>(string name, out T value)
|
|
|
|
|
{
|
|
|
|
|
var property = Properties.Settings.Default.Properties.OfType<SettingsProperty>().FirstOrDefault(x => x.Name.Equals(name));
|
|
|
|
|
|
|
|
|
|
if (property != null)
|
|
|
|
|
{
|
|
|
|
|
value = (T)property.DefaultValue;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
value = default(T);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-15 12:31:33 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the settings
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void SaveSettings()
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets settings to default
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void ResetSettings()
|
|
|
|
|
{
|
|
|
|
|
Properties.Settings.Default.Reset();
|
|
|
|
|
}
|
2014-07-28 17:18:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|