Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
AudacityFileConfig Class Referencefinal

Our own specialisation of FileConfig. More...

#include <AudacityFileConfig.h>

Inheritance diagram for AudacityFileConfig:
[legend]
Collaboration diagram for AudacityFileConfig:
[legend]

Public Member Functions

bool Flush (bool bCurrentOnly) override
 
 ~AudacityFileConfig () override
 
bool RenameEntry (const wxString &oldName, const wxString &newName) override
 
bool RenameGroup (const wxString &oldName, const wxString &newName) override
 
bool DeleteEntry (const wxString &key, bool bDeleteGroupIfEmpty) override
 
bool DeleteGroup (const wxString &key) override
 
bool DeleteAll () override
 

Static Public Member Functions

static std::unique_ptr< AudacityFileConfigCreate (const wxString &appName={}, const wxString &vendorName={}, const wxString &localFilename={}, const wxString &globalFilename={}, long style=wxCONFIG_USE_LOCAL_FILE|wxCONFIG_USE_GLOBAL_FILE, const wxMBConv &conv=wxConvAuto())
 Require a call to this factory, to guarantee proper two-phase initialization. More...
 

Protected Member Functions

bool DoWriteString (const wxString &key, const wxString &szValue) override
 
bool DoWriteLong (const wxString &key, long lValue) override
 

Private Member Functions

 AudacityFileConfig (const wxString &appName, const wxString &vendorName, const wxString &localFilename, const wxString &globalFilename, long style, const wxMBConv &conv)
 Disallow direct constructor call, because a two-phase initialization is required. More...
 
void Init ()
 
void Warn () const
 

Private Attributes

bool mDirty {false}
 
const wxString mLocalFilename
 

Detailed Description

Our own specialisation of FileConfig.

Definition at line 19 of file AudacityFileConfig.h.

Constructor & Destructor Documentation

◆ ~AudacityFileConfig()

AudacityFileConfig::~AudacityFileConfig ( )
override

Definition at line 77 of file AudacityFileConfig.cpp.

78{
79 wxASSERT(mDirty == false);
80}

References mDirty.

◆ AudacityFileConfig()

AudacityFileConfig::AudacityFileConfig ( const wxString &  appName,
const wxString &  vendorName,
const wxString &  localFilename,
const wxString &  globalFilename,
long  style,
const wxMBConv &  conv 
)
private

Disallow direct constructor call, because a two-phase initialization is required.

Definition at line 24 of file AudacityFileConfig.cpp.

32: wxFileConfig{ appName, vendorName, localFilename, globalFilename, style, conv }
33, mLocalFilename(localFilename)
34{
35}
const wxString mLocalFilename

Member Function Documentation

◆ Create()

std::unique_ptr< AudacityFileConfig > AudacityFileConfig::Create ( const wxString &  appName = {},
const wxString &  vendorName = {},
const wxString &  localFilename = {},
const wxString &  globalFilename = {},
long  style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
const wxMBConv &  conv = wxConvAuto() 
)
static

Require a call to this factory, to guarantee proper two-phase initialization.

Definition at line 82 of file AudacityFileConfig.cpp.

90{
91 // Private ctor means make_unique can't compile, so this verbosity:
92 auto result = std::unique_ptr<AudacityFileConfig>{
94 appName, vendorName, localFilename, globalFilename, style, conv } };
95 result->Init();
96 return result;
97}
#define safenew
Definition: MemoryX.h:9
Our own specialisation of FileConfig.

References safenew, and anonymous_namespace{AudacityDontAskAgainMessageDialog.cpp}::style.

Referenced by AudacityApp::InitPart2(), and anonymous_namespace{AudacityApp.cpp}::PopulatePreferences().

Here is the caller graph for this function:

◆ DeleteAll()

bool AudacityFileConfig::DeleteAll ( )
override

Definition at line 264 of file AudacityFileConfig.cpp.

265{
266 auto res = wxFileConfig::DeleteAll();
267 if (res)
268 {
269 mDirty = true;
270 }
271 return res;
272}

References mDirty.

◆ DeleteEntry()

bool AudacityFileConfig::DeleteEntry ( const wxString &  key,
bool  bDeleteGroupIfEmpty 
)
override

Definition at line 244 of file AudacityFileConfig.cpp.

245{
246 auto res = wxFileConfig::DeleteEntry(key, bDeleteGroupIfEmpty);
247 if (res)
248 {
249 mDirty = true;
250 }
251 return res;
252}
static const AudacityProject::AttachedObjects::RegisteredFactory key

References key, and mDirty.

◆ DeleteGroup()

bool AudacityFileConfig::DeleteGroup ( const wxString &  key)
override

Definition at line 254 of file AudacityFileConfig.cpp.

255{
256 auto res = wxFileConfig::DeleteGroup(key);
257 if (res)
258 {
259 mDirty = true;
260 }
261 return res;
262}

References key, and mDirty.

◆ DoWriteLong()

bool AudacityFileConfig::DoWriteLong ( const wxString &  key,
long  lValue 
)
overrideprotected

Definition at line 284 of file AudacityFileConfig.cpp.

285{
286 bool res = wxFileConfig::DoWriteLong(key, lValue);
287 if (res)
288 {
289 mDirty = true;
290 }
291 return res;
292}

References key, and mDirty.

◆ DoWriteString()

bool AudacityFileConfig::DoWriteString ( const wxString &  key,
const wxString &  szValue 
)
overrideprotected

Definition at line 274 of file AudacityFileConfig.cpp.

275{
276 bool res = wxFileConfig::DoWriteString(key, szValue);
277 if (res)
278 {
279 mDirty = true;
280 }
281 return res;
282}

References key, and mDirty.

◆ Flush()

bool AudacityFileConfig::Flush ( bool  bCurrentOnly)
override

Definition at line 99 of file AudacityFileConfig.cpp.

100{
101 if (!mDirty)
102 {
103 return true;
104 }
105
106 while (true)
107 {
108 FilePath backup = mLocalFilename + ".bkp";
109
110 if (!wxFileExists(backup) || (wxRemove(backup) == 0))
111 {
112 if (!wxFileExists(mLocalFilename) || (wxRename(mLocalFilename, backup) == 0))
113 {
114 wxFileOutputStream stream(mLocalFilename);
115 if (stream.IsOk())
116 {
117 if (Save(stream))
118 {
119 stream.Sync();
120 if (stream.IsOk() && stream.Close())
121 {
122 if (!wxFileExists(backup) || (wxRemove(backup) == 0))
123 {
124 mDirty = false;
125 return true;
126 }
127 }
128 }
129 }
130
131 if (wxFileExists(backup))
132 {
133 wxRemove(mLocalFilename);
134 wxRename(backup, mLocalFilename);
135 }
136 }
137 }
138
139 Warn();
140 }
141}
wxString FilePath
Definition: Project.h:21

References mDirty, mLocalFilename, and Warn().

Here is the call graph for this function:

◆ Init()

void AudacityFileConfig::Init ( )
private

Definition at line 37 of file AudacityFileConfig.cpp.

38{
39 // Prevent wxFileConfig from attempting a Flush() during object deletion. This happens
40 // because we don't use the wxFileConfig::Flush() method and so the wxFileConfig dirty
41 // flag never gets reset. During deletion, the dirty flag is checked and a Flush()
42 // performed. This can (and probably will) create bogus temporary files.
43 DisableAutoSave();
44
45 while (true)
46 {
47 bool canRead = false;
48 bool canWrite = false;
49 int fd;
50
51 fd = wxOpen(mLocalFilename, O_RDONLY, S_IREAD);
52 if (fd != -1 || errno == ENOENT)
53 {
54 canRead = true;
55 if (fd != -1)
56 {
57 wxClose(fd);
58 }
59 }
60
61 fd = wxOpen(mLocalFilename, O_WRONLY | O_CREAT, S_IWRITE);
62 if (fd != -1)
63 {
64 canWrite = true;
65 wxClose(fd);
66 }
67
68 if (canRead && canWrite)
69 {
70 break;
71 }
72
73 Warn();
74 }
75}

References mLocalFilename, and Warn().

Here is the call graph for this function:

◆ RenameEntry()

bool AudacityFileConfig::RenameEntry ( const wxString &  oldName,
const wxString &  newName 
)
override

Definition at line 224 of file AudacityFileConfig.cpp.

225{
226 auto res = wxFileConfig::RenameEntry(oldName, newName);
227 if (res)
228 {
229 mDirty = true;
230 }
231 return res;
232}

References mDirty.

◆ RenameGroup()

bool AudacityFileConfig::RenameGroup ( const wxString &  oldName,
const wxString &  newName 
)
override

Definition at line 234 of file AudacityFileConfig.cpp.

235{
236 auto res = wxFileConfig::RenameGroup(oldName, newName);
237 if (res)
238 {
239 mDirty = true;
240 }
241 return res;
242}

References mDirty.

◆ Warn()

void AudacityFileConfig::Warn ( ) const
private

Definition at line 143 of file AudacityFileConfig.cpp.

144{
145 wxDialogWrapper dlg(nullptr, wxID_ANY, XO("Audacity Configuration Error"));
146
147 ShuttleGui S(&dlg, eIsCreating);
148
149 wxButton *retryButton;
150 wxButton *quitButton;
151
152 S.SetBorder(5);
153 S.StartVerticalLay(wxEXPAND, 1);
154 {
155 S.SetBorder(15);
156 S.StartHorizontalLay(wxALIGN_RIGHT, 0);
157 {
158 S.AddFixedText(
159 XO("The following configuration file could not be accessed:\n\n"
160 "\t%s\n\n"
161 "This could be caused by many reasons, but the most likely are that "
162 "the disk is full or you do not have write permissions to the file. "
163 "\n\n"
164 "You can attempt to correct the issue and then click \"Retry\" to continue.\n\n"
165 "If you choose to \"Quit Audacity\", your project may be left in an unsaved "
166 "state which will be recovered the next time you open it.")
168 false,
169 500);
170 }
171 S.EndHorizontalLay();
172
173 S.SetBorder(5);
174 S.StartHorizontalLay(wxALIGN_RIGHT, 0);
175 {
176 // Can't use themed bitmap since the theme manager might not be
177 // initialized yet and it requires a configuration file.
178 wxButton *b = S.Id(wxID_HELP).AddBitmapButton(wxBitmap(Help_xpm));
179 b->SetToolTip( XO("Help").Translation() );
180 b->SetLabel(XO("Help").Translation()); // for screen readers
181
182 b = S.Id(wxID_CANCEL).AddButton(XXO("&Quit Audacity"));
183 b = S.Id(wxID_OK).AddButton(XXO("&Retry"));
184 dlg.SetAffirmativeId(wxID_OK);
185
186 b->SetDefault();
187 b->SetFocus();
188 }
189 S.EndHorizontalLay();
190 }
191 S.EndVerticalLay();
192
193 dlg.Layout();
194 dlg.GetSizer()->Fit(&dlg);
195 dlg.SetMinSize(dlg.GetSize());
196 dlg.Center();
197
198 auto onButton = [&](wxCommandEvent &e)
199 {
200 dlg.EndModal(e.GetId());
201 };
202
203 dlg.Bind(wxEVT_BUTTON, onButton);
204
205 switch (dlg.ShowModal())
206 {
207 case wxID_HELP:
208 // Can't use the HelpSystem since the theme manager may not
209 // yet be initialized and it requires a configuration file.
210 OpenInDefaultBrowser("https://" +
213 "Error:_Audacity_settings_file_unwritable");
214 break;
215
216 case wxID_CANCEL:
217 _exit(-1);
218 break;
219 }
220
221 dlg.Unbind(wxEVT_BUTTON, onButton);
222}
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
@ eIsCreating
Definition: ShuttleGui.h:37
#define S(N)
Definition: ToChars.cpp:64
Abstract base class used in importing a file.
static const wxString HelpHostname
Definition: HelpSystem.h:96
static const wxString HelpServerHomeDir
Definition: HelpSystem.h:101
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:630
bool OpenInDefaultBrowser(const wxString &url)
Open an URL in default browser.
Definition: BasicUI.cpp:240

References eIsCreating, HelpSystem::HelpHostname, HelpSystem::HelpServerHomeDir, mLocalFilename, BasicUI::OpenInDefaultBrowser(), S, XO(), and XXO().

Referenced by Flush(), and Init().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mDirty

bool AudacityFileConfig::mDirty {false}
private

◆ mLocalFilename

const wxString AudacityFileConfig::mLocalFilename
private

Definition at line 65 of file AudacityFileConfig.h.

Referenced by Flush(), Init(), and Warn().


The documentation for this class was generated from the following files: