2012-11-01 20:57:45 +00:00
|
|
|
/* print_dialog.cpp
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-04-30 09:47:58 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
#include "print_dialog.h"
|
2015-06-25 09:17:03 -07:00
|
|
|
#include <ui_print_dialog.h>
|
2012-11-01 20:57:45 +00:00
|
|
|
|
2015-10-04 18:10:29 +01:00
|
|
|
#include <wsutil/utf8_entities.h>
|
2015-07-31 10:34:43 -07:00
|
|
|
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#include "ui/packet_range.h"
|
|
|
|
#include "ui/win32/file_dlg_win32.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-01 20:57:45 +00:00
|
|
|
#include <QPrintDialog>
|
|
|
|
#include <QPageSetupDialog>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPaintEngine>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2022-01-31 19:30:09 -08:00
|
|
|
#include "main_application.h"
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
// Page element callbacks
|
|
|
|
|
2021-04-02 22:16:16 +02:00
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
static bool
|
|
|
|
print_preamble_pd(print_stream_t *self, char *, const char *)
|
2012-11-01 20:57:45 +00:00
|
|
|
{
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!self) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
PrintDialog *print_dlg = static_cast<PrintDialog *>(self->data);
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!print_dlg) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
return print_dlg->printHeader();
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
static bool
|
2012-11-01 20:57:45 +00:00
|
|
|
print_line_pd(print_stream_t *self, int indent, const char *line)
|
|
|
|
{
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!self) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
PrintDialog *print_dlg = static_cast<PrintDialog *>(self->data);
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!print_dlg) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
return print_dlg->printLine(indent, line);
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
static bool
|
2012-11-01 20:57:45 +00:00
|
|
|
new_page_pd(print_stream_t *self)
|
|
|
|
{
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!self) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
PrintDialog *print_dlg = static_cast<PrintDialog *>(self->data);
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!print_dlg) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
return print_dlg->printHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
|
2019-11-12 16:39:19 +01:00
|
|
|
PrintDialog::PrintDialog(QWidget *parent, capture_file *cf, QString selRange) :
|
2012-11-01 20:57:45 +00:00
|
|
|
QDialog(parent),
|
|
|
|
pd_ui_(new Ui::PrintDialog),
|
|
|
|
cur_printer_(NULL),
|
|
|
|
cur_painter_(NULL),
|
|
|
|
preview_(new QPrintPreviewWidget(&printer_)),
|
2020-09-28 17:40:05 -07:00
|
|
|
print_bt_(new QPushButton(tr("&Print…"))),
|
2016-07-03 22:31:32 +02:00
|
|
|
cap_file_(cf),
|
|
|
|
page_pos_(0),
|
2024-03-30 14:27:39 -07:00
|
|
|
in_preview_(false)
|
2012-11-01 20:57:45 +00:00
|
|
|
{
|
2018-05-21 22:33:37 +02:00
|
|
|
Q_ASSERT(cf);
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
pd_ui_->setupUi(this);
|
2022-01-31 19:30:09 -08:00
|
|
|
setWindowTitle(mainApp->windowTitleString(tr("Print")));
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
pd_ui_->previewLayout->insertWidget(0, preview_, Qt::AlignTop);
|
|
|
|
|
|
|
|
preview_->setMinimumWidth(preview_->height() / 2);
|
|
|
|
preview_->setToolTip(pd_ui_->zoomLabel->toolTip());
|
|
|
|
|
|
|
|
// XXX Make these configurable
|
|
|
|
header_font_.setFamily("Times");
|
|
|
|
header_font_.setPointSizeF(header_font_.pointSizeF() * 0.8);
|
2022-01-31 19:30:09 -08:00
|
|
|
packet_font_ = mainApp->monospaceFont();
|
2012-11-01 20:57:45 +00:00
|
|
|
packet_font_.setPointSizeF(packet_font_.pointSizeF() * 0.8);
|
|
|
|
|
|
|
|
memset(&print_args_, 0, sizeof(print_args_));
|
|
|
|
memset(&stream_ops_, 0, sizeof(stream_ops_));
|
|
|
|
|
|
|
|
/* Init the export range */
|
|
|
|
packet_range_init(&print_args_.range, cap_file_);
|
|
|
|
/* Default to displayed packets */
|
2024-03-30 14:27:39 -07:00
|
|
|
print_args_.range.process_filtered = true;
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
stream_ops_.print_preamble = print_preamble_pd;
|
|
|
|
stream_ops_.print_line = print_line_pd;
|
|
|
|
stream_ops_.new_page = new_page_pd;
|
|
|
|
|
|
|
|
stream_.data = this;
|
|
|
|
stream_.ops = &stream_ops_;
|
|
|
|
print_args_.stream = &stream_;
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
char *display_basename = g_filename_display_basename(cap_file_->filename);
|
2012-11-01 20:57:45 +00:00
|
|
|
printer_.setDocName(display_basename);
|
|
|
|
g_free(display_basename);
|
|
|
|
|
2019-11-12 16:39:19 +01:00
|
|
|
pd_ui_->rangeGroupBox->initRange(&print_args_.range, selRange);
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
pd_ui_->buttonBox->addButton(print_bt_, QDialogButtonBox::ActionRole);
|
2020-09-28 17:40:05 -07:00
|
|
|
pd_ui_->buttonBox->addButton(tr("Page &Setup…"), QDialogButtonBox::ResetRole);
|
2012-11-01 20:57:45 +00:00
|
|
|
print_bt_->setDefault(true);
|
|
|
|
|
|
|
|
connect(preview_, SIGNAL(paintRequested(QPrinter*)), this, SLOT(paintPreview(QPrinter*)));
|
|
|
|
connect(pd_ui_->rangeGroupBox, SIGNAL(rangeChanged()),
|
|
|
|
this, SLOT(checkValidity()));
|
|
|
|
connect(pd_ui_->formatGroupBox, SIGNAL(formatChanged()),
|
|
|
|
this, SLOT(checkValidity()));
|
|
|
|
connect(pd_ui_->formFeedCheckBox, SIGNAL(toggled(bool)),
|
|
|
|
preview_, SLOT(updatePreview()));
|
2021-04-02 22:16:16 +02:00
|
|
|
connect(pd_ui_->bannerCheckBox, SIGNAL(toggled(bool)),
|
|
|
|
preview_, SLOT(updatePreview()));
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
checkValidity();
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintDialog::~PrintDialog()
|
|
|
|
{
|
2018-05-21 22:33:37 +02:00
|
|
|
packet_range_cleanup(&print_args_.range);
|
2012-11-01 20:57:45 +00:00
|
|
|
delete pd_ui_;
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
bool PrintDialog::printHeader()
|
2012-11-01 20:57:45 +00:00
|
|
|
{
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!cap_file_ || !cap_file_->filename || !cur_printer_ || !cur_painter_) return false;
|
2020-11-23 22:08:04 +01:00
|
|
|
int page_top = cur_printer_->pageLayout().paintRectPixels(cur_printer_->resolution()).top();
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
if (page_pos_ > page_top) {
|
|
|
|
if (in_preview_) {
|
2016-02-29 18:52:52 -08:00
|
|
|
// When generating a preview, only generate the first page;
|
|
|
|
// if we're past the first page, stop the printing process.
|
2024-03-30 14:27:39 -07:00
|
|
|
return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
|
|
|
// Second and subsequent pages only
|
|
|
|
cur_printer_->newPage();
|
|
|
|
page_pos_ = page_top;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:16:16 +02:00
|
|
|
if (pd_ui_->bannerCheckBox->isChecked()) {
|
2024-10-29 02:35:01 +08:00
|
|
|
QString banner = tr("%1 %2 total packets, %3 shown")
|
2021-04-02 22:16:16 +02:00
|
|
|
.arg(cap_file_->filename)
|
|
|
|
.arg(cap_file_->count)
|
2024-06-04 08:49:38 -04:00
|
|
|
.arg(packet_range_count(&print_args_.range));
|
2021-04-02 22:16:16 +02:00
|
|
|
cur_painter_->setFont(header_font_);
|
|
|
|
cur_painter_->drawText(0, page_top, banner);
|
|
|
|
}
|
2012-11-01 20:57:45 +00:00
|
|
|
page_pos_ += cur_painter_->fontMetrics().height();
|
|
|
|
cur_painter_->setFont(packet_font_);
|
2024-03-30 14:27:39 -07:00
|
|
|
return true;
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
bool PrintDialog::printLine(int indent, const char *line)
|
2012-11-01 20:57:45 +00:00
|
|
|
{
|
2020-11-23 22:08:04 +01:00
|
|
|
QRect out_rect, page_rect;
|
2012-11-01 20:57:45 +00:00
|
|
|
QString out_line;
|
|
|
|
|
2024-03-30 14:27:39 -07:00
|
|
|
if (!line || !cur_printer_ || !cur_painter_) return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
/* Prepare the tabs for printing, depending on tree level */
|
|
|
|
out_line.fill(' ', indent * 4);
|
|
|
|
out_line += line;
|
|
|
|
|
2020-11-23 22:08:04 +01:00
|
|
|
page_rect = cur_printer_->pageLayout().paintRectPixels(cur_printer_->resolution());
|
|
|
|
|
|
|
|
out_rect = cur_painter_->boundingRect(page_rect, Qt::TextWordWrap, out_line);
|
2012-11-01 20:57:45 +00:00
|
|
|
|
2020-11-23 22:08:04 +01:00
|
|
|
if (page_rect.height() < page_pos_ + out_rect.height()) {
|
2018-07-09 20:41:07 -07:00
|
|
|
//
|
|
|
|
// We're past the end of the page, so this line will be on
|
|
|
|
// the next page.
|
|
|
|
//
|
2012-11-01 20:57:45 +00:00
|
|
|
if (in_preview_) {
|
2016-02-29 18:52:52 -08:00
|
|
|
// When generating a preview, only generate the first page;
|
|
|
|
// if we're past the first page, stop the printing process.
|
2024-03-30 14:27:39 -07:00
|
|
|
return false;
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
2018-07-09 21:10:51 -07:00
|
|
|
if (*line == '\0') {
|
|
|
|
// This is an empty line, so it's a separator; no need to
|
|
|
|
// waste space printing it at the top of a page, as the
|
|
|
|
// page break suffices as a separator.
|
2024-03-30 14:27:39 -07:00
|
|
|
return true;
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
|
|
|
printHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
out_rect.translate(0, page_pos_);
|
|
|
|
cur_painter_->drawText(out_rect, Qt::TextWordWrap, out_line);
|
|
|
|
page_pos_ += out_rect.height();
|
2024-03-30 14:27:39 -07:00
|
|
|
return true;
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Protected
|
|
|
|
|
|
|
|
void PrintDialog::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
// XXX - This differs from the main window but matches other applications (e.g. Mozilla and Safari)
|
|
|
|
switch(event->key()) {
|
|
|
|
case Qt::Key_Minus:
|
|
|
|
case Qt::Key_Underscore: // Shifted minus on U.S. keyboards
|
|
|
|
preview_->zoomOut();
|
|
|
|
break;
|
|
|
|
case Qt::Key_Plus:
|
|
|
|
case Qt::Key_Equal: // Unshifted plus on U.S. keyboards
|
|
|
|
preview_->zoomIn();
|
|
|
|
break;
|
|
|
|
case Qt::Key_0:
|
|
|
|
case Qt::Key_ParenRight: // Shifted 0 on U.S. keyboards
|
|
|
|
// fitInView doesn't grow (at least in Qt 4.8.2) so make sure it shrinks.
|
|
|
|
preview_->setUpdatesEnabled(false);
|
|
|
|
preview_->setZoomFactor(1.0);
|
|
|
|
preview_->fitInView();
|
|
|
|
preview_->setUpdatesEnabled(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
void PrintDialog::printPackets(QPrinter *printer, bool in_preview)
|
|
|
|
{
|
|
|
|
QPainter painter;
|
|
|
|
|
|
|
|
if (!printer) return;
|
|
|
|
|
2021-04-01 09:17:49 +02:00
|
|
|
page_pos_ = printer->pageLayout().paintRectPixels(printer->resolution()).top();
|
2012-11-01 20:57:45 +00:00
|
|
|
in_preview_ = in_preview;
|
|
|
|
|
|
|
|
/* Fill in our print args */
|
|
|
|
|
Qt: Enable JSON no duplicate keys options from the GUI
Enable access to the JSON "no-duplicate-keys" option available in
tshark in the GUI as well. Continue to default to it off, same as
in tshark.
As part of this, have the Export Dissections dialog packet format
group box be a stacked widget, allowing different per format
options. Note the current options are only valid for text (and
PostScript, but Export Dissections doesn't do that.) This could allow
support for CSV options, e.g. see #14260.
Have the different format group boxes be responsible for checking
validity and setting the print arguments, to reduce duplicate code
between Export Dissections and Print, and keep those widgets from
having to understand details of the group boxes.
Note that the current "no duplicate keys" format has limitations of
its own, because it doesn't preserve order in cases where there
are multiple siblings of the same field at the same tree level but
not consecutive (i.e., with other fields between them.) They will
be placed together. A different strategy, that involves even more
use of arrays, would be necessary to preserve order. (See also
issue #12958.)
Ping #13904, #19295, #19329
2025-03-01 20:27:46 -05:00
|
|
|
pd_ui_->formatGroupBox->updatePrintArgs(print_args_);
|
2012-11-01 20:57:45 +00:00
|
|
|
print_args_.print_formfeed = pd_ui_->formFeedCheckBox->isChecked();
|
|
|
|
|
|
|
|
// This should be identical to printer_. However, the QPrintPreviewWidget docs
|
|
|
|
// tell us to draw on the printer handed to us by the paintRequested() signal.
|
|
|
|
cur_printer_ = printer;
|
|
|
|
cur_painter_ = &painter;
|
|
|
|
if (!painter.begin(printer)) {
|
|
|
|
QMessageBox::warning(this, tr("Print Error"),
|
2024-10-29 02:35:01 +08:00
|
|
|
tr("Unable to print to %1.").arg(printer_.printerName()),
|
2012-11-01 20:57:45 +00:00
|
|
|
QMessageBox::Ok);
|
|
|
|
close();
|
|
|
|
}
|
2016-02-29 18:52:52 -08:00
|
|
|
// Don't show a progress bar if we're previewing; if it takes a
|
|
|
|
// significant amount of time to generate a preview of the first
|
|
|
|
// page, We Have A Real Problem
|
2024-03-30 14:27:39 -07:00
|
|
|
cf_print_packets(cap_file_, &print_args_, in_preview ? false : true);
|
2012-11-01 20:57:45 +00:00
|
|
|
cur_printer_ = NULL;
|
|
|
|
cur_painter_ = NULL;
|
|
|
|
painter.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::paintPreview(QPrinter *printer)
|
|
|
|
{
|
|
|
|
printPackets(printer, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::checkValidity()
|
|
|
|
{
|
|
|
|
bool enable = true;
|
|
|
|
|
|
|
|
if (!pd_ui_->rangeGroupBox->isValid()) enable = false;
|
|
|
|
|
Qt: Enable JSON no duplicate keys options from the GUI
Enable access to the JSON "no-duplicate-keys" option available in
tshark in the GUI as well. Continue to default to it off, same as
in tshark.
As part of this, have the Export Dissections dialog packet format
group box be a stacked widget, allowing different per format
options. Note the current options are only valid for text (and
PostScript, but Export Dissections doesn't do that.) This could allow
support for CSV options, e.g. see #14260.
Have the different format group boxes be responsible for checking
validity and setting the print arguments, to reduce duplicate code
between Export Dissections and Print, and keep those widgets from
having to understand details of the group boxes.
Note that the current "no duplicate keys" format has limitations of
its own, because it doesn't preserve order in cases where there
are multiple siblings of the same field at the same tree level but
not consecutive (i.e., with other fields between them.) They will
be placed together. A different strategy, that involves even more
use of arrays, would be necessary to preserve order. (See also
issue #12958.)
Ping #13904, #19295, #19329
2025-03-01 20:27:46 -05:00
|
|
|
if (!pd_ui_->formatGroupBox->isValid())
|
2016-01-23 13:18:30 +01:00
|
|
|
{
|
|
|
|
enable = false;
|
|
|
|
}
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
print_bt_->setEnabled(enable);
|
|
|
|
preview_->updatePreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::on_buttonBox_helpRequested()
|
|
|
|
{
|
2022-01-31 19:30:09 -08:00
|
|
|
mainApp->helpTopicAction(HELP_PRINT_DIALOG);
|
2012-11-01 20:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::on_buttonBox_clicked(QAbstractButton *button)
|
|
|
|
{
|
|
|
|
QPrintDialog *print_dlg;
|
|
|
|
QPageSetupDialog *ps_dlg;
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
HANDLE da_ctx;
|
|
|
|
#endif
|
2012-11-01 20:57:45 +00:00
|
|
|
|
|
|
|
switch (pd_ui_->buttonBox->buttonRole(button)) {
|
|
|
|
case QDialogButtonBox::ActionRole:
|
|
|
|
int result;
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
da_ctx = set_thread_per_monitor_v2_awareness();
|
|
|
|
#endif
|
2012-11-01 20:57:45 +00:00
|
|
|
print_dlg = new QPrintDialog(&printer_, this);
|
|
|
|
result = print_dlg->exec();
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
revert_thread_per_monitor_v2_awareness(da_ctx);
|
|
|
|
#endif
|
2012-11-01 20:57:45 +00:00
|
|
|
if (result == QDialog::Accepted) {
|
|
|
|
printPackets(&printer_, false);
|
|
|
|
done(result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QDialogButtonBox::ResetRole:
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
da_ctx = set_thread_per_monitor_v2_awareness();
|
|
|
|
#endif
|
2012-11-01 20:57:45 +00:00
|
|
|
ps_dlg = new QPageSetupDialog(&printer_, this);
|
|
|
|
ps_dlg->exec();
|
2018-06-11 11:18:35 -07:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
revert_thread_per_monitor_v2_awareness(da_ctx);
|
|
|
|
#endif
|
2012-11-01 20:57:45 +00:00
|
|
|
preview_->updatePreview();
|
|
|
|
break;
|
|
|
|
default: // Help, Cancel
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|