diff --git a/dataengines/notifications/notificationsengine.h b/dataengines/notifications/notificationsengine.h --- a/dataengines/notifications/notificationsengine.h +++ b/dataengines/notifications/notificationsengine.h @@ -77,6 +77,8 @@ 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 ); diff --git a/dataengines/notifications/notificationsengine.cpp b/dataengines/notifications/notificationsengine.cpp --- a/dataengines/notifications/notificationsengine.cpp +++ b/dataengines/notifications/notificationsengine.cpp @@ -24,7 +24,9 @@ #include #include #include +#include #include +#include #include #include @@ -65,6 +67,15 @@ } } + KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Notifications")); + const bool broadcastsEnabled = config.readEntry("ListenForBroadcasts", false); + + if (broadcastsEnabled) { + qDebug() << "Notifications engine is configured to listen for broadcasts"; + QDBusConnection::systemBus().connect({}, {}, QStringLiteral("org.kde.BroadcastNotifications"), + QStringLiteral("Notify"), this, SLOT(onBroadcastNotification(QMap))); + } + // Read additional single-notification-popup-only from a config file KConfig singlePopupConfig(QStringLiteral("plasma_single_popup_notificationrc")); KConfigGroup singlePopupConfigGroup(&singlePopupConfig, "General"); @@ -433,6 +444,44 @@ return rc; } +void NotificationsEngine::onBroadcastNotification(const QMap &properties) +{ + qDebug() << "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()) { + qDebug() << "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")