File indexing completed on 2024-05-12 15:27:52

0001 /***************************************************************************
0002 File                 : MQTTErrorWidget.cpp
0003 Project              : LabPlot
0004 Description          : Widget for informing about an MQTT error, and for trying to solve it
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2018 by Kovacs Ferencz (kferike98@gmail.com)
0007 ***************************************************************************/
0008 
0009 /***************************************************************************
0010 *                                                                         *
0011 *  This program is free software; you can redistribute it and/or modify   *
0012 *  it under the terms of the GNU General Public License as published by   *
0013 *  the Free Software Foundation; either version 2 of the License, or      *
0014 *  (at your option) any later version.                                    *
0015 *                                                                         *
0016 *  This program is distributed in the hope that it will be useful,        *
0017 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0018 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0019 *  GNU General Public License for more details.                           *
0020 *                                                                         *
0021 *   You should have received a copy of the GNU General Public License     *
0022 *   along with this program; if not, write to the Free Software           *
0023 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0024 *   Boston, MA  02110-1301  USA                                           *
0025 *                                                                         *
0026 ***************************************************************************/
0027 
0028 #include "src/kdefrontend/datasources/MQTTErrorWidget.h"
0029 
0030 #include "backend/datasources/MQTTClient.h"
0031 #include <QMqttSubscription>
0032 #include <QMqttTopicFilter>
0033 #include <QMqttMessage>
0034 
0035 MQTTErrorWidget::MQTTErrorWidget(QMqttClient::ClientError error, MQTTClient* client, QWidget *parent) : QWidget(parent),
0036     m_error(error),
0037     m_client(client) {
0038 
0039     ui.setupUi(this);
0040     bool close = false;
0041     //showing the appropriate options according to the error type
0042     switch (m_error) {
0043     case QMqttClient::ClientError::IdRejected:
0044         ui.lePassword->hide();
0045         ui.lPassword->hide();
0046         ui.leUserName->hide();
0047         ui.lUserName->hide();
0048         ui.lErrorType->setText("The client ID is malformed. This might be related to its length.\nSet new ID!");
0049         break;
0050     case QMqttClient::ClientError::BadUsernameOrPassword:
0051         ui.lId->hide();
0052         ui.leId->hide();
0053         ui.lErrorType->setText("The data in the username or password is malformed.\nSet new username and password!");
0054         break;
0055     case QMqttClient::ClientError::NotAuthorized:
0056         ui.lId->hide();
0057         ui.leId->hide();
0058         ui.lErrorType->setText("The client is not authorized to connect.");
0059         break;
0060     case QMqttClient::ClientError::ServerUnavailable:
0061         ui.lePassword->hide();
0062         ui.lPassword->hide();
0063         ui.leUserName->hide();
0064         ui.lUserName->hide();
0065         ui.lErrorType->setText("The network connection has been established, but the service is unavailable on the broker side.");
0066         break;
0067     case QMqttClient::ClientError::UnknownError:
0068         ui.lePassword->hide();
0069         ui.lPassword->hide();
0070         ui.leUserName->hide();
0071         ui.lUserName->hide();
0072         ui.lErrorType->setText("An unknown error occurred.");
0073         break;
0074     case QMqttClient::NoError:
0075     case QMqttClient::InvalidProtocolVersion:
0076     case QMqttClient::TransportInvalid:
0077     case QMqttClient::ProtocolViolation:
0078     case QMqttClient::Mqtt5SpecificError:
0079         close = true;
0080         break;
0081     default:
0082         close = true;
0083         break;
0084     }
0085     connect(ui.bChange, &QPushButton::clicked, this, &MQTTErrorWidget::tryToReconnect);
0086     setAttribute(Qt::WA_DeleteOnClose);
0087     if (close)
0088         this->close();
0089 }
0090 
0091 /*!
0092  *\brief Try to reconnect in MQTTClient after reseting options that might cause the error
0093  */
0094 void MQTTErrorWidget::tryToReconnect() {
0095     bool ok = false;
0096     switch (m_error) {
0097     case QMqttClient::ClientError::IdRejected:
0098         if (!ui.leId->text().isEmpty()) {
0099             m_client->setMQTTClientId(ui.leId->text());
0100             m_client->read();
0101             ok = true;
0102         }
0103         break;
0104     case QMqttClient::ClientError::BadUsernameOrPassword:
0105         if (!ui.lePassword->text().isEmpty() && !ui.leUserName->text().isEmpty()) {
0106             m_client->setMQTTClientAuthentication(ui.leUserName->text(), ui.lePassword->text());
0107             m_client->read();
0108             ok = true;
0109         }
0110         break;
0111     case QMqttClient::ClientError::NotAuthorized:
0112         if (!ui.lePassword->text().isEmpty() && !ui.leUserName->text().isEmpty()) {
0113             m_client->setMQTTClientAuthentication(ui.leUserName->text(), ui.lePassword->text());
0114             m_client->read();
0115             ok = true;
0116         }
0117         break;
0118     case QMqttClient::NoError:
0119     case QMqttClient::InvalidProtocolVersion:
0120     case QMqttClient::TransportInvalid:
0121     case QMqttClient::ServerUnavailable:
0122     case QMqttClient::UnknownError:
0123     case QMqttClient::ProtocolViolation:
0124     case QMqttClient::Mqtt5SpecificError:
0125         break;
0126     default:
0127         break;
0128     }
0129     if (ok)
0130         this->close();
0131 }