KTextAddons

bergamotengineclient.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "bergamotengineclient.h"
8#include "begamotenginedialog.h"
9#include "bergamotengineplugin.h"
10#include "translator/misc/translatorutil.h"
11#include <KConfigGroup>
12#include <KLocalizedString>
13#include <KSharedConfig>
14#include <QPointer>
15
16BergamotEngineClient::BergamotEngineClient(QObject *parent)
17 : TextTranslator::TranslatorEngineClient{parent}
18{
19 updateInstalledLanguage();
20}
21
22BergamotEngineClient::~BergamotEngineClient() = default;
23
24QString BergamotEngineClient::name() const
25{
26 return QStringLiteral("bergamot");
27}
28
29QString BergamotEngineClient::translatedName() const
30{
31 return i18n("Bergamot");
32}
33
34TextTranslator::TranslatorEnginePlugin *BergamotEngineClient::createTranslator()
35{
36 auto enginePlugin = new BergamotEnginePlugin();
37 connect(this, &BergamotEngineClient::configureChanged, enginePlugin, &BergamotEnginePlugin::slotConfigureChanged);
38 return enginePlugin;
39}
40
41QMap<TextTranslator::TranslatorUtil::Language, QString> BergamotEngineClient::supportedFromLanguages()
42{
43 return mFromLanguages;
44}
45
46QMap<TextTranslator::TranslatorUtil::Language, QString> BergamotEngineClient::supportedToLanguages()
47{
48 return mToLanguages;
49}
50
51bool BergamotEngineClient::hasConfigurationDialog() const
52{
53 return true;
54}
55
56bool BergamotEngineClient::showConfigureDialog(QWidget *parentWidget)
57{
58 bool settingsChanged = false;
59 KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
60 QPointer<BegamotEngineDialog> dlg = new BegamotEngineDialog(parentWidget);
61 BergamotEngineUtils::SettingsInfo info;
62 info.loadSettingsInfo();
63 dlg->setSettingsInfo(info);
64 if (dlg->exec()) {
65 info = dlg->settingsInfo();
66 info.saveSettingsInfo();
67 updateInstalledLanguage();
68 Q_EMIT configureChanged();
69 settingsChanged = true;
70 }
71 delete dlg;
72 return settingsChanged;
73}
74
75void BergamotEngineClient::updateInstalledLanguage()
76{
77 mLanguageInstalled = BergamotEngineUtils::languageLocallyStored();
78 updateFromLanguageList();
79 updateToLanguageList();
80}
81
82void BergamotEngineClient::updateFromLanguageList()
83{
84 mFromLanguages.clear();
85 for (const auto &lang : std::as_const(mLanguageInstalled)) {
86 const TextTranslator::TranslatorUtil::Language langUtil = TextTranslator::TranslatorUtil::stringToLanguage(lang.from);
87 mFromLanguages.insert(langUtil, TextTranslator::TranslatorUtil::translatedLanguage(langUtil));
88 // qDebug() << " SSSSSSSSSSSSSSSSSS " << langUtil << " mFromLanguages " << mFromLanguages;
89 }
90}
91
92void BergamotEngineClient::updateToLanguageList()
93{
94 mToLanguages.clear();
95 for (const auto &lang : std::as_const(mLanguageInstalled)) {
96 // qDebug() << " lang.to " << lang.to << " mLanguageCode " << mLanguageCode;
97 if (lang.to == mLanguageCode || mLanguageCode.isEmpty()) {
98 const TextTranslator::TranslatorUtil::Language langUtil = TextTranslator::TranslatorUtil::stringToLanguage(lang.to);
99 // qDebug() << " lang.to " << lang.to;
100 mToLanguages.insert(langUtil, TextTranslator::TranslatorUtil::translatedLanguage(langUtil));
101 }
102 }
103}
104
105bool BergamotEngineClient::hasInvertSupport() const
106{
107 return false;
108}
109
110void BergamotEngineClient::generateToListFromCurrentToLanguage(const QString &languageCode)
111{
112 mLanguageCode = languageCode;
113 updateToLanguageList();
114}
115
116TextTranslator::TranslatorEngineClient::EngineType BergamotEngineClient::engineType() const
117{
118 return TextTranslator::TranslatorEngineClient::Locale;
119}
120
121bool BergamotEngineClient::isSupported(TextTranslator::TranslatorUtil::Language lang) const
122{
123 Q_UNUSED(lang);
124 return true;
125}
126
127void BergamotEngineClient::updateListLanguages()
128{
129 updateInstalledLanguage();
130}
131
132#include "moc_bergamotengineclient.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
void clear()
iterator insert(const Key &key, const T &value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.