2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-03-24 15:47:53 +01:00
|
|
|
*
|
2020-07-20 12:37:27 +02:00
|
|
|
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-24 15:47:53 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-03-24 15:47:53 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
|
2020-07-20 22:21:29 +02:00
|
|
|
#include "SetTimezoneJob.h"
|
|
|
|
|
2020-07-21 17:37:09 +02:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "Settings.h"
|
2020-07-21 15:51:49 +02:00
|
|
|
#include "locale/Label.h"
|
2020-07-22 14:47:43 +02:00
|
|
|
#include "modulesystem/ModuleManager.h"
|
|
|
|
#include "network/Manager.h"
|
2020-03-24 15:47:53 +01:00
|
|
|
#include "utils/Logger.h"
|
2020-07-20 12:55:07 +02:00
|
|
|
#include "utils/Variant.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QProcess>
|
2020-07-22 01:26:15 +02:00
|
|
|
#include <QTimeZone>
|
2020-07-20 12:55:07 +02:00
|
|
|
|
|
|
|
/** @brief Load supported locale keys
|
|
|
|
*
|
|
|
|
* If i18n/SUPPORTED exists, read the lines from that and return those
|
|
|
|
* as supported locales; otherwise, try the file at @p localeGenPath
|
|
|
|
* and get lines from that. Failing both, try the output of `locale -a`.
|
|
|
|
*
|
|
|
|
* This gives us a list of locale identifiers (e.g. en_US.UTF-8), which
|
|
|
|
* are not particularly human-readable.
|
|
|
|
*
|
|
|
|
* Only UTF-8 locales are returned (even if the system claims to support
|
|
|
|
* other, non-UTF-8, locales).
|
|
|
|
*/
|
|
|
|
static QStringList
|
|
|
|
loadLocales( const QString& localeGenPath )
|
|
|
|
{
|
|
|
|
QStringList localeGenLines;
|
|
|
|
|
|
|
|
// Some distros come with a meaningfully commented and easy to parse locale.gen,
|
|
|
|
// and others ship a separate file /usr/share/i18n/SUPPORTED with a clean list of
|
|
|
|
// supported locales. We first try that one, and if it doesn't exist, we fall back
|
|
|
|
// to parsing the lines from locale.gen
|
|
|
|
localeGenLines.clear();
|
|
|
|
QFile supported( "/usr/share/i18n/SUPPORTED" );
|
|
|
|
QByteArray ba;
|
|
|
|
|
|
|
|
if ( supported.exists() && supported.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
|
|
{
|
|
|
|
ba = supported.readAll();
|
|
|
|
supported.close();
|
|
|
|
|
|
|
|
const auto lines = ba.split( '\n' );
|
|
|
|
for ( const QByteArray& line : lines )
|
|
|
|
{
|
|
|
|
localeGenLines.append( QString::fromLatin1( line.simplified() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QFile localeGen( localeGenPath );
|
|
|
|
if ( localeGen.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
|
|
{
|
|
|
|
ba = localeGen.readAll();
|
|
|
|
localeGen.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Cannot open file" << localeGenPath
|
|
|
|
<< ". Assuming the supported languages are already built into "
|
|
|
|
"the locale archive.";
|
|
|
|
QProcess localeA;
|
|
|
|
localeA.start( "locale", QStringList() << "-a" );
|
|
|
|
localeA.waitForFinished();
|
|
|
|
ba = localeA.readAllStandardOutput();
|
|
|
|
}
|
|
|
|
const auto lines = ba.split( '\n' );
|
|
|
|
for ( const QByteArray& line : lines )
|
|
|
|
{
|
|
|
|
if ( line.startsWith( "## " ) || line.startsWith( "# " ) || line.simplified() == "#" )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString lineString = QString::fromLatin1( line.simplified() );
|
|
|
|
if ( lineString.startsWith( "#" ) )
|
|
|
|
{
|
|
|
|
lineString.remove( '#' );
|
|
|
|
}
|
|
|
|
lineString = lineString.simplified();
|
|
|
|
|
|
|
|
if ( lineString.isEmpty() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
localeGenLines.append( lineString );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( localeGenLines.isEmpty() )
|
|
|
|
{
|
|
|
|
cWarning() << "cannot acquire a list of available locales."
|
|
|
|
<< "The locale and localecfg modules will be broken as long as this "
|
|
|
|
"system does not provide"
|
|
|
|
<< "\n\t "
|
|
|
|
<< "* a well-formed" << supported.fileName() << "\n\tOR"
|
|
|
|
<< "* a well-formed"
|
|
|
|
<< ( localeGenPath.isEmpty() ? QLatin1String( "/etc/locale.gen" ) : localeGenPath ) << "\n\tOR"
|
|
|
|
<< "* a complete pre-compiled locale-gen database which allows complete locale -a output.";
|
|
|
|
return localeGenLines; // something went wrong and there's nothing we can do about it.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assuming we have a list of supported locales, we usually only want UTF-8 ones
|
|
|
|
// because it's not 1995.
|
|
|
|
auto notUtf8 = []( const QString& s ) {
|
|
|
|
return !s.contains( "UTF-8", Qt::CaseInsensitive ) && !s.contains( "utf8", Qt::CaseInsensitive );
|
|
|
|
};
|
|
|
|
auto it = std::remove_if( localeGenLines.begin(), localeGenLines.end(), notUtf8 );
|
|
|
|
localeGenLines.erase( it, localeGenLines.end() );
|
|
|
|
|
|
|
|
// We strip " UTF-8" from "en_US.UTF-8 UTF-8" because it's redundant redundant.
|
|
|
|
// Also simplify whitespace.
|
|
|
|
auto unredundant = []( QString& s ) {
|
|
|
|
if ( s.endsWith( " UTF-8" ) )
|
|
|
|
{
|
|
|
|
s.chop( 6 );
|
|
|
|
}
|
|
|
|
s = s.simplified();
|
|
|
|
};
|
|
|
|
std::for_each( localeGenLines.begin(), localeGenLines.end(), unredundant );
|
|
|
|
|
|
|
|
return localeGenLines;
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:32:51 +02:00
|
|
|
static bool
|
|
|
|
updateGSLocation( Calamares::GlobalStorage* gs, const CalamaresUtils::Locale::TimeZoneData* location )
|
|
|
|
{
|
|
|
|
const QString regionKey = QStringLiteral( "locationRegion" );
|
|
|
|
const QString zoneKey = QStringLiteral( "locationZone" );
|
|
|
|
|
|
|
|
if ( !location )
|
|
|
|
{
|
|
|
|
if ( gs->contains( regionKey ) || gs->contains( zoneKey ) )
|
|
|
|
{
|
|
|
|
gs->remove( regionKey );
|
|
|
|
gs->remove( zoneKey );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the GS region and zone (and possibly the live timezone)
|
|
|
|
bool locationChanged
|
|
|
|
= ( location->region() != gs->value( regionKey ) ) || ( location->zone() != gs->value( zoneKey ) );
|
|
|
|
|
|
|
|
gs->insert( regionKey, location->region() );
|
|
|
|
gs->insert( zoneKey, location->zone() );
|
|
|
|
|
|
|
|
return locationChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
updateGSLocale( Calamares::GlobalStorage* gs, const LocaleConfiguration& locale )
|
|
|
|
{
|
|
|
|
auto map = locale.toMap();
|
|
|
|
QVariantMap vm;
|
|
|
|
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
|
|
|
|
{
|
|
|
|
vm.insert( it.key(), it.value() );
|
|
|
|
}
|
|
|
|
gs->insert( "localeConf", vm );
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:48:14 +01:00
|
|
|
Config::Config( QObject* parent )
|
|
|
|
: QObject( parent )
|
2020-08-06 01:27:03 +02:00
|
|
|
, m_regionModel( std::make_unique< CalamaresUtils::Locale::RegionsModel >() )
|
|
|
|
, m_zonesModel( std::make_unique< CalamaresUtils::Locale::ZonesModel >() )
|
|
|
|
, m_regionalZonesModel( std::make_unique< CalamaresUtils::Locale::RegionalZonesModel >( m_zonesModel.get() ) )
|
2020-03-24 15:47:53 +01:00
|
|
|
{
|
2020-07-21 17:37:09 +02:00
|
|
|
// Slightly unusual: connect to our *own* signals. Wherever the language
|
|
|
|
// or the location is changed, these signals are emitted, so hook up to
|
|
|
|
// them to update global storage accordingly. This simplifies code:
|
|
|
|
// we don't need to call an update-GS method, or introduce an intermediate
|
|
|
|
// update-thing-and-GS method. And everywhere where we **do** change
|
|
|
|
// language or location, we already emit the signal.
|
2020-07-23 23:30:43 +02:00
|
|
|
connect( this, &Config::currentLanguageCodeChanged, [&]() {
|
2020-07-21 17:37:09 +02:00
|
|
|
auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
gs->insert( "locale", m_selectedLocaleConfiguration.toBcp47() );
|
|
|
|
} );
|
|
|
|
|
2020-07-23 23:30:43 +02:00
|
|
|
connect( this, &Config::currentLCCodeChanged, [&]() {
|
2020-08-06 18:32:51 +02:00
|
|
|
updateGSLocale( Calamares::JobQueue::instance()->globalStorage(), localeConfiguration() );
|
2020-07-23 23:30:43 +02:00
|
|
|
} );
|
|
|
|
|
2020-07-21 17:37:09 +02:00
|
|
|
connect( this, &Config::currentLocationChanged, [&]() {
|
2020-08-06 18:32:51 +02:00
|
|
|
const bool locationChanged
|
|
|
|
= updateGSLocation( Calamares::JobQueue::instance()->globalStorage(), currentLocation() );
|
2020-07-22 00:23:50 +02:00
|
|
|
|
2020-07-22 00:06:56 +02:00
|
|
|
if ( locationChanged && m_adjustLiveTimezone )
|
2020-07-21 17:37:09 +02:00
|
|
|
{
|
|
|
|
QProcess::execute( "timedatectl", // depends on systemd
|
2020-08-06 18:32:51 +02:00
|
|
|
{ "set-timezone", currentTimezoneCode() } );
|
2020-07-21 17:37:09 +02:00
|
|
|
}
|
2020-08-06 12:40:24 +02:00
|
|
|
|
|
|
|
emit currentTimezoneCodeChanged( currentTimezoneCode() );
|
|
|
|
emit currentTimezoneNameChanged( currentTimezoneName() );
|
2020-07-21 17:37:09 +02:00
|
|
|
} );
|
2020-07-24 11:53:32 +02:00
|
|
|
|
|
|
|
auto prettyStatusNotify = [&]() { emit prettyStatusChanged( prettyStatus() ); };
|
|
|
|
connect( this, &Config::currentLanguageStatusChanged, prettyStatusNotify );
|
|
|
|
connect( this, &Config::currentLCStatusChanged, prettyStatusNotify );
|
|
|
|
connect( this, &Config::currentLocationStatusChanged, prettyStatusNotify );
|
2020-03-24 15:47:53 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:37:27 +02:00
|
|
|
Config::~Config() {}
|
2020-03-24 15:47:53 +01:00
|
|
|
|
2020-07-22 14:47:43 +02:00
|
|
|
void
|
|
|
|
Config::setCurrentLocation()
|
|
|
|
{
|
|
|
|
if ( !m_currentLocation && m_startingTimezone.isValid() )
|
|
|
|
{
|
|
|
|
setCurrentLocation( m_startingTimezone.first, m_startingTimezone.second );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:27:03 +02:00
|
|
|
void
|
|
|
|
Config::setCurrentLocation( const QString& regionzone )
|
2020-07-24 11:07:58 +02:00
|
|
|
{
|
|
|
|
auto r = CalamaresUtils::GeoIP::splitTZString( regionzone );
|
|
|
|
if ( r.isValid() )
|
|
|
|
{
|
|
|
|
setCurrentLocation( r.first, r.second );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:06:12 +02:00
|
|
|
void
|
|
|
|
Config::setCurrentLocation( const QString& regionName, const QString& zoneName )
|
|
|
|
{
|
|
|
|
using namespace CalamaresUtils::Locale;
|
2020-08-06 01:27:03 +02:00
|
|
|
auto* zone = m_zonesModel->find( regionName, zoneName );
|
2020-07-20 23:06:12 +02:00
|
|
|
if ( zone )
|
|
|
|
{
|
|
|
|
setCurrentLocation( zone );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-21 13:16:52 +02:00
|
|
|
// Recursive, but America/New_York always exists.
|
|
|
|
setCurrentLocation( QStringLiteral( "America" ), QStringLiteral( "New_York" ) );
|
2020-07-20 23:06:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-08-06 01:27:03 +02:00
|
|
|
Config::setCurrentLocation( const CalamaresUtils::Locale::TimeZoneData* location )
|
2020-07-20 23:06:12 +02:00
|
|
|
{
|
|
|
|
if ( location != m_currentLocation )
|
|
|
|
{
|
|
|
|
m_currentLocation = location;
|
2020-07-21 13:16:52 +02:00
|
|
|
// Overwrite those settings that have not been made explicit.
|
|
|
|
auto newLocale = automaticLocaleConfiguration();
|
|
|
|
if ( !m_selectedLocaleConfiguration.explicit_lang )
|
|
|
|
{
|
|
|
|
m_selectedLocaleConfiguration.setLanguage( newLocale.language() );
|
2020-07-21 16:27:21 +02:00
|
|
|
emit currentLanguageStatusChanged( currentLanguageStatus() );
|
2020-07-21 13:16:52 +02:00
|
|
|
}
|
|
|
|
if ( !m_selectedLocaleConfiguration.explicit_lc )
|
|
|
|
{
|
|
|
|
m_selectedLocaleConfiguration.lc_numeric = newLocale.lc_numeric;
|
|
|
|
m_selectedLocaleConfiguration.lc_time = newLocale.lc_time;
|
|
|
|
m_selectedLocaleConfiguration.lc_monetary = newLocale.lc_monetary;
|
|
|
|
m_selectedLocaleConfiguration.lc_paper = newLocale.lc_paper;
|
|
|
|
m_selectedLocaleConfiguration.lc_name = newLocale.lc_name;
|
|
|
|
m_selectedLocaleConfiguration.lc_address = newLocale.lc_address;
|
|
|
|
m_selectedLocaleConfiguration.lc_telephone = newLocale.lc_telephone;
|
|
|
|
m_selectedLocaleConfiguration.lc_measurement = newLocale.lc_measurement;
|
|
|
|
m_selectedLocaleConfiguration.lc_identification = newLocale.lc_identification;
|
2020-07-21 16:27:21 +02:00
|
|
|
|
|
|
|
emit currentLCStatusChanged( currentLCStatus() );
|
2020-07-21 13:16:52 +02:00
|
|
|
}
|
2020-07-20 23:06:12 +02:00
|
|
|
emit currentLocationChanged( m_currentLocation );
|
2020-08-06 12:40:24 +02:00
|
|
|
// Other signals come from the LocationChanged signal
|
2020-07-20 23:06:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 16:54:44 +02:00
|
|
|
|
2020-07-21 13:16:52 +02:00
|
|
|
LocaleConfiguration
|
|
|
|
Config::automaticLocaleConfiguration() const
|
|
|
|
{
|
2020-07-22 00:32:29 +02:00
|
|
|
// Special case: no location has been set at **all**
|
|
|
|
if ( !currentLocation() )
|
|
|
|
{
|
|
|
|
return LocaleConfiguration();
|
|
|
|
}
|
2020-07-21 13:16:52 +02:00
|
|
|
return LocaleConfiguration::fromLanguageAndLocation(
|
|
|
|
QLocale().name(), supportedLocales(), currentLocation()->country() );
|
|
|
|
}
|
|
|
|
|
|
|
|
LocaleConfiguration
|
|
|
|
Config::localeConfiguration() const
|
|
|
|
{
|
|
|
|
return m_selectedLocaleConfiguration.isEmpty() ? automaticLocaleConfiguration() : m_selectedLocaleConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setLanguageExplicitly( const QString& language )
|
|
|
|
{
|
|
|
|
m_selectedLocaleConfiguration.setLanguage( language );
|
|
|
|
m_selectedLocaleConfiguration.explicit_lang = true;
|
2020-07-21 16:27:21 +02:00
|
|
|
|
|
|
|
emit currentLanguageStatusChanged( currentLanguageStatus() );
|
2020-07-23 23:25:52 +02:00
|
|
|
emit currentLanguageCodeChanged( currentLanguageCode() );
|
2020-07-21 13:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setLCLocaleExplicitly( const QString& locale )
|
|
|
|
{
|
|
|
|
// TODO: improve the granularity of this setting.
|
|
|
|
m_selectedLocaleConfiguration.lc_numeric = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_time = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_monetary = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_paper = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_name = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_address = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_telephone = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_measurement = locale;
|
|
|
|
m_selectedLocaleConfiguration.lc_identification = locale;
|
|
|
|
m_selectedLocaleConfiguration.explicit_lc = true;
|
2020-07-21 16:27:21 +02:00
|
|
|
|
|
|
|
emit currentLCStatusChanged( currentLCStatus() );
|
2020-07-23 23:25:52 +02:00
|
|
|
emit currentLCCodeChanged( currentLCCode() );
|
2020-07-21 13:16:52 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 16:27:21 +02:00
|
|
|
QString
|
|
|
|
Config::currentLocationStatus() const
|
2020-07-21 15:51:49 +02:00
|
|
|
{
|
2020-08-06 12:38:55 +02:00
|
|
|
return tr( "Set timezone to %1/%2." )
|
|
|
|
.arg( m_currentLocation ? m_currentLocation->region() : QString(),
|
|
|
|
m_currentLocation ? m_currentLocation->zone() : QString() );
|
2020-07-21 16:27:21 +02:00
|
|
|
}
|
2020-07-21 15:51:49 +02:00
|
|
|
|
2020-08-06 12:40:24 +02:00
|
|
|
QString
|
|
|
|
Config::currentTimezoneCode() const
|
|
|
|
{
|
|
|
|
if ( m_currentLocation )
|
|
|
|
{
|
|
|
|
return m_currentLocation->region() + '/' + m_currentLocation->zone();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Config::currentTimezoneName() const
|
|
|
|
{
|
|
|
|
if ( m_currentLocation )
|
|
|
|
{
|
|
|
|
return m_regionModel->tr( m_currentLocation->region() ) + '/' + m_currentLocation->tr();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-21 16:27:21 +02:00
|
|
|
static inline QString
|
|
|
|
localeLabel( const QString& s )
|
|
|
|
{
|
|
|
|
using CalamaresUtils::Locale::Label;
|
2020-07-21 15:51:49 +02:00
|
|
|
|
2020-07-21 16:27:21 +02:00
|
|
|
Label lang( s, Label::LabelFormat::AlwaysWithCountry );
|
|
|
|
return lang.label();
|
2020-07-21 15:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2020-07-21 16:27:21 +02:00
|
|
|
Config::currentLanguageStatus() const
|
2020-07-21 15:51:49 +02:00
|
|
|
{
|
2020-07-21 16:27:21 +02:00
|
|
|
return tr( "The system language will be set to %1." )
|
|
|
|
.arg( localeLabel( m_selectedLocaleConfiguration.language() ) );
|
|
|
|
}
|
2020-07-21 15:51:49 +02:00
|
|
|
|
2020-07-21 16:27:21 +02:00
|
|
|
QString
|
|
|
|
Config::currentLCStatus() const
|
|
|
|
{
|
|
|
|
return tr( "The numbers and dates locale will be set to %1." )
|
|
|
|
.arg( localeLabel( m_selectedLocaleConfiguration.lc_numeric ) );
|
2020-07-21 15:51:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 11:53:32 +02:00
|
|
|
QString
|
|
|
|
Config::prettyStatus() const
|
|
|
|
{
|
|
|
|
QStringList l { currentLocationStatus(), currentLanguageStatus(), currentLCStatus() };
|
|
|
|
return l.join( QStringLiteral( "<br/>" ) );
|
|
|
|
}
|
|
|
|
|
2020-07-22 11:53:06 +02:00
|
|
|
static inline void
|
|
|
|
getLocaleGenLines( const QVariantMap& configurationMap, QStringList& localeGenLines )
|
2020-03-24 15:47:53 +01:00
|
|
|
{
|
2020-07-20 12:55:07 +02:00
|
|
|
QString localeGenPath = CalamaresUtils::getString( configurationMap, "localeGenPath" );
|
|
|
|
if ( localeGenPath.isEmpty() )
|
|
|
|
{
|
|
|
|
localeGenPath = QStringLiteral( "/etc/locale.gen" );
|
|
|
|
}
|
2020-07-22 11:53:06 +02:00
|
|
|
localeGenLines = loadLocales( localeGenPath );
|
|
|
|
}
|
2020-07-22 00:06:56 +02:00
|
|
|
|
2020-07-22 11:53:06 +02:00
|
|
|
static inline void
|
|
|
|
getAdjustLiveTimezone( const QVariantMap& configurationMap, bool& adjustLiveTimezone )
|
|
|
|
{
|
2020-07-22 14:47:43 +02:00
|
|
|
adjustLiveTimezone = CalamaresUtils::getBool(
|
|
|
|
configurationMap, "adjustLiveTimezone", Calamares::Settings::instance()->doChroot() );
|
2020-07-22 00:06:56 +02:00
|
|
|
#ifdef DEBUG_TIMEZONES
|
2020-08-06 12:14:15 +02:00
|
|
|
if ( adjustLiveTimezone )
|
2020-07-22 00:06:56 +02:00
|
|
|
{
|
2020-07-22 01:26:15 +02:00
|
|
|
cWarning() << "Turning off live-timezone adjustments because debugging is on.";
|
2020-07-22 11:53:06 +02:00
|
|
|
adjustLiveTimezone = false;
|
2020-07-22 00:06:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef __FreeBSD__
|
2020-07-22 11:53:06 +02:00
|
|
|
if ( adjustLiveTimezone )
|
2020-07-22 00:06:56 +02:00
|
|
|
{
|
2020-07-22 01:26:15 +02:00
|
|
|
cWarning() << "Turning off live-timezone adjustments on FreeBSD.";
|
2020-07-22 11:53:06 +02:00
|
|
|
adjustLiveTimezone = false;
|
2020-07-22 00:06:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
2020-07-22 11:53:06 +02:00
|
|
|
}
|
2020-07-22 01:26:15 +02:00
|
|
|
|
2020-07-22 11:53:06 +02:00
|
|
|
static inline void
|
|
|
|
getStartingTimezone( const QVariantMap& configurationMap, CalamaresUtils::GeoIP::RegionZonePair& startingTimezone )
|
|
|
|
{
|
2020-07-22 01:26:15 +02:00
|
|
|
QString region = CalamaresUtils::getString( configurationMap, "region" );
|
|
|
|
QString zone = CalamaresUtils::getString( configurationMap, "zone" );
|
|
|
|
if ( !region.isEmpty() && !zone.isEmpty() )
|
|
|
|
{
|
2020-07-22 11:53:06 +02:00
|
|
|
startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( region, zone );
|
2020-07-22 01:26:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-22 11:53:06 +02:00
|
|
|
startingTimezone
|
2020-07-22 01:26:15 +02:00
|
|
|
= CalamaresUtils::GeoIP::RegionZonePair( QStringLiteral( "America" ), QStringLiteral( "New_York" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( CalamaresUtils::getBool( configurationMap, "useSystemTimezone", false ) )
|
|
|
|
{
|
|
|
|
auto systemtz = CalamaresUtils::GeoIP::splitTZString( QTimeZone::systemTimeZoneId() );
|
|
|
|
if ( systemtz.isValid() )
|
|
|
|
{
|
2020-07-22 11:53:06 +02:00
|
|
|
cDebug() << "Overriding configured timezone" << startingTimezone << "with system timezone" << systemtz;
|
|
|
|
startingTimezone = systemtz;
|
2020-07-22 01:26:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-24 15:47:53 +01:00
|
|
|
}
|
2020-07-20 22:21:29 +02:00
|
|
|
|
2020-07-22 11:53:06 +02:00
|
|
|
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 );
|
2020-07-22 14:47:43 +02:00
|
|
|
|
2020-08-06 12:32:48 +02:00
|
|
|
#ifndef BUILD_AS_TEST
|
2020-07-22 14:47:43 +02:00
|
|
|
if ( m_geoip && m_geoip->isValid() )
|
|
|
|
{
|
|
|
|
connect(
|
|
|
|
Calamares::ModuleManager::instance(), &Calamares::ModuleManager::modulesLoaded, this, &Config::startGeoIP );
|
|
|
|
}
|
2020-08-06 12:32:48 +02:00
|
|
|
#endif
|
2020-07-22 11:53:06 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:21:29 +02:00
|
|
|
Calamares::JobList
|
|
|
|
Config::createJobs()
|
|
|
|
{
|
|
|
|
Calamares::JobList list;
|
2020-08-06 01:27:03 +02:00
|
|
|
const auto* location = currentLocation();
|
2020-07-20 22:21:29 +02:00
|
|
|
|
|
|
|
if ( location )
|
|
|
|
{
|
|
|
|
Calamares::Job* j = new SetTimezoneJob( location->region(), location->zone() );
|
|
|
|
list.append( Calamares::job_ptr( j ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2020-07-22 14:47:43 +02:00
|
|
|
|
2020-08-06 18:32:51 +02:00
|
|
|
void
|
|
|
|
Config::finalizeGlobalStorage() const
|
|
|
|
{
|
|
|
|
auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
updateGSLocale( gs, localeConfiguration() );
|
|
|
|
updateGSLocation( gs, currentLocation() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-22 14:47:43 +02:00
|
|
|
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();
|
|
|
|
}
|