From 26b61a4ddb99913c5e6ee60e21ef9f9e79ea30ea Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 2 May 2019 06:16:08 -0400 Subject: [PATCH] [libcalamares] Make RegionZonePair type stronger - Derive from QPair instead of being QPair - Add isValid() for checking - Convenience constructors --- src/libcalamares/geoip/GeoIP.cpp | 4 ++-- src/libcalamares/geoip/GeoIP.h | 20 +++++++++++++++++++- src/libcalamares/geoip/GeoIPJSON.cpp | 2 +- src/libcalamares/geoip/GeoIPXML.cpp | 4 ++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/libcalamares/geoip/GeoIP.cpp b/src/libcalamares/geoip/GeoIP.cpp index 4f2f4ce62..ec0368172 100644 --- a/src/libcalamares/geoip/GeoIP.cpp +++ b/src/libcalamares/geoip/GeoIP.cpp @@ -45,10 +45,10 @@ GeoIP::splitTZString( const QString& tz ) cDebug() << "GeoIP reporting" << timezoneString; QString region = tzParts.takeFirst(); QString zone = tzParts.join( '/' ); - return qMakePair( region, zone ); + return RegionZonePair( region, zone ); } - return qMakePair( QString(), QString() ); + return RegionZonePair( QString(), QString() ); } } // namespace diff --git a/src/libcalamares/geoip/GeoIP.h b/src/libcalamares/geoip/GeoIP.h index bb1f6a8aa..cc022d2de 100644 --- a/src/libcalamares/geoip/GeoIP.h +++ b/src/libcalamares/geoip/GeoIP.h @@ -37,7 +37,25 @@ namespace CalamaresUtils class GeoIP { public: - using RegionZonePair = QPair; + /** @brief A Region, Zone pair of strings + * + * A GeoIP lookup returns a timezone, which is represented as a Region, + * Zone pair of strings (e.g. "Europe" and "Amsterdam"). Generally, + * pasting the strings back together with a "/" is the right thing to + * do. The Zone **may** contain a "/" (e.g. "Kentucky/Monticello"). + */ + class RegionZonePair : public QPair + { + public: + /** @brief Construct from an existing pair. */ + explicit RegionZonePair( const QPair& p ) : QPair(p) { } + /** @brief Construct from two strings, like qMakePair(). */ + RegionZonePair( const QString& region, const QString& zone ) : QPair( region, zone ) { } + /** @brief An invalid zone pair (empty strings). */ + RegionZonePair() : QPair( QString(), QString() ) { } + + bool isValid() const { return !first.isEmpty(); } + } ; virtual ~GeoIP(); diff --git a/src/libcalamares/geoip/GeoIPJSON.cpp b/src/libcalamares/geoip/GeoIPJSON.cpp index 0f33ca37f..81056c536 100644 --- a/src/libcalamares/geoip/GeoIPJSON.cpp +++ b/src/libcalamares/geoip/GeoIPJSON.cpp @@ -73,7 +73,7 @@ GeoIPJSON::processReply( const QByteArray& data ) CalamaresUtils::explainYamlException( e, data, "GeoIP data"); } - return qMakePair( QString(), QString() ); + return RegionZonePair( QString(), QString() ); } } // namespace diff --git a/src/libcalamares/geoip/GeoIPXML.cpp b/src/libcalamares/geoip/GeoIPXML.cpp index e32ff42a4..64f6545d4 100644 --- a/src/libcalamares/geoip/GeoIPXML.cpp +++ b/src/libcalamares/geoip/GeoIPXML.cpp @@ -52,14 +52,14 @@ GeoIPXML::processReply( const QByteArray& data ) // None of them valid cWarning() << "GeopIP XML had no recognizable timezone"; - return qMakePair( QString(), QString() ); + return RegionZonePair( QString(), QString() ); } else { cWarning() << "GeoIP XML data error:" << domError << "(line" << errorLine << errorColumn << ')'; } - return qMakePair( QString(), QString() ); + return RegionZonePair( QString(), QString() ); } } // namespace