Skip to content

Commit

Permalink
FEAT(client, ui): separate ipv4 and ipv6 in ServerItem tooltip
Browse files Browse the repository at this point in the history
The "Addresses" field in the ServerItem tooltip shows both ipv4 and ipv6
addresses. The order of addresses in this field is not determined.
In the wild it is randomized. As discussed in #5696 it is better to implement
a concrete ordering to prevent user confusion. To further improve the
readability of the addresses we split the addresses field in two fields
called "IPv4 address" and "IPv6 address". The ipv4 address is now always
shown above the ipv6 address.

Implements #5696

Co-Authored-By: Thomas Pawelek <thomas.pawelek@stud.uni-hannover.de>
  • Loading branch information
inodanus and GuybrushThreepwoodII committed Jun 2, 2022
1 parent dd5df45 commit 026ee81
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
21 changes: 14 additions & 7 deletions src/HostAddress.cpp
Expand Up @@ -135,22 +135,29 @@ quint32 qHash(const HostAddress &ha) {
return (ha.hash[0] ^ ha.hash[1] ^ ha.hash[2] ^ ha.hash[3]);
}

QString HostAddress::toString() const {
QString HostAddress::toString(bool bracketEnclosed) const {
if (isV6()) {
if (isValid()) {
QString qs;
const char *squareBracketOpen = "";
const char *squareBracketClose = "";
if (bracketEnclosed) {
squareBracketOpen = "[";
squareBracketClose = "]";
}
#if QT_VERSION >= 0x050500
qs = QString::asprintf("[%x:%x:%x:%x:%x:%x:%x:%x]", ntohs(shorts[0]), ntohs(shorts[1]), ntohs(shorts[2]),
ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]),
ntohs(shorts[7]));
qs = QString::asprintf("%s%x:%x:%x:%x:%x:%x:%x:%x%s", squareBracketOpen, ntohs(shorts[0]), ntohs(shorts[1]),
ntohs(shorts[2]), ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]),
ntohs(shorts[6]), ntohs(shorts[7]), squareBracketClose);
#else
// sprintf() has been deprecated in Qt 5.5 in favor for the static QString::asprintf()
qs.sprintf("[%x:%x:%x:%x:%x:%x:%x:%x]", ntohs(shorts[0]), ntohs(shorts[1]), ntohs(shorts[2]),
ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]), ntohs(shorts[7]));
qs.sprintf("%s%x:%x:%x:%x:%x:%x:%x:%x%s", squareBracketOpen, ntohs(shorts[0]), ntohs(shorts[1]),
ntohs(shorts[2]), ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]),
ntohs(shorts[7]), squareBracketClose);
#endif
return qs.replace(QRegExp(QLatin1String("(:0)+")), QLatin1String(":"));
} else {
return QLatin1String("[::]");
return bracketEnclosed ? QLatin1String("[::]") : QLatin1String("::");
}
} else {
return QHostAddress(ntohl(hash[3])).toString();
Expand Down
2 changes: 1 addition & 1 deletion src/HostAddress.h
Expand Up @@ -35,7 +35,7 @@ struct HostAddress {

bool match(const HostAddress &, int bits) const;

QString toString() const;
QString toString(bool bracketEnclosed = true) const;

std::string toStdString() const;
QHostAddress toAddress() const;
Expand Down
24 changes: 19 additions & 5 deletions src/mumble/ConnectDialog.cpp
Expand Up @@ -473,11 +473,23 @@ QVariant ServerItem::data(int column, int role) const {
return uiUsers ? QString::fromLatin1("%1/%2 ").arg(uiUsers).arg(uiMaxUsers) : QVariant();
}
} else if (role == Qt::ToolTipRole) {
QStringList qsl;
QStringList qslIpv4;
QStringList qslIpv6;
foreach (const ServerAddress &addr, qlAddresses) {
const QString qsAddress = addr.host.toString() + QLatin1String(":")
+ QString::number(static_cast< unsigned long >(addr.port));
qsl << qsAddress.toHtmlEscaped();
const QString qsAddress = addr.host.toString(false).toHtmlEscaped();
if (addr.host.isV6()) {
qslIpv6 << qsAddress;
} else {
qslIpv4 << qsAddress;
}
}
QString qsIpv4 = "-";
QString qsIpv6 = "-";
if (!qslIpv4.isEmpty()) {
qsIpv4 = qslIpv4.join(QLatin1String(", "));
}
if (!qslIpv6.isEmpty()) {
qsIpv6 = qslIpv6.join(QLatin1String(", "));
}

double ploss = 100.0;
Expand All @@ -500,7 +512,9 @@ QVariant ServerItem::data(int column, int role) const {
.arg(ConnectDialog::tr("Port"))
.arg(usPort)
+ QString::fromLatin1("<tr><th align=left>%1</th><td>%2</td></tr>")
.arg(ConnectDialog::tr("Addresses"), qsl.join(QLatin1String(", ")));
.arg(ConnectDialog::tr("IPv4 address"), qsIpv4)
+ QString::fromLatin1("<tr><th align=left>%1</th><td>%2</td></tr>")
.arg(ConnectDialog::tr("IPv6 address"), qsIpv6);

if (!qsUrl.isEmpty())
qs += QString::fromLatin1("<tr><th align=left>%1</th><td>%2</td></tr>")
Expand Down

0 comments on commit 026ee81

Please sign in to comment.