modernize: Reformat ALL the source... again!
[quassel.git] / src / client / clientauthhandler.cpp
index 668c4a4..a5bf291 100644 (file)
@@ -23,9 +23,9 @@
 #include <QtEndian>
 
 #ifdef HAVE_SSL
-    #include <QSslSocket>
+#    include <QSslSocket>
 #else
-    #include <QTcpSocket>
+#    include <QTcpSocket>
 #endif
 
 #include "client.h"
 
 using namespace Protocol;
 
-ClientAuthHandler::ClientAuthHandler(CoreAccount account, QObject *parent)
-    : AuthHandler(parent),
-    _peer(nullptr),
-    _account(account),
-    _probing(false),
-    _legacy(false),
-    _connectionFeatures(0)
-{
-
-}
+ClientAuthHandler::ClientAuthHandler(CoreAccount account, QObject* parent)
+    : AuthHandler(parent)
+    , _peer(nullptr)
+    , _account(account)
+    , _probing(false)
+    , _legacy(false)
+    , _connectionFeatures(0)
+{}
 
-
-Peer *ClientAuthHandler::peer() const
+Peer* ClientAuthHandler::peer() const
 {
     return _peer;
 }
 
-
 void ClientAuthHandler::connectToCore()
 {
     CoreAccountSettings s;
 
 #ifdef HAVE_SSL
-    auto *socket = new QSslSocket(this);
+    autosocket = new QSslSocket(this);
     // make sure the warning is shown if we happen to connect without SSL support later
     s.setAccountValue("ShowNoClientSslWarning", true);
 #else
@@ -74,14 +70,13 @@ void ClientAuthHandler::connectToCore()
             s.setAccountValue("ShowNoClientSslWarning", false);
         }
     }
-    QTcpSocket *socket = new QTcpSocket(this);
+    QTcpSocketsocket = new QTcpSocket(this);
 #endif
 
 #ifndef QT_NO_NETWORKPROXY
     QNetworkProxy proxy;
     proxy.setType(_account.proxyType());
-    if (_account.proxyType() == QNetworkProxy::Socks5Proxy ||
-            _account.proxyType() == QNetworkProxy::HttpProxy) {
+    if (_account.proxyType() == QNetworkProxy::Socks5Proxy || _account.proxyType() == QNetworkProxy::HttpProxy) {
         proxy.setHostName(_account.proxyHostName());
         proxy.setPort(_account.proxyPort());
         proxy.setUser(_account.proxyUser());
@@ -90,7 +85,8 @@ void ClientAuthHandler::connectToCore()
 
     if (_account.proxyType() == QNetworkProxy::DefaultProxy) {
         QNetworkProxyFactory::setUseSystemConfiguration(true);
-    } else {
+    }
+    else {
         QNetworkProxyFactory::setUseSystemConfiguration(false);
         socket->setProxy(proxy);
     }
@@ -105,39 +101,38 @@ void ClientAuthHandler::connectToCore()
     socket->connectToHost(_account.hostName(), _account.port());
 }
 
-
 void ClientAuthHandler::onSocketStateChanged(QAbstractSocket::SocketState socketState)
 {
     QString text;
 
-    switch(socketState) {
-        case QAbstractSocket::HostLookupState:
-            if (!_legacy)
-                text = tr("Looking up %1...").arg(_account.hostName());
-            break;
-        case QAbstractSocket::ConnectingState:
-            if (!_legacy)
-                text = tr("Connecting to %1...").arg(_account.hostName());
-            break;
-        case QAbstractSocket::ConnectedState:
-            text = tr("Connected to %1").arg(_account.hostName());
-            break;
-        case QAbstractSocket::ClosingState:
-            if (!_probing)
-                text = tr("Disconnecting from %1...").arg(_account.hostName());
-            break;
-        case QAbstractSocket::UnconnectedState:
-            if (!_probing) {
-                text = tr("Disconnected");
-                // Ensure the disconnected() signal is sent even if we haven't reached the Connected state yet.
-                // The baseclass implementation will make sure to only send the signal once.
-                // However, we do want to prefer a potential socket error signal that may be on route already, so
-                // give this a chance to overtake us by spinning the loop...
-                QTimer::singleShot(0, this, &ClientAuthHandler::onSocketDisconnected);
-            }
-            break;
-        default:
-            break;
+    switch (socketState) {
+    case QAbstractSocket::HostLookupState:
+        if (!_legacy)
+            text = tr("Looking up %1...").arg(_account.hostName());
+        break;
+    case QAbstractSocket::ConnectingState:
+        if (!_legacy)
+            text = tr("Connecting to %1...").arg(_account.hostName());
+        break;
+    case QAbstractSocket::ConnectedState:
+        text = tr("Connected to %1").arg(_account.hostName());
+        break;
+    case QAbstractSocket::ClosingState:
+        if (!_probing)
+            text = tr("Disconnecting from %1...").arg(_account.hostName());
+        break;
+    case QAbstractSocket::UnconnectedState:
+        if (!_probing) {
+            text = tr("Disconnected");
+            // Ensure the disconnected() signal is sent even if we haven't reached the Connected state yet.
+            // The baseclass implementation will make sure to only send the signal once.
+            // However, we do want to prefer a potential socket error signal that may be on route already, so
+            // give this a chance to overtake us by spinning the loop...
+            QTimer::singleShot(0, this, &ClientAuthHandler::onSocketDisconnected);
+        }
+        break;
+    default:
+        break;
     }
 
     if (!text.isEmpty()) {
@@ -152,11 +147,10 @@ void ClientAuthHandler::onSocketError(QAbstractSocket::SocketError error)
         return;
     }
 
-    _probing = false; // all other errors are unrelated to probing and should be handled
+    _probing = false;  // all other errors are unrelated to probing and should be handled
     AuthHandler::onSocketError(error);
 }
 
-
 void ClientAuthHandler::onSocketDisconnected()
 {
     if (_probing && _legacy) {
@@ -171,7 +165,6 @@ void ClientAuthHandler::onSocketDisconnected()
     AuthHandler::onSocketDisconnected();
 }
 
-
 void ClientAuthHandler::onSocketConnected()
 {
     if (_peer) {
@@ -185,7 +178,7 @@ void ClientAuthHandler::onSocketConnected()
         // First connection attempt, try probing for a capable core
         _probing = true;
 
-        QDataStream stream(socket()); // stream handles the endianness for us
+        QDataStream stream(socket());  // stream handles the endianness for us
         stream.setVersion(QDataStream::Qt_4_2);
 
         quint32 magic = Protocol::magic;
@@ -201,13 +194,13 @@ void ClientAuthHandler::onSocketConnected()
         PeerFactory::ProtoList protos = PeerFactory::supportedProtocols();
         for (int i = 0; i < protos.count(); ++i) {
             quint32 reply = protos[i].first;
-            reply |= protos[i].second<<8;
+            reply |= protos[i].second << 8;
             if (i == protos.count() - 1)
-                reply |= 0x80000000; // end list
+                reply |= 0x80000000;  // end list
             stream << reply;
         }
 
-        socket()->flush(); // make sure the probing data is sent immediately
+        socket()->flush();  // make sure the probing data is sent immediately
         return;
     }
 
@@ -215,32 +208,35 @@ void ClientAuthHandler::onSocketConnected()
 
     qDebug() << "Legacy core detected, switching to compatibility mode";
 
-    auto *peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(Protocol::LegacyProtocol, 0), this, socket(), Compressor::NoCompression, this);
+    auto* peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(Protocol::LegacyProtocol, 0),
+                                         this,
+                                         socket(),
+                                         Compressor::NoCompression,
+                                         this);
     // Only needed for the legacy peer, as all others check the protocol version before instantiation
     connect(peer, &RemotePeer::protocolVersionMismatch, this, &ClientAuthHandler::onProtocolVersionMismatch);
 
     setPeer(peer);
 }
 
-
 void ClientAuthHandler::onReadyRead()
 {
     if (socket()->bytesAvailable() < 4)
         return;
 
     if (!_probing)
-        return; // make sure to not read more data than needed
+        return;  // make sure to not read more data than needed
 
     _probing = false;
     disconnect(socket(), &QIODevice::readyRead, this, &ClientAuthHandler::onReadyRead);
 
     quint32 reply;
-    socket()->read((char *)&reply, 4);
+    socket()->read((char*)&reply, 4);
     reply = qFromBigEndian<quint32>(reply);
 
     auto type = static_cast<Protocol::Type>(reply & 0xff);
-    auto protoFeatures = static_cast<quint16>(reply>>8 & 0xffff);
-    _connectionFeatures = static_cast<quint8>(reply>>24);
+    auto protoFeatures = static_cast<quint16>(reply >> 8 & 0xffff);
+    _connectionFeatures = static_cast<quint8>(reply >> 24);
 
     Compressor::CompressionLevel level;
     if (_connectionFeatures & Protocol::Compression)
@@ -248,7 +244,7 @@ void ClientAuthHandler::onReadyRead()
     else
         level = Compressor::NoCompression;
 
-    RemotePeer *peer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(type, protoFeatures), this, socket(), level, this);
+    RemotePeerpeer = PeerFactory::createPeer(PeerFactory::ProtoDescriptor(type, protoFeatures), this, socket(), level, this);
     if (!peer) {
         qWarning() << "No valid protocol supported for this core!";
         emit errorPopup(tr("<b>Incompatible Quassel Core!</b><br>"
@@ -266,16 +262,15 @@ void ClientAuthHandler::onReadyRead()
     setPeer(peer);
 }
 
-
 void ClientAuthHandler::onProtocolVersionMismatch(int actual, int expected)
 {
     emit errorPopup(tr("<b>The Quassel Core you are trying to connect to is too old!</b><br>"
-           "We need at least protocol v%1, but the core speaks v%2 only.").arg(expected, actual));
+                       "We need at least protocol v%1, but the core speaks v%2 only.")
+                        .arg(expected, actual));
     requestDisconnect(tr("Incompatible protocol version, connection to core refused"));
 }
 
-
-void ClientAuthHandler::setPeer(RemotePeer *peer)
+void ClientAuthHandler::setPeer(RemotePeer* peer)
 {
     qDebug().nospace() << "Using " << qPrintable(peer->protocolName()) << "...";
 
@@ -290,7 +285,6 @@ void ClientAuthHandler::setPeer(RemotePeer *peer)
         checkAndEnableSsl(_connectionFeatures & Protocol::Encryption);
 }
 
-
 void ClientAuthHandler::startRegistration()
 {
     emit statusMessage(tr("Synchronizing to core..."));
@@ -304,15 +298,13 @@ void ClientAuthHandler::startRegistration()
     _peer->dispatch(RegisterClient(Quassel::Features{}, Quassel::buildInfo().fancyVersionString, Quassel::buildInfo().commitDate, useSsl));
 }
 
-
-void ClientAuthHandler::handle(const ClientDenied &msg)
+void ClientAuthHandler::handle(const ClientDenied& msg)
 {
     emit errorPopup(msg.errorString);
     requestDisconnect(tr("The core refused connection from this client"));
 }
 
-
-void ClientAuthHandler::handle(const ClientRegistered &msg)
+void ClientAuthHandler::handle(const ClientRegistered& msg)
 {
     _coreConfigured = msg.coreConfigured;
     _backendInfo = msg.backendInfo;
@@ -321,16 +313,15 @@ void ClientAuthHandler::handle(const ClientRegistered &msg)
     _peer->setFeatures(std::move(msg.features));
 
     // The legacy protocol enables SSL at this point
-    if(_legacy && _account.useSsl())
+    if (_legacy && _account.useSsl())
         checkAndEnableSsl(msg.sslSupported);
     else
         onConnectionReady();
 }
 
-
 void ClientAuthHandler::onConnectionReady()
 {
-    const auto &coreFeatures = _peer->features();
+    const autocoreFeatures = _peer->features();
     auto unsupported = coreFeatures.toStringList(false);
     if (!unsupported.isEmpty()) {
         quInfo() << qPrintable(tr("Core does not support the following features: %1").arg(unsupported.join(", ")));
@@ -346,32 +337,28 @@ void ClientAuthHandler::onConnectionReady()
         // start wizard
         emit startCoreSetup(_backendInfo, _authenticatorInfo);
     }
-    else // TODO: check if we need LoginEnabled
+    else  // TODO: check if we need LoginEnabled
         login();
 }
 
-
-void ClientAuthHandler::setupCore(const SetupData &setupData)
+void ClientAuthHandler::setupCore(const SetupData& setupData)
 {
     _peer->dispatch(setupData);
 }
 
-
-void ClientAuthHandler::handle(const SetupFailed &msg)
+void ClientAuthHandler::handle(const SetupFailed& msg)
 {
     emit coreSetupFailed(msg.errorString);
 }
 
-
-void ClientAuthHandler::handle(const SetupDone &msg)
+void ClientAuthHandler::handle(const SetupDone& msg)
 {
     Q_UNUSED(msg)
 
     emit coreSetupSuccessful();
 }
 
-
-void ClientAuthHandler::login(const QString &user, const QString &password, bool remember)
+void ClientAuthHandler::login(const QString& user, const QString& password, bool remember)
 {
     _account.setUser(user);
     _account.setPassword(password);
@@ -379,13 +366,12 @@ void ClientAuthHandler::login(const QString &user, const QString &password, bool
     login();
 }
 
-
-void ClientAuthHandler::login(const QString &previousError)
+void ClientAuthHandler::login(const QString& previousError)
 {
     emit statusMessage(tr("Logging in..."));
     if (_account.user().isEmpty() || _account.password().isEmpty() || !previousError.isEmpty()) {
         bool valid = false;
-        emit userAuthenticationRequired(&_account, &valid, previousError); // *must* be a synchronous call
+        emit userAuthenticationRequired(&_account, &valid, previousError);  // *must* be a synchronous call
         if (!valid || _account.user().isEmpty() || _account.password().isEmpty()) {
             requestDisconnect(tr("Login canceled"));
             return;
@@ -395,31 +381,27 @@ void ClientAuthHandler::login(const QString &previousError)
     _peer->dispatch(Login(_account.user(), _account.password()));
 }
 
-
-void ClientAuthHandler::handle(const LoginFailed &msg)
+void ClientAuthHandler::handle(const LoginFailed& msg)
 {
     login(msg.errorString);
 }
 
-
-void ClientAuthHandler::handle(const LoginSuccess &msg)
+void ClientAuthHandler::handle(const LoginSuccess& msg)
 {
     Q_UNUSED(msg)
 
     emit loginSuccessful(_account);
 }
 
-
-void ClientAuthHandler::handle(const SessionState &msg)
+void ClientAuthHandler::handle(const SessionState& msg)
 {
-    disconnect(socket(), nullptr, this, nullptr); // this is the last message we shall ever get
+    disconnect(socket(), nullptr, this, nullptr);  // this is the last message we shall ever get
 
     // give up ownership of the peer; CoreSession takes responsibility now
     _peer->setParent(nullptr);
     emit handshakeComplete(_peer, msg);
 }
 
-
 /*** SSL Stuff ***/
 
 void ClientAuthHandler::checkAndEnableSsl(bool coreSupportsSsl)
@@ -432,7 +414,7 @@ void ClientAuthHandler::checkAndEnableSsl(bool coreSupportsSsl)
         // Make sure the warning is shown next time we don't have SSL in the core
         s.setAccountValue("ShowNoCoreSslWarning", true);
 
-        auto *sslSocket = qobject_cast<QSslSocket *>(socket());
+        auto* sslSocket = qobject_cast<QSslSocket*>(socket());
         Q_ASSERT(sslSocket);
         connect(sslSocket, &QSslSocket::encrypted, this, &ClientAuthHandler::onSslSocketEncrypted);
         connect(sslSocket, selectOverload<const QList<QSslError>&>(&QSslSocket::sslErrors), this, &ClientAuthHandler::onSslErrors);
@@ -464,7 +446,7 @@ void ClientAuthHandler::checkAndEnableSsl(bool coreSupportsSsl)
 
 void ClientAuthHandler::onSslSocketEncrypted()
 {
-    auto *socket = qobject_cast<QSslSocket *>(sender());
+    auto* socket = qobject_cast<QSslSocket*>(sender());
     Q_ASSERT(socket);
 
     if (!socket->sslErrors().count()) {
@@ -483,15 +465,15 @@ void ClientAuthHandler::onSslSocketEncrypted()
         startRegistration();
 }
 
-
 void ClientAuthHandler::onSslErrors()
 {
-    auto *socket = qobject_cast<QSslSocket *>(sender());
+    auto* socket = qobject_cast<QSslSocket*>(sender());
     Q_ASSERT(socket);
 
     CoreAccountSettings s;
     QByteArray knownDigest = s.accountValue("SslCert").toByteArray();
-    ClientAuthHandler::DigestVersion knownDigestVersion = static_cast<ClientAuthHandler::DigestVersion>(s.accountValue("SslCertDigestVersion").toInt());
+    ClientAuthHandler::DigestVersion knownDigestVersion = static_cast<ClientAuthHandler::DigestVersion>(
+        s.accountValue("SslCertDigestVersion").toInt());
 
     QByteArray calculatedDigest;
     switch (knownDigestVersion) {