// ****************************************************************
// Copyright 2002-2003, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************
namespace NUnit.Util
{
using System;
///
/// SettingsGroup is the base class representing a group
/// of user or system settings. All storge of settings
/// is delegated to a SettingsStorage.
///
public class SettingsGroup : ISettings, IDisposable
{
#region Instance Fields
protected ISettingsStorage storage;
#endregion
#region Constructors
///
/// Construct a settings group.
///
/// Storage for the group settings
public SettingsGroup( ISettingsStorage storage )
{
this.storage = storage;
}
///
/// Protected constructor for use by derived classes that
/// set the storage themselves or don't use a storage.
///
protected SettingsGroup()
{
}
#endregion
#region Properties
public event SettingsEventHandler Changed;
///
/// The storage used for the group settings
///
public ISettingsStorage Storage
{
get { return storage; }
}
#endregion
#region ISettings Members
///
/// Load the value of one of the group's settings
///
/// Name of setting to load
/// Value of the setting or null
public object GetSetting( string settingName )
{
return storage.GetSetting( settingName );
}
///
/// Load the value of one of the group's settings or return a default value
///
/// Name of setting to load
/// Value to return if the seeting is not present
/// Value of the setting or the default
public object GetSetting( string settingName, object defaultValue )
{
object result = GetSetting(settingName );
if ( result == null )
result = defaultValue;
return result;
}
///
/// Load the value of one of the group's integer settings
/// in a type-safe manner or return a default value
///
/// Name of setting to load
/// Value to return if the seeting is not present
/// Value of the setting or the default
public int GetSetting( string settingName, int defaultValue )
{
object result = GetSetting(settingName );
if ( result == null )
return defaultValue;
if ( result is int )
return (int) result;
try
{
return Int32.Parse( result.ToString() );
}
catch
{
return defaultValue;
}
}
///
/// Load the value of one of the group's boolean settings
/// in a type-safe manner.
///
/// Name of setting to load
/// Value of the setting or the default
/// Value of the setting
public bool GetSetting( string settingName, bool defaultValue )
{
object result = GetSetting(settingName );
if ( result == null )
return defaultValue;
// Handle legacy formats
// if ( result is int )
// return (int)result == 1;
//
// if ( result is string )
// {
// if ( (string)result == "1" ) return true;
// if ( (string)result == "0" ) return false;
// }
if ( result is bool )
return (bool) result ;
try
{
return Boolean.Parse( result.ToString() );
}
catch
{
return defaultValue;
}
}
///
/// Load the value of one of the group's string settings
/// in a type-safe manner or return a default value
///
/// Name of setting to load
/// Value to return if the setting is not present
/// Value of the setting or the default
public string GetSetting( string settingName, string defaultValue )
{
object result = GetSetting(settingName );
if ( result == null )
return defaultValue;
if ( result is string )
return (string) result;
else
return result.ToString();
}
///
/// Load the value of one of the group's enum settings
/// in a type-safe manner or return a default value
///
/// Name of setting to load
/// Value to return if the setting is not present
/// Value of the setting or the default
public System.Enum GetSetting( string settingName, System.Enum defaultValue )
{
object result = GetSetting(settingName );
if ( result == null )
return defaultValue;
if ( result is System.Enum )
return (System.Enum) result;
try
{
return (System.Enum)System.Enum.Parse( defaultValue.GetType(), result.ToString(), true );
}
catch
{
return defaultValue;
}
}
///
/// Remove a setting from the group
///
/// Name of the setting to remove
public void RemoveSetting( string settingName )
{
storage.RemoveSetting( settingName );
if ( Changed != null )
Changed( this, new SettingsEventArgs( settingName ) );
}
///
/// Remove a group of settings
///
///
public void RemoveGroup( string groupName )
{
storage.RemoveGroup( groupName );
}
///
/// Save the value of one of the group's settings
///
/// Name of the setting to save
/// Value to be saved
public void SaveSetting( string settingName, object settingValue )
{
object oldValue = storage.GetSetting( settingName );
// Avoid signaling "changes" when there is not really a change
if ( oldValue != null )
{
if( oldValue is string && settingValue is string && (string)oldValue == (string)settingValue ||
oldValue is int && settingValue is int && (int)oldValue == (int)settingValue ||
oldValue is bool && settingValue is bool && (bool)oldValue == (bool)settingValue ||
oldValue is Enum && settingValue is Enum && oldValue.Equals(settingValue) )
return;
}
storage.SaveSetting( settingName, settingValue );
if ( Changed != null )
Changed( this, new SettingsEventArgs( settingName ) );
}
#endregion
#region IDisposable Members
///
/// Dispose of this group by disposing of it's storage implementation
///
public void Dispose()
{
if ( storage != null )
{
storage.Dispose();
storage = null;
}
}
#endregion
}
}