diff --git a/src/modules/users/CMakeLists.txt b/src/modules/users/CMakeLists.txt index fdae38440..56a61093b 100644 --- a/src/modules/users/CMakeLists.txt +++ b/src/modules/users/CMakeLists.txt @@ -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( JOB_SRC diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index f8904b9d4..186dd0ade 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -23,6 +23,11 @@ #include #include +#ifdef HAVE_ICU +#include +#include +#endif + static const QRegExp USERNAME_RX( "^[a-z_][a-z0-9_-]*[$]?$" ); static constexpr const int USERNAME_MAX_LENGTH = 31; @@ -279,6 +284,30 @@ guessProductName() } return dmiProduct; } +#ifdef HAVE_ICU +static QString +transliterate( const QString& input ) +{ + static UErrorCode ue = UErrorCode::U_ZERO_ERROR; + static auto transliterator = std::unique_ptr< icu::Transliterator >( + icu::Transliterator::createInstance( "Any-Latin; Latin-ASCII", UTRANS_FORWARD, ue ) + ); + + if(ue!=0){ + cWarning() << "Can't create transliterator"; + + //it'll be checked later for non-ASCII characters + return input; + } + + auto transliterable = icu::UnicodeString( input.toUtf8().data() ); + + transliterator->transliterate( transliterable ); + + return QString::fromUtf16( transliterable.getTerminatedBuffer() ); + +} +#endif static QString makeLoginNameSuggestion( const QStringList& parts ) @@ -337,8 +366,16 @@ Config::setFullName( const QString& name ) emit fullNameChanged( name ); // Build login and hostname, if needed + QString cleanName = CalamaresUtils::removeDiacritics( name ).toLower().simplified(); + +#ifdef HAVE_ICU + cleanName = transliterate(cleanName); +#else QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive ); - QString cleanName = CalamaresUtils::removeDiacritics( name ).toLower().replace( rx, " " ).simplified(); + cleanName.replace( rx, " " ); +#endif + + QStringList cleanParts = cleanName.split( ' ' ); if ( !m_customLoginName ) diff --git a/src/modules/usersq/CMakeLists.txt b/src/modules/usersq/CMakeLists.txt index 26c270bfb..37b4416ad 100644 --- a/src/modules/usersq/CMakeLists.txt +++ b/src/modules/usersq/CMakeLists.txt @@ -29,6 +29,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() + calamares_add_plugin( usersq TYPE viewmodule EXPORT_MACRO PLUGINDLLEXPORT_PRO