[locale] Refactor GeoIP handlers

- Read the data in the caller of the handler, instead of in the callers
main
Adriaan de Groot 7 years ago
parent 939cdff93b
commit 5b98e58ae7

@ -23,7 +23,7 @@
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
class QNetworkReply; class QByteArray;
/** /**
* @brief Interface for GeoIP retrievers. * @brief Interface for GeoIP retrievers.
@ -47,7 +47,7 @@ struct GeoIP
* likes. On error, returns a RegionZonePair with empty * likes. On error, returns a RegionZonePair with empty
* strings (e.g. ( "", "" ) ). * strings (e.g. ( "", "" ) ).
*/ */
virtual RegionZonePair processReply( QNetworkReply* ) = 0; virtual RegionZonePair processReply( const QByteArray& ) = 0;
/** @brief Splits a region/zone string into a pair. */ /** @brief Splits a region/zone string into a pair. */
static RegionZonePair splitTZString( const QString& s ); static RegionZonePair splitTZString( const QString& s );

@ -22,15 +22,13 @@
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/YamlUtils.h" #include "utils/YamlUtils.h"
#include <QNetworkReply> #include <QByteArray>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
GeoIP::RegionZonePair GeoIP::RegionZonePair
FreeGeoIP::processReply( QNetworkReply* reply ) FreeGeoIP::processReply( const QByteArray& data )
{ {
QByteArray data = reply->readAll();
try try
{ {
YAML::Node doc = YAML::Load( data ); YAML::Node doc = YAML::Load( data );

@ -31,7 +31,7 @@
*/ */
struct FreeGeoIP : public GeoIP struct FreeGeoIP : public GeoIP
{ {
virtual RegionZonePair processReply( QNetworkReply* ); virtual RegionZonePair processReply( const QByteArray& );
} ; } ;
#endif #endif

@ -24,13 +24,13 @@
#include <QtXml/QDomDocument> #include <QtXml/QDomDocument>
GeoIP::RegionZonePair GeoIP::RegionZonePair
XMLGeoIP::processReply( QNetworkReply* reply ) XMLGeoIP::processReply( const QByteArray& data )
{ {
QString domError; QString domError;
int errorLine, errorColumn; int errorLine, errorColumn;
QDomDocument doc; QDomDocument doc;
if ( doc.setContent( reply->readAll(), false, &domError, &errorLine, &errorColumn ) ) if ( doc.setContent( data, false, &domError, &errorLine, &errorColumn ) )
{ {
const auto tzElements = doc.elementsByTagName( "TimeZone" ); const auto tzElements = doc.elementsByTagName( "TimeZone" );
cDebug() << "GeoIP found" << tzElements.length() << "elements"; cDebug() << "GeoIP found" << tzElements.length() << "elements";

@ -30,7 +30,7 @@
*/ */
struct XMLGeoIP : public GeoIP struct XMLGeoIP : public GeoIP
{ {
virtual RegionZonePair processReply( QNetworkReply* ); virtual RegionZonePair processReply( const QByteArray& );
} ; } ;
#endif #endif

@ -122,7 +122,7 @@ LocaleViewStep::fetchGeoIpTimezone()
{ {
if ( reply->error() == QNetworkReply::NoError ) if ( reply->error() == QNetworkReply::NoError )
{ {
auto tz = handler->processReply( reply ); auto tz = handler->processReply( reply->readAll() );
if ( !tz.first.isEmpty() ) if ( !tz.first.isEmpty() )
m_startingTimezone = tz; m_startingTimezone = tz;
} }

Loading…
Cancel
Save