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
10 #include "filter_action.h"
12 #include <ui/qt/main_application.h>
13 #include <ui/qt/main_window.h>
18 FilterAction::FilterAction(QObject
*parent
, FilterAction::Action action
, FilterAction::ActionType type
, QString actionName
) :
22 actionName_(actionName
)
27 FilterAction::FilterAction(QObject
*parent
, FilterAction::Action action
, FilterAction::ActionType type
, FilterAction::ActionDirection direction
) :
33 setText(actionDirectionName(direction
));
36 FilterAction::FilterAction(QObject
*parent
, FilterAction::Action action
, FilterAction::ActionType type
) :
40 direction_(ActionDirectionAToAny
)
42 setText(actionTypeName(type
));
45 FilterAction::FilterAction(QObject
*parent
, FilterAction::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
>()
65 const QString
FilterAction::actionName(Action action
) {
68 return QObject::tr("Apply as Filter");
70 return QObject::tr("Prepare as Filter");
72 return QObject::tr("Find");
74 return QObject::tr("Colorize");
76 return QObject::tr("Look Up");
78 return QObject::tr("Copy");
80 return QObject::tr("UNKNOWN");
85 const QList
<FilterAction::ActionType
> FilterAction::actionTypes(Action filter_action
)
87 static const QList
<ActionType
> action_types_
= QList
<ActionType
>()
95 static const QList
<ActionType
> simple_action_types_
= QList
<ActionType
>()
99 switch (filter_action
) {
102 return simple_action_types_
;
107 return action_types_
;
110 const QString
FilterAction::actionTypeName(ActionType type
) {
112 case ActionTypePlain
:
113 return QObject::tr("Selected");
115 return QObject::tr("Not Selected");
117 return QObject::tr("…and Selected");
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");
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
) {
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");
166 return QObject::tr("UNKNOWN");
170 QActionGroup
* FilterAction::createFilterGroup(QString filter
, bool prepare
, bool enabled
, QWidget
* parent
)
172 if (filter
.isEmpty())
175 bool filterEmpty
= false;
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
);
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());
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);
266 void FilterAction::copyActionTriggered()
268 QAction
* sendAction
= qobject_cast
<QAction
*>(sender());
272 QString filter
= sendAction
->property("filter").toString();
273 if (filter
.length() > 0)
274 mainApp
->clipboard()->setText(filter
);