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.";
}