[locale] Move GeoIP lookup to config
- replace the weird synchronous-lookup-during-requirements-checking with a proper async lookup when the system is ready.
This commit is contained in:
parent
a25d61077f
commit
42331f6e13
@ -26,6 +26,8 @@
|
|||||||
#include "JobQueue.h"
|
#include "JobQueue.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include "locale/Label.h"
|
#include "locale/Label.h"
|
||||||
|
#include "modulesystem/ModuleManager.h"
|
||||||
|
#include "network/Manager.h"
|
||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
#include "utils/Variant.h"
|
#include "utils/Variant.h"
|
||||||
|
|
||||||
@ -204,6 +206,15 @@ Config::timezoneData() const
|
|||||||
return ::timezoneData();
|
return ::timezoneData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Config::setCurrentLocation()
|
||||||
|
{
|
||||||
|
if ( !m_currentLocation && m_startingTimezone.isValid() )
|
||||||
|
{
|
||||||
|
setCurrentLocation( m_startingTimezone.first, m_startingTimezone.second );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Config::setCurrentLocation( const QString& regionName, const QString& zoneName )
|
Config::setCurrentLocation( const QString& regionName, const QString& zoneName )
|
||||||
{
|
{
|
||||||
@ -252,7 +263,6 @@ Config::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LocaleConfiguration
|
LocaleConfiguration
|
||||||
Config::automaticLocaleConfiguration() const
|
Config::automaticLocaleConfiguration() const
|
||||||
{
|
{
|
||||||
@ -341,8 +351,8 @@ getLocaleGenLines( const QVariantMap& configurationMap, QStringList& localeGenLi
|
|||||||
static inline void
|
static inline void
|
||||||
getAdjustLiveTimezone( const QVariantMap& configurationMap, bool& adjustLiveTimezone )
|
getAdjustLiveTimezone( const QVariantMap& configurationMap, bool& adjustLiveTimezone )
|
||||||
{
|
{
|
||||||
adjustLiveTimezone
|
adjustLiveTimezone = CalamaresUtils::getBool(
|
||||||
= CalamaresUtils::getBool( configurationMap, "adjustLiveTimezone", Calamares::Settings::instance()->doChroot() );
|
configurationMap, "adjustLiveTimezone", Calamares::Settings::instance()->doChroot() );
|
||||||
#ifdef DEBUG_TIMEZONES
|
#ifdef DEBUG_TIMEZONES
|
||||||
if ( m_adjustLiveTimezone )
|
if ( m_adjustLiveTimezone )
|
||||||
{
|
{
|
||||||
@ -411,6 +421,12 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
|
|||||||
getAdjustLiveTimezone( configurationMap, m_adjustLiveTimezone );
|
getAdjustLiveTimezone( configurationMap, m_adjustLiveTimezone );
|
||||||
getStartingTimezone( configurationMap, m_startingTimezone );
|
getStartingTimezone( configurationMap, m_startingTimezone );
|
||||||
getGeoIP( configurationMap, m_geoip );
|
getGeoIP( configurationMap, m_geoip );
|
||||||
|
|
||||||
|
if ( m_geoip && m_geoip->isValid() )
|
||||||
|
{
|
||||||
|
connect(
|
||||||
|
Calamares::ModuleManager::instance(), &Calamares::ModuleManager::modulesLoaded, this, &Config::startGeoIP );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Calamares::JobList
|
Calamares::JobList
|
||||||
@ -427,3 +443,42 @@ Config::createJobs()
|
|||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Config::startGeoIP()
|
||||||
|
{
|
||||||
|
if ( m_geoip && m_geoip->isValid() )
|
||||||
|
{
|
||||||
|
auto& network = CalamaresUtils::Network::Manager::instance();
|
||||||
|
if ( network.hasInternet() || network.synchronousPing( m_geoip->url() ) )
|
||||||
|
{
|
||||||
|
using Watcher = QFutureWatcher< CalamaresUtils::GeoIP::RegionZonePair >;
|
||||||
|
m_geoipWatcher = std::make_unique< Watcher >();
|
||||||
|
m_geoipWatcher->setFuture( m_geoip->query() );
|
||||||
|
connect( m_geoipWatcher.get(), &Watcher::finished, this, &Config::completeGeoIP );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Config::completeGeoIP()
|
||||||
|
{
|
||||||
|
if ( !currentLocation() )
|
||||||
|
{
|
||||||
|
auto r = m_geoipWatcher->result();
|
||||||
|
if ( r.isValid() )
|
||||||
|
{
|
||||||
|
m_startingTimezone = r;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cWarning() << "GeoIP returned invalid result.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cWarning() << "GeoIP result ignored because a location is already set.";
|
||||||
|
}
|
||||||
|
m_geoipWatcher.reset();
|
||||||
|
m_geoip.reset();
|
||||||
|
}
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include "geoip/Interface.h"
|
#include "geoip/Interface.h"
|
||||||
#include "locale/TimeZone.h"
|
#include "locale/TimeZone.h"
|
||||||
|
|
||||||
|
#include <QFutureWatcher>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -59,7 +60,6 @@ public:
|
|||||||
/** @brief The currently selected location (timezone)
|
/** @brief The currently selected location (timezone)
|
||||||
*
|
*
|
||||||
* The location is a pointer into the date that timezoneData() returns.
|
* The location is a pointer into the date that timezoneData() returns.
|
||||||
* It is possible to return nullptr, if no location has been picked yet.
|
|
||||||
*/
|
*/
|
||||||
const CalamaresUtils::Locale::TZZone* currentLocation() const { return m_currentLocation; }
|
const CalamaresUtils::Locale::TZZone* currentLocation() const { return m_currentLocation; }
|
||||||
|
|
||||||
@ -79,6 +79,9 @@ public:
|
|||||||
CalamaresUtils::Locale::CStringListModel* regionModel() const { return m_regionModel.get(); }
|
CalamaresUtils::Locale::CStringListModel* regionModel() const { return m_regionModel.get(); }
|
||||||
CalamaresUtils::Locale::CStringListModel* zonesModel() const { return m_zonesModel.get(); }
|
CalamaresUtils::Locale::CStringListModel* zonesModel() const { return m_zonesModel.get(); }
|
||||||
|
|
||||||
|
/// Special case, set location from starting timezone if not already set
|
||||||
|
void setCurrentLocation();
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
/// Set a language by user-choice, overriding future location changes
|
/// Set a language by user-choice, overriding future location changes
|
||||||
void setLanguageExplicitly( const QString& language );
|
void setLanguageExplicitly( const QString& language );
|
||||||
@ -149,6 +152,11 @@ private:
|
|||||||
* by explicitly calling *TODO*
|
* by explicitly calling *TODO*
|
||||||
*/
|
*/
|
||||||
std::unique_ptr< CalamaresUtils::GeoIP::Handler > m_geoip;
|
std::unique_ptr< CalamaresUtils::GeoIP::Handler > m_geoip;
|
||||||
|
|
||||||
|
// Implementation details for doing GeoIP lookup
|
||||||
|
void startGeoIP();
|
||||||
|
void completeGeoIP();
|
||||||
|
std::unique_ptr< QFutureWatcher< CalamaresUtils::GeoIP::RegionZonePair > > m_geoipWatcher;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +43,6 @@ LocaleViewStep::LocaleViewStep( QObject* parent )
|
|||||||
, m_widget( new QWidget() )
|
, m_widget( new QWidget() )
|
||||||
, m_actualWidget( nullptr )
|
, m_actualWidget( nullptr )
|
||||||
, m_nextEnabled( false )
|
, m_nextEnabled( false )
|
||||||
, m_geoip( nullptr )
|
|
||||||
, m_config( std::make_unique< Config >() )
|
, m_config( std::make_unique< Config >() )
|
||||||
{
|
{
|
||||||
QBoxLayout* mainLayout = new QHBoxLayout;
|
QBoxLayout* mainLayout = new QHBoxLayout;
|
||||||
@ -66,11 +65,11 @@ LocaleViewStep::~LocaleViewStep()
|
|||||||
void
|
void
|
||||||
LocaleViewStep::setUpPage()
|
LocaleViewStep::setUpPage()
|
||||||
{
|
{
|
||||||
|
m_config->setCurrentLocation();
|
||||||
if ( !m_actualWidget )
|
if ( !m_actualWidget )
|
||||||
{
|
{
|
||||||
m_actualWidget = new LocalePage( m_config.get() );
|
m_actualWidget = new LocalePage( m_config.get() );
|
||||||
}
|
}
|
||||||
m_config->setCurrentLocation( m_startingTimezone.first, m_startingTimezone.second );
|
|
||||||
m_widget->layout()->addWidget( m_actualWidget );
|
m_widget->layout()->addWidget( m_actualWidget );
|
||||||
|
|
||||||
ensureSize( m_actualWidget->sizeHint() );
|
ensureSize( m_actualWidget->sizeHint() );
|
||||||
@ -80,20 +79,6 @@ LocaleViewStep::setUpPage()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
LocaleViewStep::fetchGeoIpTimezone()
|
|
||||||
{
|
|
||||||
if ( m_geoip && m_geoip->isValid() )
|
|
||||||
{
|
|
||||||
m_startingTimezone = m_geoip->get();
|
|
||||||
if ( !m_startingTimezone.isValid() )
|
|
||||||
{
|
|
||||||
cWarning() << "GeoIP lookup at" << m_geoip->url() << "failed.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
QString
|
QString
|
||||||
LocaleViewStep::prettyName() const
|
LocaleViewStep::prettyName() const
|
||||||
{
|
{
|
||||||
@ -171,54 +156,5 @@ LocaleViewStep::onLeave()
|
|||||||
void
|
void
|
||||||
LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
|
||||||
{
|
{
|
||||||
QString region = CalamaresUtils::getString( configurationMap, "region" );
|
|
||||||
QString zone = CalamaresUtils::getString( configurationMap, "zone" );
|
|
||||||
if ( !region.isEmpty() && !zone.isEmpty() )
|
|
||||||
{
|
|
||||||
m_startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( region, zone );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_startingTimezone
|
|
||||||
= CalamaresUtils::GeoIP::RegionZonePair( QStringLiteral( "America" ), QStringLiteral( "New_York" ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = false;
|
|
||||||
QVariantMap geoip = CalamaresUtils::getSubMap( configurationMap, "geoip", ok );
|
|
||||||
if ( ok )
|
|
||||||
{
|
|
||||||
QString url = CalamaresUtils::getString( geoip, "url" );
|
|
||||||
QString style = CalamaresUtils::getString( geoip, "style" );
|
|
||||||
QString selector = CalamaresUtils::getString( geoip, "selector" );
|
|
||||||
|
|
||||||
m_geoip = std::make_unique< CalamaresUtils::GeoIP::Handler >( style, url, selector );
|
|
||||||
if ( !m_geoip->isValid() )
|
|
||||||
{
|
|
||||||
cWarning() << "GeoIP Style" << style << "is not recognized.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_config->setConfigurationMap( configurationMap );
|
m_config->setConfigurationMap( configurationMap );
|
||||||
}
|
}
|
||||||
|
|
||||||
Calamares::RequirementsList
|
|
||||||
LocaleViewStep::checkRequirements()
|
|
||||||
{
|
|
||||||
if ( m_geoip && m_geoip->isValid() )
|
|
||||||
{
|
|
||||||
auto& network = CalamaresUtils::Network::Manager::instance();
|
|
||||||
if ( network.hasInternet() )
|
|
||||||
{
|
|
||||||
fetchGeoIpTimezone();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ( network.synchronousPing( m_geoip->url() ) )
|
|
||||||
{
|
|
||||||
fetchGeoIpTimezone();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Calamares::RequirementsList();
|
|
||||||
}
|
|
||||||
|
@ -58,22 +58,15 @@ public:
|
|||||||
|
|
||||||
void setConfigurationMap( const QVariantMap& configurationMap ) override;
|
void setConfigurationMap( const QVariantMap& configurationMap ) override;
|
||||||
|
|
||||||
/// @brief Do setup (returns empty list) asynchronously
|
|
||||||
virtual Calamares::RequirementsList checkRequirements() override;
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void setUpPage();
|
void setUpPage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetchGeoIpTimezone();
|
|
||||||
QWidget* m_widget;
|
QWidget* m_widget;
|
||||||
|
|
||||||
LocalePage* m_actualWidget;
|
LocalePage* m_actualWidget;
|
||||||
bool m_nextEnabled;
|
bool m_nextEnabled;
|
||||||
|
|
||||||
CalamaresUtils::GeoIP::RegionZonePair m_startingTimezone;
|
|
||||||
std::unique_ptr< CalamaresUtils::GeoIP::Handler > m_geoip;
|
|
||||||
|
|
||||||
std::unique_ptr< Config > m_config;
|
std::unique_ptr< Config > m_config;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user