diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index d1dbd81b2..2173c825c 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -70,19 +70,19 @@ if( WITH_PYTHON ) endif() set( geoipSources - geoip/GeoIP.cpp - geoip/GeoIPHandler.cpp - geoip/GeoIPJSON.cpp + geoip/Interface.cpp + geoip/Handler.cpp ) +set( geoipImplementations geoip/GeoIPJSON.cpp ) set( geoip_libs ) find_package(Qt5 COMPONENTS Xml) if( Qt5Xml_FOUND ) - list( APPEND geoipSources geoip/GeoIPXML.cpp ) + list( APPEND geoipImplementations geoip/GeoIPXML.cpp ) list( APPEND geoip_libs Qt5::Network Qt5::Xml ) endif() -add_library( calamares SHARED ${libSources} ${kdsagSources} ${utilsSources} ${geoipSources} ) +add_library( calamares SHARED ${libSources} ${kdsagSources} ${utilsSources} ${geoipSources} ${geoipImplementations} ) set_target_properties( calamares PROPERTIES VERSION ${CALAMARES_VERSION_SHORT} diff --git a/src/libcalamares/geoip/GeoIPJSON.cpp b/src/libcalamares/geoip/GeoIPJSON.cpp index 81056c536..801031c9e 100644 --- a/src/libcalamares/geoip/GeoIPJSON.cpp +++ b/src/libcalamares/geoip/GeoIPJSON.cpp @@ -25,11 +25,11 @@ #include -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { GeoIPJSON::GeoIPJSON(const QString& attribute) - : GeoIP( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute ) + : Interface( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute ) { } diff --git a/src/libcalamares/geoip/GeoIPJSON.h b/src/libcalamares/geoip/GeoIPJSON.h index 10a38c48d..355fa0739 100644 --- a/src/libcalamares/geoip/GeoIPJSON.h +++ b/src/libcalamares/geoip/GeoIPJSON.h @@ -19,9 +19,9 @@ #ifndef GEOIP_GEOIPJSON_H #define GEOIP_GEOIPJSON_H -#include "GeoIP.h" +#include "Interface.h" -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { /** @brief GeoIP lookup for services that return JSON. * @@ -29,8 +29,10 @@ namespace CalamaresUtils * (e.g. using the FreeGeoIP.net service), or similar. * * The data is assumed to be in JSON format with a time_zone attribute. + * + * @note This class is an implementation detail. */ -class GeoIPJSON : public GeoIP +class GeoIPJSON : public Interface { public: /** @brief Configure the attribute name which is selected. diff --git a/src/libcalamares/geoip/GeoIPTests.cpp b/src/libcalamares/geoip/GeoIPTests.cpp index c84eb4713..f72388892 100644 --- a/src/libcalamares/geoip/GeoIPTests.cpp +++ b/src/libcalamares/geoip/GeoIPTests.cpp @@ -31,9 +31,7 @@ QTEST_GUILESS_MAIN( GeoIPTests ) -using CalamaresUtils::GeoIP; -using CalamaresUtils::GeoIPJSON; -using CalamaresUtils::GeoIPXML; +using namespace CalamaresUtils::GeoIP; GeoIPTests::GeoIPTests() { @@ -176,24 +174,25 @@ GeoIPTests::testXMLbad() void GeoIPTests::testSplitTZ() { - auto tz = GeoIP::splitTZString( QStringLiteral("Moon/Dark_side") ); + using namespace CalamaresUtils::GeoIP; + auto tz = splitTZString( QStringLiteral("Moon/Dark_side") ); QCOMPARE( tz.first, QStringLiteral("Moon") ); QCOMPARE( tz.second, QStringLiteral("Dark_side") ); // Some providers return weirdly escaped data - tz = GeoIP::splitTZString( QStringLiteral("America\\/NewYork") ); + tz = splitTZString( QStringLiteral("America\\/NewYork") ); QCOMPARE( tz.first, QStringLiteral("America") ); QCOMPARE( tz.second, QStringLiteral("NewYork") ); // That's not actually the zone name // Check that bogus data fails - tz = GeoIP::splitTZString( QString() ); + tz = splitTZString( QString() ); QCOMPARE( tz.first, QString() ); - tz = GeoIP::splitTZString( QStringLiteral("America.NewYork") ); + tz = splitTZString( QStringLiteral("America.NewYork") ); QCOMPARE( tz.first, QString() ); // Check that three-level is split properly and space is replaced - tz = GeoIP::splitTZString( QStringLiteral("America/North Dakota/Beulah") ); + tz = splitTZString( QStringLiteral("America/North Dakota/Beulah") ); QCOMPARE( tz.first, QStringLiteral("America") ); QCOMPARE( tz.second, QStringLiteral("North_Dakota/Beulah") ); } diff --git a/src/libcalamares/geoip/GeoIPXML.cpp b/src/libcalamares/geoip/GeoIPXML.cpp index 64f6545d4..e67354e1d 100644 --- a/src/libcalamares/geoip/GeoIPXML.cpp +++ b/src/libcalamares/geoip/GeoIPXML.cpp @@ -23,11 +23,11 @@ #include #include -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { GeoIPXML::GeoIPXML( const QString& element ) - : GeoIP( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element ) + : Interface( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element ) { } diff --git a/src/libcalamares/geoip/GeoIPXML.h b/src/libcalamares/geoip/GeoIPXML.h index 15c4e1260..367c307f8 100644 --- a/src/libcalamares/geoip/GeoIPXML.h +++ b/src/libcalamares/geoip/GeoIPXML.h @@ -19,9 +19,9 @@ #ifndef GEOIP_GEOIPXML_H #define GEOIP_GEOIPXML_H -#include "GeoIP.h" +#include "Interface.h" -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { /** @brief GeoIP lookup with XML data * @@ -29,8 +29,10 @@ namespace CalamaresUtils * * element, which contains the text (string) for the region/zone. This * format is expected by, e.g. the Ubiquity installer. + * + * @note This class is an implementation detail. */ -class GeoIPXML : public GeoIP +class GeoIPXML : public Interface { public: /** @brief Configure the element tag which is selected. diff --git a/src/libcalamares/geoip/GeoIPHandler.cpp b/src/libcalamares/geoip/Handler.cpp similarity index 81% rename from src/libcalamares/geoip/GeoIPHandler.cpp rename to src/libcalamares/geoip/Handler.cpp index 87aa1cc24..22643d9ac 100644 --- a/src/libcalamares/geoip/GeoIPHandler.cpp +++ b/src/libcalamares/geoip/Handler.cpp @@ -16,25 +16,25 @@ * along with Calamares. If not, see . */ -#include "GeoIPHandler.h" +#include "Handler.h" -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { -GeoIPHandler::GeoIPHandler() +Handler::Handler() { } bool -GeoIPHandler::isValid() const +Handler::isValid() const { return false; } -GeoIP::RegionZonePair -GeoIPHandler::query() const +RegionZonePair +Handler::query() const { - return GeoIP::RegionZonePair(); + return RegionZonePair(); } } // namespace diff --git a/src/libcalamares/geoip/GeoIPHandler.h b/src/libcalamares/geoip/Handler.h similarity index 85% rename from src/libcalamares/geoip/GeoIPHandler.h rename to src/libcalamares/geoip/Handler.h index 92b35eecf..2e7bed888 100644 --- a/src/libcalamares/geoip/GeoIPHandler.h +++ b/src/libcalamares/geoip/Handler.h @@ -16,12 +16,13 @@ * along with Calamares. If not, see . */ -#ifndef GEOIP_GEOIPHANDLER_H -#define GEOIP_GEOIPHANDLER_H +#ifndef GEOIP_HANDLER_H +#define GEOIP_HANDLER_H -#include "GeoIP.h" +#include "Interface.h" -namespace CalamaresUtils +namespace CalamaresUtils {} +namespace CalamaresUtils::GeoIP { /** @brief Handle one complete GeoIP lookup. @@ -31,13 +32,13 @@ namespace CalamaresUtils * synchronous API and will return an invalid zone pair on * error or if the configuration is not understood/ */ -class GeoIPHandler +class DLLEXPORT Handler { public: /** @brief An unconfigured handler; this always returns errors. */ - GeoIPHandler(); + Handler(); - GeoIP::RegionZonePair query() const; + RegionZonePair query() const; bool isValid() const; }; diff --git a/src/libcalamares/geoip/GeoIP.cpp b/src/libcalamares/geoip/Interface.cpp similarity index 88% rename from src/libcalamares/geoip/GeoIP.cpp rename to src/libcalamares/geoip/Interface.cpp index ec0368172..50aa04683 100644 --- a/src/libcalamares/geoip/GeoIP.cpp +++ b/src/libcalamares/geoip/Interface.cpp @@ -16,24 +16,24 @@ * along with Calamares. If not, see . */ -#include "GeoIP.h" +#include "Interface.h" #include "utils/Logger.h" -namespace CalamaresUtils +namespace CalamaresUtils::GeoIP { -GeoIP::GeoIP(const QString& e) +Interface::Interface(const QString& e) : m_element( e ) { } -GeoIP::~GeoIP() +Interface::~Interface() { } -GeoIP::RegionZonePair -GeoIP::splitTZString( const QString& tz ) +RegionZonePair +splitTZString( const QString& tz ) { QString timezoneString( tz ); timezoneString.remove( '\\' ); diff --git a/src/libcalamares/geoip/GeoIP.h b/src/libcalamares/geoip/Interface.h similarity index 52% rename from src/libcalamares/geoip/GeoIP.h rename to src/libcalamares/geoip/Interface.h index cc022d2de..94c6b3c20 100644 --- a/src/libcalamares/geoip/GeoIP.h +++ b/src/libcalamares/geoip/Interface.h @@ -16,8 +16,10 @@ * along with Calamares. If not, see . */ -#ifndef GEOIP_GEOIP_H -#define GEOIP_GEOIP_H +#ifndef GEOIP_INTERFACE_H +#define GEOIP_INTERFACE_H + +#include "DllMacro.h" #include #include @@ -25,8 +27,41 @@ class QByteArray; -namespace CalamaresUtils +namespace CalamaresUtils {} +namespace CalamaresUtils::GeoIP { +/** @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 DLLEXPORT 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(); } +} ; + +/** @brief Splits a region/zone string into a pair. + * + * Cleans up the string by removing backslashes (\\) + * since some providers return silly-escaped names. Replaces + * spaces with _ since some providers return human-readable names. + * Splits on the first / in the resulting string, or returns a + * pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah + * will return "America", "North_Dakota/Beulah"). + */ +DLLEXPORT RegionZonePair +splitTZString( const QString& s ); + /** * @brief Interface for GeoIP retrievers. * @@ -34,30 +69,10 @@ namespace CalamaresUtils * and can handle the data returned from its interpretation of that * configured URL, returning a region and zone. */ -class GeoIP +class DLLEXPORT Interface { public: - /** @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(); + virtual ~Interface(); /** @brief Handle a (successful) request by interpreting the data. * @@ -70,19 +85,8 @@ public: */ virtual RegionZonePair processReply( const QByteArray& ) = 0; - /** @brief Splits a region/zone string into a pair. - * - * Cleans up the string by removing backslashes (\\) - * since some providers return silly-escaped names. Replaces - * spaces with _ since some providers return human-readable names. - * Splits on the first / in the resulting string, or returns a - * pair of empty QStrings if it can't. (e.g. America/North Dakota/Beulah - * will return "America", "North_Dakota/Beulah"). - */ - static RegionZonePair splitTZString( const QString& s ); - protected: - GeoIP( const QString& e = QString() ); + Interface( const QString& e = QString() ); QString m_element; // string for selecting from data } ; diff --git a/src/libcalamares/geoip/test_geoip.cpp b/src/libcalamares/geoip/test_geoip.cpp index b5e0234df..5f7ab935c 100644 --- a/src/libcalamares/geoip/test_geoip.cpp +++ b/src/libcalamares/geoip/test_geoip.cpp @@ -28,9 +28,7 @@ #endif using std::cerr; -using CalamaresUtils::GeoIP; -using CalamaresUtils::GeoIPJSON; -using CalamaresUtils::GeoIPXML; +using namespace CalamaresUtils::GeoIP; int main(int argc, char** argv) { @@ -40,7 +38,7 @@ int main(int argc, char** argv) return 1; } - GeoIP* handler = nullptr; + Interface* handler = nullptr; if ( QStringLiteral( "json" ) == argv[1] ) handler = new GeoIPJSON; #ifdef QT_XML_LIB diff --git a/src/modules/locale/LocaleViewStep.cpp b/src/modules/locale/LocaleViewStep.cpp index a3ff5df91..6ad73cfbe 100644 --- a/src/modules/locale/LocaleViewStep.cpp +++ b/src/modules/locale/LocaleViewStep.cpp @@ -26,7 +26,7 @@ #include "GlobalStorage.h" #include "JobQueue.h" -#include "geoip/GeoIP.h" +#include "geoip/Interface.h" #include "geoip/GeoIPJSON.h" #ifdef QT_XML_LIB #include "geoip/GeoIPXML.h" @@ -117,14 +117,10 @@ LocaleViewStep::setUpPage() void LocaleViewStep::fetchGeoIpTimezone() { - using CalamaresUtils::GeoIP; - using CalamaresUtils::GeoIPJSON; -#if defined(QT_XML_LIB) - using CalamaresUtils::GeoIPXML; -#endif + using namespace CalamaresUtils::GeoIP; QString actualUrl( m_geoipUrl ); - GeoIP *handler = nullptr; + Interface* handler = nullptr; if ( m_geoipStyle.isEmpty() || m_geoipStyle == "legacy" ) {