From d839f7f227f4aa15c9b6d81574567bf6c3ea7625 Mon Sep 17 00:00:00 2001 From: markuspg Date: Mon, 12 Mar 2018 22:37:07 +0100 Subject: [PATCH] Reformat ClientHelpNotificationServer --- src/Lib/clienthelpnotificationserver.cpp | 106 +++++++++++++---------- src/Lib/clienthelpnotificationserver.h | 22 ++--- 2 files changed, 70 insertions(+), 58 deletions(-) mode change 100755 => 100644 src/Lib/clienthelpnotificationserver.cpp mode change 100755 => 100644 src/Lib/clienthelpnotificationserver.h diff --git a/src/Lib/clienthelpnotificationserver.cpp b/src/Lib/clienthelpnotificationserver.cpp old mode 100755 new mode 100644 index 40938c0..4b4f8b3 --- a/src/Lib/clienthelpnotificationserver.cpp +++ b/src/Lib/clienthelpnotificationserver.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 Markus Prasser + * Copyright 2014-2018 Markus Prasser, Tobias Weiss * * This file is part of Labcontrol. * @@ -17,98 +17,114 @@ * along with Labcontrol. If not, see . */ -#include - #include "clienthelpnotificationserver.h" #include "settings.h" -extern std::unique_ptr< lc::Settings > settings; +#include +#include +#include +#include +#include + +#include -lc::ClientHelpNotificationServer::ClientHelpNotificationServer( QObject *argParent ) : - QObject{ argParent }, - hostAddress{ settings->serverIP } +extern std::unique_ptr settings; + +lc::ClientHelpNotificationServer::ClientHelpNotificationServer(QObject *argParent) : + QObject{argParent}, + 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(); + QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; + settings.beginGroup(QLatin1String{"QtNetwork"}); + 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 }; - connect( networkSession, &QNetworkSession::opened, - this, &ClientHelpNotificationServer::OpenSession ); + networkSession = new QNetworkSession{config, this}; + connect(networkSession, &QNetworkSession::opened, + this, &ClientHelpNotificationServer::OpenSession); networkSession->open(); } else { OpenSession(); } - connect( helpMessageServer, &QTcpServer::newConnection, - this, &ClientHelpNotificationServer::SendReply ); + connect(helpMessageServer, &QTcpServer::newConnection, + this, &ClientHelpNotificationServer::SendReply); } -void lc::ClientHelpNotificationServer::OpenSession() { +void lc::ClientHelpNotificationServer::OpenSession() +{ // Save the used configuration - if ( networkSession ) { - QNetworkConfiguration config = networkSession->configuration(); + if (networkSession) { + QNetworkConfiguration config{networkSession->configuration()}; QString id; - if ( config.type() == QNetworkConfiguration::UserChoice ) { - id = networkSession->sessionProperty( QLatin1String{ "UserChoiceConfiguration" } ).toString(); + if (config.type() == QNetworkConfiguration::UserChoice) { + id = networkSession->sessionProperty(QLatin1String{"UserChoiceConfiguration"}).toString(); } else { id = config.identifier(); } - QSettings settings( QSettings::UserScope, QLatin1String{ "QtProject" } ); - settings.beginGroup( QLatin1String{ "QtNetwork" } ); - settings.setValue( QLatin1String{ "DefaultNetworkConfiguration" }, id ); + QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; + settings.beginGroup(QLatin1String{"QtNetwork"}); + settings.setValue(QLatin1String{"DefaultNetworkConfiguration"}, id); settings.endGroup(); } - helpMessageServer = new QTcpServer{ this }; + helpMessageServer = new QTcpServer{this}; if ( !helpMessageServer->listen( hostAddress, settings->clientHelpNotificationServerPort ) ) { QMessageBox messageBox{ QMessageBox::Critical, tr( "Unable to start the client help notification server" ), - tr( "Unable to start the client help notification server.\nThe following error occurred:\n\n%1." ).arg( helpMessageServer->errorString() ), QMessageBox::Ok }; + tr( "Unable to start the client help notification server.\nThe following error occurred:\n\n%1." ).arg( helpMessageServer->errorString() ), QMessageBox::Ok }; messageBox.exec(); return; } } -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 << QString{ "Help demand retrieved." }; - out.device()->seek( 0 ); - out << ( quint16 )( block.size() - sizeof( quint16 ) ); + QDataStream out{&block, QIODevice::WriteOnly}; + out.setVersion(QDataStream::Qt_5_2); + out << static_cast(0); + out << QString{"Help demand retrieved."}; + out.device()->seek(0); + out << static_cast(block.size() - sizeof(quint16)); - QTcpSocket *clientConnection = helpMessageServer->nextPendingConnection(); + auto clientConnection = helpMessageServer->nextPendingConnection(); QString peerAddress = clientConnection->peerAddress().toString(); QString peerName; bool unknownClient = false; - if ( settings->clIPsToClMap.contains( peerAddress ) ) { - peerName = settings->clIPsToClMap[ peerAddress ]->name; + if (settings->clIPsToClMap.contains(peerAddress)) { + peerName = settings->clIPsToClMap[peerAddress]->name; } else { unknownClient = true; } - connect( clientConnection, &QTcpSocket::disconnected, clientConnection, &QTcpSocket::deleteLater ); - clientConnection->write( block ); + 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 }; + 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}; 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(); } } diff --git a/src/Lib/clienthelpnotificationserver.h b/src/Lib/clienthelpnotificationserver.h old mode 100755 new mode 100644 index d46e9d7..c1d704c --- a/src/Lib/clienthelpnotificationserver.h +++ b/src/Lib/clienthelpnotificationserver.h @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 Markus Prasser + * Copyright 2014-2018 Markus Prasser, Tobias Weiss * * This file is part of Labcontrol. * @@ -20,23 +20,19 @@ #ifndef CLIENTHELPNOTIFICATIONSERVER_H #define CLIENTHELPNOTIFICATIONSERVER_H -#include "src/Lib/client.h" - -#include #include -#include -#include + +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: +public: + explicit ClientHelpNotificationServer(QObject *argParent = nullptr); private: QTcpServer *helpMessageServer = nullptr; @@ -48,6 +44,6 @@ private slots: void SendReply(); }; -} +} // namespace lc #endif // CLIENTHELPNOTIFICATIONSERVER_H