Reformat ClientHelpNotificationServer

remotes/origin/HEAD
markuspg 7 years ago
parent e533ede28b
commit d839f7f227

@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Markus Prasser
* Copyright 2014-2018 Markus Prasser, Tobias Weiss
*
* This file is part of Labcontrol.
*
@ -17,11 +17,17 @@
* along with Labcontrol. If not, see <http://www.gnu.org/licenses/>.
*/
#include <memory>
#include "clienthelpnotificationserver.h"
#include "settings.h"
#include <QMessageBox>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QTcpServer>
#include <QTcpSocket>
#include <memory>
extern std::unique_ptr<lc::Settings> settings;
lc::ClientHelpNotificationServer::ClientHelpNotificationServer(QObject *argParent) :
@ -29,16 +35,18 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argPare
hostAddress{settings->serverIP}
{
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 QString id{settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString()};
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier( id );
if ( ( config.state() & QNetworkConfiguration::Discovered ) != QNetworkConfiguration::Discovered ) {
QNetworkConfiguration config{manager.configurationFromIdentifier(id)};
if ((config.state() & QNetworkConfiguration::Discovered)
!= QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession{config, this};
@ -53,10 +61,11 @@ lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argPare
this, &ClientHelpNotificationServer::SendReply);
}
void lc::ClientHelpNotificationServer::OpenSession() {
void lc::ClientHelpNotificationServer::OpenSession()
{
// Save the used configuration
if (networkSession) {
QNetworkConfiguration config = networkSession->configuration();
QNetworkConfiguration config{networkSession->configuration()};
QString id;
if (config.type() == QNetworkConfiguration::UserChoice) {
id = networkSession->sessionProperty(QLatin1String{"UserChoiceConfiguration"}).toString();
@ -64,7 +73,7 @@ void lc::ClientHelpNotificationServer::OpenSession() {
id = config.identifier();
}
QSettings settings( QSettings::UserScope, QLatin1String{ "QtProject" } );
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"});
settings.setValue(QLatin1String{"DefaultNetworkConfiguration"}, id);
settings.endGroup();
@ -79,16 +88,17 @@ void lc::ClientHelpNotificationServer::OpenSession() {
}
}
void lc::ClientHelpNotificationServer::SendReply() {
void lc::ClientHelpNotificationServer::SendReply()
{
QByteArray block;
QDataStream out{&block, QIODevice::WriteOnly};
out.setVersion(QDataStream::Qt_5_2);
out << ( quint16 )0;
out << static_cast<quint16>(0);
out << QString{"Help demand retrieved."};
out.device()->seek(0);
out << ( quint16 )( block.size() - sizeof( quint16 ) );
out << static_cast<quint16>(block.size() - sizeof(quint16));
QTcpSocket *clientConnection = helpMessageServer->nextPendingConnection();
auto clientConnection = helpMessageServer->nextPendingConnection();
QString peerAddress = clientConnection->peerAddress().toString();
QString peerName;
bool unknownClient = false;
@ -98,17 +108,23 @@ void lc::ClientHelpNotificationServer::SendReply() {
unknownClient = true;
}
connect( clientConnection, &QTcpSocket::disconnected, clientConnection, &QTcpSocket::deleteLater );
connect(clientConnection, &QTcpSocket::disconnected,
clientConnection, &QTcpSocket::deleteLater);
clientConnection->write(block);
clientConnection->disconnectFromHost();
if (unknownClient) {
QMessageBox requestReceivedBox{ QMessageBox::Information, tr( "Unknown client asked for help."),
tr( "An unknown client with IP '%1' asked for help.").arg( peerAddress ), QMessageBox::Ok };
QMessageBox requestReceivedBox{QMessageBox::Information,
tr("Unknown client asked for help."),
tr("An unknown client with IP '%1' asked"
" for help.").arg(peerAddress),
QMessageBox::Ok};
requestReceivedBox.exec();
} else {
QMessageBox requestReceivedBox{ QMessageBox::Information, tr( "'%1' asked for help.").arg( peerName ),
tr( "'%1' asked for help.").arg( peerName ), QMessageBox::Ok };
QMessageBox requestReceivedBox{QMessageBox::Information,
tr("'%1' asked for help.").arg( peerName ),
tr("'%1' asked for help.").arg(peerName),
QMessageBox::Ok};
requestReceivedBox.exec();
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Markus Prasser
* Copyright 2014-2018 Markus Prasser, Tobias Weiss
*
* This file is part of Labcontrol.
*
@ -20,24 +20,20 @@
#ifndef CLIENTHELPNOTIFICATIONSERVER_H
#define CLIENTHELPNOTIFICATIONSERVER_H
#include "src/Lib/client.h"
#include <QObject>
#include <QHostAddress>
#include <QMessageBox>
#include <QtNetwork>
class QNetworkSession;
class QTcpServer;
namespace lc {
class ClientHelpNotificationServer : public QObject {
class ClientHelpNotificationServer : public QObject
{
Q_OBJECT
public:
explicit ClientHelpNotificationServer(QObject *argParent = nullptr);
signals:
public slots:
private:
QTcpServer *helpMessageServer = nullptr;
const QHostAddress hostAddress;
@ -48,6 +44,6 @@ private slots:
void SendReply();
};
}
} // namespace lc
#endif // CLIENTHELPNOTIFICATIONSERVER_H

Loading…
Cancel
Save