diff --git a/src/modules/locale/GeoIP.cpp b/src/modules/locale/GeoIP.cpp index 1bed7d3f4..f11a44db7 100644 --- a/src/modules/locale/GeoIP.cpp +++ b/src/modules/locale/GeoIP.cpp @@ -20,6 +20,11 @@ #include "utils/Logger.h" +GeoIP::GeoIP(const QString& e) + : m_element( e ) +{ +} + GeoIP::~GeoIP() { } diff --git a/src/modules/locale/GeoIP.h b/src/modules/locale/GeoIP.h index c84a9b5e4..6088c8a45 100644 --- a/src/modules/locale/GeoIP.h +++ b/src/modules/locale/GeoIP.h @@ -32,8 +32,9 @@ class QByteArray; * and can handle the data returned from its interpretation of that * configured URL, returning a region and zone. */ -struct GeoIP +class GeoIP { +public: using RegionZonePair = QPair; virtual ~GeoIP(); @@ -51,6 +52,11 @@ struct GeoIP /** @brief Splits a region/zone string into a pair. */ static RegionZonePair splitTZString( const QString& s ); + +protected: + GeoIP( const QString& e = QString() ); + + QString m_element; // string for selecting from data } ; #endif diff --git a/src/modules/locale/GeoIPJSON.cpp b/src/modules/locale/GeoIPJSON.cpp index 78f6c67a7..aa5a2b411 100644 --- a/src/modules/locale/GeoIPJSON.cpp +++ b/src/modules/locale/GeoIPJSON.cpp @@ -26,6 +26,17 @@ #include +GeoIPJSON::GeoIPJSON(const QString& attribute) + : GeoIP( attribute ) +{ +} + +GeoIPJSON::GeoIPJSON() + : GeoIPJSON( QLatin1Literal( "time_zone" ) ) +{ +} + + GeoIP::RegionZonePair GeoIPJSON::processReply( const QByteArray& data ) { @@ -39,12 +50,14 @@ GeoIPJSON::processReply( const QByteArray& data ) var.type() == QVariant::Map ) { QVariantMap map = var.toMap(); - if ( map.contains( "time_zone" ) && - !map.value( "time_zone" ).toString().isEmpty() ) + if ( map.contains( m_element ) && + !map.value( m_element ).toString().isEmpty() ) { - return splitTZString( map.value( "time_zone" ).toString() ); + return splitTZString( map.value( m_element ).toString() ); } } + else + cWarning() << "Invalid YAML data for GeoIPJSON"; } catch ( YAML::Exception& e ) { diff --git a/src/modules/locale/GeoIPJSON.h b/src/modules/locale/GeoIPJSON.h index 26a0560f6..dbe1eeffa 100644 --- a/src/modules/locale/GeoIPJSON.h +++ b/src/modules/locale/GeoIPJSON.h @@ -28,8 +28,12 @@ * * The data is assumed to be in JSON format with a time_zone attribute. */ -struct GeoIPJSON : public GeoIP +class GeoIPJSON : public GeoIP { +public: + explicit GeoIPJSON( const QString& attribute ); + explicit GeoIPJSON(); + virtual RegionZonePair processReply( const QByteArray& ); } ; diff --git a/src/modules/locale/GeoIPTests.cpp b/src/modules/locale/GeoIPTests.cpp index d4522836c..4848f1a2d 100644 --- a/src/modules/locale/GeoIPTests.cpp +++ b/src/modules/locale/GeoIPTests.cpp @@ -76,6 +76,9 @@ GeoIPTests::testJSONbad() tz = handler.processReply( "404 Forbidden" ); QCOMPARE( tz.first, QString() ); + + tz = handler.processReply( "{ time zone = 'America/LosAngeles'}" ); + QCOMPARE( tz.first, QString() ); } diff --git a/src/modules/locale/GeoIPXML.cpp b/src/modules/locale/GeoIPXML.cpp index 28761e163..a2117b2f2 100644 --- a/src/modules/locale/GeoIPXML.cpp +++ b/src/modules/locale/GeoIPXML.cpp @@ -23,6 +23,17 @@ #include #include +GeoIPXML::GeoIPXML( const QString& element ) + : GeoIP( element ) +{ +} + +GeoIPXML::GeoIPXML() + : GeoIPXML( QLatin1Literal( "TimeZone" ) ) +{ +} + + GeoIP::RegionZonePair GeoIPXML::processReply( const QByteArray& data ) { @@ -32,7 +43,7 @@ GeoIPXML::processReply( const QByteArray& data ) QDomDocument doc; if ( doc.setContent( data, false, &domError, &errorLine, &errorColumn ) ) { - const auto tzElements = doc.elementsByTagName( "TimeZone" ); + const auto tzElements = doc.elementsByTagName( m_element ); cDebug() << "GeoIP found" << tzElements.length() << "elements"; for ( int it = 0; it < tzElements.length(); ++it ) { diff --git a/src/modules/locale/GeoIPXML.h b/src/modules/locale/GeoIPXML.h index adffc5bca..bda359485 100644 --- a/src/modules/locale/GeoIPXML.h +++ b/src/modules/locale/GeoIPXML.h @@ -28,8 +28,14 @@ * element, which contains the text (string) for the region/zone. This * format is expected by, e.g. the Ubiquity installer. */ -struct GeoIPXML : public GeoIP +class GeoIPXML : public GeoIP { +public: + /** @brief Configure the element name which is selected. */ + explicit GeoIPXML( const QString& element ); + /** @brief Use default TimeZone element. */ + explicit GeoIPXML(); + virtual RegionZonePair processReply( const QByteArray& ); } ;