update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / qt / models / supported_protocols_model.cpp
blob9ebfddc7e732a57e31f16c14eccb44d1e189b67d
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
8 */
10 #include <QSortFilterProxyModel>
11 #include <QStringList>
12 #include <QPalette>
13 #include <QApplication>
14 #include <QBrush>
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),
21 proto_(proto),
22 name_(name),
23 filter_(filter),
24 ftype_(ftype),
25 descr_(descr)
29 SupportedProtocolsItem::~SupportedProtocolsItem()
34 SupportedProtocolsModel::SupportedProtocolsModel(QObject *parent) :
35 QAbstractItemModel(parent),
36 root_(new SupportedProtocolsItem(NULL, NULL, NULL, FT_NONE, NULL, NULL)),
37 field_count_(0)
41 SupportedProtocolsModel::~SupportedProtocolsModel()
43 delete root_;
46 int SupportedProtocolsModel::rowCount(const QModelIndex &parent) const
48 SupportedProtocolsItem *parent_item;
49 if (parent.column() > 0)
50 return 0;
52 if (!parent.isValid())
53 parent_item = root_;
54 else
55 parent_item = static_cast<SupportedProtocolsItem*>(parent.internalPointer());
57 if (parent_item == NULL)
58 return 0;
60 return parent_item->childCount();
63 int SupportedProtocolsModel::columnCount(const QModelIndex&) const
65 return colLast;
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) {
73 case colName:
74 return tr("Name");
75 case colFilter:
76 return tr("Filter");
77 case colType:
78 return tr("Type");
79 case colDescription:
80 return tr("Description");
81 default:
82 break;
85 return QVariant();
88 QModelIndex SupportedProtocolsModel::parent(const QModelIndex& index) const
90 if (!index.isValid())
91 return QModelIndex();
93 SupportedProtocolsItem* item = static_cast<SupportedProtocolsItem*>(index.internalPointer());
94 if (item != NULL) {
95 SupportedProtocolsItem* parent_item = item->parentItem();
96 if (parent_item != NULL) {
97 if (parent_item == root_)
98 return QModelIndex();
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())
115 parent_item = root_;
116 else
117 parent_item = static_cast<SupportedProtocolsItem*>(parent.internalPointer());
119 Q_ASSERT(parent_item);
121 child_item = parent_item->child(row);
122 if (child_item) {
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)
132 return QVariant();
134 SupportedProtocolsItem* item = static_cast<SupportedProtocolsItem*>(index.internalPointer());
135 if (item == NULL)
136 return QVariant();
138 switch ((enum SupportedProtocolsColumn)index.column()) {
139 case colName:
140 return item->name();
141 case colFilter:
142 return item->filter();
143 case colType:
144 if (index.parent().isValid())
145 return QString(ftype_pretty_name(item->type()));
147 return QVariant();
148 case colDescription:
149 return item->description();
150 default:
151 break;
154 return QVariant();
157 void SupportedProtocolsModel::populate()
159 void *proto_cookie;
160 void *field_cookie;
162 beginResetModel();
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)
177 continue;
179 fieldItem = new SupportedProtocolsItem(protocol, hfinfo->name, hfinfo->abbrev, hfinfo->type, hfinfo->blurb, protoItem);
180 protoItem->prependChild(fieldItem);
181 field_count_++;
185 endResetModel();
190 SupportedProtocolsProxyModel::SupportedProtocolsProxyModel(QObject * parent)
191 : QSortFilterProxyModel(parent),
192 filter_()
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);
204 if (compare_ret < 0)
205 return true;
208 return false;
211 bool SupportedProtocolsProxyModel::filterAcceptItem(SupportedProtocolsItem& item) const
213 QRegularExpression regex(filter_, QRegularExpression::CaseInsensitiveOption);
214 if (! regex.isValid())
215 return false;
217 if (item.name().contains(regex))
218 return true;
220 if (item.filter().contains(regex))
221 return true;
223 if (item.description().contains(regex))
224 return true;
226 return false;
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());
233 if (item == NULL)
234 return true;
236 if (!filter_.isEmpty()) {
237 if (filterAcceptItem(*item))
238 return true;
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)))
247 return true;
251 return false;
254 return true;
257 void SupportedProtocolsProxyModel::setFilter(const QString& filter)
259 filter_ = filter;
260 invalidateFilter();