aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/traffic_types_list.cpp
blob: 30126ef947c7d169ddd5bac94cce6802ba580883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/** @file
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <glib.h>

#include <epan/conversation_table.h>

#include <ui/qt/widgets/traffic_types_list.h>

#include <QStringList>

TrafficTypesRowData::TrafficTypesRowData(int protocol, QString name) :
    _protocol(protocol),
    _name(name),
    _checked(false)
{}

int TrafficTypesRowData::protocol() const
{
    return _protocol;
}

QString TrafficTypesRowData::name() const
{
    return _name;
}

bool TrafficTypesRowData::checked() const
{
    return _checked;
}

void TrafficTypesRowData::setChecked(bool checked)
{
    _checked = checked;
}

static bool iterateProtocols(const void *key, void *value, void *userdata)
{
    QList<TrafficTypesRowData> * protocols = (QList<TrafficTypesRowData> *)userdata;

    register_ct_t* ct = (register_ct_t*)value;
    const QString title = (const gchar*)key;
    int proto_id = get_conversation_proto_id(ct);
    TrafficTypesRowData entry(proto_id, title);
    protocols->append(entry);

    return FALSE;
}

TrafficTypesModel::TrafficTypesModel(GList ** recentList, QObject *parent) :
    QAbstractListModel(parent),
    _recentList(recentList)
{
    conversation_table_iterate_tables(iterateProtocols, &_allTaps);

    std::sort(_allTaps.begin(), _allTaps.end(), [](TrafficTypesRowData a, TrafficTypesRowData b) {
        return a.name().compare(b.name(), Qt::CaseInsensitive) < 0;
    });

    QList<int> _protocols;

    for (GList * endTab = *_recentList; endTab; endTab = endTab->next) {
        int protoId = proto_get_id_by_short_name((const char *)endTab->data);
        if (protoId > -1 && ! _protocols.contains(protoId))
            _protocols.append(protoId);
    }

    if (_protocols.isEmpty()) {
        QStringList protoNames = QStringList() << "eth" << "ip" << "ipv6" << "tcp" << "udp";
        foreach(QString name, protoNames)
            _protocols << proto_get_id_by_filter_name(name.toStdString().c_str());
    }

    for(int cnt = 0; cnt < _allTaps.count(); cnt++)
    {
        _allTaps[cnt].setChecked(false);
        if (_protocols.contains(_allTaps[cnt].protocol()))
            _allTaps[cnt].setChecked(true);
    }

}

int TrafficTypesModel::rowCount(const QModelIndex &) const
{
    return (int) _allTaps.count();
}

int TrafficTypesModel::columnCount(const QModelIndex &) const
{
    return TrafficTypesModel::COL_NUM;
}

QVariant TrafficTypesModel::data(const QModelIndex &idx, int role) const
{
    if (!idx.isValid())
        return QVariant();

    TrafficTypesRowData data = _allTaps[idx.row()];
    if (role == Qt::DisplayRole)
    {
        switch(idx.column())
        {
            case(TrafficTypesModel::COL_NAME):
                return data.name();
            case(TrafficTypesModel::COL_PROTOCOL):
                return data.protocol();
        }
    } else if (role == Qt::CheckStateRole && idx.column() == TrafficTypesModel::COL_CHECKED) {
        return data.checked() ? Qt::Checked : Qt::Unchecked;
    } else if (role == TrafficTypesModel::TRAFFIC_PROTOCOL) {
        return data.protocol();
    } else if (role == TrafficTypesModel::TRAFFIC_IS_CHECKED) {
        return (bool)data.checked();
    }

    return QVariant();
}

QVariant TrafficTypesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (section < 0 || role != Qt::DisplayRole || orientation != Qt::Horizontal)
        return QVariant();

    if (section == TrafficTypesModel::COL_NAME)
        return tr("Protocol");
    return QVariant();
}

Qt::ItemFlags TrafficTypesModel::flags (const QModelIndex & idx) const
{
    Qt::ItemFlags defaultFlags = QAbstractListModel::flags(idx);
    if (idx.isValid())
        return defaultFlags | Qt::ItemIsUserCheckable;

    return defaultFlags;
}

bool TrafficTypesModel::setData(const QModelIndex &idx, const QVariant &value, int role)
{
    if(!idx.isValid() || role != Qt::CheckStateRole)
        return false;

    if (_allTaps.count() <= idx.row())
        return false;

    _allTaps[idx.row()].setChecked(value.toInt() == Qt::Checked);

    QList<int> selected;
    prefs_clear_string_list(*_recentList);
    *_recentList = NULL;

    for (int cnt = 0; cnt < _allTaps.count(); cnt++) {
        if (_allTaps[cnt].checked()) {
            int protoId = _allTaps[cnt].protocol();
            selected.append(protoId);
            char *title = g_strdup(proto_get_protocol_short_name(find_protocol_by_id(protoId)));
            *_recentList = g_list_append(*_recentList, title);
        }
    }

    emit protocolsChanged(selected);

    emit dataChanged(idx, idx);
    return true;
}

void TrafficTypesModel::selectProtocols(QList<int> protocols)
{
    beginResetModel();
    for (int cnt = 0; cnt < _allTaps.count(); cnt++) {
        _allTaps[cnt].setChecked(false);
        if (protocols.contains(_allTaps[cnt].protocol()))
            _allTaps[cnt].setChecked(true);
    }
    endResetModel();
}


TrafficListSortModel::TrafficListSortModel(QObject * parent) :
    QSortFilterProxyModel(parent)
{}

bool TrafficListSortModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
    if (source_left.isValid() && source_left.column() == TrafficTypesModel::COL_NAME) {
        QString valA = source_left.data().toString();
        QString valB = source_right.data().toString();
        return valA.compare(valB, Qt::CaseInsensitive) <= 0;
    }
    return QSortFilterProxyModel::lessThan(source_left, source_right);
}

void TrafficListSortModel::setFilter(QString filter)
{
    if ( filter.compare(_filter) != 0 ) {
        _filter = filter;
        invalidateFilter();
    }
}

bool TrafficListSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    if (sourceModel() && _filter.length() > 0) {
        QModelIndex idx = sourceModel()->index(source_row, TrafficTypesModel::COL_NAME);

        if (idx.isValid()) {
            QString name = idx.data().toString();
            if (name.contains(_filter, Qt::CaseInsensitive))
                return true;
            return false;
        }
    }

    return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}


TrafficTypesList::TrafficTypesList(QWidget *parent) :
    QTreeView(parent)
{
    _name = QString();
    _model = nullptr;
    _sortModel = nullptr;

    setAlternatingRowColors(true);
    setRootIsDecorated(false);
}

void TrafficTypesList::setProtocolInfo(QString name, GList ** recentList)
{
    _name = name;

    _sortModel = new TrafficListSortModel(this);

    _model = new TrafficTypesModel(recentList, this);
    _sortModel->setSourceModel(_model);
    setModel(_sortModel);

    setSortingEnabled(true);
    sortByColumn(TrafficTypesModel::COL_NAME, Qt::AscendingOrder);

    connect(_model, &TrafficTypesModel::protocolsChanged, this, &TrafficTypesList::protocolsChanged);

    resizeColumnToContents(0);
    resizeColumnToContents(1);
}

void TrafficTypesList::selectProtocols(QList<int> protocols)
{
    if (_model) {
        _model->selectProtocols(protocols);
        emit clearFilterList();
    }
}

QList<int> TrafficTypesList::protocols(bool onlySelected) const
{
    QList<int> entries;
    for (int cnt = 0; cnt < _model->rowCount(); cnt++) {
        QModelIndex idx = _model->index(cnt, TrafficTypesModel::COL_CHECKED);
        int protoId = _model->data(idx, TrafficTypesModel::TRAFFIC_PROTOCOL).toInt();
        if (protoId > 0 && ! entries.contains(protoId)) {
            if (!onlySelected || _model->data(idx, TrafficTypesModel::TRAFFIC_IS_CHECKED).toBool())
                entries.append(protoId);
        }
    }

    return entries;
}

void TrafficTypesList::filterList(QString filter)
{
    _sortModel->setFilter(filter);
}