2019-05-02 12:55:41 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2019, 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-05-02 13:25:48 +02:00
|
|
|
#include "Handler.h"
|
2019-05-02 12:55:41 +02:00
|
|
|
|
2019-05-02 14:33:29 +02:00
|
|
|
#include "GeoIPJSON.h"
|
|
|
|
#if defined(QT_XML_LIB)
|
|
|
|
#include "GeoIPXML.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2019-05-02 17:01:01 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2019-05-02 13:25:48 +02:00
|
|
|
namespace CalamaresUtils::GeoIP
|
2019-05-02 12:55:41 +02:00
|
|
|
{
|
|
|
|
|
2019-05-02 13:25:48 +02:00
|
|
|
Handler::Handler()
|
2019-05-02 17:01:01 +02:00
|
|
|
: m_type( Type::None )
|
2019-05-02 14:33:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Handler::Handler( const QString& implementation, const QString& url, const QString& selector )
|
2019-05-02 17:01:01 +02:00
|
|
|
: m_type( Type::None )
|
2019-05-02 14:33:29 +02:00
|
|
|
, m_url( url )
|
|
|
|
{
|
|
|
|
if ( implementation.compare( "json", Qt::CaseInsensitive ) == 0 )
|
|
|
|
{
|
2019-05-02 17:01:01 +02:00
|
|
|
|
|
|
|
m_type = Type::JSON;
|
2019-05-02 14:33:29 +02:00
|
|
|
}
|
|
|
|
#if defined(QT_XML_LIB)
|
|
|
|
else if ( implementation.compare( "xml", Qt::CaseInsensitive ) == 0 )
|
|
|
|
{
|
2019-05-02 17:01:01 +02:00
|
|
|
m_type = Type::XML;
|
2019-05-02 14:33:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "GeoIP Style" << implementation << "is not recognized.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Handler::~Handler()
|
2019-05-02 12:55:41 +02:00
|
|
|
{
|
2019-05-02 14:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static QByteArray
|
|
|
|
synchronous_get( const QString& urlstring )
|
|
|
|
{
|
|
|
|
QUrl url( urlstring );
|
|
|
|
QNetworkAccessManager manager;
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
QObject::connect( &manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit );
|
|
|
|
|
|
|
|
QNetworkRequest request( url );
|
|
|
|
QNetworkReply* reply = manager.get( request );
|
|
|
|
loop.exec();
|
|
|
|
reply->deleteLater();
|
|
|
|
return reply->readAll();
|
2019-05-02 12:55:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:01:01 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 14:33:29 +02:00
|
|
|
|
2019-05-02 17:17:55 +02:00
|
|
|
static RegionZonePair
|
|
|
|
do_query( Handler::Type type, const QString& url, const QString& selector )
|
|
|
|
{
|
|
|
|
const auto interface = create_interface( type, selector );
|
|
|
|
if ( !interface )
|
|
|
|
return RegionZonePair();
|
|
|
|
|
|
|
|
return interface->processReply( synchronous_get( url ) );
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:48 +02:00
|
|
|
RegionZonePair
|
2019-05-02 17:01:01 +02:00
|
|
|
Handler::get() const
|
2019-05-02 12:55:41 +02:00
|
|
|
{
|
2019-05-02 14:33:29 +02:00
|
|
|
if ( !isValid() )
|
|
|
|
return RegionZonePair();
|
2019-05-02 17:17:55 +02:00
|
|
|
return do_query( m_type, m_url, m_selector );
|
2019-05-02 17:01:01 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:17:55 +02:00
|
|
|
|
2019-05-02 17:01:01 +02:00
|
|
|
QFuture< RegionZonePair >
|
|
|
|
Handler::query() const
|
|
|
|
{
|
2019-05-02 17:17:55 +02:00
|
|
|
Handler::Type type = m_type;
|
|
|
|
QString url = m_url;
|
|
|
|
QString selector = m_selector;
|
|
|
|
|
|
|
|
return QtConcurrent::run( [=]
|
|
|
|
{
|
|
|
|
return do_query( type, url, selector );
|
|
|
|
} );
|
2019-05-02 12:55:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|