Clean up more includes of wtap.h.
[metze/wireshark/wip.git] / ui / qt / compiled_filter_output.cpp
1 /* compiled_filter_output.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22
23 #include <ui_compiled_filter_output.h>
24 #include "compiled_filter_output.h"
25
26 #include <pcap.h>
27
28 #include "capture_opts.h"
29 #include <wiretap/wtap.h>
30 #include "ui/capture_globals.h"
31
32 #include "wireshark_application.h"
33
34 #include <QClipboard>
35 #include <QPushButton>
36
37 CompiledFilterOutput::CompiledFilterOutput(QWidget *parent, QStringList &intList, QString &compile_filter) :
38     QDialog(parent),
39     intList_(intList),
40     compile_filter_(compile_filter),
41     ui(new Ui::CompiledFilterOutput)
42 {
43     ui->setupUi(this);
44     setAttribute(Qt::WA_DeleteOnClose, true);
45     ui->filterList->setCurrentFont(wsApp->monospaceFont());
46
47     copy_bt_ = ui->buttonBox->addButton(tr("Copy"), QDialogButtonBox::ActionRole);
48     copy_bt_->setToolTip(tr("Copy filter text to the clipboard."));
49     connect(copy_bt_, SIGNAL(clicked()), this, SLOT(copyFilterText()));
50
51     QPushButton *close_bt = ui->buttonBox->button(QDialogButtonBox::Close);
52     close_bt->setDefault(true);
53
54     interface_list_ = ui->interfaceList;
55 #if GLIB_CHECK_VERSION(2,31,0)
56     pcap_compile_mtx = g_new(GMutex,1);
57     g_mutex_init(pcap_compile_mtx);
58 #else
59     pcap_compile_mtx = g_mutex_new();
60 #endif
61     compileFilter();
62 }
63
64 CompiledFilterOutput::~CompiledFilterOutput()
65 {
66     // For some reason closing this dialog either lowers the Capture Interfaces dialog
67     // or raises the main window. Work around the problem for now by manually raising
68     // and activating our parent (presumably the Capture Interfaces dialog).
69     if (parentWidget()) {
70         parentWidget()->raise();
71         parentWidget()->activateWindow();
72     }
73     delete ui;
74 }
75
76 void CompiledFilterOutput::compileFilter()
77 {
78     struct bpf_program fcode;
79
80     foreach (QString interfaces, intList_) {
81         for (guint i = 0; i < global_capture_opts.all_ifaces->len; i++) {
82             interface_t device = g_array_index(global_capture_opts.all_ifaces, interface_t, i);
83
84             if (interfaces.compare(device.display_name)) {
85                 continue;
86             } else {
87                 pcap_t *pd = pcap_open_dead(device.active_dlt, WTAP_MAX_PACKET_SIZE);
88                 g_mutex_lock(pcap_compile_mtx);
89                 if (pcap_compile(pd, &fcode, compile_filter_.toUtf8().constData(), 1, 0) < 0) {
90                     compile_results.insert(interfaces, QString("%1").arg(g_strdup(pcap_geterr(pd))));
91                     g_mutex_unlock(pcap_compile_mtx);
92                     ui->interfaceList->addItem(new QListWidgetItem(QIcon(":expert/expert_error.png"),interfaces));
93                 } else {
94                     GString *bpf_code_dump = g_string_new("");
95                     struct bpf_insn *insn = fcode.bf_insns;
96                     int ii, n = fcode.bf_len;
97                     gchar *bpf_code_str;
98                     for (ii = 0; ii < n; ++insn, ++ii) {
99                         g_string_append(bpf_code_dump, bpf_image(insn, ii));
100                         g_string_append(bpf_code_dump, "\n");
101                     }
102                     bpf_code_str = g_string_free(bpf_code_dump, FALSE);
103                     g_mutex_unlock(pcap_compile_mtx);
104                     compile_results.insert(interfaces, QString("%1").arg(g_strdup(bpf_code_str)));
105                     ui->interfaceList->addItem(new QListWidgetItem(interfaces));
106                 }
107                 break;
108             }
109         }
110     }
111 }
112
113 void CompiledFilterOutput::on_interfaceList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *)
114 {
115     QString interface = current->text();
116     QHash<QString, QString>::const_iterator iter = compile_results.find(interface);
117     ui->filterList->clear();
118     ui->filterList->setPlainText(iter.value());
119 }
120
121 void CompiledFilterOutput::copyFilterText()
122 {
123     wsApp->clipboard()->setText(ui->filterList->toPlainText());
124 }
125
126 //
127 // Editor modelines  -  http://www.wireshark.org/tools/modelines.html
128 //
129 // Local variables:
130 // c-basic-offset: 4
131 // tab-width: 4
132 // indent-tabs-mode: nil
133 // End:
134 //
135 // vi: set shiftwidth=4 tabstop=4 expandtab:
136 // :indentSize=4:tabSize=4:noTabs=true:
137 //
138