aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/enabled_protocols_dialog.cpp
blob: 1f1703f0518474e46384a68d8981ddc17c6055f2 (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
/* enabled_protocols_dialog.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "enabled_protocols_dialog.h"
#include <ui_enabled_protocols_dialog.h>

#include <errno.h>

#include <epan/proto.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/disabled_protos.h>
#include <wsutil/filesystem.h>

#include "wireshark_application.h"
#include "ui/simple_dialog.h"

enum
{
    PROTOCOL_COLUMN,
    DESCRIPTION_COLUMN
};

enum
{
    enable_type_ = 1000,
    protocol_type_ = 1001,
    heuristic_type_ = 1002
};

#include <QDebug>
class EnableProtocolTreeWidgetItem : public QTreeWidgetItem
{
public:
    EnableProtocolTreeWidgetItem(QTreeWidgetItem *parent, QString proto_name, QString short_name, bool enable, int type = enable_type_) :
        QTreeWidgetItem (parent, type),
        short_name_(short_name),
        proto_name_(proto_name),
        enable_(enable)
    {
        setCheckState(PROTOCOL_COLUMN, enable_ ? Qt::Checked : Qt::Unchecked);
        setText(PROTOCOL_COLUMN, short_name_);
        setText(DESCRIPTION_COLUMN, proto_name_);
    }

    virtual bool applyValue(bool value)
    {
        bool change = (value != enable_);
        enable_ = value;
        applyValuePrivate(value);
        return change;
    }

    // Useful if we ever add "save as" / "copy as".
//    QList<QVariant> rowData() {
//        return QList<QVariant>() << short_name_ << proto_name_ << enable_;
//    }

protected:
    virtual void applyValuePrivate(gboolean value) = 0;

private:
    QString short_name_;
    QString proto_name_;
    bool enable_;
};

class ProtocolTreeWidgetItem : public EnableProtocolTreeWidgetItem
{
public:
    ProtocolTreeWidgetItem(QTreeWidgetItem *parent, const protocol_t *protocol) :
        EnableProtocolTreeWidgetItem (parent, proto_get_protocol_long_name(protocol), proto_get_protocol_short_name(protocol), proto_is_protocol_enabled(protocol), protocol_type_),
        protocol_(protocol)
    {
    }
    const protocol_t *protocol() { return protocol_; }

protected:
    virtual void applyValuePrivate(gboolean value)
    {
        proto_set_decoding(proto_get_id(protocol_), value);
    }

private:
    const protocol_t *protocol_;
};

class HeuristicTreeWidgetItem : public EnableProtocolTreeWidgetItem
{
public:
    HeuristicTreeWidgetItem(QTreeWidgetItem *parent, heur_dtbl_entry_t *heuristic) :
        EnableProtocolTreeWidgetItem (parent, heuristic->display_name, heuristic->short_name, heuristic->enabled, heuristic_type_),
        heuristic_(heuristic)
    {
    }

protected:
    virtual void applyValuePrivate(gboolean value)
    {
        heuristic_->enabled = value;
    }

private:
    heur_dtbl_entry_t *heuristic_;
};

EnabledProtocolsDialog::EnabledProtocolsDialog(QWidget *parent) :
    GeometryStateDialog(parent),
    ui(new Ui::EnabledProtocolsDialog)
{
    ui->setupUi(this);
    loadGeometry();
    setWindowTitle(wsApp->windowTitleString(tr("Enabled Protocols")));

    void *cookie;
    protocol_t *protocol;

    //Remove "original" item
    ui->protocol_tree_->takeTopLevelItem(0);

    // Iterate over all the protocols
    for (gint i = proto_get_first_protocol(&cookie);
         i != -1;
         i = proto_get_next_protocol(&cookie))
    {
        if (proto_can_toggle_protocol(i))
        {
            protocol = find_protocol_by_id(i);
            ProtocolTreeWidgetItem* protocol_row = new ProtocolTreeWidgetItem(ui->protocol_tree_->invisibleRootItem(), protocol);

            proto_heuristic_dissector_foreach(protocol, addHeuristicItem, protocol_row);
        }
    }

    ui->protocol_tree_->expandAll();

    //make sortable
    ui->protocol_tree_->setSortingEnabled(true);
    ui->protocol_tree_->sortByColumn(PROTOCOL_COLUMN, Qt::AscendingOrder);

    // Some protocols have excessively long names. Instead of calling
    // resizeColumnToContents, pick a reasonable-ish em width and apply it.
    int one_em = ui->protocol_tree_->fontMetrics().height();
    ui->protocol_tree_->setColumnWidth(PROTOCOL_COLUMN, one_em * 18);

    //"Remove" Save button
    if (!prefs.gui_use_pref_save)
        ui->buttonBox->button(QDialogButtonBox::Save)->setHidden(true);
}

EnabledProtocolsDialog::~EnabledProtocolsDialog()
{
    delete ui;
}

void EnabledProtocolsDialog::selectProtocol(_protocol *protocol)
{
    QTreeWidgetItemIterator it(ui->protocol_tree_->invisibleRootItem());
    while (*it)
    {
        if ((*it)->type() == protocol_type_)
        {
            ProtocolTreeWidgetItem* protocol_item = dynamic_cast<ProtocolTreeWidgetItem*>((ProtocolTreeWidgetItem*)(*it));
            if (protocol_item && protocol_item->protocol() == protocol) {
                protocol_item->setSelected(true);
                ui->protocol_tree_->scrollToItem((*it));
                return;
            }
        }

        ++it;
    }
}

void EnabledProtocolsDialog::addHeuristicItem(gpointer data, gpointer user_data)
{
    heur_dtbl_entry_t* heur = (heur_dtbl_entry_t*)data;
    ProtocolTreeWidgetItem* protocol_item = (ProtocolTreeWidgetItem*)user_data;

    new HeuristicTreeWidgetItem(protocol_item, heur);
}

void EnabledProtocolsDialog::on_invert_button__clicked()
{
    QTreeWidgetItemIterator it(ui->protocol_tree_->invisibleRootItem());
    while (*it)
    {
        if ((*it)->checkState(PROTOCOL_COLUMN) == Qt::Unchecked)
        {
            (*it)->setCheckState(PROTOCOL_COLUMN, Qt::Checked);
        }
        else
        {
            (*it)->setCheckState(PROTOCOL_COLUMN, Qt::Unchecked);
        }

        ++it;
    }
}

void EnabledProtocolsDialog::on_enable_all_button__clicked()
{
    QTreeWidgetItemIterator it(ui->protocol_tree_->invisibleRootItem());
    while (*it)
    {
       (*it)->setCheckState(PROTOCOL_COLUMN, Qt::Checked);
        ++it;
    }
}

void EnabledProtocolsDialog::on_disable_all_button__clicked()
{
    QTreeWidgetItemIterator it(ui->protocol_tree_->invisibleRootItem());
    while (*it)
    {
       (*it)->setCheckState(PROTOCOL_COLUMN, Qt::Unchecked);
        ++it;
    }
}

bool EnabledProtocolsDialog::applyChanges()
{
    bool redissect = false;

    QTreeWidgetItemIterator it(ui->protocol_tree_);
    while (*it)
    {
        EnableProtocolTreeWidgetItem* it_cast = dynamic_cast<EnableProtocolTreeWidgetItem *>(*it);

        if ((*it)->checkState(PROTOCOL_COLUMN) == Qt::Checked)
        {
            redissect |= it_cast->applyValue(true);
        }
        else
        {
            redissect |= it_cast->applyValue(false);
        }
        ++it;
    }

    return redissect;
}

void EnabledProtocolsDialog::writeChanges()
{
    save_enabled_and_disabled_lists();
}

void EnabledProtocolsDialog::on_search_line_edit__textChanged(const QString &search_re)
{
    QTreeWidgetItemIterator it(ui->protocol_tree_);
    QRegExp regex(search_re, Qt::CaseInsensitive);
    while (*it) {
        bool hidden = true;
        if (search_re.isEmpty() || (*it)->text(PROTOCOL_COLUMN).contains(regex) || (*it)->text(DESCRIPTION_COLUMN).contains(regex)) {
            hidden = false;
        }
        (*it)->setHidden(hidden);
        ++it;
    }
}

void EnabledProtocolsDialog::on_buttonBox_accepted()
{
    if (applyChanges())
    {
        writeChanges();
        wsApp->queueAppSignal(WiresharkApplication::PacketDissectionChanged);
    }
}

#if 0
// If we ever find and fix the bug behind queueAppSignal we can re-enable
// this.
void EnabledProtocolsDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    if (button == ui->buttonBox->button(QDialogButtonBox::Apply))
    {
        if (applyChanges())
        {
            // if we don't have a Save button, just save the settings now
            if (!prefs.gui_use_pref_save)
                writeChanges();

            wsApp->emitAppSignal(WiresharkApplication::PacketDissectionChanged);
        }
    }
}
#endif

void EnabledProtocolsDialog::on_buttonBox_helpRequested()
{
    wsApp->helpTopicAction(HELP_ENABLED_PROTOCOLS_DIALOG);
}

/*
 * 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:
 */