2020-05-30 16:15:03 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
2019-05-02 12:55:41 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*
|
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>
|
|
|
|
|
2019-05-03 17:00:57 +02:00
|
|
|
static const NamedEnumTable< CalamaresUtils::GeoIP::Handler::Type >&
|
|
|
|
handlerTypes()
|
|
|
|
{
|
|
|
|
using Type = CalamaresUtils::GeoIP::Handler::Type;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-13 12:25:14 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
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-06-03 15:03:19 +02:00
|
|
|
else if ( m_type == Type::Fixed && Calamares::Settings::instance() && !Calamares::Settings::instance()->debugMode() )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2019-05-13 18:04:31 +02:00
|
|
|
NOTREACHED return nullptr;
|
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
|
|
|
|
2019-08-22 16:03:21 +02:00
|
|
|
return interface->processReply( CalamaresUtils::Network::Manager::instance().synchronousGet( url ) );
|
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
|
|
|
|
2019-08-22 16:03:21 +02:00
|
|
|
return interface->rawReply( CalamaresUtils::Network::Manager::instance().synchronousGet( url ) );
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
} // namespace CalamaresUtils
|