IPreference stores preferences, but knows nothing about loading or

saving them; that's up to PrefsManager.

Treat IPreference as a simple data holder, with simple facilities for
looking them up by name, converting to/from strings, and storing to/from
an XNode, but with no application-specific code to save to disk.  This
can be used alone as a way for code to configure things internally, even
for uses that have no notion of storing user preferences (eg. unit tests),
and other programs can use it to store preferences in entirely different
ways (SMPackage could use it to store to the registry).  PrefsManager is
layered on top, to implement StepMania's particular use of Preference
(saving and loading INIs), but isn't needed for Preference to be useful.

This also makes Preference only use XNode, not the more specialized IniFile.

(One piece is missing: several low-level places, eg. Dialog, want to set
a preference and write it to disk immediately.  The only way to do that
is to have access to PREFSMAN.  FIXME.)
This commit is contained in:
Glenn Maynard
2005-12-31 04:59:56 +00:00
parent 7c2220b403
commit f435983996
3 changed files with 22 additions and 17 deletions
+11 -9
View File
@@ -1,6 +1,6 @@
#include "global.h"
#include "Preference.h"
#include "IniFile.h"
#include "XmlFile.h"
#include "RageLog.h"
#include "LuaFunctions.h"
#include "LuaManager.h"
@@ -37,16 +37,18 @@ void IPreference::LoadAllDefaults()
(*p)->LoadDefault();
}
void IPreference::ReadAllPrefsFromIni( const IniFile &ini, const CString &sSection )
void IPreference::ReadAllPrefsFromNode( const XNode* pNode )
{
if( pNode == NULL )
return;
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->ReadFrom( ini, sSection );
(*p)->ReadFrom( pNode );
}
void IPreference::SavePrefsToIni( IniFile &ini )
void IPreference::SavePrefsToNode( XNode* pNode )
{
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->WriteTo( ini );
(*p)->WriteTo( pNode );
}
void IPreference::PushValue( lua_State *L ) const
@@ -87,16 +89,16 @@ READFROM_AND_WRITETO( float )
READFROM_AND_WRITETO( bool )
READFROM_AND_WRITETO( CString )
void IPreference::ReadFrom( const IniFile &ini, const CString &sSection )
void IPreference::ReadFrom( const XNode* pNode )
{
CString sVal;
if( ini.GetValue( sSection, m_sName, sVal ) )
if( pNode->GetAttrValue(m_sName, sVal) )
FromString( sVal );
}
void IPreference::WriteTo( IniFile &ini ) const
void IPreference::WriteTo( XNode* pNode ) const
{
ini.SetValue( "Options", m_sName, ToString() );
pNode->AppendAttr( m_sName, ToString() );
}
void BroadcastPreferenceChanged( const CString& sPreferenceName )
+5 -5
View File
@@ -5,7 +5,7 @@
#include "EnumHelper.h"
#include "RageUtil.h"
class IniFile;
class XNode;
struct lua_State;
class IPreference
@@ -15,8 +15,8 @@ public:
virtual ~IPreference();
virtual void LoadDefault() = 0;
virtual void ReadFrom( const IniFile &ini, const CString &sSection );
virtual void WriteTo( IniFile &ini ) const;
virtual void ReadFrom( const XNode* pNode );
virtual void WriteTo( XNode* pNode ) const;
virtual CString ToString() const = 0;
virtual void FromString( const CString &s ) = 0;
@@ -28,8 +28,8 @@ public:
static IPreference *GetPreferenceByName( const CString &sName );
static void LoadAllDefaults();
static void ReadAllPrefsFromIni( const IniFile &ini, const CString &sSection );
static void SavePrefsToIni( IniFile &ini );
static void ReadAllPrefsFromNode( const XNode* pNode );
static void SavePrefsToNode( XNode* pNode );
protected:
CString m_sName;
+6 -3
View File
@@ -419,7 +419,7 @@ void PrefsManager::ReadPrefsFromIni( const IniFile &ini, const CString &sSection
CString sFallback;
if( ini.GetValue(sSection,"Fallback",sFallback) )
{
ReadPrefsFromIni( ini, sFallback );
IPreference::ReadAllPrefsFromNode( ini.GetChild(sFallback) );
}
//IPreference *pPref = PREFSMAN->GetPreferenceByName( *sName );
@@ -430,7 +430,7 @@ void PrefsManager::ReadPrefsFromIni( const IniFile &ini, const CString &sSection
// }
// pPref->FromString( sVal );
IPreference::ReadAllPrefsFromIni( ini, sSection );
IPreference::ReadAllPrefsFromNode( ini.GetChild(sSection) );
// validate
m_iSongsPerPlay.Set( clamp(m_iSongsPerPlay.Get(),0,MAX_SONGS_PER_PLAY) );
@@ -469,7 +469,10 @@ void PrefsManager::SavePrefsToIni( IniFile &ini )
if( !m_sCurrentGame.Get().empty() )
StoreGamePrefs();
IPreference::SavePrefsToIni( ini );
XNode* pNode = ini.GetChild( "Options" );
if( pNode == NULL )
pNode = ini.AppendChild( "Options" );
IPreference::SavePrefsToNode( pNode );
FOREACHM_CONST( CString, GamePrefs, m_mapGameNameToGamePrefs, iter )
{