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
|
/* field_filter_edit.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 "config.h"
#include <glib.h>
#include <epan/dfilter/dfilter.h>
#include <filter_files.h>
#include <wsutil/utf8_entities.h>
#include "field_filter_edit.h"
#include "filter_dialog.h"
#include "stock_icon_tool_button.h"
#include "syntax_line_edit.h"
#include <QAction>
#include <QAbstractItemView>
#include <QComboBox>
#include <QCompleter>
#include <QEvent>
#include <QMenu>
#include <QMessageBox>
#include <QPainter>
#include <QStringListModel>
#include <wsutil/utf8_entities.h>
// To do:
// - Get rid of shortcuts and replace them with "n most recently applied filters"?
// - We need simplified (button- and dropdown-free) versions for use in dialogs and field-only checking.
// - Add a separator or otherwise distinguish between recent items and fields
// in the completion dropdown.
#ifdef __APPLE__
#define DEFAULT_MODIFIER UTF8_PLACE_OF_INTEREST_SIGN
#else
#define DEFAULT_MODIFIER "Ctrl-"
#endif
// proto.c:fld_abbrev_chars
static const QString fld_abbrev_chars_ = "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
FieldFilterEdit::FieldFilterEdit(QWidget *parent) :
SyntaxLineEdit(parent),
save_action_(NULL),
remove_action_(NULL)
{
setAccessibleName(tr("Display filter entry"));
completion_model_ = new QStringListModel(this);
setCompleter(new QCompleter(completion_model_, this));
setCompletionTokenChars(fld_abbrev_chars_);
setDefaultPlaceholderText();
// DFCombo
// Bookmark
// DisplayFilterEdit
// Clear button
// Apply (right arrow)
// Combo drop-down
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(checkFilter(const QString&)));
// connect(this, SIGNAL(returnPressed()), this, SLOT(applyDisplayFilter()));
}
void FieldFilterEdit::setDefaultPlaceholderText()
{
placeholder_text_ = QString(tr("Enter a field %1")).arg(UTF8_HORIZONTAL_ELLIPSIS);
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
setPlaceholderText(placeholder_text_);
#endif
}
void FieldFilterEdit::paintEvent(QPaintEvent *evt) {
SyntaxLineEdit::paintEvent(evt);
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
// http://wiki.forum.nokia.com/index.php/Custom_QLineEdit
if (text().isEmpty() && ! this->hasFocus()) {
QPainter p(this);
QFont f = font();
f.setItalic(true);
p.setFont(f);
QColor color(palette().color(foregroundRole()));
color.setAlphaF(0.5);
p.setPen(color);
QStyleOptionFrame opt;
initStyleOption(&opt);
QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this);
cr.setLeft(cr.left() + 2);
cr.setRight(cr.right() - 2);
p.drawText(cr, Qt::AlignLeft|Qt::AlignVCenter, placeholder_text_);
}
// else check filter syntax and set the background accordingly
// XXX - Should we add little warning/error icons as well?
#endif // QT < 4.7
}
void FieldFilterEdit::focusOutEvent(QFocusEvent *event)
{
if (syntaxState() == Valid)
emit popFilterSyntaxStatus();
SyntaxLineEdit::focusOutEvent(event);
}
bool FieldFilterEdit::checkFilter()
{
checkFilter(text());
return syntaxState() != Invalid;
}
void FieldFilterEdit::checkFilter(const QString& filter_text)
{
popFilterSyntaxStatus();
checkDisplayFilter(filter_text);
switch (syntaxState()) {
case Deprecated:
{
emit pushFilterSyntaxWarning(syntaxErrorMessage());
break;
}
case Invalid:
{
QString invalidMsg(tr("Invalid filter: "));
invalidMsg.append(syntaxErrorMessage());
emit pushFilterSyntaxStatus(invalidMsg);
break;
}
default:
break;
}
}
// GTK+ behavior:
// - Operates on words (proto.c:fld_abbrev_chars).
// - Popup appears when you enter or remove text.
// Our behavior:
// - Operates on words (fld_abbrev_chars_).
// - Popup appears when you enter or remove text.
// - Popup appears when you move the cursor.
// - Popup does not appear when text is selected.
// - Recent and saved display filters in popup when editing first word.
// ui/gtk/filter_autocomplete.c:build_autocompletion_list
void FieldFilterEdit::buildCompletionList(const QString &field_word)
{
// Push a hint about the current field.
if (syntaxState() == Valid) {
emit popFilterSyntaxStatus();
header_field_info *hfinfo = proto_registrar_get_byname(field_word.toUtf8().constData());
if (hfinfo) {
QString cursor_field_msg = QString("%1: %2")
.arg(hfinfo->name)
.arg(ftype_pretty_name(hfinfo->type));
emit pushFilterSyntaxStatus(cursor_field_msg);
}
}
if (field_word.length() < 1) {
completion_model_->setStringList(QStringList());
return;
}
void *proto_cookie;
QStringList field_list;
int field_dots = field_word.count('.'); // Some protocol names (_ws.expert) contain periods.
for (int proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie)) {
protocol_t *protocol = find_protocol_by_id(proto_id);
if (!proto_is_protocol_enabled(protocol)) continue;
// Don't complete the current word.
const QString pfname = proto_get_protocol_filter_name(proto_id);
if (field_word.compare(pfname)) field_list << pfname;
// Add fields only if we're past the protocol name and only for the
// current protocol.
if (field_dots > pfname.count('.')) {
void *field_cookie;
const QByteArray fw_ba = field_word.toUtf8(); // or toLatin1 or toStdString?
const char *fw_utf8 = fw_ba.constData();
gsize fw_len = (gsize) strlen(fw_utf8);
for (header_field_info *hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie)) {
if (hfinfo->same_name_prev_id != -1) continue; // Ignore duplicate names.
if (!g_ascii_strncasecmp(fw_utf8, hfinfo->abbrev, fw_len)) {
if ((gsize) strlen(hfinfo->abbrev) != fw_len) field_list << hfinfo->abbrev;
}
}
}
}
field_list.sort();
completion_model_->setStringList(field_list);
completer()->setCompletionPrefix(field_word);
}
void FieldFilterEdit::clearFilter()
{
clear();
QString new_filter;
emit filterPackets(new_filter, true);
}
void FieldFilterEdit::applyDisplayFilter()
{
if (syntaxState() == Invalid) {
return;
}
QString new_filter = text();
emit filterPackets(new_filter, true);
}
void FieldFilterEdit::changeEvent(QEvent* event)
{
if (0 != event)
{
switch (event->type())
{
case QEvent::LanguageChange:
setDefaultPlaceholderText();
break;
default:
break;
}
}
SyntaxLineEdit::changeEvent(event);
}
void FieldFilterEdit::showFilters()
{
FilterDialog display_filter_dlg(window(), FilterDialog::DisplayFilter);
display_filter_dlg.exec();
}
void FieldFilterEdit::prepareFilter()
{
QAction *pa = qobject_cast<QAction*>(sender());
if (!pa || pa->data().toString().isEmpty()) return;
setText(pa->data().toString());
}
/*
* 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:
*/
|