From c525784f77aac2d575f598ea4d71987e9e449bbd Mon Sep 17 00:00:00 2001 From: Igor Korot Date: Tue, 30 Jul 2019 00:01:32 -0500 Subject: [PATCH] Allow disabling saving wxFileConfig data when it is destroyed This can be useful to avoid saving the changes performed by the user if this turns out to be undesirable, for whatever reason. Closes https://github.com/wxWidgets/wxWidgets/pull/1451 Closes #13788. --- include/wx/fileconf.h | 4 ++++ interface/wx/fileconf.h | 23 +++++++++++++++++++++++ src/common/fileconf.cpp | 4 +++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/wx/fileconf.h b/include/wx/fileconf.h index a15468cfd7..fd392da194 100644 --- a/include/wx/fileconf.h +++ b/include/wx/fileconf.h @@ -182,6 +182,9 @@ public: virtual bool Save(wxOutputStream& os, const wxMBConv& conv = wxConvAuto()); #endif // wxUSE_STREAMS + void EnableAutoSave() { m_autosave = true; } + void DisableAutoSave() { m_autosave = false; } + public: // functions to work with this list wxFileConfigLineList *LineListAppend(const wxString& str); @@ -250,6 +253,7 @@ private: #endif // __UNIX__ bool m_isDirty; // if true, we have unsaved changes + bool m_autosave; // if true, save changes on destruction wxDECLARE_NO_COPY_CLASS(wxFileConfig); wxDECLARE_ABSTRACT_CLASS(wxFileConfig); diff --git a/interface/wx/fileconf.h b/interface/wx/fileconf.h index 8e71afb541..caef821174 100644 --- a/interface/wx/fileconf.h +++ b/interface/wx/fileconf.h @@ -95,6 +95,29 @@ public: */ virtual bool Save(wxOutputStream& os, const wxMBConv& conv = wxConvAuto()); + /** + Enables saving data to the disk file when this object is destroyed. + + This is the default behaviour and this function doesn't need to be + called explicitly unless DisableAutoSave() had been previously called. + + @since 3.1.3 + */ + void EnableAutoSave(); + + /** + Prevent this object from saving data to the disk file when it is + destroyed. + + By default, changes to this object are only saved permanently when + Flush() is explicitly called or when it is destroyed. If this method is + called, Flush() won't be called automatically from the destructor, + meaning that any non-explicitly-flushed changes will be lost. + + @since 3.1.3 + */ + void DisableAutoSave(); + /** Allows setting the mode to be used for the config file creation. For example, to create a config file which is not readable by other users (useful if it stores diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 54262d8038..1f492580a5 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -346,6 +346,7 @@ void wxFileConfig::Init() } m_isDirty = false; + m_autosave = true; } // constructor supports creation of wxFileConfig objects of any type @@ -487,7 +488,8 @@ void wxFileConfig::CleanUp() wxFileConfig::~wxFileConfig() { - Flush(); + if ( m_autosave ) + Flush(); CleanUp();