diff options
author | Fabian Müller <fmueller@owncloud.com> | 2021-03-16 18:28:11 +0300 |
---|---|---|
committer | Fabian Müller <80399010+fmoc@users.noreply.github.com> | 2021-03-31 12:43:26 +0300 |
commit | 7cbb9f20b509d5de0757e6c2dd8b639d322b0a22 (patch) | |
tree | 230e4a6b5c88da0ec15d59fc76af7ed85955fe13 /src/gui/generalsettings.cpp | |
parent | d840a087d1c1b71c7a09acd9d78bcc20fab6a870 (diff) |
Add language dropdown to general settings page
Allows users to specify their preferred language, similar to --language. If not set, the existing auto-detection will be used.
Diffstat (limited to 'src/gui/generalsettings.cpp')
-rw-r--r-- | src/gui/generalsettings.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/gui/generalsettings.cpp b/src/gui/generalsettings.cpp index 8ab7e68ff..59124c714 100644 --- a/src/gui/generalsettings.cpp +++ b/src/gui/generalsettings.cpp @@ -32,6 +32,7 @@ #include "ignorelisteditor.h" #include "config.h" +#include "translations.h" #include <QNetworkProxy> #include <QDir> @@ -52,6 +53,9 @@ GeneralSettings::GeneralSettings(QWidget *parent) this, &GeneralSettings::slotToggleOptionalDesktopNotifications); connect(_ui->showInExplorerNavigationPaneCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::slotShowInExplorerNavigationPane); + // needs to be called before settings are loaded + loadLanguageNamesIntoDropdown(); + reloadConfig(); loadMiscSettings(); slotUpdateInfo(); @@ -63,6 +67,10 @@ GeneralSettings::GeneralSettings(QWidget *parent) connect(_ui->newFolderLimitSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &GeneralSettings::saveMiscSettings); connect(_ui->newExternalStorage, &QAbstractButton::toggled, this, &GeneralSettings::saveMiscSettings); + connect(_ui->languageDropdown, QOverload<int>::of(&QComboBox::activated), this, [this](int) { + this->saveMiscSettings(); + }); + /* handle the hidden file checkbox */ /* the ignoreHiddenFiles flag is a folder specific setting, but for now, it is @@ -138,6 +146,27 @@ void GeneralSettings::loadMiscSettings() _ui->newFolderLimitSpinBox->setValue(newFolderLimit.second); _ui->newExternalStorage->setChecked(cfgFile.confirmExternalStorage()); _ui->monoIconsCheckBox->setChecked(cfgFile.monoIcons()); + + // we assume the languages have been loaded into the dropdown already (by making sure that the corresponding method is called before this one) + const auto &language = cfgFile.uiLanguage(); + + // index 0 means "use default", which we use unless the loop below sets another entry + _ui->languageDropdown->setCurrentIndex(0); + + if (!language.isEmpty()) { + // a simple linear search to find the right entry and choose it is sufficient for this application + // we can skip the "use default" entry by starting at index 1 + // note that if the loop below never breaks, the setting falls back to "use default" + // this is desired behavior, as it handles cases when the selected language no longer exists + for (int i = 1; i < _ui->languageDropdown->count(); ++i) { + const auto text = _ui->languageDropdown->itemText(i); + + if (text == language) { + _ui->languageDropdown->setCurrentIndex(i); + break; + } + } + } } void GeneralSettings::showEvent(QShowEvent *) @@ -235,6 +264,19 @@ void GeneralSettings::saveMiscSettings() cfgFile.setNewBigFolderSizeLimit(_ui->newFolderLimitCheckBox->isChecked(), _ui->newFolderLimitSpinBox->value()); cfgFile.setConfirmExternalStorage(_ui->newExternalStorage->isChecked()); + + const auto pickedLanguageIndex = _ui->languageDropdown->currentIndex(); + + // the first entry, identified by index 0, means "use default", which is a special case handled below + if (pickedLanguageIndex > 0) { + // for now, we use the locale names as labels in the dropdown + // therefore, we can store them directly in the config file + // in future versions, we will likely display nice names instead of locales to improve the user experience + cfgFile.setUiLanguage(_ui->languageDropdown->itemText(pickedLanguageIndex)); + } else { + // empty string means "use system default" + cfgFile.setUiLanguage(""); + } } void GeneralSettings::slotToggleLaunchOnStartup(bool enable) @@ -286,4 +328,19 @@ void GeneralSettings::reloadConfig() } } +void GeneralSettings::loadLanguageNamesIntoDropdown() +{ + _ui->languageDropdown->clear(); + + // if no option has been chosen explicitly by the user, the first entry shall be used + _ui->languageDropdown->addItem(tr("(use default)")); + + QStringList availableTranslations(Translations::listAvailableTranslations().toList()); + availableTranslations.sort(Qt::CaseInsensitive); + + for (const auto &i : availableTranslations) { + _ui->languageDropdown->addItem(i); + } +} + } // namespace OCC |