From 8a14cc7ffc1616dd254456190ab53e23465c6809 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 27 Jul 2020 16:09:29 +0200 Subject: [PATCH] [users] Move some configuration from Page to Config object - make the HostName textbox just a view on the Config's HostName - make the username and login textboxes view onto Config - query the Config rather than the UI for job data --- src/modules/users/Config.cpp | 2 + src/modules/users/UsersPage.cpp | 135 ++++------------------------ src/modules/users/UsersPage.h | 7 -- src/modules/users/UsersViewStep.cpp | 2 +- src/modules/users/page_usersetup.ui | 4 +- 5 files changed, 24 insertions(+), 126 deletions(-) diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index 9deaef81f..24b14faf1 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -168,6 +168,8 @@ makeHostnameSuggestion( const QStringList& parts ) void Config::setUserName( const QString& name ) { + // TODO: handle "empty" case + // TODO: rename to "FullName" if ( name != m_fullName ) { m_fullName = name; diff --git a/src/modules/users/UsersPage.cpp b/src/modules/users/UsersPage.cpp index 62b8920e7..60afa1bb3 100644 --- a/src/modules/users/UsersPage.cpp +++ b/src/modules/users/UsersPage.cpp @@ -24,9 +24,9 @@ */ #include "UsersPage.h" - #include "ui_page_usersetup.h" +#include "Config.h" #include "CreateUserJob.h" #include "SetHostNameJob.h" #include "SetPasswordJob.h" @@ -34,7 +34,6 @@ #include "GlobalStorage.h" #include "JobQueue.h" #include "Settings.h" - #include "utils/CalamaresUtilsGui.h" #include "utils/Logger.h" #include "utils/Retranslator.h" @@ -94,8 +93,6 @@ UsersPage::UsersPage( Config* config, QWidget* parent ) // Connect signals and slots connect( ui->textBoxFullName, &QLineEdit::textEdited, this, &UsersPage::onFullNameTextEdited ); - connect( ui->textBoxUsername, &QLineEdit::textEdited, this, &UsersPage::onUsernameTextEdited ); - connect( ui->textBoxHostname, &QLineEdit::textEdited, this, &UsersPage::onHostnameTextEdited ); connect( ui->textBoxUserPassword, &QLineEdit::textChanged, this, &UsersPage::onPasswordTextChanged ); connect( ui->textBoxUserVerifiedPassword, &QLineEdit::textChanged, this, &UsersPage::onPasswordTextChanged ); connect( ui->textBoxRootPassword, &QLineEdit::textChanged, this, &UsersPage::onRootPasswordTextChanged ); @@ -124,8 +121,13 @@ UsersPage::UsersPage( Config* config, QWidget* parent ) checkReady( isReady() ); } ); - m_customUsername = false; - m_customHostname = false; + connect( ui->textBoxHostName, &QLineEdit::textEdited, config, &Config::setHostName); + connect( config, &Config::hostNameChanged, ui->textBoxHostName, &QLineEdit::setText ); + connect( config, &Config::hostNameChanged, this, &UsersPage::validateHostnameText ); + + connect( ui->textBoxLoginName, &QLineEdit::textEdited, config, &Config::setLoginName ); + connect( config, &Config::loginNameChanged, ui->textBoxLoginName, &QLineEdit::setText ); + connect( config, &Config::loginNameChanged, this, &UsersPage::validateUsernameText ); setWriteRootPassword( true ); ui->checkBoxReusePassword->setChecked( true ); @@ -147,13 +149,13 @@ UsersPage::retranslate() ui->retranslateUi( this ); if ( Calamares::Settings::instance()->isSetupMode() ) { - ui->textBoxUsername->setToolTip( tr( "If more than one person will " + ui->textBoxLoginName->setToolTip( tr( "If more than one person will " "use this computer, you can create multiple " "accounts after setup." ) ); } else { - ui->textBoxUsername->setToolTip( tr( "If more than one person will " + ui->textBoxLoginName->setToolTip( tr( "If more than one person will " "use this computer, you can create multiple " "accounts after installation." ) ); } @@ -177,12 +179,6 @@ UsersPage::isReady() return readyFields && m_readyRootPassword; } -QString -UsersPage::getHostname() const -{ - return ui->textBoxHostname->text(); -} - QString UsersPage::getRootPassword() const { @@ -206,7 +202,7 @@ UsersPage::getRootPassword() const QPair< QString, QString > UsersPage::getUserPassword() const { - return QPair< QString, QString >( ui->textBoxUsername->text(), ui->textBoxUserPassword->text() ); + return QPair< QString, QString >( m_config->loginName(), ui->textBoxUserPassword->text() ); } QList< Calamares::job_ptr > @@ -221,9 +217,9 @@ UsersPage::createJobs( const QStringList& defaultGroupsList ) Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); Calamares::Job* j; - j = new CreateUserJob( ui->textBoxUsername->text(), - ui->textBoxFullName->text().isEmpty() ? ui->textBoxUsername->text() - : ui->textBoxFullName->text(), + j = new CreateUserJob( m_config->loginName(), + m_config->userName().isEmpty() ? m_config->loginName() + : m_config->userName(), ui->checkBoxAutoLogin->isChecked(), defaultGroupsList ); list.append( Calamares::job_ptr( j ) ); @@ -232,13 +228,13 @@ UsersPage::createJobs( const QStringList& defaultGroupsList ) { gs->insert( "reuseRootPassword", ui->checkBoxReusePassword->isChecked() ); } - gs->insert( "hostname", ui->textBoxHostname->text() ); + gs->insert( "hostname", m_config->hostName() ); if ( ui->checkBoxAutoLogin->isChecked() ) { - gs->insert( "autologinUser", ui->textBoxUsername->text() ); + gs->insert( "autologinUser", m_config->loginName() ); } - gs->insert( "username", ui->textBoxUsername->text() ); + gs->insert( "username", m_config->loginName() ); gs->insert( "password", CalamaresUtils::obscure( ui->textBoxUserPassword->text() ) ); return list; @@ -269,6 +265,7 @@ UsersPage::onFullNameTextEdited( const QString& textRef ) { ui->labelFullNameError->clear(); ui->labelFullName->clear(); +#if 0 if ( !m_customUsername ) { ui->textBoxUsername->clear(); @@ -277,6 +274,7 @@ UsersPage::onFullNameTextEdited( const QString& textRef ) { ui->textBoxHostname->clear(); } +#endif m_readyFullName = false; } else @@ -284,97 +282,10 @@ UsersPage::onFullNameTextEdited( const QString& textRef ) ui->labelFullName->setPixmap( CalamaresUtils::defaultPixmap( CalamaresUtils::Yes, CalamaresUtils::Original, ui->labelFullName->size() ) ); m_readyFullName = true; - fillSuggestions(); } checkReady( isReady() ); } -/** @brief Guess the machine's name - * - * If there is DMI data, use that; otherwise, just call the machine "-pc". - * Reads the DMI data just once. - */ -static QString -guessProductName() -{ - static bool tried = false; - static QString dmiProduct; - - if ( !tried ) - { - // yes validateHostnameText() but these files can be a mess - QRegExp dmirx( "[^a-zA-Z0-9]", Qt::CaseInsensitive ); - QFile dmiFile( QStringLiteral( "/sys/devices/virtual/dmi/id/product_name" ) ); - - if ( dmiFile.exists() && dmiFile.open( QIODevice::ReadOnly ) ) - { - dmiProduct = QString::fromLocal8Bit( dmiFile.readAll().simplified().data() ) - .toLower() - .replace( dmirx, " " ) - .remove( ' ' ); - } - if ( dmiProduct.isEmpty() ) - { - dmiProduct = QStringLiteral( "-pc" ); - } - tried = true; - } - return dmiProduct; -} - -void -UsersPage::fillSuggestions() -{ - QString fullName = ui->textBoxFullName->text(); - QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive ); - QString cleanName = CalamaresUtils::removeDiacritics( fullName ).toLower().replace( rx, " " ).simplified(); - QStringList cleanParts = cleanName.split( ' ' ); - - if ( !m_customUsername ) - { - if ( !cleanParts.isEmpty() && !cleanParts.first().isEmpty() ) - { - QString usernameSuggestion = cleanParts.first(); - for ( int i = 1; i < cleanParts.length(); ++i ) - { - if ( !cleanParts.value( i ).isEmpty() ) - { - usernameSuggestion.append( cleanParts.value( i ).at( 0 ) ); - } - } - if ( USERNAME_RX.indexIn( usernameSuggestion ) != -1 ) - { - ui->textBoxUsername->setText( usernameSuggestion ); - validateUsernameText( usernameSuggestion ); - m_customUsername = false; - } - } - } - - if ( !m_customHostname ) - { - if ( !cleanParts.isEmpty() && !cleanParts.first().isEmpty() ) - { - QString hostnameSuggestion; - QString productName = guessProductName(); - hostnameSuggestion = QString( "%1-%2" ).arg( cleanParts.first() ).arg( productName ); - if ( HOSTNAME_RX.indexIn( hostnameSuggestion ) != -1 ) - { - ui->textBoxHostname->setText( hostnameSuggestion ); - validateHostnameText( hostnameSuggestion ); - m_customHostname = false; - } - } - } -} - - -void -UsersPage::onUsernameTextEdited( const QString& textRef ) -{ - m_customUsername = true; - validateUsernameText( textRef ); -} void @@ -427,14 +338,6 @@ UsersPage::validateUsernameText( const QString& textRef ) } -void -UsersPage::onHostnameTextEdited( const QString& textRef ) -{ - m_customHostname = true; - validateHostnameText( textRef ); -} - - void UsersPage::validateHostnameText( const QString& textRef ) { diff --git a/src/modules/users/UsersPage.h b/src/modules/users/UsersPage.h index a13886de6..ac5701b2d 100644 --- a/src/modules/users/UsersPage.h +++ b/src/modules/users/UsersPage.h @@ -65,8 +65,6 @@ public: */ void addPasswordCheck( const QString& key, const QVariant& value ); - ///@brief Hostname as entered / auto-filled - QString getHostname() const; ///@brief Root password, depends on settings, may be empty QString getRootPassword() const; ///@brief User name and password @@ -74,10 +72,7 @@ public: protected slots: void onFullNameTextEdited( const QString& ); - void fillSuggestions(); - void onUsernameTextEdited( const QString& ); void validateUsernameText( const QString& ); - void onHostnameTextEdited( const QString& ); void validateHostnameText( const QString& ); void onPasswordTextChanged( const QString& ); void onRootPasswordTextChanged( const QString& ); @@ -104,9 +99,7 @@ private: bool m_readyFullName; bool m_readyUsername; - bool m_customUsername; bool m_readyHostname; - bool m_customHostname; bool m_readyPassword; bool m_readyRootPassword; diff --git a/src/modules/users/UsersViewStep.cpp b/src/modules/users/UsersViewStep.cpp index 745163c2c..be6d61878 100644 --- a/src/modules/users/UsersViewStep.cpp +++ b/src/modules/users/UsersViewStep.cpp @@ -153,7 +153,7 @@ UsersViewStep::onLeave() j = new SetPasswordJob( "root", m_widget->getRootPassword() ); m_jobs.append( Calamares::job_ptr( j ) ); - j = new SetHostNameJob( m_widget->getHostname(), m_actions ); + j = new SetHostNameJob( m_config->hostName(), m_actions ); m_jobs.append( Calamares::job_ptr( j ) ); } diff --git a/src/modules/users/page_usersetup.ui b/src/modules/users/page_usersetup.ui index b778647d8..4aefa9981 100644 --- a/src/modules/users/page_usersetup.ui +++ b/src/modules/users/page_usersetup.ui @@ -127,7 +127,7 @@ - + 0 @@ -226,7 +226,7 @@ - + 0