From a25d61077fa09d835387caa9384a6f7a054ed315 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 22 Jul 2020 11:53:06 +0200 Subject: [PATCH] [locale] Add GeoIP settings to Config - this doesn't do the lookup **yet** - while here, refactor setConfigurationMap so it reads like a story, with chunks bitten out into a handful of static inline void methods. --- src/modules/locale/Config.cpp | 58 ++++++++++++++++++++++++++++------- src/modules/locale/Config.h | 11 +++++++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 0ecd7e049..fc797c0aa 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -327,43 +327,50 @@ Config::currentLCStatus() const .arg( localeLabel( m_selectedLocaleConfiguration.lc_numeric ) ); } - -void -Config::setConfigurationMap( const QVariantMap& configurationMap ) +static inline void +getLocaleGenLines( const QVariantMap& configurationMap, QStringList& localeGenLines ) { QString localeGenPath = CalamaresUtils::getString( configurationMap, "localeGenPath" ); if ( localeGenPath.isEmpty() ) { localeGenPath = QStringLiteral( "/etc/locale.gen" ); } - m_localeGenLines = loadLocales( localeGenPath ); + localeGenLines = loadLocales( localeGenPath ); +} - m_adjustLiveTimezone +static inline void +getAdjustLiveTimezone( const QVariantMap& configurationMap, bool& adjustLiveTimezone ) +{ + adjustLiveTimezone = CalamaresUtils::getBool( configurationMap, "adjustLiveTimezone", Calamares::Settings::instance()->doChroot() ); #ifdef DEBUG_TIMEZONES if ( m_adjustLiveTimezone ) { cWarning() << "Turning off live-timezone adjustments because debugging is on."; - m_adjustLiveTimezone = false; + adjustLiveTimezone = false; } #endif #ifdef __FreeBSD__ - if ( m_adjustLiveTimezone ) + if ( adjustLiveTimezone ) { cWarning() << "Turning off live-timezone adjustments on FreeBSD."; - m_adjustLiveTimezone = false; + adjustLiveTimezone = false; } #endif +} +static inline void +getStartingTimezone( const QVariantMap& configurationMap, CalamaresUtils::GeoIP::RegionZonePair& startingTimezone ) +{ QString region = CalamaresUtils::getString( configurationMap, "region" ); QString zone = CalamaresUtils::getString( configurationMap, "zone" ); if ( !region.isEmpty() && !zone.isEmpty() ) { - m_startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( region, zone ); + startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( region, zone ); } else { - m_startingTimezone + startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( QStringLiteral( "America" ), QStringLiteral( "New_York" ) ); } @@ -372,11 +379,40 @@ Config::setConfigurationMap( const QVariantMap& configurationMap ) auto systemtz = CalamaresUtils::GeoIP::splitTZString( QTimeZone::systemTimeZoneId() ); if ( systemtz.isValid() ) { - m_startingTimezone = systemtz; + cDebug() << "Overriding configured timezone" << startingTimezone << "with system timezone" << systemtz; + startingTimezone = systemtz; } } } +static inline void +getGeoIP( const QVariantMap& configurationMap, std::unique_ptr< CalamaresUtils::GeoIP::Handler >& geoip ) +{ + bool ok = false; + QVariantMap map = CalamaresUtils::getSubMap( configurationMap, "geoip", ok ); + if ( ok ) + { + QString url = CalamaresUtils::getString( map, "url" ); + QString style = CalamaresUtils::getString( map, "style" ); + QString selector = CalamaresUtils::getString( map, "selector" ); + + geoip = std::make_unique< CalamaresUtils::GeoIP::Handler >( style, url, selector ); + if ( !geoip->isValid() ) + { + cWarning() << "GeoIP Style" << style << "is not recognized."; + } + } +} + +void +Config::setConfigurationMap( const QVariantMap& configurationMap ) +{ + getLocaleGenLines( configurationMap, m_localeGenLines ); + getAdjustLiveTimezone( configurationMap, m_adjustLiveTimezone ); + getStartingTimezone( configurationMap, m_startingTimezone ); + getGeoIP( configurationMap, m_geoip ); +} + Calamares::JobList Config::createJobs() { diff --git a/src/modules/locale/Config.h b/src/modules/locale/Config.h index 7e634a4a8..421bb7998 100644 --- a/src/modules/locale/Config.h +++ b/src/modules/locale/Config.h @@ -24,6 +24,7 @@ #include "LocaleConfiguration.h" #include "Job.h" +#include "geoip/Handler.h" #include "geoip/Interface.h" #include "locale/TimeZone.h" @@ -136,8 +137,18 @@ private: bool m_adjustLiveTimezone; /** @brief The initial timezone (region, zone) specified in the config. + * + * This may be overridden by setting *useSystemTimezone* or by + * GeoIP settings. */ CalamaresUtils::GeoIP::RegionZonePair m_startingTimezone; + + /** @brief Handler for GeoIP lookup (if configured) + * + * The GeoIP lookup needs to be started at some suitable time, + * by explicitly calling *TODO* + */ + std::unique_ptr< CalamaresUtils::GeoIP::Handler > m_geoip; };