diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index 856d27684..ac01f9933 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -23,8 +23,11 @@ #include "GlobalStorage.h" #include "JobQueue.h" #include "utils/Logger.h" +#include "utils/String.h" #include "utils/Variant.h" +#include + Config::Config( QObject* parent ) : QObject( parent ) { @@ -75,11 +78,34 @@ Config::setLoginName( const QString& login ) { if ( login != m_loginName ) { + m_customLoginName = !login.isEmpty(); m_loginName = login; emit loginNameChanged( login ); } } +static const QRegExp USERNAME_RX( "^[a-z_][a-z0-9_-]*[$]?$" ); + +static QString +makeLoginNameSuggestion( const QStringList& parts ) +{ + if ( parts.isEmpty() ) + { + return QString(); + } + + QString usernameSuggestion = parts.first(); + for ( int i = 1; i < parts.length(); ++i ) + { + if ( !parts.value( i ).isEmpty() ) + { + usernameSuggestion.append( parts.value( i ).at( 0 ) ); + } + } + + return USERNAME_RX.indexIn( usernameSuggestion ) != -1 ? usernameSuggestion : QString(); +} + void Config::setUserName( const QString& name ) { @@ -87,6 +113,21 @@ Config::setUserName( const QString& name ) { m_fullName = name; emit userNameChanged( name ); + + // Build login and hostname, if needed + QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive ); + QString cleanName = CalamaresUtils::removeDiacritics( name ).toLower().replace( rx, " " ).simplified(); + QStringList cleanParts = cleanName.split( ' ' ); + + if ( !m_customLoginName ) + { + QString login = makeLoginNameSuggestion( cleanParts ); + if ( !login.isEmpty() && login != m_loginName ) + { + m_loginName = login; + emit loginNameChanged( login ); + } + } } } diff --git a/src/modules/users/Config.h b/src/modules/users/Config.h index 32e51a309..6aed5840f 100644 --- a/src/modules/users/Config.h +++ b/src/modules/users/Config.h @@ -77,7 +77,7 @@ public Q_SLOTS: /// Sets the full name, may guess a loginName void setUserName( const QString& name ); - /// Sets the login name + /// Sets the login name (flags it as "custom") void setLoginName( const QString& login ); signals: @@ -93,6 +93,7 @@ private: QString m_sudoersGroup; QString m_fullName; QString m_loginName; + bool m_customLoginName = false; }; #endif