[libcalamares] Shuffle GeoIP into a namespace

- Use CalamaresUtils::GeoIP for GeoIP code
 - Name the public interface parts generically, while the
   implementation details retain GeoIP<foo> names.
main
Adriaan de Groot 6 years ago
parent 16413e7bdb
commit 9bc8d28800

@ -70,19 +70,19 @@ if( WITH_PYTHON )
endif() endif()
set( geoipSources set( geoipSources
geoip/GeoIP.cpp geoip/Interface.cpp
geoip/GeoIPHandler.cpp geoip/Handler.cpp
geoip/GeoIPJSON.cpp
) )
set( geoipImplementations geoip/GeoIPJSON.cpp )
set( geoip_libs ) set( geoip_libs )
find_package(Qt5 COMPONENTS Xml) find_package(Qt5 COMPONENTS Xml)
if( Qt5Xml_FOUND ) if( Qt5Xml_FOUND )
list( APPEND geoipSources geoip/GeoIPXML.cpp ) list( APPEND geoipImplementations geoip/GeoIPXML.cpp )
list( APPEND geoip_libs Qt5::Network Qt5::Xml ) list( APPEND geoip_libs Qt5::Network Qt5::Xml )
endif() endif()
add_library( calamares SHARED ${libSources} ${kdsagSources} ${utilsSources} ${geoipSources} ) add_library( calamares SHARED ${libSources} ${kdsagSources} ${utilsSources} ${geoipSources} ${geoipImplementations} )
set_target_properties( calamares set_target_properties( calamares
PROPERTIES PROPERTIES
VERSION ${CALAMARES_VERSION_SHORT} VERSION ${CALAMARES_VERSION_SHORT}

@ -25,11 +25,11 @@
#include <QByteArray> #include <QByteArray>
namespace CalamaresUtils namespace CalamaresUtils::GeoIP
{ {
GeoIPJSON::GeoIPJSON(const QString& attribute) GeoIPJSON::GeoIPJSON(const QString& attribute)
: GeoIP( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute ) : Interface( attribute.isEmpty() ? QStringLiteral( "time_zone" ) : attribute )
{ {
} }

@ -19,9 +19,9 @@
#ifndef GEOIP_GEOIPJSON_H #ifndef GEOIP_GEOIPJSON_H
#define 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. /** @brief GeoIP lookup for services that return JSON.
* *
@ -29,8 +29,10 @@ namespace CalamaresUtils
* (e.g. using the FreeGeoIP.net service), or similar. * (e.g. using the FreeGeoIP.net service), or similar.
* *
* The data is assumed to be in JSON format with a time_zone attribute. * 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: public:
/** @brief Configure the attribute name which is selected. /** @brief Configure the attribute name which is selected.

@ -31,9 +31,7 @@
QTEST_GUILESS_MAIN( GeoIPTests ) QTEST_GUILESS_MAIN( GeoIPTests )
using CalamaresUtils::GeoIP; using namespace CalamaresUtils::GeoIP;
using CalamaresUtils::GeoIPJSON;
using CalamaresUtils::GeoIPXML;
GeoIPTests::GeoIPTests() GeoIPTests::GeoIPTests()
{ {
@ -176,24 +174,25 @@ GeoIPTests::testXMLbad()
void GeoIPTests::testSplitTZ() 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.first, QStringLiteral("Moon") );
QCOMPARE( tz.second, QStringLiteral("Dark_side") ); QCOMPARE( tz.second, QStringLiteral("Dark_side") );
// Some providers return weirdly escaped data // Some providers return weirdly escaped data
tz = GeoIP::splitTZString( QStringLiteral("America\\/NewYork") ); tz = splitTZString( QStringLiteral("America\\/NewYork") );
QCOMPARE( tz.first, QStringLiteral("America") ); QCOMPARE( tz.first, QStringLiteral("America") );
QCOMPARE( tz.second, QStringLiteral("NewYork") ); // That's not actually the zone name QCOMPARE( tz.second, QStringLiteral("NewYork") ); // That's not actually the zone name
// Check that bogus data fails // Check that bogus data fails
tz = GeoIP::splitTZString( QString() ); tz = splitTZString( QString() );
QCOMPARE( tz.first, QString() ); QCOMPARE( tz.first, QString() );
tz = GeoIP::splitTZString( QStringLiteral("America.NewYork") ); tz = splitTZString( QStringLiteral("America.NewYork") );
QCOMPARE( tz.first, QString() ); QCOMPARE( tz.first, QString() );
// Check that three-level is split properly and space is replaced // 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.first, QStringLiteral("America") );
QCOMPARE( tz.second, QStringLiteral("North_Dakota/Beulah") ); QCOMPARE( tz.second, QStringLiteral("North_Dakota/Beulah") );
} }

@ -23,11 +23,11 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QtXml/QDomDocument> #include <QtXml/QDomDocument>
namespace CalamaresUtils namespace CalamaresUtils::GeoIP
{ {
GeoIPXML::GeoIPXML( const QString& element ) GeoIPXML::GeoIPXML( const QString& element )
: GeoIP( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element ) : Interface( element.isEmpty() ? QStringLiteral( "TimeZone" ) : element )
{ {
} }

@ -19,9 +19,9 @@
#ifndef GEOIP_GEOIPXML_H #ifndef GEOIP_GEOIPXML_H
#define GEOIP_GEOIPXML_H #define GEOIP_GEOIPXML_H
#include "GeoIP.h" #include "Interface.h"
namespace CalamaresUtils namespace CalamaresUtils::GeoIP
{ {
/** @brief GeoIP lookup with XML data /** @brief GeoIP lookup with XML data
* *
@ -29,8 +29,10 @@ namespace CalamaresUtils
* <Response><TimeZone></TimeZone></Response> * <Response><TimeZone></TimeZone></Response>
* element, which contains the text (string) for the region/zone. This * element, which contains the text (string) for the region/zone. This
* format is expected by, e.g. the Ubiquity installer. * 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: public:
/** @brief Configure the element tag which is selected. /** @brief Configure the element tag which is selected.

@ -16,25 +16,25 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "GeoIPHandler.h" #include "Handler.h"
namespace CalamaresUtils namespace CalamaresUtils::GeoIP
{ {
GeoIPHandler::GeoIPHandler() Handler::Handler()
{ {
} }
bool bool
GeoIPHandler::isValid() const Handler::isValid() const
{ {
return false; return false;
} }
GeoIP::RegionZonePair RegionZonePair
GeoIPHandler::query() const Handler::query() const
{ {
return GeoIP::RegionZonePair(); return RegionZonePair();
} }
} // namespace } // namespace

@ -16,12 +16,13 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef GEOIP_GEOIPHANDLER_H #ifndef GEOIP_HANDLER_H
#define GEOIP_GEOIPHANDLER_H #define GEOIP_HANDLER_H
#include "GeoIP.h" #include "Interface.h"
namespace CalamaresUtils namespace CalamaresUtils {}
namespace CalamaresUtils::GeoIP
{ {
/** @brief Handle one complete GeoIP lookup. /** @brief Handle one complete GeoIP lookup.
@ -31,13 +32,13 @@ namespace CalamaresUtils
* synchronous API and will return an invalid zone pair on * 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/
*/ */
class GeoIPHandler class DLLEXPORT Handler
{ {
public: public:
/** @brief An unconfigured handler; this always returns errors. */ /** @brief An unconfigured handler; this always returns errors. */
GeoIPHandler(); Handler();
GeoIP::RegionZonePair query() const; RegionZonePair query() const;
bool isValid() const; bool isValid() const;
}; };

@ -16,24 +16,24 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "GeoIP.h" #include "Interface.h"
#include "utils/Logger.h" #include "utils/Logger.h"
namespace CalamaresUtils namespace CalamaresUtils::GeoIP
{ {
GeoIP::GeoIP(const QString& e) Interface::Interface(const QString& e)
: m_element( e ) : m_element( e )
{ {
} }
GeoIP::~GeoIP() Interface::~Interface()
{ {
} }
GeoIP::RegionZonePair RegionZonePair
GeoIP::splitTZString( const QString& tz ) splitTZString( const QString& tz )
{ {
QString timezoneString( tz ); QString timezoneString( tz );
timezoneString.remove( '\\' ); timezoneString.remove( '\\' );

@ -16,8 +16,10 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef GEOIP_GEOIP_H #ifndef GEOIP_INTERFACE_H
#define GEOIP_GEOIP_H #define GEOIP_INTERFACE_H
#include "DllMacro.h"
#include <QPair> #include <QPair>
#include <QString> #include <QString>
@ -25,8 +27,41 @@
class QByteArray; 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<QString, QString>
{
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. * @brief Interface for GeoIP retrievers.
* *
@ -34,30 +69,10 @@ namespace CalamaresUtils
* and can handle the data returned from its interpretation of that * and can handle the data returned from its interpretation of that
* configured URL, returning a region and zone. * configured URL, returning a region and zone.
*/ */
class GeoIP class DLLEXPORT Interface
{ {
public: public:
/** @brief A Region, Zone pair of strings virtual ~Interface();
*
* 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<QString, QString>
{
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();
/** @brief Handle a (successful) request by interpreting the data. /** @brief Handle a (successful) request by interpreting the data.
* *
@ -70,19 +85,8 @@ public:
*/ */
virtual RegionZonePair processReply( const QByteArray& ) = 0; 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: protected:
GeoIP( const QString& e = QString() ); Interface( const QString& e = QString() );
QString m_element; // string for selecting from data QString m_element; // string for selecting from data
} ; } ;

@ -28,9 +28,7 @@
#endif #endif
using std::cerr; using std::cerr;
using CalamaresUtils::GeoIP; using namespace CalamaresUtils::GeoIP;
using CalamaresUtils::GeoIPJSON;
using CalamaresUtils::GeoIPXML;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -40,7 +38,7 @@ int main(int argc, char** argv)
return 1; return 1;
} }
GeoIP* handler = nullptr; Interface* handler = nullptr;
if ( QStringLiteral( "json" ) == argv[1] ) if ( QStringLiteral( "json" ) == argv[1] )
handler = new GeoIPJSON; handler = new GeoIPJSON;
#ifdef QT_XML_LIB #ifdef QT_XML_LIB

@ -26,7 +26,7 @@
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "geoip/GeoIP.h" #include "geoip/Interface.h"
#include "geoip/GeoIPJSON.h" #include "geoip/GeoIPJSON.h"
#ifdef QT_XML_LIB #ifdef QT_XML_LIB
#include "geoip/GeoIPXML.h" #include "geoip/GeoIPXML.h"
@ -117,14 +117,10 @@ LocaleViewStep::setUpPage()
void void
LocaleViewStep::fetchGeoIpTimezone() LocaleViewStep::fetchGeoIpTimezone()
{ {
using CalamaresUtils::GeoIP; using namespace CalamaresUtils::GeoIP;
using CalamaresUtils::GeoIPJSON;
#if defined(QT_XML_LIB)
using CalamaresUtils::GeoIPXML;
#endif
QString actualUrl( m_geoipUrl ); QString actualUrl( m_geoipUrl );
GeoIP *handler = nullptr; Interface* handler = nullptr;
if ( m_geoipStyle.isEmpty() || m_geoipStyle == "legacy" ) if ( m_geoipStyle.isEmpty() || m_geoipStyle == "legacy" )
{ {

Loading…
Cancel
Save