[users] Full name transliteration support for user creation

This commit is contained in:
Artem Grinev 2020-11-01 19:24:42 -05:00
parent 0bad5469fc
commit 48eae463cc
3 changed files with 62 additions and 1 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( JOB_SRC

View File

@ -23,6 +23,11 @@
#include <QFile>
#include <QRegExp>
#ifdef HAVE_ICU
#include <unicode/translit.h>
#include <unicode/unistr.h>
#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 )

View File

@ -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