diff --git a/dataengines/notifications/CMakeLists.txt b/dataengines/notifications/CMakeLists.txt --- a/dataengines/notifications/CMakeLists.txt +++ b/dataengines/notifications/CMakeLists.txt @@ -1,21 +1,16 @@ -include(ECMMarkAsTest) - add_definitions(-DTRANSLATION_DOMAIN=\"plasma_engine_notifications\") set(notifications_engine_SRCS notificationsengine.cpp notificationservice.cpp notificationaction.cpp - notificationsanitizer.cpp ) ecm_qt_declare_logging_category(notifications_engine_SRCS HEADER debug.h IDENTIFIER NOTIFICATIONS CATEGORY_NAME kde.dataengine.notifications` DEFAULT_SEVERITY Info) -qt5_add_dbus_adaptor( notifications_engine_SRCS org.freedesktop.Notifications.xml notificationsengine.h NotificationsEngine ) - add_library(plasma_engine_notifications MODULE ${notifications_engine_SRCS}) target_link_libraries(plasma_engine_notifications @@ -27,26 +22,11 @@ KF5::Plasma KF5::Service KF5::NotifyConfig + PW::LibNotificationManager ) kcoreaddons_desktop_to_json(plasma_engine_notifications plasma-dataengine-notifications.desktop) install(TARGETS plasma_engine_notifications DESTINATION ${KDE_INSTALL_PLUGINDIR}/plasma/dataengine) install(FILES plasma-dataengine-notifications.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR} ) install(FILES notifications.operations DESTINATION ${PLASMA_DATA_INSTALL_DIR}/services) - - -#unit test -set(notifications_test_SRCS - notificationsanitizer.cpp - notifications_test.cpp -) - -ecm_qt_declare_logging_category(notifications_test_SRCS HEADER debug.h - IDENTIFIER NOTIFICATIONS - CATEGORY_NAME kde.dataengine.notifications` - DEFAULT_SEVERITY Info) - -add_executable(notification_test ${notifications_test_SRCS}) -target_link_libraries(notification_test Qt5::Test Qt5::Core) -ecm_mark_as_test(notification_test) diff --git a/dataengines/notifications/notificationaction.cpp b/dataengines/notifications/notificationaction.cpp --- a/dataengines/notifications/notificationaction.cpp +++ b/dataengines/notifications/notificationaction.cpp @@ -19,10 +19,14 @@ #include "notificationaction.h" #include "notificationsengine.h" +#include "server.h" + #include #include "debug.h" +using namespace NotificationManager; + void NotificationAction::start() { qCDebug(NOTIFICATIONS) << "Trying to perform the action " << operationName() << " on " << destination(); @@ -50,7 +54,7 @@ if (operationName() == QLatin1String("invokeAction")) { qCDebug(NOTIFICATIONS) << "invoking action on " << id; - emit m_engine->ActionInvoked(id, parameters()[QStringLiteral("actionId")].toString()); + Server::self().invokeAction(id, parameters()[QStringLiteral("actionId")].toString()); } else if (operationName() == QLatin1String("userClosed")) { //userClosedNotification deletes the job, so we have to invoke it queued, in this case emitResult() can be called m_engine->metaObject()->invokeMethod(m_engine, "removeNotification", Qt::QueuedConnection, Q_ARG(uint, id), Q_ARG(uint, 2)); diff --git a/dataengines/notifications/notifications_test.cpp b/dataengines/notifications/notifications_test.cpp deleted file mode 100644 --- a/dataengines/notifications/notifications_test.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2017 David Edmundson - * - * This program is free software you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include -#include -#include -#include "notificationsanitizer.h" - -class NotificationTest : public QObject -{ - Q_OBJECT -public: - NotificationTest() {} -private Q_SLOTS: - void parse_data(); - void parse(); -}; - -void NotificationTest::parse_data() -{ - QTest::addColumn("messageIn"); - QTest::addColumn("expectedOut"); - - QTest::newRow("basic no HTML") << "I am a notification" << "I am a notification"; - QTest::newRow("whitespace") << " I am a notification " << "I am a notification"; - - QTest::newRow("basic html") << "I am the notification" << "I am the notification"; - QTest::newRow("nested html") << "I am the notification" << "I am the notification"; - - QTest::newRow("no extra tags") << "I am the notification" << "I am the notification"; - QTest::newRow("no extra attrs") << "I am the notification" << "I am the notification"; - - QTest::newRow("newlines") << "I am\nthe\nnotification" << "I am
the
notification"; - QTest::newRow("multinewlines") << "I am\n\nthe\n\n\nnotification" << "I am
the
notification"; - - QTest::newRow("amp") << "me&you" << "me&you"; - QTest::newRow("double escape") << "foo & <bar>" << "foo & <bar>"; - - QTest::newRow("quotes") << "'foo'" << "'foo'";//as label can't handle this normally valid entity - - QTest::newRow("image normal") << "This is \"cheese\"/ and more text" << "This is \"cheese\"/ and more text"; - - //this input is technically wrong, so the output is also wrong, but QTextHtmlParser does the "right" thing - QTest::newRow("image normal no close") << "This is \"cheese\" and more text" << "This is \"cheese\" and more text"; - - QTest::newRow("image remote URL") << "This is \"cheese\" and more text" << "This is \"cheese\"/ and more text"; - - //more bad formatted options. To some extent actual output doesn't matter. Garbage in, garbabe out. - //the important thing is that it doesn't contain anything that could be parsed as the remote URL - QTest::newRow("image remote URL no close") << "This is \" alt=\"cheese\"> and more text" << "This is \"cheese\" and more text"; - QTest::newRow("image remote URL double open") << "This is <\" and more text" << "This is "; - QTest::newRow("image remote URL no entitiy close") << "This is \"cheese\" and more text" << "This is "; - - QTest::newRow("link") << "This is a link and more text" << "This is a link and more text"; -} - -void NotificationTest::parse() -{ - QFETCH(QString, messageIn); - QFETCH(QString, expectedOut); - - const QString out = NotificationSanitizer::parse(messageIn); - expectedOut = "" + expectedOut + "\n"; - QCOMPARE(out, expectedOut); -} - - -QTEST_GUILESS_MAIN(NotificationTest) - -#include "notifications_test.moc" diff --git a/dataengines/notifications/notificationsanitizer.h b/dataengines/notifications/notificationsanitizer.h deleted file mode 100644 --- a/dataengines/notifications/notificationsanitizer.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2017 David Edmundson - * - * This program is free software you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#ifndef NOTIFICATIONSANITIZER_H -#define NOTIFICATIONSANITIZER_H - -#include - -namespace NotificationSanitizer -{ - /* - * This turns generic random text of either plain text of any degree of faux-HTML into HTML allowed - * in the notification spec namely: - * a, img, b, i, u, and br - * In addition to the notification spec the following tags are allowed: - * table, tr, and td - * All other tags and attributes are stripped - * Whitespace is stripped and converted to
- * Double newlines are compressed - * - * Image src is only copied when referring to a local file - */ - QString parse(const QString &in); -} - -#endif diff --git a/dataengines/notifications/notificationsanitizer.cpp b/dataengines/notifications/notificationsanitizer.cpp deleted file mode 100644 --- a/dataengines/notifications/notificationsanitizer.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2017 David Edmundson - * - * This program is free software you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "notificationsanitizer.h" - -#include -#include -#include -#include - -#include "debug.h" - -QString NotificationSanitizer::parse(const QString &text) -{ - // replace all \ns with
- QString t = text; - - t.replace(QLatin1String("\n"), QStringLiteral("
")); - // Now remove all inner whitespace (\ns are already
s) - t = t.simplified(); - // Finally, check if we don't have multiple
s following, - // can happen for example when "\n \n" is sent, this replaces - // all
s in succsession with just one - t.replace(QRegularExpression(QStringLiteral("
\\s*
(\\s|
)*")), QLatin1String("
")); - // This fancy RegExp escapes every occurrence of & since QtQuick Text will blatantly cut off - // text where it finds a stray ampersand. - // Only &{apos, quot, gt, lt, amp}; as well as { character references will be allowed - t.replace(QRegularExpression(QStringLiteral("&(?!(?:apos|quot|[gl]t|amp);|#)")), QLatin1String("&")); - - QXmlStreamReader r(QStringLiteral("") + t + QStringLiteral("")); - QString result; - QXmlStreamWriter out(&result); - - const QVector allowedTags = {"b", "i", "u", "img", "a", "html", "br", "table", "tr", "td"}; - - out.writeStartDocument(); - while (!r.atEnd()) { - r.readNext(); - - if (r.tokenType() == QXmlStreamReader::StartElement) { - const QString name = r.name().toString(); - if (!allowedTags.contains(name)) { - continue; - } - out.writeStartElement(name); - if (name == QLatin1String("img")) { - auto src = r.attributes().value("src").toString(); - auto alt = r.attributes().value("alt").toString(); - - const QUrl url(src); - if (url.isLocalFile()) { - out.writeAttribute(QStringLiteral("src"), src); - } else { - //image denied for security reasons! Do not copy the image src here! - } - - out.writeAttribute(QStringLiteral("alt"), alt); - } - if (name == QLatin1String("a")) { - out.writeAttribute(QStringLiteral("href"), r.attributes().value("href").toString()); - } - } - - if (r.tokenType() == QXmlStreamReader::EndElement) { - const QString name = r.name().toString(); - if (!allowedTags.contains(name)) { - continue; - } - out.writeEndElement(); - } - - if (r.tokenType() == QXmlStreamReader::Characters) { - const auto text = r.text().toString(); - out.writeCharacters(text); //this auto escapes chars -> HTML entities - } - } - out.writeEndDocument(); - - if (r.hasError()) { - qCWarning(NOTIFICATIONS) << "Notification to send to backend contains invalid XML: " - << r.errorString() << "line" << r.lineNumber() - << "col" << r.columnNumber(); - } - - // The Text.StyledText format handles only html3.2 stuff and ' is html4 stuff - // so we need to replace it here otherwise it will not render at all. - result = result.replace(QLatin1String("'"), QChar('\'')); - - - return result; -} diff --git a/dataengines/notifications/notificationsengine.h b/dataengines/notifications/notificationsengine.h --- a/dataengines/notifications/notificationsengine.h +++ b/dataengines/notifications/notificationsengine.h @@ -25,6 +25,11 @@ #include #include +namespace NotificationManager +{ +class Notification; +} + struct NotificationInhibiton { QString hint; @@ -53,14 +58,8 @@ */ uint Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, const QString &body, const QStringList &actions, const QVariantMap &hints, int timeout); - void CloseNotification( uint id ); - Plasma::Service* serviceForSource(const QString& source) override; - QStringList GetCapabilities(); - - QString GetServerInformation(QString& vendor, QString& version, QString& specVersion); - int createNotification(const QString &appName, const QString &appIcon, const QString &summary, const QString &body, int timeout, const QStringList &actions, const QVariantMap &hints); @@ -75,38 +74,12 @@ public Q_SLOTS: void removeNotification(uint id, uint closeReason); - bool registerDBusService(); - - void onBroadcastNotification(const QMap &properties); - -Q_SIGNALS: - void NotificationClosed( uint id, uint reason ); - void ActionInvoked( uint id, const QString& actionKey ); private: - /** - * Holds the id that will be assigned to the next notification source - * that will be created - */ - uint m_nextId; + void notificationAdded(const NotificationManager::Notification ¬ification); QHash m_activeNotifications; - QHash m_configurableApplications; - - /** - * A "blacklist" of apps for which always the previous notification from this app - * is replaced by the newer one. This is the case for eg. media players - * as we simply want to update the notification, not get spammed by tens - * of notifications for quickly changing songs in playlist - */ - QSet m_alwaysReplaceAppsList; - /** - * This holds the notifications sent from apps from the list above - * for fast lookup - */ - QHash m_notificationsFromReplaceableApp; - QList m_inhibitions; friend class NotificationAction; diff --git a/dataengines/notifications/notificationsengine.cpp b/dataengines/notifications/notificationsengine.cpp --- a/dataengines/notifications/notificationsengine.cpp +++ b/dataengines/notifications/notificationsengine.cpp @@ -19,255 +19,91 @@ #include "notificationsengine.h" #include "notificationservice.h" -#include "notificationsadaptor.h" -#include "notificationsanitizer.h" +#include "server.h" +#include "notification.h" + +#include #include #include #include #include -#include #include -#include - #include #include #include -#include -#include - -// for ::kill -#include - #include "debug.h" +using namespace NotificationManager; + NotificationsEngine::NotificationsEngine( QObject* parent, const QVariantList& args ) - : Plasma::DataEngine( parent, args ), m_nextId( 1 ), m_alwaysReplaceAppsList({QStringLiteral("Clementine"), QStringLiteral("Spotify"), QStringLiteral("Amarok")}) + : Plasma::DataEngine( parent, args ) { - new NotificationsAdaptor(this); - - if (!registerDBusService()) { - QDBusConnection dbus = QDBusConnection::sessionBus(); - // Retrieve the pid of the current o.f.Notifications service - QDBusReply pidReply = dbus.interface()->servicePid(QStringLiteral("org.freedesktop.Notifications")); - uint pid = pidReply.value(); - // Check if it's not the same app as our own - if (pid != qApp->applicationPid()) { - QDBusReply plasmaPidReply = dbus.interface()->servicePid(QStringLiteral("org.kde.plasmashell")); - // It's not the same but check if it isn't plasma, - // we don't want to kill Plasma - if (pid != plasmaPidReply.value()) { - qCDebug(NOTIFICATIONS) << "Terminating current Notification service with pid" << pid; - // Now finally terminate the service and register our own - ::kill(pid, SIGTERM); - // Wait 3 seconds and then try registering it again - QTimer::singleShot(3000, this, &NotificationsEngine::registerDBusService); - } - } - } - KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Notifications")); - const bool broadcastsEnabled = config.readEntry("ListenForBroadcasts", false); + connect(&Server::self(), &Server::notificationAdded, this, [this](const Notification ¬ification) { + notificationAdded(notification); + }); - if (broadcastsEnabled) { - qCDebug(NOTIFICATIONS) << "Notifications engine is configured to listen for broadcasts"; - QDBusConnection::systemBus().connect({}, {}, QStringLiteral("org.kde.BroadcastNotifications"), - QStringLiteral("Notify"), this, SLOT(onBroadcastNotification(QMap))); - } + connect(&Server::self(), &Server::notificationReplaced, this, [this](uint replacedId, const Notification ¬ification) { + // Notification will already have the correct identical ID + Q_UNUSED(replacedId); + notificationAdded(notification); + }); - // Read additional single-notification-popup-only from a config file - KConfig singlePopupConfig(QStringLiteral("plasma_single_popup_notificationrc")); - KConfigGroup singlePopupConfigGroup(&singlePopupConfig, "General"); - m_alwaysReplaceAppsList += QSet::fromList(singlePopupConfigGroup.readEntry("applications", QStringList())); + connect(&Server::self(), &Server::notificationRemoved, this, [this](uint id, Server::CloseReason reason) { + Q_UNUSED(reason); + const QString source = QStringLiteral("notification %1").arg(id); + // if we don't have that notification in our local list, + // it has already been closed so don't notify a second time + if (m_activeNotifications.remove(source) > 0) { + removeSource(source); + } + }); } NotificationsEngine::~NotificationsEngine() { - QDBusConnection dbus = QDBusConnection::sessionBus(); - dbus.unregisterService( QStringLiteral("org.freedesktop.Notifications") ); -} - -void NotificationsEngine::init() -{ -} - -bool NotificationsEngine::registerDBusService() -{ - QDBusConnection dbus = QDBusConnection::sessionBus(); - dbus.registerObject(QStringLiteral("/org/freedesktop/Notifications"), this); - bool so = dbus.registerService(QStringLiteral("org.freedesktop.Notifications")); - if (so) { - return true; - } - - qCInfo(NOTIFICATIONS) << "Failed to register Notifications service"; - return false; -} - -inline void copyLineRGB32(QRgb* dst, const char* src, int width) -{ - const char* end = src + width * 3; - for (; src != end; ++dst, src+=3) { - *dst = qRgb(src[0], src[1], src[2]); - } -} - -inline void copyLineARGB32(QRgb* dst, const char* src, int width) -{ - const char* end = src + width * 4; - for (; src != end; ++dst, src+=4) { - *dst = qRgba(src[0], src[1], src[2], src[3]); - } -} - -static QImage decodeNotificationSpecImageHint(const QDBusArgument& arg) -{ - int width, height, rowStride, hasAlpha, bitsPerSample, channels; - QByteArray pixels; - char* ptr; - char* end; - - arg.beginStructure(); - arg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample >> channels >> pixels; - arg.endStructure(); - - #define SANITY_CHECK(condition) \ - if (!(condition)) { \ - qWarning() << "Sanity check failed on" << #condition; \ - return QImage(); \ - } - - SANITY_CHECK(width > 0); - SANITY_CHECK(width < 2048); - SANITY_CHECK(height > 0); - SANITY_CHECK(height < 2048); - SANITY_CHECK(rowStride > 0); - - #undef SANITY_CHECK - - QImage::Format format = QImage::Format_Invalid; - void (*fcn)(QRgb*, const char*, int) = nullptr; - if (bitsPerSample == 8) { - if (channels == 4) { - format = QImage::Format_ARGB32; - fcn = copyLineARGB32; - } else if (channels == 3) { - format = QImage::Format_RGB32; - fcn = copyLineRGB32; - } - } - if (format == QImage::Format_Invalid) { - qWarning() << "Unsupported image format (hasAlpha:" << hasAlpha << "bitsPerSample:" << bitsPerSample << "channels:" << channels << ")"; - return QImage(); - } - - QImage image(width, height, format); - ptr = pixels.data(); - end = ptr + pixels.length(); - for (int y=0; y end) { - qWarning() << "Image data is incomplete. y:" << y << "height:" << height; - break; - } - fcn((QRgb*)image.scanLine(y), ptr, width); - } - return image; } -static QString findImageForSpecImagePath(const QString &_path) +void NotificationsEngine::init() { - QString path = _path; - if (path.startsWith(QLatin1String("file:"))) { - QUrl url(path); - path = url.toLocalFile(); - } - return KIconLoader::global()->iconPath(path, -KIconLoader::SizeHuge, - true /* canReturnNull */); } -uint NotificationsEngine::Notify(const QString &app_name, uint replaces_id, - const QString &app_icon, const QString &summary, const QString &body, - const QStringList &actions, const QVariantMap &hints, int timeout) +void NotificationsEngine::notificationAdded(const Notification ¬ification) { - foreach(NotificationInhibiton *ni, m_inhibitions) { - if (hints[ni->hint] == ni->value) { - qCDebug(NOTIFICATIONS) << "notification inhibited. Skipping"; - return -1; - } - } + const QString app_name = notification.applicationName(); + const QString appRealName = notification.notifyRcName(); + const QString eventId = notification.eventId(); // FIXME = hints[QStringLiteral("x-kde-eventId")].toString(); + const QStringList urls = QUrl::toStringList(notification.urls()); + const QString desktopEntry = notification.desktopEntry(); + const QString summary = notification.summary(); - uint partOf = 0; - const QString appRealName = hints[QStringLiteral("x-kde-appname")].toString(); - const QString eventId = hints[QStringLiteral("x-kde-eventId")].toString(); - const bool skipGrouping = hints[QStringLiteral("x-kde-skipGrouping")].toBool(); - const QStringList &urls = hints[QStringLiteral("x-kde-urls")].toStringList(); - const QString &desktopEntry = hints[QStringLiteral("desktop-entry")].toString(); - - // group notifications that have the same title coming from the same app - // or if they are on the "blacklist", honor the skipGrouping hint sent - if (!replaces_id && m_activeNotifications.values().contains(app_name + summary) && !skipGrouping && urls.isEmpty() && !m_alwaysReplaceAppsList.contains(app_name)) { - // cut off the "notification " from the source name - partOf = m_activeNotifications.key(app_name + summary).midRef(13).toUInt(); - } + QString bodyFinal = notification.body(); // is already sanitized by NotificationManager + QString summaryFinal = notification.summary(); + int timeout = notification.timeout(); - qCDebug(NOTIFICATIONS) << "Currrent active notifications:" << m_activeNotifications; - qCDebug(NOTIFICATIONS) << "Guessing partOf as:" << partOf; - qCDebug(NOTIFICATIONS) << " New Notification: " << summary << body << timeout << "& Part of:" << partOf; - QString bodyFinal = NotificationSanitizer::parse(body); - QString summaryFinal = summary; - - if (partOf > 0) { - const QString source = QStringLiteral("notification %1").arg(partOf); - Plasma::DataContainer *container = containerForSource(source); - if (container) { - // append the body text - const QString previousBody = container->data()[QStringLiteral("body")].toString(); - if (previousBody != bodyFinal) { - // FIXME: This will just append the entire old XML document to another one, leading to: - // old
new - // It works but is not very clean. - bodyFinal = previousBody + QStringLiteral("
") + bodyFinal; - } - - replaces_id = partOf; - - // remove the old notification and replace it with the new one - // TODO: maybe just update the current notification? - CloseNotification(partOf); - } - } else if (bodyFinal.isEmpty()) { + if (bodyFinal.isEmpty()) { //some ridiculous apps will send just a title (#372112), in that case, treat it as though there's only a body bodyFinal = summary; summaryFinal = app_name; } - uint id = replaces_id ? replaces_id : m_nextId++; - - // If the current app is in the "blacklist"... - if (m_alwaysReplaceAppsList.contains(app_name)) { - // ...check if we already have a notification from that particular - // app and if yes, use its id to replace it - if (m_notificationsFromReplaceableApp.contains(app_name)) { - id = m_notificationsFromReplaceableApp.value(app_name); - } else { - m_notificationsFromReplaceableApp.insert(app_name, id); - } - } + uint id = notification.id();// replaces_id ? replaces_id : m_nextId++; QString appname_str = app_name; if (appname_str.isEmpty()) { appname_str = i18n("Unknown Application"); } - bool isPersistent = timeout == 0; + bool isPersistent = (timeout == 0); const int AVERAGE_WORD_LENGTH = 6; const int WORD_PER_MINUTE = 250; - int count = summary.length() + body.length() - strlen(""); + int count = notification.summary().length() + notification.body().length() - strlen(""); // -1 is "server default", 0 is persistent with "server default" display time, // anything more should honor the setting @@ -285,10 +121,25 @@ Plasma::DataEngine::Data notificationData; notificationData.insert(QStringLiteral("id"), QString::number(id)); notificationData.insert(QStringLiteral("eventId"), eventId); - notificationData.insert(QStringLiteral("appName"), appname_str); - notificationData.insert(QStringLiteral("appIcon"), app_icon); + notificationData.insert(QStringLiteral("appName"), notification.applicationName()); + // TODO should be proper passed in icon? + notificationData.insert(QStringLiteral("appIcon"), notification.applicationIconName()); notificationData.insert(QStringLiteral("summary"), summaryFinal); notificationData.insert(QStringLiteral("body"), bodyFinal); + + QStringList actions; + for (int i = 0; i < notification.actionNames().count(); ++i) { + actions << notification.actionNames().at(i) << notification.actionLabels().at(i); + } + // NotificationManager hides the configure and default stuff from us but we need to re-add them + // to the actions list for compatibility + if (!notification.configureActionLabel().isEmpty()) { + actions << QStringLiteral("settings") << notification.configureActionLabel(); + } + if (notification.hasDefaultAction()) { + actions << QStringLiteral("default") << QString(); + } + notificationData.insert(QStringLiteral("actions"), actions); notificationData.insert(QStringLiteral("isPersistent"), isPersistent); notificationData.insert(QStringLiteral("expireTimeout"), timeout); @@ -301,70 +152,36 @@ notificationData.insert(QStringLiteral("appServiceIcon"), service->icon()); } - bool configurable = false; - if (!appRealName.isEmpty()) { + notificationData.insert(QStringLiteral("appRealName"), appRealName); + // NotificationManager configurable is anything that has a notifyrc or desktop entry + // but the old stuff assumes only stuff with notifyrc to be configurable + notificationData.insert(QStringLiteral("configurable"), !notification.notifyRcName().isEmpty()); - if (m_configurableApplications.contains(appRealName)) { - configurable = m_configurableApplications.value(appRealName); - } else { - // Check whether the application actually has notifications we can configure - KConfig config(appRealName + QStringLiteral(".notifyrc"), KConfig::NoGlobals); - config.addConfigSources(QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, - QStringLiteral("knotifications5/") + appRealName + QStringLiteral(".notifyrc"))); + QImage image = notification.image(); + notificationData.insert(QStringLiteral("image"), image.isNull() ? QVariant() : image); - const QRegularExpression regexp(QStringLiteral("^Event/([^/]*)$")); - configurable = !config.groupList().filter(regexp).isEmpty(); - m_configurableApplications.insert(appRealName, configurable); - } + int urgency = -1; + switch (notification.urgency()) { + case Notifications::LowUrgency: + urgency = 0; + break; + case Notifications::NormalUrgency: + urgency = 1; + break; + case Notifications::CriticalUrgency: + urgency = 2; + break; } - notificationData.insert(QStringLiteral("appRealName"), appRealName); - notificationData.insert(QStringLiteral("configurable"), configurable); - - QImage image; - // Underscored hints was in use in version 1.1 of the spec but has been - // replaced by dashed hints in version 1.2. We need to support it for - // users of the 1.2 version of the spec. - if (hints.contains(QStringLiteral("image-data"))) { - QDBusArgument arg = hints[QStringLiteral("image-data")].value(); - image = decodeNotificationSpecImageHint(arg); - } else if (hints.contains(QStringLiteral("image_data"))) { - QDBusArgument arg = hints[QStringLiteral("image_data")].value(); - image = decodeNotificationSpecImageHint(arg); - } else if (hints.contains(QStringLiteral("image-path"))) { - QString path = findImageForSpecImagePath(hints[QStringLiteral("image-path")].toString()); - if (!path.isEmpty()) { - image.load(path); - } - } else if (hints.contains(QStringLiteral("image_path"))) { - QString path = findImageForSpecImagePath(hints[QStringLiteral("image_path")].toString()); - if (!path.isEmpty()) { - image.load(path); - } - } else if (hints.contains(QStringLiteral("icon_data"))) { - // This hint was in use in version 1.0 of the spec but has been - // replaced by "image_data" in version 1.1. We need to support it for - // users of the 1.0 version of the spec. - QDBusArgument arg = hints[QStringLiteral("icon_data")].value(); - image = decodeNotificationSpecImageHint(arg); - } - notificationData.insert(QStringLiteral("image"), image.isNull() ? QVariant() : image); - if (hints.contains(QStringLiteral("urgency"))) { - notificationData.insert(QStringLiteral("urgency"), hints[QStringLiteral("urgency")].toInt()); + if (urgency > -1) { + notificationData.insert(QStringLiteral("urgency"), urgency); } notificationData.insert(QStringLiteral("urls"), urls); setData(source, notificationData); - m_activeNotifications.insert(source, app_name + summary); - - return id; -} - -void NotificationsEngine::CloseNotification(uint id) -{ - removeNotification(id, 3); + m_activeNotifications.insert(source, notification.applicationName() + notification.summary()); } void NotificationsEngine::removeNotification(uint id, uint closeReason) @@ -374,41 +191,28 @@ // it has already been closed so don't notify a second time if (m_activeNotifications.remove(source) > 0) { removeSource(source); - emit NotificationClosed(id, closeReason); + Server::self().closeNotification(id, static_cast(closeReason)); } } Plasma::Service* NotificationsEngine::serviceForSource(const QString& source) { return new NotificationService(this, source); } -QStringList NotificationsEngine::GetCapabilities() -{ - return QStringList() - << QStringLiteral("body") - << QStringLiteral("body-hyperlinks") - << QStringLiteral("body-markup") - << QStringLiteral("body-images") - << QStringLiteral("icon-static") - << QStringLiteral("actions") - ; -} - -// FIXME: Signature is ugly -QString NotificationsEngine::GetServerInformation(QString& vendor, QString& version, QString& specVersion) -{ - vendor = QLatin1String("KDE"); - version = QLatin1String("2.0"); // FIXME - specVersion = QLatin1String("1.1"); - return QStringLiteral("Plasma"); -} - int NotificationsEngine::createNotification(const QString &appName, const QString &appIcon, const QString &summary, const QString &body, int timeout, const QStringList &actions, const QVariantMap &hints) { - Notify(appName, 0, appIcon, summary, body, actions, hints, timeout); - return m_nextId; + Notification notification; + notification.setApplicationName(appName); + notification.setApplicationIconName(appIcon); + notification.setSummary(summary); + notification.setBody(body); // sanitizes + notification.setActions(actions); + notification.setTimeout(timeout); + notification.processHints(hints); + Server::self().add(notification); + return 0; } void NotificationsEngine::configureNotification(const QString &appName, const QString &eventId) @@ -435,45 +239,6 @@ return rc; } -void NotificationsEngine::onBroadcastNotification(const QMap &properties) -{ - qCDebug(NOTIFICATIONS) << "Received broadcast notification"; - - const auto currentUserId = KUserId::currentEffectiveUserId().nativeId(); - - // a QVariantList with ints arrives as QDBusArgument here, using a QStringList for simplicity - const QStringList &userIds = properties.value(QStringLiteral("uids")).toStringList(); - if (!userIds.isEmpty()) { - auto it = std::find_if(userIds.constBegin(), userIds.constEnd(), [currentUserId](const QVariant &id) { - bool ok; - auto uid = id.toString().toLongLong(&ok); - return ok && uid == currentUserId; - }); - - if (it == userIds.constEnd()) { - qCDebug(NOTIFICATIONS) << "It is not meant for us, ignoring"; - return; - } - } - - bool ok; - int timeout = properties.value(QStringLiteral("timeout")).toInt(&ok); - if (!ok) { - timeout = -1; // -1 = server default, 0 would be "persistent" - } - - Notify( - properties.value(QStringLiteral("appName")).toString(), - 0, // replaces_id - properties.value(QStringLiteral("appIcon")).toString(), - properties.value(QStringLiteral("summary")).toString(), - properties.value(QStringLiteral("body")).toString(), - {}, // no actions - properties.value(QStringLiteral("hints")).toMap(), - timeout - ); -} - K_EXPORT_PLASMA_DATAENGINE_WITH_JSON(notifications, NotificationsEngine, "plasma-dataengine-notifications.json") #include "notificationsengine.moc" diff --git a/dataengines/notifications/org.freedesktop.Notifications.xml b/dataengines/notifications/org.freedesktop.Notifications.xml deleted file mode 100644 --- a/dataengines/notifications/org.freedesktop.Notifications.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -