From a6318b4987a4082ae70f57b542136283f87a0066 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 20 Feb 2022 16:14:43 +0100 Subject: [PATCH] Fix connecting via preferred connection --- libnymea-app/connection/nymeaconnection.cpp | 29 +++++++++++---------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/libnymea-app/connection/nymeaconnection.cpp b/libnymea-app/connection/nymeaconnection.cpp index 64e22ddc..16ae76e5 100644 --- a/libnymea-app/connection/nymeaconnection.cpp +++ b/libnymea-app/connection/nymeaconnection.cpp @@ -132,7 +132,20 @@ void NymeaConnection::setCurrentHost(NymeaHost *host) return; } - m_preferredConnection = nullptr; + if (m_preferredConnection) { + if (host->connections()->find(m_preferredConnection->url())) { + qCInfo(dcNymeaConnection()) << "Setting preferred connection to" << m_preferredConnection->url(); + // Unset the preferred connection when it is removed + connect(m_preferredConnection, &Connection::destroyed, this, [=](){ + qCWarning(dcNymeaConnection()) << "Unsetting preferred connection as it was destroyed."; + m_preferredConnection = nullptr; + }); + } else { + qCWarning(dcNymeaConnection()) << "Connection" << m_preferredConnection << "is not a candidate for" << host->name() << "Not setting preferred connection."; + m_preferredConnection = nullptr; + } + } + if (m_currentTransport) { m_currentTransport = nullptr; @@ -456,19 +469,7 @@ void NymeaConnection::connectToHost(NymeaHost *nymeaHost, Connection *connection return; } - m_preferredConnection = nullptr; - if (connection) { - if (nymeaHost->connections()->find(connection->url())) { - qCInfo(dcNymeaConnection()) << "Setting preferred connection to" << connection->url(); - m_preferredConnection = connection; - // Unset the preferred connection when it is removed - connect(connection, &Connection::destroyed, this, [=](){ - m_preferredConnection = nullptr; - }); - } else { - qCWarning(dcNymeaConnection()) << "Connection" << connection << "is not a candidate for" << nymeaHost->name() << "Not setting preferred connection."; - } - } + m_preferredConnection = connection; setCurrentHost(nymeaHost); }