diff --git a/kmail/grammarplugins/languagetool/src/CMakeLists.txt b/kmail/grammarplugins/languagetool/src/CMakeLists.txt index fda2c3c1..2aed12ff 100644 --- a/kmail/grammarplugins/languagetool/src/CMakeLists.txt +++ b/kmail/grammarplugins/languagetool/src/CMakeLists.txt @@ -1,33 +1,34 @@ set(kmaillanguagetool_SRCS languagetoolparser.cpp languagetoolgrammarerror.cpp languagetoolresultwidget.cpp languagetoolconfigdialog.cpp languagetoolconfigwidget.cpp languagetoolmanager.cpp languagetoolresultjob.cpp languagetoolcombobox.cpp languagetoolgetlistoflanguagejob.cpp languagetoollistoflanguagesparser.cpp languageinfo.cpp + languagetoolupdatecombobox.cpp ) ecm_qt_declare_logging_category(kmaillanguagetool_SRCS HEADER liblanguagetool_debug.h IDENTIFIER LIBLANGUAGE_PLUGIN_LOG CATEGORY_NAME org.kde.pim.liblanguagetoolplugin) add_library(kmaillanguagetool ${kmaillanguagetool_SRCS}) generate_export_header(kmaillanguagetool BASE_NAME libkmaillanguagetool) target_link_libraries(kmaillanguagetool KF5::I18n KF5::IconThemes KF5::ConfigCore KF5::WidgetsAddons KF5::KIOWidgets KF5::MessageComposer grammarcommon ) set_target_properties(kmaillanguagetool PROPERTIES OUTPUT_NAME kmaillanguagetool VERSION ${KDEPIMADDONS_LIB_VERSION} SOVERSION ${KDEPIMADDONS_LIB_SOVERSION} ) install(TARGETS kmaillanguagetool ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) diff --git a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.cpp b/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.cpp index 52517847..6d689596 100644 --- a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.cpp +++ b/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.cpp @@ -1,156 +1,127 @@ /* Copyright (C) 2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "languagetoolconfigwidget.h" #include "languagetoolmanager.h" #include "languagetoolcombobox.h" +#include "languagetoolupdatecombobox.h" #include "liblanguagetool_debug.h" #include "languagetoolgetlistoflanguagejob.h" #include "languagetoollistoflanguagesparser.h" #include #include #include #include #include #include #include #include #include #include #include #include LanguageToolConfigWidget::LanguageToolConfigWidget(QWidget *parent) : QWidget(parent) { + mLanguageToolUpdateCombobox = new LanguageToolUpdateComboBox(this); QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainlayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mUseLocalInstance = new QCheckBox(i18n("Use Local Instance"), this); mUseLocalInstance->setObjectName(QStringLiteral("uselocalinstance")); mainLayout->addWidget(mUseLocalInstance); QHBoxLayout *instanceLayout = new QHBoxLayout; instanceLayout->setObjectName(QStringLiteral("instancelayout")); instanceLayout->setContentsMargins(0, 0, 0, 0); mInstancePathLabel = new QLabel(i18n("Instance Path:"), this); mInstancePathLabel->setObjectName(QStringLiteral("instancepath")); mInstancePathLabel->setEnabled(false); instanceLayout->addWidget(mInstancePathLabel); mInstancePath = new QLineEdit(this); mInstancePath->setObjectName(QStringLiteral("instancepath")); mInstancePath->setEnabled(false); mInstancePath->setClearButtonEnabled(true); instanceLayout->addWidget(mInstancePath); mainLayout->addLayout(instanceLayout); connect(mUseLocalInstance, &QCheckBox::clicked, this, [this](bool b) { updateWidgets(b); } ); QHBoxLayout *languageLayout = new QHBoxLayout; languageLayout->setObjectName(QStringLiteral("languagelayout")); QLabel *languageLabel = new QLabel(i18n("Language:"), this); languageLabel->setObjectName(QStringLiteral("languageLabel")); languageLayout->addWidget(languageLabel); mLanguageToolCombobox = new LanguageToolComboBox(this); mLanguageToolCombobox->setObjectName(QStringLiteral("languagecombobox")); languageLayout->addWidget(mLanguageToolCombobox); + mLanguageToolUpdateCombobox->setLanguageToolCombobox(mLanguageToolCombobox); + mLanguageToolUpdateCombobox->setParentWidget(this); QToolButton *refreshButton = new QToolButton(this); refreshButton->setObjectName(QStringLiteral("refreshbutton")); refreshButton->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh"))); refreshButton->setToolTip(i18n("Refresh")); languageLayout->addWidget(refreshButton); - connect(refreshButton, &QToolButton::clicked, this, &LanguageToolConfigWidget::refreshListOfLanguages); + connect(refreshButton, &QToolButton::clicked, this, [this]() { + mLanguageToolUpdateCombobox->checkListOfLanguagesFromSpecificPath(mInstancePath->text()); + }); mainLayout->addLayout(languageLayout); mainLayout->addStretch(1); - uploadListOfLanguages(); + mLanguageToolUpdateCombobox->refreshListOfLanguages(); loadSettings(); updateWidgets(mUseLocalInstance->isChecked()); } LanguageToolConfigWidget::~LanguageToolConfigWidget() { saveSettings(); } void LanguageToolConfigWidget::updateWidgets(bool enabled) { mInstancePathLabel->setEnabled(enabled); mInstancePath->setEnabled(enabled); } -void LanguageToolConfigWidget::refreshListOfLanguages() -{ - LanguageToolGetListOfLanguageJob *job = new LanguageToolGetListOfLanguageJob(this); - job->setUrl(LanguageToolManager::convertToLanguagePath(mInstancePath->text())); - job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager()); - connect(job, &LanguageToolGetListOfLanguageJob::finished, this, &LanguageToolConfigWidget::slotGetLanguagesFinished); - connect(job, &LanguageToolGetListOfLanguageJob::error, this, &LanguageToolConfigWidget::slotGetLanguagesError); - job->start(); -} - void LanguageToolConfigWidget::loadSettings() { mUseLocalInstance->setChecked(LanguageToolManager::self()->useLocalInstance()); mInstancePath->setText(LanguageToolManager::self()->languageToolPath()); mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language()); } void LanguageToolConfigWidget::saveSettings() { LanguageToolManager::self()->setUseLocalInstance(mUseLocalInstance->isChecked()); LanguageToolManager::self()->setLanguageToolPath(mInstancePath->text()); LanguageToolManager::self()->setLanguage(mLanguageToolCombobox->language()); LanguageToolManager::self()->saveSettings(); } - -void LanguageToolConfigWidget::uploadListOfLanguages() -{ - LanguageToolGetListOfLanguageJob *job = new LanguageToolGetListOfLanguageJob(this); - job->setUrl(LanguageToolManager::self()->languageToolLanguagesPath()); - job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager()); - connect(job, &LanguageToolGetListOfLanguageJob::finished, this, &LanguageToolConfigWidget::slotGetLanguagesFinished); - connect(job, &LanguageToolGetListOfLanguageJob::error, this, &LanguageToolConfigWidget::slotGetLanguagesError); - job->start(); -} - -void LanguageToolConfigWidget::slotGetLanguagesError(const QString &error) -{ - qCWarning(LIBLANGUAGE_PLUGIN_LOG) << "Error during loading languages from server : " << error; - KMessageBox::error(this, i18n("An error was found during got languages:\n%1", error), i18n("List of Languages")); -} - -void LanguageToolConfigWidget::slotGetLanguagesFinished(const QString &result) -{ - const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); - const QJsonArray fields = doc.array(); - LanguageToolListOfLanguagesParser parser; - mLanguageToolCombobox->fillComboBox(parser.parseResult(fields)); - mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language()); -} diff --git a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h b/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h index 5498d24b..ce9464c4 100644 --- a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h +++ b/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h @@ -1,50 +1,48 @@ /* Copyright (C) 2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef LANGUAGETOOLCONFIGWIDGET_H #define LANGUAGETOOLCONFIGWIDGET_H #include #include "libkmaillanguagetool_export.h" class QCheckBox; class QLineEdit; class QLabel; class LanguageToolComboBox; +class LanguageToolUpdateComboBox; class LIBKMAILLANGUAGETOOL_EXPORT LanguageToolConfigWidget : public QWidget { Q_OBJECT public: explicit LanguageToolConfigWidget(QWidget *parent = nullptr); ~LanguageToolConfigWidget(); void loadSettings(); void saveSettings(); private: Q_DISABLE_COPY(LanguageToolConfigWidget) - void uploadListOfLanguages(); - void slotGetLanguagesError(const QString &error); - void slotGetLanguagesFinished(const QString &result); - void refreshListOfLanguages(); void updateWidgets(bool enabled); QCheckBox *mUseLocalInstance = nullptr; QLineEdit *mInstancePath = nullptr; QLabel *mInstancePathLabel = nullptr; LanguageToolComboBox *mLanguageToolCombobox = nullptr; + LanguageToolUpdateComboBox *mLanguageToolUpdateCombobox = nullptr; }; #endif // LANGUAGETOOLCONFIGWIDGET_H diff --git a/kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.cpp b/kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.cpp new file mode 100644 index 00000000..ebc710af --- /dev/null +++ b/kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.cpp @@ -0,0 +1,98 @@ +/* + Copyright (C) 2019 Montel Laurent + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This program 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "languagetoolupdatecombobox.h" +#include "languagetoolgetlistoflanguagejob.h" +#include "languagetoollistoflanguagesparser.h" +#include "languagetoolmanager.h" +#include "liblanguagetool_debug.h" +#include "languagetoolcombobox.h" + +#include +#include +#include +#include + +LanguageToolUpdateComboBox::LanguageToolUpdateComboBox(QObject *parent) + : QObject(parent) +{ + +} + +LanguageToolUpdateComboBox::~LanguageToolUpdateComboBox() +{ + +} + + +void LanguageToolUpdateComboBox::checkListOfLanguagesFromSpecificPath(const QString &url) +{ + LanguageToolGetListOfLanguageJob *job = new LanguageToolGetListOfLanguageJob(this); + job->setUrl(LanguageToolManager::convertToLanguagePath(url)); + job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager()); + connect(job, &LanguageToolGetListOfLanguageJob::finished, this, &LanguageToolUpdateComboBox::slotGetLanguagesFinished); + connect(job, &LanguageToolGetListOfLanguageJob::error, this, &LanguageToolUpdateComboBox::slotGetLanguagesError); + job->start(); +} + +void LanguageToolUpdateComboBox::refreshListOfLanguages() +{ + LanguageToolGetListOfLanguageJob *job = new LanguageToolGetListOfLanguageJob(this); + job->setUrl(LanguageToolManager::self()->languageToolLanguagesPath()); + job->setNetworkAccessManager(LanguageToolManager::self()->networkAccessManager()); + connect(job, &LanguageToolGetListOfLanguageJob::finished, this, &LanguageToolUpdateComboBox::slotGetLanguagesFinished); + connect(job, &LanguageToolGetListOfLanguageJob::error, this, &LanguageToolUpdateComboBox::slotGetLanguagesError); + job->start(); +} + +QWidget *LanguageToolUpdateComboBox::parentWidget() const +{ + return mParentWidget; +} + +void LanguageToolUpdateComboBox::setParentWidget(QWidget *parentWidget) +{ + mParentWidget = parentWidget; +} + +LanguageToolComboBox *LanguageToolUpdateComboBox::languageToolCombobox() const +{ + return mLanguageToolCombobox; +} + +void LanguageToolUpdateComboBox::setLanguageToolCombobox(LanguageToolComboBox *languageToolCombobox) +{ + mLanguageToolCombobox = languageToolCombobox; +} + + +void LanguageToolUpdateComboBox::slotGetLanguagesError(const QString &error) +{ + qCWarning(LIBLANGUAGE_PLUGIN_LOG) << "Error during loading languages from server : " << error; + KMessageBox::error(parentWidget(), i18n("An error was found during got languages:\n%1", error), i18n("List of Languages")); +} + +void LanguageToolUpdateComboBox::slotGetLanguagesFinished(const QString &result) +{ + const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8()); + const QJsonArray fields = doc.array(); + LanguageToolListOfLanguagesParser parser; + mLanguageToolCombobox->fillComboBox(parser.parseResult(fields)); + mLanguageToolCombobox->setLanguage(LanguageToolManager::self()->language()); +} diff --git a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h b/kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.h similarity index 61% copy from kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h copy to kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.h index 5498d24b..2e53f6b1 100644 --- a/kmail/grammarplugins/languagetool/src/languagetoolconfigwidget.h +++ b/kmail/grammarplugins/languagetool/src/languagetoolupdatecombobox.h @@ -1,50 +1,48 @@ /* Copyright (C) 2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef LANGUAGETOOLCONFIGWIDGET_H -#define LANGUAGETOOLCONFIGWIDGET_H - -#include -#include "libkmaillanguagetool_export.h" -class QCheckBox; -class QLineEdit; -class QLabel; +#ifndef LANGUAGETOOLUPDATECOMBOBOX_H +#define LANGUAGETOOLUPDATECOMBOBOX_H + +#include class LanguageToolComboBox; -class LIBKMAILLANGUAGETOOL_EXPORT LanguageToolConfigWidget : public QWidget +class LanguageToolUpdateComboBox : public QObject { Q_OBJECT public: - explicit LanguageToolConfigWidget(QWidget *parent = nullptr); - ~LanguageToolConfigWidget(); - void loadSettings(); - void saveSettings(); + explicit LanguageToolUpdateComboBox(QObject *parent = nullptr); + ~LanguageToolUpdateComboBox(); + + LanguageToolComboBox *languageToolCombobox() const; + void setLanguageToolCombobox(LanguageToolComboBox *languageToolCombobox); + + void checkListOfLanguagesFromSpecificPath(const QString &url); + void refreshListOfLanguages(); + + QWidget *parentWidget() const; + void setParentWidget(QWidget *parentWidget); + private: - Q_DISABLE_COPY(LanguageToolConfigWidget) - void uploadListOfLanguages(); - void slotGetLanguagesError(const QString &error); void slotGetLanguagesFinished(const QString &result); - void refreshListOfLanguages(); - void updateWidgets(bool enabled); - QCheckBox *mUseLocalInstance = nullptr; - QLineEdit *mInstancePath = nullptr; - QLabel *mInstancePathLabel = nullptr; + void slotGetLanguagesError(const QString &error); LanguageToolComboBox *mLanguageToolCombobox = nullptr; + QWidget *mParentWidget = nullptr; }; -#endif // LANGUAGETOOLCONFIGWIDGET_H +#endif // LANGUAGETOOLUPDATECOMBOBOX_H