diff options
author | Guy Harris <gharris@sonic.net> | 2021-05-18 01:03:04 -0700 |
---|---|---|
committer | Guy Harris <gharris@sonic.net> | 2021-05-18 09:41:00 +0000 |
commit | 82b0eac6af65283af4546ec087f8facad505c9da (patch) | |
tree | a72fada2f38f4387d0a21cb3c424cc5e3bab79ee | |
parent | 4a5dece15532cf42059c707017773e130b996f7d (diff) |
PortsModel: don't populate it by doing a lot of weird string-pushing.
When enumerating port-to-name entries, the callback to
wmem_map_foreach() gets passed:
- a key, which is the port number for the entry;
- a value, which is a pointer to a structure containing pointers to port
names for various transport protocols;
- a user data pointer.
That's sufficient (if you work around some C++ annoyances) to append a
row to a PortsModel, if the user data pointer is a pointer to the
PortsModel.
The existing code, instead, appended to a QStringList of lines (in
effect, undoing the effort of the code that read the services file and
filled in the wmem_map, re-generating a set of lines) in the callback,
and then iterated over all the lines, splitting them with blanks and
appending rows.
Looking at that made my eyeballs bleed so badly that I decided not to
spend any time figuring out why it wasn't working.
So I just make the callback just append rows, avoiding all the
string-pushing.
Fixes #17395.
(cherry picked from commit 6e95a0aa47dd739edd852a3aa12f80d1d35ff270)
-rw-r--r-- | ui/qt/models/astringlist_list_model.h | 9 | ||||
-rw-r--r-- | ui/qt/models/resolved_addresses_models.cpp | 52 |
2 files changed, 45 insertions, 16 deletions
diff --git a/ui/qt/models/astringlist_list_model.h b/ui/qt/models/astringlist_list_model.h index d21fe31a5a..21e79d1a97 100644 --- a/ui/qt/models/astringlist_list_model.h +++ b/ui/qt/models/astringlist_list_model.h @@ -32,9 +32,16 @@ public: virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; -protected: + // + // This is not protected because we may need to invoke it from + // a wmem_map_foreach() callback implemented as an extern "C" + // static member function of a subclass. wmem_map_foreach() is + // passed, as the user data, a pointer to the class instance to + // which we want to append rows. + // virtual void appendRow(const QStringList &, const QString & row_tooltip = QString(), const QModelIndex &parent = QModelIndex()); +protected: virtual QStringList headerColumns() const = 0; private: diff --git a/ui/qt/models/resolved_addresses_models.cpp b/ui/qt/models/resolved_addresses_models.cpp index 1955eb538d..b2e0a90c06 100644 --- a/ui/qt/models/resolved_addresses_models.cpp +++ b/ui/qt/models/resolved_addresses_models.cpp @@ -20,20 +20,47 @@ extern "C" { static void -serv_port_hash_to_qstringlist(gpointer key, gpointer value, gpointer sl_ptr) +serv_port_hash_to_qstringlist(gpointer key, gpointer value, gpointer member_ptr) { - QStringList *string_list = (QStringList *) sl_ptr; + PortsModel *model = static_cast<PortsModel *>(member_ptr); serv_port_t *serv_port = (serv_port_t *)value; guint port = GPOINTER_TO_UINT(key); - QStringList entries; - - if (serv_port->tcp_name) entries << QString("%1 %2 tcp").arg(serv_port->tcp_name).arg(port); - if (serv_port->udp_name) entries << QString("%1 %2 udp").arg(serv_port->udp_name).arg(port); - if (serv_port->sctp_name) entries << QString("%1 %2 sctp").arg(serv_port->sctp_name).arg(port); - if (serv_port->dccp_name) entries << QString("%1 %2 dccp").arg(serv_port->dccp_name).arg(port); + if (serv_port->tcp_name) { + QStringList entries; - if (!entries.isEmpty()) *string_list << entries.join("\n"); + entries << serv_port->tcp_name; + entries << QString("%1").arg(port); + entries << "tcp"; + model->appendRow(entries); + } + if (serv_port->udp_name) { + QStringList entries; + + entries = QStringList(); + entries << serv_port->udp_name; + entries << QString("%1").arg(port); + entries << "udp"; + model->appendRow(entries); + } + if (serv_port->sctp_name) { + QStringList entries; + + entries = QStringList(); + entries << serv_port->sctp_name; + entries << QString("%1").arg(port); + entries << "sctp"; + model->appendRow(entries); + } + if (serv_port->dccp_name) { + QStringList entries; + + entries = QStringList(); + entries << serv_port->dccp_name; + entries << QString("%1").arg(port); + entries << "dccp"; + model->appendRow(entries); + } } static void @@ -175,15 +202,10 @@ QStringList PortsModel::headerColumns() const void PortsModel::populate() { - QStringList values; - wmem_map_t *serv_port_hashtable = get_serv_port_hashtable(); if (serv_port_hashtable) { - wmem_map_foreach(serv_port_hashtable, serv_port_hash_to_qstringlist, &values); + wmem_map_foreach(serv_port_hashtable, serv_port_hash_to_qstringlist, this); } - - foreach(QString line, values) - appendRow(QStringList() << line.split(" ")); } /* |