[finished] Heavy refactor

- move most of the business logic to Config
- make retranslate of the page more robust (e.g. changing language
  after failure would restore the un-failed message)

There's still some bits left.
This commit is contained in:
Adriaan de Groot 2021-02-23 15:03:16 +01:00
parent 288fe5b274
commit 9d6d8ecaea
5 changed files with 159 additions and 94 deletions

View File

@ -33,11 +33,55 @@ Config::Config( QObject* parent )
{
}
void
Config::setRestartNowMode( Config::RestartMode m )
{
// Can only go "down" in state (Always > UserDefaultChecked > .. > Never)
if ( m > m_restartNowMode )
{
return;
}
// If changing to an unconditional mode, also set other flag
if ( m == RestartMode::Always || m == RestartMode::Never )
{
setRestartNowWanted( m == RestartMode::Always );
}
if ( m != m_restartNowMode )
{
m_restartNowMode = m;
emit restartModeChanged( m );
}
}
void
Config::setRestartNowWanted( bool w )
{
// Follow the mode which may affect @p w
if ( m_restartNowMode == RestartMode::Always )
{
w = true;
}
if ( m_restartNowMode == RestartMode::Never )
{
w = false;
}
if ( w != m_userWantsRestart )
{
m_userWantsRestart = w;
emit restartNowWantedChanged( w );
}
}
void
Config::setConfigurationMap( const QVariantMap& configurationMap )
{
RestartMode mode = RestartMode::Never;
//TODO:3.3 remove deprecated restart settings
QString restartMode = CalamaresUtils::getString( configurationMap, "restartNowMode" );
if ( restartMode.isEmpty() )
{
@ -69,6 +113,9 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
}
m_restartNowMode = mode;
m_userWantsRestart = ( mode == RestartMode::Always || mode == RestartMode::UserDefaultChecked );
emit restartModeChanged( m_restartNowMode );
emit restartNowWantedChanged( m_userWantsRestart );
if ( mode != RestartMode::Never )
{

View File

@ -20,8 +20,10 @@ class Config : public QObject
{
Q_OBJECT
Q_PROPERTY( RestartMode restartNowMode READ restartNowMode WRITE setRestartNowMode NOTIFY restartModeChanged )
Q_PROPERTY( bool restartNowWanted READ restartNowWanted WRITE setRestartNowWanted NOTIFY restartNowWantedChanged )
Q_PROPERTY( QString restartNowCommand READ restartNowCommand CONSTANT FINAL )
Q_PROPERTY( RestartMode restartNowMode READ restartNowMode CONSTANT FINAL )
Q_PROPERTY( bool notifyOnFinished READ notifyOnFinished CONSTANT FINAL )
public:
@ -36,15 +38,26 @@ public:
};
Q_ENUM( RestartMode )
QString restartNowCommand() const { return m_restartNowCommand; }
RestartMode restartNowMode() const { return m_restartNowMode; }
bool restartNowWanted() const { return m_userWantsRestart; }
QString restartNowCommand() const { return m_restartNowCommand; }
bool notifyOnFinished() const { return m_notifyOnFinished; }
void setConfigurationMap( const QVariantMap& configurationMap );
public slots:
void setRestartNowMode( RestartMode m );
void setRestartNowWanted( bool w );
signals:
void restartModeChanged( RestartMode m );
void restartNowWantedChanged( bool w );
private:
QString m_restartNowCommand;
RestartMode m_restartNowMode = RestartMode::Never;
bool m_userWantsRestart = false;
bool m_notifyOnFinished = false;
};

View File

@ -27,10 +27,9 @@
#include <QProcess>
FinishedPage::FinishedPage( QWidget* parent )
FinishedPage::FinishedPage( Config* config, QWidget* parent )
: QWidget( parent )
, ui( new Ui::FinishedPage )
, m_mode( Config::RestartMode::UserDefaultUnchecked )
{
ui->setupUi( this );
@ -38,68 +37,19 @@ FinishedPage::FinishedPage( QWidget* parent )
ui->mainText->setWordWrap( true );
ui->mainText->setOpenExternalLinks( true );
CALAMARES_RETRANSLATE(
const auto* branding = Calamares::Branding::instance(); ui->retranslateUi( this );
if ( Calamares::Settings::instance()->isSetupMode() ) {
ui->mainText->setText( tr( "<h1>All done.</h1><br/>"
"%1 has been set up on your computer.<br/>"
"You may now start using your new system." )
.arg( branding->versionedName() ) );
ui->restartCheckBox->setToolTip( tr( "<html><head/><body>"
"<p>When this box is checked, your system will "
"restart immediately when you click on "
"<span style=\"font-style:italic;\">Done</span> "
"or close the setup program.</p></body></html>" ) );
} else {
ui->mainText->setText( tr( "<h1>All done.</h1><br/>"
"%1 has been installed on your computer.<br/>"
"You may now restart into your new system, or continue "
"using the %2 Live environment." )
.arg( branding->versionedName(), branding->productName() ) );
ui->restartCheckBox->setToolTip( tr( "<html><head/><body>"
"<p>When this box is checked, your system will "
"restart immediately when you click on "
"<span style=\"font-style:italic;\">Done</span> "
"or close the installer.</p></body></html>" ) );
} )
}
void
FinishedPage::setRestart( Config::RestartMode mode )
{
connect( config, &Config::restartModeChanged, [this]( Config::RestartMode mode ) {
using Mode = Config::RestartMode;
m_mode = mode;
ui->restartCheckBox->setVisible( mode != Mode::Never );
ui->restartCheckBox->setEnabled( mode != Mode::Always );
ui->restartCheckBox->setChecked( ( mode == Mode::Always ) || ( mode == Mode::UserDefaultChecked ) );
}
void
FinishedPage::setRestartNowCommand( const QString& command )
{
m_restartNowCommand = command;
}
void
FinishedPage::setUpRestart()
{
cDebug() << "FinishedPage::setUpRestart(), Quit button"
<< "setup=" << restartModes().find( m_mode ) << "command=" << m_restartNowCommand;
connect( qApp, &QApplication::aboutToQuit, [this]() {
if ( ui->restartCheckBox->isVisible() && ui->restartCheckBox->isChecked() )
{
cDebug() << "Running restart command" << m_restartNowCommand;
QProcess::execute( "/bin/sh", { "-c", m_restartNowCommand } );
}
} );
}
connect( config, &Config::restartNowWantedChanged, ui->restartCheckBox, &QCheckBox::setChecked );
connect( ui->restartCheckBox, &QCheckBox::stateChanged, [config]( int state ) {
config->setRestartNowWanted( state != 0 );
} );
CALAMARES_RETRANSLATE_SLOT( &FinishedPage::retranslate );
}
void
FinishedPage::focusInEvent( QFocusEvent* e )
@ -110,19 +60,64 @@ FinishedPage::focusInEvent( QFocusEvent* e )
void
FinishedPage::onInstallationFailed( const QString& message, const QString& details )
{
m_failure = !message.isEmpty() ? message : details;
retranslate();
}
void
FinishedPage::retranslate()
{
const auto* branding = Calamares::Branding::instance();
Q_UNUSED( details )
ui->retranslateUi( this );
if ( !m_failure.has_value() )
{
if ( Calamares::Settings::instance()->isSetupMode() )
{
ui->mainText->setText( tr( "<h1>All done.</h1><br/>"
"%1 has been set up on your computer.<br/>"
"You may now start using your new system." )
.arg( branding->versionedName() ) );
ui->restartCheckBox->setToolTip( tr( "<html><head/><body>"
"<p>When this box is checked, your system will "
"restart immediately when you click on "
"<span style=\"font-style:italic;\">Done</span> "
"or close the setup program.</p></body></html>" ) );
}
else
{
ui->mainText->setText( tr( "<h1>All done.</h1><br/>"
"%1 has been installed on your computer.<br/>"
"You may now restart into your new system, or continue "
"using the %2 Live environment." )
.arg( branding->versionedName(), branding->productName() ) );
ui->restartCheckBox->setToolTip( tr( "<html><head/><body>"
"<p>When this box is checked, your system will "
"restart immediately when you click on "
"<span style=\"font-style:italic;\">Done</span> "
"or close the installer.</p></body></html>" ) );
}
}
else
{
const QString message = m_failure.value();
if ( Calamares::Settings::instance()->isSetupMode() )
{
ui->mainText->setText( tr( "<h1>Setup Failed</h1><br/>"
"%1 has not been set up on your computer.<br/>"
"The error message was: %2." )
.arg( branding->versionedName() )
.arg( message ) );
}
else
{
ui->mainText->setText( tr( "<h1>Installation Failed</h1><br/>"
"%1 has not been installed on your computer.<br/>"
"The error message was: %2." )
.arg( branding->versionedName() )
.arg( message ) );
setRestart( Config::RestartMode::Never );
}
}
}

View File

@ -16,6 +16,8 @@
#include <QWidget>
#include <optional>
namespace Ui
{
class FinishedPage;
@ -25,24 +27,19 @@ class FinishedPage : public QWidget
{
Q_OBJECT
public:
explicit FinishedPage( QWidget* parent = nullptr );
explicit FinishedPage( Config* config, QWidget* parent = nullptr );
void setRestart( Config::RestartMode mode );
void setRestartNowCommand( const QString& command );
void setUpRestart();
public slots:
void onInstallationFailed( const QString& message, const QString& details );
void retranslate();
protected:
void focusInEvent( QFocusEvent* e ) override; //choose the child widget to focus
private:
Ui::FinishedPage* ui;
Config::RestartMode m_mode;
QString m_restartNowCommand;
std::optional< QString > m_failure;
};
#endif // FINISHEDPAGE_H

View File

@ -28,7 +28,7 @@
FinishedViewStep::FinishedViewStep( QObject* parent )
: Calamares::ViewStep( parent )
, m_config( new Config( this ) )
, m_widget( new FinishedPage() )
, m_widget( new FinishedPage( m_config ) )
, m_installFailed( false )
{
auto jq = Calamares::JobQueue::instance();
@ -128,10 +128,29 @@ FinishedViewStep::sendNotification()
}
#if 0
void
FinishedPage::setUpRestart()
{
cDebug() << "FinishedPage::setUpRestart(), Quit button"
<< "setup=" << restartModes().find( m_mode ) << "command=" << m_restartNowCommand;
connect( qApp, &QApplication::aboutToQuit, [this]()
{
if ( ui->restartCheckBox->isVisible() && ui->restartCheckBox->isChecked() )
{
cDebug() << "Running restart command" << m_restartNowCommand;
QProcess::execute( "/bin/sh", { "-c", m_restartNowCommand } );
}
} );
}
#endif
void
FinishedViewStep::onActivate()
{
m_widget->setUpRestart();
// m_widget->setUpRestart();
if ( m_config->notifyOnFinished() )
{
@ -158,12 +177,6 @@ void
FinishedViewStep::setConfigurationMap( const QVariantMap& configurationMap )
{
m_config->setConfigurationMap( configurationMap );
m_widget->setRestart( m_config->restartNowMode() );
if ( m_config->restartNowMode() != Config::RestartMode::Never )
{
m_widget->setRestartNowCommand( m_config->restartNowCommand() );
}
}
CALAMARES_PLUGIN_FACTORY_DEFINITION( FinishedViewStepFactory, registerPlugin< FinishedViewStep >(); )