Fix HTML escaping for custom OSD notifications

Fixes #618
This commit is contained in:
Jonas Kvinge
2020-12-11 23:59:38 +01:00
parent 9149d1baa3
commit 497952611d
15 changed files with 219 additions and 112 deletions

View File

@@ -130,23 +130,38 @@ void OSDBase::ShowPlaying(const Song &song, const QUrl &cover_url, const QImage
QStringList message_parts;
QString summary;
bool html_escaped = false;
if (use_custom_text_) {
summary = ReplaceMessage(custom_text1_, song);
message_parts << ReplaceMessage(custom_text2_, song);
summary = ReplaceMessage(Type_Summary, custom_text1_, song);
message_parts << ReplaceMessage(Type_Message, custom_text2_, song);
}
else {
summary = song.PrettyTitle();
if (!song.artist().isEmpty())
if (!song.artist().isEmpty()) {
summary = QString("%1 - %2").arg(song.artist(), summary);
if (!song.album().isEmpty())
}
if (!song.album().isEmpty()) {
message_parts << song.album();
if (song.disc() > 0)
}
if (song.disc() > 0) {
message_parts << tr("disc %1").arg(song.disc());
if (song.track() > 0)
}
if (song.track() > 0) {
message_parts << tr("track %1").arg(song.track());
}
if (behaviour_ == Pretty) {
summary = summary.toHtmlEscaped();
html_escaped = true;
}
#if defined(HAVE_DBUS) && !defined(Q_OS_MACOS)
else if (behaviour_ == Native) {
html_escaped = true;
}
#endif
}
QString message = message_parts.join(", ");
if (html_escaped) message = message.toHtmlEscaped();
if (show_art_) {
ShowMessage(summary, message, "notification-audio-play", image);
@@ -172,7 +187,7 @@ void OSDBase::Paused() {
if (show_on_pause_) {
QString summary;
if (use_custom_text_) {
summary = ReplaceMessage(custom_text1_, last_song_);
summary = ReplaceMessage(Type_Summary, custom_text1_, last_song_);
}
else {
summary = last_song_.PrettyTitle();
@@ -180,8 +195,10 @@ void OSDBase::Paused() {
summary.prepend(" - ");
summary.prepend(last_song_.artist());
}
if (behaviour_ == Pretty) {
summary = summary.toHtmlEscaped();
}
}
summary = summary.remove('&').simplified();
if (show_art_) {
ShowMessage(summary, tr("Paused"), QString(), last_image_);
}
@@ -215,7 +232,7 @@ void OSDBase::Stopped() {
QString summary;
if (use_custom_text_) {
summary = ReplaceMessage(custom_text1_, last_song_);
summary = ReplaceMessage(Type_Summary, custom_text1_, last_song_);
}
else {
summary = last_song_.PrettyTitle();
@@ -223,10 +240,11 @@ void OSDBase::Stopped() {
summary.prepend(" - ");
summary.prepend(last_song_.artist());
}
if (behaviour_ == Pretty) {
summary = summary.toHtmlEscaped();
}
}
summary = summary.remove('&').simplified();
if (show_art_) {
ShowMessage(summary, tr("Stopped"), QString(), last_image_);
}
@@ -257,7 +275,17 @@ void OSDBase::VolumeChanged(int value) {
if (!show_on_volume_change_) return;
ShowMessage(app_name_, tr("Volume %1%").arg(value));
QString message = tr("Volume %1%").arg(value);
if (behaviour_ == Pretty) {
message = message.toHtmlEscaped();
}
#if defined(HAVE_DBUS) && !defined(Q_OS_MACOS)
else if (behaviour_ == Native) {
message = message.toHtmlEscaped();
}
#endif
ShowMessage(app_name_, message);
}
@@ -330,39 +358,51 @@ void OSDBase::RepeatModeChanged(PlaylistSequence::RepeatMode mode) {
}
QString OSDBase::ReplaceMessage(const QString &message, const Song &song) {
QString OSDBase::ReplaceMessage(const MessageType type, const QString &message, const Song &song) {
QString newline = "<br/>";
bool html_escaped = false;
QString newline = "";
if (message.indexOf("%newline%") != -1) {
// We need different strings depending on notification type
switch (behaviour_) {
case Native:
#ifdef Q_OS_MACOS
newline = "\n";
break;
// We need different strings depending on notification type
switch (behaviour_) {
case Native:
#if defined(Q_OS_MACOS)
html_escaped = false;
newline = "\n";
break;
#elif defined(HAVE_DBUS)
switch (type) {
case Type_Summary:{
html_escaped = false;
newline = "";
break;
}
case Type_Message: {
html_escaped = true;
newline = "<br />";
break;
}
}
break;
#else
// Other OSes doesn't support native notifications.
qLog(Debug) << "Native notifications are not supported on this OS.";
break;
#endif
#ifdef Q_OS_LINUX
break;
#endif
#ifdef Q_OS_WIN32
// Other OS don't support native notifications
qLog(Debug) << "New line not supported by this notification type under Windows";
newline = "";
break;
#endif
case TrayPopup:
qLog(Debug) << "New line not supported by this notification type";
newline = "";
break;
case Pretty:
default:
// When notifications are disabled, we force the PrettyOSD
break;
}
case TrayPopup:
qLog(Debug) << "New line not supported by this notification type.";
html_escaped = false;
newline = "";
break;
case Disabled: // When notifications are disabled, we force the PrettyOSD
case Pretty:
html_escaped = true;
newline = "<br />";
break;
}
return Utilities::ReplaceMessage(message, song, newline);
return Utilities::ReplaceMessage(message, song, newline, html_escaped);
}
void OSDBase::ShowPreview(const Behaviour type, const QString &line1, const QString &line2, const Song &song) {
@@ -390,5 +430,5 @@ bool OSDBase::SupportsTrayPopups() {
}
void OSDBase::ShowMessageNative(const QString&, const QString&, const QString&, const QImage&) {
qLog(Warning) << "Not implemented";
qLog(Warning) << "Native notifications are not supported on this OS.";
}

View File

@@ -83,9 +83,13 @@ class OSDBase : public QObject {
void ShowPreview(const Behaviour type, const QString &line1, const QString &line2, const Song &song);
private:
enum MessageType {
Type_Summary,
Type_Message,
};
void ShowPlaying(const Song &song, const QUrl &cover_url, const QImage &image, const bool preview = false);
void ShowMessage(const QString &summary, const QString &message = QString(), const QString icon = QString("strawberry"), const QImage &image = QImage());
QString ReplaceMessage(const QString &message, const Song &song);
QString ReplaceMessage(const MessageType type, const QString &message, const Song &song);
virtual void ShowMessageNative(const QString &summary, const QString &message, const QString &icon = QString(), const QImage &image = QImage());
private slots:

View File

@@ -144,7 +144,6 @@ void OSDDBus::ShowMessageNative(const QString &summary, const QString &message,
QVariantMap hints;
QString summary_stripped = summary;
summary_stripped = summary_stripped.remove(QRegularExpression("[&\"<>]")).simplified();
QString message_stripped = message.toHtmlEscaped();
if (!image.isNull()) {
if (version_ >= QVersionNumber(1, 2)) {
@@ -167,7 +166,7 @@ void OSDDBus::ShowMessageNative(const QString &summary, const QString &message,
id = notification_id_;
}
QDBusPendingReply<uint> reply = interface_->Notify(app_name(), id, icon, summary_stripped, message_stripped, QStringList(), hints, timeout_msec());
QDBusPendingReply<uint> reply = interface_->Notify(app_name(), id, icon, summary_stripped, message, QStringList(), hints, timeout_msec());
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(CallFinished(QDBusPendingCallWatcher*)));

View File

@@ -343,7 +343,7 @@ void OSDPretty::paintEvent(QPaintEvent *) {
}
void OSDPretty::SetMessage(const QString& summary, const QString& message, const QImage &image) {
void OSDPretty::SetMessage(const QString &summary, const QString& message, const QImage &image) {
if (!image.isNull()) {
QImage scaled_image = image.scaled(kMaxIconSize, kMaxIconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

View File

@@ -53,6 +53,9 @@
<height>16777215</height>
</size>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
@@ -72,6 +75,9 @@
<height>16777215</height>
</size>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>