diff --git a/src/modules/locale/Config.cpp b/src/modules/locale/Config.cpp index 5a97339a8..c19569d43 100644 --- a/src/modules/locale/Config.cpp +++ b/src/modules/locale/Config.cpp @@ -175,7 +175,8 @@ Config::setCurrentLocation( const QString& regionName, const QString& zoneName ) } else { - setCurrentLocation( QStringLiteral("America"), QStringLiteral("New_York") ); + // Recursive, but America/New_York always exists. + setCurrentLocation( QStringLiteral( "America" ), QStringLiteral( "New_York" ) ); } } @@ -185,10 +186,66 @@ Config::setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ) if ( location != m_currentLocation ) { m_currentLocation = location; + // Overwrite those settings that have not been made explicit. + auto newLocale = automaticLocaleConfiguration(); + if ( !m_selectedLocaleConfiguration.explicit_lang ) + { + m_selectedLocaleConfiguration.setLanguage( newLocale.language() ); + } + 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; + } emit currentLocationChanged( m_currentLocation ); } } + +LocaleConfiguration +Config::automaticLocaleConfiguration() const +{ + 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; +} + +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; +} + + void Config::setConfigurationMap( const QVariantMap& configurationMap ) { @@ -204,7 +261,7 @@ Calamares::JobList Config::createJobs() { Calamares::JobList list; - const CalamaresUtils::Locale::TZZone* location = nullptr; // TODO: m_tzWidget->currentLocation(); + const CalamaresUtils::Locale::TZZone* location = currentLocation(); if ( location ) { diff --git a/src/modules/locale/Config.h b/src/modules/locale/Config.h index 44e7c9142..ff9125c13 100644 --- a/src/modules/locale/Config.h +++ b/src/modules/locale/Config.h @@ -21,6 +21,8 @@ #ifndef LOCALE_CONFIG_H #define LOCALE_CONFIG_H +#include "LocaleConfiguration.h" + #include "Job.h" #include "locale/TimeZone.h" @@ -35,7 +37,8 @@ class Config : public QObject Q_PROPERTY( CalamaresUtils::Locale::CStringListModel* zonesModel READ zonesModel CONSTANT FINAL ) Q_PROPERTY( CalamaresUtils::Locale::CStringListModel* regionModel READ regionModel CONSTANT FINAL ) Q_PROPERTY( const CalamaresUtils::Locale::CStringPairList& timezoneData READ timezoneData CONSTANT FINAL ) - Q_PROPERTY( const CalamaresUtils::Locale::TZZone* currentLocation READ currentLocation WRITE setCurrentLocation NOTIFY currentLocationChanged ) + Q_PROPERTY( const CalamaresUtils::Locale::TZZone* currentLocation READ currentLocation WRITE setCurrentLocation + NOTIFY currentLocationChanged ) public: Config( QObject* parent = nullptr ); @@ -60,11 +63,29 @@ public Q_SLOTS: /** @brief Sets a location by pointer * * Pointer should be within the same model as the widget uses. + * This can update the locale configuration -- the automatic one + * follows the current location, and otherwise only explicitly-set + * values will ignore changes to the location. */ void setCurrentLocation( const CalamaresUtils::Locale::TZZone* location ); + /** @brief The currently selected location (timezone) + * + * 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; } + /// locale configuration (LC_* and LANG) based solely on the current location. + LocaleConfiguration automaticLocaleConfiguration() const; + /// locale configuration that takes explicit settings into account + LocaleConfiguration localeConfiguration() const; + + /// Set a language by user-choice, overriding future location changes + void setLanguageExplicitly( const QString& language ); + /// Set LC (formats) by user-choice, overriding future location changes + void setLCLocaleExplicitly( const QString& locale ); + signals: void currentLocationChanged( const CalamaresUtils::Locale::TZZone* location ); @@ -80,6 +101,16 @@ private: /// The location, points into the timezone data const CalamaresUtils::Locale::TZZone* m_currentLocation = nullptr; + /** @brief Specific locale configurations + * + * "Automatic" locale configuration based on the location (e.g. + * Europe/Amsterdam means Dutch language and Dutch locale) leave + * this empty; if the user explicitly sets something, then + * this configuration is non-empty and takes precedence over the + * automatic location settings (so a user in Amsterdam can still + * pick Ukranian settings, for instance). + */ + LocaleConfiguration m_selectedLocaleConfiguration; }; diff --git a/src/modules/locale/LocalePage.cpp b/src/modules/locale/LocalePage.cpp index abb4b63bc..d09d38540 100644 --- a/src/modules/locale/LocalePage.cpp +++ b/src/modules/locale/LocalePage.cpp @@ -137,8 +137,7 @@ LocalePage::updateLocaleLabels() m_localeChangeButton->setText( tr( "&Change..." ) ); m_formatsChangeButton->setText( tr( "&Change..." ) ); - LocaleConfiguration lc - = m_selectedLocaleConfiguration.isEmpty() ? guessLocaleConfiguration() : m_selectedLocaleConfiguration; + LocaleConfiguration lc = m_config->localeConfiguration(); auto labels = prettyLocaleStatus( lc ); m_localeLabel->setText( labels.first ); m_formatsLabel->setText( labels.second ); @@ -163,8 +162,7 @@ LocalePage::prettyStatus() const QString status; status += tr( "Set timezone to %1/%2.
" ).arg( m_regionCombo->currentText() ).arg( m_zoneCombo->currentText() ); - LocaleConfiguration lc - = m_selectedLocaleConfiguration.isEmpty() ? guessLocaleConfiguration() : m_selectedLocaleConfiguration; + LocaleConfiguration lc = m_config->localeConfiguration(); auto labels = prettyLocaleStatus( lc ); status += labels.first + "
"; status += labels.second + "
"; @@ -176,8 +174,7 @@ LocalePage::prettyStatus() const QMap< QString, QString > LocalePage::localesMap() { - return m_selectedLocaleConfiguration.isEmpty() ? guessLocaleConfiguration().toMap() - : m_selectedLocaleConfiguration.toMap(); + return m_config->localeConfiguration().toMap(); } @@ -185,21 +182,8 @@ void LocalePage::onActivate() { m_regionCombo->setFocus(); - if ( m_selectedLocaleConfiguration.isEmpty() || !m_selectedLocaleConfiguration.explicit_lang ) - { - auto newLocale = guessLocaleConfiguration(); - m_selectedLocaleConfiguration.setLanguage( newLocale.language() ); - updateGlobalLocale(); - updateLocaleLabels(); - } -} - - -LocaleConfiguration -LocalePage::guessLocaleConfiguration() const -{ - return LocaleConfiguration::fromLanguageAndLocation( - QLocale().name(), m_config->supportedLocales(), m_config->currentLocation()->country() ); + updateGlobalLocale(); + updateLocaleLabels(); } @@ -207,7 +191,7 @@ void LocalePage::updateGlobalLocale() { auto* gs = Calamares::JobQueue::instance()->globalStorage(); - const QString bcp47 = m_selectedLocaleConfiguration.toBcp47(); + const QString bcp47 = m_config->localeConfiguration().toBcp47(); gs->insert( "locale", bcp47 ); } @@ -236,28 +220,6 @@ LocalePage::updateGlobalStorage() } #endif - // Preserve those settings that have been made explicit. - auto newLocale = guessLocaleConfiguration(); - if ( !m_selectedLocaleConfiguration.isEmpty() && m_selectedLocaleConfiguration.explicit_lang ) - { - newLocale.setLanguage( m_selectedLocaleConfiguration.language() ); - } - if ( !m_selectedLocaleConfiguration.isEmpty() && m_selectedLocaleConfiguration.explicit_lc ) - { - newLocale.lc_numeric = m_selectedLocaleConfiguration.lc_numeric; - newLocale.lc_time = m_selectedLocaleConfiguration.lc_time; - newLocale.lc_monetary = m_selectedLocaleConfiguration.lc_monetary; - newLocale.lc_paper = m_selectedLocaleConfiguration.lc_paper; - newLocale.lc_name = m_selectedLocaleConfiguration.lc_name; - newLocale.lc_address = m_selectedLocaleConfiguration.lc_address; - newLocale.lc_telephone = m_selectedLocaleConfiguration.lc_telephone; - newLocale.lc_measurement = m_selectedLocaleConfiguration.lc_measurement; - newLocale.lc_identification = m_selectedLocaleConfiguration.lc_identification; - } - newLocale.explicit_lang = m_selectedLocaleConfiguration.explicit_lang; - newLocale.explicit_lc = m_selectedLocaleConfiguration.explicit_lc; - - m_selectedLocaleConfiguration = newLocale; updateLocaleLabels(); } @@ -324,17 +286,13 @@ void LocalePage::changeLocale() { LCLocaleDialog* dlg - = new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ? guessLocaleConfiguration().language() - : m_selectedLocaleConfiguration.language(), - m_config->supportedLocales(), - this ); + = new LCLocaleDialog( m_config->localeConfiguration().language(), m_config->supportedLocales(), this ); dlg->exec(); if ( dlg->result() == QDialog::Accepted && !dlg->selectedLCLocale().isEmpty() ) { - m_selectedLocaleConfiguration.setLanguage( dlg->selectedLCLocale() ); - m_selectedLocaleConfiguration.explicit_lang = true; - this->updateGlobalLocale(); - this->updateLocaleLabels(); + m_config->setLanguageExplicitly( dlg->selectedLCLocale() ); + updateGlobalLocale(); + updateLocaleLabels(); } dlg->deleteLater(); @@ -345,26 +303,12 @@ void LocalePage::changeFormats() { LCLocaleDialog* dlg - = new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ? guessLocaleConfiguration().lc_numeric - : m_selectedLocaleConfiguration.lc_numeric, - m_config->supportedLocales(), - this ); + = new LCLocaleDialog( m_config->localeConfiguration().lc_numeric, m_config->supportedLocales(), this ); dlg->exec(); if ( dlg->result() == QDialog::Accepted && !dlg->selectedLCLocale().isEmpty() ) { - // TODO: improve the granularity of this setting. - m_selectedLocaleConfiguration.lc_numeric = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_time = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_monetary = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_paper = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_name = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_address = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_telephone = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_measurement = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.lc_identification = dlg->selectedLCLocale(); - m_selectedLocaleConfiguration.explicit_lc = true; - - this->updateLocaleLabels(); + m_config->setLCLocaleExplicitly( dlg->selectedLCLocale() ); + updateLocaleLabels(); } dlg->deleteLater(); diff --git a/src/modules/locale/LocalePage.h b/src/modules/locale/LocalePage.h index 3d4b1d03f..9690c4d97 100644 --- a/src/modules/locale/LocalePage.h +++ b/src/modules/locale/LocalePage.h @@ -50,8 +50,6 @@ public: void onActivate(); private: - LocaleConfiguration guessLocaleConfiguration() const; - /// @brief Non-owning pointer to the ViewStep's config Config* m_config; @@ -85,7 +83,6 @@ private: QLabel* m_formatsLabel; QPushButton* m_formatsChangeButton; - LocaleConfiguration m_selectedLocaleConfiguration; bool m_blockTzWidgetSet; };