summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/dissector_syntax_line_edit.cpp
blob: 4f228ce4f2ea9fd16216113b8505c57d7d9d1f03 (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
/* dissector_syntax_line_edit.cpp
 *
 * 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 <epan/packet.h>

#include <wsutil/utf8_entities.h>

#include <ui/qt/widgets/dissector_syntax_line_edit.h>
#include <ui/qt/widgets/syntax_line_edit.h>

#include <QAction>
#include <QCompleter>
#include <QEvent>
#include <QStringListModel>

#include <wsutil/utf8_entities.h>

// Ordinary dissector names allow the same characters as display filter
// fields (heuristic dissectors don't allow upper-case.)
static const QString fld_abbrev_chars_ = "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

DissectorSyntaxLineEdit::DissectorSyntaxLineEdit(QWidget *parent) :
    SyntaxLineEdit(parent)
{
    setAccessibleName(tr("Dissector entry"));

    completion_model_ = new QStringListModel(this);
    setCompleter(new QCompleter(completion_model_, this));
    setCompletionTokenChars(fld_abbrev_chars_);

    updateDissectorNames();
    setDefaultPlaceholderText();

    connect(this, &DissectorSyntaxLineEdit::textChanged, this,
            static_cast<void (DissectorSyntaxLineEdit::*)(const QString &)>(&DissectorSyntaxLineEdit::checkDissectorName));
}

void DissectorSyntaxLineEdit::updateDissectorNames()
{
    GList *dissector_names = get_dissector_names();
    QStringList dissector_list;
    for (GList* l = dissector_names; l != NULL; l = l->next) {
        dissector_list << (const char*) l->data;
    }
    g_list_free(dissector_names);
    dissector_list.sort();
    completion_model_->setStringList(dissector_list);
}

void DissectorSyntaxLineEdit::setDefaultPlaceholderText()
{
    placeholder_text_ = QString(tr("Enter a dissector %1")).arg(UTF8_HORIZONTAL_ELLIPSIS);

    setPlaceholderText(placeholder_text_);
}

void DissectorSyntaxLineEdit::checkDissectorName(const QString &dissector)
{
    if (dissector.isEmpty()) {
        setSyntaxState(SyntaxLineEdit::Empty);
    } else if (find_dissector(dissector.trimmed().toUtf8().constData())) {
        setSyntaxState(SyntaxLineEdit::Valid);
    } else {
        setSyntaxState(SyntaxLineEdit::Invalid);
    }
}

void DissectorSyntaxLineEdit::buildCompletionList(const QString &field_word, const QString &preamble _U_)
{
#if 0
    // It would be nice to push a hint about the current dissector with
    // the description, as we do with the status line in the main window
    // with filters.
    if (syntaxState() == Valid) {
        dissector_handle_t handle = find_dissector(field_word.toUtf8().constData());
        if (handle) {
            QString cursor_field_msg = QString("%1: %2")
                    .arg(dissector_handle_get_dissector_name(handle))
                    .arg(dissector_handle_get_description(handle));
        }
    }
#endif

    completer()->setCompletionPrefix(field_word);
}

void DissectorSyntaxLineEdit::changeEvent(QEvent* event)
{
    if (0 != event)
    {
        switch (event->type())
        {
        case QEvent::LanguageChange:
            setDefaultPlaceholderText();
            break;
        default:
            break;
        }
    }
    SyntaxLineEdit::changeEvent(event);
}