[locale] Start refactoring geoip handling
- Introduce a handler interface for GeoIP providers - Move the implementation of FreeGeoIP into a struct of its own
This commit is contained in:
parent
b3f0932ff9
commit
445f181cc3
@ -4,6 +4,7 @@ calamares_add_plugin( locale
|
|||||||
TYPE viewmodule
|
TYPE viewmodule
|
||||||
EXPORT_MACRO PLUGINDLLEXPORT_PRO
|
EXPORT_MACRO PLUGINDLLEXPORT_PRO
|
||||||
SOURCES
|
SOURCES
|
||||||
|
GeoIPFreeGeoIP.cpp
|
||||||
LCLocaleDialog.cpp
|
LCLocaleDialog.cpp
|
||||||
LocaleConfiguration.cpp
|
LocaleConfiguration.cpp
|
||||||
LocalePage.cpp
|
LocalePage.cpp
|
||||||
|
61
src/modules/locale/GeoIP.h
Normal file
61
src/modules/locale/GeoIP.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GEOIP_H
|
||||||
|
#define GEOIP_H
|
||||||
|
|
||||||
|
#include <QPair>
|
||||||
|
#include <QString>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
class QNetworkReply;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Interface for GeoIP retrievers.
|
||||||
|
*
|
||||||
|
* A GeoIP retriever takes a configured URL (from the config file)
|
||||||
|
* and can handle the data returned from its interpretation of that
|
||||||
|
* configured URL, returning a region and zone.
|
||||||
|
*/
|
||||||
|
struct GeoIP
|
||||||
|
{
|
||||||
|
using RegionZonePair = QPair<QString, QString>;
|
||||||
|
|
||||||
|
/** @brief Convert configured URL to a complete URL.
|
||||||
|
*
|
||||||
|
* Some GeoIP providers are configured with one URL, but actually
|
||||||
|
* do retrieval with another (e.g. when using the original
|
||||||
|
* implementation of FreeGeoIP, or when adding an API key).
|
||||||
|
*/
|
||||||
|
virtual QUrl fullUrl( const QString& configUrl ) = 0;
|
||||||
|
|
||||||
|
/** @brief Handle a (successful) request by interpreting the data.
|
||||||
|
*
|
||||||
|
* Should return a ( <zone>, <region> ) pair, e.g.
|
||||||
|
* ( "Europe", "Amsterdam" ). This is called **only** if the
|
||||||
|
* request to the fullUrl was successful; the handler
|
||||||
|
* is free to read as much, or as little, data as it
|
||||||
|
* likes. On error, returns a RegionZonePair with empty
|
||||||
|
* strings (e.g. ( "", "" ) ).
|
||||||
|
*/
|
||||||
|
virtual RegionZonePair processReply( QNetworkReply* ) = 0;
|
||||||
|
|
||||||
|
virtual ~GeoIP(); // Defined in LocaleViewStep
|
||||||
|
} ;
|
||||||
|
|
||||||
|
#endif
|
74
src/modules/locale/GeoIPFreeGeoIP.cpp
Normal file
74
src/modules/locale/GeoIPFreeGeoIP.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
||||||
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "GeoIPFreeGeoIP.h"
|
||||||
|
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
#include "utils/YamlUtils.h"
|
||||||
|
|
||||||
|
#include <QNetworkReply>
|
||||||
|
|
||||||
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
QUrl
|
||||||
|
FreeGeoIP::fullUrl( const QString& configUrl )
|
||||||
|
{
|
||||||
|
// FIXME: derpy way to append "/json" to the user-specified config URL
|
||||||
|
QString requestUrl = QString( "%1/json" )
|
||||||
|
.arg( QUrl::fromUserInput( configUrl ).toString() );
|
||||||
|
return QUrl( requestUrl );
|
||||||
|
}
|
||||||
|
|
||||||
|
GeoIP::RegionZonePair
|
||||||
|
FreeGeoIP::processReply( QNetworkReply* reply )
|
||||||
|
{
|
||||||
|
QByteArray data = reply->readAll();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
YAML::Node doc = YAML::Load( data );
|
||||||
|
|
||||||
|
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
||||||
|
if ( !var.isNull() &&
|
||||||
|
var.isValid() &&
|
||||||
|
var.type() == QVariant::Map )
|
||||||
|
{
|
||||||
|
QVariantMap map = var.toMap();
|
||||||
|
if ( map.contains( "time_zone" ) &&
|
||||||
|
!map.value( "time_zone" ).toString().isEmpty() )
|
||||||
|
{
|
||||||
|
QString timezoneString = map.value( "time_zone" ).toString();
|
||||||
|
QStringList tzParts = timezoneString.split( '/', QString::SkipEmptyParts );
|
||||||
|
if ( tzParts.size() >= 2 )
|
||||||
|
{
|
||||||
|
cDebug() << "GeoIP reporting" << timezoneString;
|
||||||
|
QString region = tzParts.takeFirst();
|
||||||
|
QString zone = tzParts.join( '/' );
|
||||||
|
return qMakePair( region, zone );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( YAML::Exception& e )
|
||||||
|
{
|
||||||
|
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return qMakePair( QString(), QString() );
|
||||||
|
}
|
30
src/modules/locale/GeoIPFreeGeoIP.h
Normal file
30
src/modules/locale/GeoIPFreeGeoIP.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Calamares is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GEOIPFREEGEOIP_H
|
||||||
|
#define GEOIPFREEGEOIP_H
|
||||||
|
|
||||||
|
#include "GeoIP.h"
|
||||||
|
|
||||||
|
struct FreeGeoIP : public GeoIP
|
||||||
|
{
|
||||||
|
virtual QUrl fullUrl( const QString& configUrl );
|
||||||
|
virtual RegionZonePair processReply( QNetworkReply* );
|
||||||
|
} ;
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
||||||
*
|
*
|
||||||
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
||||||
|
* Copyright 2018, Adriaan de Groot <groot.org>
|
||||||
*
|
*
|
||||||
* Calamares is free software: you can redistribute it and/or modify
|
* Calamares is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -18,11 +19,14 @@
|
|||||||
|
|
||||||
#include "LocaleViewStep.h"
|
#include "LocaleViewStep.h"
|
||||||
|
|
||||||
|
#include "GeoIP.h"
|
||||||
|
#include "GeoIPFreeGeoIP.h"
|
||||||
|
#include "GlobalStorage.h"
|
||||||
|
#include "JobQueue.h"
|
||||||
#include "LocalePage.h"
|
#include "LocalePage.h"
|
||||||
|
|
||||||
#include "timezonewidget/localeglobal.h"
|
#include "timezonewidget/localeglobal.h"
|
||||||
#include "widgets/WaitingWidget.h"
|
#include "widgets/WaitingWidget.h"
|
||||||
#include "JobQueue.h"
|
|
||||||
#include "GlobalStorage.h"
|
|
||||||
|
|
||||||
#include "utils/CalamaresUtilsGui.h"
|
#include "utils/CalamaresUtilsGui.h"
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
@ -111,53 +115,25 @@ void
|
|||||||
LocaleViewStep::fetchGeoIpTimezone()
|
LocaleViewStep::fetchGeoIpTimezone()
|
||||||
{
|
{
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
|
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
|
||||||
|
GeoIP *handler = new FreeGeoIP;
|
||||||
|
|
||||||
connect( manager, &QNetworkAccessManager::finished,
|
connect( manager, &QNetworkAccessManager::finished,
|
||||||
[=]( QNetworkReply* reply )
|
[=]( QNetworkReply* reply )
|
||||||
{
|
{
|
||||||
if ( reply->error() == QNetworkReply::NoError )
|
if ( reply->error() == QNetworkReply::NoError )
|
||||||
{
|
{
|
||||||
QByteArray data = reply->readAll();
|
auto tz = handler->processReply( reply );
|
||||||
|
if ( !tz.first.isEmpty() )
|
||||||
try
|
m_startingTimezone = tz;
|
||||||
{
|
|
||||||
YAML::Node doc = YAML::Load( data );
|
|
||||||
|
|
||||||
QVariant var = CalamaresUtils::yamlToVariant( doc );
|
|
||||||
if ( !var.isNull() &&
|
|
||||||
var.isValid() &&
|
|
||||||
var.type() == QVariant::Map )
|
|
||||||
{
|
|
||||||
QVariantMap map = var.toMap();
|
|
||||||
if ( map.contains( "time_zone" ) &&
|
|
||||||
!map.value( "time_zone" ).toString().isEmpty() )
|
|
||||||
{
|
|
||||||
QString timezoneString = map.value( "time_zone" ).toString();
|
|
||||||
QStringList tzParts = timezoneString.split( '/', QString::SkipEmptyParts );
|
|
||||||
if ( tzParts.size() >= 2 )
|
|
||||||
{
|
|
||||||
cDebug() << "GeoIP reporting" << timezoneString;
|
|
||||||
QString region = tzParts.takeFirst();
|
|
||||||
QString zone = tzParts.join( '/' );
|
|
||||||
m_startingTimezone = qMakePair( region, zone );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch ( YAML::Exception& e )
|
|
||||||
{
|
|
||||||
CalamaresUtils::explainYamlException( e, data, "GeoIP data");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
delete handler;
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
manager->deleteLater();
|
manager->deleteLater();
|
||||||
setUpPage();
|
setUpPage();
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
QString requestUrl = QString( "%1/json" )
|
request.setUrl( handler->fullUrl( m_geoipUrl ) );
|
||||||
.arg( QUrl::fromUserInput( m_geoipUrl ).toString() );
|
|
||||||
request.setUrl( QUrl( requestUrl ) );
|
|
||||||
request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
|
request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
|
||||||
manager->get( request );
|
manager->get( request );
|
||||||
}
|
}
|
||||||
@ -294,3 +270,9 @@ LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
|||||||
m_geoipUrl = configurationMap.value( "geoipUrl" ).toString();
|
m_geoipUrl = configurationMap.value( "geoipUrl" ).toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Defined here since the struct has nothing else in it
|
||||||
|
GeoIP::~GeoIP()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user