diff --git a/kcms/translations/CMakeLists.txt b/kcms/translations/CMakeLists.txt --- a/kcms/translations/CMakeLists.txt +++ b/kcms/translations/CMakeLists.txt @@ -6,7 +6,8 @@ ########### next target ############### -set(kcm_translations_PART_SRCS translations.cpp translationsmodel.cpp) +set(kcm_translations_PART_SRCS translations.cpp translationsmodel.cpp translationssettings.cpp) +kconfig_add_kcfg_files(kcm_translations_PART_SRCS translationssettingsbase.kcfgc GENERATE_MOC) add_library(kcm_translations MODULE ${kcm_translations_PART_SRCS}) diff --git a/kcms/translations/package/contents/ui/main.qml b/kcms/translations/package/contents/ui/main.qml --- a/kcms/translations/package/contents/ui/main.qml +++ b/kcms/translations/package/contents/ui/main.qml @@ -29,7 +29,6 @@ id: root ConfigModule.quickHelp: i18n("Language") - ConfigModule.buttons: ConfigModule.Help | ConfigModule.Defaults | ConfigModule.Apply Component { id: addLanguageItemComponent diff --git a/kcms/translations/translations.h b/kcms/translations/translations.h --- a/kcms/translations/translations.h +++ b/kcms/translations/translations.h @@ -1,6 +1,7 @@ /* * Copyright (C) 2014 John Layt * Copyright (C) 2018 Eike Hein + * Copyright (C) 2019 Kevin Ottens * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -21,16 +22,14 @@ #ifndef TRANSLATIONS_H #define TRANSLATIONS_H -#include - -#include +#include class AvailableTranslationsModel; class SelectedTranslationsModel; class TranslationsModel; +class TranslationsSettings; - -class Translations : public KQuickAddons::ConfigModule +class Translations : public KQuickAddons::ManagedConfigModule { Q_OBJECT @@ -59,16 +58,15 @@ private Q_SLOTS: void selectedLanguagesChanged(); - void missingLanguagesChanged(); private: + bool isSaveNeeded() const override; + + TranslationsSettings *m_settings; TranslationsModel *m_translationsModel; SelectedTranslationsModel *m_selectedTranslationsModel; AvailableTranslationsModel *m_availableTranslationsModel; - KConfigGroup m_config; - QStringList m_configuredLanguages; - bool m_everSaved; }; diff --git a/kcms/translations/translations.cpp b/kcms/translations/translations.cpp --- a/kcms/translations/translations.cpp +++ b/kcms/translations/translations.cpp @@ -1,6 +1,7 @@ /* * Copyright (C) 2014 John Layt * Copyright (C) 2018 Eike Hein + * Copyright (C) 2019 Kevin Ottens * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -20,19 +21,18 @@ #include "translations.h" #include "translationsmodel.h" +#include "translationssettings.h" #include #include #include #include K_PLUGIN_CLASS_WITH_JSON(Translations, "kcm_translations.json") -static const QString configFile = QStringLiteral("plasma-localerc"); -static const QString lcLanguage = QStringLiteral("LANGUAGE"); - Translations::Translations(QObject *parent, const QVariantList &args) - : KQuickAddons::ConfigModule(parent, args) + : KQuickAddons::ManagedConfigModule(parent, args) + , m_settings(new TranslationsSettings(this)) , m_translationsModel(new TranslationsModel(this)) , m_selectedTranslationsModel(new SelectedTranslationsModel(this)) , m_availableTranslationsModel(new AvailableTranslationsModel(this)) @@ -45,15 +45,10 @@ setButtons(Apply | Default); - m_config = KConfigGroup(KSharedConfig::openConfig(configFile), "Translations"); - connect(m_selectedTranslationsModel, &SelectedTranslationsModel::selectedLanguagesChanged, - this, &Translations::selectedLanguagesChanged); - connect(m_selectedTranslationsModel, &SelectedTranslationsModel::missingLanguagesChanged, - this, &Translations::missingLanguagesChanged); - + this, &Translations::selectedLanguagesChanged); connect(m_selectedTranslationsModel, &SelectedTranslationsModel::selectedLanguagesChanged, - m_availableTranslationsModel, &AvailableTranslationsModel::setSelectedLanguages); + m_availableTranslationsModel, &AvailableTranslationsModel::setSelectedLanguages); } Translations::~Translations() @@ -82,62 +77,41 @@ void Translations::load() { - m_configuredLanguages = m_config.readEntry(lcLanguage, - QString()).split(QLatin1Char(':'), QString::SkipEmptyParts); - - m_availableTranslationsModel->setSelectedLanguages(m_configuredLanguages); - m_selectedTranslationsModel->setSelectedLanguages(m_configuredLanguages); + KQuickAddons::ManagedConfigModule::load(); + m_availableTranslationsModel->setSelectedLanguages(m_settings->configuredLanguages()); + m_selectedTranslationsModel->setSelectedLanguages(m_settings->configuredLanguages()); } void Translations::save() { m_everSaved = true; emit everSavedChanged(); - - m_configuredLanguages = m_selectedTranslationsModel->selectedLanguages(); - - const auto missingLanguages = m_selectedTranslationsModel->missingLanguages(); - for (const QString& lang : missingLanguages) { - m_configuredLanguages.removeOne(lang); - } - - m_config.writeEntry(lcLanguage, m_configuredLanguages.join(QLatin1Char(':')), KConfig::Persistent); - m_config.sync(); - - m_selectedTranslationsModel->setSelectedLanguages(m_configuredLanguages); + KQuickAddons::ManagedConfigModule::save(); } void Translations::defaults() { - KConfigGroup formatsConfig = KConfigGroup(KSharedConfig::openConfig(configFile), "Formats"); - - QString lang = formatsConfig.readEntry("LANG", QString()); - - if (lang.isEmpty() - || !KLocalizedString::availableDomainTranslations("plasmashell").contains(lang)) { - lang = QLocale::system().name(); - } - - if (!KLocalizedString::availableDomainTranslations("plasmashell").contains(lang)) { - lang = QStringLiteral("en_US"); - } - - QStringList languages; - languages << lang; - - m_selectedTranslationsModel->setSelectedLanguages(languages); + KQuickAddons::ManagedConfigModule::defaults(); + m_availableTranslationsModel->setSelectedLanguages(m_settings->configuredLanguages()); + m_selectedTranslationsModel->setSelectedLanguages(m_settings->configuredLanguages()); } void Translations::selectedLanguagesChanged() { - setNeedsSave(m_configuredLanguages != m_selectedTranslationsModel->selectedLanguages()); + auto configuredLanguages = m_selectedTranslationsModel->selectedLanguages(); + + const auto missingLanguages = m_selectedTranslationsModel->missingLanguages(); + for (const auto &lang : missingLanguages) { + configuredLanguages.removeOne(lang); + } + + m_settings->setConfiguredLanguages(configuredLanguages); + m_selectedTranslationsModel->setSelectedLanguages(configuredLanguages); } -void Translations::missingLanguagesChanged() +bool Translations::isSaveNeeded() const { - if (!m_selectedTranslationsModel->missingLanguages().isEmpty()) { - setNeedsSave(true); - } + return !m_selectedTranslationsModel->missingLanguages().isEmpty(); } #include "translations.moc" diff --git a/kcms/translations/translationssettings.h b/kcms/translations/translationssettings.h new file mode 100644 --- /dev/null +++ b/kcms/translations/translationssettings.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2019 Kevin Ottens + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef TRANSLATIONSSETTINGS_H +#define TRANSLATIONSSETTINGS_H + +#include "translationssettingsbase.h" + +class TranslationsSettings : public TranslationsSettingsBase +{ + Q_OBJECT + Q_PROPERTY(QStringList configuredLanguages READ configuredLanguages WRITE setConfiguredLanguages NOTIFY configuredLanguagesChanged) +public: + TranslationsSettings(QObject *parent = nullptr); + ~TranslationsSettings() override; + + QStringList configuredLanguages() const; + void setConfiguredLanguages(const QStringList &langs); + +signals: + void configuredLanguagesChanged(); +}; + +#endif // TRANSLATIONSSETTINGS_H diff --git a/kcms/translations/translationssettings.cpp b/kcms/translations/translationssettings.cpp new file mode 100644 --- /dev/null +++ b/kcms/translations/translationssettings.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2019 Kevin Ottens + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "translationssettings.h" + +TranslationsSettings::TranslationsSettings(QObject *parent) + : TranslationsSettingsBase(parent) +{ + connect(this, &TranslationsSettingsBase::languageStringChanged, + this, &TranslationsSettings::configuredLanguagesChanged); +} + +TranslationsSettings::~TranslationsSettings() +{ +} + +QStringList TranslationsSettings::configuredLanguages() const +{ + return languageString().split(QLatin1Char(':'), QString::SkipEmptyParts); +} + +void TranslationsSettings::setConfiguredLanguages(const QStringList &langs) +{ + setLanguageString(langs.join(QLatin1Char(':'))); +} diff --git a/kcms/translations/translationssettingsbase.kcfg b/kcms/translations/translationssettingsbase.kcfg new file mode 100644 --- /dev/null +++ b/kcms/translations/translationssettingsbase.kcfg @@ -0,0 +1,28 @@ + + + + KLocalizedString + + + + KConfigGroup formatsConfig = KConfigGroup(KSharedConfig::openConfig("plasma-localerc"), "Formats"); + + QString lang = formatsConfig.readEntry("LANG", QString()); + + if (lang.isEmpty() + || !KLocalizedString::availableDomainTranslations("plasmashell").contains(lang)) { + lang = QLocale::system().name(); + } + + if (!KLocalizedString::availableDomainTranslations("plasmashell").contains(lang)) { + lang = QStringLiteral("en_US"); + } + + lang + + + + diff --git a/kcms/translations/translationssettingsbase.kcfgc b/kcms/translations/translationssettingsbase.kcfgc new file mode 100644 --- /dev/null +++ b/kcms/translations/translationssettingsbase.kcfgc @@ -0,0 +1,6 @@ +File=translationssettingsbase.kcfg +ClassName=TranslationsSettingsBase +Mutators=true +DefaultValueGetters=true +GenerateProperties=true +ParentInConstructor=true