Qt: Add html_escape to qt_ui_utils.

Add an html_escape convenience function, which escapes HTML
metacharacters using Qt::escape on Qt4 and QString::toHtmlEscaped on
Qt5. Use it where we were previously using #if QT_VERSION and calling
the API-specific functions.

Change-Id: Ifda3e9634a37fc00bdb46e08d5711f934692fef5
Reviewed-on: https://code.wireshark.org/review/17984
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Gerald Combs 2016-09-29 11:25:52 -07:00 committed by Roland Knall
parent 5f69295c47
commit 1f633cfc84
7 changed files with 34 additions and 64 deletions

View File

@ -516,12 +516,7 @@ void CaptureFilePropertiesDialog::fillDetails()
if (!file_comments.isEmpty()) {
QString file_comments_html;
QString comment_escaped;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
comment_escaped = Qt::escape(file_comments);
#else
comment_escaped = file_comments.toHtmlEscaped();
#endif
QString comment_escaped = html_escape(file_comments);
file_comments_html = section_tmpl_.arg(tr("File Comment"));
file_comments_html += para_tmpl_.arg(comment_escaped);
@ -541,11 +536,7 @@ void CaptureFilePropertiesDialog::fillDetails()
QString frame_comment_html = tr("<p>Frame %1: ").arg(framenum);
QString raw_comment = pkt_comment;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
frame_comment_html += Qt::escape(raw_comment);
#else
frame_comment_html += raw_comment.toHtmlEscaped();
#endif
frame_comment_html += html_escape(raw_comment);
frame_comment_html += "</p>\n";
cursor.insertBlock();
cursor.insertHtml(frame_comment_html);

View File

@ -42,10 +42,6 @@
#include <QRadioButton>
#include <QScrollBar>
#include <QSpacerItem>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt::escape
#include <QTextDocument>
#endif
Q_DECLARE_METATYPE(pref_t *)
@ -63,7 +59,7 @@ static const QString title_to_shortcut(const char *title) {
extern "C" {
// Callbacks prefs routines
/* show a single preference on the GtkGrid of a preference page */
/* Add a single preference to the QVBoxLayout of a preference page */
static guint
pref_show(pref_t *pref, gpointer layout_ptr)
{
@ -72,12 +68,7 @@ pref_show(pref_t *pref, gpointer layout_ptr)
if (!pref || !vb) return 0;
// Convert the pref description from plain text to rich text.
QString description;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
description = Qt::escape(pref->description);
#else
description = QString(pref->description).toHtmlEscaped();
#endif
QString description = html_escape(pref->description);
description.replace('\n', "<br>");
QString tooltip = QString("<span>%1</span>").arg(description);

View File

@ -38,11 +38,6 @@
#include <QPushButton>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt::escape
#include <QTextDocument>
#endif
// To do:
// - Handle PREF_FILENAME and PREF_DIRNAME.
@ -85,12 +80,7 @@ void PreferenceEditorFrame::editPreference(preference *pref, pref_module *module
ui->preferenceTitleLabel->setText(QString("%1:").arg(pref->title));
// Convert the pref description from plain text to rich text.
QString description;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
description = Qt::escape(pref->description);
#else
description = QString(pref->description).toHtmlEscaped();
#endif
QString description = html_escape(pref->description);
description.replace('\n', "<br>");
QString tooltip = QString("<span>%1</span>").arg(description);
ui->preferenceTitleLabel->setToolTip(tooltip);

View File

@ -47,6 +47,11 @@
#include <QUrl>
#include <QUuid>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt::escape
#include <QTextDocument>
#endif
/* Make the format_size_flags_e enum usable in C++ */
format_size_flags_e operator|(format_size_flags_e lhs, format_size_flags_e rhs) {
return (format_size_flags_e) ((int)lhs| (int)rhs);
@ -176,6 +181,15 @@ const QString time_t_to_qstring(time_t ti_time)
return time_str;
}
QString html_escape(const QString plain_string) {
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
return Qt::escape(plain_string);
#else
return plain_string.toHtmlEscaped();
#endif
}
void smooth_font_size(QFont &font) {
QFontDatabase fdb;
#if QT_VERSION < QT_VERSION_CHECK(4, 8, 0)

View File

@ -171,6 +171,14 @@ const QString file_size_to_qstring(const gint64 size);
*/
const QString time_t_to_qstring(time_t ti_time);
/** Escape HTML metacharacters in a string.
*
* @param plain_string String to convert.
*
* @return A QString with escaped metacharacters.
*/
QString html_escape(const QString plain_string);
/**
* Round the current size of a font up to its next "smooth" size.
* If a smooth size can't be found the font is left unchanged.
@ -211,6 +219,7 @@ void desktop_show_in_folder(const QString file_path);
* screens, false otherwise.
*/
bool rect_on_screen(const QRect &rect);
#endif /* __QT_UI_UTILS__H__ */
// XXX Add a routine to fetch the HWND corresponding to a widget using QPlatformIntegration

View File

@ -29,6 +29,7 @@
#include "color_utils.h"
#include "progress_frame.h"
#include "qt_ui_utils.h"
#include "sequence_diagram.h"
#include "wireshark_application.h"
@ -36,10 +37,6 @@
#include <QFileDialog>
#include <QFontMetrics>
#include <QPoint>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt::escape
#include <QTextDocument>
#endif
// To do:
// - Resize or show + hide the Time and Comment axes, possibly via one of
@ -324,11 +321,7 @@ void SequenceDialog::mouseMoved(QMouseEvent *event)
seq_analysis_item_t *sai = seq_diagram_->itemForPosY(event->pos().y());
if (sai) {
packet_num_ = sai->frame_number;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QString raw_comment = Qt::escape(sai->comment);
#else
QString raw_comment = QString(sai->comment).toHtmlEscaped();
#endif
QString raw_comment = html_escape(sai->comment);
hint = QString("Packet %1: %2").arg(packet_num_).arg(raw_comment);
}
}

View File

@ -46,16 +46,13 @@
#include "wsutil/file_util.h"
#include "progress_frame.h"
#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QClipboard>
#include <QContextMenuEvent>
#include <QMessageBox>
#include <QFileDialog>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// Qt::escape
#include <QTextDocument>
#endif
// The GTK+ counterpart uses tap_param_dlg, which we don't use. If we
// need tap parameters we should probably create a TapParameterDialog
@ -306,22 +303,12 @@ QByteArray TapParameterDialog::getTreeAsString(st_format_type format)
{
// XXX What's a useful format? This mostly conforms to DocBook.
ba.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
QString title;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
title = Qt::escape(windowSubtitle());
#else
title = QString(windowSubtitle()).toHtmlEscaped();
#endif
QString title = html_escape(windowSubtitle());
QString xml_header = QString("<table>\n<title>%1</title>\n").arg(title);
ba.append(xml_header.toUtf8());
ba.append("<thead>\n<row>\n");
for (int col = 0; col < ui->statsTreeWidget->columnCount(); col++) {
title = ui->statsTreeWidget->headerItem()->text(col);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
title = Qt::escape(title);
#else
title = title.toHtmlEscaped();
#endif
title = html_escape(ui->statsTreeWidget->headerItem()->text(col));
title = QString(" <entry>%1</entry>\n").arg(title);
ba.append(title.toUtf8());
}
@ -385,12 +372,7 @@ QByteArray TapParameterDialog::getTreeAsString(st_format_type format)
{
line = "<row>\n";
foreach (QVariant var, tid) {
QString entry;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
entry = Qt::escape(var.toString());
#else
entry = var.toString().toHtmlEscaped();
#endif
QString entry = html_escape(var.toString());
line.append(QString(" <entry>%1</entry>\n").arg(entry));
}
line.append("</row>\n");