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
This commit is contained in:
sithlord48
2025-08-07 12:17:19 -04:00
committed by Chris Rizzitello
parent a7048f76f9
commit 0dfd4ebc4a
3 changed files with 59 additions and 51 deletions

View File

@ -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"(<span style="color:%1;">%2</span>)");
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("<p>If connecting via the hostname fails, try %1</p>"); ipList.count() < 2) {
ui->lblIpAddresses->setToolTip(toolTipBase.arg(tr("the suggested IP.")));
} else {
ui->lblIpAddresses->setToolTip(toolTipBase.arg(tr("one of the following IPs:<br/>%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<QHostAddress> 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"(<span style="color:%1;">%2</span>)";
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();

View File

@ -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;

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>735</width>
<height>515</height>
<width>781</width>
<height>483</height>
</rect>
</property>
<property name="sizePolicy">
@ -131,31 +131,25 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<width>6</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="lblIpAddresses">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="lblIpAddresses">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string notr="true">This computer's IP addresses:</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">