diff --git a/src/libcalamares/geoip/GeoIPTests.cpp b/src/libcalamares/geoip/GeoIPTests.cpp index a39a147b6..ec7511370 100644 --- a/src/libcalamares/geoip/GeoIPTests.cpp +++ b/src/libcalamares/geoip/GeoIPTests.cpp @@ -222,7 +222,7 @@ synchronous_get( const char* urlstring ) auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \ qDebug() << tz; \ QCOMPARE( default_tz, tz ); \ - auto tz2 = CalamaresUtils::GeoIP::Handler( ""#t, url, selector ).query(); \ + auto tz2 = CalamaresUtils::GeoIP::Handler( ""#t, url, selector ).get(); \ qDebug() << tz2; \ QCOMPARE( default_tz, tz2 ); \ } diff --git a/src/libcalamares/geoip/Handler.cpp b/src/libcalamares/geoip/Handler.cpp index f5f8efd11..98c8d194b 100644 --- a/src/libcalamares/geoip/Handler.cpp +++ b/src/libcalamares/geoip/Handler.cpp @@ -29,26 +29,29 @@ #include #include +#include + namespace CalamaresUtils::GeoIP { Handler::Handler() - : m_interface( nullptr ) + : m_type( Type::None ) { } Handler::Handler( const QString& implementation, const QString& url, const QString& selector ) - : m_interface( nullptr ) + : m_type( Type::None ) , m_url( url ) { if ( implementation.compare( "json", Qt::CaseInsensitive ) == 0 ) { - m_interface = new GeoIPJSON( selector ); + + m_type = Type::JSON; } #if defined(QT_XML_LIB) else if ( implementation.compare( "xml", Qt::CaseInsensitive ) == 0 ) { - m_interface = new GeoIPXML( selector ); + m_type = Type::XML; } #endif else @@ -59,13 +62,6 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri Handler::~Handler() { - delete m_interface; -} - -bool -Handler::isValid() const -{ - return m_interface; } static QByteArray @@ -84,14 +80,44 @@ synchronous_get( const QString& urlstring ) return reply->readAll(); } +static std::unique_ptr< Interface > +create_interface( Handler::Type t, const QString& selector ) +{ + switch( t ) + { + case Handler::Type::None: + return nullptr; + case Handler::Type::JSON: + return std::make_unique< GeoIPJSON >( selector ); + case Handler::Type::XML: +#if defined(QT_XML_LIB) + return std::make_unique< GeoIPXML >( selector ); +#else + return nullptr; +#endif + default: // there are no others + return nullptr; + } +} RegionZonePair -Handler::query() const +Handler::get() const { if ( !isValid() ) return RegionZonePair(); - return m_interface->processReply( synchronous_get( m_url ) ); + const auto interface = create_interface( m_type, m_selector ); + if ( !interface ) + return RegionZonePair(); + + return interface->processReply( synchronous_get( m_url ) ); } +/* +QFuture< RegionZonePair > +Handler::query() const +{ +} +*/ + } // namespace diff --git a/src/libcalamares/geoip/Handler.h b/src/libcalamares/geoip/Handler.h index e31e1d7df..a07b62c7b 100644 --- a/src/libcalamares/geoip/Handler.h +++ b/src/libcalamares/geoip/Handler.h @@ -21,6 +21,7 @@ #include "Interface.h" +#include #include #include @@ -31,13 +32,21 @@ namespace CalamaresUtils::GeoIP /** @brief Handle one complete GeoIP lookup. * * This class handles one complete GeoIP lookup. Create it with - * suitable configuration values, then call lookup(). This is a + * suitable configuration values, then call get(). This is a * synchronous API and will return an invalid zone pair on - * error or if the configuration is not understood/ + * error or if the configuration is not understood. For an + * async API, use query(). */ class DLLEXPORT Handler { public: + enum class Type + { + None, + JSON, + XML + } ; + /** @brief An unconfigured handler; this always returns errors. */ Handler(); /** @brief A handler for a specific GeoIP source. @@ -63,13 +72,27 @@ public: ~Handler(); - RegionZonePair query() const; + /** @brief Synchronously get the GeoIP result. + * + * If the Handler is valid, then do the actual fetching and interpretation + * of data and return the result. An invalid Handler will return an + * invalid (empty) result. + */ + RegionZonePair get() const; - bool isValid() const; + /** @brief Asynchronously get the GeoIP result. + * + * See get() for the return value. + */ + QFuture< RegionZonePair > query() const; + + bool isValid() const { return m_type != Type::None; } + Type type() const { return m_type; } private: - Interface* m_interface; + Type m_type; const QString m_url; + const QString m_selector; }; } // namespace