KTextAddons

languagetoolupdatecombobox.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 "languagetoolupdatecombobox.h"
8#include "languagetoolcombobox.h"
9#include "languagetoolgetlistoflanguagejob.h"
10#include "languagetoollistoflanguagesparser.h"
11#include "languagetoolmanager.h"
12#include "textgrammarcheck_debug.h"
13
14#include <KLocalizedString>
15#include <KMessageBox>
16#include <QJsonArray>
17#include <QJsonDocument>
18using namespace TextGrammarCheck;
19LanguageToolUpdateComboBox::LanguageToolUpdateComboBox(QObject *parent)
20 : QObject(parent)
21{
22}
23
24LanguageToolUpdateComboBox::~LanguageToolUpdateComboBox() = default;
25
26void LanguageToolUpdateComboBox::checkListOfLanguagesFromSpecificPath(const QString &url)
27{
28 auto job = new LanguageToolGetListOfLanguageJob(this);
29 job->setUrl(url);
30 job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager());
31 connect(job, &LanguageToolGetListOfLanguageJob::finished, this, &LanguageToolUpdateComboBox::slotGetLanguagesFinished);
32 connect(job, &LanguageToolGetListOfLanguageJob::error, this, &LanguageToolUpdateComboBox::slotGetLanguagesError);
33 job->start();
34}
35
36void LanguageToolUpdateComboBox::firstRefreshListOfLanguages()
37{
38 if (!mBListWasLoaded) {
39 checkListOfLanguagesFromSpecificPath(LanguageToolManager::self()->languageToolLanguagesPath());
40 } else {
41 mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language());
42 }
43}
44
45void LanguageToolUpdateComboBox::refreshListOfLanguages()
46{
47 if (LanguageToolManager::self()->allowToGetListOfLanguages()) {
48 checkListOfLanguagesFromSpecificPath(LanguageToolManager::self()->languageToolLanguagesPath());
49 }
50}
51
52QWidget *LanguageToolUpdateComboBox::parentWidget() const
53{
54 return mParentWidget;
55}
56
57void LanguageToolUpdateComboBox::setParentWidget(QWidget *parentWidget)
58{
59 mParentWidget = parentWidget;
60}
61
62LanguageToolComboBox *LanguageToolUpdateComboBox::languageToolCombobox() const
63{
64 return mLanguageToolCombobox;
65}
66
67void LanguageToolUpdateComboBox::setLanguageToolCombobox(LanguageToolComboBox *languageToolCombobox)
68{
69 mLanguageToolCombobox = languageToolCombobox;
70}
71
72void LanguageToolUpdateComboBox::slotGetLanguagesError(const QString &error)
73{
74 qCWarning(TEXTGRAMMARCHECK_LOG) << "Error during loading languages from server : " << error;
75 KMessageBox::error(parentWidget(),
76 i18n("An error occurred attempting to load the list of available languages:\n%1", error),
77 i18nc("@title:window", "List of Languages"));
78}
79
80void LanguageToolUpdateComboBox::slotGetLanguagesFinished(const QString &result)
81{
82 const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
83 const QJsonArray fields = doc.array();
84 LanguageToolListOfLanguagesParser parser;
85 mLanguageToolCombobox->fillComboBox(parser.parseResult(fields));
86 mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language());
87 mBListWasLoaded = true;
88}
89
90#include "moc_languagetoolupdatecombobox.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QJsonArray array() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.