aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/decode_as_delegate.cpp
blob: f9bbc789b450dc3cac195691dda780ce42207ff8 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* decode_as_delegate.cpp
 * Delegates for editing various field types in a Decode As record.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+*/

#include "decode_as_delegate.h"

#include "epan/decode_as.h"
#include "epan/epan_dissect.h"

#include <ui/qt/utils/variant_pointer.h>

#include <QComboBox>
#include <QEvent>
#include <QLineEdit>
#include <QTreeView>

typedef struct _dissector_info_t {
    QString             proto_name;
    dissector_handle_t  dissector_handle;
} dissector_info_t;

Q_DECLARE_METATYPE(dissector_info_t *)

DecodeAsDelegate::DecodeAsDelegate(QObject *parent, capture_file *cf)
 : QStyledItemDelegate(parent),
    cap_file_(cf)
{
    cachePacketProtocols();
}

DecodeAsItem* DecodeAsDelegate::indexToField(const QModelIndex &index) const
{
    const QVariant v = index.model()->data(index, Qt::UserRole);
    return static_cast<DecodeAsItem*>(v.value<void *>());
}

void DecodeAsDelegate::cachePacketProtocols()
{
    //cache the list of potential decode as protocols in the current packet
    if (cap_file_ && cap_file_->edt) {

        wmem_list_frame_t * protos = wmem_list_head(cap_file_->edt->pi.layers);
        guint8 curr_layer_num = 1;

        while (protos != NULL) {
            int proto_id = GPOINTER_TO_INT(wmem_list_frame_data(protos));
            const gchar * proto_name = proto_get_protocol_filter_name(proto_id);
            for (GList *cur = decode_as_list; cur; cur = cur->next) {
                decode_as_t *entry = (decode_as_t *) cur->data;
                if (g_strcmp0(proto_name, entry->name) == 0) {
                    packet_proto_data_t proto_data;

                    proto_data.table_ui_name = get_dissector_table_ui_name(entry->table_name);
                    proto_data.proto_name = proto_name;
                    proto_data.curr_layer_num = curr_layer_num;

                    packet_proto_list_.append(proto_data);
                }
            }
            protos = wmem_list_frame_next(protos);
            curr_layer_num++;
        }
    }
}

void DecodeAsDelegate::collectDAProtocols(QSet<QString>& all_protocols, QList<QString>& current_list) const
{
    // If a packet is selected group its tables at the top in order
    // from last-dissected to first.

    //gather the initial list
    for (GList *cur = decode_as_list; cur; cur = cur->next) {
        decode_as_t *entry = (decode_as_t *) cur->data;
        const char *table_name = get_dissector_table_ui_name(entry->table_name);
        if (table_name) {
            all_protocols.insert(get_dissector_table_ui_name(entry->table_name));
        }
    }

    //filter out those in selected packet
    foreach(packet_proto_data_t proto, packet_proto_list_)
    {
        current_list.append(proto.table_ui_name);
        all_protocols.remove(proto.table_ui_name);
    }
}

//Determine if there are multiple values in the selector field that would
//correspond to using a combo box
bool DecodeAsDelegate::isSelectorCombo(DecodeAsItem* item) const
{
    const gchar *proto_name = NULL;

    foreach(packet_proto_data_t proto, packet_proto_list_)
    {
        if (g_strcmp0(proto.table_ui_name, item->tableUIName_) == 0) {
            proto_name = proto.proto_name;
            break;
        }
    }

    for (GList *cur = decode_as_list; cur; cur = cur->next) {
        decode_as_t *entry = (decode_as_t *) cur->data;
        if ((g_strcmp0(proto_name, entry->name) == 0) &&
            (g_strcmp0(item->tableName_, entry->table_name) == 0) &&
            (cap_file_ && cap_file_->edt) &&
            (entry->num_items > 1)) {
                return true;
        }
    }

    return false;
}

void DecodeAsDelegate::decodeAddProtocol(const gchar *, const gchar *proto_name, gpointer value, gpointer user_data)
{
    QMap<QString, dissector_info_t*>* proto_list = (QMap<QString, dissector_info_t*>*)user_data;

    if (!proto_list)
        return;

    dissector_info_t  *dissector_info = new dissector_info_t();
    dissector_info->proto_name = proto_name;
    dissector_info->dissector_handle = (dissector_handle_t) value;

    proto_list->insert(proto_name, dissector_info);
}

QWidget* DecodeAsDelegate::createEditor(QWidget *parentWidget, const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    DecodeAsItem* item = indexToField(index);

    switch(index.column())
    {
    case DecodeAsModel::colTable:
        {
        QComboBox *editor = new QComboBox(parentWidget);
        QSet<QString> da_set;
        QList<QString> packet_list;
        QString table_ui_name;

        collectDAProtocols(da_set, packet_list);

        editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);

        //put the protocols from the packet first in the combo box
        foreach (table_ui_name, packet_list) {
            editor->addItem(table_ui_name, table_ui_name);
        }
        if (packet_list.count() > 0) {
            editor->insertSeparator(packet_list.count());
        }

        //put the rest of the protocols in the combo box
        QList<QString> da_list = da_set.toList();
        qSort(da_list.begin(), da_list.end());

        foreach (table_ui_name, da_list) {
            editor->addItem(table_ui_name, table_ui_name);
        }

        //Make sure the combo box is at least as wide as the column
        QTreeView* parentTree = (QTreeView*)parent();
        int protoColWidth = parentTree->columnWidth(index.column());
        if (protoColWidth > editor->size().width())
            editor->setFixedWidth(protoColWidth);

        return editor;
        }
    case DecodeAsModel::colSelector:
        {
        QComboBox *editor = NULL;
        const gchar *proto_name = NULL;
        bool edt_present = cap_file_ && cap_file_->edt;
        gint8 curr_layer_num_saved = edt_present ? cap_file_->edt->pi.curr_layer_num : 0;

        foreach(packet_proto_data_t proto, packet_proto_list_)
        {
            if (g_strcmp0(proto.table_ui_name, item->tableUIName_) == 0) {
                if (edt_present) {
                    cap_file_->edt->pi.curr_layer_num = proto.curr_layer_num;
                }
                proto_name = proto.proto_name;
                //XXX - break?  Or do we always want the last layer of tunnelled protocols?
            }
        }

        for (GList *cur = decode_as_list; cur; cur = cur->next) {
            decode_as_t *entry = (decode_as_t *) cur->data;
            if ((g_strcmp0(proto_name, entry->name) == 0) &&
                (g_strcmp0(item->tableName_, entry->table_name) == 0)) {
                if (edt_present) {
                    if (entry->num_items > 1)
                    {
                        //only create a combobox if there is a choice of values, otherwise it looks funny
                        editor = new QComboBox(parentWidget);

                        //Don't limit user to just what's in combo box
                        editor->setEditable(true);

                        editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);

                        //add the current value of the column
                        const QString& current_value = index.model()->data(index, Qt::EditRole).toString();
                        if (!current_value.isEmpty())
                            editor->addItem(current_value);

                        //get the value(s) from the packet
                        for (uint ni = 0; ni < entry->num_items; ni++) {
                            if (entry->values[ni].num_values == 1) { // Skip over multi-value ("both") entries
                                QString entryStr = DecodeAsModel::entryString(entry->table_name,
                                                                        entry->values[ni].build_values[0](&cap_file_->edt->pi));
                                //don't duplicate entries
                                if (editor->findText(entryStr) < 0)
                                    editor->addItem(entryStr);
                            }
                        }
                        editor->setCurrentIndex(entry->default_index_value);

                        //Make sure the combo box is at least as wide as the column
                        QTreeView* parentTree = (QTreeView*)parent();
                        int protoColWidth = parentTree->columnWidth(index.column());
                        if (protoColWidth > editor->size().width())
                            editor->setFixedWidth(protoColWidth);

                    }
                }
                break;
            }
        }

        if (edt_present) {
            cap_file_->edt->pi.curr_layer_num = curr_layer_num_saved;
        }

        //if there isn't a need for a combobox, just let user have a text box for direct edit
        if (editor == NULL)
            return QStyledItemDelegate::createEditor(parentWidget, option, index);

        return editor;
        }

    case DecodeAsModel::colProtocol:
        {
        QComboBox *editor = new QComboBox(parentWidget);
        QMap<QString, dissector_info_t*> protocols;

        editor->setSizeAdjustPolicy(QComboBox::AdjustToContents);

        for (GList *cur = decode_as_list; cur; cur = cur->next) {
            decode_as_t *entry = (decode_as_t *) cur->data;
            if (g_strcmp0(item->tableName_, entry->table_name) == 0) {
                entry->populate_list(entry->table_name, decodeAddProtocol, &protocols);
                break;
            }
        }

        editor->addItem(DECODE_AS_NONE);
        editor->insertSeparator(editor->count());

        //QMap already sorts the keys (protocols) alphabetically
        QMap<QString, dissector_info_t*>::iterator protocol;
        for(protocol = protocols.begin(); protocol != protocols.end(); protocol++)
        {
            editor->addItem(protocol.key(), VariantPointer<dissector_info_t>::asQVariant(protocol.value()));
        }

        //Make sure the combo box is at least as wide as the column
        QTreeView* parentTree = (QTreeView*)parent();
        int protoColWidth = parentTree->columnWidth(index.column());
        if (protoColWidth > editor->size().width())
            editor->setFixedWidth(protoColWidth);

        return editor;
        }
    }

    return NULL;
}

void DecodeAsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    DecodeAsItem* item = indexToField(index);

    switch(index.column())
    {
    case DecodeAsModel::colTable:
    case DecodeAsModel::colProtocol:
        {
        QComboBox *combobox = static_cast<QComboBox *>(editor);
        const QString &data = index.model()->data(index, Qt::EditRole).toString();
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
        combobox->setCurrentText(data);
#else
        int new_index = combobox->findText(data);
        if (new_index >= 0) {
            combobox->setCurrentIndex(new_index);
        }
#endif
        }
        break;
    case DecodeAsModel::colSelector:
        if (isSelectorCombo(item)) {
            QComboBox *combobox = static_cast<QComboBox *>(editor);
            const QString &data = index.model()->data(index, Qt::EditRole).toString();
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
            combobox->setCurrentText(data);
#else
            int new_index = combobox->findText(data);
            if (new_index >= 0) {
                combobox->setCurrentIndex(new_index);
            }
#endif
        }
        else {
            QStyledItemDelegate::setEditorData(editor, index);
        }
        break;
    default:
        QStyledItemDelegate::setEditorData(editor, index);
        break;
    }
}

void DecodeAsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                              const QModelIndex &index) const
{
    DecodeAsItem* item = indexToField(index);

    switch(index.column())
    {
    case DecodeAsModel::colTable:
        {
        QComboBox *combobox = static_cast<QComboBox *>(editor);
        const QString &data = combobox->currentText();
        model->setData(index, data, Qt::EditRole);
        break;
        }
    case DecodeAsModel::colSelector:
        if (isSelectorCombo(item)) {
            QComboBox *combobox = static_cast<QComboBox *>(editor);
            const QString &data = combobox->currentText();
            model->setData(index, data, Qt::EditRole);
        } else {
            QStyledItemDelegate::setModelData(editor, model, index);
        }
        break;
    case DecodeAsModel::colProtocol:
        {
        QComboBox *combobox = static_cast<QComboBox *>(editor);
        const QString &data = combobox->currentText();
        model->setData(index, data, Qt::EditRole);

        //set the dissector handle
        QVariant var = combobox->itemData(combobox->currentIndex());
        dissector_info_t* dissector_info = VariantPointer<dissector_info_t>::asPtr(var);
        if (dissector_info != NULL) {
            ((DecodeAsModel*)model)->setDissectorHandle(index, dissector_info->dissector_handle);
        } else {
            ((DecodeAsModel*)model)->setDissectorHandle(index, NULL);
        }
        break;
        }
    default:
        QStyledItemDelegate::setModelData(editor, model, index);
        break;
    }
}

#if 0
// Qt docs suggest overriding updateEditorGeometry, but the defaults seem sane.
void UatDelegate::updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}
#endif

/* * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */