File indexing completed on 2025-11-16 03:41:19
0001 /* 0002 File : MQTTConnectionManagerWidget.cpp 0003 Project : LabPlot 0004 Description : widget for managing MQTT connections 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2018 Ferencz Kovacs <kferike98@gmail.com> 0007 SPDX-FileCopyrightText: 2018-2019 Alexander Semke <alexander.semke@web.de> 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "MQTTConnectionManagerWidget.h" 0012 #include "backend/lib/macros.h" 0013 0014 #include <QListWidgetItem> 0015 #include <QTimer> 0016 #include <QtMqtt> 0017 0018 #include <KConfig> 0019 #include <KConfigGroup> 0020 #include <KLocalizedString> 0021 #include <KMessageBox> 0022 0023 #include <kcoreaddons_version.h> 0024 0025 /*! 0026 \class MQTTConnectionManagerWidget 0027 \brief widget for managing MQTT connections, embedded in \c MQTTConnectionManagerDialog. 0028 0029 \ingroup kdefrontend 0030 */ 0031 MQTTConnectionManagerWidget::MQTTConnectionManagerWidget(QWidget* parent, const QString& conn) 0032 : QWidget(parent) 0033 , m_initConnName(conn) { 0034 ui.setupUi(this); 0035 0036 m_configPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).constFirst() + QStringLiteral("MQTT_connections"); 0037 0038 ui.lePort->setValidator(new QIntValidator(ui.lePort)); 0039 ui.bAdd->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); 0040 ui.bRemove->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); 0041 ui.bAdd->setToolTip(i18n("Add new MQTT connection")); 0042 ui.bRemove->setToolTip(i18n("Remove selected MQTT connection")); 0043 ui.bTest->setIcon(QIcon::fromTheme(QStringLiteral("network-connect"))); 0044 0045 // SIGNALs/SLOTs 0046 connect(ui.leName, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::nameChanged); 0047 connect(ui.lwConnections, &QListWidget::currentRowChanged, this, &MQTTConnectionManagerWidget::connectionChanged); 0048 connect(ui.bAdd, &QPushButton::clicked, this, &MQTTConnectionManagerWidget::addConnection); 0049 connect(ui.bRemove, &QPushButton::clicked, this, &MQTTConnectionManagerWidget::deleteConnection); 0050 connect(ui.leHost, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::hostChanged); 0051 connect(ui.lePort, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::portChanged); 0052 connect(ui.leUserName, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::userNameChanged); 0053 connect(ui.lePassword, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::passwordChanged); 0054 connect(ui.leID, &QLineEdit::textChanged, this, &MQTTConnectionManagerWidget::clientIdChanged); 0055 connect(ui.chbAuthentication, &QCheckBox::toggled, this, &MQTTConnectionManagerWidget::authenticationChecked); 0056 connect(ui.chbID, &QCheckBox::toggled, this, &MQTTConnectionManagerWidget::idChecked); 0057 connect(ui.chbRetain, &QCheckBox::toggled, this, &MQTTConnectionManagerWidget::retainChecked); 0058 connect(ui.bTest, &QPushButton::clicked, this, &MQTTConnectionManagerWidget::testConnection); 0059 0060 ui.lePassword->hide(); 0061 ui.lPassword->hide(); 0062 ui.lePassword->setToolTip(i18n("Please set a password.")); 0063 ui.leUserName->hide(); 0064 ui.lUsername->hide(); 0065 ui.leUserName->setToolTip(i18n("Please set a username.")); 0066 ui.leID->hide(); 0067 ui.lID->hide(); 0068 ui.leID->setToolTip(i18n("Please set a client ID.")); 0069 ui.leHost->setToolTip(i18n("Please set a valid host name.")); 0070 ui.leHost->setToolTip(i18n("Please set a valid name.")); 0071 0072 QTimer::singleShot(100, this, SLOT(loadConnections())); 0073 } 0074 0075 MQTTConnectionManagerWidget::~MQTTConnectionManagerWidget() { 0076 delete m_client; 0077 } 0078 0079 /*! 0080 * \brief Returns the currently selected connection's name 0081 */ 0082 QString MQTTConnectionManagerWidget::connection() const { 0083 if (ui.lwConnections->currentItem()) 0084 return ui.lwConnections->currentItem()->text(); 0085 0086 return {}; 0087 } 0088 0089 /*! 0090 * \brief Shows the settings of the currently selected connection 0091 * \param index the index of the new connection 0092 */ 0093 void MQTTConnectionManagerWidget::connectionChanged(int index) { 0094 CONDITIONAL_RETURN_NO_LOCK; 0095 0096 if (index == -1) { 0097 m_currentConnection = nullptr; 0098 return; 0099 } 0100 0101 m_initializing = true; 0102 m_currentConnection = &m_connections[index]; 0103 0104 // show the settings for the selected connection 0105 ui.leName->setText(m_currentConnection->name); 0106 ui.leHost->setText(m_currentConnection->hostName); 0107 ui.lePort->setText(QString::number(m_currentConnection->port)); 0108 0109 if (m_currentConnection->useAuthentication) { 0110 ui.chbAuthentication->setChecked(true); 0111 ui.leUserName->setText(m_currentConnection->userName); 0112 ui.lePassword->setText(m_currentConnection->password); 0113 } else 0114 ui.chbAuthentication->setChecked(false); 0115 0116 if (m_currentConnection->useID) { 0117 ui.chbID->setChecked(true); 0118 ui.leID->setText(m_currentConnection->clientID); 0119 } else 0120 ui.chbID->setChecked(false); 0121 0122 ui.chbRetain->setChecked(m_currentConnection->retain); 0123 0124 m_initializing = false; 0125 } 0126 0127 /*! 0128 * \brief Called when the name is changed 0129 * Sets the name for the current connection 0130 */ 0131 void MQTTConnectionManagerWidget::nameChanged(const QString& name) { 0132 if (name.isEmpty()) { 0133 SET_WARNING_STYLE(ui.leName) 0134 ui.leHost->setToolTip(i18n("Please set a valid name.")); 0135 } else { 0136 // check uniqueness of the provided name 0137 bool unique = true; 0138 for (int i = 0; i < ui.lwConnections->count(); ++i) { 0139 if (ui.lwConnections->currentRow() == i) 0140 continue; 0141 0142 if (name == ui.lwConnections->item(i)->text()) { 0143 unique = false; 0144 break; 0145 } 0146 } 0147 0148 if (unique) { 0149 ui.leName->setStyleSheet(QString()); 0150 ui.leName->setToolTip(QString()); 0151 ui.lwConnections->currentItem()->setText(name); 0152 0153 if (!m_initializing) { 0154 m_currentConnection->name = name; 0155 Q_EMIT changed(); 0156 } 0157 } else { 0158 SET_WARNING_STYLE(ui.leName) 0159 ui.leHost->setToolTip(i18n("Please provide a unique name.")); 0160 } 0161 } 0162 } 0163 0164 /*! 0165 * \brief Called when the host name is changed 0166 * Sets the host name for the current connection 0167 */ 0168 void MQTTConnectionManagerWidget::hostChanged(const QString& hostName) { 0169 if (hostName.isEmpty()) { 0170 SET_WARNING_STYLE(ui.leHost) 0171 ui.leHost->setToolTip(i18n("Please set a valid host name.")); 0172 } else { 0173 m_currentConnection->hostName = hostName; 0174 // check uniqueness of the provided host name 0175 bool unique = true; 0176 for (auto& c : m_connections) { 0177 if (m_currentConnection == &c) 0178 continue; 0179 0180 if (m_currentConnection->hostName == c.hostName && m_currentConnection->port == c.port) { 0181 unique = false; 0182 break; 0183 } 0184 } 0185 0186 if (!unique) { 0187 SET_WARNING_STYLE(ui.leHost) 0188 SET_WARNING_STYLE(ui.lePort) 0189 ui.leHost->setToolTip(i18n("Host name and port must be unique.")); 0190 ui.lePort->setToolTip(i18n("Host name and port must be unique.")); 0191 } else { 0192 ui.leHost->setStyleSheet(QString()); 0193 ui.lePort->setStyleSheet(QString()); 0194 ui.leHost->setToolTip(QString()); 0195 ui.lePort->setToolTip(QString()); 0196 0197 if (!m_initializing) 0198 Q_EMIT changed(); 0199 } 0200 } 0201 } 0202 0203 /*! 0204 * \brief Called when the port is changed 0205 * Sets the port for the current connection 0206 */ 0207 void MQTTConnectionManagerWidget::portChanged(const QString& portString) { 0208 if (portString.isEmpty()) { 0209 SET_WARNING_STYLE(ui.leHost) 0210 ui.leHost->setToolTip(i18n("Please set a valid port.")); 0211 } else { 0212 m_currentConnection->port = portString.simplified().toInt(); 0213 // check uniqueness of the provided host name 0214 bool unique = true; 0215 for (auto& c : m_connections) { 0216 if (m_currentConnection == &c) 0217 continue; 0218 0219 if (m_currentConnection->hostName == c.hostName && m_currentConnection->port == c.port) { 0220 unique = false; 0221 break; 0222 } 0223 } 0224 0225 if (!unique) { 0226 SET_WARNING_STYLE(ui.leHost) 0227 SET_WARNING_STYLE(ui.lePort) 0228 ui.leHost->setToolTip(i18n("Host name and port must be unique.")); 0229 ui.lePort->setToolTip(i18n("Host name and port must be unique.")); 0230 } else { 0231 ui.leHost->setStyleSheet(QString()); 0232 ui.leHost->setToolTip(QString()); 0233 ui.lePort->setStyleSheet(QString()); 0234 ui.lePort->setToolTip(QString()); 0235 0236 if (!m_initializing) 0237 Q_EMIT changed(); 0238 } 0239 } 0240 } 0241 0242 /*! 0243 *\brief Called when authentication checkbox's state is changed, 0244 * if checked two lineEdits are shown so the user can set the username and password 0245 * 0246 * \param state the state of the checkbox 0247 */ 0248 void MQTTConnectionManagerWidget::authenticationChecked(bool state) { 0249 if (state) { 0250 ui.lPassword->show(); 0251 ui.lePassword->show(); 0252 ui.lUsername->show(); 0253 ui.leUserName->show(); 0254 0255 if (m_currentConnection) { 0256 ui.lePassword->setText(m_currentConnection->password); 0257 ui.leUserName->setText(m_currentConnection->userName); 0258 if (!m_initializing) 0259 m_currentConnection->useAuthentication = true; 0260 } 0261 0262 } else { 0263 ui.lPassword->hide(); 0264 ui.lePassword->hide(); 0265 ui.lePassword->clear(); 0266 ui.lUsername->hide(); 0267 ui.leUserName->hide(); 0268 ui.leUserName->clear(); 0269 0270 if (m_currentConnection && !m_initializing) { 0271 m_currentConnection->useAuthentication = false; 0272 } 0273 } 0274 0275 if (!m_initializing) 0276 Q_EMIT changed(); 0277 } 0278 0279 /*! 0280 *\brief called when ID checkbox's state is changed, if checked a lineEdit is shown so the user can set the ID 0281 * \param state the state of the checkbox 0282 */ 0283 void MQTTConnectionManagerWidget::idChecked(bool state) { 0284 if (state) { 0285 ui.lID->show(); 0286 ui.leID->show(); 0287 0288 if (m_currentConnection) { 0289 ui.leID->setText(m_currentConnection->clientID); 0290 if (!m_initializing) 0291 m_currentConnection->useID = true; 0292 } 0293 0294 } else { 0295 ui.lID->hide(); 0296 ui.leID->hide(); 0297 ui.leID->clear(); 0298 0299 if (m_currentConnection && !m_initializing) { 0300 m_currentConnection->useID = false; 0301 } 0302 } 0303 0304 if (!m_initializing) 0305 Q_EMIT changed(); 0306 } 0307 0308 /*! 0309 * \brief called when retain checkbox's state is changed 0310 * \param state the state of the checkbox 0311 */ 0312 void MQTTConnectionManagerWidget::retainChecked(bool state) { 0313 CONDITIONAL_RETURN_NO_LOCK; 0314 0315 if (m_currentConnection) { 0316 if (state) 0317 m_currentConnection->retain = true; 0318 else 0319 m_currentConnection->retain = false; 0320 } 0321 Q_EMIT changed(); 0322 } 0323 0324 /*! 0325 * \brief Called when the username is changed 0326 * Sets the username for the current connection 0327 */ 0328 void MQTTConnectionManagerWidget::userNameChanged(const QString& userName) { 0329 if (userName.isEmpty()) { 0330 SET_WARNING_STYLE(ui.leUserName) 0331 ui.leUserName->setToolTip(i18n("Please set a username.")); 0332 } else { 0333 ui.leUserName->setStyleSheet(QString()); 0334 ui.leUserName->setToolTip(QString()); 0335 } 0336 0337 CONDITIONAL_RETURN_NO_LOCK; 0338 0339 if (m_currentConnection) 0340 m_currentConnection->userName = userName; 0341 Q_EMIT changed(); 0342 } 0343 0344 /*! 0345 * \brief Called when the password is changed 0346 * Sets the password for the current connection 0347 */ 0348 void MQTTConnectionManagerWidget::passwordChanged(const QString& password) { 0349 if (password.isEmpty()) { 0350 SET_WARNING_STYLE(ui.lePassword) 0351 ui.lePassword->setToolTip(i18n("Please set a password.")); 0352 } else { 0353 ui.lePassword->setStyleSheet(QString()); 0354 ui.lePassword->setToolTip(QString()); 0355 } 0356 0357 CONDITIONAL_RETURN_NO_LOCK; 0358 0359 if (m_currentConnection) 0360 m_currentConnection->password = password; 0361 Q_EMIT changed(); 0362 } 0363 0364 /*! 0365 * \brief Called when the client ID is changed 0366 * Sets the client ID for the current connection 0367 */ 0368 void MQTTConnectionManagerWidget::clientIdChanged(const QString& clientID) { 0369 if (clientID.isEmpty()) { 0370 SET_WARNING_STYLE(ui.leID) 0371 ui.leID->setToolTip(i18n("Please set a client ID.")); 0372 } else { 0373 ui.leID->setStyleSheet(QString()); 0374 ui.leID->setToolTip(QString()); 0375 } 0376 0377 CONDITIONAL_RETURN_NO_LOCK; 0378 0379 if (m_currentConnection) 0380 m_currentConnection->clientID = clientID; 0381 Q_EMIT changed(); 0382 } 0383 0384 /*! 0385 adds a new sample connection to the end of the list. 0386 */ 0387 void MQTTConnectionManagerWidget::addConnection() { 0388 qDebug() << "Adding new connection"; 0389 MQTTConnection conn; 0390 conn.name = uniqueName(); 0391 conn.hostName = QStringLiteral("localhost"); 0392 0393 m_connections.append(conn); 0394 m_currentConnection = &m_connections.back(); 0395 ui.lwConnections->addItem(conn.hostName); 0396 ui.lwConnections->setCurrentRow(m_connections.size() - 1); 0397 0398 // we have now more than one connection, enable widgets 0399 ui.bRemove->setEnabled(true); 0400 ui.leHost->setEnabled(true); 0401 ui.lePort->setEnabled(true); 0402 ui.leUserName->setEnabled(true); 0403 ui.lePassword->setEnabled(true); 0404 ui.leID->setEnabled(true); 0405 ui.leName->setEnabled(true); 0406 Q_EMIT changed(); 0407 } 0408 0409 /*! 0410 removes the current selected connection. 0411 */ 0412 void MQTTConnectionManagerWidget::deleteConnection() { 0413 #if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) 0414 auto status = KMessageBox::questionTwoActions(this, 0415 i18n("Do you really want to delete the connection '%1'?", ui.lwConnections->currentItem()->text()), 0416 i18n("Delete Connection"), 0417 KStandardGuiItem::del(), 0418 KStandardGuiItem::cancel()); 0419 if (status != KMessageBox::PrimaryAction) 0420 return; 0421 #else 0422 auto status = KMessageBox::questionYesNo(this, 0423 i18n("Do you really want to delete the connection '%1'?", ui.lwConnections->currentItem()->text()), 0424 i18n("Delete Connection")); 0425 if (status != KMessageBox::Yes) 0426 return; 0427 #endif 0428 0429 // remove the current selected connection 0430 m_connections.removeAt(ui.lwConnections->currentRow()); 0431 m_initializing = true; 0432 delete ui.lwConnections->takeItem(ui.lwConnections->currentRow()); 0433 m_initializing = false; 0434 0435 // show the connection for the item that was automatically selected after the deletion 0436 connectionChanged(ui.lwConnections->currentRow()); 0437 0438 // disable widgets if there are no connections anymore 0439 if (!m_currentConnection) { 0440 m_initializing = true; 0441 ui.leName->clear(); 0442 ui.leName->setEnabled(false); 0443 ui.bRemove->setEnabled(false); 0444 ui.leHost->clear(); 0445 ui.leHost->setEnabled(false); 0446 ui.lePort->clear(); 0447 ui.lePort->setEnabled(false); 0448 ui.leUserName->clear(); 0449 ui.leUserName->setEnabled(false); 0450 ui.lePassword->clear(); 0451 ui.lePassword->setEnabled(false); 0452 ui.leID->clear(); 0453 ui.leID->setEnabled(false); 0454 m_initializing = false; 0455 } 0456 Q_EMIT changed(); 0457 } 0458 0459 /*! 0460 Loads the saved connections. 0461 */ 0462 void MQTTConnectionManagerWidget::loadConnections() { 0463 qDebug() << "Loading connections from " << m_configPath; 0464 0465 m_initializing = true; 0466 0467 KConfig config(m_configPath, KConfig::SimpleConfig); 0468 for (const auto& groupName : config.groupList()) { 0469 const KConfigGroup& group = config.group(groupName); 0470 MQTTConnection conn; 0471 conn.name = groupName; 0472 conn.hostName = group.readEntry("Host", ""); 0473 conn.port = group.readEntry("Port", 0); 0474 0475 conn.useAuthentication = group.readEntry("UseAuthentication", false); 0476 if (conn.useAuthentication) { 0477 conn.userName = group.readEntry("UserName", ""); 0478 conn.password = group.readEntry("Password", ""); 0479 } 0480 0481 conn.useID = group.readEntry("UseID", false); 0482 if (conn.useID) 0483 conn.clientID = group.readEntry("ClientID", ""); 0484 0485 conn.retain = group.readEntry("Retain", false); 0486 0487 m_connections.append(conn); 0488 ui.lwConnections->addItem(conn.name); 0489 } 0490 0491 // show the first connection if available, create a new connection otherwise 0492 if (!m_connections.empty()) { 0493 if (!m_initConnName.isEmpty()) { 0494 auto items = ui.lwConnections->findItems(m_initConnName, Qt::MatchExactly); 0495 if (items.empty()) 0496 ui.lwConnections->setCurrentRow(ui.lwConnections->count() - 1); 0497 else 0498 ui.lwConnections->setCurrentItem(items.constFirst()); 0499 } else { 0500 ui.lwConnections->setCurrentRow(ui.lwConnections->count() - 1); 0501 } 0502 } else { 0503 addConnection(); 0504 } 0505 0506 m_initializing = false; 0507 0508 // show the settings of the current connection 0509 connectionChanged(ui.lwConnections->currentRow()); 0510 } 0511 0512 /*! 0513 * \brief Saves the connections present in the list widget. 0514 */ 0515 void MQTTConnectionManagerWidget::saveConnections() { 0516 qDebug() << "Saving connections to " << m_configPath; 0517 // delete saved connections 0518 KConfig config(m_configPath, KConfig::SimpleConfig); 0519 for (const auto& group : config.groupList()) 0520 config.deleteGroup(group); 0521 0522 // save connections 0523 for (const auto& conn : m_connections) { 0524 KConfigGroup group = config.group(conn.name); 0525 group.writeEntry("Host", conn.hostName); 0526 group.writeEntry("Port", conn.port); 0527 group.writeEntry("UseAuthentication", QString::number(conn.useAuthentication)); 0528 group.writeEntry("UserName", conn.userName); 0529 group.writeEntry("Password", conn.password); 0530 group.writeEntry("UseID", QString::number(conn.useID)); 0531 group.writeEntry("ClientID", conn.clientID); 0532 group.writeEntry("Retain", QString::number(conn.retain)); 0533 } 0534 0535 config.sync(); 0536 } 0537 0538 /*! 0539 * \brief Checks whether every connection's settings are alright or not 0540 */ 0541 bool MQTTConnectionManagerWidget::checkConnections() { 0542 if (m_connections.isEmpty()) 0543 return true; 0544 0545 bool connectionsOk = true; 0546 0547 for (int i = 0; i < m_connections.size(); ++i) { 0548 auto& c1 = m_connections[i]; 0549 QList<QListWidgetItem*> equalNames = ui.lwConnections->findItems(c1.name, Qt::MatchExactly); 0550 bool nameOK = (!c1.name.isEmpty()) && (equalNames.size() == 1); 0551 0552 bool authenticationUsed = c1.useAuthentication; 0553 bool idUsed = c1.useID; 0554 bool authenticationFilled = !c1.userName.isEmpty() && !c1.password.isEmpty(); 0555 bool idFilled = !c1.clientID.isEmpty(); 0556 bool authenticationOK = (!authenticationUsed || authenticationFilled); 0557 bool idOK = (!idUsed || idFilled); 0558 0559 bool uniqueHost = true; 0560 for (int j = 0; j < m_connections.size(); ++j) { 0561 if (i == j) 0562 continue; 0563 auto& c2 = m_connections[j]; 0564 0565 if (c2.hostName == c1.hostName && c2.port == c1.port) { 0566 uniqueHost = false; 0567 break; 0568 } 0569 } 0570 bool hostOK = (!c1.hostName.isEmpty()) && uniqueHost; 0571 bool allOk = authenticationOK && idOK && hostOK && nameOK; 0572 0573 if (!allOk) { 0574 connectionsOk = false; 0575 SET_WARNING_BACKGROUND(ui.lwConnections->item(i)) 0576 } else 0577 ui.lwConnections->item(i)->setBackground(QBrush()); 0578 } 0579 return connectionsOk; 0580 } 0581 0582 /*! 0583 * \brief Provides a sample host name, which has to be changed before use. 0584 * \return 0585 */ 0586 QString MQTTConnectionManagerWidget::uniqueName() { 0587 QString name = i18n("New connection"); 0588 0589 QStringList connectionNames; 0590 for (int row = 0; row < ui.lwConnections->count(); row++) 0591 connectionNames << ui.lwConnections->item(row)->text(); 0592 0593 if (!connectionNames.contains(name)) 0594 return name; 0595 0596 QString base = name; 0597 int lastNonDigit; 0598 for (lastNonDigit = base.size() - 1; lastNonDigit >= 0 && base[lastNonDigit].category() == QChar::Number_DecimalDigit; --lastNonDigit) 0599 base.chop(1); 0600 0601 if (lastNonDigit >= 0 && base[lastNonDigit].category() != QChar::Separator_Space) 0602 base.append(QLatin1Char(' ')); 0603 0604 int newNr = name.right(name.size() - base.size()).toInt(); 0605 QString newName; 0606 do 0607 newName = base + QString::number(++newNr); 0608 while (connectionNames.contains(newName)); 0609 0610 return newName; 0611 } 0612 0613 /*! 0614 * \brief Tests the currently selected connection 0615 */ 0616 void MQTTConnectionManagerWidget::testConnection() { 0617 if (!m_currentConnection) 0618 return; 0619 0620 WAIT_CURSOR; 0621 0622 m_testing = true; 0623 0624 if (!m_client) { 0625 m_client = new QMqttClient; 0626 m_testTimer = new QTimer(this); 0627 m_testTimer->setInterval(5000); 0628 connect(m_client, &QMqttClient::connected, this, &MQTTConnectionManagerWidget::onConnect); 0629 connect(m_client, &QMqttClient::disconnected, this, &MQTTConnectionManagerWidget::onDisconnect); 0630 connect(m_testTimer, &QTimer::timeout, this, &MQTTConnectionManagerWidget::testTimeout); 0631 } 0632 0633 m_client->setHostname(m_currentConnection->hostName); 0634 m_client->setPort(m_currentConnection->port); 0635 0636 if (m_currentConnection->useID) 0637 m_client->setClientId(m_currentConnection->clientID); 0638 0639 if (m_currentConnection->useAuthentication) { 0640 m_client->setUsername(m_currentConnection->userName); 0641 m_client->setPassword(m_currentConnection->password); 0642 } 0643 0644 m_testTimer->start(); 0645 m_client->connectToHost(); 0646 } 0647 0648 /*! 0649 * \brief Called when the client connects to the host, this means the test was successful 0650 */ 0651 void MQTTConnectionManagerWidget::onConnect() { 0652 RESET_CURSOR; 0653 m_testTimer->stop(); 0654 0655 KMessageBox::information(this, 0656 i18n("Connection to the broker '%1:%2' was successful.", m_currentConnection->hostName, m_currentConnection->port), 0657 i18n("Connection Successful")); 0658 0659 m_client->disconnectFromHost(); 0660 } 0661 0662 /*! 0663 * \brief Called when testTimer times out, this means that the test wasn't successful 0664 */ 0665 void MQTTConnectionManagerWidget::testTimeout() { 0666 RESET_CURSOR; 0667 m_testTimer->stop(); 0668 0669 KMessageBox::error(this, 0670 i18n("Failed to connect to the broker '%1:%2'.", m_currentConnection->hostName, m_currentConnection->port), 0671 i18n("Connection Failed")); 0672 0673 m_client->disconnectFromHost(); 0674 } 0675 0676 /*! 0677 * \brief Called when the client disconnects from the host 0678 */ 0679 void MQTTConnectionManagerWidget::onDisconnect() { 0680 RESET_CURSOR; 0681 if (m_testTimer->isActive()) { 0682 KMessageBox::error( 0683 this, 0684 i18n("Disconnected from the broker '%1:%2' before the connection was successful.", m_currentConnection->hostName, m_currentConnection->port), 0685 i18n("Connection Failed")); 0686 m_testTimer->stop(); 0687 } 0688 }