diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,6 @@ endif() find_package(KF5Config ${KF5_DEP_VERSION} REQUIRED) -find_package(KF5Codecs ${KF5_DEP_VERSION} REQUIRED) find_package(KF5CoreAddons ${KF5_DEP_VERSION} REQUIRED) if (NOT WIN32 AND NOT ANDROID) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,7 +106,6 @@ target_link_libraries(KF5Notifications PRIVATE KF5::CoreAddons KF5::ConfigCore - KF5::Codecs ) if (TARGET KF5::WindowSystem) diff --git a/src/knotificationplugin.h b/src/knotificationplugin.h --- a/src/knotificationplugin.h +++ b/src/knotificationplugin.h @@ -23,6 +23,8 @@ #define KNOTIFICATIONPLUGIN_H #include +#include + #include #include "knotifications_export.h" @@ -94,6 +96,11 @@ */ void finish(KNotification *notification); + static inline QString stripRichText(const QString &s) + { + return QTextDocumentFragment::fromHtml(s).toPlainText(); + } + Q_SIGNALS: /** * the presentation is finished. diff --git a/src/notifybypopup.cpp b/src/notifybypopup.cpp --- a/src/notifybypopup.cpp +++ b/src/notifybypopup.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -52,7 +51,6 @@ #include #include -#include static const char dbusServiceName[] = "org.freedesktop.Notifications"; static const char dbusInterfaceName[] = "org.freedesktop.Notifications"; @@ -66,13 +64,7 @@ * Fills the KPassivePopup with data */ void fillPopup(KPassivePopup *popup, KNotification *notification, const KNotifyConfig &config); - /** - * Removes HTML from a given string. Replaces line breaks with \n and - * HTML entities by their 'normal forms'. - * @param string the HTML to remove. - * @return the cleaned string. - */ - QString stripHtml(const QString &text); + /** * Sends notification to DBus "org.freedesktop.notifications" interface. * @param id knotify-sid identifier of notification @@ -140,16 +132,6 @@ NotifyByPopup * const q; - - /** - * A class for resolving HTML entities in XML documents (used - * during HTML stripping) - */ - class HtmlEntityResolver : public QXmlStreamEntityResolver - { - QString resolveUndeclaredEntity(const QString &name) override; - }; - }; //--------------------------------------------------------------------------------------- @@ -616,10 +598,10 @@ if (!popupServerCapabilities.contains(QLatin1String("body-markup"))) { if (title.startsWith(QLatin1String(""))) { - title = stripHtml(title); + title = q->stripRichText(title); } if (text.startsWith(QLatin1String(""))) { - text = stripHtml(text); + text = q->stripRichText(text); } } @@ -788,50 +770,5 @@ } } -QString NotifyByPopupPrivate::stripHtml(const QString &text) -{ - QXmlStreamReader r(QStringLiteral("") + text + QStringLiteral("")); - HtmlEntityResolver resolver; - r.setEntityResolver(&resolver); - QString result; - while (!r.atEnd()) { - r.readNext(); - if (r.tokenType() == QXmlStreamReader::Characters) { - result.append(r.text()); - } else if (r.tokenType() == QXmlStreamReader::StartElement && r.name() == QLatin1String("br")) { - result.append(QLatin1Char('\n')); - } - } - if (r.hasError()) { - // XML error in the given text, just return the original string - qCWarning(LOG_KNOTIFICATIONS) << "Notification to send to backend which does " - "not support HTML, contains invalid XML:" - << r.errorString() << "line" << r.lineNumber() - << "col" << r.columnNumber(); - return text; - } - - return result; -} - -QString NotifyByPopupPrivate::HtmlEntityResolver::resolveUndeclaredEntity(const QString &name) -{ - QString result = QXmlStreamEntityResolver::resolveUndeclaredEntity(name); - - if (!result.isEmpty()) { - return result; - } - - QChar ent = KCharsets::fromEntity(QLatin1Char('&') + name); - - if (ent.isNull()) { - qCWarning(LOG_KNOTIFICATIONS) << "Notification to send to backend which does " - "not support HTML, contains invalid entity: " - << name; - ent = QLatin1Char(' '); - } - - return QString(ent); -} diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp --- a/src/notifybysnore.cpp +++ b/src/notifybysnore.cpp @@ -153,11 +153,11 @@ arguments << QStringLiteral("-t"); if (!notification->title().isEmpty()) { - arguments << notification->title(); + arguments << stripRichText(notification->title()); } else { arguments << qApp->applicationDisplayName(); } - arguments << QStringLiteral("-m") << notification->text(); + arguments << QStringLiteral("-m") << stripRichText(notification->text()); const QString iconPath = m_iconDir.path() + QLatin1Char('/') + QString::number(notification->id()) + QStringLiteral(".png"); if (!notification->pixmap().isNull()) {