//******************************************************************************************************
// IniSettingsBase.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 04/01/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 09/21/2011 - J. Ritchie Carroll
// Excluded class from Mono deployments due to P/Invoke requirements.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.ComponentModel;
using GSF.Interop;
namespace GSF.Configuration
{
///
/// Represents the base class for application settings that are synchronized to an INI file.
///
///
///
/// In order to make custom types serializable for the INI file, implement a for the type.
/// See MSDN for details.
///
///
/// Here is an example class derived from that automatically
/// serializes its fields and properties to the INI file.
///
/// public enum MyEnum
/// {
/// One,
/// Two,
/// Three
/// }
///
/// public class MySettings : IniSettingsBase
/// {
/// // Private property fields (private fields will not be serialized)
/// private double m_doubleVal;
///
/// // Public settings fields
/// public bool BoolVal = true;
/// public int IntVal = 1;
/// public float FloatVal = 3.14F;
/// public string StrVal = "This is a test...";
/// public MyEnum EnumVal = MyEnum.Three;
///
/// [SettingName("UserOptions"), EncryptSetting()]
/// public string Password = "default";
///
/// // Mark this field to not be serialized to INI file...
/// [SerializeSetting(false)]
/// public decimal DecimalVal;
///
/// public MySettings()
/// : base(FilePath.GetAbsolutePath("MySettings.ini"), "GeneralSettings") {}
///
/// [Category("OtherSettings"), DefaultValue(1.159D)]
/// public double DoubleVal
/// {
/// get
/// {
/// return m_doubleVal;
/// }
/// set
/// {
/// m_doubleVal = value;
/// }
/// }
///
/// [SerializeSetting(false)]
/// public bool DontSerializeMe { get; set; }
/// }
///
///
///
public abstract class IniSettingsBase : SettingsBase
{
#region [ Members ]
// Fields
private IniFile m_iniFile;
private string m_sectionName;
private bool m_useCategoryAttributes;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class for the application's INI file.
///
/// Name of INI file to use for accessing settings.
/// Name of default section to use to get and set settings from INI file.
protected IniSettingsBase(string iniFileName, string sectionName)
: this(new IniFile(iniFileName), sectionName, true, false, true)
{
}
///
/// Creates a new instance of the class for the application's INI file.
///
/// Name of INI file to use for accessing settings.
/// Name of default section to use to get and set settings from INI file.
/// Determines if category attributes will be used for section names.
///
/// Assigns flag that determines if is required
/// to exist before a field or property is serialized to the INI file.
///
///
/// If is false, all settings will be placed in section labeled by the
/// value; otherwise, if a exists on a field or
/// property then the member value will serialized into the INI file in a section labeled the same
/// as the value and if the attribute doesn't exist the member value
/// will serialized into the section labeled by the value.
///
protected IniSettingsBase(string iniFileName, string sectionName, bool useCategoryAttributes, bool requireSerializeSettingAttribute)
: this(new IniFile(iniFileName), sectionName, useCategoryAttributes, requireSerializeSettingAttribute, true)
{
}
///
/// Creates a new instance of the class for the application's INI file.
///
/// INI file to use for accessing settings.
/// Name of default section to use to get and set settings from INI file.
/// Determines if category attributes will be used for section names.
///
/// Assigns flag that determines if is required
/// to exist before a field or property is serialized to the INI file.
///
/// Determines if method should be called from constructor.
///
///
/// If is false, all settings will be placed in section labeled by the
/// value; otherwise, if a exists on a field or
/// property then the member value will serialized into the INI file in a section labeled the same
/// as the value and if the attribute doesn't exist the member value
/// will serialized into the section labeled by the value.
///
///
/// Note that some .NET languages (e.g., Visual Basic) will not initialize member elements before call to constuctor,
/// in this case should be set to false, then the
/// method should be called manually after all properties have been initialized. Alternately, consider using the
/// on the fields or properties and this will be used to initialize the values.
///
///
protected IniSettingsBase(IniFile iniFile, string sectionName, bool useCategoryAttributes, bool requireSerializeSettingAttribute, bool initialize)
: base(requireSerializeSettingAttribute)
{
m_iniFile = iniFile;
m_sectionName = sectionName;
m_useCategoryAttributes = useCategoryAttributes;
// Make sure settings exist and load current values
if (initialize)
Initialize();
}
#endregion
#region [ Properties ]
///
/// Gets or sets reference to working INI file.
///
/// value cannot be null.
protected IniFile IniFile
{
get
{
return m_iniFile;
}
set
{
if ((object)value == null)
throw new NullReferenceException("value cannot be null");
m_iniFile = value;
}
}
///
/// Gets or sets default name of section used to access settings in INI file.
///
public string SectionName
{
get
{
return m_sectionName;
}
set
{
m_sectionName = value;
}
}
///
/// Gets or sets value that determines whether a applied to a field or property
/// will be used for the section name.
///
///
/// If is false, all settings will be placed in section labeled by the
/// value; otherwise, if a exists on a field or
/// property then the member value will serialized into the INI file in a section labeled the same
/// as the value and if the attribute doesn't exist the member value
/// will serialized into the section labeled by the value.
///
public bool UseCategoryAttributes
{
get
{
return m_useCategoryAttributes;
}
set
{
m_useCategoryAttributes = value;
}
}
#endregion
#region [ Methods ]
///
/// Create setting in INI file if it doesn't already exist.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void CreateSetting(string name, string setting, string value)
{
string section = GetFieldSectionName(name);
if (string.IsNullOrEmpty(m_iniFile[section, setting]))
m_iniFile[section, setting] = value;
}
///
/// Retrieves setting from INI file.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override string RetrieveSetting(string name, string setting)
{
return m_iniFile[GetFieldSectionName(name), setting];
}
///
/// Stores setting to INI file.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void StoreSetting(string name, string setting, string value)
{
m_iniFile[GetFieldSectionName(name), setting] = value;
}
///
/// Persists any pending changes to INI file.
/// This method is for internal use.
///
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void PersistSettings()
{
// INI files flush at every update...
}
///
/// Gets the section name to use for the specified field or property.
///
/// Field or property name.
/// applied to specified field or property; or if attribute does not exist.
/// cannot be null or empty.
///
/// will only be returned if is true; otherwise
/// value will be returned.
///
public string GetFieldSectionName(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name cannot be null or empty");
// If user wants to respect category attributes, we attempt to use those as configuration section names
if (m_useCategoryAttributes)
return GetAttributeValue(name, m_sectionName, attribute => attribute.Category);
// Otherwise return default category name
return m_sectionName;
}
#endregion
}
}