Modernize helpmessage sender utility

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

2
.gitignore vendored

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

@ -3,7 +3,7 @@ project(Labcontrol CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
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 "ui_helpmessagewindow.h"
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QSettings>
#include <QTcpSocket>
#include <iostream>
lcHelpMessageWindow::lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort,
QWidget *argParent) :
const quint16 argServerPort,
QWidget *const argParent) :
QMainWindow{argParent},
helpMessageSocket {new QTcpSocket{this}},
serverPort{argServerPort},
serverAddress{argServerIP},
serverPort{argServerPort},
ui{new Ui::HelpMessageWindow}
{
helpMessageSocket = new QTcpSocket{this};
ui->setupUi(this);
connect(ui->PBAskForHelp, &QPushButton::clicked,
this, &lcHelpMessageWindow::RequestHelp);
connect(helpMessageSocket, &QTcpSocket::readyRead,
this, &lcHelpMessageWindow::ReadHelpReply);
connect(helpMessageSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(DisplayError(QAbstractSocket::SocketError)));
connect(helpMessageSocket,
static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>
(&QAbstractSocket::error),
this, &lcHelpMessageWindow::DisplayError);
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
if (manager.capabilities()
& QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"});
const QString id
= settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString();
const auto id{settings.value("DefaultNetworkConfiguration").toString()};
settings.endGroup();
// 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)
!= QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
@ -47,9 +75,9 @@ lcHelpMessageWindow::~lcHelpMessageWindow() {
delete ui;
}
void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError socketError) {
void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError argSocketError) {
QString errorMessage;
switch (socketError) {
switch (argSocketError) {
case QAbstractSocket::RemoteHostClosedError:
return;
case QAbstractSocket::HostNotFoundError:
@ -70,9 +98,9 @@ void lcHelpMessageWindow::DisplayError(QAbstractSocket::SocketError socketError)
void lcHelpMessageWindow::OpenedSession() {
// Save the used configuration
QNetworkConfiguration config = networkSession->configuration();
QString id;
if (config.type() == QNetworkConfiguration::UserChoice) {
if (const auto config{networkSession->configuration()};
config.type() == QNetworkConfiguration::UserChoice) {
id = networkSession->sessionProperty(
QLatin1String{"UserChoiceConfiguration"}).toString();
} else {
@ -86,11 +114,12 @@ void lcHelpMessageWindow::OpenedSession() {
}
void lcHelpMessageWindow::ReadHelpReply() {
QDataStream in(helpMessageSocket);
QDataStream in{helpMessageSocket};
in.setVersion(QDataStream::Qt_5_2);
if (blockSize == 0) {
if (helpMessageSocket->bytesAvailable() < (int)sizeof(quint16)) {
if (helpMessageSocket->bytesAvailable()
< static_cast<int>(sizeof(quint16))) {
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
#define HELPMESSAGEWINDOW_H
#include "ui_helpmessagewindow.h"
#include <QHostAddress>
#include <QMainWindow>
#include <QMessageBox>
#include <QtNetwork>
class QNetworkSession;
class QTcpSocket;
namespace Ui {
class HelpMessageWindow;
@ -17,23 +36,23 @@ class lcHelpMessageWindow : public QMainWindow
public:
explicit lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort,
QWidget *argParent = nullptr);
~lcHelpMessageWindow();
const quint16 argServerPort,
QWidget *const argParent = nullptr);
~lcHelpMessageWindow() override;
private:
quint16 blockSize = 0;
QTcpSocket *helpMessageSocket = nullptr;
QNetworkSession *networkSession = nullptr;
const quint16 serverPort = 0;
const QHostAddress serverAddress;
Ui::HelpMessageWindow *ui;
const quint16 serverPort = 0;
Ui::HelpMessageWindow *const ui = nullptr;
private slots:
void RequestHelp();
void ReadHelpReply();
void DisplayError(QAbstractSocket::SocketError socketError);
void DisplayError(QAbstractSocket::SocketError argSocketError);
void OpenedSession();
void ReadHelpReply();
void RequestHelp();
};
#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 <QApplication>
#include <QSettings>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication app{argc, argv};
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(),
labSettings.value("client_help_server_port", "0").toUInt()};
portNum};
w.show();
return a.exec();
return app.exec();
}

Loading…
Cancel
Save