Bug Summary

File:builds/wireshark/wireshark/ui/qt/main_status_bar.cpp
Warning:line 724, column 9
Potential leak of memory pointed to by 'placer'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main_status_bar.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/build/ui/qt -isystem /builds/wireshark/wireshark/ui/qt -isystem /builds/wireshark/wireshark/ui/qt/lua_debugger -isystem /usr/include/x86_64-linux-gnu/qt6/QtConcurrent -isystem /usr/include/x86_64-linux-gnu/qt6 -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt6/mkspecs/linux-g++ -isystem /usr/include/x86_64-linux-gnu/qt6/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport -isystem /usr/include/x86_64-linux-gnu/qt6/QtGui -isystem /usr/include/x86_64-linux-gnu/qt6/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt6/QtSvg -isystem /usr/include/x86_64-linux-gnu/qt6/QtMultimedia -isystem /usr/include/x86_64-linux-gnu/qt6/QtDBus -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D QT_CONCURRENT_LIB -D QT_CORE_LIB -D QT_DBUS_LIB -D QT_GUI_LIB -D QT_MULTIMEDIA_LIB -D QT_NETWORK_LIB -D QT_PRINTSUPPORT_LIB -D QT_SVG_LIB -D QT_WIDGETS_LIB -D WS_DEBUG -D WS_DEBUG_UTF_8 -I /builds/wireshark/wireshark/build/ui/qt/qtui_autogen/include -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=c++17 -fdeprecated-macro -ferror-limit 19 -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-25-100324-3661-1 -x c++ /builds/wireshark/wireshark/ui/qt/main_status_bar.cpp
1/* main_status_bar.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 "config.h"
11
12#include "file.h"
13
14#include <epan/expert.h>
15#include <epan/prefs.h>
16#include <epan/tag_rules.h>
17
18#include <wsutil/filesystem.h>
19#include <wsutil/utf8_entities.h>
20
21#include "ui/main_statusbar.h"
22#include <ui/qt/utils/qt_ui_utils.h>
23#include <ui/qt/main_window.h>
24
25#include "capture_file.h"
26#include "main_status_bar.h"
27#include "profile_dialog.h"
28#include <ui/qt/utils/color_utils.h>
29#include <ui/qt/utils/font_manager.h>
30#include <ui/qt/utils/theme_styler.h>
31#include <ui/qt/utils/zoom_scaling.h>
32#include <ui/qt/utils/themes/contrast_adapt_icon.h>
33#include <ui/qt/utils/themes/themed_icon.h>
34#include <ui/qt/capture_file.h>
35#include <ui/qt/widgets/clickable_label.h>
36#include <ui/recent.h>
37
38#include <QAction>
39#include <QHBoxLayout>
40#include <QMenu>
41#include <QScreen>
42#include <QSplitter>
43#include <QToolButton>
44#include <QLatin1Char>
45
46#include <functional>
47
48// To do:
49// - Use the CaptureFile class.
50
51// XXX - The GTK+ code assigns priorities to these and pushes/pops accordingly.
52
53Q_DECLARE_METATYPE(ProfileDialog::ProfileAction)template <> struct QMetaTypeId< ProfileDialog::ProfileAction
> { enum { Defined = 1 }; static_assert(QtPrivate::checkTypeIsSuitableForMetaType
<ProfileDialog::ProfileAction>()); static int qt_metatype_id
() { [[clang::require_constant_initialization]] static QBasicAtomicInt
metatype_id = { 0 }; if (const int id = metatype_id.loadAcquire
()) return id; constexpr auto arr = QtPrivate::typenameHelper
<ProfileDialog::ProfileAction>(); auto name = arr.data(
); if (QByteArrayView(name) == ("ProfileDialog::ProfileAction"
)) { const int id = qRegisterNormalizedMetaType<ProfileDialog
::ProfileAction>(name); metatype_id.storeRelease(id); return
id; } const int newId = qRegisterMetaType< ProfileDialog::
ProfileAction >("ProfileDialog::ProfileAction"); metatype_id
.storeRelease(newId); return newId; } };
54
55// The profile menu opens near the right edge of the screen. Without room on
56// the right, Qt opens a submenu on the left, but its submenus on the right
57// again, on top of the menus they were opened from. Once a submenu has been
58// opened on the left, keep opening its submenus on the left.
59class SubMenuPlacer : public QObject
60{
61public:
62 explicit SubMenuPlacer(QObject *parent) : QObject(parent) {}
63
64protected:
65 bool eventFilter(QObject *obj, QEvent *event) override
66 {
67 QMenu *menu = qobject_cast<QMenu *>(obj);
68 if (event->type() != QEvent::Show || !menu)
69 return false;
70
71 QMenu *parentMenu = qobject_cast<QMenu *>(menu->parentWidget());
72 QMenu *grandMenu = parentMenu ? qobject_cast<QMenu *>(parentMenu->parentWidget()) : Q_NULLPTRnullptr;
73 if (!grandMenu || parentMenu->x() >= grandMenu->x() || menu->x() < parentMenu->x())
74 return false;
75
76 QPoint pos(parentMenu->x() - menu->width(), menu->y());
77 if (menu->screen() && pos.x() >= menu->screen()->availableGeometry().left())
78 menu->move(pos);
79 return false;
80 }
81};
82
83// If we ever add support for multiple windows this will need to be replaced.
84// See also: main_window.cpp
85static MainStatusBar *cur_main_status_bar_;
86
87/*
88 * Push a formatted temporary message onto the statusbar.
89 */
90void
91statusbar_push_temporary_msg(const char *msg_format, ...)
92{
93 va_list ap;
94 QString push_msg;
95
96 if (!cur_main_status_bar_) return;
97
98 va_start(ap, msg_format)__builtin_va_start(ap, msg_format);
99 push_msg = QString::vasprintf(msg_format, ap);
100 va_end(ap)__builtin_va_end(ap);
101
102 mainApp->pushStatus(WiresharkApplication::TemporaryStatus, push_msg);
103}
104
105/*
106 * Update the packets statusbar to the current values
107 */
108void
109packets_bar_update(void)
110{
111 if (!cur_main_status_bar_) return;
112
113 cur_main_status_bar_->updateCaptureStatistics(NULL__null);
114}
115
116MainStatusBar::MainStatusBar(QWidget *parent) :
117 QStatusBar(parent),
118 cap_file_(NULL__null),
119 cs_fixed_(false),
120 cs_count_(0)
121{
122 QSplitter *splitter = new QSplitter(this);
123 QWidget *info_progress = new QWidget(this);
124 info_progress_hb_ = new QHBoxLayout(info_progress);
125
126#if defined(Q_OS_WIN)
127 // Handles are the same color as widgets, at least on Windows 7.
128 splitter->setHandleWidth(3);
129 splitter->setStyleSheet(QStringLiteral((QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
130 "QSplitter::handle {"(QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
131 " border-left: 1px solid palette(mid);"(QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
132 " border-right: 1px solid palette(mid);"(QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
133 "}"(QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
134 )(QString(QtPrivate::qMakeStringPrivate(u"" "QSplitter::handle {"
" border-left: 1px solid palette(mid);" " border-right: 1px solid palette(mid);"
"}")))
);
135#endif
136
137#ifdef Q_OS_MACOS
138 profile_status_.setAttribute(Qt::WA_MacSmallSize, true);
139#endif
140
141 QString button_ss =
142 "QToolButton {"
143 " border: none;"
144 " background: transparent;" // Disables platform style on Windows.
145 " padding: 0px;"
146 " margin: 0px;"
147 "}";
148
149 QString label_ss =
150 "QLabel {"
151 " margin-left: 0.5em;"
152 "}";
153
154 expert_button_ = new QToolButton(this);
155 expert_button_->setStyleSheet(button_ss);
156 expert_button_->setAccessibleName(tr("Expert information"));
157 expert_button_->setAccessibleDescription(tr("Opens the expert information dialog, showing errors, warnings, and other relevant information about the capture."));
158 expert_button_->hide();
159
160 // We just want a clickable image. Using a QPushButton or QToolButton would require
161 // a lot of adjustment.
162 ThemedIcon comment_icon(QStringLiteral(":/svg_icons/capture-comment.svg")(QString(QtPrivate::qMakeStringPrivate(u"" ":/svg_icons/capture-comment.svg"
)))
, ThemeManager::PaletteText);
163 comment_button_ = new QToolButton(this);
164 comment_button_->setIcon(comment_icon);
165 comment_button_->setStyleSheet(button_ss);
166 comment_button_->setAccessibleName(tr("Capture comment"));
167 comment_button_->setAccessibleDescription(tr("Opens the Capture File Properties dialog to view or edit capture-level comments."));
168
169 comment_button_->setToolTip(tr("Open the Capture File Properties dialog"));
170 comment_button_->setEnabled(false);
171 connect(expert_button_, &QToolButton::clicked, this, &MainStatusBar::showExpertInfo);
172 connect(comment_button_, &QToolButton::clicked, this, &MainStatusBar::editCaptureComment);
173
174 applyIconSizes();
175 connect(FontManager::instance(), &FontManager::zoomChanged, this, &MainStatusBar::applyIconSizes);
176
177 info_status_.setTemporaryContext(STATUS_CTX_TEMPORARY);
178 info_status_.setShrinkable(true);
179 info_status_.setAccessibleName(tr("Information"));
180 info_status_.setAccessibleDescription(tr("Displays general information, status messages, and expert severity details."));
181
182 info_progress_hb_->addWidget(expert_button_);
183 info_progress_hb_->addWidget(comment_button_);
184 info_progress_hb_->addWidget(&info_status_);
185 info_progress_hb_->addWidget(&progress_frame_);
186 info_progress_hb_->addStretch(10);
187
188 // LabelStack does setStyleSheet for QLabel but ClickableLabel does not
189 packet_status_.setAccessibleName(tr("Packet statistics"));
190 packet_status_.setAccessibleDescription(tr("Shows the number of captured, displayed, and selected packets."));
191 profile_status_.setStyleSheet(label_ss);
192 // The profile menus, created in the status bar, have many neighbouring
193 // submenus, which open as the mouse moves over them
194 ThemeStyler::setPlainSubMenus(this);
195 profile_status_.setAccessibleName(tr("Configuration profile"));
196 profile_status_.setAccessibleDescription(tr("Displays the current configuration profile and allows switching between profiles."));
197
198 splitter->addWidget(info_progress);
199 splitter->addWidget(&packet_status_);
200 splitter->addWidget(&profile_status_);
201
202 splitter->setStretchFactor(0, 3);
203 splitter->setStretchFactor(1, 3);
204 splitter->setStretchFactor(2, 1);
205
206 addWidget(splitter, 1);
207
208 cur_main_status_bar_ = this;
209
210 splitter->hide();
211 setReadyMsg();
212 packets_bar_update();
213
214#ifdef QWINTASKBARPROGRESS_H
215 progress_frame_.enableTaskbarUpdates(true);
216#endif
217
218 mainApp->whenInitialized(splitter, [splitter]() { splitter->show(); });
219 mainApp->whenInitialized(this, [this]() { appInitialized(); });
220 connect(&info_status_, &LabelStack::toggleTemporaryFlash, this, &MainStatusBar::toggleBackground);
221 connect(mainApp, &MainApplication::profileNameChanged, this, &MainStatusBar::setProfileName);
222 connect(&profile_status_, &ClickableLabel::clickedAt, this, &MainStatusBar::showProfileMenu);
223
224 connect(&progress_frame_, &ProgressFrame::stopLoading, this, &MainStatusBar::stopLoading);
225}
226
227MainStatusBar::~MainStatusBar()
228{
229
230}
231
232
233void MainStatusBar::showExpert() {
234 expertUpdate();
235}
236
237void MainStatusBar::captureFileClosing() {
238 expert_button_->hide();
239 progress_frame_.captureFileClosing();
240 field_status_supplement_.clear();
241 field_status_include_base_ = true;
242 field_status_base_.clear();
243 popGenericStatus(STATUS_CTX_FIELD);
244}
245
246void MainStatusBar::expertUpdate() {
247 // <img> won't load @2x versions in Qt versions earlier than 5.4.
248 // https://bugreports.qt.io/browse/QTBUG-36383
249 // We might have to switch to a QPushButton.
250 QString stock_name = "expert-";
251 QString tt_text = tr(" is the highest expert information level");
252
253 switch(expert_get_highest_severity()) {
254 case(PI_ERROR0x00800000):
255 stock_name.append("error");
256 tt_text.prepend(tr("ERROR"));
257 break;
258 case(PI_WARN0x00600000):
259 stock_name.append("warn");
260 tt_text.prepend(tr("WARNING"));
261 break;
262 case(PI_NOTE0x00400000):
263 stock_name.append("note");
264 tt_text.prepend(tr("NOTE"));
265 break;
266 case(PI_CHAT0x00200000):
267 stock_name.append("chat");
268 tt_text.prepend(tr("CHAT"));
269 break;
270 default:
271 stock_name.append("none");
272 tt_text = tr("No expert information");
273 break;
274 }
275
276 ContrastAdaptIcon expert_icon(QStringLiteral(":/svg_icons/")(QString(QtPrivate::qMakeStringPrivate(u"" ":/svg_icons/"))) + stock_name.toLatin1() + QStringLiteral(".svg")(QString(QtPrivate::qMakeStringPrivate(u"" ".svg"))),
277 QPalette::Window);
278 applyIconSizes();
279 expert_button_->setIcon(expert_icon);
280 expert_button_->setToolTip(tt_text);
281 expert_button_->show();
282}
283
284// ui/gtk/main_statusbar.c
285void MainStatusBar::setFileName(CaptureFile &cf)
286{
287 if (cf.isValid()) {
288 popGenericStatus(STATUS_CTX_FILE);
289 QString msgtip = QStringLiteral("%1 (%2)")(QString(QtPrivate::qMakeStringPrivate(u"" "%1 (%2)")))
290 .arg(cf.capFile()->filename, file_size_to_qstring(cf.capFile()->f_datalen));
291 pushGenericStatus(STATUS_CTX_FILE, cf.fileName(), msgtip);
292 }
293}
294
295void MainStatusBar::setReadyMsg()
296{
297#ifdef HAVE_LIBPCAP1
298 QString ready_msg(tr("Ready to load or capture"));
299#else
300 QString ready_msg(tr("Ready to load file"));
301#endif
302 info_status_.pushText(std::move(ready_msg), STATUS_CTX_MAIN);
303}
304
305void MainStatusBar::applyIconSizes()
306{
307 // Both indicators match the display filter toolbar icons. They are not
308 // sized from the status text, whose font is smaller on macOS.
309 const int side = ZoomScaling::barIconSize();
310 expert_button_->setIconSize(QSize(side, side));
311 comment_button_->setIconSize(QSize(side, side));
312 info_progress_hb_->setContentsMargins(side / 2, 0, 0, 0);
313}
314
315void MainStatusBar::changeEvent(QEvent *event)
316{
317 if (event->type() == QEvent::LanguageChange) {
318 info_status_.popText(STATUS_CTX_MAIN);
319 setReadyMsg();
320 setStatusbarForCaptureFile();
321 showCaptureStatistics();
322 setProfileName();
323 }
324 if (event->type() == QEvent::FontChange || event->type() == QEvent::ApplicationFontChange) {
325 applyIconSizes();
326 }
327 QStatusBar::changeEvent(event);
328}
329
330void MainStatusBar::setCaptureFile(capture_file *cf)
331{
332 cap_file_ = cf;
333 comment_button_->setEnabled(cap_file_ != NULL__null);
334}
335
336void MainStatusBar::setStatusbarForCaptureFile()
337{
338 if (cap_file_ && cap_file_->filename && (cap_file_->state != FILE_CLOSED)) {
339 popGenericStatus(STATUS_CTX_FILE);
340 QString msgtip = QStringLiteral("%1 (%2)")(QString(QtPrivate::qMakeStringPrivate(u"" "%1 (%2)")))
341 .arg(cap_file_->filename, file_size_to_qstring(cap_file_->f_datalen));
342 pushGenericStatus(STATUS_CTX_FILE,
343 gchar_free_to_qstring(cf_get_display_name(cap_file_)), msgtip);
344 }
345}
346
347void MainStatusBar::setFieldStatusSupplement(const QString &supplement, bool include_selected_field)
348{
349 field_status_supplement_ = supplement;
350 field_status_include_base_ = include_selected_field;
351 refreshFieldStatus();
352}
353
354void MainStatusBar::refreshFieldStatus()
355{
356 QString display;
357 if (!field_status_supplement_.isEmpty()) {
358 display = field_status_supplement_;
359 if (field_status_include_base_ && !field_status_base_.isEmpty()) {
360 display += QLatin1Char(' ') + field_status_base_;
361 }
362 } else {
363 display = field_status_base_;
364 }
365 pushGenericStatus(STATUS_CTX_FIELD, display);
366}
367
368void MainStatusBar::selectedFieldChanged(FieldInformation * finfo)
369{
370 QString item_info;
371
372 if (! finfo) {
373 field_status_base_.clear();
374 refreshFieldStatus();
375 return;
376 }
377
378 FieldInformation::HeaderInfo hInfo = finfo->headerInfo();
379
380 if (hInfo.isValid)
381 {
382 /* For frame.tag items, show the rule's comment in the status bar instead of the blurb. */
383 if (hInfo.abbreviation == "frame.tag") {
384 field_info *fi = finfo->fieldInfo();
385 const char *rule_name = fi ? fvalue_get_string(fi->value) : nullptr;
386 QString comment_str;
387 if (rule_name) {
388 for (const GSList *r = tag_rules_get_list(); r; r = g_slist_next(r)((r) ? (((GSList *)(r))->next) : __null)) {
389 const tag_rule_t *rule = (const tag_rule_t *)r->data;
390 if (strcmp(rule->rule_name, rule_name) == 0) {
391 if (rule->comment && rule->comment[0])
392 comment_str = QString::fromUtf8(rule->comment);
393 break;
394 }
395 }
396 }
397 item_info.append(comment_str.isEmpty() ? hInfo.description : comment_str);
398 } else if (hInfo.description.length() > 0) {
399 item_info.append(hInfo.description);
400 } else {
401 item_info.append(hInfo.name);
402 }
403 }
404
405 if (!item_info.isEmpty()) {
406 int finfo_length;
407 if (hInfo.isValid)
408 item_info.append(" (" + hInfo.abbreviation + ")");
409
410 finfo_length = finfo->position().length + finfo->appendix().length;
411 if (finfo_length > 0) {
412 int finfo_bits = FI_GET_BITS_SIZE(finfo->fieldInfo())(((finfo->fieldInfo()) ? ((finfo->fieldInfo())->flags
& ((((63) & 63) << 12))) : 0) >> 12)
;
413 if (finfo_bits % 8 == 0) {
414 item_info.append(", " + tr("%Ln byte(s)", "", finfo_length));
415 } else {
416 item_info.append(", " + tr("%Ln bit(s)", "", finfo_bits));
417 }
418 }
419 }
420
421 field_status_base_ = item_info;
422 refreshFieldStatus();
423}
424
425void MainStatusBar::highlightedFieldChanged(FieldInformation * finfo)
426{
427 QString hint;
428
429 if (finfo)
430 {
431 FieldInformation::Position pos = finfo->position();
432
433 if (pos.length < 2) {
434 hint = tr("Byte %1").arg(pos.start);
435 } else {
436 hint = tr("Bytes %1-%2").arg(pos.start).arg(pos.start + pos.length - 1);
437 }
438 hint += QStringLiteral(": %1 (%2)")(QString(QtPrivate::qMakeStringPrivate(u"" ": %1 (%2)")))
439 .arg(finfo->headerInfo().name, finfo->headerInfo().abbreviation);
440 }
441
442 pushGenericStatus(STATUS_CTX_BYTE, hint);
443}
444
445void MainStatusBar::pushGenericStatus(StatusContext status, const QString &message, const QString &messagetip)
446{
447 LabelStack * stack = &info_status_;
448
449 if (status == STATUS_CTX_MAIN)
450 stack = &packet_status_;
451
452 if (message.isEmpty() && status != STATUS_CTX_FILE && status != STATUS_CTX_TEMPORARY && status != STATUS_CTX_PROGRESS)
453 popGenericStatus(status);
454 else
455 stack->pushText(message, status, messagetip);
456
457 if (status == STATUS_CTX_FILTER || status == STATUS_CTX_FILE)
458 expertUpdate();
459}
460
461void MainStatusBar::popGenericStatus(StatusContext status)
462{
463 LabelStack * stack = &info_status_;
464
465 if (status == STATUS_CTX_MAIN)
466 stack = &packet_status_;
467
468 stack->popText(status);
469}
470
471void MainStatusBar::setProfileName()
472{
473 profile_status_.setText(tr("Profile: %1").arg(get_profile_name()));
474}
475
476void MainStatusBar::appInitialized()
477{
478 setProfileName();
479 connect(mainApp->mainWindow(), &MainWindow::framesSelected, this, &MainStatusBar::selectedFrameChanged);
480}
481
482void MainStatusBar::selectedFrameChanged(QList<int>)
483{
484 showCaptureStatistics();
485}
486
487void MainStatusBar::showCaptureStatistics()
488{
489 QString packets_str;
490
491 QList<int> rows;
492 MainWindow * mw = mainApp->mainWindow();
493 // Only a single selected packet is shown, so don't build the
494 // list of a large selection.
495 if (mw && !mw->hasSelection()) {
496 rows = mw->selectedRows(true);
497 }
498
499#ifdef HAVE_LIBPCAP1
500 if (cap_file_) {
501 /* Do we have any packets? */
502 if (!cs_fixed_) {
503 cs_count_ = cap_file_->count;
504 }
505 if (cs_count_ > 0) {
506 if (prefs.gui_show_selected_packet && rows.count() == 1) {
507 packets_str.append(tr("Selected Packet: %1 %2 ")
508 .arg(rows.at(0))
509 .arg(UTF8_MIDDLE_DOT"\u00b7"));
510 }
511 packets_str.append(tr("Packets: %1").arg(cs_count_));
512
513 if (cap_file_->dfilter) {
514 packets_str.append(tr(" %1 Displayed: %2 (%3%)")
515 .arg(UTF8_MIDDLE_DOT"\u00b7")
516 .arg(cap_file_->displayed_count)
517 .arg((100.0*cap_file_->displayed_count)/cs_count_, 0, 'f', 1));
518 }
519 if (recent.aggregation_view) {
520 packets_str.append(tr(" %1 Aggregated: %2")
521 .arg(UTF8_MIDDLE_DOT"\u00b7")
522 .arg(cap_file_->aggregation_count));
523 }
524 if (rows.count() > 1) {
525 packets_str.append(tr(" %1 Selected: %2 (%3%)")
526 .arg(UTF8_MIDDLE_DOT"\u00b7")
527 .arg(rows.count())
528 .arg((100.0*rows.count())/cs_count_, 0, 'f', 1));
529 }
530 if (cap_file_->marked_count > 0) {
531 packets_str.append(tr(" %1 Marked: %2 (%3%)")
532 .arg(UTF8_MIDDLE_DOT"\u00b7")
533 .arg(cap_file_->marked_count)
534 .arg((100.0*cap_file_->marked_count)/cs_count_, 0, 'f', 1));
535 }
536 if (cap_file_->drops_known) {
537 packets_str.append(tr(" %1 Dropped: %2 (%3%)")
538 .arg(UTF8_MIDDLE_DOT"\u00b7")
539 .arg(cap_file_->drops)
540 .arg((100.0*cap_file_->drops)/(cs_count_ + cap_file_->drops), 0, 'f', 1));
541 }
542 if (cap_file_->ignored_count > 0) {
543 packets_str.append(tr(" %1 Ignored: %2 (%3%)")
544 .arg(UTF8_MIDDLE_DOT"\u00b7")
545 .arg(cap_file_->ignored_count)
546 .arg((100.0*cap_file_->ignored_count)/cs_count_, 0, 'f', 1));
547 }
548 if (cap_file_->packet_comment_count > 0) {
549 packets_str.append(tr(" %1 Comments: %2")
550 .arg(UTF8_MIDDLE_DOT"\u00b7")
551 .arg(cap_file_->packet_comment_count));
552 }
553 if (prefs.gui_show_file_load_time && !cap_file_->is_tempfile) {
554 /* Loading an existing file */
555 unsigned long computed_elapsed = cf_get_computed_elapsed(cap_file_);
556 packets_str.append(tr(" %1 Load time: %2:%3.%4")
557 .arg(UTF8_MIDDLE_DOT"\u00b7")
558 .arg(computed_elapsed/60000, 2, 10, QLatin1Char('0'))
559 .arg(computed_elapsed%60000/1000, 2, 10, QLatin1Char('0'))
560 .arg(computed_elapsed%1000, 3, 10, QLatin1Char('0')));
561 }
562 }
563 } else if (cs_fixed_ && cs_count_ > 0) {
564 /* There shouldn't be any rows without a cap_file_ but this is benign */
565 if (prefs.gui_show_selected_packet && rows.count() == 1) {
566 packets_str.append(tr("Selected Packet: %1 %2 ")
567 .arg(rows.at(0))
568 .arg(UTF8_MIDDLE_DOT"\u00b7"));
569 }
570 packets_str.append(tr("Packets: %1")
571 .arg(cs_count_));
572 }
573#endif // HAVE_LIBPCAP
574
575 if (packets_str.isEmpty()) {
576 packets_str = tr("No Packets");
577 }
578
579 popGenericStatus(STATUS_CTX_MAIN);
580 pushGenericStatus(STATUS_CTX_MAIN, packets_str);
581}
582
583// These two counts are different in multiple file mode. cap_session->count
584// is the total number in the session across all files; cap_file_->count is
585// count in the current file. Perhaps we could display both in both cases?
586
587void MainStatusBar::updateCaptureStatistics(capture_session *cap_session)
588{
589 cs_fixed_ = false;
590
591#ifndef HAVE_LIBPCAP1
592 Q_UNUSED(cap_session)(void)cap_session;
593#else
594 if ((!cap_session || cap_session->cf == cap_file_) && cap_file_ && cap_file_->count) {
595 cs_count_ = cap_file_->count;
596 } else {
597 cs_count_ = 0;
598 }
599#endif // HAVE_LIBPCAP
600
601 showCaptureStatistics();
602}
603
604void MainStatusBar::updateCaptureFixedStatistics(capture_session *cap_session)
605{
606 cs_fixed_ = true;
607
608#ifndef HAVE_LIBPCAP1
609 Q_UNUSED(cap_session)(void)cap_session;
610#else
611 if (cap_session && cap_session->count) {
612 cs_count_ = cap_session->count;
613 } else {
614 cs_count_ = 0;
615 }
616#endif // HAVE_LIBPCAP
617
618 showCaptureStatistics();
619}
620
621void MainStatusBar::showProfileMenu(const QPoint &global_pos, Qt::MouseButton button)
622{
623 //Use the model object to pull the profile information
624 ProfileModel model;
625 ProfileSortModel sortModel;
626 sortModel.setSourceModel(&model);
627 sortModel.sort(0, Qt::DescendingOrder);
628
629 QMenu * ctx_menu_;
630 QMenu * profile_menu;
631 if (button == Qt::LeftButton) {
1
Assuming 'button' is equal to LeftButton
2
Taking true branch
632 ctx_menu_ = nullptr;
633 profile_menu = new QMenu(this);
634 profile_menu->setAttribute(Qt::WA_DeleteOnClose);
635 } else {
636 ctx_menu_ = new QMenu(this);
637 ctx_menu_->setAttribute(Qt::WA_DeleteOnClose);
638 profile_menu = new QMenu(ctx_menu_);
639 }
640
641 const ProfileItem* currentProfile = model.getCurrentProfile();
642
643 // Add the profiles of one scope, using a submenu for profiles with children
644 std::function<void(QMenu *, const QModelIndex &, bool)> addProfiles;
645 // NOLINTNEXTLINE(misc-no-recursion)
646 addProfiles = [&](QMenu * menu, const QModelIndex &parent, bool global) {
647 for (int i = 0; i < sortModel.rowCount(parent); ++i)
648 {
649 //Go through the proxy model to get the profile information in the right order
650 QModelIndex proxyIndex = sortModel.index(i, 0, parent);
651 const ProfileItem* profile = model.getProfile(sortModel.mapToSource(proxyIndex));
652 if (profile == Q_NULLPTRnullptr || profile->isGlobal() != global)
653 continue;
654
655 /* Check if this profile does not exist as user */
656 bool hidden = global && model.getPersonalProfile(profile->getName()) != Q_NULLPTRnullptr;
657 bool hasChildren = sortModel.rowCount(proxyIndex) > 0;
658 if (hidden && !hasChildren)
659 continue;
660
661 // An ampersand in the menu item's text sets Alt+F as a shortcut for this menu.
662 // Use "&&" to get a real ampersand in the menu bar.
663 QString name = profile->getLeafName();
664 name.replace('&', "&&");
665
666 QMenu * subMenu = hasChildren ? menu->addMenu(name) : menu;
667
668 if (hasChildren)
669 {
670 QFont subMenuFont = subMenu->menuAction()->font();
671 subMenuFont.setItalic(profile->isGlobal());
672 // Show the path to the current profile in bold, including the current profile itself
673 if ((currentProfile != Q_NULLPTRnullptr) &&
674 (currentProfile->getName().compare(profile->getName()) == 0 ||
675 currentProfile->getName().startsWith(profile->getName() + "/")) &&
676 profile->isGlobal() == currentProfile->isGlobal())
677 subMenuFont.setBold(true);
678 subMenu->menuAction()->setFont(subMenuFont);
679 }
680 if (!hidden)
681 {
682 QAction * pa = subMenu->addAction(name);
683 QFont profileFont;
684 pa->setCheckable(true);
685 if ((currentProfile != Q_NULLPTRnullptr) &&
686 (profile->getName().compare(currentProfile->getName()) == 0) &&
687 profile->isGlobal() == currentProfile->isGlobal())
688 {
689 profileFont.setBold(true);
690 pa->setChecked(true);
691 }
692
693 if (profile->isGlobal())
694 profileFont.setItalic(true);
695
696 pa->setFont(profileFont);
697 pa->setProperty("profile_name", profile->getName());
698 pa->setProperty("profile_is_global", profile->isGlobal());
699
700 connect(pa, &QAction::triggered, this, &MainStatusBar::switchToProfile);
701 }
702
703 if (hasChildren)
704 {
705 subMenu->addSeparator();
706 addProfiles(subMenu, proxyIndex, global);
707
708 // Remove a submenu with only the separator, when the profile and all its children are hidden
709 if (subMenu->actions().count() <= 1)
710 delete subMenu;
711 }
712 }
713 };
714
715 addProfiles(profile_menu, QModelIndex(), false);
716 profile_menu->addSeparator();
717 addProfiles(profile_menu, QModelIndex(), true);
718
719 SubMenuPlacer *placer = new SubMenuPlacer(profile_menu);
3
Memory is allocated
720 for (QMenu *subMenu : profile_menu->findChildren<QMenu *>())
721 subMenu->installEventFilter(placer);
722
723 if (button
3.1
'button' is equal to LeftButton
== Qt::LeftButton) {
4
Taking true branch
724 profile_menu->popup(global_pos);
5
Potential leak of memory pointed to by 'placer'
725 } else {
726
727 bool enable_edit = false;
728
729 if ((currentProfile != Q_NULLPTRnullptr) && !currentProfile->isDefault() && !currentProfile->isGlobal())
730 enable_edit = true;
731
732 profile_menu->setTitle(tr("Switch to"));
733 QAction * action = ctx_menu_->addAction(tr("Manage Profiles…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "733")
);
734 action->setProperty("dialog_action_", (int)ProfileDialog::ShowProfiles);
735
736 ctx_menu_->addSeparator();
737 action = ctx_menu_->addAction(tr("New…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "737")
);
738 action->setProperty("dialog_action_", (int)ProfileDialog::NewProfile);
739 action = ctx_menu_->addAction(tr("Edit…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "739")
);
740 action->setProperty("dialog_action_", (int)ProfileDialog::EditCurrentProfile);
741 action->setEnabled(enable_edit);
742 action = ctx_menu_->addAction(tr("Delete"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "742")
);
743 action->setProperty("dialog_action_", (int)ProfileDialog::DeleteCurrentProfile);
744 action->setEnabled(enable_edit);
745 ctx_menu_->addSeparator();
746
747#if defined(HAVE_MINIZIP1) || defined(HAVE_MINIZIPNG)
748 QMenu * importMenu = new QMenu(tr("Import"), ctx_menu_);
749 action = importMenu->addAction(tr("From Zip File…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "749")
);
750 action->setProperty("dialog_action_", (int)ProfileDialog::ImportZipProfile);
751 action = importMenu->addAction(tr("From Directory…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "751")
);
752 action->setProperty("dialog_action_", (int)ProfileDialog::ImportDirProfile);
753 ctx_menu_->addMenu(importMenu);
754
755 if (model.userProfilesExist())
756 {
757 QMenu * exportMenu = new QMenu(tr("Export"), ctx_menu_);
758 if (enable_edit)
759 {
760 action = exportMenu->addAction(tr("Selected Personal Profile…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "760")
);
761 action->setProperty("dialog_action_", (int)ProfileDialog::ExportSingleProfile);
762 action->setEnabled(enable_edit);
763
764 qsizetype count = model.withChildren({model.findByName(currentProfile->getName())}).count();
765 if (count > 1)
766 {
767 action = exportMenu->addAction(tr("%Ln Personal Profile(s) (including sub-profiles)…", "", static_cast<int>(count)), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "767")
);
768 action->setProperty("dialog_action_", (int)ProfileDialog::ExportSingleProfileWithChildren);
769 }
770 }
771 action = exportMenu->addAction(tr("All Personal Profiles…"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "771")
);
772 action->setProperty("dialog_action_", (int)ProfileDialog::ExportAllProfiles);
773 ctx_menu_->addMenu(exportMenu);
774 }
775
776#else
777 action = ctx_menu_->addAction(tr("Import"), this, SLOT(manageProfile())qFlagLocation("1" "manageProfile()" "\0" "ui/qt/main_status_bar.cpp"
":" "777")
);
778 action->setProperty("dialog_action_", (int)ProfileDialog::ImportDirProfile);
779#endif
780 ctx_menu_->addSeparator();
781
782 ctx_menu_->addMenu(profile_menu);
783 ctx_menu_->popup(global_pos);
784 }
785}
786
787void MainStatusBar::toggleBackground(bool enabled)
788{
789 if (enabled) {
790 setStyleSheet(QStringLiteral((QString(QtPrivate::qMakeStringPrivate(u"" "QStatusBar {" " background-color: %2;"
"}")))
791 "QStatusBar {"(QString(QtPrivate::qMakeStringPrivate(u"" "QStatusBar {" " background-color: %2;"
"}")))
792 " background-color: %2;"(QString(QtPrivate::qMakeStringPrivate(u"" "QStatusBar {" " background-color: %2;"
"}")))
793 "}"(QString(QtPrivate::qMakeStringPrivate(u"" "QStatusBar {" " background-color: %2;"
"}")))
794 )(QString(QtPrivate::qMakeStringPrivate(u"" "QStatusBar {" " background-color: %2;"
"}")))
795 .arg(ColorUtils::warningBackground().name()));
796 } else {
797 setStyleSheet(QString());
798 }
799}
800
801void MainStatusBar::switchToProfile()
802{
803 QAction *pa = qobject_cast<QAction*>(sender());
804
805 if (pa && pa->property("profile_name").isValid()) {
806 QString profile = pa->property("profile_name").toString();
807 mainApp->setConfigurationProfile(profile.toUtf8().constData());
808 }
809}
810
811void MainStatusBar::manageProfile()
812{
813 QAction *pa = qobject_cast<QAction*>(sender());
814
815 if (pa) {
816 ProfileDialog * cp_dialog = new ProfileDialog(this);
817 cp_dialog->setAttribute(Qt::WA_DeleteOnClose);
818
819 int profileAction = pa->property("dialog_action_").toInt();
820 cp_dialog->execAction(static_cast<ProfileDialog::ProfileAction>(profileAction));
821 }
822}
823
824void MainStatusBar::captureEventHandler(CaptureEvent ev)
825{
826 switch(ev.captureContext())
827 {
828#ifdef HAVE_LIBPCAP1
829 case CaptureEvent::Update:
830 switch (ev.eventType())
831 {
832 case CaptureEvent::Continued:
833 updateCaptureStatistics(ev.capSession());
834 break;
835 case CaptureEvent::Finished:
836 updateCaptureStatistics(ev.capSession());
837 break;
838 default:
839 break;
840 }
841 break;
842 case CaptureEvent::Fixed:
843 switch (ev.eventType())
844 {
845 case CaptureEvent::Continued:
846 updateCaptureFixedStatistics(ev.capSession());
847 break;
848 default:
849 break;
850 }
851 break;
852#endif
853 case CaptureEvent::Save:
854 switch (ev.eventType())
855 {
856 case CaptureEvent::Finished:
857 case CaptureEvent::Failed:
858 case CaptureEvent::Stopped:
859 popGenericStatus(STATUS_CTX_FILE);
860 break;
861 default:
862 break;
863 }
864 break;
865 default:
866 break;
867 }
868}