[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.
This commit is contained in:
parent
16413e7bdb
commit
9bc8d28800
@ -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}
|
||||
|
@ -25,11 +25,11 @@
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
namespace CalamaresUtils
|
||||
namespace CalamaresUtils::GeoIP
|
||||
{
|
||||
|
||||
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
|
||||
#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.
|
||||
|
@ -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") );
|
||||
}
|
||||
|
@ -23,11 +23,11 @@
|
||||
#include <QNetworkReply>
|
||||
#include <QtXml/QDomDocument>
|
||||
|
||||
namespace CalamaresUtils
|
||||
namespace CalamaresUtils::GeoIP
|
||||
{
|
||||
|
||||
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
|
||||
#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
|
||||
* <Response><TimeZone></TimeZone></Response>
|
||||
* 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.
|
||||
|
@ -16,25 +16,25 @@
|
||||
* 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
|
||||
GeoIPHandler::isValid() const
|
||||
Handler::isValid() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
GeoIP::RegionZonePair
|
||||
GeoIPHandler::query() const
|
||||
RegionZonePair
|
||||
Handler::query() const
|
||||
{
|
||||
return GeoIP::RegionZonePair();
|
||||
return RegionZonePair();
|
||||
}
|
||||
|
||||
} // namespace
|
@ -16,12 +16,13 @@
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
};
|
@ -16,24 +16,24 @@
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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( '\\' );
|
@ -16,8 +16,10 @@
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef GEOIP_GEOIP_H
|
||||
#define GEOIP_GEOIP_H
|
||||
#ifndef GEOIP_INTERFACE_H
|
||||
#define GEOIP_INTERFACE_H
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
@ -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<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.
|
||||
*
|
||||
@ -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<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();
|
||||
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
|
||||
} ;
|
@ -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
|
||||
|
@ -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" )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user