diff --git a/src/main.cpp b/src/main.cpp index 9cad4d808d..49561b4b04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1075,7 +1075,7 @@ QString UsageArguments ( char** argv ) " -L, --licence show an agreement window before users can connect\n" " -m, --htmlstatus enable HTML status file, set file name\n" " -o, --serverinfo registration info for this Server. Format:\n" - " [name];[city];[country as Qt5 QLocale ID]\n" + " [name];[city];[country as two-letter ISO country code or Qt5 QLocale ID]\n" " --serverpublicip public IP address for this Server. Needed when\n" " registering with a server list hosted\n" " behind the same NAT\n" diff --git a/src/serverlist.cpp b/src/serverlist.cpp index e03b6f092b..52888bdd33 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -205,16 +205,36 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, // [this server country as QLocale ID] bool ok; const int iCountry = slServInfoSeparateParams[2].toInt ( &ok ); - if ( ok && iCountry >= 0 && CLocale::IsCountryCodeSupported ( iCountry ) ) + if ( ok ) { - // Convert from externally-supplied format ("wire format", Qt5 codes) to - // native format. On Qt5 builds, this is a noop, on Qt6 builds, a conversion - // takes place. - // We try to do such conversions at the outer-most interface which is capable of doing it. - // Although the value comes from src/main -> src/server, this very place is - // the first where we have access to the parsed country code: - ThisServerListEntry.eCountry = CLocale::WireFormatCountryCodeToQtCountry ( iCountry ); + if ( iCountry >= 0 && CLocale::IsCountryCodeSupported ( iCountry ) ) + { + // Convert from externally-supplied format ("wire format", Qt5 codes) to + // native format. On Qt5 builds, this is a noop, on Qt6 builds, a conversion + // takes place. + // We try to do such conversions at the outer-most interface which is capable of doing it. + // Although the value comes from src/main -> src/server, this very place is + // the first where we have access to the parsed country code: + ThisServerListEntry.eCountry = CLocale::WireFormatCountryCodeToQtCountry ( iCountry ); + } + } + else + { + QLocale::Country qlCountry = CLocale::GetCountryCodeByTwoLetterCode ( slServInfoSeparateParams[2] ); + if ( qlCountry != QLocale::AnyCountry ) + { + ThisServerListEntry.eCountry = qlCountry; + } } + qInfo() << qUtf8Printable ( QString ( "Using server info: name = \"%1\", city = \"%2\", country/region = \"%3\" (%4)" ) + .arg ( ThisServerListEntry.strName ) + .arg ( ThisServerListEntry.strCity ) + .arg ( slServInfoSeparateParams[2] ) + .arg ( QLocale::countryToString ( ThisServerListEntry.eCountry ) ) ); + } + else + { + qWarning() << "Ignoring invalid serverinfo, please verify the parameter syntax."; } // per definition, the very first entry is this server and this entry will diff --git a/src/util.cpp b/src/util.cpp index 6481c2e90a..42a653b39f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1436,6 +1436,31 @@ bool CLocale::IsCountryCodeSupported ( unsigned short iCountryCode ) #endif } +QLocale::Country CLocale::GetCountryCodeByTwoLetterCode ( QString sTwoLetterCode ) +{ +#if QT_VERSION >= QT_VERSION_CHECK( 6, 2, 0 ) + return QLocale::codeToTerritory ( sTwoLetterCode ); +#else + QList vLocaleList = QLocale::matchingLocales ( QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry ); + QStringList vstrLocParts; + + // Qt < 6.2 does not support lookups from two-letter iso codes to + // QLocale::Country. Therefore, we have to loop over all supported + // locales and perform the matching ourselves. + // In the future, QLocale::codeToTerritory can be used. + foreach ( const QLocale qLocale, vLocaleList ) + { + QStringList vstrLocParts = qLocale.name().split ( "_" ); + + if ( vstrLocParts.size() >= 2 && vstrLocParts.at ( 1 ).toLower() == sTwoLetterCode ) + { + return qLocale.country(); + } + } + return QLocale::AnyCountry; +#endif +} + QString CLocale::GetCountryFlagIconsResourceReference ( const QLocale::Country eCountry /* Qt-native value */ ) { QString strReturn = ""; diff --git a/src/util.h b/src/util.h index 8ed8725fff..4a711e9b0b 100644 --- a/src/util.h +++ b/src/util.h @@ -827,6 +827,7 @@ class CLocale static QLocale::Country WireFormatCountryCodeToQtCountry ( unsigned short iCountryCode ); static unsigned short QtCountryToWireFormatCountryCode ( const QLocale::Country eCountry ); static bool IsCountryCodeSupported ( unsigned short iCountryCode ); + static QLocale::Country GetCountryCodeByTwoLetterCode ( QString sTwoLetterCode ); #if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) // ./tools/qt5-to-qt6-country-code-table.py generates these lists: constexpr int const static wireFormatToQt6Table[] = {