Commit 34216709 authored by Kevin Ottens's avatar Kevin Ottens
Browse files

Port the translations module to ManagedConfigModule

Reviewers: #plasma, mart, davidedmundson, crossi, bport

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D26242
parent 56529e7b
......@@ -6,7 +6,8 @@ add_definitions(-DTRANSLATION_DOMAIN=\"kcm_translations\")
########### 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})
......
......@@ -29,7 +29,6 @@ ScrollViewKCM {
id: root
ConfigModule.quickHelp: i18n("Language")
ConfigModule.buttons: ConfigModule.Help | ConfigModule.Defaults | ConfigModule.Apply
Component {
id: addLanguageItemComponent
......
/*
* Copyright (C) 2014 John Layt <john@layt.net>
* Copyright (C) 2018 Eike Hein <hein@kde.org>
* Copyright (C) 2019 Kevin Ottens <kevin.ottens@enioka.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -20,6 +21,7 @@
#include "translations.h"
#include "translationsmodel.h"
#include "translationssettings.h"
#include <KAboutData>
#include <KLocalizedString>
......@@ -28,11 +30,9 @@
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 @@ Translations::Translations(QObject *parent, const QVariantList &args)
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 @@ bool Translations::everSaved() const
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"
/*
* Copyright (C) 2014 John Layt <john@layt.net>
* Copyright (C) 2018 Eike Hein <hein@kde.org>
* Copyright (C) 2019 Kevin Ottens <kevin.ottens@enioka.com>
*
* 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 <KQuickAddons/ConfigModule>
#include <KConfigGroup>
#include <KQuickAddons/ManagedConfigModule>
class AvailableTranslationsModel;
class SelectedTranslationsModel;
class TranslationsModel;
class TranslationsSettings;
class Translations : public KQuickAddons::ConfigModule
class Translations : public KQuickAddons::ManagedConfigModule
{
Q_OBJECT
......@@ -59,16 +58,15 @@ class Translations : public KQuickAddons::ConfigModule
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;
};
......
/*
* Copyright (C) 2019 Kevin Ottens <kevin.ottens@enioka.com>
*
* 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(':')));
}
/*
* Copyright (C) 2019 Kevin Ottens <kevin.ottens@enioka.com>
*
* 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
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile name="plasma-localerc" />
<include>KLocalizedString</include>
<group name="Translations">
<entry name="languageString" key="LANGUAGE" type="String">
<code>
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");
}
</code>
<default code="true">lang</default>
<label>Configured languages</label>
</entry>
</group>
</kcfg>
File=translationssettingsbase.kcfg
ClassName=TranslationsSettingsBase
Mutators=true
DefaultValueGetters=true
GenerateProperties=true
ParentInConstructor=true
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment