1 /* supported_protocols_model.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include <QSortFilterProxyModel>
11 #include <QStringList>
13 #include <QApplication>
15 #include <QRegularExpression>
17 #include <ui/qt/models/supported_protocols_model.h>
19 SupportedProtocolsItem::SupportedProtocolsItem(protocol_t
* proto
, const char *name
, const char* filter
, ftenum_t ftype
, const char* descr
, SupportedProtocolsItem
* parent
)
20 : ModelHelperTreeItem
<SupportedProtocolsItem
>(parent
),
29 SupportedProtocolsItem::~SupportedProtocolsItem()
34 SupportedProtocolsModel::SupportedProtocolsModel(QObject
*parent
) :
35 QAbstractItemModel(parent
),
36 root_(new SupportedProtocolsItem(NULL
, NULL
, NULL
, FT_NONE
, NULL
, NULL
)),
41 SupportedProtocolsModel::~SupportedProtocolsModel()
46 int SupportedProtocolsModel::rowCount(const QModelIndex
&parent
) const
48 SupportedProtocolsItem
*parent_item
;
49 if (parent
.column() > 0)
52 if (!parent
.isValid())
55 parent_item
= static_cast<SupportedProtocolsItem
*>(parent
.internalPointer());
57 if (parent_item
== NULL
)
60 return parent_item
->childCount();
63 int SupportedProtocolsModel::columnCount(const QModelIndex
&) const
68 QVariant
SupportedProtocolsModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
70 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
72 switch ((enum SupportedProtocolsColumn
)section
) {
80 return tr("Description");
88 QModelIndex
SupportedProtocolsModel::parent(const QModelIndex
& index
) const
93 SupportedProtocolsItem
* item
= static_cast<SupportedProtocolsItem
*>(index
.internalPointer());
95 SupportedProtocolsItem
* parent_item
= item
->parentItem();
96 if (parent_item
!= NULL
) {
97 if (parent_item
== root_
)
100 return createIndex(parent_item
->row(), 0, parent_item
);
104 return QModelIndex();
107 QModelIndex
SupportedProtocolsModel::index(int row
, int column
, const QModelIndex
& parent
) const
109 if (!hasIndex(row
, column
, parent
))
110 return QModelIndex();
112 SupportedProtocolsItem
*parent_item
, *child_item
;
114 if (!parent
.isValid())
117 parent_item
= static_cast<SupportedProtocolsItem
*>(parent
.internalPointer());
119 Q_ASSERT(parent_item
);
121 child_item
= parent_item
->child(row
);
123 return createIndex(row
, column
, child_item
);
126 return QModelIndex();
129 QVariant
SupportedProtocolsModel::data(const QModelIndex
&index
, int role
) const
131 if (!index
.isValid() || role
!= Qt::DisplayRole
)
134 SupportedProtocolsItem
* item
= static_cast<SupportedProtocolsItem
*>(index
.internalPointer());
138 switch ((enum SupportedProtocolsColumn
)index
.column()) {
142 return item
->filter();
144 if (index
.parent().isValid())
145 return QString(ftype_pretty_name(item
->type()));
149 return item
->description();
157 void SupportedProtocolsModel::populate()
164 SupportedProtocolsItem
*protoItem
, *fieldItem
;
165 protocol_t
*protocol
;
167 for (int proto_id
= proto_get_first_protocol(&proto_cookie
); proto_id
!= -1;
168 proto_id
= proto_get_next_protocol(&proto_cookie
)) {
170 protocol
= find_protocol_by_id(proto_id
);
171 protoItem
= new SupportedProtocolsItem(protocol
, proto_get_protocol_short_name(protocol
), proto_get_protocol_filter_name(proto_id
), FT_PROTOCOL
, proto_get_protocol_long_name(protocol
), root_
);
172 root_
->prependChild(protoItem
);
174 for (header_field_info
*hfinfo
= proto_get_first_protocol_field(proto_id
, &field_cookie
); hfinfo
!= NULL
;
175 hfinfo
= proto_get_next_protocol_field(proto_id
, &field_cookie
)) {
176 if (hfinfo
->same_name_prev_id
!= -1)
179 fieldItem
= new SupportedProtocolsItem(protocol
, hfinfo
->name
, hfinfo
->abbrev
, hfinfo
->type
, hfinfo
->blurb
, protoItem
);
180 protoItem
->prependChild(fieldItem
);
190 SupportedProtocolsProxyModel::SupportedProtocolsProxyModel(QObject
* parent
)
191 : QSortFilterProxyModel(parent
),
196 bool SupportedProtocolsProxyModel::lessThan(const QModelIndex
&left
, const QModelIndex
&right
) const
198 //Use SupportedProtocolsItem directly for better performance
199 SupportedProtocolsItem
* left_item
= static_cast<SupportedProtocolsItem
*>(left
.internalPointer());
200 SupportedProtocolsItem
* right_item
= static_cast<SupportedProtocolsItem
*>(right
.internalPointer());
202 if ((left_item
!= NULL
) && (right_item
!= NULL
)) {
203 int compare_ret
= QString::compare(left_item
->name(), right_item
->name(), Qt::CaseInsensitive
);
211 bool SupportedProtocolsProxyModel::filterAcceptItem(SupportedProtocolsItem
& item
) const
213 QRegularExpression
regex(filter_
, QRegularExpression::CaseInsensitiveOption
);
214 if (! regex
.isValid())
217 if (item
.name().contains(regex
))
220 if (item
.filter().contains(regex
))
223 if (item
.description().contains(regex
))
229 bool SupportedProtocolsProxyModel::filterAcceptsRow(int sourceRow
, const QModelIndex
&sourceParent
) const
231 QModelIndex nameIdx
= sourceModel()->index(sourceRow
, SupportedProtocolsModel::colName
, sourceParent
);
232 SupportedProtocolsItem
* item
= static_cast<SupportedProtocolsItem
*>(nameIdx
.internalPointer());
236 if (!filter_
.isEmpty()) {
237 if (filterAcceptItem(*item
))
240 if (!nameIdx
.parent().isValid())
242 SupportedProtocolsItem
* child_item
;
243 for (int row
= 0; row
< item
->childCount(); row
++)
245 child_item
= item
->child(row
);
246 if ((child_item
!= NULL
) && (filterAcceptItem(*child_item
)))
257 void SupportedProtocolsProxyModel::setFilter(const QString
& filter
)