From c21d4269f80286aa1465a1bee9a1ab5dddc6d560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loi=CC=88c=20PE=CC=81RON?= Date: Fri, 10 Sep 2021 11:18:10 +0200 Subject: [PATCH 1/2] Use screen name instead of IP address to announce server through zeroconf --- src/gui/src/ZeroconfService.cpp | 38 ++++----------------------------- src/gui/src/ZeroconfService.h | 1 - 2 files changed, 4 insertions(+), 35 deletions(-) diff --git a/src/gui/src/ZeroconfService.cpp b/src/gui/src/ZeroconfService.cpp index f7b10007b9..f6d1ccd68c 100644 --- a/src/gui/src/ZeroconfService.cpp +++ b/src/gui/src/ZeroconfService.cpp @@ -124,27 +124,6 @@ void ZeroconfService::errorHandle(DNSServiceErrorType errorCode) tr("Error code: %1.").arg(errorCode)); } -QString ZeroconfService::getLocalIPAddresses() -{ - QStringList addresses; - for (const QHostAddress& address : QNetworkInterface::allAddresses()) { - if (address.protocol() == QAbstractSocket::IPv4Protocol && - address != QHostAddress(QHostAddress::LocalHost)) { - addresses.append(address.toString()); - } - } - - for (const QString& preferedIP : preferedIPAddress) { - for (const QString& address : addresses) { - if (address.startsWith(preferedIP)) { - return address; - } - } - } - - return ""; -} - bool ZeroconfService::registerService(bool server) { bool result = true; @@ -159,19 +138,10 @@ bool ZeroconfService::registerService(bool server) else { m_pZeroconfRegister = new ZeroconfRegister(this); if (server) { - QString localIP = getLocalIPAddresses(); - if (localIP.isEmpty()) { - QMessageBox::warning(m_pMainWindow, tr("Barrier"), - tr("Failed to get local IP address. " - "Please manually type in server address " - "on your clients")); - } - else { - m_pZeroconfRegister->registerService( - ZeroconfRecord(tr("%1").arg(localIP), - QLatin1String(m_ServerServiceName), QString()), - m_zeroconfServer.serverPort()); - } + m_pZeroconfRegister->registerService( + ZeroconfRecord(tr("%1").arg(m_pMainWindow->getScreenName()), + QLatin1String(m_ServerServiceName), QString()), + m_zeroconfServer.serverPort()); } else { m_pZeroconfRegister->registerService( diff --git a/src/gui/src/ZeroconfService.h b/src/gui/src/ZeroconfService.h index 929cd05581..8a3b8beb14 100644 --- a/src/gui/src/ZeroconfService.h +++ b/src/gui/src/ZeroconfService.h @@ -42,7 +42,6 @@ private slots: void errorHandle(DNSServiceErrorType errorCode); private: - QString getLocalIPAddresses(); bool registerService(bool server); private: From c4d0ba727f25809416ece77b4cdcc9b569ee52ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20P=C3=89RON?= <44115467+nicerloop@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:41:45 +0100 Subject: [PATCH 2/2] Add news fragment for #1286 --- doc/newsfragments/zeroconf-announce-screen-name.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/newsfragments/zeroconf-announce-screen-name.bugfix diff --git a/doc/newsfragments/zeroconf-announce-screen-name.bugfix b/doc/newsfragments/zeroconf-announce-screen-name.bugfix new file mode 100644 index 0000000000..dd7542bc8b --- /dev/null +++ b/doc/newsfragments/zeroconf-announce-screen-name.bugfix @@ -0,0 +1 @@ +Use screen name instead of IP address to announce server through zeroconf.