diff --git a/.gitignore b/.gitignore index cb075c3..01ce015 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ ui_* Makefile* *.pro.user *.stash +*.Release +*.Debug diff --git a/MQTTCute.pro b/MQTTCute.pro index 03357af..ba91533 100644 --- a/MQTTCute.pro +++ b/MQTTCute.pro @@ -85,4 +85,4 @@ win32-msvc:CONFIG(debug, debug|release): LIBS += -L$$PWD/mosquitto/msvc_2017_x64 RESOURCES += \ images.qrc -win32:RC_ICONS += img/mqtticon-large.png +#win32:RC_ICONS += img/mqtticon-large.png diff --git a/mainwindow.cpp b/mainwindow.cpp index 5aae61d..9003f11 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -164,6 +164,28 @@ void MainWindow::connectRemote() { return; } } + + if (usingDefaultSession && defaultSession.loginType == LOGIN_TYPE_PASSWORD) { + if (!mqtt->setPassword(defaultSession.username, defaultSession.password)) { + cerr << "Setting username/password failed.\n"; + QMessageBox::critical(this, tr("Setting username/password failed"), + tr("Setting the username and/or password failed.\n\n\ + Please check the provided info and try again.")); + + return; + } + + } + else if (loadedSession.loginType == LOGIN_TYPE_PASSWORD) { + if (!mqtt->setPassword(loadedSession.username, loadedSession.password)) { + cerr << "Setting username/password failed.\n"; + QMessageBox::critical(this, tr("Setting username/password failed"), + tr("Setting the username and/or password failed.\n\n\ + Please check the provided info and try again.")); + + return; + } + } // Try to connect. If successful, update local copy and stored remote server address. // The MQTT client class has to run on its own thread since it uses its own event loop. @@ -216,6 +238,11 @@ void MainWindow::remoteConnected() { void MainWindow::errorHandler(QString err) { QMessageBox::warning(this, tr("Error"), err); connected = false; + + // Reset UI elements. + ui->actionConnect->setEnabled(true); + ui->actionDisconnect->setEnabled(false); + ui->mainToolBar->setDisabled(true); } diff --git a/mosquitto/lib/mosquitto.c b/mosquitto/lib/mosquitto.c index d8946c5..454f6c8 100644 --- a/mosquitto/lib/mosquitto.c +++ b/mosquitto/lib/mosquitto.c @@ -26,7 +26,7 @@ and the Eclipse Distribution License is available at #else #include #include -typedef int ssize_t; +//typedef int ssize_t; #endif #include "mosquitto.h" diff --git a/mosquitto/lib/net_mosq.h b/mosquitto/lib/net_mosq.h index 4768060..8805be0 100644 --- a/mosquitto/lib/net_mosq.h +++ b/mosquitto/lib/net_mosq.h @@ -20,7 +20,7 @@ and the Eclipse Distribution License is available at #include #else #include -typedef int ssize_t; +//typedef int ssize_t; #endif #include "mosquitto_internal.h" diff --git a/mqttlistener.cpp b/mqttlistener.cpp index 46e6820..67d7ab9 100644 --- a/mqttlistener.cpp +++ b/mqttlistener.cpp @@ -51,9 +51,31 @@ bool MqttListener::setTLS(string &ca, string &cert, string &key) { } +// --- SET PASSWORD --- +// Set the username and password for this MQTT connection. +bool MqttListener::setPassword(string &username, string &password) { + int res = username_pw_set(username.c_str(), password.c_str()); + if (res != MOSQ_ERR_SUCCESS) { + if (res == MOSQ_ERR_INVAL) { + cerr << "Invalid input parameters for Mosquitto PW.\n"; + } + else if (res == MOSQ_ERR_NOMEM) { + cerr << "Out of memory on Mosquitto PW.\n"; + } + else { + cerr << "Unknown Mosquitto PW error.\n"; + } + + return false; + } + + return true; +} + + // --- CONNECT BROKER --- void MqttListener::connectBroker() { - cout << "Connecting to broker..." << host.toStdString() << ":" << port << "\n"; + cout << "Connecting to broker: " << host.toStdString() << ":" << port << "\n"; int keepalive = 60; mosqpp::mosquittopp::connect(host.toStdString().c_str(), port, keepalive); diff --git a/mqttlistener.h b/mqttlistener.h index 462a7d1..8bada50 100644 --- a/mqttlistener.h +++ b/mqttlistener.h @@ -40,6 +40,7 @@ class MqttListener : public QObject, mosqpp::mosquittopp { ~MqttListener(); bool setTLS(string &ca, string &cert, string &key); + bool setPassword(string &username, string &password); void on_connect(int rc); void on_message(const struct mosquitto_message* message); void on_subscribe(int mid, int qos_count, const int* granted_qos);