Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / qt / filter_action.cpp
blob3268b47ddce2dedf31421929fffcf801aad6b8fc
1 /* filter_action.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "filter_action.h"
12 #include <ui/qt/main_application.h>
13 #include <ui/qt/main_window.h>
15 #include <QClipboard>
16 #include <QMenu>
18 FilterAction::FilterAction(QObject *parent, FilterAction::Action action, FilterAction::ActionType type, QString actionName) :
19 QAction(parent),
20 action_(action),
21 type_(type),
22 actionName_(actionName)
24 setText(actionName);
27 FilterAction::FilterAction(QObject *parent, FilterAction::Action action, FilterAction::ActionType type, FilterAction::ActionDirection direction) :
28 QAction(parent),
29 action_(action),
30 type_(type),
31 direction_(direction)
33 setText(actionDirectionName(direction));
36 FilterAction::FilterAction(QObject *parent, FilterAction::Action action, FilterAction::ActionType type) :
37 QAction(parent),
38 action_(action),
39 type_(type),
40 direction_(ActionDirectionAToAny)
42 setText(actionTypeName(type));
45 FilterAction::FilterAction(QObject *parent, FilterAction::Action action) :
46 QAction(parent),
47 action_(action),
48 type_(ActionTypePlain),
49 direction_(ActionDirectionAToAny)
51 setText(actionName(action));
54 const QList<FilterAction::Action> FilterAction::actions() {
55 static const QList<Action> actions_ = QList<Action>()
56 << ActionApply
57 << ActionPrepare
58 << ActionFind
59 << ActionColorize
60 << ActionWebLookup
61 << ActionCopy;
62 return actions_;
65 const QString FilterAction::actionName(Action action) {
66 switch (action) {
67 case ActionApply:
68 return QObject::tr("Apply as Filter");
69 case ActionPrepare:
70 return QObject::tr("Prepare as Filter");
71 case ActionFind:
72 return QObject::tr("Find");
73 case ActionColorize:
74 return QObject::tr("Colorize");
75 case ActionWebLookup:
76 return QObject::tr("Look Up");
77 case ActionCopy:
78 return QObject::tr("Copy");
79 default:
80 return QObject::tr("UNKNOWN");
85 const QList<FilterAction::ActionType> FilterAction::actionTypes(Action filter_action)
87 static const QList<ActionType> action_types_ = QList<ActionType>()
88 << ActionTypePlain
89 << ActionTypeNot
90 << ActionTypeAnd
91 << ActionTypeOr
92 << ActionTypeAndNot
93 << ActionTypeOrNot;
95 static const QList<ActionType> simple_action_types_ = QList<ActionType>()
96 << ActionTypePlain
97 << ActionTypeNot;
99 switch (filter_action) {
100 case ActionFind:
101 case ActionColorize:
102 return simple_action_types_;
103 default:
104 break;
107 return action_types_;
110 const QString FilterAction::actionTypeName(ActionType type) {
111 switch (type) {
112 case ActionTypePlain:
113 return QObject::tr("Selected");
114 case ActionTypeNot:
115 return QObject::tr("Not Selected");
116 case ActionTypeAnd:
117 return QObject::tr("…and Selected");
118 case ActionTypeOr:
119 return QObject::tr("…or Selected");
120 case ActionTypeAndNot:
121 return QObject::tr("…and not Selected");
122 case ActionTypeOrNot:
123 return QObject::tr("…or not Selected");
124 default:
125 return QObject::tr("UNKNOWN");
130 const QList<FilterAction::ActionDirection> FilterAction::actionDirections()
132 static const QList<FilterAction::ActionDirection> action_directions_ = QList<ActionDirection>()
133 << ActionDirectionAToFromB
134 << ActionDirectionAToB
135 << ActionDirectionAFromB
136 << ActionDirectionAToFromAny
137 << ActionDirectionAToAny
138 << ActionDirectionAFromAny
139 << ActionDirectionAnyToFromB
140 << ActionDirectionAnyToB
141 << ActionDirectionAnyFromB;
142 return action_directions_;
145 const QString FilterAction::actionDirectionName(ActionDirection direction) {
146 switch (direction) {
147 case ActionDirectionAToFromB:
148 return QObject::tr("A " UTF8_LEFT_RIGHT_ARROW " B");
149 case ActionDirectionAToB:
150 return QObject::tr("A " UTF8_RIGHTWARDS_ARROW " B");
151 case ActionDirectionAFromB:
152 return QObject::tr("B " UTF8_RIGHTWARDS_ARROW " A");
153 case ActionDirectionAToFromAny:
154 return QObject::tr("A " UTF8_LEFT_RIGHT_ARROW " Any");
155 case ActionDirectionAToAny:
156 return QObject::tr("A " UTF8_RIGHTWARDS_ARROW " Any");
157 case ActionDirectionAFromAny:
158 return QObject::tr("Any " UTF8_RIGHTWARDS_ARROW " A");
159 case ActionDirectionAnyToFromB:
160 return QObject::tr("Any " UTF8_LEFT_RIGHT_ARROW " B");
161 case ActionDirectionAnyToB:
162 return QObject::tr("Any " UTF8_RIGHTWARDS_ARROW " B");
163 case ActionDirectionAnyFromB:
164 return QObject::tr("B " UTF8_RIGHTWARDS_ARROW " Any");
165 default:
166 return QObject::tr("UNKNOWN");
170 QActionGroup * FilterAction::createFilterGroup(QString filter, bool prepare, bool enabled, QWidget * parent)
172 if (filter.isEmpty())
173 enabled = false;
175 bool filterEmpty = false;
176 if (mainApp)
178 QWidget * mainWin = mainApp->mainWindow();
179 if (qobject_cast<MainWindow *>(mainWin))
180 filterEmpty = qobject_cast<MainWindow *>(mainWin)->getFilter().isEmpty();
183 FilterAction * filterAction = new FilterAction(parent, prepare ? FilterAction::ActionPrepare : FilterAction::ActionApply);
185 QActionGroup * group = new QActionGroup(parent);
186 group->setProperty("filter", filter);
187 group->setProperty("filterAction", prepare ? FilterAction::ActionPrepare : FilterAction::ActionApply);
188 QAction * action = group->addAction(tr("Selected"));
189 action->setProperty("filterType", FilterAction::ActionTypePlain);
190 action = group->addAction(tr("Not Selected"));
191 action->setProperty("filterType", FilterAction::ActionTypeNot);
192 action = group->addAction(tr("…and Selected"));
193 action->setProperty("filterType", FilterAction::ActionTypeAnd);
194 action->setEnabled(!filterEmpty);
195 action = group->addAction(tr("…or Selected"));
196 action->setProperty("filterType", FilterAction::ActionTypeOr);
197 action->setEnabled(!filterEmpty);
198 action = group->addAction(tr("…and not Selected"));
199 action->setProperty("filterType", FilterAction::ActionTypeAndNot);
200 action->setEnabled(!filterEmpty);
201 action = group->addAction(tr("…or not Selected"));
202 action->setProperty("filterType", FilterAction::ActionTypeOrNot);
203 action->setEnabled(!filterEmpty);
204 group->setEnabled(enabled);
205 if (! filter.isEmpty())
206 connect(group, &QActionGroup::triggered, filterAction, &FilterAction::groupTriggered);
208 return group;
211 QMenu * FilterAction::createFilterMenu(FilterAction::Action act, QString filter, bool enabled, QWidget * par)
213 QString title = (act == FilterAction::ActionApply) ? QObject::tr("Apply as Filter") : QObject::tr("Prepare as Filter");
214 bool prepare = (act == FilterAction::ActionApply) ? false : true;
216 QMenu * submenu = new QMenu(title, par);
217 if (filter.length() > 0)
219 int one_em = submenu->fontMetrics().height();
220 QString prep_text = QStringLiteral("%1: %2").arg(title).arg(filter);
221 prep_text = submenu->fontMetrics().elidedText(prep_text, Qt::ElideRight, one_em * 40);
222 QAction * comment = submenu->addAction(prep_text);
223 comment->setEnabled(false);
224 submenu->addSeparator();
226 QActionGroup * group = FilterAction::createFilterGroup(filter, prepare, enabled, par);
227 submenu->addActions(group->actions());
229 return submenu;
232 void FilterAction::groupTriggered(QAction * action)
234 if (action && mainApp)
236 if (action->property("filterType").canConvert<FilterAction::ActionType>() &&
237 sender()->property("filterAction").canConvert<FilterAction::Action>())
239 FilterAction::Action act = sender()->property("filterAction").value<FilterAction::Action>();
240 FilterAction::ActionType type = action->property("filterType").value<FilterAction::ActionType>();
241 QString filter = sender()->property("filter").toString();
243 QWidget * mainWin = mainApp->mainWindow();
244 if (qobject_cast<MainWindow *>(mainWin))
246 MainWindow * mw = qobject_cast<MainWindow *>(mainWin);
247 mw->setDisplayFilter(filter, act, type);
253 QAction * FilterAction::copyFilterAction(QString filter, QWidget *par)
255 FilterAction * filterAction = new FilterAction(par, ActionCopy);
256 QAction * action = new QAction(QObject::tr("Copy"), par);
257 action->setProperty("filter", QVariant::fromValue(filter));
258 connect(action, &QAction::triggered, filterAction, &FilterAction::copyActionTriggered);
260 if (filter.isEmpty())
261 action->setEnabled(false);
263 return action;
266 void FilterAction::copyActionTriggered()
268 QAction * sendAction = qobject_cast<QAction *>(sender());
269 if (! sendAction)
270 return;
272 QString filter = sendAction->property("filter").toString();
273 if (filter.length() > 0)
274 mainApp->clipboard()->setText(filter);