1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-15 16:17:41 +02:00

Move FileConfig::Warn() into AudacityFileConfig as an override

This commit is contained in:
Paul Licameli 2021-01-31 10:55:32 -05:00
parent c853450ce0
commit 3480317ecd
4 changed files with 119 additions and 106 deletions

View File

@ -11,6 +11,15 @@ Paul Licameli split from Prefs.cpp
#include "Audacity.h"
#include "AudacityFileConfig.h"
#include "widgets/HelpSystem.h"
#include "widgets/wxPanelWrapper.h"
#include "ShuttleGui.h"
#include "../images/Help.xpm"
#include <wx/app.h>
#include <wx/bmpbuttn.h>
#include <wx/sizer.h>
AudacityFileConfig::AudacityFileConfig(
const wxString& appName,
const wxString& vendorName,
@ -40,3 +49,102 @@ std::unique_ptr<AudacityFileConfig> AudacityFileConfig::Create(
result->Init();
return result;
}
void AudacityFileConfig::Warn(bool canRetry)
{
wxDialogWrapper dlg(nullptr, wxID_ANY, XO("Audacity Configuration Error"));
ShuttleGui S(&dlg, eIsCreating);
wxButton *retryButton;
wxButton *quitButton;
S.SetBorder(5);
S.StartVerticalLay(wxEXPAND, 1);
{
S.SetBorder(15);
S.StartHorizontalLay(wxALIGN_RIGHT, 0);
{
auto cause = canRetry
? XO("The following configuration file could not be written:")
: XO("The following configuration file could not be read:");
auto retryMsg = canRetry
? XO("You can attempt to correct the issue and then click \"Retry\" to coninue.\n\n")
: XO("");
S.AddFixedText(
XO("%s:\n\n"
"\t%s\n\n"
"This could be caused by many reasons, but the most likely are that "
"the disk is full or you do not have write permissions to the file. "
"More information can be obtained by clicking the help button below.\n\n"
"%s"
"If you choose to \"Quit Audacity\", your project may be left in an unsaved "
"state which will be recovered the next time you open it.")
.Format(cause, GetFilePath(), retryMsg),
false,
500);
}
S.EndHorizontalLay();
S.SetBorder(5);
S.StartHorizontalLay(wxALIGN_RIGHT, 0);
{
// Can't use themed bitmap since the theme manager might not be
// initialized yet and it requires a configuration file.
wxButton *b = S.Id(wxID_HELP).AddBitmapButton(wxBitmap(Help_xpm));
b->SetToolTip( XO("Help").Translation() );
b->SetLabel(XO("Help").Translation()); // for screen readers
b = S.Id(wxID_CANCEL).AddButton(XXO("&Quit Audacity"));
if (canRetry)
{
b = S.Id(wxID_OK).AddButton(XXO("&Retry"));
dlg.SetAffirmativeId(wxID_OK);
}
b->SetDefault();
b->SetFocus();
}
S.EndHorizontalLay();
}
S.EndVerticalLay();
dlg.Layout();
dlg.GetSizer()->Fit(&dlg);
dlg.SetMinSize(dlg.GetSize());
dlg.Center();
auto onButton = [&](wxCommandEvent &e)
{
dlg.EndModal(e.GetId());
};
dlg.Bind(wxEVT_BUTTON, onButton);
switch (dlg.ShowModal())
{
case wxID_HELP:
// Can't use the HelpSystem since the theme manager may not
// yet be initialized and it requires a configuration file.
OpenInDefaultBrowser("https://" +
HelpSystem::HelpHostname +
HelpSystem::HelpServerHomeDir +
"Error:_Audacity_settings_file_unwritable");
break;
case wxID_CANCEL:
// This REALLY needs to use an exception with decent cleanup and program exit
wxExit();
#if defined(__WXGTK__)
wxAbort();
#else
exit(-1);
#endif
break;
}
dlg.Unbind(wxEVT_BUTTON, onButton);
}

View File

@ -31,6 +31,9 @@ public:
~AudacityFileConfig() override;
protected:
void Warn(bool canRetry) override;
private:
//! Disallow direct constructor call, because a two-phase initialization is required
AudacityFileConfig(

View File

@ -20,11 +20,6 @@
#include <wx/wfstream.h>
#include "FileConfig.h"
#include "HelpSystem.h"
#include "wxPanelWrapper.h"
#include "../ShuttleGui.h"
#include "../../images/Help.xpm"
#if !defined(F_OK)
#define F_OK 0x00
@ -91,6 +86,8 @@ void FileConfig::Init()
//
// If the wxFileConfig class allowed us to call wxFileConfig::Init(), we wouldn't
// have to do all this mess.
// (Note that invocation of virtual Warn() can't be done in the ctor,
// which is why this is two-phase construction.)
Warn(canRead == true);
}
}
@ -177,102 +174,3 @@ bool FileConfig::DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf)
return res;
}
#endif // wxUSE_BASE64
void FileConfig::Warn(bool canRetry)
{
wxDialogWrapper dlg(nullptr, wxID_ANY, XO("Audacity Configuration Error"));
ShuttleGui S(&dlg, eIsCreating);
wxButton *retryButton;
wxButton *quitButton;
S.SetBorder(5);
S.StartVerticalLay(wxEXPAND, 1);
{
S.SetBorder(15);
S.StartHorizontalLay(wxALIGN_RIGHT, 0);
{
auto cause = canRetry
? XO("The following configuration file could not be written:")
: XO("The following configuration file could not be read:");
auto retryMsg = canRetry
? XO("You can attempt to correct the issue and then click \"Retry\" to coninue.\n\n")
: XO("");
S.AddFixedText(
XO("%s:\n\n"
"\t%s\n\n"
"This could be caused by many reasons, but the most likely are that "
"the disk is full or you do not have write permissions to the file. "
"More information can be obtained by clicking the help button below.\n\n"
"%s"
"If you choose to \"Quit Audacity\", your project may be left in an unsaved "
"state which will be recovered the next time you open it.")
.Format(cause, mConfigPath, retryMsg),
false,
500);
}
S.EndHorizontalLay();
S.SetBorder(5);
S.StartHorizontalLay(wxALIGN_RIGHT, 0);
{
// Can't use themed bitmap since the theme manager might not be
// initialized yet and it requires a configuration file.
wxButton *b = S.Id(wxID_HELP).AddBitmapButton(wxBitmap(Help_xpm));
b->SetToolTip( XO("Help").Translation() );
b->SetLabel(XO("Help").Translation()); // for screen readers
b = S.Id(wxID_CANCEL).AddButton(XXO("&Quit Audacity"));
if (canRetry)
{
b = S.Id(wxID_OK).AddButton(XXO("&Retry"));
dlg.SetAffirmativeId(wxID_OK);
}
b->SetDefault();
b->SetFocus();
}
S.EndHorizontalLay();
}
S.EndVerticalLay();
dlg.Layout();
dlg.GetSizer()->Fit(&dlg);
dlg.SetMinSize(dlg.GetSize());
dlg.Center();
auto onButton = [&](wxCommandEvent &e)
{
dlg.EndModal(e.GetId());
};
dlg.Bind(wxEVT_BUTTON, onButton);
switch (dlg.ShowModal())
{
case wxID_HELP:
// Can't use the HelpSystem since the theme manager may not
// yet be initialized and it requires a configuration file.
OpenInDefaultBrowser("https://" +
HelpSystem::HelpHostname +
HelpSystem::HelpServerHomeDir +
"Error:_Audacity_settings_file_unwritable");
break;
case wxID_CANCEL:
// This REALLY needs to use an exception with decent cleanup and program exit
wxExit();
#if defined(__WXGTK__)
wxAbort();
#else
exit(-1);
#endif
break;
}
dlg.Unbind(wxEVT_BUTTON, onButton);
}

View File

@ -51,9 +51,13 @@ protected:
virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) wxOVERRIDE;
#endif // wxUSE_BASE64
private:
void Warn(bool canRetry = true);
protected:
//! Override to notify the user of error conditions involving writability of config files
virtual void Warn(bool canRetry = true) = 0;
const FilePath &GetFilePath() const { return mConfigPath; }
private:
const FilePath mConfigPath;
// values of the version major/minor/micro keys in audacity.cfg