Modernize helpmessage sender utility

remotes/origin/HEAD
markuspg 6 years ago
parent bc345ff4d3
commit e8b31b5cca

2
.gitignore vendored

@ -31,3 +31,5 @@
# Qt Creator User Project Files # Qt Creator User Project Files
*.pro.user *.pro.user
*.eccba64 *.eccba64
/CMakeLists.txt.user*

@ -3,7 +3,7 @@ project(Labcontrol CXX)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
find_package(Qt5 COMPONENTS Core Network Widgets REQUIRED) find_package(Qt5 COMPONENTS Core Network Widgets REQUIRED)

@ -1,35 +1,63 @@
/*
* Copyright 2018 Markus Prasser
*
* This file is part of Labcontrol.
*
* Labcontrol 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 3 of the License, or
* (at your option) any later version.
*
* Labcontrol 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 Labcontrol. If not, see <http://www.gnu.org/licenses/>.
*/
#include "helpmessagewindow.h" #include "helpmessagewindow.h"
#include "ui_helpmessagewindow.h"
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QSettings>
#include <QTcpSocket>
#include <iostream> #include <iostream>
lcHelpMessageWindow::lcHelpMessageWindow(const QString &argServerIP, lcHelpMessageWindow::lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort, const quint16 argServerPort,
QWidget *argParent) : QWidget *const argParent) :
QMainWindow{argParent}, QMainWindow{argParent},
helpMessageSocket {new QTcpSocket{this}},
serverPort{argServerPort},
serverAddress{argServerIP}, serverAddress{argServerIP},
serverPort{argServerPort},
ui{new Ui::HelpMessageWindow} ui{new Ui::HelpMessageWindow}
{ {
helpMessageSocket = new QTcpSocket{this};
ui->setupUi(this); ui->setupUi(this);
connect(ui->PBAskForHelp, &QPushButton::clicked, connect(ui->PBAskForHelp, &QPushButton::clicked,
this, &lcHelpMessageWindow::RequestHelp); this, &lcHelpMessageWindow::RequestHelp);
connect(helpMessageSocket, &QTcpSocket::readyRead, connect(helpMessageSocket, &QTcpSocket::readyRead,
this, &lcHelpMessageWindow::ReadHelpReply); this, &lcHelpMessageWindow::ReadHelpReply);
connect(helpMessageSocket, SIGNAL(error(QAbstractSocket::SocketError)), connect(helpMessageSocket,
this, SLOT(DisplayError(QAbstractSocket::SocketError))); static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>
(&QAbstractSocket::error),
this, &lcHelpMessageWindow::DisplayError);
QNetworkConfigurationManager manager; QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) { if (manager.capabilities()
& QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration // Get saved network configuration
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"}); settings.beginGroup(QLatin1String{"QtNetwork"});
const QString id const auto id{settings.value("DefaultNetworkConfiguration").toString()};
= settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString();
settings.endGroup(); settings.endGroup();
// If the saved network configuration is not currently discovered use the system default // If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier( id ); QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) if ((config.state() & QNetworkConfiguration::Discovered)
!= QNetworkConfiguration::Discovered) { != QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration(); config = manager.defaultConfiguration();
@ -47,9 +75,9 @@ lcHelpMessageWindow::~lcHelpMessageWindow() {
delete ui; delete ui;
} }
void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError socketError) { void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError argSocketError) {
QString errorMessage; QString errorMessage;
switch (socketError) { switch (argSocketError) {
case QAbstractSocket::RemoteHostClosedError: case QAbstractSocket::RemoteHostClosedError:
return; return;
case QAbstractSocket::HostNotFoundError: case QAbstractSocket::HostNotFoundError:
@ -70,9 +98,9 @@ void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError socketError)
void lcHelpMessageWindow::OpenedSession() { void lcHelpMessageWindow::OpenedSession() {
// Save the used configuration // Save the used configuration
QNetworkConfiguration config = networkSession->configuration();
QString id; QString id;
if (config.type() == QNetworkConfiguration::UserChoice) { if (const auto config{networkSession->configuration()};
config.type() == QNetworkConfiguration::UserChoice) {
id = networkSession->sessionProperty( id = networkSession->sessionProperty(
QLatin1String{"UserChoiceConfiguration"}).toString(); QLatin1String{"UserChoiceConfiguration"}).toString();
} else { } else {
@ -86,11 +114,12 @@ void lcHelpMessageWindow::OpenedSession() {
} }
void lcHelpMessageWindow::ReadHelpReply() { void lcHelpMessageWindow::ReadHelpReply() {
QDataStream in(helpMessageSocket); QDataStream in{helpMessageSocket};
in.setVersion(QDataStream::Qt_5_2); in.setVersion(QDataStream::Qt_5_2);
if (blockSize == 0) { if (blockSize == 0) {
if (helpMessageSocket->bytesAvailable() < (int)sizeof(quint16)) { if (helpMessageSocket->bytesAvailable()
< static_cast<int>(sizeof(quint16))) {
return; return;
} }

@ -1,11 +1,30 @@
/*
* Copyright 2018 Markus Prasser
*
* This file is part of Labcontrol.
*
* Labcontrol 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 3 of the License, or
* (at your option) any later version.
*
* Labcontrol 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 Labcontrol. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HELPMESSAGEWINDOW_H #ifndef HELPMESSAGEWINDOW_H
#define HELPMESSAGEWINDOW_H #define HELPMESSAGEWINDOW_H
#include "ui_helpmessagewindow.h" #include <QHostAddress>
#include <QMainWindow> #include <QMainWindow>
#include <QMessageBox>
#include <QtNetwork> class QNetworkSession;
class QTcpSocket;
namespace Ui { namespace Ui {
class HelpMessageWindow; class HelpMessageWindow;
@ -17,23 +36,23 @@ class lcHelpMessageWindow : public QMainWindow
public: public:
explicit lcHelpMessageWindow(const QString &argServerIP, explicit lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort, const quint16 argServerPort,
QWidget *argParent = nullptr); QWidget *const argParent = nullptr);
~lcHelpMessageWindow(); ~lcHelpMessageWindow() override;
private: private:
quint16 blockSize = 0; quint16 blockSize = 0;
QTcpSocket *helpMessageSocket = nullptr; QTcpSocket *helpMessageSocket = nullptr;
QNetworkSession *networkSession = nullptr; QNetworkSession *networkSession = nullptr;
const quint16 serverPort = 0;
const QHostAddress serverAddress; const QHostAddress serverAddress;
Ui::HelpMessageWindow *ui; const quint16 serverPort = 0;
Ui::HelpMessageWindow *const ui = nullptr;
private slots: private slots:
void RequestHelp(); void DisplayError(QAbstractSocket::SocketError argSocketError);
void ReadHelpReply();
void DisplayError(QAbstractSocket::SocketError socketError);
void OpenedSession(); void OpenedSession();
void ReadHelpReply();
void RequestHelp();
}; };
#endif // HELPMESSAGEWINDOW_H #endif // HELPMESSAGEWINDOW_H

@ -1,14 +1,47 @@
/*
* Copyright 2018 Markus Prasser
*
* This file is part of Labcontrol.
*
* Labcontrol 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 3 of the License, or
* (at your option) any later version.
*
* Labcontrol 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 Labcontrol. If not, see <http://www.gnu.org/licenses/>.
*/
#include "helpmessagewindow.h" #include "helpmessagewindow.h"
#include <QApplication> #include <QApplication>
#include <QSettings>
#include <iostream>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication app{argc, argv};
QSettings labSettings{"Economic Laboratory", "Labcontrol"}; QSettings labSettings{"Economic Laboratory", "Labcontrol"};
bool convSuccess = false;
const auto portNum{
static_cast<quint16>(labSettings.value("client_help_server_port", "0")
.toUInt(&convSuccess))};
if (convSuccess == false) {
std::cerr << "Failed to convert \"client_help_server_port\" setting\n";
return 1;
}
lcHelpMessageWindow w{labSettings.value("server_ip", "127.0.0.1").toString(), lcHelpMessageWindow w{labSettings.value("server_ip", "127.0.0.1").toString(),
labSettings.value("client_help_server_port", "0").toUInt()}; portNum};
w.show(); w.show();
return a.exec(); return app.exec();
} }

Loading…
Cancel
Save