2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
2020-05-30 16:15:03 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2020-08-25 16:05:56 +02:00
|
|
|
*
|
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2019-05-02 12:55:41 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-02 13:25:48 +02:00
|
|
|
#include "Handler.h"
|
2019-05-02 12:55:41 +02:00
|
|
|
|
2020-06-03 15:03:19 +02:00
|
|
|
#include "GeoIPFixed.h"
|
2019-05-02 14:33:29 +02:00
|
|
|
#include "GeoIPJSON.h"
|
2019-08-08 12:32:21 +02:00
|
|
|
#if defined( QT_XML_LIB )
|
2019-05-02 14:33:29 +02:00
|
|
|
#include "GeoIPXML.h"
|
|
|
|
#endif
|
|
|
|
|
2020-06-03 15:03:19 +02:00
|
|
|
#include "Settings.h"
|
2019-08-22 16:03:21 +02:00
|
|
|
#include "network/Manager.h"
|
2019-05-02 14:33:29 +02:00
|
|
|
#include "utils/Logger.h"
|
2019-05-03 17:00:57 +02:00
|
|
|
#include "utils/NamedEnum.h"
|
2019-05-03 17:06:36 +02:00
|
|
|
#include "utils/Variant.h"
|
2019-05-02 14:33:29 +02:00
|
|
|
|
2019-05-02 17:01:01 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2023-09-10 22:26:17 +02:00
|
|
|
static const NamedEnumTable< Calamares::GeoIP::Handler::Type >&
|
2019-05-03 17:00:57 +02:00
|
|
|
handlerTypes()
|
|
|
|
{
|
2023-09-10 22:26:17 +02:00
|
|
|
using Type = Calamares::GeoIP::Handler::Type;
|
2019-05-03 17:00:57 +02:00
|
|
|
|
2019-08-08 11:56:20 +02:00
|
|
|
// *INDENT-OFF*
|
|
|
|
// clang-format off
|
2019-05-03 17:00:57 +02:00
|
|
|
static const NamedEnumTable<Type> names{
|
2019-08-08 11:56:20 +02:00
|
|
|
{ QStringLiteral( "none" ), Type::None },
|
|
|
|
{ QStringLiteral( "json" ), Type::JSON },
|
2020-06-03 15:03:19 +02:00
|
|
|
{ QStringLiteral( "xml" ), Type::XML },
|
|
|
|
{ QStringLiteral( "fixed" ), Type::Fixed }
|
2019-05-03 17:00:57 +02:00
|
|
|
};
|
2019-08-08 11:56:20 +02:00
|
|
|
// *INDENT-ON*
|
|
|
|
// clang-format on
|
2019-05-03 17:00:57 +02:00
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:26:17 +02:00
|
|
|
namespace Calamares
|
2019-05-13 12:25:14 +02:00
|
|
|
{
|
|
|
|
namespace 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 )
|
2019-05-09 21:09:58 +02:00
|
|
|
, m_selector( selector )
|
2019-05-02 14:33:29 +02:00
|
|
|
{
|
2019-05-03 17:00:57 +02:00
|
|
|
bool ok = false;
|
|
|
|
m_type = handlerTypes().find( implementation, ok );
|
2019-06-18 12:33:56 +02:00
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
cWarning() << "GeoIP style" << implementation << "is not recognized.";
|
|
|
|
}
|
|
|
|
else if ( m_type == Type::None )
|
|
|
|
{
|
|
|
|
cWarning() << "GeoIP style *none* does not do anything.";
|
|
|
|
}
|
2020-08-25 23:44:08 +02:00
|
|
|
else if ( m_type == Type::Fixed && Calamares::Settings::instance()
|
|
|
|
&& !Calamares::Settings::instance()->debugMode() )
|
2020-06-03 15:03:19 +02:00
|
|
|
{
|
|
|
|
cWarning() << "GeoIP style *fixed* is not recommended for production.";
|
|
|
|
}
|
2019-08-08 12:32:21 +02:00
|
|
|
#if !defined( QT_XML_LIB )
|
2019-06-18 12:33:56 +02:00
|
|
|
else if ( m_type == Type::XML )
|
2019-05-02 14:33:29 +02:00
|
|
|
{
|
2019-05-03 17:00:57 +02:00
|
|
|
m_type = Type::None;
|
2019-06-18 12:33:56 +02:00
|
|
|
cWarning() << "GeoIP style *xml* is not supported in this version of Calamares.";
|
2019-05-02 14:33:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-08 12:32:21 +02:00
|
|
|
Handler::~Handler() {}
|
2019-05-02 14:33:29 +02:00
|
|
|
|
2019-05-02 17:01:01 +02:00
|
|
|
static std::unique_ptr< Interface >
|
|
|
|
create_interface( Handler::Type t, const QString& selector )
|
|
|
|
{
|
2019-08-08 12:32:21 +02:00
|
|
|
switch ( t )
|
2019-05-02 17:01:01 +02:00
|
|
|
{
|
2019-08-08 12:32:21 +02:00
|
|
|
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 );
|
2019-05-02 17:01:01 +02:00
|
|
|
#else
|
2019-08-08 12:32:21 +02:00
|
|
|
return nullptr;
|
2019-05-02 17:01:01 +02:00
|
|
|
#endif
|
2020-06-03 15:03:19 +02:00
|
|
|
case Handler::Type::Fixed:
|
|
|
|
return std::make_unique< GeoIPFixed >( selector );
|
2019-05-02 17:01:01 +02:00
|
|
|
}
|
2020-10-25 17:48:54 +01:00
|
|
|
__builtin_unreachable();
|
2019-05-02 17:01:01 +02:00
|
|
|
}
|
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 )
|
2019-08-08 12:32:21 +02:00
|
|
|
{
|
2019-05-02 17:17:55 +02:00
|
|
|
return RegionZonePair();
|
2019-08-08 12:32:21 +02:00
|
|
|
}
|
2019-05-02 17:17:55 +02:00
|
|
|
|
2023-09-10 22:32:49 +02:00
|
|
|
using namespace Calamares::Network;
|
2020-10-16 13:54:29 +02:00
|
|
|
return interface->processReply(
|
2023-10-24 01:59:54 +02:00
|
|
|
Calamares::Network::Manager().synchronousGet( url, { RequestOptions::FakeUserAgent } ) );
|
2019-05-02 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 16:40:51 +02:00
|
|
|
static QString
|
|
|
|
do_raw_query( Handler::Type type, const QString& url, const QString& selector )
|
|
|
|
{
|
|
|
|
const auto interface = create_interface( type, selector );
|
|
|
|
if ( !interface )
|
2019-08-08 12:32:21 +02:00
|
|
|
{
|
2019-05-09 16:40:51 +02:00
|
|
|
return QString();
|
2019-08-08 12:32:21 +02:00
|
|
|
}
|
2019-05-09 16:40:51 +02:00
|
|
|
|
2023-09-10 22:32:49 +02:00
|
|
|
using namespace Calamares::Network;
|
2020-10-16 13:54:29 +02:00
|
|
|
return interface->rawReply(
|
2023-10-24 01:59:54 +02:00
|
|
|
Calamares::Network::Manager().synchronousGet( url, { RequestOptions::FakeUserAgent } ) );
|
2019-05-09 16:40:51 +02:00
|
|
|
}
|
|
|
|
|
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() )
|
2019-08-08 12:32:21 +02:00
|
|
|
{
|
2019-05-02 14:33:29 +02:00
|
|
|
return RegionZonePair();
|
2019-08-08 12:32:21 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-08-08 12:32:21 +02:00
|
|
|
return QtConcurrent::run( [ = ] { return do_query( type, url, selector ); } );
|
2019-05-02 12:55:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 16:40:51 +02:00
|
|
|
QString
|
|
|
|
Handler::getRaw() const
|
|
|
|
{
|
|
|
|
if ( !isValid() )
|
2019-08-08 12:32:21 +02:00
|
|
|
{
|
2019-05-09 16:40:51 +02:00
|
|
|
return QString();
|
2019-08-08 12:32:21 +02:00
|
|
|
}
|
2019-05-09 16:40:51 +02:00
|
|
|
return do_raw_query( m_type, m_url, m_selector );
|
|
|
|
}
|
|
|
|
|
|
|
|
QFuture< QString >
|
|
|
|
Handler::queryRaw() const
|
|
|
|
{
|
|
|
|
Handler::Type type = m_type;
|
|
|
|
QString url = m_url;
|
|
|
|
QString selector = m_selector;
|
|
|
|
|
2019-08-08 12:32:21 +02:00
|
|
|
return QtConcurrent::run( [ = ] { return do_raw_query( type, url, selector ); } );
|
2019-05-09 16:40:51 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 12:32:21 +02:00
|
|
|
} // namespace GeoIP
|
2023-09-10 22:26:17 +02:00
|
|
|
} // namespace Calamares
|