Merge pull request #1563 from LordTermor/calamares

[users] Full name transliteration support for user creation
This commit is contained in:
Adriaan de Groot 2020-11-10 01:19:44 +01:00 committed by GitHub
commit b503aa645b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 3 deletions

View File

@ -21,6 +21,18 @@ if( LibPWQuality_FOUND )
add_definitions( -DCHECK_PWQUALITY -DHAVE_LIBPWQUALITY )
endif()
find_package( ICU COMPONENTS uc i18n )
set_package_properties(
ICU PROPERTIES
PURPOSE "Transliteration support for full name to username conversion"
)
if( ICU_FOUND )
list( APPEND USER_EXTRA_LIB ICU::uc ICU::i18n )
include_directories( ${ICU_INCLUDE_DIRS} )
add_definitions( -DHAVE_ICU )
endif()
include_directories( ${PROJECT_BINARY_DIR}/src/libcalamaresui )
set( _users_src

View File

@ -24,6 +24,18 @@
#include <QFile>
#include <QRegExp>
#ifdef HAVE_ICU
#include <unicode/translit.h>
#include <unicode/unistr.h>
//Needed for ICU to apply some transliteration ruleset.
//Still needs to be adjusted to fit the needs of the most of users
static const char TRANSLITERATOR_ID[] = "Russian-Latin/BGN;"
"Greek-Latin/UNGEGN;"
"Any-Latin;"
"Latin-ASCII";
#endif
static const QRegExp USERNAME_RX( "^[a-z_][a-z0-9_-]*[$]?$" );
static constexpr const int USERNAME_MAX_LENGTH = 31;
@ -314,6 +326,33 @@ guessProductName()
}
return dmiProduct;
}
#ifdef HAVE_ICU
static QString
transliterate( const QString& input )
{
static auto ue = UErrorCode::U_ZERO_ERROR;
static auto transliterator = std::unique_ptr< icu::Transliterator >(
icu::Transliterator::createInstance( TRANSLITERATOR_ID, UTRANS_FORWARD, ue ) );
if ( ue != UErrorCode::U_ZERO_ERROR )
{
cWarning() << "Can't create transliterator";
//it'll be checked later for non-ASCII characters
return input;
}
icu::UnicodeString transliterable( input.utf16() );
transliterator->transliterate( transliterable );
return QString::fromUtf16( transliterable.getTerminatedBuffer() );
}
#else
static QString
transliterate( const QString& input )
{
return input;
}
#endif
static QString
makeLoginNameSuggestion( const QStringList& parts )
@ -372,8 +411,15 @@ Config::setFullName( const QString& name )
emit fullNameChanged( name );
// Build login and hostname, if needed
QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive );
QString cleanName = CalamaresUtils::removeDiacritics( name ).toLower().replace( rx, " " ).simplified();
static QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive );
QString cleanName = CalamaresUtils::removeDiacritics( transliterate( name ) )
.replace( QRegExp( "[-']" ), "" )
.replace( rx, " " )
.toLower()
.simplified();
QStringList cleanParts = cleanName.split( ' ' );
if ( !m_customLoginName )

View File

@ -28,6 +28,19 @@ if( LibPWQuality_FOUND )
add_definitions( -DCHECK_PWQUALITY -DHAVE_LIBPWQUALITY )
endif()
#needed for ${_users}/Config.cpp
find_package( ICU COMPONENTS uc i18n )
set_package_properties(
ICU PROPERTIES
PURPOSE "Transliteration support for full name to username conversion"
)
if( ICU_FOUND )
list( APPEND USER_EXTRA_LIB ICU::uc ICU::i18n )
include_directories( ${ICU_INCLUDE_DIRS} )
add_definitions( -DHAVE_ICU )
endif()
calamares_add_plugin( usersq
TYPE viewmodule
EXPORT_MACRO PLUGINDLLEXPORT_PRO