From ee4a0f1cbabef0ac3683ab2235e7f66e590e8510 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Wed, 13 Aug 2014 17:16:54 +0200 Subject: [PATCH] Add support for full name, and user/host autocompletion and validation. --- src/modules/users/UsersPage.cpp | 114 +++++++++++++++++++++++++++++--- src/modules/users/UsersPage.h | 15 ++++- 2 files changed, 117 insertions(+), 12 deletions(-) diff --git a/src/modules/users/UsersPage.cpp b/src/modules/users/UsersPage.cpp index 5b55b359e..b4aa14673 100644 --- a/src/modules/users/UsersPage.cpp +++ b/src/modules/users/UsersPage.cpp @@ -26,6 +26,8 @@ #include "SetPasswordJob.h" #include "JobQueue.h" #include "GlobalStorage.h" +#include "utils/Logger.h" +#include "utils/CalamaresUtils.h" #include #include @@ -38,6 +40,7 @@ UsersPage::UsersPage( QWidget* parent ) : QWidget( parent ) , ui( new Ui::Page_UserSetup ) + , m_readyFullName( false ) , m_readyUsername( false ) , m_readyHostname( false ) , m_readyPassword( false ) @@ -46,10 +49,12 @@ UsersPage::UsersPage( QWidget* parent ) ui->setupUi( this ); // Connect signals and slots - connect( ui->textBoxUsername, &QLineEdit::textChanged, - this, &UsersPage::onUsernameTextChanged ); - connect( ui->textBoxHostname, &QLineEdit::textChanged, - this, &UsersPage::onHostnameTextChanged ); + 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, @@ -58,6 +63,9 @@ UsersPage::UsersPage( QWidget* parent ) this, &UsersPage::onRootPasswordTextChanged ); connect( ui->textBoxVerifiedRootPassword, &QLineEdit::textChanged, this, &UsersPage::onRootPasswordTextChanged ); + + m_customUsername = false; + m_customHostname = false; } @@ -70,7 +78,11 @@ UsersPage::~UsersPage() bool UsersPage::isReady() { - return m_readyHostname && m_readyPassword && m_readyRootPassword && m_readyUsername; + return m_readyFullName && + m_readyHostname && + m_readyPassword && + m_readyRootPassword && + m_readyUsername; } @@ -105,10 +117,84 @@ UsersPage::createJobs() void -UsersPage::onUsernameTextChanged( const QString& textRef ) +UsersPage::onFullNameTextEdited( const QString &textRef ) { - QString text = textRef; - QRegExp rx( "^[a-z][-a-z0-9_]*\\$" ); + if ( textRef.isEmpty() ) + { + ui->labelFullNameError->clear(); + ui->labelFullName->clear(); + if ( !m_customUsername ) + ui->textBoxUsername->clear(); + if ( !m_customHostname ) + ui->textBoxHostname->clear(); + } + else + { + ui->labelFullName->setPixmap( QPixmap( ":/images/valid.png" ) ); + m_readyFullName = true; + fillSuggestions(); + } + checkReady( isReady() ); +} + + +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 ( m_usernameRx.indexIn( usernameSuggestion ) != -1 ) + { + ui->textBoxUsername->setText( usernameSuggestion ); + validateUsernameText( usernameSuggestion ); + m_customUsername = false; + } + } + } + + if ( !m_customHostname ) + { + if ( !cleanParts.isEmpty() && !cleanParts.first().isEmpty() ) + { + QString hostnameSuggestion = QString( "%1-pc" ).arg( cleanParts.first() ); + if ( m_hostnameRx.indexIn( hostnameSuggestion ) != -1 ) + { + ui->textBoxHostname->setText( hostnameSuggestion ); + validateHostnameText( hostnameSuggestion ); + m_customHostname = false; + } + } + } +} + + +void +UsersPage::onUsernameTextEdited( const QString& textRef ) +{ + m_customUsername = true; + validateUsernameText( textRef ); +} + + +void +UsersPage::validateUsernameText( const QString& textRef ) +{ + QString text( textRef ); + QRegExp rx( m_usernameRx ); QRegExpValidator val( rx ); int pos = -1; @@ -144,10 +230,18 @@ UsersPage::onUsernameTextChanged( const QString& textRef ) void -UsersPage::onHostnameTextChanged( const QString& textRef ) +UsersPage::onHostnameTextEdited( const QString& textRef ) +{ + m_customHostname = true; + validateHostnameText( textRef ); +} + + +void +UsersPage::validateHostnameText( const QString& textRef ) { QString text = textRef; - QRegExp rx( "^[a-zA-Z][-a-zA-Z0-9_]*\\$" ); + QRegExp rx( m_hostnameRx ); QRegExpValidator val( rx ); int pos = -1; diff --git a/src/modules/users/UsersPage.h b/src/modules/users/UsersPage.h index e25e5266b..659b44445 100644 --- a/src/modules/users/UsersPage.h +++ b/src/modules/users/UsersPage.h @@ -43,8 +43,12 @@ public: QList< Calamares::job_ptr > createJobs(); protected slots: - void onUsernameTextChanged( const QString& ); - void onHostnameTextChanged( const QString& ); + 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& ); @@ -53,8 +57,15 @@ signals: private: Ui::Page_UserSetup* ui; + + const QRegExp m_usernameRx = QRegExp( "^[a-z_][a-z0-9_-]*[$]?$" ); + const QRegExp m_hostnameRx = QRegExp( "^[a-zA-Z][-a-zA-Z0-9_]*$" ); + + bool m_readyFullName; bool m_readyUsername; + bool m_customUsername; bool m_readyHostname; + bool m_customHostname; bool m_readyPassword; bool m_readyRootPassword;