[locale] Apply coding style

main
Adriaan de Groot 5 years ago
parent 0ee8427d5a
commit 5cac3ac6ad

@ -25,9 +25,7 @@
#include <QListWidget>
#include <QPushButton>
LCLocaleDialog::LCLocaleDialog( const QString& guessedLCLocale,
const QStringList& localeGenLines,
QWidget* parent )
LCLocaleDialog::LCLocaleDialog( const QString& guessedLCLocale, const QStringList& localeGenLines, QWidget* parent )
: QDialog( parent )
{
setModal( true );
@ -41,7 +39,7 @@ LCLocaleDialog::LCLocaleDialog( const QString& guessedLCLocale,
upperText->setText( tr( "The system locale setting affects the language and character "
"set for some command line user interface elements.<br/>"
"The current setting is <strong>%1</strong>." )
.arg( guessedLCLocale ) );
.arg( guessedLCLocale ) );
mainLayout->addWidget( upperText );
setMinimumWidth( upperText->fontMetrics().height() * 24 );
@ -60,33 +58,32 @@ LCLocaleDialog::LCLocaleDialog( const QString& guessedLCLocale,
}
}
QDialogButtonBox* dbb = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal,
this );
QDialogButtonBox* dbb
= new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
dbb->button( QDialogButtonBox::Cancel )->setText( tr( "&Cancel" ) );
dbb->button( QDialogButtonBox::Ok )->setText( tr( "&OK" ) );
mainLayout->addWidget( dbb );
connect( dbb->button( QDialogButtonBox::Ok ), &QPushButton::clicked,
this, &QDialog::accept );
connect( dbb->button( QDialogButtonBox::Cancel ), &QPushButton::clicked,
this, &QDialog::reject );
connect( dbb->button( QDialogButtonBox::Ok ), &QPushButton::clicked, this, &QDialog::accept );
connect( dbb->button( QDialogButtonBox::Cancel ), &QPushButton::clicked, this, &QDialog::reject );
connect( m_localesWidget, &QListWidget::itemDoubleClicked,
this, &QDialog::accept );
connect( m_localesWidget, &QListWidget::itemSelectionChanged,
[this, dbb]()
{
connect( m_localesWidget, &QListWidget::itemDoubleClicked, this, &QDialog::accept );
connect( m_localesWidget, &QListWidget::itemSelectionChanged, [this, dbb]() {
if ( m_localesWidget->selectedItems().isEmpty() )
{
dbb->button( QDialogButtonBox::Ok )->setEnabled( false );
}
else
{
dbb->button( QDialogButtonBox::Ok )->setEnabled( true );
}
} );
if ( selected > -1 )
{
m_localesWidget->setCurrentRow( selected );
}
}

@ -37,4 +37,4 @@ private:
QListWidget* m_localesWidget;
};
#endif // LCLOCALEDIALOG_H
#endif // LCLOCALEDIALOG_H

@ -30,16 +30,15 @@ LocaleConfiguration::LocaleConfiguration()
LocaleConfiguration::LocaleConfiguration( const QString& localeName, const QString& formatsName )
: LocaleConfiguration()
{
lc_numeric = lc_time = lc_monetary = lc_paper = lc_name
= lc_address = lc_telephone = lc_measurement
= lc_identification = formatsName;
lc_numeric = lc_time = lc_monetary = lc_paper = lc_name = lc_address = lc_telephone = lc_measurement
= lc_identification = formatsName;
(void) setLanguage( localeName );
(void)setLanguage( localeName );
}
void
LocaleConfiguration::setLanguage(const QString& localeName )
LocaleConfiguration::setLanguage( const QString& localeName )
{
QString language = localeName.split( '_' ).first();
m_languageLocaleBcp47 = QLocale( language ).bcp47Name().toLower();
@ -55,30 +54,39 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
QString language = languageLocale.split( '_' ).first();
QStringList linesForLanguage;
for ( const QString &line : availableLocales )
for ( const QString& line : availableLocales )
{
if ( line.startsWith( language ) )
{
linesForLanguage.append( line );
}
}
QString lang;
if ( linesForLanguage.length() == 0 || languageLocale.isEmpty() )
{
lang = "en_US.UTF-8";
}
else if ( linesForLanguage.length() == 1 )
{
lang = linesForLanguage.first();
}
else
{
QStringList linesForLanguageUtf;
// FIXME: this might be useless if we already filter out non-UTF8 locales
foreach ( QString line, linesForLanguage )
{
if ( line.contains( "UTF-8", Qt::CaseInsensitive ) ||
line.contains( "utf8", Qt::CaseInsensitive ) )
if ( line.contains( "UTF-8", Qt::CaseInsensitive ) || line.contains( "utf8", Qt::CaseInsensitive ) )
{
linesForLanguageUtf.append( line );
}
}
if ( linesForLanguageUtf.length() == 1 )
{
lang = linesForLanguageUtf.first();
}
}
// lang could still be empty if we found multiple locales that satisfy myLanguage
@ -92,8 +100,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
# locale categories reflect the selected location. */
if ( language == "pt" || language == "zh" )
{
QString proposedLocale = QString( "%1_%2" ).arg( language )
.arg( countryCode );
QString proposedLocale = QString( "%1_%2" ).arg( language ).arg( countryCode );
foreach ( QString line, linesForLanguage )
{
if ( line.contains( proposedLocale ) )
@ -108,7 +115,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
// language locale and pick the first result, if any.
if ( lang.isEmpty() )
{
for ( const QString &line : availableLocales )
for ( const QString& line : availableLocales )
{
if ( line.startsWith( languageLocale ) )
{
@ -116,13 +123,14 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
break;
}
}
}
// Else we have an unrecognized or unsupported locale, all we can do is go with
// en_US.UTF-8 UTF-8. This completes all default language setting guesswork.
if ( lang.isEmpty() )
{
lang = "en_US.UTF-8";
}
// The following block was inspired by Ubiquity, scripts/localechooser-apply.
@ -171,10 +179,9 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
// We make a proposed locale based on the UI language and the timezone's country. There is no
// guarantee that this will be a valid, supported locale (often it won't).
QString lc_formats;
QString combined = QString( "%1_%2" ).arg( language )
.arg( countryCode );
QString combined = QString( "%1_%2" ).arg( language ).arg( countryCode );
// We look up if it's a supported locale.
for ( const QString &line : availableLocales )
for ( const QString& line : availableLocales )
{
if ( line.startsWith( combined ) )
{
@ -187,7 +194,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
if ( lc_formats.isEmpty() )
{
QStringList available;
for ( const QString &line : availableLocales )
for ( const QString& line : availableLocales )
{
if ( line.contains( QString( "_%1" ).arg( countryCode ) ) )
{
@ -248,11 +255,10 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
};
if ( countryToDefaultLanguage.contains( countryCode ) )
{
QString combinedLocale =
QString( "%1_%2" ).arg( countryToDefaultLanguage.value( countryCode ) )
.arg( countryCode );
QString combinedLocale
= QString( "%1_%2" ).arg( countryToDefaultLanguage.value( countryCode ) ).arg( countryCode );
for ( const QString &line : availableLocales )
for ( const QString& line : availableLocales )
{
if ( line.startsWith( combinedLocale ) )
{
@ -267,7 +273,9 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
// If we cannot make a good choice for a given country we go with the LANG
// setting, which defaults to en_US.UTF-8 UTF-8 if all else fails.
if ( lc_formats.isEmpty() )
{
lc_formats = lang;
}
return LocaleConfiguration( lang, lc_formats );
}
@ -276,16 +284,9 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
bool
LocaleConfiguration::isEmpty() const
{
return m_lang.isEmpty() &&
lc_numeric.isEmpty() &&
lc_time.isEmpty() &&
lc_monetary.isEmpty() &&
lc_paper.isEmpty() &&
lc_name.isEmpty() &&
lc_address.isEmpty() &&
lc_telephone.isEmpty() &&
lc_measurement.isEmpty() &&
lc_identification.isEmpty();
return m_lang.isEmpty() && lc_numeric.isEmpty() && lc_time.isEmpty() && lc_monetary.isEmpty() && lc_paper.isEmpty()
&& lc_name.isEmpty() && lc_address.isEmpty() && lc_telephone.isEmpty() && lc_measurement.isEmpty()
&& lc_identification.isEmpty();
}
@ -295,35 +296,54 @@ LocaleConfiguration::toMap() const
QMap< QString, QString > map;
if ( !m_lang.isEmpty() )
{
map.insert( "LANG", m_lang );
}
if ( !lc_numeric.isEmpty() )
{
map.insert( "LC_NUMERIC", lc_numeric );
}
if ( !lc_time.isEmpty() )
{
map.insert( "LC_TIME", lc_time );
}
if ( !lc_monetary.isEmpty() )
{
map.insert( "LC_MONETARY", lc_monetary );
}
if ( !lc_paper.isEmpty() )
{
map.insert( "LC_PAPER", lc_paper );
}
if ( !lc_name.isEmpty() )
{
map.insert( "LC_NAME", lc_name );
}
if ( !lc_address.isEmpty() )
{
map.insert( "LC_ADDRESS", lc_address );
}
if ( !lc_telephone.isEmpty() )
{
map.insert( "LC_TELEPHONE", lc_telephone );
}
if ( !lc_measurement.isEmpty() )
{
map.insert( "LC_MEASUREMENT", lc_measurement );
}
if ( !lc_identification.isEmpty() )
{
map.insert( "LC_IDENTIFICATION", lc_identification );
}
return map;
}

@ -21,8 +21,8 @@
#define LOCALECONFIGURATION_H
#include <QDebug>
#include <QString>
#include <QMap>
#include <QString>
class LocaleConfiguration
{
@ -31,13 +31,14 @@ public:
explicit LocaleConfiguration();
/// @brief Create a locale with everything set to the given @p localeName
explicit LocaleConfiguration( const QString& localeName /* "en_US.UTF-8" */ )
: LocaleConfiguration( localeName, localeName ) { }
: LocaleConfiguration( localeName, localeName )
{
}
/// @brief Create a locale with language and formats separate
explicit LocaleConfiguration( const QString& localeName, const QString& formatsName );
static LocaleConfiguration fromLanguageAndLocation( const QString& language,
const QStringList& availableLocales,
const QString& countryCode );
static LocaleConfiguration
fromLanguageAndLocation( const QString& language, const QStringList& availableLocales, const QString& countryCode );
bool isEmpty() const;
@ -55,8 +56,8 @@ public:
// These become all uppercase in locale.conf, but we keep them lowercase here to
// avoid confusion with locale.h.
QString lc_numeric, lc_time, lc_monetary, lc_paper, lc_name, lc_address,
lc_telephone, lc_measurement, lc_identification;
QString lc_numeric, lc_time, lc_monetary, lc_paper, lc_name, lc_address, lc_telephone, lc_measurement,
lc_identification;
// If the user has explicitly selected language (from the dialog)
// or numbers format, set these to avoid implicit changes to them.
@ -67,9 +68,10 @@ private:
QString m_languageLocaleBcp47;
};
inline QDebug& operator <<( QDebug& s, const LocaleConfiguration& l )
inline QDebug&
operator<<( QDebug& s, const LocaleConfiguration& l )
{
return s << l.language() << '(' << l.toBcp47() << ") +" << l.lc_numeric;
}
#endif // LOCALECONFIGURATION_H
#endif // LOCALECONFIGURATION_H

@ -38,7 +38,7 @@
#include <QtConcurrent/QtConcurrentRun>
CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin<LocaleViewStep>(); )
CALAMARES_PLUGIN_FACTORY_DEFINITION( LocaleViewStepFactory, registerPlugin< LocaleViewStep >(); )
LocaleViewStep::LocaleViewStep( QObject* parent )
: Calamares::ViewStep( parent )
@ -53,19 +53,17 @@ LocaleViewStep::LocaleViewStep( QObject* parent )
m_waitingWidget = new WaitingWidget( tr( "Loading location data..." ) );
mainLayout->addWidget( m_waitingWidget );
connect( &m_initWatcher, &QFutureWatcher< void >::finished,
this, [=]
{
bool hasInternet = Calamares::JobQueue::instance()->globalStorage()
->value( "hasInternet" ).toBool();
connect( &m_initWatcher, &QFutureWatcher< void >::finished, this, [=] {
bool hasInternet = Calamares::JobQueue::instance()->globalStorage()->value( "hasInternet" ).toBool();
if ( m_geoipUrl.isEmpty() || !hasInternet )
setUpPage();
else
{
fetchGeoIpTimezone();
});
}
} );
QFuture< void > initFuture = QtConcurrent::run( [=]
{
QFuture< void > initFuture = QtConcurrent::run( [=] {
LocaleGlobal::init();
if ( m_geoipUrl.isEmpty() )
return;
@ -90,16 +88,16 @@ LocaleViewStep::LocaleViewStep( QObject* parent )
LocaleViewStep::~LocaleViewStep()
{
if ( m_widget && m_widget->parent() == nullptr )
{
m_widget->deleteLater();
}
}
void
LocaleViewStep::setUpPage()
{
m_actualWidget->init( m_startingTimezone.first,
m_startingTimezone.second,
m_localeGenPath );
m_actualWidget->init( m_startingTimezone.first, m_startingTimezone.second, m_localeGenPath );
m_widget->layout()->removeWidget( m_waitingWidget );
m_waitingWidget->deleteLater();
m_widget->layout()->addWidget( m_actualWidget );
@ -116,10 +114,14 @@ LocaleViewStep::fetchGeoIpTimezone()
{
m_startingTimezone = h.get();
if ( !m_startingTimezone.isValid() )
{
cWarning() << "GeoIP lookup at" << m_geoipUrl << "failed.";
}
}
else
{
cWarning() << "GeoIP Style" << m_geoipStyle << "is not recognized.";
}
setUpPage();
}
@ -198,10 +200,11 @@ LocaleViewStep::onLeave()
auto map = m_actualWidget->localesMap();
QVariantMap vm;
for ( auto it = map.constBegin(); it != map.constEnd(); ++it )
{
vm.insert( it.key(), it.value() );
}
Calamares::JobQueue::instance()->globalStorage()
->insert( "localeConf", vm );
Calamares::JobQueue::instance()->globalStorage()->insert( "localeConf", vm );
}
@ -216,12 +219,15 @@ LocaleViewStep::setConfigurationMap( const QVariantMap& configurationMap )
}
else
{
m_startingTimezone = CalamaresUtils::GeoIP::RegionZonePair( QStringLiteral( "America" ), QStringLiteral( "New_York" ) );
m_startingTimezone
= CalamaresUtils::GeoIP::RegionZonePair( QStringLiteral( "America" ), QStringLiteral( "New_York" ) );
}
m_localeGenPath = CalamaresUtils::getString( configurationMap, "localeGenPath" );
if ( m_localeGenPath.isEmpty() )
{
m_localeGenPath = QStringLiteral( "/etc/locale.gen" );
}
bool ok = false;
QVariantMap geoip = CalamaresUtils::getSubMap( configurationMap, "geoip", ok );

@ -83,4 +83,4 @@ private:
CALAMARES_PLUGIN_FACTORY_DECLARATION( LocaleViewStepFactory )
#endif // LOCALEVIEWSTEP_H
#endif // LOCALEVIEWSTEP_H

@ -19,11 +19,11 @@
#include <SetTimezoneJob.h>
#include "JobQueue.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "Settings.h"
#include "utils/Logger.h"
#include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h"
#include <QDir>
#include <QFileInfo>
@ -51,13 +51,13 @@ SetTimezoneJob::exec()
// to a running timedated over D-Bus), and we have code that works
if ( !Calamares::Settings::instance()->doChroot() )
{
int ec = CalamaresUtils::System::instance()->
targetEnvCall( { "timedatectl",
"set-timezone",
m_region + '/' + m_zone } );
int ec = CalamaresUtils::System::instance()->targetEnvCall(
{ "timedatectl", "set-timezone", m_region + '/' + m_zone } );
if ( !ec )
{
return Calamares::JobResult::ok();
}
}
QString localtimeSlink( "/etc/localtime" );
@ -72,31 +72,21 @@ SetTimezoneJob::exec()
tr( "Bad path: %1" ).arg( zoneFile.absolutePath() ) );
// Make sure /etc/localtime doesn't exist, otherwise symlinking will fail
CalamaresUtils::System::instance()->
targetEnvCall( { "rm",
"-f",
localtimeSlink } );
int ec = CalamaresUtils::System::instance()->
targetEnvCall( { "ln",
"-s",
zoneinfoPath,
localtimeSlink } );
CalamaresUtils::System::instance()->targetEnvCall( { "rm", "-f", localtimeSlink } );
int ec = CalamaresUtils::System::instance()->targetEnvCall( { "ln", "-s", zoneinfoPath, localtimeSlink } );
if ( ec )
return Calamares::JobResult::error( tr( "Cannot set timezone." ),
tr( "Link creation failed, target: %1; link name: %2" )
.arg( zoneinfoPath )
.arg( "/etc/localtime" ) );
return Calamares::JobResult::error(
tr( "Cannot set timezone." ),
tr( "Link creation failed, target: %1; link name: %2" ).arg( zoneinfoPath ).arg( "/etc/localtime" ) );
QFile timezoneFile( gs->value( "rootMountPoint" ).toString() + "/etc/timezone" );
if ( !timezoneFile.open( QIODevice::WriteOnly |
QIODevice::Text |
QIODevice::Truncate ) )
return Calamares::JobResult::error( tr( "Cannot set timezone,"),
tr( "Cannot open /etc/timezone for writing"));
if ( !timezoneFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
return Calamares::JobResult::error( tr( "Cannot set timezone," ),
tr( "Cannot open /etc/timezone for writing" ) );
QTextStream out(&timezoneFile);
QTextStream out( &timezoneFile );
out << m_region << '/' << m_zone << "\n";
timezoneFile.close();

@ -26,8 +26,7 @@ class SetTimezoneJob : public Calamares::Job
{
Q_OBJECT
public:
SetTimezoneJob( const QString& region,
const QString& zone );
SetTimezoneJob( const QString& region, const QString& zone );
QString prettyName() const override;
Calamares::JobResult exec() override;

@ -25,19 +25,17 @@
QTEST_GUILESS_MAIN( LocaleTests )
LocaleTests::LocaleTests()
{
}
LocaleTests::LocaleTests() {}
LocaleTests::~LocaleTests()
{
}
LocaleTests::~LocaleTests() {}
void LocaleTests::initTestCase()
void
LocaleTests::initTestCase()
{
}
void LocaleTests::testEmptyLocaleConfiguration()
void
LocaleTests::testEmptyLocaleConfiguration()
{
LocaleConfiguration lc;
@ -45,7 +43,8 @@ void LocaleTests::testEmptyLocaleConfiguration()
QCOMPARE( lc.toBcp47(), QString() );
}
void LocaleTests::testDefaultLocaleConfiguration()
void
LocaleTests::testDefaultLocaleConfiguration()
{
LocaleConfiguration lc( "en_US.UTF-8" );
QVERIFY( !lc.isEmpty() );
@ -58,7 +57,8 @@ void LocaleTests::testDefaultLocaleConfiguration()
QCOMPARE( lc2.toBcp47(), "de" );
}
void LocaleTests::testSplitLocaleConfiguration()
void
LocaleTests::testSplitLocaleConfiguration()
{
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
QVERIFY( !lc.isEmpty() );
@ -76,5 +76,4 @@ void LocaleTests::testSplitLocaleConfiguration()
QVERIFY( !lc3.isEmpty() );
QCOMPARE( lc3.toBcp47(), "da" );
QCOMPARE( lc3.lc_numeric, "de_DE.UTF-8" );
}

Loading…
Cancel
Save