29#ifndef __AUDACITY_PREFS__
30#define __AUDACITY_PREFS__
39#define AUDACITY_PREFS_VERSION_STRING "1.1.1r1"
46#include <wx/filename.h>
47#include <wx/textfile.h>
54PREFERENCES_API
void InitPreferences( std::unique_ptr<audacity::BasicSettings> uPrefs );
160template<
typename T >
164 using TransactionalSettingBase::TransactionalSettingBase;
170 mutable T mCurrentValue{};
171 mutable bool mValid{
false};
176template<
typename T >
189 , mDefaultValue{ defaultValue }
195 , mFunction{ function }
202 mDefaultValue = mFunction();
203 return mDefaultValue;
209 return ReadWithDefault( pVar, GetDefault() );
216 *pVar = defaultValue;
217 if ( pVar && this->mValid ) {
218 *pVar = this->mCurrentValue;
222 if ( pVar && config ) {
223 if ((this->mValid = config->Read( this->mPath, &this->mCurrentValue )))
224 *pVar = this->mCurrentValue;
227 return (this->mValid =
false);
235 return ReadWithDefault( GetDefault() );
244 return this->mCurrentValue;
247 this->mCurrentValue =
248 config->ReadObject(this->
mPath, defaultValue);
251 this->mValid = (this->mCurrentValue != defaultValue);
252 return this->mCurrentValue;
263 if (config ==
nullptr)
270 this->mCurrentValue = value;
277 this->mCurrentValue = value;
286 return Write( GetDefault() );
291 this->mValid =
false;
297 const T value = Read();
299 for (
size_t i = mPreviousValues.size(); i < depth; ++i)
300 this->mPreviousValues.emplace_back( value );
306 assert(!this->mPreviousValues.empty());
308 if (this->mPreviousValues.empty())
311 const auto result = this->mPreviousValues.size() > 1 || DoWrite();
312 mPreviousValues.pop_back();
320 assert(!this->mPreviousValues.empty());
322 if (!this->mPreviousValues.empty())
324 this->mCurrentValue = std::move(this->mPreviousValues.back());
325 this->mPreviousValues.pop_back();
335 return this->mValid =
336 config ? config->Write( this->
mPath, this->mCurrentValue ) :
false;
340 mutable T mDefaultValue{};
420 long defaultSymbol = -1)
422 , mSymbols{ move(symbols) }
423 , mpOtherSettings{ &
key }
424 , mDefaultSymbol{ defaultSymbol }
426 assert(defaultSymbol <
static_cast<long>(mSymbols.size()));
431 long defaultSymbol = -1)
433 , mSymbols{ move(symbols) }
434 , mDefaultSymbol{ defaultSymbol }
436 assert(defaultSymbol <
static_cast<long>(mSymbols.size()));
439 const wxString &
Key()
const {
return mKey; }
443 wxString Read()
const;
448 wxString ReadWithDefault(
const wxString & )
const;
450 bool Write(
const wxString &value );
453 void SetDefault(
long value );
456 size_t Find(
const wxString &value )
const;
457 virtual void Migrate( wxString& );
464 mutable bool mMigrated {
false };
476 template<
typename Key>
482 std::vector<int> intValues,
483 const wxString &oldKey = {}
485 , mIntValues{ move(intValues) }
488 assert (mIntValues.size() == mSymbols.size());
499 int ReadIntWithDefault(
int defaultValue )
const;
503 size_t FindInt(
int code )
const;
504 void Migrate( wxString& )
override;
512template<
typename Enum >
518 template<
typename Key>
524 std::vector< Enum >
values,
525 const wxString &oldKey = {}
528 std::forward<Key>(
key), move(symbols), defaultSymbol,
529 ConvertValues(
values), oldKey
535 {
return static_cast<Enum
>(
ReadInt() ); }
542 auto integer =
static_cast<int>(defaultValue);
543 return static_cast<Enum
>( ReadIntWithDefault( integer ) );
547 {
return WriteInt(
static_cast<int>( value ) ); }
553 std::vector<int> result;
554 result.reserve(
values.size());
556 result.push_back(
static_cast<int>(value));
564 static void Register(std::unique_ptr<PreferencesResetHandler>
handler);
568 template<
typename HandlerType>
571 template<
typename... Args>
573 Register(std::make_unique<HandlerType>(std::forward<Args>(args)...));
588template<
typename SettingType>
610 assert(!mCapturedValue.has_value());
612 if(mSetting.Read(&value))
613 mCapturedValue = value;
618 if(mCapturedValue.has_value())
620 auto Do =
finally([=]{ mCapturedValue = std::nullopt; });
621 mSetting.Write(*mCapturedValue);
628 template<
typename... Args>
631 , mResetHandlerRegistration(mSetting)
640 SettingType&
Get() noexcept {
return mSetting; }
641 const SettingType&
Get() const noexcept {
return mSetting; }
644 const SettingType*
operator->() const noexcept {
return &mSetting; }
647 const SettingType&
operator*() const noexcept {
return mSetting; }
662 static void Broadcast(
int id = 0);
669 virtual void UpdatePrefs() = 0;
676 virtual void UpdateSelectedPrefs(
int id );
696 virtual void operator () () = 0;
698 static void ReinitializeAll();
PREFERENCES_API void GetPreferencesVersion(int &vMajor, int &vMinor, int &vMicro)
PREFERENCES_API wxString WarningDialogKey(const wxString &internalDialogName)
PREFERENCES_API void SetPreferencesVersion(int vMajor, int vMinor, int vMicor)
PREFERENCES_API void FinishPreferences()
PREFERENCES_API void ResetPreferences()
Call this to reset preferences to an (almost)-"new" default state.
PREFERENCES_API ByColumns_t ByColumns
PREFERENCES_API StickySetting< BoolSetting > DefaultUpdatesCheckingFlag
PREFERENCES_API void InitPreferences(std::unique_ptr< audacity::BasicSettings > uPrefs)
PREFERENCES_API audacity::BasicSettings * gPrefs
std::vector< TranslatableString > TranslatableStrings
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Class template adds an in-memory cache of a value to TransactionalSettingBase and support for Setting...
CachingSettingBase(const CachingSettingBase &)=default
CachingSettingBase(const SettingBase &path)
const wxString & Key() const
ChoiceSetting(const SettingBase &key, EnumValueSymbols symbols, long defaultSymbol=-1)
const EnumValueSymbols mSymbols
ChoiceSetting(const SettingPath &, EnumValueSymbols, long=-1)=delete
const EnumValueSymbols & GetSymbols() const
ChoiceSetting(TransactionalSettingBase &key, EnumValueSymbols symbols, long defaultSymbol=-1)
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Specialization of Setting for double.
std::vector< int > mIntValues
EnumSettingBase(Key &&key, EnumValueSymbols symbols, long defaultSymbol, std::vector< int > intValues, const wxString &oldKey={})
Adapts EnumSettingBase to a particular enumeration type.
std::vector< int > ConvertValues(const std::vector< Enum > &values)
bool WriteEnum(Enum value)
EnumSetting(Key &&key, EnumValueSymbols symbols, long defaultSymbol, std::vector< Enum > values, const wxString &oldKey={})
Enum ReadEnumWithDefault(Enum defaultValue) const
EnumValueSymbols()=default
wxArrayStringEx mInternals
EnumValueSymbols(std::vector< EnumValueSymbol > symbols)
EnumValueSymbols(std::initializer_list< EnumValueSymbol > symbols)
TranslatableStrings mMsgids
Specialization of Setting for int.
Allows custom logic for preferences reset event.
virtual void OnSettingResetBegin()=0
Happens before preferences reset.
virtual void OnSettingResetEnd()=0
Happens after preferences reset.
virtual ~PreferencesResetHandler()
A listener notified of changes in preferences.
std::unique_ptr< Impl > mpImpl
Base class for settings objects. It holds a configuration key path.
audacity::BasicSettings * GetConfig() const
SettingBase(const wxChar *path)
SettingBase(const wxString &path)
SettingBase(const char *path)
SettingBase(const SettingBase &)=default
const SettingPath & GetPath() const
bool Write(const T &value)
Write value to config and return true if successful.
T Read() const
overload of Read, always returning a value
Setting(const SettingBase &path, const T &defaultValue)
Usual overload supplies a default value.
T ReadWithDefault(const T &defaultValue) const
new direct use is discouraged but it may be needed in legacy code
void EnterTransaction(size_t depth) override
void Invalidate() override
bool ReadWithDefault(T *pVar, const T &defaultValue) const
overload of ReadWithDefault returning a boolean that is true if the value was previously defined */
void Rollback() noexcept override
bool Reset()
Reset to the default value.
bool DoWrite()
Write cached value to config and return true if successful.
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined */
std::function< T() > DefaultValueFunction
std::vector< T > mPreviousValues
const T & GetDefault() const
Setting(const SettingBase &path, DefaultValueFunction function)
This overload causes recomputation of the default each time it is needed.
const DefaultValueFunction mFunction
Makes temporary changes to preferences, then rolls them back at destruction.
static AddResult Add(TransactionalSettingBase &setting)
SettingScope(const SettingScope &)=delete
SettingScope & operator=(const SettingScope &)=delete
std::set< TransactionalSettingBase * > mPending
Extend SettingScope with Commit() which flushes updates in a batch.
void OnSettingResetEnd() override
Happens after preferences reset.
std::optional< ValueType > mCapturedValue
ResetHandler(ResetHandler &&)=delete
ResetHandler(const ResetHandler &)=delete
ResetHandler(SettingType &setting)
void OnSettingResetBegin() override
Happens before preferences reset.
ResetHandler & operator=(ResetHandler &&)=delete
ResetHandler & operator=(const ResetHandler &)=delete
StickySetting(StickySetting &&)=delete
SettingType * operator->() noexcept
StickySetting(const StickySetting &)=delete
PreferencesResetHandler::Registration< ResetHandler > mResetHandlerRegistration
SettingType & operator*() noexcept
const SettingType * operator->() const noexcept
StickySetting & operator=(const StickySetting &)=delete
StickySetting(Args &&...args)
const SettingType & Get() const noexcept
SettingType & Get() noexcept
StringSetting & operator=(StickySetting &&)=delete
const SettingType & operator*() const noexcept
Specialization of Setting for strings.
friend class SettingScope
virtual void Rollback() noexcept=0
virtual void EnterTransaction(size_t depth)=0
virtual void Invalidate()=0
Base class for objects that provide facility to store data persistently, and access it with string ke...
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
PROJECT_FILE_IO_API void Add(const FilePath &path)
PROJECT_FILE_IO_API wxString Find(const FilePath &path)
bool GetConfig(const EffectDefinitionInterface &ident, ConfigurationType type, const RegistryPath &group, const RegistryPath &key, Value &var, const Value &defval)
static const auto WriteInt
static const auto ReadInt
AuthorizationHandler handler
Performs single-time global handler registration.
Registration(Args &&... args)