summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Uwe Broulik <[email protected]>2016-12-12 14:35:25 +0100
committerKai Uwe Broulik <[email protected]>2016-12-12 14:36:38 +0100
commitdcf8e3dca547ba04e248b86a37d9d4c1754e89ac (patch)
treed22a0ff015a29386b7f2c624ce6a0e0634af8a05
parentee0c3abd260423f2f4a88d47be9d8cea6d8e4e56 (diff)
Listen for broadcast notifications on system bus
This will listen for a signal on the system DBus to show a notification to all users on the current machine. This allows prettier and more versatile notifications than using wall/write. This is disabled by default but can be enabled in e.g. ~/.config/plasmashellrc per user or /etc/xdg/plasmashellrc globally: [Notifications] ListenForBroadcasts=true Differential Revision: https://phabricator.kde.org/D3606
-rw-r--r--dataengines/notifications/notificationsengine.cpp49
-rw-r--r--dataengines/notifications/notificationsengine.h2
2 files changed, 51 insertions, 0 deletions
diff --git a/dataengines/notifications/notificationsengine.cpp b/dataengines/notifications/notificationsengine.cpp
index ca3d792..9a33bf4 100644
--- a/dataengines/notifications/notificationsengine.cpp
+++ b/dataengines/notifications/notificationsengine.cpp
@@ -24,7 +24,9 @@
#include <QDebug>
#include <KConfigGroup>
#include <klocalizedstring.h>
+#include <KSharedConfig>
#include <KNotifyConfigWidget>
+#include <KUser>
#include <QGuiApplication>
#include <QRegularExpression>
@@ -65,6 +67,15 @@ NotificationsEngine::NotificationsEngine( QObject* parent, const QVariantList& a
}
}
+ 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<QString,QVariant>)));
+ }
+
// 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 @@ QSharedPointer<NotificationInhibiton> NotificationsEngine::createInhibition(cons
return rc;
}
+void NotificationsEngine::onBroadcastNotification(const QMap<QString, QVariant> &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")
diff --git a/dataengines/notifications/notificationsengine.h b/dataengines/notifications/notificationsengine.h
index 9eb1972..aa996fb 100644
--- a/dataengines/notifications/notificationsengine.h
+++ b/dataengines/notifications/notificationsengine.h
@@ -77,6 +77,8 @@ public Q_SLOTS:
void removeNotification(uint id, uint closeReason);
bool registerDBusService();
+ void onBroadcastNotification(const QMap<QString, QVariant> &properties);
+
Q_SIGNALS:
void NotificationClosed( uint id, uint reason );
void ActionInvoked( uint id, const QString& actionKey );