KTextAddons

languagetoolmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "languagetoolmanager.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include <KConfigGroup>
11#include <KSharedConfig>
12#include <QColor>
13#include <QNetworkAccessManager>
14#include <QRandomGenerator>
15namespace
16{
17static const char myLanguageToolManagerGroupName[] = "LanguageTool";
18}
19using namespace TextGrammarCheck;
20LanguageToolManager::LanguageToolManager(QObject *parent)
21 : QObject(parent)
22 , mNetworkAccessManager(new QNetworkAccessManager(this))
23{
24 mNetworkAccessManager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
25 mNetworkAccessManager->setStrictTransportSecurityEnabled(true);
26 mNetworkAccessManager->enableStrictTransportSecurityStore(true);
27 loadSettings();
28}
29
30LanguageToolManager::~LanguageToolManager() = default;
31
32LanguageToolManager *LanguageToolManager::self()
33{
34 static LanguageToolManager s_self;
35 return &s_self;
36}
37
38QNetworkAccessManager *LanguageToolManager::networkAccessManager() const
39{
40 return mNetworkAccessManager;
41}
42
43QString LanguageToolManager::languageToolCheckPath() const
44{
45 if (mLanguageToolPath.isEmpty()) {
46 return {};
47 }
48 return mLanguageToolPath + QStringLiteral("/check");
49}
50
51QString LanguageToolManager::languageToolLanguagesPath() const
52{
53 if (mLanguageToolPath.isEmpty()) {
54 return {};
55 }
56 return convertToLanguagePath(mLanguageToolPath);
57}
58
59QString LanguageToolManager::convertToLanguagePath(const QString &path)
60{
61 return path + QStringLiteral("/languages");
62}
63
64QColor LanguageToolManager::grammarColorForError(const QString &error)
65{
66 QColor col = mGrammarColor.value(error);
67 if (col.isValid()) {
68 return col;
69 }
70 col.setRgb(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256));
71 // Generate color.
72 mGrammarColor.insert(error, col);
73 return col;
74}
75
76QString LanguageToolManager::languageToolPath() const
77{
78 return mLanguageToolPath;
79}
80
81void LanguageToolManager::setLanguageToolPath(const QString &path)
82{
83 mLanguageToolPath = path;
84}
85
86void LanguageToolManager::loadSettings()
87{
88 KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myLanguageToolManagerGroupName));
89 mLanguageToolPath = grp.readEntry(QStringLiteral("languagetoolpath"), QStringLiteral("https://api.languagetoolplus.com/v2"));
90 if (mLanguageToolPath == "https://languagetool.org/api/v2"_L1) {
91 mLanguageToolPath = QStringLiteral("https://api.languagetoolplus.com/v2");
92 }
93 mLanguage = grp.readEntry(QStringLiteral("language"), QStringLiteral("en"));
94 mUseLocalInstance = grp.readEntry(QStringLiteral("useLocalInstance"), false);
95 // TODO add options ?
96}
97
98void LanguageToolManager::saveSettings()
99{
100 KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myLanguageToolManagerGroupName));
101 grp.writeEntry(QStringLiteral("languagetoolpath"), mLanguageToolPath);
102 grp.writeEntry(QStringLiteral("language"), mLanguage);
103 grp.writeEntry(QStringLiteral("useLocalInstance"), mUseLocalInstance);
104}
105
106QString LanguageToolManager::language() const
107{
108 return mLanguage;
109}
110
111void LanguageToolManager::setLanguage(const QString &language)
112{
113 mLanguage = language;
114}
115
116bool LanguageToolManager::useLocalInstance() const
117{
118 return mUseLocalInstance;
119}
120
121void LanguageToolManager::setUseLocalInstance(bool useLocalInstance)
122{
123 mUseLocalInstance = useLocalInstance;
124}
125
126bool LanguageToolManager::allowToGetListOfLanguages() const
127{
128 return mUseLocalInstance;
129}
130
131#include "moc_languagetoolmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString path(const QString &relativePath)
bool isValid() const const
void setRgb(QRgb rgb)
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
QRandomGenerator * global()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.