From 0dfd4ebc4acd2d54dbe3df6773b84c42e2b5277e Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Thu, 7 Aug 2025 12:17:19 -0400 Subject: [PATCH] refactor: Mainwindow, adjust how and where the ip address is shown. - Add a new method updateNetworkInfo to update lblIpAddresses This method can be hooked to the system network info in the future when we want to have this be more dynamic. - Only show the IP address when in server mode. - When showing the IP address show either the suggested IP or a message the user maybe offline - Use the linkVisited color to for our error message --- src/lib/gui/MainWindow.cpp | 76 ++++++++++++++++++++++---------------- src/lib/gui/MainWindow.h | 2 +- src/lib/gui/MainWindow.ui | 32 +++++++--------- 3 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/lib/gui/MainWindow.cpp b/src/lib/gui/MainWindow.cpp index 273e6458e..d7284004d 100644 --- a/src/lib/gui/MainWindow.cpp +++ b/src/lib/gui/MainWindow.cpp @@ -220,7 +220,8 @@ void MainWindow::setupControls() secureSocket(false); ui->btnConfigureServer->setIcon(QIcon::fromTheme(QStringLiteral("configure"))); - ui->lblIpAddresses->setText(tr("This computer's IP addresses: %1").arg(getIPAddresses())); + + updateNetworkInfo(); if (Settings::value(Settings::Core::LastVersion).toString() != kVersion) { Settings::setValue(Settings::Core::LastVersion, kVersion); @@ -567,6 +568,7 @@ void MainWindow::coreModeToggled() void MainWindow::updateModeControls(bool serverMode) { + ui->lblIpAddresses->setVisible(serverMode); ui->serverOptions->setVisible(serverMode); ui->clientOptions->setVisible(!serverMode); ui->lblNoMode->setVisible(false); @@ -606,6 +608,48 @@ void MainWindow::updateSecurityIcon(bool visible) m_lblSecurityStatus->setPixmap(icon.pixmap(QSize(32, 32))); } +void MainWindow::updateNetworkInfo() +{ + static const auto colorText = QStringLiteral(R"(%2)"); + + QStringList ipList; + QString suggestedAddress; + + bool hinted = false; + + const auto addresses = QNetworkInterface::allAddresses(); + for (const auto &address : addresses) { + if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost) && + !address.isInSubnet(QHostAddress::parseSubnet("169.254.0.0/16"))) { + // usually 192.168.x.x is a useful ip for the user, so indicate + // this by coloring it in the "link" color + if (!hinted && address.isInSubnet(QHostAddress::parseSubnet("192.168/16"))) { + suggestedAddress = address.toString(); + ipList.append(colorText.arg(palette().link().color().name(), suggestedAddress)); + hinted = true; + } else { + ipList.append(address.toString()); + } + } + } + + if (ipList.isEmpty()) { + ui->lblIpAddresses->setText(colorText.arg(palette().linkVisited().color().name(), tr("No IP Detected"))); + ui->lblIpAddresses->setToolTip(tr("Unable to detect an IP address. Check your network connection is active.")); + return; + } + + ui->lblIpAddresses->setText( + QStringLiteral("Suggested IP: %1").arg(suggestedAddress.isEmpty() ? ipList.first() : suggestedAddress) + ); + + if (auto toolTipBase = tr("

If connecting via the hostname fails, try %1

"); ipList.count() < 2) { + ui->lblIpAddresses->setToolTip(toolTipBase.arg(tr("the suggested IP."))); + } else { + ui->lblIpAddresses->setToolTip(toolTipBase.arg(tr("one of the following IPs:
%1").arg(ipList.join("br/>")))); + } +} + void MainWindow::serverConnectionConfigureClient(const QString &clientName) { Settings::setValue(Settings::Server::ConfigVisible, true); @@ -996,36 +1040,6 @@ void MainWindow::coreConnectionStateChanged(CoreConnectionState state) } } -QString MainWindow::getIPAddresses() const -{ - QStringList result; - bool hinted = false; - const auto localnet = QHostAddress::parseSubnet("192.168.0.0/16"); - const QList addresses = QNetworkInterface::allAddresses(); - - for (const auto &address : addresses) { - if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost) && - !address.isInSubnet(QHostAddress::parseSubnet("169.254.0.0/16"))) { - - // usually 192.168.x.x is a useful ip for the user, so indicate - // this by coloring it in the "link" color. - if (!hinted && address.isInSubnet(localnet)) { - QString format = R"(%2)"; - result.append(format.arg(palette().link().color().name(), address.toString())); - hinted = true; - } else { - result.append(address.toString()); - } - } - } - - if (result.isEmpty()) { - result.append("Unknown"); - } - - return result.join(", "); -} - void MainWindow::updateLocalFingerprint() { const bool tlsEnabled = Settings::value(Settings::Security::TlsEnabled).toBool(); diff --git a/src/lib/gui/MainWindow.h b/src/lib/gui/MainWindow.h index 0e1705a01..83f58851d 100644 --- a/src/lib/gui/MainWindow.h +++ b/src/lib/gui/MainWindow.h @@ -113,6 +113,7 @@ private: void showMyFingerprint(); void updateSecurityIcon(bool visible); + void updateNetworkInfo(); void coreModeToggled(); void updateModeControls(bool serverMode); @@ -126,7 +127,6 @@ private: void setIcon(); void setStatus(const QString &status); void updateFromLogLine(const QString &line); - [[nodiscard]] QString getIPAddresses() const; void checkConnected(const QString &line); void checkFingerprint(const QString &line); [[nodiscard]] QString getTimeStamp() const; diff --git a/src/lib/gui/MainWindow.ui b/src/lib/gui/MainWindow.ui index 3a3ad8ad4..4981c6bdb 100644 --- a/src/lib/gui/MainWindow.ui +++ b/src/lib/gui/MainWindow.ui @@ -6,8 +6,8 @@ 0 0 - 735 - 515 + 781 + 483 @@ -131,31 +131,25 @@ - 40 + 6 20 + + + + + 0 + 0 + + + + - - - - - 0 - 0 - - - - <html><head/><body><p>The highlighted IP is the one we think you should use. The server listens on all IPs, so the other IPs may work as well.</p></body></html> - - - This computer's IP addresses: - - -