From 2bb4dd8e22bdee360803ef10f42873da47242b7d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 14 Feb 2020 11:45:45 +0100 Subject: [PATCH] [users] Refactor hostname-guessing --- src/modules/users/UsersPage.cpp | 54 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/modules/users/UsersPage.cpp b/src/modules/users/UsersPage.cpp index a757f5d5a..ac6ff974c 100644 --- a/src/modules/users/UsersPage.cpp +++ b/src/modules/users/UsersPage.cpp @@ -271,6 +271,38 @@ UsersPage::onFullNameTextEdited( const QString& textRef ) 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() @@ -305,27 +337,9 @@ UsersPage::fillSuggestions() { if ( !cleanParts.isEmpty() && !cleanParts.first().isEmpty() ) { - - QString dmiProductName; QString hostnameSuggestion; - // 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)) - { - dmiProductName = QString::fromLocal8Bit( dmiFile.readAll().simplified().data() ) - .toLower().replace(dmirx, " ").remove(' '); - } - if ( !dmiProductName.isEmpty() ) - { - hostnameSuggestion = QString( "%1-%2" ).arg( cleanParts.first() ).arg( dmiProductName ); - } - else - { - hostnameSuggestion = QString( "%1-pc" ).arg( cleanParts.first() ); - } + QString productName = guessProductName(); + hostnameSuggestion = QString( "%1-%2" ).arg( cleanParts.first() ).arg( productName ); if ( HOSTNAME_RX.indexIn( hostnameSuggestion ) != -1 ) { ui->textBoxHostname->setText( hostnameSuggestion );