From 9d6d8ecaea64d6476776b547ad737d545419ee3c Mon Sep 17 00:00:00 2001
From: Adriaan de Groot
Date: Tue, 23 Feb 2021 15:03:16 +0100
Subject: [PATCH] [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.
---
src/modules/finished/Config.cpp | 47 +++++++
src/modules/finished/Config.h | 17 ++-
src/modules/finished/FinishedPage.cpp | 147 +++++++++++-----------
src/modules/finished/FinishedPage.h | 13 +-
src/modules/finished/FinishedViewStep.cpp | 29 +++--
5 files changed, 159 insertions(+), 94 deletions(-)
diff --git a/src/modules/finished/Config.cpp b/src/modules/finished/Config.cpp
index 7a84e8f22..a612661a8 100644
--- a/src/modules/finished/Config.cpp
+++ b/src/modules/finished/Config.cpp
@@ -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 )
{
diff --git a/src/modules/finished/Config.h b/src/modules/finished/Config.h
index 9b8c5d8d3..32cc7bf22 100644
--- a/src/modules/finished/Config.h
+++ b/src/modules/finished/Config.h
@@ -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;
};
diff --git a/src/modules/finished/FinishedPage.cpp b/src/modules/finished/FinishedPage.cpp
index 118ee96bf..5f021dd3d 100644
--- a/src/modules/finished/FinishedPage.cpp
+++ b/src/modules/finished/FinishedPage.cpp
@@ -27,10 +27,9 @@
#include
-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( "All done.
"
- "%1 has been set up on your computer.
"
- "You may now start using your new system." )
- .arg( branding->versionedName() ) );
- ui->restartCheckBox->setToolTip( tr( ""
- "When this box is checked, your system will "
- "restart immediately when you click on "
- "Done "
- "or close the setup program.
" ) );
- } else {
- ui->mainText->setText( tr( "All done.
"
- "%1 has been installed on your computer.
"
- "You may now restart into your new system, or continue "
- "using the %2 Live environment." )
- .arg( branding->versionedName(), branding->productName() ) );
- ui->restartCheckBox->setToolTip( tr( "
"
- "When this box is checked, your system will "
- "restart immediately when you click on "
- "Done "
- "or close the installer.
" ) );
- } )
-}
+ connect( config, &Config::restartModeChanged, [this]( Config::RestartMode mode ) {
+ using Mode = Config::RestartMode;
-
-void
-FinishedPage::setRestart( 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 } );
- }
+ ui->restartCheckBox->setVisible( mode != Mode::Never );
+ ui->restartCheckBox->setEnabled( mode != Mode::Always );
+ } );
+ 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 )
{
- const auto* branding = Calamares::Branding::instance();
- Q_UNUSED( details )
- if ( Calamares::Settings::instance()->isSetupMode() )
- ui->mainText->setText( tr( "Setup Failed
"
- "%1 has not been set up on your computer.
"
- "The error message was: %2." )
- .arg( branding->versionedName() )
- .arg( message ) );
- else
- ui->mainText->setText( tr( "Installation Failed
"
- "%1 has not been installed on your computer.
"
- "The error message was: %2." )
- .arg( branding->versionedName() )
- .arg( message ) );
- setRestart( Config::RestartMode::Never );
+ m_failure = !message.isEmpty() ? message : details;
+ retranslate();
+}
+
+void
+FinishedPage::retranslate()
+{
+
+ const auto* branding = Calamares::Branding::instance();
+
+ ui->retranslateUi( this );
+ if ( !m_failure.has_value() )
+ {
+ if ( Calamares::Settings::instance()->isSetupMode() )
+ {
+ ui->mainText->setText( tr( "All done.
"
+ "%1 has been set up on your computer.
"
+ "You may now start using your new system." )
+ .arg( branding->versionedName() ) );
+ ui->restartCheckBox->setToolTip( tr( ""
+ "When this box is checked, your system will "
+ "restart immediately when you click on "
+ "Done "
+ "or close the setup program.
" ) );
+ }
+ else
+ {
+ ui->mainText->setText( tr( "All done.
"
+ "%1 has been installed on your computer.
"
+ "You may now restart into your new system, or continue "
+ "using the %2 Live environment." )
+ .arg( branding->versionedName(), branding->productName() ) );
+ ui->restartCheckBox->setToolTip( tr( ""
+ "When this box is checked, your system will "
+ "restart immediately when you click on "
+ "Done "
+ "or close the installer.
" ) );
+ }
+ }
+ else
+ {
+ const QString message = m_failure.value();
+
+ if ( Calamares::Settings::instance()->isSetupMode() )
+ {
+ ui->mainText->setText( tr( "Setup Failed
"
+ "%1 has not been set up on your computer.
"
+ "The error message was: %2." )
+ .arg( branding->versionedName() )
+ .arg( message ) );
+ }
+ else
+ {
+ ui->mainText->setText( tr( "Installation Failed
"
+ "%1 has not been installed on your computer.
"
+ "The error message was: %2." )
+ .arg( branding->versionedName() )
+ .arg( message ) );
+ }
+ }
}
diff --git a/src/modules/finished/FinishedPage.h b/src/modules/finished/FinishedPage.h
index 30df5f8c3..4e312ab8b 100644
--- a/src/modules/finished/FinishedPage.h
+++ b/src/modules/finished/FinishedPage.h
@@ -16,6 +16,8 @@
#include
+#include
+
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
diff --git a/src/modules/finished/FinishedViewStep.cpp b/src/modules/finished/FinishedViewStep.cpp
index 1469262a0..48e2c47d5 100644
--- a/src/modules/finished/FinishedViewStep.cpp
+++ b/src/modules/finished/FinishedViewStep.cpp
@@ -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 >(); )