### Bug Summary

File: | builds/wireshark/wireshark/ui/qt/packet_list.cpp
Warning: | line 922, column 20 Potential leak of memory pointed to by 'drag_label'

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
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 packet_list.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-21/lib/clang/21 -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 /usr/include/x86_64-linux-gnu/qt6/QtWidgets -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/QtGui -isystem /usr/include/x86_64-linux-gnu/qt6/QtCore5Compat -isystem /usr/include/x86_64-linux-gnu/qt6/QtConcurrent -isystem /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport -isystem /usr/include/x86_64-linux-gnu/qt6/QtMultimedia -isystem /usr/include/x86_64-linux-gnu/qt6/QtNetwork -isystem /usr/include/x86_64-linux-gnu/qt6/QtDBus -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D QT_CONCURRENT_LIB -D QT_CORE5COMPAT_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_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/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../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 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2026-03-27-100342-3641-1 -x c++ /builds/wireshark/wireshark/ui/qt/packet_list.cpp

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

1 | /* packet_list.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 <ui/qt/packet_list.h>
11 |
12 | #include "config.h"
13 |
14 | #include "file.h"
15 |
16 | #include <epan/epan.h>
17 | #include <epan/epan_dissect.h>
18 |
19 | #include <epan/column.h>
20 | #include <epan/expert.h>
21 | #include <epan/packet.h>
22 | #include <epan/prefs.h>
23 | #include <epan/proto.h>
24 |
25 | #include "ui/main_statusbar.h"
26 | #include "ui/packet_list_utils.h"
27 | #include "ui/preference_utils.h"
28 | #include "ui/recent.h"
29 | #include "ui/recent_utils.h"
30 | #include "ui/ws_ui_util.h"
31 | #include "ui/simple_dialog.h"
32 | #include <wsutil/utf8_entities.h>
33 | #include "ui/util.h"
34 |
35 | #include "wiretap/wtap_opttypes.h"
36 | #include "app/application_flavor.h"
37 | #include "wsutil/str_util.h"
38 | #include <wsutil/wslog.h>
39 |
40 | #include <epan/color_filters.h>
41 |
42 | #include <ui/qt/utils/color_utils.h>
43 | #include <ui/qt/widgets/overlay_scroll_bar.h>
44 | #include "proto_tree.h"
45 | #include <ui/qt/utils/qt_ui_utils.h>
46 | #include "main_application.h"
47 | #include <ui/qt/utils/data_printer.h>
48 | #include <ui/qt/utils/frame_information.h>
49 | #include <ui/qt/utils/profile_switcher.h>
50 | #include <ui/qt/utils/variant_pointer.h>
51 | #include <ui/qt/models/pref_models.h>
52 | #include <ui/qt/widgets/packet_list_header.h>
53 | #include <ui/qt/utils/wireshark_mime_data.h>
54 | #include <ui/qt/widgets/drag_label.h>
55 | #include <ui/qt/filter_action.h>
56 | #include <ui/qt/follow_stream_action.h>
57 | #include <ui/qt/decode_as_dialog.h>
58 | #include <ui/qt/wireshark_main_window.h>
59 |
60 | #include <QAction>
61 | #include <QActionGroup>
62 | #include <QClipboard>
63 | #include <QContextMenuEvent>
64 | #include <QtCore/qmath.h>
65 | #include <QElapsedTimer>
66 | #include <QFontMetrics>
67 | #include <QHeaderView>
68 | #include <QMessageBox>
69 | #include <QPainter>
70 | #include <QScreen>
71 | #include <QScrollBar>
72 | #include <QTabWidget>
73 | #include <QTextEdit>
74 | #include <QTimerEvent>
75 | #include <QTreeWidget>
76 | #include <QWindow>
77 | #include <QJsonObject>
78 | #include <QJsonDocument>
79 |
80 | #ifdef Q_OS_WIN
81 | #include "wsutil/file_util.h"
82 | #include <QSysInfo>
83 | #include <uxtheme.h>
84 | #endif
85 |
86 | // To do:
87 | // - Fix "apply as filter" behavior.
88 | // - Add colorize conversation.
89 | // - Use a timer to trigger automatic scrolling.
90 |
91 | // If we ever add the ability to open multiple capture files we might be
92 | // able to use something like QMap<capture_file *, PacketList *> to match
93 | // capture files against packet lists and models.
94 | static PacketList *gbl_cur_packet_list;
95 |
96 | const int max_comments_to_fetch_ = 20000000; // Arbitrary
97 | const int overlay_update_interval_ = 100; // 250; // Milliseconds.
98 |
99 |
100 | /*
101 | * Given a frame_data structure, scroll to and select the row in the
102 | * packet list corresponding to that frame. If there is no such
103 | * row, return false, otherwise return true.
104 | */
105 | bool
106 | packet_list_select_row_from_data(frame_data *fdata_needle)
107 | {
108 | if (! gbl_cur_packet_list || ! gbl_cur_packet_list->model())
109 | return false;
110 | return gbl_cur_packet_list->selectRow(fdata_needle);
111 | }
112 |
113 | /*
114 | * Given a field_info, select the field (which will scroll to it in
115 | * the main ProtoTree, etc.) This is kind of an odd place for it,
116 | * but we call this when performing Find Packet in lieu of changing the
117 | * selected frame (the function above), because we found a match in the
118 | * same frame as the currently selected one.
119 | */
120 | bool
121 | packet_list_select_finfo(field_info *fi)
122 | {
123 | if (! gbl_cur_packet_list || ! gbl_cur_packet_list->model())
124 | return false;
125 |
126 | if (fi) {
127 | FieldInformation finfo(fi, gbl_cur_packet_list);
128 | emit gbl_cur_packet_list->fieldSelected(&finfo);
129 | } else {
130 | emit gbl_cur_packet_list->fieldSelected(0);
131 | }
132 | return true;
133 | }
134 |
135 | void
136 | packet_list_clear(void)
137 | {
138 | if (gbl_cur_packet_list) {
139 | gbl_cur_packet_list->clear();
140 | }
141 | }
142 |
143 | void
144 | packet_list_freeze(void)
145 | {
146 | if (gbl_cur_packet_list) {
147 | gbl_cur_packet_list->freeze();
148 | }
149 | }
150 |
151 | void
152 | packet_list_thaw(void)
153 | {
154 | if (gbl_cur_packet_list) {
155 | gbl_cur_packet_list->thaw();
156 | }
157 |
158 | packets_bar_update();
159 | }
160 |
161 | /* Redraw the packet list *and* currently-selected detail */
162 | void
163 | packet_list_queue_draw(void)
164 | {
165 | if (gbl_cur_packet_list)
166 | gbl_cur_packet_list->redrawVisiblePackets();
167 | }
168 |
169 | void
170 | packet_list_recent_write_all(FILE *rf) {
171 | if (!gbl_cur_packet_list)
172 | return;
173 |
174 | gbl_cur_packet_list->writeRecent(rf);
175 | }
176 |
177 | bool
178 | packet_list_multi_select_active(void)
179 | {
180 | if (gbl_cur_packet_list) {
181 | return gbl_cur_packet_list->multiSelectActive();
182 | }
183 | return false;
184 | }
185 |
186 | #define MIN_COL_WIDTH_STR"MMMMMM" "MMMMMM"
187 |
188 | PacketList::PacketList(QWidget *parent) :
189 | QTreeView(parent),
190 | proto_tree_(nullptr),
191 | cap_file_(nullptr),
192 | ctx_column_(-1),
193 | overlay_timer_id_(0),
194 | create_near_overlay_(true),
195 | create_far_overlay_(true),
196 | mouse_pressed_at_(QModelIndex()),
197 | capture_in_progress_(false),
198 | tail_at_end_(0),
199 | columns_changed_(false),
200 | set_column_visibility_(false),
201 | set_style_sheet_(false),
202 | frozen_current_row_(QModelIndex()),
203 | frozen_selected_rows_(QModelIndexList()),
204 | cur_history_(-1),
205 | in_history_(false),
206 | finfo_array(nullptr),
207 | profile_switcher_(nullptr)
208 | {
209 | setItemsExpandable(false);
210 | setRootIsDecorated(false);
211 | setSortingEnabled(prefs.gui_packet_list_sortable);
212 | setUniformRowHeights(true);
213 | setAccessibleName(tr("Packet list"));
214 | setAccessibleDescription(tr("List of captured packets"));
215 |
216 | packet_list_header_ = new PacketListHeader(header()->orientation());
217 | connect(packet_list_header_, &PacketListHeader::resetColumnWidth, this, &PacketList::setRecentColumnWidth);
218 | connect(packet_list_header_, &PacketListHeader::updatePackets, this, &PacketList::updatePackets);
219 | connect(packet_list_header_, &PacketListHeader::showColumnPreferences, this, &PacketList::showProtocolPreferences);
220 | connect(packet_list_header_, &PacketListHeader::editColumn, this, &PacketList::editColumn);
221 | connect(packet_list_header_, &PacketListHeader::columnsChanged, this, &PacketList::columnsChanged);
222 | setHeader(packet_list_header_);
223 |
224 | header()->setFirstSectionMovable(true);
225 |
226 | setSelectionMode(QAbstractItemView::ExtendedSelection);
227 |
228 | // Shrink down to a small but nonzero size in the main splitter.
229 | int one_em = fontMetrics().height();
230 | setMinimumSize(one_em, one_em);
231 |
232 | overlay_sb_ = new OverlayScrollBar(Qt::Vertical, this);
233 | setVerticalScrollBar(overlay_sb_);
234 |
235 | header()->setSortIndicator(-1, Qt::AscendingOrder);
236 |
237 | packet_list_model_ = new PacketListModel(this, cap_file_);
238 | setModel(packet_list_model_);
239 |
240 | Q_ASSERT(gbl_cur_packet_list == Q_NULLPTR)((gbl_cur_packet_list == nullptr) ? static_cast<void>(0 ) : qt_assert("gbl_cur_packet_list == Q_NULLPTR", "ui/qt/packet_list.cpp" , 240));
241 | gbl_cur_packet_list = this;
242 |
243 | connect(packet_list_model_, &PacketListModel::goToPacket, this, [=](int packet) { goToPacket(packet); });
244 | connect(mainApp, &MainApplication::addressResolutionChanged, this, &PacketList::redrawVisiblePacketsDontSelectCurrent);
245 | connect(mainApp, &MainApplication::columnDataChanged, this, &PacketList::redrawVisiblePacketsDontSelectCurrent);
246 | connect(mainApp, &MainApplication::preferencesChanged, this, [=]() {
247 | /* The pref is a uint but QCache maxCost is a signed int (/
248 | * qsizetype in Qt 6). Note that QAbstractItemModel row numbers
249 | * are ints, not unsigned ints, so we're limited to INT_MAX
250 | * rows anyway.
251 | */
252 | PacketListRecord::setMaxCache(prefs.gui_packet_list_cached_rows_max > INT_MAX2147483647 ? INT_MAX2147483647 : prefs.gui_packet_list_cached_rows_max);
253 | if ((bool) (prefs.gui_packet_list_sortable) != isSortingEnabled()) {
254 | setSortingEnabled(prefs.gui_packet_list_sortable);
255 | }
256 | });
257 |
258 | connect(header(), &QHeaderView::sectionResized, this, &PacketList::sectionResized);
259 | connect(header(), &QHeaderView::sectionMoved, this, &PacketList::sectionMoved);
260 |
261 | connect(verticalScrollBar(), &QScrollBar::actionTriggered, this, &PacketList::vScrollBarActionTriggered);
262 | }
263 |
264 | PacketList::~PacketList()
265 | {
266 | if (finfo_array)
267 | {
268 | g_ptr_array_free(finfo_array, true);
269 | }
270 | }
271 |
272 | void PacketList::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint)
273 | {
274 | /* QAbstractItemView doesn't have a way to indicate "auto scroll, but
275 | * only vertically." So just restore the horizontal scroll value whenever
276 | * it scrolls.
277 | */
278 | setUpdatesEnabled(false);
279 | int horizVal = horizontalScrollBar()->value();
280 | QTreeView::scrollTo(index, hint);
281 | horizontalScrollBar()->setValue(horizVal);
282 | setUpdatesEnabled(true);
283 | }
284 |
285 | void PacketList::colorsChanged()
286 | {
287 | const QString c_active = "active";
288 | const QString c_inactive = "!active";
289 |
290 | QString flat_style_format =
291 | "QTreeView::item:selected:%1 {"
292 | " color: %2;"
293 | " background-color: %3;"
294 | "}";
295 |
296 | QString gradient_style_format =
297 | "QTreeView::item:selected:%1 {"
298 | " color: %2;"
299 | " background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1 stop: 0 %4, stop: 0.5 %3, stop: 1 %4);"
300 | "}";
301 |
302 | QString hover_style = QStringLiteral((QString(QtPrivate::qMakeStringPrivate(u"" "QTreeView:item:hover {" " background-color: %1;" " color: palette(text);" "}")))
303 | "QTreeView:item:hover {"(QString(QtPrivate::qMakeStringPrivate(u"" "QTreeView:item:hover {" " background-color: %1;" " color: palette(text);" "}")))
304 | " background-color: %1;"(QString(QtPrivate::qMakeStringPrivate(u"" "QTreeView:item:hover {" " background-color: %1;" " color: palette(text);" "}")))
305 | " color: palette(text);"(QString(QtPrivate::qMakeStringPrivate(u"" "QTreeView:item:hover {" " background-color: %1;" " color: palette(text);" "}")))
306 | "}")(QString(QtPrivate::qMakeStringPrivate(u"" "QTreeView:item:hover {" " background-color: %1;" " color: palette(text);" "}"))).arg(ColorUtils::hoverBackground().name(QColor::HexArgb));
307 |
308 | QString active_style = QString();
309 | QString inactive_style = QString();
310 |
311 | if (prefs.gui_active_style == COLOR_STYLE_DEFAULT0) {
312 | // ACTIVE = Default
313 | } else if (prefs.gui_active_style == COLOR_STYLE_FLAT1) {
314 | // ACTIVE = Flat
315 | QColor foreground = ColorUtils::fromColorT(prefs.gui_active_fg);
316 | QColor background = ColorUtils::fromColorT(prefs.gui_active_bg);
317 |
318 | active_style = flat_style_format.arg(
319 | c_active,
320 | foreground.name(),
321 | background.name());
322 | } else if (prefs.gui_active_style == COLOR_STYLE_GRADIENT2) {
323 | // ACTIVE = Gradient
324 | QColor foreground = ColorUtils::fromColorT(prefs.gui_active_fg);
325 | QColor background1 = ColorUtils::fromColorT(prefs.gui_active_bg);
326 | QColor background2 = QColor::fromRgb(ColorUtils::alphaBlend(foreground, background1, COLOR_STYLE_ALPHA0.25));
327 |
328 | active_style = gradient_style_format.arg(
329 | c_active,
330 | foreground.name(),
331 | background1.name(),
332 | background2.name());
333 | }
334 |
335 | // INACTIVE style sheet settings
336 | if (prefs.gui_inactive_style == COLOR_STYLE_DEFAULT0) {
337 | // INACTIVE = Default
338 | } else if (prefs.gui_inactive_style == COLOR_STYLE_FLAT1) {
339 | // INACTIVE = Flat
340 | QColor foreground = ColorUtils::fromColorT(prefs.gui_inactive_fg);
341 | QColor background = ColorUtils::fromColorT(prefs.gui_inactive_bg);
342 |
343 | inactive_style = flat_style_format.arg(
344 | c_inactive,
345 | foreground.name(),
346 | background.name());
347 | } else if (prefs.gui_inactive_style == COLOR_STYLE_GRADIENT2) {
348 | // INACTIVE = Gradient
349 | QColor foreground = ColorUtils::fromColorT(prefs.gui_inactive_fg);
350 | QColor background1 = ColorUtils::fromColorT(prefs.gui_inactive_bg);
351 | QColor background2 = QColor::fromRgb(ColorUtils::alphaBlend(foreground, background1, COLOR_STYLE_ALPHA0.25));
352 |
353 | inactive_style = gradient_style_format.arg(
354 | c_inactive,
355 | foreground.name(),
356 | background1.name(),
357 | background2.name());
358 | }
359 |
360 | // Set the style sheet
361 | set_style_sheet_ = true;
362 | if(prefs.gui_packet_list_hover_style) {
363 | setStyleSheet(active_style + inactive_style + hover_style);
364 | } else {
365 | setStyleSheet(active_style + inactive_style);
366 | }
367 | set_style_sheet_ = false;
368 | #if \
369 | ( \
370 | (QT_VERSION((6<<16)|(4<<8)|(2)) >= QT_VERSION_CHECK(6, 5, 4)((6<<16)|(5<<8)|(4)) && QT_VERSION((6<<16)|(4<<8)|(2)) < QT_VERSION_CHECK(6, 6, 0)((6<<16)|(6<<8)|(0))) \
371 | || (QT_VERSION((6<<16)|(4<<8)|(2)) >= QT_VERSION_CHECK(6, 6, 1)((6<<16)|(6<<8)|(1))) \
372 | )
373 | // https://bugreports.qt.io/browse/QTBUG-122109
374 | // Affects Qt 6.5.4 and later, 6.6.1 and later.
375 | // When setting the style sheet, all visible sections are set
376 | // to the new minimum DefaultSectionSize (even if it hasn't
377 | // changed.) So make sure the new widths aren't saved to recent
378 | // and then restore from recent.
379 | applyRecentColumnWidths();
380 | setColumnVisibility();
381 | #endif
382 | }
383 |
384 | QString PacketList::joinSummaryRow(QStringList col_parts, int row, SummaryCopyType type)
385 | {
386 | QString copy_text;
387 | switch (type) {
388 | case CopyAsCSV:
389 | copy_text = "\"";
390 | copy_text += col_parts.join("\",\"");
391 | copy_text += "\"";
392 | break;
393 | case CopyAsYAML:
394 | copy_text = "----\n";
395 | copy_text += QStringLiteral("# Packet %1 from %2\n")(QString(QtPrivate::qMakeStringPrivate(u"" "# Packet %1 from %2\n" ))).arg(row).arg(cap_file_->filename);
396 | copy_text += "- ";
397 | copy_text += col_parts.join("\n- ");
398 | copy_text += "\n";
399 | break;
400 | case CopyAsText:
401 | default:
402 | copy_text = col_parts.join("\t");
403 | }
404 |
405 | return copy_text;
406 | }
407 |
408 | void PacketList::drawRow (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
409 | {
410 | QTreeView::drawRow(painter, option, index);
411 |
412 | if (prefs.gui_packet_list_separator) {
413 | QRect rect = visualRect(index);
414 |
415 | painter->setPen(QColor(Qt::white));
416 | painter->drawLine(0, rect.y() + rect.height() - 1, width(), rect.y() + rect.height() - 1);
417 | }
418 | }
419 |
420 | void PacketList::setProtoTree (ProtoTree *proto_tree) {
421 | proto_tree_ = proto_tree;
422 |
423 | connect(proto_tree_, &ProtoTree::goToPacket, this, [=](int packet) { goToPacket(packet); });
424 | connect(proto_tree_, &ProtoTree::relatedFrame,
425 | &related_packet_delegate_, &RelatedPacketDelegate::addRelatedFrame);
426 | }
427 |
428 | bool PacketList::uniqueSelectActive()
429 | {
430 | return selectionModel()->selectedRows(0).count() == 1 ? true : false;
431 | }
432 |
433 | bool PacketList::multiSelectActive()
434 | {
435 | return selectionModel()->selectedRows(0).count() > 1 ? true : false;
436 | }
437 |
438 | QList<int> PacketList::selectedRows(bool useFrameNum)
439 | {
440 | QList<int> rows;
441 | if (selectionModel() && selectionModel()->hasSelection())
442 | {
443 | foreach (QModelIndex idx, selectionModel()->selectedRows(0))for (auto _container_443 = QtPrivate::qMakeForeachContainer(selectionModel ()->selectedRows(0)); _container_443.i != _container_443.e ; ++_container_443.i) if (QModelIndex idx = *_container_443.i ; false) {} else
444 | {
445 | if (idx.isValid())
446 | {
447 | if (! useFrameNum)
448 | rows << idx.row();
449 | else if (useFrameNum)
450 | {
451 | frame_data * frame = getFDataForRow(idx.row());
452 | if (frame)
453 | rows << frame->num;
454 | }
455 | }
456 | }
457 | }
458 | else if (currentIndex().isValid())
459 | {
460 | //
461 | // XXX - will we ever have a current index but not a selection
462 | // model?
463 | //
464 | if (! useFrameNum)
465 | rows << currentIndex().row();
466 | else
467 | {
468 | frame_data *frame = getFDataForRow(currentIndex().row());
469 | if (frame)
470 | rows << frame->num;
471 | }
472 | }
473 |
474 | return rows;
475 | }
476 |
477 | void PacketList::selectionChanged (const QItemSelection & selected, const QItemSelection & deselected)
478 | {
479 | QTreeView::selectionChanged(selected, deselected);
480 |
481 | if (!cap_file_) return;
482 |
483 | int row = -1;
484 | static bool multiSelect = false;
485 |
486 | if (selectionModel())
487 | {
488 | QModelIndexList selRows = selectionModel()->selectedRows(0);
489 | if (selRows.count() > 1)
490 | {
491 | QList<int> rows;
492 | foreach (QModelIndex idx, selRows)for (auto _container_492 = QtPrivate::qMakeForeachContainer(selRows ); _container_492.i != _container_492.e; ++_container_492.i) if (QModelIndex idx = *_container_492.i; false) {} else
493 | {
494 | if (idx.isValid())
495 | rows << idx.row();
496 | }
497 |
498 | emit framesSelected(rows);
499 | emit fieldSelected(0);
500 | cf_unselect_packet(cap_file_);
501 |
502 | /* We have to repaint the content while changing state, as some delegates react to multi-select */
503 | if (! multiSelect)
504 | {
505 | related_packet_delegate_.clear();
506 | viewport()->update();
507 | }
508 |
509 | multiSelect = true;
510 |
511 | return;
512 | }
513 | else if (selRows.count() > 0 && selRows.at(0).isValid())
514 | {
515 | multiSelect = false;
516 | row = selRows.at(0).row();
517 | }
518 |
519 | /* Handling empty selection */
520 | if (selRows.count() <= 0)
521 | {
522 | /* Nothing selected, but multiSelect is still active */
523 | if (multiSelect)
524 | {
525 | multiSelect = false;
526 | if (currentIndex().isValid())
527 | {
528 | selectionModel()->select(currentIndex(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
529 | return;
530 | }
531 | }
532 | /* Nothing selected, so in WS <= 3.0 nothing was indicated as well */
533 | else if (currentIndex().isValid())
534 | {
535 | setCurrentIndex(QModelIndex());
536 | }
537 | }
538 | }
539 |
540 | if (row < 0 || !packet_list_model_)
541 | cf_unselect_packet(cap_file_);
542 | else {
543 | frame_data * fdata = packet_list_model_->getRowFdata(row);
544 | cf_select_packet(cap_file_, fdata);
545 | }
546 |
547 | if (!in_history_ && cap_file_->current_frame) {
548 | cur_history_++;
549 | selection_history_.resize(cur_history_);
550 | selection_history_.append(cap_file_->current_frame->num);
551 | }
552 |
553 | related_packet_delegate_.clear();
554 |
555 | // The previous dissection state has been invalidated by cf_select_packet
556 | // above, receivers must clear the previous state and apply the updated one.
557 | emit framesSelected(QList<int>() << row);
558 |
559 | if (!cap_file_->edt) {
560 | viewport()->update();
561 | emit fieldSelected(0);
562 | return;
563 | }
564 |
565 | if (cap_file_->edt->tree) {
566 | packet_info *pi = &cap_file_->edt->pi;
567 | related_packet_delegate_.setCurrentFrame(pi->num);
568 | conversation_t *conv = find_conversation_pinfo_ro(pi, 0);
569 | if (conv) {
570 | related_packet_delegate_.setConversation(conv);
571 | }
572 | viewport()->update();
573 | }
574 |
575 | if (cap_file_->search_in_progress) {
576 | field_info *fi = NULL__null;
577 |
578 | if (cap_file_->string && cap_file_->decode_data) {
579 | // The tree where the target string matched one of the labels was discarded in
580 | // match_protocol_tree() so we have to search again in the latest tree.
581 | fi = cf_find_string_protocol_tree(cap_file_, cap_file_->edt->tree);
582 | } else if (cap_file_->search_len != 0) {
583 | // Find the finfo that corresponds to our byte.
584 | // The match can span multiple fields (and a single byte can
585 | // match more than one field.) Our behavior is to find the last
586 | // field in the tree (so hopefully spanning fewer bytes) that
587 | // matches the last byte in the search match.
588 | // (regex search can find a zero length match not at the
589 | // start of the frame if lookbehind is used, but
590 | // proto_find_field_from_offset doesn't match such a field
591 | // and it's not clear which field we would want to match.)
592 | fi = proto_find_field_from_offset(cap_file_->edt->tree, cap_file_->search_pos + cap_file_->search_len - 1,
593 | cap_file_->edt->tvb);
594 | }
595 |
596 | if (fi) {
597 | FieldInformation finfo(fi, this);
598 | emit fieldSelected(&finfo);
599 | } else {
600 | emit fieldSelected(0);
601 | }
602 | } else if (proto_tree_) {
603 | proto_tree_->restoreSelectedField();
604 | }
605 | }
606 |
607 | void PacketList::contextMenuEvent(QContextMenuEvent *event)
608 | {
609 | const char *module_name = NULL__null;
610 |
611 | if (finfo_array)
612 | {
613 | g_ptr_array_free(finfo_array, true);
614 | finfo_array = NULL__null;
615 | }
616 |
617 | QModelIndex ctxIndex = indexAt(event->pos());
618 |
619 | if (selectionModel() && selectionModel()->selectedRows(0).count() > 1)
620 | selectionModel()->select(ctxIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
621 |
622 | // frameData will be owned by one of the submenus, see below.
623 | FrameInformation * frameData =
624 | new FrameInformation(new CaptureFile(this, cap_file_), packet_list_model_->getRowFdata(ctxIndex.row()));
625 |
626 | QMenu * ctx_menu = new QMenu(this);
627 | ctx_menu->setAttribute(Qt::WA_DeleteOnClose);
628 | // XXX We might want to reimplement setParent() and fill in the context
629 | // menu there.
630 | ctx_menu->addAction(window()->findChild<QAction *>("actionEditMarkSelected"));
631 | ctx_menu->addAction(window()->findChild<QAction *>("actionEditIgnoreSelected"));
632 | ctx_menu->addAction(window()->findChild<QAction *>("actionEditSetTimeReference"));
633 | ctx_menu->addAction(window()->findChild<QAction *>("actionEditTimeShift"));
634 | ctx_menu->addMenu(window()->findChild<QMenu *>("menuPacketComment"));
635 |
636 | ctx_menu->addSeparator();
637 |
638 | // Code for custom context menus from Lua's register_packet_menu()
639 | MainWindow * mainWindow = mainApp->mainWindow();
640 | // N.B., will only call for a single frame selection,
641 | if (cap_file_ && cap_file_->edt && cap_file_->edt->tree) {
642 | finfo_array = proto_all_finfos(cap_file_->edt->tree);
643 | if (mainWindow) {
644 | bool insertedPacketMenu = mainWindow->addPacketMenus(ctx_menu, finfo_array);
645 | if (insertedPacketMenu) {
646 | ctx_menu->addSeparator();
647 | }
648 | }
649 | }
650 |
651 | ctx_menu->addAction(window()->findChild<QAction *>("actionViewEditResolvedName"));
652 | ctx_menu->addSeparator();
653 |
654 | QString selectedfilter = getFilterFromRowAndColumn(currentIndex());
655 |
656 | if (! hasFocus() && cap_file_ && cap_file_->finfo_selected) {
657 | char *tmp_field = proto_construct_match_selected_string(cap_file_->finfo_selected, cap_file_->edt);
658 | selectedfilter = QString(tmp_field);
659 | wmem_free(NULL__null, tmp_field);
660 | }
661 |
662 | bool have_filter_expr = !selectedfilter.isEmpty();
663 | ctx_menu->addMenu(FilterAction::createFilterMenu(FilterAction::ActionApply, selectedfilter, have_filter_expr, ctx_menu));
664 | ctx_menu->addMenu(FilterAction::createFilterMenu(FilterAction::ActionPrepare, selectedfilter, have_filter_expr, ctx_menu));
665 |
666 | const char *conv_menu_name = "menuConversationFilter";
667 | QMenu * main_menu_item = window()->findChild<QMenu *>(conv_menu_name);
668 | conv_menu_.setTitle(main_menu_item->title());
669 | conv_menu_.setObjectName(conv_menu_name);
670 | ctx_menu->addMenu(&conv_menu_);
671 |
672 | const char *colorize_menu_name = "menuColorizeConversation";
673 | main_menu_item = window()->findChild<QMenu *>(colorize_menu_name);
674 | colorize_menu_.setTitle(main_menu_item->title());
675 | colorize_menu_.setObjectName(colorize_menu_name);
676 | ctx_menu->addMenu(&colorize_menu_);
677 |
678 | QMenu * submenu;
679 | main_menu_item = window()->findChild<QMenu *>("menuSCTP");
680 | if (main_menu_item) {
681 | submenu = new QMenu(main_menu_item->title(), ctx_menu);
682 | ctx_menu->addMenu(submenu);
683 | submenu->addAction(window()->findChild<QAction *>("actionSCTPAnalyseThisAssociation"));
684 | submenu->addAction(window()->findChild<QAction *>("actionSCTPShowAllAssociations"));
685 | submenu->addAction(window()->findChild<QAction *>("actionSCTPFilterThisAssociation"));
686 | }
687 |
688 | main_menu_item = window()->findChild<QMenu *>("menuFollow");
689 | if (main_menu_item) {
690 | submenu = new QMenu(main_menu_item->title(), ctx_menu);
691 | ctx_menu->addMenu(submenu);
692 | foreach (FollowStreamAction *follow_action, main_menu_item->findChildren<FollowStreamAction *>())for (auto _container_692 = QtPrivate::qMakeForeachContainer(main_menu_item ->findChildren<FollowStreamAction *>()); _container_692 .i != _container_692.e; ++_container_692.i) if (FollowStreamAction *follow_action = *_container_692.i; false) {} else {
693 | /* XXX: We could, like the prefs above, walk the protocols/layers
694 | * and add the follow actions in the order they appear in the packet.
695 | */
696 | if (follow_action->isEnabled()) {
697 | submenu->addAction(follow_action);
698 | }
699 | }
700 | }
701 |
702 | ctx_menu->addSeparator();
703 |
704 | main_menu_item = window()->findChild<QMenu *>("menuEditCopy");
705 | submenu = new QMenu(main_menu_item->title(), ctx_menu);
706 | submenu->setToolTipsVisible(true);
707 | ctx_menu->addMenu(submenu);
708 |
709 | QAction * action = submenu->addAction(tr("Summary as Text"));
710 | action->setData(CopyAsText);
711 | connect(action, &QAction::triggered, this, &PacketList::copySummary);
712 | action = submenu->addAction(tr("…as CSV"));
713 | action->setData(CopyAsCSV);
714 | connect(action, &QAction::triggered, this, &PacketList::copySummary);
715 | action = submenu->addAction(tr("…as YAML"));
716 | action->setData(CopyAsYAML);
717 | connect(action, &QAction::triggered, this, &PacketList::copySummary);
718 | action = submenu->addAction(tr("…as HTML"));
719 | action->setData(CopyAsHTML);
720 | connect(action, &QAction::triggered, this, &PacketList::copySummary);
721 | submenu->addSeparator();
722 |
723 | submenu->addAction(window()->findChild<QAction *>("actionEditCopyAsFilter"));
724 | submenu->addSeparator();
725 |
726 | QActionGroup * copyEntries = DataPrinter::copyActions(this, frameData);
727 | submenu->addActions(copyEntries->actions());
728 | copyEntries->setParent(submenu);
729 | frameData->setParent(submenu);
730 |
731 | if (application_flavor_is_wireshark()) {
732 | /* i.e., Wireshark only */
733 | ctx_menu->addSeparator();
734 | QMenu *proto_prefs_menus = new QMenu(ProtocolPreferencesMenu::tr("Protocol Preferences"), ctx_menu);
735 |
736 | if (cap_file_ && cap_file_->edt && cap_file_->edt->tree) {
737 | QList<QString> added_proto_prefs;
738 | // N.B. finfo_array will be assigned above
739 | for (unsigned i = 0; i < finfo_array->len; i++) {
740 | field_info *fi = (field_info *)g_ptr_array_index (finfo_array, i)((finfo_array)->pdata)[i];
741 | const header_field_info *hfinfo = fi->hfinfo;
742 |
743 | if (prefs_is_registered_protocol(hfinfo->abbrev)) {
744 | if (hfinfo->parent == -1) {
745 | module_name = hfinfo->abbrev;
746 | } else {
747 | module_name = proto_registrar_get_abbrev(hfinfo->parent);
748 | }
749 |
750 | if (added_proto_prefs.contains(module_name)) {
751 | continue;
752 | }
753 |
754 | ProtocolPreferencesMenu *proto_prefs_menu = new ProtocolPreferencesMenu(hfinfo->name, module_name, proto_prefs_menus);
755 |
756 | connect(proto_prefs_menu, &ProtocolPreferencesMenu::showProtocolPreferences,
757 | this, &PacketList::showProtocolPreferences);
758 | connect(proto_prefs_menu, SIGNAL(editProtocolPreference(pref_t*,module_t*))qFlagLocation("2" "editProtocolPreference(pref_t*,module_t*)" "\0" "ui/qt/packet_list.cpp" ":" "758"),
759 | this, SIGNAL(editProtocolPreference(pref_t*,module_t*))qFlagLocation("2" "editProtocolPreference(pref_t*,module_t*)" "\0" "ui/qt/packet_list.cpp" ":" "759"));
760 |
761 | proto_prefs_menus->addMenu(proto_prefs_menu);
762 | added_proto_prefs << module_name;
763 | }
764 | }
765 | }
766 | ctx_menu->addMenu(proto_prefs_menus);
767 | action = ctx_menu->addAction(tr("Decode As…"));
768 | action->setProperty("create_new", QVariant(true));
769 | connect(action, &QAction::triggered, this, &PacketList::ctxDecodeAsDialog);
770 | // "Print" not ported intentionally
771 | action = window()->findChild<QAction *>("actionViewShowPacketInNewWindow");
772 | ctx_menu->addAction(action);
773 | }
774 |
775 | // Set menu sensitivity for the current column and set action data.
776 | if (frameData)
777 | emit framesSelected(QList<int>() << frameData->frameNum());
778 | else
779 | emit framesSelected(QList<int>());
780 |
781 | ctx_menu->popup(event->globalPos());
782 | }
783 |
784 | void PacketList::ctxDecodeAsDialog()
785 | {
786 | QAction *da_action = qobject_cast<QAction*>(sender());
787 | if (! da_action)
788 | return;
789 | bool create_new = da_action->property("create_new").toBool();
790 |
791 | DecodeAsDialog *da_dialog = new DecodeAsDialog(this, cap_file_, create_new);
792 | connect(da_dialog, &DecodeAsDialog::destroyed, mainApp, &MainApplication::flushAppSignals);
793 | da_dialog->setWindowModality(Qt::ApplicationModal);
794 | da_dialog->setAttribute(Qt::WA_DeleteOnClose);
795 | da_dialog->show();
796 | }
797 |
798 | void PacketList::timerEvent(QTimerEvent *event)
799 | {
800 | if (event->timerId() == overlay_timer_id_) {
801 | if (!capture_in_progress_ && model() != nullptr) {
802 | if (create_near_overlay_) drawNearOverlay();
803 | if (create_far_overlay_) drawFarOverlay();
804 | }
805 | } else {
806 | QTreeView::timerEvent(event);
807 | }
808 | }
809 |
810 | void PacketList::paintEvent(QPaintEvent *event)
811 | {
812 | // XXX This is overkill, but there are quite a few events that
813 | // require a new overlay, e.g. page up/down, scrolling, column
814 | // resizing, etc.
815 | create_near_overlay_ = true;
816 | QTreeView::paintEvent(event);
817 | }
818 |
819 | void PacketList::mousePressEvent(QMouseEvent *event)
820 | {
821 | QTreeView::mousePressEvent(event);
822 |
823 | QModelIndex curIndex = indexAt(event->pos());
824 | mouse_pressed_at_ = curIndex;
825 |
826 | bool midButton = (event->buttons() & Qt::MiddleButton) == Qt::MiddleButton;
827 | if (midButton && cap_file_ && packet_list_model_)
828 | {
829 | packet_list_model_->toggleFrameMark(QModelIndexList() << curIndex);
830 |
831 | // Make sure the packet list's frame.marked related field text is updated.
832 | redrawVisiblePackets();
833 |
834 | create_far_overlay_ = true;
835 | packets_bar_update();
836 | }
837 | }
838 |
839 | void PacketList::mouseReleaseEvent(QMouseEvent *event) {
840 | QTreeView::mouseReleaseEvent(event);
841 |
842 | mouse_pressed_at_ = QModelIndex();
843 | }
844 |
845 | void PacketList::mouseMoveEvent (QMouseEvent *event)
846 | {
847 | QModelIndex curIndex = indexAt(event->pos());
848 | if (event->buttons() & Qt::LeftButton && curIndex.isValid() && curIndex == mouse_pressed_at_)
1 | Assuming the condition is true | →
2 | ← | Taking true branch | →
849 | {
850 | ctx_column_ = curIndex.column();
851 | QMimeData * mimeData = new QMimeData();
852 | DragLabel * drag_label = nullptr;
853 |
854 | QString filter = getFilterFromRowAndColumn(curIndex);
855 | QList<int> rows = selectedRows();
856 | if (rows.count() > 1)
3 | ← | Assuming the condition is false | →
4 | ← | Taking false branch | →
857 | {
858 | QStringList entries;
859 | foreach (int row, rows)for (auto _container_859 = QtPrivate::qMakeForeachContainer(rows ); _container_859.i != _container_859.e; ++_container_859.i) if (int row = *_container_859.i; false) {} else
860 | {
861 | QModelIndex idx = model()->index(row, 0);
862 | if (! idx.isValid())
863 | continue;
864 |
865 | QString entry = createSummaryText(idx, CopyAsText);
866 | entries << entry;
867 | }
868 |
869 | if (entries.count() > 0)
870 | mimeData->setText(entries.join("\n"));
871 | }
872 | else if (! filter.isEmpty())
5 | ← | Assuming the condition is true | →
6 | ← | Taking true branch | →
873 | {
874 | QString abbrev;
875 | QString name = model()->headerData(curIndex.column(), header()->orientation()).toString();
876 |
877 | if (! filter.isEmpty())
7 | ← | Assuming the condition is false | →
8 | ← | Taking false branch | →
878 | {
879 | abbrev = filter.left(filter.indexOf(' '));
880 | }
881 | else
882 | {
883 | filter = model()->data(curIndex).toString().toLower();
884 | abbrev = filter;
885 | }
886 |
887 | mimeData->setText(filter);
888 |
889 | QJsonObject filterData;
890 | filterData["filter"] = filter;
891 | filterData["name"] = abbrev;
892 | filterData["description"] = name;
893 |
894 | mimeData->setData(WiresharkMimeData::DisplayFilterMimeType, QJsonDocument(filterData).toJson());
895 | drag_label = new DragLabel(QStringLiteral("%1\n%2")(QString(QtPrivate::qMakeStringPrivate(u"" "%1\n%2"))).arg(name, abbrev), this);
9 | ← | Memory is allocated | →
896 | }
897 | else
898 | {
899 | QString text = model()->data(curIndex).toString();
900 | if (! text.isEmpty())
901 | mimeData->setText(text);
902 | }
903 |
904 | if (mimeData->hasText() || mimeData->hasFormat(WiresharkMimeData::DisplayFilterMimeType))
10 | ← | Assuming the condition is false | →
11 | ← | Assuming the condition is false | →
12 | ← | Taking false branch | →
905 | {
906 | QDrag * drag = new QDrag(this);
907 | drag->setMimeData(mimeData);
908 | if (drag_label)
909 | {
910 | qreal dpr = window()->windowHandle()->devicePixelRatio();
911 | QPixmap pixmap= QPixmap(drag_label->size() * dpr);
912 | pixmap.setDevicePixelRatio(dpr);
913 | drag_label->render(&pixmap);
914 | drag->setPixmap(pixmap);
915 | delete drag_label;
916 | }
917 |
918 | drag->exec(Qt::CopyAction);
919 | }
920 | else
921 | {
922 | delete mimeData;
13 | ← | Potential leak of memory pointed to by 'drag_label'
923 | }
924 | }
925 | }
926 |
927 | void PacketList::keyPressEvent(QKeyEvent *event)
928 | {
929 | QTreeView::keyPressEvent(event);
930 |
931 | if (event->matches(QKeySequence::Copy))
932 | {
933 | QStringList content, htmlContent;
934 | if (model() && selectionModel() && selectionModel()->hasSelection())
935 | {
936 | QList<int> rows;
937 | QModelIndexList selRows = selectionModel()->selectedRows(0);
938 | foreach(QModelIndex row, selRows)for (auto _container_938 = QtPrivate::qMakeForeachContainer(selRows ); _container_938.i != _container_938.e; ++_container_938.i) if (QModelIndex row = *_container_938.i; false) {} else
939 | rows.append(row.row());
940 |
941 | QStringList hdr_parts;
942 | QList<int> align_parts, size_parts;
943 |
944 | switch (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut) {
945 | case COPY_FORMAT_TEXT:
946 | case COPY_FORMAT_HTML:
947 | if (prefs.gui_packet_list_copy_text_with_aligned_columns) {
948 | hdr_parts = createHeaderPartsForAligned();
949 | align_parts = createAlignmentPartsForAligned();
950 | size_parts = createSizePartsForAligned(false, hdr_parts, rows);
951 | }
952 | if (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut == COPY_FORMAT_HTML) {
953 | htmlContent << createDefaultStyleForHtml();
954 | htmlContent << createOpeningTagForHtml();
955 | }
956 | break;
957 | case COPY_FORMAT_CSV:
958 | case COPY_FORMAT_YAML:
959 | break;
960 | }
961 |
962 | QList<QStringList> entries;
963 | foreach(int row, rows)for (auto _container_963 = QtPrivate::qMakeForeachContainer(rows ); _container_963.i != _container_963.e; ++_container_963.i) if (int row = *_container_963.i; false) {} else
964 | {
965 | QModelIndex idx = model()->index(row, 0);
966 | if (! idx.isValid())
967 | continue;
968 |
969 | switch (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut) {
970 | case COPY_FORMAT_TEXT:
971 | case COPY_FORMAT_HTML:
972 | if (prefs.gui_packet_list_copy_text_with_aligned_columns)
973 | content << createSummaryForAligned(idx, align_parts, size_parts);
974 | else
975 | content << createSummaryText(idx, CopyAsText);
976 | if (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut == COPY_FORMAT_HTML)
977 | htmlContent << createSummaryForHtml(idx);
978 | break;
979 | case COPY_FORMAT_CSV:
980 | content << createSummaryText(idx, CopyAsCSV);
981 | break;
982 | case COPY_FORMAT_YAML:
983 | content << createSummaryText(idx, CopyAsYAML);
984 | break;
985 | }
986 | }
987 | }
988 |
989 | if (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut == COPY_FORMAT_HTML) {
990 | // htmlContent will never be empty as they will always have style and table tags
991 | QMimeData *mimeData = new QMimeData;
992 | htmlContent << createClosingTagForHtml();
993 | mimeData->setHtml(htmlContent.join('\n'));
994 | mimeData->setText(content.join('\n').append("\n"));
995 | mainApp->clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
996 | }
997 | else {
998 | if (content.count() > 0) {
999 | QString copy_text;
1000 | if (prefs.gui_packet_list_copy_format_options_for_keyboard_shortcut == COPY_FORMAT_YAML) {
1001 | copy_text = content.join("");
1002 | }
1003 | else {
1004 | copy_text = content.join("\n");
1005 | copy_text += "\n";
1006 | }
1007 | mainApp->clipboard()->setText(copy_text);
1008 | }
1009 | }
1010 | }
1011 | }
1012 |
1013 | void PacketList::resizeEvent(QResizeEvent *event)
1014 | {
1015 | create_near_overlay_ = true;
1016 | create_far_overlay_ = true;
1017 | QTreeView::resizeEvent(event);
1018 | }
1019 |
1020 | void PacketList::setColumnVisibility()
1021 | {
1022 | set_column_visibility_ = true;
1023 | for (unsigned i = 0; i < prefs.num_cols; i++) {
1024 | setColumnHidden(i, get_column_visible(i) ? false : true);
1025 | }
1026 | setColumnDelegate();
1027 | set_column_visibility_ = false;
1028 | }
1029 |
1030 | void PacketList::setColumnDelegate()
1031 | {
1032 | for (unsigned i = 0; i < prefs.num_cols; i++) {
1033 | setItemDelegateForColumn(i, nullptr); // Reset all delegates
1034 | }
1035 |
1036 | // Multi-color delegate takes precedence over related packet delegate (row stripes only, not scrollbar-only mode)
1037 | if (prefs.gui_packet_list_multi_color_mode == PACKET_LIST_MULTI_COLOR_MODE_FULL ||
1038 | prefs.gui_packet_list_multi_color_mode == PACKET_LIST_MULTI_COLOR_MODE_SHIFT_RIGHT) {
1039 | for (unsigned i = 0; i < prefs.num_cols; i++) {
1040 | if (get_column_visible(i)) {
1041 | setItemDelegateForColumn(i, &multi_color_delegate_);
1042 | }
1043 | }
1044 | }
1045 | else if (prefs.gui_packet_list_show_related) {
1046 | for (unsigned i = 0; i < prefs.num_cols; i++) {
1047 | if (get_column_visible(i)) {
1048 | setItemDelegateForColumn(i, &related_packet_delegate_);
1049 | break; // Set the delegate only on the first visible column
1050 | }
1051 | }
1052 | }
1053 | }
1054 |
1055 | void PacketList::setRecentColumnWidth(int col)
1056 | {
1057 | int col_width = recent_get_column_width(col);
1058 |
1059 | if (col_width < 1) {
1060 | int fmt = get_column_format(col);
1061 | const char *long_str = get_column_width_string(fmt, col);
1062 |
1063 | QFontMetrics fm = QFontMetrics(mainApp->monospaceFont());
1064 | if (long_str) {
1065 | col_width = fm.horizontalAdvance(long_str);
1066 | } else {
1067 | col_width = fm.horizontalAdvance(MIN_COL_WIDTH_STR"MMMMMM");
1068 | }
1069 | // Custom delegate padding
1070 | if (itemDelegateForColumn(col)) {
1071 | QStyleOptionViewItem option;
1072 | #if QT_VERSION((6<<16)|(4<<8)|(2)) >= QT_VERSION_CHECK(6, 0, 0)((6<<16)|(0<<8)|(0))
1073 | initViewItemOption(&option);
1074 | #else
1075 | option = viewOptions();
1076 | #endif
1077 | // This is adding "how much width hinted for an empty index, plus
1078 | // the decoration, plus any padding between the decoration and
1079 | // normal display?" Many styles however have a non zero hint for
1080 | // an empty index, so this isn't quite right. What we really want
1081 | // is a size hint for an index whose data is the string above, and
1082 | // to just use that for the width.
1083 | col_width += itemDelegateForColumn(col)->sizeHint(option, QModelIndex()).width();
1084 | }
1085 | }
1086 |
1087 | setColumnWidth(col, col_width);
1088 | }
1089 |
1090 | void PacketList::drawCurrentPacket()
1091 | {
1092 | // XXX - Update for multi-select? If more than one packet is Selected,
1093 | // this changes it so that only the Current packet is Selected.
1094 | QModelIndex current_index = currentIndex();
1095 | if (selectionModel() && current_index.isValid()) {
1096 | selectionModel()->clearSelection();
1097 | selectionModel()->setCurrentIndex(current_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
1098 | }
1099 | }
1100 |
1101 | // Redraw the packet list and detail. Re-selects the current packet (causes
1102 | // the UI to scroll to that packet).
1103 | // Called from many places.
1104 | void PacketList::redrawVisiblePackets() {
1105 | redrawVisiblePacketsDontSelectCurrent();
1106 | drawCurrentPacket();
1107 | }
1108 |
1109 | // Redraw the packet list and detail.
1110 | // Does not scroll back to the selected packet.
1111 | void PacketList::redrawVisiblePacketsDontSelectCurrent() {
1112 | packet_list_model_->invalidateAllColumnStrings();
1113 | }
1114 |
1115 | void PacketList::resetColumns()
1116 | {
1117 | packet_list_model_->resetColumns();
1118 | }
1119 |
1120 | // Return true if we have a visible packet further along in the history.
1121 | bool PacketList::haveNextHistory(bool update_cur)
1122 | {
1123 | if (selection_history_.size() < 1 || cur_history_ >= selection_history_.size() - 1) {
1124 | return false;
1125 | }
1126 |
1127 | for (int i = cur_history_ + 1; i < selection_history_.size(); i++) {
1128 | if (packet_list_model_->packetNumberToRow(selection_history_.at(i)) >= 0) {
1129 | if (update_cur) {
1130 | cur_history_ = i;
1131 | }
1132 | return true;
1133 | }
1134 | }
1135 | return false;
1136 | }
1137 |
1138 | // Return true if we have a visible packet back in the history.
1139 | bool PacketList::havePreviousHistory(bool update_cur)
1140 | {
1141 | if (selection_history_.size() < 1 || cur_history_ < 1) {
1142 | return false;
1143 | }
1144 |
1145 | for (int i = cur_history_ - 1; i >= 0; i--) {
1146 | if (packet_list_model_->packetNumberToRow(selection_history_.at(i)) >= 0) {
1147 | if (update_cur) {
1148 | cur_history_ = i;
1149 | }
1150 | return true;
1151 | }
1152 | }
1153 | return false;
1154 | }
1155 |
1156 | void PacketList::setProfileSwitcher(ProfileSwitcher *profile_switcher)
1157 | {
1158 | profile_switcher_ = profile_switcher;
1159 | if (profile_switcher) {
1160 | connect(packet_list_model_, &PacketListModel::packetAppended, profile_switcher_, &ProfileSwitcher::checkPacket);
1161 | }
1162 | }
1163 |
1164 | frame_data *PacketList::getFDataForRow(int row) const
1165 | {
1166 | return packet_list_model_->getRowFdata(row);
1167 | }
1168 |
1169 | // prefs.col_list has changed.
1170 | void PacketList::columnsChanged()
1171 | {
1172 | columns_changed_ = true;
1173 | column_register_fields();
1174 | mainApp->emitAppSignal(MainApplication::FieldsChanged);
1175 | if (!cap_file_) {
1176 | // Keep columns_changed_ = true until we load a capture file.
1177 | return;
1178 | }
1179 |
1180 | prefs.num_cols = g_list_length(prefs.col_list);
1181 | col_cleanup(&cap_file_->cinfo);
1182 | build_column_format_array(&cap_file_->cinfo, prefs.num_cols, false);
1183 | create_far_overlay_ = true;
1184 | resetColumns();
1185 | applyRecentColumnWidths();
1186 | setColumnVisibility();
1187 | columns_changed_ = false;
1188 | }
1189 |
1190 | // Fields have changed, update custom columns
1191 | void PacketList::fieldsChanged(capture_file *cf)
1192 | {
1193 | // hfids used by custom columns or by the _ws.col fields may have changed,
1194 | // so recreate the columns. We shouldn't need to if prefs.col_list is NULL,
1195 | // since that doesn't register and deregister _ws.col fields.
1196 | // If the column pref changes to or from NULL, that triggers columnsChanged
1197 | // above, so we don't need to do it twice.
1198 | //
1199 | // XXX - If we knew exactly which fields changed, we could rebuild the
1200 | // columns only if a field used by the columns changed.
1201 | if (prefs.col_list) {
1202 | prefs.num_cols = g_list_length(prefs.col_list);
1203 | col_cleanup(&cf->cinfo);
1204 | build_column_format_array(&cf->cinfo, prefs.num_cols, false);
1205 | resetColumns();
1206 | }
1207 | }
1208 |
1209 | // Column widths should
1210 | // - Load from recent when we load a new profile (including at starting up).
1211 | // - Reapply when changing columns.
1212 | // - Persist across freezes and thaws.
1213 | // - Persist across file closing and opening.
1214 | // - Save to recent when we save our profile (including shutting down).
1215 | // - Not be affected by the behavior of stretchLastSection. (XXX: We
1216 | // still save the stretched value to recent, sectionResized doesn't
1217 | // distinguish between a resize from being stretched and a manual change.)
1218 | void PacketList::applyRecentColumnWidths()
1219 | {
1220 | // Either we've just started up or a profile has changed. Read
1221 | // the recent settings, apply them, and save the header state.
1222 |
1223 | for (unsigned col = 0; col < prefs.num_cols; col++) {
1224 | // The column must be shown before setting column width.
1225 | // Visibility will be updated in setColumnVisibility().
1226 | setColumnHidden(col, false);
1227 | setRecentColumnWidth(col);
1228 | }
1229 |
1230 | column_state_ = header()->saveState();
1231 | }
1232 |
1233 | void PacketList::preferencesChanged()
1234 | {
1235 | // Intelligent scroll bar (minimap)
1236 | if (prefs.gui_packet_list_show_minimap) {
1237 | if (overlay_timer_id_ == 0) {
1238 | overlay_timer_id_ = startTimer(overlay_update_interval_);
1239 | }
1240 | } else {
1241 | if (overlay_timer_id_ != 0) {
1242 | killTimer(overlay_timer_id_);
1243 | overlay_timer_id_ = 0;
1244 | }
1245 | }
1246 |
1247 | // Elide mode.
1248 | // This sets the mode for the entire view. If we want to make this setting
1249 | // per-column we'll either have to generalize RelatedPacketDelegate so that
1250 | // we can set it for entire rows or create another delegate.
1251 | Qt::TextElideMode elide_mode = Qt::ElideRight;
1252 | switch (prefs.gui_packet_list_elide_mode) {
1253 | case ELIDE_LEFT:
1254 | elide_mode = Qt::ElideLeft;
1255 | break;
1256 | case ELIDE_MIDDLE:
1257 | elide_mode = Qt::ElideMiddle;
1258 | break;
1259 | case ELIDE_NONE:
1260 | elide_mode = Qt::ElideNone;
1261 | break;
1262 | default:
1263 | break;
1264 | }
1265 | setTextElideMode(elide_mode);
1266 | }
1267 |
1268 | void PacketList::freezePacketList(bool changing_profile)
1269 | {
1270 | changing_profile_ = changing_profile;
1271 | freeze(true);
1272 | }
1273 |
1274 | void PacketList::recolorPackets()
1275 | {
1276 | packet_list_model_->resetColorized();
1277 | redrawVisiblePackets();
1278 | }
1279 |
1280 | // Enable autoscroll.
1281 | void PacketList::setVerticalAutoScroll(bool enabled)
1282 | {
1283 | tail_at_end_ = enabled;
1284 | if (enabled && capture_in_progress_) {
1285 | scrollToBottom();
1286 | }
1287 | }
1288 |
1289 | // Called when we finish reading, reloading, rescanning, and retapping
1290 | // packets.
1291 | void PacketList::captureFileReadFinished()
1292 | {
1293 | packet_list_model_->flushVisibleRows();
1294 | packet_list_model_->dissectIdle(true);
1295 | // Invalidating the column strings picks up and request/response
1296 | // tracking changes. We might just want to call it from flushVisibleRows.
1297 | packet_list_model_->invalidateAllColumnStrings();
1298 | // Sort *after* invalidating the column strings
1299 | if (isSortingEnabled()) {
1300 | sortByColumn(header()->sortIndicatorSection(), header()->sortIndicatorOrder());
1301 | }
1302 | }
1303 |
1304 | bool PacketList::freeze(bool keep_current_frame)
1305 | {
1306 | if (!cap_file_ || model() == Q_NULLPTRnullptr) {
1307 | // No capture file or already frozen
1308 | return false;
1309 | }
1310 |
1311 | frame_data *current_frame = cap_file_->current_frame;
1312 | column_state_ = header()->saveState();
1313 | setHeaderHidden(true);
1314 | frozen_current_row_ = currentIndex();
1315 | frozen_selected_rows_ = selectionModel()->selectedRows();
1316 | selectionModel()->clear();
1317 | setModel(Q_NULLPTRnullptr);
1318 | // It looks like GTK+ sends a cursor-changed signal at this point but Qt doesn't
1319 | // call selectionChanged.
1320 | related_packet_delegate_.clear();
1321 |
1322 | if (keep_current_frame) {
1323 | cap_file_->current_frame = current_frame;
1324 | }
1325 |
1326 | /* Clears packet list as well as byteview */
1327 | emit framesSelected(QList<int>());
1328 |
1329 | return true;
1330 | }
1331 |
1332 | bool PacketList::thaw(bool restore_selection)
1333 | {
1334 | if (!cap_file_ || model() != Q_NULLPTRnullptr) {
1335 | // No capture file or not frozen
1336 | return false;
1337 | }
1338 |
1339 | setHeaderHidden(false);
1340 | // Note that if we have a current sort status set in the header,
1341 | // this will automatically try to sort the model (we don't want
1342 | // that to happen if we're in the middle of reading the file).
1343 | setModel(packet_list_model_);
1344 |
1345 | if (changing_profile_) {
1346 | // When changing profile the new recent settings must be applied to the columns.
1347 | applyRecentColumnWidths();
1348 | setColumnVisibility();
1349 | changing_profile_ = false;
1350 | } else {
1351 | // Resetting the model resets our column widths so we restore them here.
1352 | // We don't reapply the recent settings because the user could have
1353 | // resized the columns manually since they were initially loaded.
1354 | header()->restoreState(column_state_);
1355 | }
1356 |
1357 | if (restore_selection && frozen_selected_rows_.length() > 0 && selectionModel()) {
1358 | /* This updates our selection, which redissects the current packet,
1359 | * which is needed when we're called from MainWindow::layoutPanes.
1360 | * Also, this resets all ProtoTree and ByteView data */
1361 | clearSelection();
1362 | setCurrentIndex(frozen_current_row_);
1363 | foreach (QModelIndex idx, frozen_selected_rows_)for (auto _container_1363 = QtPrivate::qMakeForeachContainer( frozen_selected_rows_); _container_1363.i != _container_1363. e; ++_container_1363.i) if (QModelIndex idx = *_container_1363 .i; false) {} else {
1364 | selectionModel()->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
1365 | }
1366 | scrollTo(currentIndex(), PositionAtCenter);
1367 | }
1368 | frozen_current_row_ = QModelIndex();
1369 | frozen_selected_rows_ = QModelIndexList();
1370 |
1371 | return true;
1372 | }
1373 |
1374 | void PacketList::clear() {
1375 | related_packet_delegate_.clear();
1376 | selectionModel()->clear();
1377 | packet_list_model_->clear();
1378 | proto_tree_->clear();
1379 | selection_history_.clear();
1380 | cur_history_ = -1;
1381 | in_history_ = false;
1382 |
1383 | QImage overlay;
1384 | overlay_sb_->setNearOverlayImage(overlay);
1385 | overlay_sb_->setMarkedPacketImage(overlay);
1386 | create_near_overlay_ = true;
1387 | create_far_overlay_ = true;
1388 | }
1389 |
1390 | void PacketList::writeRecent(FILE *rf) {
1391 | int width, col_fmt;
1392 | char xalign;
1393 |
1394 | fprintf (rf, "%s:\n", RECENT_KEY_COL_WIDTH"column.width");
1395 | for (unsigned col = 0; col < prefs.num_cols; col++) {
1396 | if (col > 0) {
1397 | fprintf (rf, ",\n");
1398 | }
1399 | col_fmt = get_column_format(col);
1400 | if (col_fmt == COL_CUSTOM) {
1401 | fprintf (rf, " \"%%Cus:%s\",", get_column_custom_fields(col));
1402 | } else {
1403 | fprintf (rf, " %s,", col_format_to_string(col_fmt));
1404 | }
1405 | width = recent_get_column_width (col);
1406 | xalign = recent_get_column_xalign (col);
1407 | fprintf (rf, " %d", width);
1408 | if (xalign != COLUMN_XALIGN_DEFAULT0) {
1409 | fprintf (rf, ":%c", xalign);
1410 | }
1411 | }
1412 | fprintf (rf, "\n");
1413 | }
1414 |
1415 | bool PacketList::contextMenuActive()
1416 | {
1417 | return ctx_column_ >= 0 ? true : false;
1418 | }
1419 |
1420 | QString PacketList::getFilterFromRowAndColumn(QModelIndex idx)
1421 | {
1422 | frame_data *fdata;
1423 | QString filter;
1424 |
1425 | if (! idx.isValid())
1426 | return filter;
1427 |
1428 | int row = idx.row();
1429 | int column = idx.column();
1430 |
1431 | if (!cap_file_ || !packet_list_model_ || column < 0 || (unsigned)column >= cap_file_->cinfo.num_cols)
1432 | return filter;
1433 |
1434 | fdata = packet_list_model_->getRowFdata(row);
1435 |
1436 | if (fdata != NULL__null) {
1437 | epan_dissect_t edt;
1438 | wtap_rec rec; /* Record information */
1439 |
1440 | wtap_rec_init(&rec, DEFAULT_INIT_BUFFER_SIZE_2048(2 * 1024));
1441 | if (!cf_read_record(cap_file_, fdata, &rec)) {
1442 | wtap_rec_cleanup(&rec);
1443 | return filter; /* error reading the record */
1444 | }
1445 | /* proto tree, visible. We need a proto tree if there's custom columns */
1446 | epan_dissect_init(&edt, cap_file_->epan, have_custom_cols(&cap_file_->cinfo), false);
1447 | col_custom_prime_edt(&edt, &cap_file_->cinfo);
1448 |
1449 | epan_dissect_run(&edt, cap_file_->cd_t, &rec, fdata, &cap_file_->cinfo);
1450 |
1451 | if (cap_file_->cinfo.columns[column].col_fmt == COL_CUSTOM) {
1452 | filter.append(gchar_free_to_qstring(col_custom_get_filter(&edt, &cap_file_->cinfo, (unsigned)column)));
1453 | } else {
1454 | /* We don't need to fill in the custom columns, as we get their
1455 | * filters above.
1456 | */
1457 | col_fill_in(&edt.pi, true, true);
1458 | if (strlen(cap_file_->cinfo.col_expr.col_expr[column]) != 0 &&
1459 | strlen(cap_file_->cinfo.col_expr.col_expr_val[column]) != 0) {
1460 | bool is_string_value = false;
1461 | header_field_info *hfi = proto_registrar_get_byname(cap_file_->cinfo.col_expr.col_expr[column]);
1462 | if (hfi && FT_IS_STRING(hfi->type)((hfi->type) == FT_STRING || (hfi->type) == FT_STRINGZ || (hfi->type) == FT_STRINGZPAD || (hfi->type) == FT_STRINGZTRUNC || (hfi->type) == FT_UINT_STRING || (hfi->type) == FT_AX25 )) {
1463 | /* Could be an address type such as usb.src which must be quoted. */
1464 | is_string_value = true;
1465 | }
1466 |
1467 | if (filter.isEmpty()) {
1468 | if (is_string_value) {
1469 | filter.append(QStringLiteral("%1 == \"%2\"")(QString(QtPrivate::qMakeStringPrivate(u"" "%1 == \"%2\"")))
1470 | .arg(cap_file_->cinfo.col_expr.col_expr[column])
1471 | .arg(cap_file_->cinfo.col_expr.col_expr_val[column]));
1472 | } else {
1473 | filter.append(QStringLiteral("%1 == %2")(QString(QtPrivate::qMakeStringPrivate(u"" "%1 == %2")))
1474 | .arg(cap_file_->cinfo.col_expr.col_expr[column])
1475 | .arg(cap_file_->cinfo.col_expr.col_expr_val[column]));
1476 | }
1477 | }
1478 | }
1479 | }
1480 |
1481 | epan_dissect_cleanup(&edt);
1482 | wtap_rec_cleanup(&rec);
1483 | }
1484 |
1485 | return filter;
1486 | }
1487 |
1488 | void PacketList::resetColorized()
1489 | {
1490 | packet_list_model_->resetColorized();
1491 | update();
1492 | }
1493 |
1494 | QString PacketList::getPacketComment(unsigned c_number)
1495 | {
1496 | int row = currentIndex().row();
1497 | const frame_data *fdata;
1498 | char *pkt_comment;
1499 | wtap_opttype_return_val result;
1500 | QString ret_val = NULL__null;
1501 |
1502 | if (!cap_file_ || !packet_list_model_) return NULL__null;
1503 |
1504 | fdata = packet_list_model_->getRowFdata(row);
1505 |
1506 | if (!fdata) return NULL__null;
1507 |
1508 | wtap_block_t pkt_block = cf_get_packet_block(cap_file_, fdata);
1509 | result = wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, c_number, &pkt_comment);
1510 | if (result == WTAP_OPTTYPE_SUCCESS) {
1511 | ret_val = QString(pkt_comment);
1512 | }
1513 | wtap_block_unref(pkt_block);
1514 | return ret_val;
1515 | }
1516 |
1517 | void PacketList::addPacketComment(QString new_comment)
1518 | {
1519 | if (!cap_file_ || !packet_list_model_) return;
1520 | if (new_comment.isEmpty()) return;
1521 |
1522 | QByteArray ba = new_comment.toUtf8();
1523 |
1524 | /*
1525 | * Make sure this would fit in a pcapng option.
1526 | *
1527 | * XXX - 65535 is the maximum size for an option in pcapng;
1528 | * what if another capture file format supports larger
1529 | * comments?
1530 | */
1531 | if (ba.size() > 65535) {
1532 | simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
1533 | "That comment is too large to save in a capture file.");
1534 | return;
1535 | }
1536 |
1537 | if (selectionModel() && selectionModel()->hasSelection()) {
1538 | packet_list_model_->addFrameComment(selectionModel()->selectedRows(), ba);
1539 | drawCurrentPacket();
1540 | }
1541 | }
1542 |
1543 | void PacketList::setPacketComment(unsigned c_number, QString new_comment)
1544 | {
1545 | QModelIndex curIndex = currentIndex();
1546 |
1547 | if (!cap_file_ || !packet_list_model_) return;
1548 |
1549 | QByteArray ba = new_comment.toUtf8();
1550 | /*
1551 | * Make sure this would fit in a pcapng option.
1552 | *
1553 | * XXX - 65535 is the maximum size for an option in pcapng;
1554 | * what if another capture file format supports larger
1555 | * comments?
1556 | */
1557 | if (ba.size() > 65535) {
1558 | simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK0x01,
1559 | "That comment is too large to save in a capture file.");
1560 | return;
1561 | }
1562 |
1563 | packet_list_model_->setFrameComment(curIndex, ba, c_number);
1564 | drawCurrentPacket();
1565 | }
1566 |
1567 | QString PacketList::allPacketComments()
1568 | {
1569 | uint32_t framenum;
1570 | frame_data *fdata;
1571 | QString buf_str;
1572 |
1573 | if (!cap_file_) return buf_str;
1574 |
1575 | for (framenum = 1; framenum <= cap_file_->count ; framenum++) {
1576 | fdata = frame_data_sequence_find(cap_file_->provider.frames, framenum);
1577 |
1578 | wtap_block_t pkt_block = cf_get_packet_block(cap_file_, fdata);
1579 |
1580 | if (pkt_block) {
1581 | unsigned n_comments = wtap_block_count_option(pkt_block, OPT_COMMENT1);
1582 | for (unsigned i = 0; i < n_comments; i++) {
1583 | char *comment_text;
1584 | if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_nth_string_option_value(pkt_block, OPT_COMMENT1, i, &comment_text)) {
1585 | buf_str.append(tr("Frame %1: %2\n\n").arg(framenum).arg(comment_text));
1586 | if (buf_str.length() > max_comments_to_fetch_) {
1587 | buf_str.append(tr("[ Comment text exceeds %1. Stopping. ]")
1588 | .arg(format_size(max_comments_to_fetch_, FORMAT_SIZE_UNIT_BYTES, FORMAT_SIZE_PREFIX_SI)format_size_wmem(__null, max_comments_to_fetch_, FORMAT_SIZE_UNIT_BYTES , (1 << 0))));
1589 | return buf_str;
1590 | }
1591 | }
1592 | }
1593 | }
1594 | }
1595 | return buf_str;
1596 | }
1597 |
1598 | void PacketList::deleteCommentsFromPackets()
1599 | {
1600 | if (!cap_file_ || !packet_list_model_) return;
1601 |
1602 | if (selectionModel() && selectionModel()->hasSelection()) {
1603 | packet_list_model_->deleteFrameComments(selectionModel()->selectedRows());
1604 | drawCurrentPacket();
1605 | }
1606 | }
1607 |
1608 | void PacketList::deleteAllPacketComments()
1609 | {
1610 | if (!cap_file_ || !packet_list_model_) return;
1611 |
1612 | packet_list_model_->deleteAllFrameComments();
1613 | drawCurrentPacket();
1614 | }
1615 |
1616 |
1617 | // Slots
1618 |
1619 | void PacketList::setCaptureFile(capture_file *cf)
1620 | {
1621 | cap_file_ = cf;
1622 | packet_list_model_->setCaptureFile(cf);
1623 | if (cf) {
1624 | if (columns_changed_) {
1625 | columnsChanged();
1626 | } else {
1627 | // Restore columns widths and visibility.
1628 | header()->restoreState(column_state_);
1629 | setColumnVisibility();
1630 | }
1631 | }
1632 | create_near_overlay_ = true;
1633 | changing_profile_ = false;
1634 | sortByColumn(-1, Qt::AscendingOrder);
1635 | }
1636 |
1637 | void PacketList::setMonospaceFont(const QFont &mono_font)
1638 | {
1639 | setFont(mono_font);
1640 | }
1641 |
1642 | void PacketList::setRegularFont(const QFont &regular_font)
1643 | {
1644 | header()->setFont(regular_font);
1645 | header()->viewport()->setFont(regular_font);
1646 | }
1647 |
1648 | void PacketList::goNextPacket(void)
1649 | {
1650 | if (QApplication::keyboardModifiers() & Qt::AltModifier) {
1651 | // Alt+toolbar
1652 | goNextHistoryPacket();
1653 | return;
1654 | }
1655 |
1656 | if (selectionModel()->hasSelection()) {
1657 | selectionModel()->setCurrentIndex(moveCursor(MoveDown, Qt::NoModifier), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1658 | } else {
1659 | // First visible packet.
1660 | selectionModel()->setCurrentIndex(indexAt(viewport()->rect().topLeft()), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1661 | }
1662 |
1663 | scrollViewChanged(false);
1664 | }
1665 |
1666 | void PacketList::goPreviousPacket(void)
1667 | {
1668 | if (QApplication::keyboardModifiers() & Qt::AltModifier) {
1669 | // Alt+toolbar
1670 | goPreviousHistoryPacket();
1671 | return;
1672 | }
1673 |
1674 | if (selectionModel()->hasSelection()) {
1675 | selectionModel()->setCurrentIndex(moveCursor(MoveUp, Qt::NoModifier), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1676 | } else {
1677 | // Last visible packet.
1678 | QModelIndex last_idx = indexAt(viewport()->rect().bottomLeft());
1679 | if (last_idx.isValid()) {
1680 | selectionModel()->setCurrentIndex(last_idx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1681 | } else {
1682 | goLastPacket();
1683 | }
1684 | }
1685 |
1686 | scrollViewChanged(false);
1687 | }
1688 |
1689 | void PacketList::goFirstPacket(void) {
1690 | if (packet_list_model_->rowCount() < 1) return;
1691 | selectionModel()->setCurrentIndex(packet_list_model_->index(0, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1692 | scrollTo(currentIndex());
1693 |
1694 | scrollViewChanged(false);
1695 | }
1696 |
1697 | void PacketList::goLastPacket(void) {
1698 | if (packet_list_model_->rowCount() < 1) return;
1699 | selectionModel()->setCurrentIndex(packet_list_model_->index(packet_list_model_->rowCount() - 1, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
1700 | scrollTo(currentIndex());
1701 |
1702 | scrollViewChanged(false);
1703 | }
1704 |
1705 | void PacketList::goToPacket(int packet, int hf_id)
1706 | {
1707 | if (!cf_goto_frame(cap_file_, packet, false))
1708 | return;
1709 |
1710 | // cf_goto_frame only returns true if packet_list_select_row_from_data
1711 | // succeeds, the latter has already selected and scrolled to the frame.
1712 | if (hf_id > 0) {
1713 | proto_tree_->goToHfid(hf_id);
1714 | }
1715 |
1716 | scrollViewChanged(false);
1717 | }
1718 |
1719 | void PacketList::goNextHistoryPacket()
1720 | {
1721 | if (haveNextHistory(true)) {
1722 | in_history_ = true;
1723 | goToPacket(selection_history_.at(cur_history_));
1724 | in_history_ = false;
1725 | }
1726 | }
1727 |
1728 | void PacketList::goPreviousHistoryPacket()
1729 | {
1730 | if (havePreviousHistory(true)) {
1731 | in_history_ = true;
1732 | goToPacket(selection_history_.at(cur_history_));
1733 | in_history_ = false;
1734 | }
1735 | }
1736 |
1737 | void PacketList::markFrame()
1738 | {
1739 | if (!cap_file_ || !packet_list_model_) return;
1740 |
1741 | QModelIndexList frames;
1742 |
1743 | if (selectionModel() && selectionModel()->hasSelection())
1744 | {
1745 | QModelIndexList selRows = selectionModel()->selectedRows(0);
1746 | foreach (QModelIndex idx, selRows)for (auto _container_1746 = QtPrivate::qMakeForeachContainer( selRows); _container_1746.i != _container_1746.e; ++_container_1746 .i) if (QModelIndex idx = *_container_1746.i; false) {} else
1747 | {
1748 | if (idx.isValid())
1749 | {
1750 | frames << idx;
1751 | }
1752 | }
1753 | }
1754 | else
1755 | frames << currentIndex();
1756 |
1757 | packet_list_model_->toggleFrameMark(frames);
1758 |
1759 | // Make sure the packet list's frame.marked related field text is updated.
1760 | redrawVisiblePackets();
1761 |
1762 | create_far_overlay_ = true;
1763 | packets_bar_update();
1764 | }
1765 |
1766 | void PacketList::markAllDisplayedFrames(bool set)
1767 | {
1768 | if (!cap_file_ || !packet_list_model_) return;
1769 |
1770 | packet_list_model_->setDisplayedFrameMark(set);
1771 |
1772 | // Make sure the packet list's frame.marked related field text is updated.
1773 | redrawVisiblePackets();
1774 |
1775 | create_far_overlay_ = true;
1776 | packets_bar_update();
1777 | }
1778 |
1779 | void PacketList::ignoreFrame()
1780 | {
1781 | if (!cap_file_ || !packet_list_model_) return;
1782 |
1783 | QModelIndexList frames;
1784 |
1785 | if (selectionModel() && selectionModel()->hasSelection())
1786 | {
1787 | foreach (QModelIndex idx, selectionModel()->selectedRows(0))for (auto _container_1787 = QtPrivate::qMakeForeachContainer( selectionModel()->selectedRows(0)); _container_1787.i != _container_1787 .e; ++_container_1787.i) if (QModelIndex idx = *_container_1787 .i; false) {} else
1788 | {
1789 | if (idx.isValid())
1790 | {
1791 | frames << idx;
1792 | }
1793 | }
1794 | }
1795 | else
1796 | frames << currentIndex();
1797 |
1798 |
1799 | packet_list_model_->toggleFrameIgnore(frames);
1800 | create_far_overlay_ = true;
1801 | int sb_val = verticalScrollBar()->value(); // Surely there's a better way to keep our position?
1802 | setUpdatesEnabled(false);
1803 | emit packetDissectionChanged();
1804 | setUpdatesEnabled(true);
1805 | verticalScrollBar()->setValue(sb_val);
1806 | }
1807 |
1808 | void PacketList::ignoreAllDisplayedFrames(bool set)
1809 | {
1810 | if (!cap_file_ || !packet_list_model_) return;
1811 |
1812 | packet_list_model_->setDisplayedFrameIgnore(set);
1813 | create_far_overlay_ = true;
1814 | emit packetDissectionChanged();
1815 | }
1816 |
1817 | void PacketList::setTimeReference()
1818 | {
1819 | if (!cap_file_ || !packet_list_model_) return;
1820 | packet_list_model_->toggleFrameRefTime(currentIndex());
1821 | create_far_overlay_ = true;
1822 | }
1823 |
1824 | void PacketList::unsetAllTimeReferences()
1825 | {
1826 | if (!cap_file_ || !packet_list_model_) return;
1827 | packet_list_model_->unsetAllFrameRefTime();
1828 | create_far_overlay_ = true;
1829 | }
1830 |
1831 | void PacketList::applyTimeShift()
1832 | {
1833 | packet_list_model_->resetColumns();
1834 | redrawVisiblePackets();
1835 | emit packetDissectionChanged();
1836 | }
1837 |
1838 | void PacketList::updatePackets(bool redraw)
1839 | {
1840 | if (redraw) {
1841 | packet_list_model_->resetColumns();
1842 | redrawVisiblePackets();
1843 | } else {
1844 | update();
1845 | }
1846 | }
1847 |
1848 | void PacketList::columnVisibilityTriggered()
1849 | {
1850 | QAction *ha = qobject_cast<QAction*>(sender());
1851 | if (!ha) return;
1852 |
1853 | int col = ha->data().toInt();
1854 | set_column_visible(col, ha->isChecked());
1855 | setColumnVisibility();
1856 | if (ha->isChecked()) {
1857 | setRecentColumnWidth(col);
1858 | }
1859 | prefs_main_write();
1860 | }
1861 |
1862 | void PacketList::sectionResized(int col, int, int new_width)
1863 | {
1864 | if (isVisible() && !columns_changed_ && !set_column_visibility_ && !set_style_sheet_ && new_width > 0) {
1865 | // Column 1 gets an invalid value (32 on macOS) when we're not yet
1866 | // visible.
1867 | //
1868 | // Don't set column width when columns changed or setting column
1869 | // visibility because we may get a sectionResized() from QTreeView
1870 | // with values from a old columns layout.
1871 | //
1872 | // Don't set column width when hiding a column.
1873 |
1874 | recent_set_column_width(col, new_width);
1875 | }
1876 | }
1877 |
1878 | // The user moved a column. Make sure prefs.col_list, the column format
1879 | // array, and the header's visual and logical indices all agree.
1880 | // gtk/packet_list.c:column_dnd_changed_cb
1881 | void PacketList::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
1882 | {
1883 | GList *new_col_list = NULL__null;
1884 | GList *new_recent_col_list = NULL__null;
1885 | QList<int> saved_sizes;
1886 | int sort_idx;
1887 |
1888 | // Since we undo the move below, these should always stay in sync.
1889 | // Otherwise the order of columns can be unexpected after drag and drop.
1890 | if (logicalIndex != oldVisualIndex) {
1891 | ws_warning("Column moved from an unexpected state (%d, %d, %d)",do { if (true) { ws_log_full("", LOG_LEVEL_WARNING, "ui/qt/packet_list.cpp" , 1892, __func__, "Column moved from an unexpected state (%d, %d, %d)" , logicalIndex, oldVisualIndex, newVisualIndex); } } while (0 )
1892 | logicalIndex, oldVisualIndex, newVisualIndex)do { if (true) { ws_log_full("", LOG_LEVEL_WARNING, "ui/qt/packet_list.cpp" , 1892, __func__, "Column moved from an unexpected state (%d, %d, %d)" , logicalIndex, oldVisualIndex, newVisualIndex); } } while (0 );
1893 | }
1894 |
1895 | // Remember which column should be sorted. Use the visual index since this
1896 | // points to the current GUI state rather than the outdated column order
1897 | // (indicated by the logical index).
1898 | sort_idx = header()->sortIndicatorSection();
1899 | if (sort_idx != -1) {
1900 | sort_idx = header()->visualIndex(sort_idx);
1901 | }
1902 |
1903 | // Build a new column list based on the header's logical order.
1904 | for (int vis_idx = 0; vis_idx < header()->count(); vis_idx++) {
1905 | int log_idx = header()->logicalIndex(vis_idx);
1906 | saved_sizes << header()->sectionSize(log_idx);
1907 |
1908 | void *pref_data = g_list_nth_data(prefs.col_list, log_idx);
1909 | if (pref_data) {
1910 | new_col_list = g_list_append(new_col_list, pref_data);
1911 | }
1912 |
1913 | pref_data = g_list_nth_data(recent.col_width_list, log_idx);
1914 | if (pref_data) {
1915 | new_recent_col_list = g_list_append(new_recent_col_list, pref_data);
1916 | }
1917 | }
1918 |
1919 | // Undo move to ensure that the logical indices map to the visual indices,
1920 | // otherwise the column order is changed twice (once via the modified
1921 | // col_list, once because of the visual/logical index mismatch).
1922 | disconnect(header(), &QHeaderView::sectionMoved, this, &PacketList::sectionMoved);
1923 | header()->moveSection(newVisualIndex, oldVisualIndex);
1924 | connect(header(), &QHeaderView::sectionMoved, this, &PacketList::sectionMoved);
1925 |
1926 | // Clear and rebuild our (and the header's) model. There doesn't appear
1927 | // to be another way to reset the logical index.
1928 | freeze();
1929 |
1930 | g_list_free(prefs.col_list);
1931 | prefs.col_list = new_col_list;
1932 | g_list_free(recent.col_width_list);
1933 | recent.col_width_list = new_recent_col_list;
1934 |
1935 | thaw(true);
1936 |
1937 | for (int i = 0; i < saved_sizes.length(); i++) {
1938 | if (saved_sizes[i] < 1) continue;
1939 | header()->resizeSection(i, saved_sizes[i]);
1940 | }
1941 |
1942 | prefs_main_write();
1943 |
1944 | mainApp->emitAppSignal(MainApplication::ColumnsChanged);
1945 |
1946 | // If the column with the sort indicator got shifted, mark the new column
1947 | // after updating the columns contents (via ColumnsChanged) to ensure that
1948 | // the columns are sorted using the intended column contents.
1949 | int left_col = MIN(oldVisualIndex, newVisualIndex)(((oldVisualIndex) < (newVisualIndex)) ? (oldVisualIndex) : (newVisualIndex));
1950 | int right_col = MAX(oldVisualIndex, newVisualIndex)(((oldVisualIndex) > (newVisualIndex)) ? (oldVisualIndex) : (newVisualIndex));
1951 | if (left_col <= sort_idx && sort_idx <= right_col) {
1952 | header()->setSortIndicator(sort_idx, header()->sortIndicatorOrder());
1953 | }
1954 | }
1955 |
1956 | QString PacketList::createSummaryText(QModelIndex idx, SummaryCopyType type)
1957 | {
1958 | if (! idx.isValid())
1959 | return "";
1960 |
1961 | QStringList col_parts;
1962 | int row = idx.row();
1963 | for (int col = 0; col < packet_list_model_->columnCount(); col++) {
1964 | if (get_column_visible(col)) {
1965 | col_parts << packet_list_model_->data(packet_list_model_->index(row, col), Qt::DisplayRole).toString();
1966 | }
1967 | }
1968 | return joinSummaryRow(col_parts, row, type);
1969 | }
1970 |
1971 | QString PacketList::createHeaderSummaryText(SummaryCopyType type)
1972 | {
1973 | QStringList col_parts;
1974 | for (int col = 0; col < packet_list_model_->columnCount(); ++col) {
1975 | if (get_column_visible(col)) {
1976 | col_parts << packet_list_model_->headerData(col, Qt::Orientation::Horizontal, Qt::DisplayRole).toString();
1977 | }
1978 | }
1979 | return joinSummaryRow(col_parts, 0, type);
1980 | }
1981 |
1982 | QStringList PacketList::createHeaderPartsForAligned()
1983 | {
1984 | QStringList hdr_parts;
1985 | for (int col = 0; col < packet_list_model_->columnCount(); ++col) {
1986 | if (get_column_visible(col)) {
1987 | hdr_parts << packet_list_model_->headerData(col, Qt::Orientation::Horizontal, Qt::DisplayRole).toString();
1988 | }
1989 | }
1990 | return hdr_parts;
1991 | }
1992 |
1993 | QList<int> PacketList::createAlignmentPartsForAligned()
1994 | {
1995 | QList<int> align_parts;
1996 | for (int col = 0; col < packet_list_model_->columnCount(); col++) {
1997 | if (get_column_visible(col)) {
1998 | align_parts << packet_list_model_->data(packet_list_model_->index(0, col), Qt::TextAlignmentRole).toInt();
1999 | }
2000 | }
2001 | return align_parts;
2002 | }
2003 |
2004 | QList<int> PacketList::createSizePartsForAligned(bool useHeader, QStringList hdr_parts, QList<int> rows)
2005 | {
2006 | QList<int> size_parts;
2007 |
2008 | for (int i = 0; i < hdr_parts.size(); ++i) {
2009 | if (useHeader)
2010 | size_parts << static_cast<int>(hdr_parts.at(i).size());
2011 | else
2012 | size_parts << 0;
2013 | }
2014 |
2015 | foreach(int row, rows)for (auto _container_2015 = QtPrivate::qMakeForeachContainer( rows); _container_2015.i != _container_2015.e; ++_container_2015 .i) if (int row = *_container_2015.i; false) {} else
2016 | {
2017 | QModelIndex idx = model()->index(row, 0);
2018 | if (! idx.isValid())
2019 | continue;
2020 |
2021 | QStringList col_parts;
2022 | for (int col = 0; col < packet_list_model_->columnCount(); col++) {
2023 | if (get_column_visible(col)) {
2024 | col_parts << (packet_list_model_->data(packet_list_model_->index(row, col), Qt::DisplayRole).toString());
2025 | }
2026 | }
2027 |
2028 | for (int i = 0; i < col_parts.size(); ++i) {
2029 | if (col_parts.at(i).size() > size_parts.at(i)) {
2030 | size_parts[i] = static_cast<int>(col_parts.at(i).size());
2031 | }
2032 | }
2033 | col_parts.clear();
2034 | }
2035 |
2036 | return size_parts;
2037 | }
2038 |
2039 | QString PacketList::createHeaderSummaryForAligned(QStringList hdr_parts, QList<int> align_parts, QList<int> size_parts)
2040 | {
2041 | QString hdr_text;
2042 | for (int i = 0; i < hdr_parts.size(); ++i) {
2043 | if (align_parts.at(i) == Qt::AlignLeft) {
2044 | hdr_text += hdr_parts[i].leftJustified(size_parts.at(i), ' ') + " ";
2045 | }
2046 | else if (align_parts.at(i) == Qt::AlignRight) {
2047 | hdr_text += hdr_parts[i].rightJustified(size_parts.at(i), ' ') + " ";
2048 | }
2049 | }
2050 | return QStringLiteral("-%1")(QString(QtPrivate::qMakeStringPrivate(u"" "-%1"))).arg(hdr_text).trimmed().mid(1);
2051 | }
2052 |
2053 | QString PacketList::createSummaryForAligned(QModelIndex idx, QList<int> align_parts, QList<int> size_parts)
2054 | {
2055 | if (! idx.isValid())
2056 | return "";
2057 |
2058 | QStringList col_parts;
2059 | int row = idx.row();
2060 | for (int col = 0; col < packet_list_model_->columnCount(); col++) {
2061 | if (get_column_visible(col)) {
2062 | col_parts << packet_list_model_->data(packet_list_model_->index(row, col), Qt::DisplayRole).toString();
2063 | }
2064 | }
2065 |
2066 | QString col_text;
2067 | for (int i = 0; i < col_parts.size(); ++i) {
2068 | if (align_parts.at(i) == Qt::AlignLeft) {
2069 | col_text += col_parts[i].leftJustified(size_parts.at(i), ' ') + " ";
2070 | }
2071 | else if (align_parts.at(i) == Qt::AlignRight) {
2072 | col_text += col_parts[i].rightJustified(size_parts.at(i), ' ') + " ";
2073 | }
2074 | }
2075 |
2076 | return QStringLiteral("-%1")(QString(QtPrivate::qMakeStringPrivate(u"" "-%1"))).arg(col_text).trimmed().mid(1);
2077 | }
2078 |
2079 | QString PacketList::createDefaultStyleForHtml()
2080 | {
2081 | QString fontFamily = QString(prefs.gui_font_name).split(",")[0];
2082 | QString fontSize = QString(prefs.gui_font_name).split(",")[1];
2083 |
2084 | return QStringLiteral("<style>"(QString(QtPrivate::qMakeStringPrivate(u"" "<style>" "table{font-family:%1;font-size:%2pt;}" "th{background-color:#000000;color:#ffffff;text-align:left;}" "th,td{padding:%3pt}" "</style>")))
2085 | "table{font-family:%1;font-size:%2pt;}"(QString(QtPrivate::qMakeStringPrivate(u"" "<style>" "table{font-family:%1;font-size:%2pt;}" "th{background-color:#000000;color:#ffffff;text-align:left;}" "th,td{padding:%3pt}" "</style>")))
2086 | "th{background-color:#000000;color:#ffffff;text-align:left;}"(QString(QtPrivate::qMakeStringPrivate(u"" "<style>" "table{font-family:%1;font-size:%2pt;}" "th{background-color:#000000;color:#ffffff;text-align:left;}" "th,td{padding:%3pt}" "</style>")))
2087 | "th,td{padding:%3pt}"(QString(QtPrivate::qMakeStringPrivate(u"" "<style>" "table{font-family:%1;font-size:%2pt;}" "th{background-color:#000000;color:#ffffff;text-align:left;}" "th,td{padding:%3pt}" "</style>")))
2088 | "</style>")(QString(QtPrivate::qMakeStringPrivate(u"" "<style>" "table{font-family:%1;font-size:%2pt;}" "th{background-color:#000000;color:#ffffff;text-align:left;}" "th,td{padding:%3pt}" "</style>"))).arg(fontFamily, fontSize).arg(fontSize.toInt() / 2);
2089 | }
2090 |
2091 | QString PacketList::createOpeningTagForHtml()
2092 | {
2093 | return "<table>";
2094 | }
2095 |
2096 | QString PacketList::createHeaderSummaryForHtml()
2097 | {
2098 | QString hdr_text;
2099 | hdr_text += "<tr>";
2100 | for (int col = 0; col < packet_list_model_->columnCount(); ++col) {
2101 | if (get_column_visible(col)) {
2102 | hdr_text += "<th>";
2103 | hdr_text += packet_list_model_->headerData(col, Qt::Orientation::Horizontal, Qt::DisplayRole).toString();
2104 | hdr_text += "</th>";
2105 | }
2106 | }
2107 | hdr_text += "</tr>";
2108 | return hdr_text;
2109 | }
2110 |
2111 | QString PacketList::createSummaryForHtml(QModelIndex idx)
2112 | {
2113 | if (! idx.isValid())
2114 | return "";
2115 |
2116 | int row = idx.row();
2117 | QString col_text;
2118 |
2119 | QString bg_color = packet_list_model_->data(packet_list_model_->index(row, 0), Qt::BackgroundRole).toString();
2120 | QString fg_color = packet_list_model_->data(packet_list_model_->index(row, 0), Qt::ForegroundRole).toString();
2121 | col_text += "<tr style=\"background-color:" + bg_color + ";color:" + fg_color + ";\">";
2122 |
2123 | QString alignment[] = {"left", "right", "center", "justify"};
2124 |
2125 | for (int col = 0; col < packet_list_model_->columnCount(); col++) {
2126 | if (get_column_visible(col)) {
2127 | col_text += "<td style=\"text-align:" + alignment[packet_list_model_->data(packet_list_model_->index(row, col), Qt::TextAlignmentRole).toInt() / 2] + ";\">";
2128 | col_text += packet_list_model_->data(packet_list_model_->index(row, col), Qt::DisplayRole).toString();
2129 | col_text += "</td>";
2130 | }
2131 | }
2132 |
2133 | col_text += "</tr>";
2134 | return col_text;
2135 | }
2136 |
2137 | QString PacketList::createClosingTagForHtml()
2138 | {
2139 | return "</table>";
2140 | }
2141 |
2142 | void PacketList::copySummary()
2143 | {
2144 | if (!currentIndex().isValid()) return;
2145 |
2146 | QAction *ca = qobject_cast<QAction*>(sender());
2147 | if (!ca) return;
2148 |
2149 | QVariant type = ca->data();
2150 | if (! type.canConvert<SummaryCopyType>())
2151 | return;
2152 | SummaryCopyType copy_type = type.value<SummaryCopyType>();
2153 |
2154 | QString copy_text;
2155 | if (type == CopyAsText || type == CopyAsHTML) {
2156 | if (prefs.gui_packet_list_copy_text_with_aligned_columns) {
2157 | QList<int> rows;
2158 | rows << currentIndex().row();
2159 | QStringList hdr_parts;
2160 | QList<int> align_parts, size_parts;
2161 | hdr_parts = createHeaderPartsForAligned();
2162 | align_parts = createAlignmentPartsForAligned();
2163 | size_parts = createSizePartsForAligned(false, hdr_parts, rows);
2164 | copy_text = createSummaryForAligned(currentIndex(), align_parts, size_parts);
2165 | }
2166 | else {
2167 | copy_text = createSummaryText(currentIndex(), CopyAsText);
2168 | }
2169 | copy_text += "\n";
2170 | if (type == CopyAsHTML) {
2171 | QStringList htmlContent;
2172 | htmlContent << createDefaultStyleForHtml();
2173 | htmlContent << createOpeningTagForHtml();
2174 | htmlContent << createSummaryForHtml(currentIndex());
2175 | htmlContent << createClosingTagForHtml();
2176 | // htmlContent will never be empty as they will always have
2177 | // style and table tags
2178 | QMimeData *mimeData = new QMimeData;
2179 | mimeData->setHtml(htmlContent.join('\n'));
2180 | mimeData->setText(copy_text);
2181 | mainApp->clipboard()->setMimeData(mimeData, QClipboard::Clipboard);
2182 | }
2183 | else {
2184 | mainApp->clipboard()->setText(copy_text);
2185 | }
2186 | }
2187 | else {
2188 | copy_text = createSummaryText(currentIndex(), copy_type);
2189 | if (type != CopyAsYAML)
2190 | copy_text += "\n";
2191 | mainApp->clipboard()->setText(copy_text);
2192 | }
2193 | }
2194 |
2195 | // We need to tell when the user has scrolled the packet list, either to
2196 | // the end or anywhere other than the end.
2197 | void PacketList::vScrollBarActionTriggered(int)
2198 | {
2199 | // If we're scrolling with a mouse wheel or trackpad sliderPosition can end up
2200 | // past the end.
2201 | tail_at_end_ = (overlay_sb_->sliderPosition() >= overlay_sb_->maximum());
2202 |
2203 | scrollViewChanged(tail_at_end_);
2204 | }
2205 |
2206 | void PacketList::scrollViewChanged(bool at_end)
2207 | {
2208 | if (capture_in_progress_) {
2209 | // We want to start auto scrolling when the user scrolls to (or past)
2210 | // the end only if recent.capture_auto_scroll is set.
2211 | // We want to stop autoscrolling if the user scrolls up or uses
2212 | // Go to Packet regardless of the preference setting.
2213 | if (recent.capture_auto_scroll || !at_end) {
2214 | emit packetListScrolled(at_end);
2215 | }
2216 | }
2217 | }
2218 |
2219 | // Goal: Overlay the packet list scroll bar with the colors of all of the
2220 | // packets.
2221 | // Try 1: Average packet colors in each scroll bar raster line. This has
2222 | // two problems: It's easy to wash out colors and we dissect every packet.
2223 | // Try 2: Color across a 5000 or 10000 packet window. We still end up washing
2224 | // out colors.
2225 | // Try 3: One packet per vertical scroll bar pixel. This seems to work best
2226 | // but has the smallest window.
2227 | // Try 4: Use a multiple of the scroll bar height and scale the image down
2228 | // using Qt::SmoothTransformation. This gives us more packets per raster
2229 | // line.
2230 |
2231 | // Odd (prime?) numbers resulted in fewer scaling artifacts. A multiplier
2232 | // of 9 washed out colors a little too much.
2233 | //const int height_multiplier_ = 7;
2234 | void PacketList::drawNearOverlay()
2235 | {
2236 | if (create_near_overlay_) {
2237 | create_near_overlay_ = false;
2238 | }
2239 |
2240 | if (!cap_file_ || cap_file_->state != FILE_READ_DONE) return;
2241 |
2242 | if (!prefs.gui_packet_list_show_minimap) return;
2243 |
2244 | qreal dp_ratio = overlay_sb_->devicePixelRatio();
2245 | int o_height = overlay_sb_->height() * dp_ratio;
2246 | int o_rows = qMin(packet_list_model_->rowCount(), o_height);
2247 | QFontMetricsF fmf(mainApp->font());
2248 | int o_width = ((static_cast<int>(fmf.height())) * 2 * dp_ratio) + 2; // 2ems + 1-pixel border on either side.
2249 |
2250 | if (recent.packet_list_colorize && o_rows > 0) {
2251 | QImage overlay(o_width, o_height, QImage::Format_ARGB32_Premultiplied);
2252 |
2253 | QPainter painter(&overlay);
2254 |
2255 | overlay.fill(Qt::transparent);
2256 |
2257 | int cur_line = 0;
2258 | int start = 0;
2259 |
2260 | if (packet_list_model_->rowCount() > o_height && overlay_sb_->maximum() > 0) {
2261 | start += ((double) overlay_sb_->value() / overlay_sb_->maximum()) * (packet_list_model_->rowCount() - o_rows);
2262 | }
2263 | int end = start + o_rows;
2264 | for (int row = start; row < end; row++) {
2265 | packet_list_model_->ensureRowColorized(row);
2266 |
2267 | frame_data *fdata = packet_list_model_->getRowFdata(row);
2268 | int next_line = (row - start + 1) * o_height / o_rows;
2269 | int row_height = next_line - cur_line;
2270 |
2271 | // Multi-color support in minimap (enabled for all non-Off modes)
2272 | if (prefs.gui_packet_list_multi_color_mode != PACKET_LIST_MULTI_COLOR_MODE_OFF) {
2273 | QModelIndex idx = packet_list_model_->index(row, 0);
2274 | PacketListRecord *record = static_cast<PacketListRecord*>(idx.internalPointer());
2275 | // Conversation color filters take full precedence — skip multi-color stripe rendering
2276 | bool is_conversation_color = fdata->color_filter &&
2277 | strncmp(((const color_filter_t *)fdata->color_filter)->filter_name,
2278 | CONVERSATION_COLOR_PREFIX"___conversation_color_filter___", strlen(CONVERSATION_COLOR_PREFIX"___conversation_color_filter___")) == 0;
2279 | if (record && record->hasMultipleColors() && !is_conversation_color) {
2280 | // Draw vertical stripes for multiple colors (skip paused filters)
2281 | const GSList *filters = record->matchingColorFilters();
2282 |
2283 | // Count non-paused filters
2284 | int num_active_colors = 0;
2285 | for (const GSList *item = filters; item != NULL__null; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : __null)) {
2286 | const color_filter_t *colorf = (const color_filter_t *)item->data;
2287 | if (!color_filter_is_session_disabled(colorf->filter_name)) {
2288 | num_active_colors++;
2289 | }
2290 | }
2291 |
2292 | if (num_active_colors > 0) {
2293 | int stripe_width = o_width / num_active_colors;
2294 | int x_pos = 0;
2295 | int stripe_idx = 0;
2296 |
2297 | for (const GSList *item = filters; item != NULL__null; item = g_slist_next(item)((item) ? (((GSList *)(item))->next) : __null)) {
2298 | const color_filter_t *colorf = (const color_filter_t *)item->data;
2299 | // Skip paused filters
2300 | if (!color_filter_is_session_disabled(colorf->filter_name)) {
2301 | QColor color(ColorUtils::fromColorT(&colorf->bg_color));
2302 | int width = (stripe_idx == num_active_colors - 1) ? (o_width - x_pos) : stripe_width;
2303 | painter.fillRect(x_pos, cur_line, width, row_height, color);
2304 | x_pos += stripe_width;
2305 | stripe_idx++;
2306 | }
2307 | }
2308 | } else {
2309 | // All filters paused, draw no color
2310 | painter.fillRect(0, cur_line, o_width, row_height, QColor(Qt::white));
2311 | }
2312 | } else if (fdata->color_filter) {
2313 | // Single color fallback
2314 | const color_filter_t *color_filter = (const color_filter_t *) fdata->color_filter;
2315 | QColor color(ColorUtils::fromColorT(&color_filter->bg_color));
2316 | painter.fillRect(0, cur_line, o_width, row_height, color);
2317 | }
2318 | } else {
2319 | // Original single-color behavior
2320 | const color_t *bgcolor = NULL__null;
2321 | if (fdata->color_filter) {
2322 | const color_filter_t *color_filter = (const color_filter_t *) fdata->color_filter;
2323 | bgcolor = &color_filter->bg_color;
2324 | }
2325 |
2326 | if (bgcolor) {
2327 | QColor color(ColorUtils::fromColorT(bgcolor));
2328 | painter.fillRect(0, cur_line, o_width, row_height, color);
2329 | }
2330 | }
2331 |
2332 | cur_line = next_line;
2333 | }
2334 |
2335 | // If the selected packet is in the overlay set selected_pos
2336 | // accordingly. Otherwise, pin it to either the top or bottom.
2337 | QList<int> positions;
2338 | if (selectionModel()->hasSelection()) {
2339 |
2340 | QModelIndexList selRows = selectionModel()->selectedRows(0);
2341 | int last_row = -1;
2342 | int last_pos = -1;
2343 | foreach (QModelIndex idx, selRows)for (auto _container_2343 = QtPrivate::qMakeForeachContainer( selRows); _container_2343.i != _container_2343.e; ++_container_2343 .i) if (QModelIndex idx = *_container_2343.i; false) {} else
2344 | {
2345 | int selected_pos = -1;
2346 | int sel_row = idx.row();
2347 | if (sel_row < start) {
2348 | selected_pos = 0;
2349 | } else if (sel_row >= end) {
2350 | selected_pos = overlay.height() - 1;
2351 | } else {
2352 | selected_pos = (sel_row - start) * o_height / o_rows;
2353 | }
2354 |
2355 | /* Due to the difference in the display height, we sometimes get empty positions
2356 | * inbetween consecutive valid rows. If those are detected, they are signaled as
2357 | * being selected as well */
2358 | if (last_pos >= 0 && selected_pos > (last_pos + 1) && (last_row + 1) == sel_row)
2359 | {
2360 | for (int pos = (last_pos + 1); pos < selected_pos; pos++)
2361 | {
2362 | if (! positions.contains(pos))
2363 | positions << pos;
2364 | }
2365 | }
2366 | else if (selected_pos != -1 && ! positions.contains(selected_pos))
2367 | positions << selected_pos;
2368 |
2369 | last_row = sel_row;
2370 | last_pos = selected_pos;
2371 | }
2372 | }
2373 |
2374 | overlay_sb_->setNearOverlayImage(overlay, packet_list_model_->rowCount(), start, end, positions, (o_height / o_rows));
2375 | } else {
2376 | QImage overlay;
2377 | overlay_sb_->setNearOverlayImage(overlay);
2378 | }
2379 | }
2380 |
2381 | void PacketList::drawFarOverlay()
2382 | {
2383 | if (create_far_overlay_) {
2384 | create_far_overlay_ = false;
2385 | }
2386 |
2387 | if (!cap_file_ || cap_file_->state != FILE_READ_DONE) return;
2388 |
2389 | if (!prefs.gui_packet_list_show_minimap) return;
2390 |
2391 | QSize groove_size = overlay_sb_->grooveRect().size();
2392 | qreal dp_ratio = overlay_sb_->devicePixelRatio();
2393 | groove_size *= dp_ratio;
2394 | int o_width = groove_size.width();
2395 | int o_height = groove_size.height();
2396 | int pl_rows = packet_list_model_->rowCount();
2397 | QImage overlay(o_width, o_height, QImage::Format_ARGB32_Premultiplied);
2398 | bool have_marked_image = false;
2399 |
2400 | // If only there were references from popular culture about getting into
2401 | // some sort of groove.
2402 | if (!overlay.isNull() && recent.packet_list_colorize && pl_rows > 0) {
2403 |
2404 | QPainter painter(&overlay);
2405 |
2406 | // Draw text-colored tick marks on a transparent background.
2407 | // Hopefully no themes use the text color for the groove color.
2408 | overlay.fill(Qt::transparent);
2409 |
2410 | QColor tick_color = palette().text().color();
2411 | tick_color.setAlphaF(0.3f);
2412 | painter.setPen(tick_color);
2413 |
2414 | for (int row = 0; row < pl_rows; row++) {
2415 |
2416 | frame_data *fdata = packet_list_model_->getRowFdata(row);
2417 | if (fdata->marked || fdata->ref_time || fdata->ignored) {
2418 | int new_line = row * o_height / pl_rows;
2419 | int tick_width = o_width / 3;
2420 | // Marked or ignored: left side, time refs: right side.
2421 | // XXX Draw ignored ticks in the middle?
2422 | int x1 = fdata->ref_time ? o_width - tick_width : 1;
2423 | int x2 = fdata->ref_time ? o_width - 1 : tick_width;
2424 |
2425 | painter.drawLine(x1, new_line, x2, new_line);
2426 | have_marked_image = true;
2427 | }
2428 | }
2429 |
2430 | if (have_marked_image) {
2431 | overlay_sb_->setMarkedPacketImage(overlay);
2432 | return;
2433 | }
2434 | }
2435 |
2436 | if (!have_marked_image) {
2437 | QImage null_overlay;
2438 | overlay_sb_->setMarkedPacketImage(null_overlay);
2439 | }
2440 | }
2441 |
2442 | // Auto scroll if:
2443 | // - We are capturing
2444 | // - actionGoAutoScroll in the main UI is checked.
2445 |
2446 | // actionGoAutoScroll in the main UI:
2447 | // - Is set to the value of recent.capture_auto_scroll when beginning a capture
2448 | // - Can be triggered manually by the user
2449 | // - Is turned on if the last user-set vertical scrollbar position is at the
2450 | // end and recent.capture_auto_scroll is enabled
2451 | // - Is turned off if the last user-set vertical scrollbar is not at the end,
2452 | // or if one of the Go to Packet actions is used (XXX: Should keyboard
2453 | // navigation in keyPressEvent turn it off for similar reasons?)
2454 | void PacketList::rowsInserted(const QModelIndex &parent, int start, int end)
2455 | {
2456 | QTreeView::rowsInserted(parent, start, end);
2457 | if (recent.aggregation_view && currentIndex().isValid() && currentIndex().row() >= 0) {
2458 | selectRow(getFDataForRow(currentIndex().row()), false);
2459 | }
2460 | if (capture_in_progress_ && tail_at_end_) {
2461 | scrollToBottom();
2462 | }
2463 | }
2464 |
2465 | void PacketList::resizeAllColumns(bool onlyTimeFormatted)
2466 | {
2467 | if (!cap_file_ || cap_file_->state == FILE_CLOSED || cap_file_->state == FILE_READ_PENDING)
2468 | return;
2469 |
2470 | for (unsigned col = 0; col < cap_file_->cinfo.num_cols; col++) {
2471 | if (! onlyTimeFormatted || col_has_time_fmt(&cap_file_->cinfo, col)) {
2472 | resizeColumnToContents(col);
2473 | }
2474 | }
2475 | }
2476 |
2477 | bool PacketList::selectRow(const frame_data* fdata, bool flushRows)
2478 | {
2479 | PacketListModel* pktListModel = qobject_cast<PacketListModel*>(model());
2480 |
2481 | if (!pktListModel)
2482 | return false;
2483 |
2484 | if (flushRows) {
2485 | pktListModel->flushVisibleRows();
2486 | }
2487 | int row = -1;
2488 | if (!fdata)
2489 | row = 0;
2490 | else
2491 | row = pktListModel->visibleIndexOf(fdata);
2492 |
2493 | if (row >= 0) {
2494 | /* Calling ClearAndSelect with setCurrentIndex clears the "current"
2495 | * item, but doesn't clear the "selected" item. We want to clear
2496 | * the "selected" item as well so that selectionChanged() will be
2497 | * emitted in order to force an update of the packet details and
2498 | * packet bytes after a search.
2499 | */
2500 | selectionModel()->clearSelection();
2501 | selectionModel()->setCurrentIndex(pktListModel->index(row, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
2502 | scrollTo(currentIndex(), PacketList::PositionAtCenter);
2503 | return true;
2504 | }
2505 |
2506 | return false;
2507 | }