Changeset View
Changeset View
Standalone View
Standalone View
kcms/translations/translations.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2014 John Layt <john@layt.net> | ||||
3 | * Copyright (C) 2018 Eike Hein <hein@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "translations.h" | ||||
22 | #include "translationsmodel.h" | ||||
23 | | ||||
24 | #include "../formats/writeexports.h" | ||||
25 | | ||||
26 | #include <KAboutData> | ||||
27 | #include <KLocalizedString> | ||||
28 | #include <KPluginFactory> | ||||
29 | #include <KSharedConfig> | ||||
30 | | ||||
31 | K_PLUGIN_FACTORY_WITH_JSON(TranslationsFactory, "kcm_translations.json", registerPlugin<Translations>();) | ||||
32 | | ||||
33 | Translations::Translations(QObject *parent, const QVariantList &args) | ||||
34 | : KQuickAddons::ConfigModule(parent, args) | ||||
35 | , m_translationsModel(new TranslationsModel(this)) | ||||
36 | , m_everSaved(false) | ||||
37 | { | ||||
38 | KAboutData *about = new KAboutData("kcm_translations", | ||||
39 | i18n("Configure Plasma translations"), | ||||
40 | "2.0", QString(), KAboutLicense::LGPL); | ||||
41 | setAboutData(about); | ||||
42 | | ||||
43 | setButtons(Apply | Default); | ||||
44 | | ||||
45 | m_config = KConfigGroup(KSharedConfig::openConfig(configFile), "Translations"); | ||||
46 | | ||||
47 | connect(m_translationsModel, &TranslationsModel::selectedLanguagesChanged, | ||||
48 | this, &Translations::selectedLanguagesChanged); | ||||
49 | connect(m_translationsModel, &TranslationsModel::missingLanguagesChanged, | ||||
50 | this, &Translations::missingLanguagesChanged); | ||||
51 | } | ||||
52 | | ||||
53 | Translations::~Translations() | ||||
54 | { | ||||
55 | } | ||||
56 | | ||||
57 | QAbstractItemModel* Translations::translationsModel() const | ||||
58 | { | ||||
59 | return m_translationsModel; | ||||
60 | } | ||||
61 | | ||||
62 | bool Translations::everSaved() const | ||||
63 | { | ||||
64 | return m_everSaved; | ||||
65 | } | ||||
66 | | ||||
67 | void Translations::load() | ||||
68 | { | ||||
69 | m_configuredLanguages = m_config.readEntry(lcLanguage, | ||||
70 | QString()).split(':', QString::SkipEmptyParts); | ||||
71 | | ||||
72 | m_translationsModel->setSelectedLanguages(m_configuredLanguages); | ||||
73 | } | ||||
74 | | ||||
75 | void Translations::save() | ||||
76 | { | ||||
77 | m_everSaved = true; | ||||
78 | emit everSavedChanged(); | ||||
79 | | ||||
80 | m_configuredLanguages = m_translationsModel->selectedLanguages(); | ||||
81 | qDebug() << m_configuredLanguages; | ||||
82 | | ||||
83 | for (const QString& lang : m_translationsModel->missingLanguages()) { | ||||
84 | m_configuredLanguages.removeOne(lang); | ||||
85 | } | ||||
86 | | ||||
87 | m_config.writeEntry(lcLanguage, m_configuredLanguages.join(QStringLiteral(":")), | ||||
88 | KConfig::Persistent | KConfig::Global); | ||||
89 | m_config.sync(); | ||||
90 | | ||||
91 | m_translationsModel->setSelectedLanguages(m_configuredLanguages); | ||||
92 | } | ||||
93 | | ||||
94 | void Translations::defaults() | ||||
95 | { | ||||
96 | KConfigGroup formatsConfig = KConfigGroup(KSharedConfig::openConfig(configFile), "Formats"); | ||||
97 | | ||||
98 | QString lang = formatsConfig.readEntry("LANG", QString()); | ||||
99 | | ||||
100 | if (lang.isEmpty() | ||||
101 | || !KLocalizedString::availableDomainTranslations("systemsettings").contains(lang)) { | ||||
102 | lang = QLocale::system().name(); | ||||
103 | } | ||||
104 | | ||||
105 | if (!KLocalizedString::availableDomainTranslations("systemsettings").contains(lang)) { | ||||
106 | lang = QStringLiteral("en_US"); | ||||
107 | } | ||||
108 | | ||||
109 | QStringList languages; | ||||
110 | languages << lang; | ||||
111 | | ||||
112 | m_translationsModel->setSelectedLanguages(languages); | ||||
113 | } | ||||
114 | | ||||
115 | void Translations::selectedLanguagesChanged() | ||||
116 | { | ||||
117 | setNeedsSave(m_configuredLanguages != m_translationsModel->selectedLanguages()); | ||||
118 | } | ||||
119 | | ||||
120 | void Translations::missingLanguagesChanged() | ||||
121 | { | ||||
122 | if (m_translationsModel->missingLanguages().count()) { | ||||
123 | setNeedsSave(true); | ||||
124 | } | ||||
125 | } | ||||
126 | | ||||
127 | #include "translations.moc" |