From bb8f46ed68597e39a7056145bc6cbb66c6030477 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 29 Aug 2019 18:13:48 +0200 Subject: [PATCH] Ref T718, improved country "resolution" in server names/descriptions --- src/blackgui/components/serverlistselector.cpp | 18 ++++++++++++++++-- src/blackmisc/countrylist.cpp | 9 +++++---- src/blackmisc/countrylist.h | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/blackgui/components/serverlistselector.cpp b/src/blackgui/components/serverlistselector.cpp index 8be4f23df..2fe6baee0 100644 --- a/src/blackgui/components/serverlistselector.cpp +++ b/src/blackgui/components/serverlistselector.cpp @@ -95,7 +95,7 @@ namespace BlackGui const CCountry country(this->findCountry(server)); if (country.getName().isEmpty()) { - this->addItem(d); + this->addItem(CIcons::empty16(), d); } else { @@ -135,7 +135,21 @@ namespace BlackGui { if (!CServerListSelector::knowsAllCountries()) { return CCountry(); } static const CCountryList countries(sGui->getWebDataServices()->getCountries()); - return countries.findBestMatchByCountryName(server.getName()); + const CCountry ctryByName = countries.findBestMatchByCountryName(server.getName()); + if (ctryByName.isValid()) { return ctryByName; } + + // own approach, see if description contains a valid countr name + for (const CCountry &testCtry : countries) + { + if (testCtry.getName().isEmpty()) { continue; } + if (server.getDescription().contains(testCtry.getName(), Qt::CaseInsensitive)) + { + return testCtry; + } + } + + const CCountry ctryByDescription = countries.findBestMatchByCountryName(server.getDescription()); + return ctryByDescription; } } // ns } // ns diff --git a/src/blackmisc/countrylist.cpp b/src/blackmisc/countrylist.cpp index 745b9ac78..1dbfb1678 100644 --- a/src/blackmisc/countrylist.cpp +++ b/src/blackmisc/countrylist.cpp @@ -29,17 +29,18 @@ namespace BlackMisc return IDatastoreObjectList::findByKey(isoCode); } - CCountry CCountryList::findBestMatchByCountryName(const QString &countryName) const + CCountry CCountryList::findBestMatchByCountryName(const QString &candidate) const { - if (countryName.isEmpty()) { return CCountry(); } + if (candidate.isEmpty()) { return CCountry(); } thread_local const QRegularExpression reg("^[a-z]+", QRegularExpression::CaseInsensitiveOption); - const QRegularExpressionMatch match = reg.match(countryName); - const QString cn(match.hasMatch() ? match.captured(0) : countryName); + const QRegularExpressionMatch match = reg.match(candidate); + const QString cn(match.hasMatch() ? match.captured(0) : candidate); const CCountryList countries = this->findBy([&](const CCountry & country) { return country.matchesCountryName(cn); }); + if (countries.isEmpty()) { return this->findFirstByAlias(cn); } if (countries.size() < 2) { return countries.frontOrDefault(); } diff --git a/src/blackmisc/countrylist.h b/src/blackmisc/countrylist.h index 02b61c40d..887a8e4bd 100644 --- a/src/blackmisc/countrylist.h +++ b/src/blackmisc/countrylist.h @@ -47,7 +47,7 @@ namespace BlackMisc CCountry findByIsoCode(const QString &isoCode) const; //! Find "best match" by country - CCountry findBestMatchByCountryName(const QString &countryName) const; + CCountry findBestMatchByCountryName(const QString &candidate) const; //! Find first by alias CCountry findFirstByAlias(const QString &alias) const;