Fix comment end after SPDX identifier
[metze/wireshark/wip.git] / ui / qt / widgets / capture_filter_combo.cpp
1 /* capture_filter_combo.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include <stdio.h>
11
12 #include <ui/qt/utils/qt_ui_utils.h>
13 #include "ui/recent_utils.h"
14 #include "ui/recent.h"
15
16 #include <epan/prefs.h>
17
18 #include <ui/qt/widgets/capture_filter_combo.h>
19 #include "wireshark_application.h"
20
21 CaptureFilterCombo::CaptureFilterCombo(QWidget *parent, bool plain) :
22     QComboBox(parent),
23     cf_edit_(NULL)
24 {
25     cf_edit_ = new CaptureFilterEdit(this, plain);
26
27     setEditable(true);
28     // Enabling autocompletion here gives us two simultaneous completions:
29     // Inline (highlighted text) for entire filters, handled here and popup
30     // completion for fields handled by CaptureFilterEdit.
31     setAutoCompletion(false);
32     setLineEdit(cf_edit_);
33     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
34     setInsertPolicy(QComboBox::NoInsert);
35     setAccessibleName(tr("Capture filter selector"));
36     setStyleSheet(
37             "QComboBox {"
38 #ifdef Q_OS_MAC
39             "  border: 1px solid gray;"
40 #else
41             "  border: 1px solid palette(shadow);"
42 #endif
43             "  border-radius: 3px;"
44             "  padding: 0px 0px 0px 0px;"
45             "  margin-left: 0px;"
46             "  min-width: 20em;"
47             " }"
48
49             "QComboBox::drop-down {"
50             "  subcontrol-origin: padding;"
51             "  subcontrol-position: top right;"
52             "  width: 16px;"
53             "  border-left-width: 0px;"
54             " }"
55
56             "QComboBox::down-arrow {"
57             "  image: url(:/stock_icons/14x14/x-filter-dropdown.png);"
58             " }"
59
60             "QComboBox::down-arrow:on { /* shift the arrow when popup is open */"
61             "  top: 1px;"
62             "  left: 1px;"
63             "}"
64             );
65
66     connect(this, SIGNAL(interfacesChanged()), cf_edit_, SLOT(checkFilter()));
67     connect(cf_edit_, SIGNAL(pushFilterSyntaxStatus(const QString&)),
68             this, SIGNAL(pushFilterSyntaxStatus(const QString&)));
69     connect(cf_edit_, SIGNAL(popFilterSyntaxStatus()),
70             this, SIGNAL(popFilterSyntaxStatus()));
71     connect(cf_edit_, SIGNAL(captureFilterSyntaxChanged(bool)),
72             this, SIGNAL(captureFilterSyntaxChanged(bool)));
73     connect(cf_edit_, SIGNAL(startCapture()), this, SIGNAL(startCapture()));
74     connect(cf_edit_, SIGNAL(startCapture()), this, SLOT(saveAndRebuildFilterList()));
75     connect(wsApp, SIGNAL(appInitialized()), this, SLOT(rebuildFilterList()));
76     connect(wsApp, SIGNAL(preferencesChanged()), this, SLOT(rebuildFilterList()));
77
78     rebuildFilterList();
79     clearEditText();
80 }
81
82 void CaptureFilterCombo::writeRecent(FILE *rf)
83 {
84     int i;
85
86     for (i = 0; i < count(); i++) {
87         const QByteArray& filter = itemText(i).toUtf8();
88         if (!filter.isEmpty()) {
89             fprintf(rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", filter.constData());
90         }
91     }
92 }
93
94 void CaptureFilterCombo::saveAndRebuildFilterList()
95 {
96     if (!currentText().isEmpty()) {
97         recent_add_cfilter(NULL, currentText().toUtf8().constData());
98     }
99     rebuildFilterList();
100 }
101
102 void CaptureFilterCombo::rebuildFilterList()
103 {
104     lineEdit()->blockSignals(true);
105     GList *cfilter_list = recent_get_cfilter_list(NULL);
106     QString cur_filter = currentText();
107     clear();
108     for (GList *li = g_list_first(cfilter_list); li != NULL; li = g_list_next(li)) {
109         insertItem(0, (const gchar *) li->data);
110     }
111     lineEdit()->setText(cur_filter);
112     lineEdit()->blockSignals(false);
113 }
114
115 /*
116  * Editor modelines
117  *
118  * Local Variables:
119  * c-basic-offset: 4
120  * tab-width: 8
121  * indent-tabs-mode: nil
122  * End:
123  *
124  * ex: set shiftwidth=4 tabstop=8 expandtab:
125  * :indentSize=4:tabSize=8:noTabs=true:
126  */