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: