1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
/*
SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "server_p.h"
#include "debug.h"
#include "notificationmanageradaptor.h"
#include "notificationsadaptor.h"
#include "notification_p.h"
#include "server.h"
#include "serverinfo.h"
#include "utils_p.h"
#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <KConfigGroup>
#include <KService>
#include <KSharedConfig>
#include <KUser>
using namespace NotificationManager;
ServerPrivate::ServerPrivate(QObject *parent)
: QObject(parent)
, m_inhibitionWatcher(new QDBusServiceWatcher(this))
, m_notificationWatchers(new QDBusServiceWatcher(this))
{
m_inhibitionWatcher->setConnection(QDBusConnection::sessionBus());
m_inhibitionWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_inhibitionWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &ServerPrivate::onInhibitionServiceUnregistered);
m_notificationWatchers->setConnection(QDBusConnection::sessionBus());
m_notificationWatchers->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_notificationWatchers, &QDBusServiceWatcher::serviceUnregistered, [=](const QString &service) {
m_notificationWatchers->removeWatchedService(service);
});
}
ServerPrivate::~ServerPrivate() = default;
QString ServerPrivate::notificationServiceName()
{
return QStringLiteral("org.freedesktop.Notifications");
}
QString ServerPrivate::notificationServicePath()
{
return QStringLiteral("/org/freedesktop/Notifications");
}
QString ServerPrivate::notificationServiceInterface()
{
return notificationServiceName();
}
ServerInfo *ServerPrivate::currentOwner() const
{
if (!m_currentOwner) {
m_currentOwner.reset(new ServerInfo());
}
return m_currentOwner.get();
}
bool ServerPrivate::init()
{
if (m_valid) {
return true;
}
new NotificationsAdaptor(this);
new NotificationManagerAdaptor(this);
if (!m_dbusObjectValid) { // if already registered, don't fail here
m_dbusObjectValid = QDBusConnection::sessionBus().registerObject(notificationServicePath(), this);
}
if (!m_dbusObjectValid) {
qCWarning(NOTIFICATIONMANAGER) << "Failed to register Notification DBus object";
return false;
}
// Only the "dbus master" (effectively plasmashell) should be the true owner of notifications
const bool master = Utils::isDBusMaster();
QDBusConnectionInterface *dbusIface = QDBusConnection::sessionBus().interface();
if (!master) {
// NOTE this connects to whether the application lost ownership of given service
// This is not a wildcard listener for all unregistered services on the bus!
connect(dbusIface, &QDBusConnectionInterface::serviceUnregistered, this, &ServerPrivate::onServiceOwnershipLost, Qt::UniqueConnection);
}
auto registration = dbusIface->registerService(notificationServiceName(),
master ? QDBusConnectionInterface::ReplaceExistingService : QDBusConnectionInterface::DontQueueService,
master ? QDBusConnectionInterface::DontAllowReplacement : QDBusConnectionInterface::AllowReplacement);
if (registration.value() != QDBusConnectionInterface::ServiceRegistered) {
qCWarning(NOTIFICATIONMANAGER) << "Failed to register Notification service on DBus";
return false;
}
connect(this, &ServerPrivate::inhibitedChanged, this, &ServerPrivate::onInhibitedChanged, Qt::UniqueConnection);
qCDebug(NOTIFICATIONMANAGER) << "Registered Notification service on DBus";
KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("Notifications"));
const bool broadcastsEnabled = config.readEntry("ListenForBroadcasts", false);
if (broadcastsEnabled) {
qCDebug(NOTIFICATIONMANAGER) << "Notification server is configured to listen for broadcasts";
// NOTE Keep disconnect() call in onServiceOwnershipLost in sync if you change this!
QDBusConnection::systemBus().connect({},
{},
QStringLiteral("org.kde.BroadcastNotifications"),
QStringLiteral("Notify"),
this,
SLOT(onBroadcastNotification(QMap<QString, QVariant>)));
}
m_valid = true;
Q_EMIT validChanged();
return true;
}
uint ServerPrivate::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)
{
const bool wasReplaced = replaces_id > 0;
uint notificationId = 0;
if (wasReplaced) {
notificationId = replaces_id;
} else {
// Avoid wrapping around to 0 in case of overflow
if (!m_highestNotificationId) {
++m_highestNotificationId;
}
notificationId = m_highestNotificationId;
++m_highestNotificationId;
}
Notification notification(notificationId);
notification.setDBusService(message().service());
notification.setSummary(summary);
notification.setBody(body);
notification.setApplicationName(app_name);
notification.setActions(actions);
notification.setTimeout(timeout);
// might override some of the things we set above (like application name)
notification.d->processHints(hints);
// If we didn't get a pixmap, load the app_icon instead
if (notification.d->image.isNull()) {
notification.setIcon(app_icon);
}
uint pid = 0;
if (notification.desktopEntry().isEmpty() || notification.applicationName().isEmpty()) {
if (notification.desktopEntry().isEmpty() && notification.applicationName().isEmpty()) {
qCInfo(NOTIFICATIONMANAGER) << "Notification from service" << message().service()
<< "didn't contain any identification information, this is an application bug!";
}
QDBusReply<uint> pidReply = connection().interface()->servicePid(message().service());
if (pidReply.isValid()) {
pid = pidReply.value();
}
}
// No desktop entry? Try to read the BAMF_DESKTOP_FILE_HINT in the environment of snaps
if (notification.desktopEntry().isEmpty() && pid > 0) {
const QString desktopEntry = Utils::desktopEntryFromPid(pid);
if (!desktopEntry.isEmpty()) {
qCDebug(NOTIFICATIONMANAGER) << "Resolved notification to be from desktop entry" << desktopEntry;
notification.setDesktopEntry(desktopEntry);
// No application name? Set it to the service name, which is nicer than the process name fallback below
// Also if the title looks like it's just the desktop entry, use the nicer service name
if (notification.applicationName().isEmpty() || notification.applicationName() == desktopEntry) {
KService::Ptr service = KService::serviceByDesktopName(desktopEntry);
if (service) {
notification.setApplicationName(service->name());
}
}
}
}
// No application name? Try to figure out the process name using the sender's PID
if (notification.applicationName().isEmpty() && pid > 0) {
const QString processName = Utils::processNameFromPid(pid);
if (!processName.isEmpty()) {
qCDebug(NOTIFICATIONMANAGER) << "Resolved notification to be from process name" << processName;
notification.setApplicationName(processName);
}
}
// If multiple identical notifications are sent in quick succession, refuse the request
if (m_lastNotification.applicationName() == notification.applicationName() && m_lastNotification.summary() == notification.summary()
&& m_lastNotification.body() == notification.body() && m_lastNotification.desktopEntry() == notification.desktopEntry()
&& m_lastNotification.eventId() == notification.eventId() && m_lastNotification.actionNames() == notification.actionNames()
&& m_lastNotification.urls() == notification.urls() && m_lastNotification.created().msecsTo(notification.created()) < 1000) {
qCDebug(NOTIFICATIONMANAGER) << "Discarding excess notification creation request";
sendErrorReply(QStringLiteral("org.freedesktop.Notifications.Error.ExcessNotificationGeneration"),
QStringLiteral("Created too many similar notifications in quick succession"));
return 0;
}
m_lastNotification = notification;
if (wasReplaced) {
notification.resetUpdated();
Q_EMIT static_cast<Server *>(parent())->notificationReplaced(replaces_id, notification);
} else {
Q_EMIT static_cast<Server *>(parent())->notificationAdded(notification);
}
// currently we dispatch all notification, this is ugly
// TODO: come up with proper authentication/user selection
for (const QString &service : m_notificationWatchers->watchedServices()) {
QDBusMessage msg = QDBusMessage::createMethodCall(service,
QStringLiteral("/NotificationWatcher"),
QStringLiteral("org.kde.NotificationWatcher"),
QStringLiteral("Notify"));
msg.setArguments({notificationId,
notification.applicationName(),
replaces_id,
notification.applicationIconName(),
notification.summary(),
// we pass raw body data since this data goes through another sanitization
// in WatchedNotificationsModel when notification object is created.
notification.rawBody(),
actions,
hints,
notification.timeout()});
QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
}
return notificationId;
}
void ServerPrivate::CloseNotification(uint id)
{
for (const QString &service : m_notificationWatchers->watchedServices()) {
QDBusMessage msg = QDBusMessage::createMethodCall(service,
QStringLiteral("/NotificationWatcher"),
QStringLiteral("org.kde.NotificationWatcher"),
QStringLiteral("CloseNotification"));
msg.setArguments({id});
QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
}
// spec says "If the notification no longer exists, an empty D-BUS Error message is sent back."
static_cast<Server *>(parent())->closeNotification(id, Server::CloseReason::Revoked);
}
QStringList ServerPrivate::GetCapabilities() const
{
// should this be configurable somehow so the UI can tell what it implements?
return QStringList{QStringLiteral("body"),
QStringLiteral("body-hyperlinks"),
QStringLiteral("body-markup"),
QStringLiteral("body-images"),
QStringLiteral("icon-static"),
QStringLiteral("actions"),
QStringLiteral("persistence"),
QStringLiteral("inline-reply"),
QStringLiteral("x-kde-urls"),
QStringLiteral("x-kde-origin-name"),
QStringLiteral("x-kde-display-appname"),
QStringLiteral("inhibitions")};
}
QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const
{
vendor = QStringLiteral("KDE");
version = QLatin1String(PROJECT_VERSION);
specVersion = QStringLiteral("1.2");
return QStringLiteral("Plasma");
}
void ServerPrivate::onBroadcastNotification(const QMap<QString, QVariant> &properties)
{
qCDebug(NOTIFICATIONMANAGER) << "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(NOTIFICATIONMANAGER) << "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);
}
uint ServerPrivate::add(const Notification ¬ification)
{
// TODO check if notification with ID already exists and signal update instead
if (notification.id() == 0) {
++m_highestNotificationId;
notification.d->id = m_highestNotificationId;
Q_EMIT static_cast<Server *>(parent())->notificationAdded(notification);
} else {
Q_EMIT static_cast<Server *>(parent())->notificationReplaced(notification.id(), notification);
}
return notification.id();
}
void ServerPrivate::sendReplyText(const QString &dbusService, uint notificationId, const QString &text, Notifications::InvokeBehavior behavior)
{
if (dbusService.isEmpty()) {
qCWarning(NOTIFICATIONMANAGER) << "Sending notification reply text for notification" << notificationId << "untargeted";
}
QDBusMessage msg =
QDBusMessage::createTargetedSignal(dbusService, notificationServicePath(), notificationServiceName(), QStringLiteral("NotificationReplied"));
msg.setArguments({notificationId, text});
QDBusConnection::sessionBus().send(msg);
if (behavior & Notifications::Close) {
Q_EMIT CloseNotification(notificationId);
}
}
uint ServerPrivate::Inhibit(const QString &desktop_entry, const QString &reason, const QVariantMap &hints)
{
const QString dbusService = message().service();
QString applicationName = desktop_entry;
qCDebug(NOTIFICATIONMANAGER) << "Request inhibit from service" << dbusService << "which is" << desktop_entry << "with reason" << reason;
// xdg-desktop-portal forwards appId only for sandboxed apps it can trust
// Resolve it to process name here to at least have something, even if that means showing "xdg-desktop-portal-kde is currently..."
if (desktop_entry.isEmpty()) {
QDBusReply<uint> pidReply = connection().interface()->servicePid(message().service());
if (pidReply.isValid()) {
const auto pid = pidReply.value();
const QString processName = Utils::processNameFromPid(pid);
if (!processName.isEmpty()) {
qCDebug(NOTIFICATIONMANAGER) << "Resolved inhibition to be from process name" << processName;
applicationName = processName;
}
}
} else {
KService::Ptr service = KService::serviceByDesktopName(desktop_entry);
if (service) {
applicationName = service->name();
}
}
if (applicationName.isEmpty()) {
sendErrorReply(QDBusError::InvalidArgs, QStringLiteral("No meaningful desktop_entry provided"));
return 0;
}
m_inhibitionWatcher->addWatchedService(dbusService);
++m_highestInhibitionCookie;
const bool oldExternalInhibited = externalInhibited();
m_externalInhibitions.insert(m_highestInhibitionCookie, {desktop_entry, applicationName, reason, hints});
m_inhibitionServices.insert(m_highestInhibitionCookie, dbusService);
if (externalInhibited() != oldExternalInhibited) {
Q_EMIT externalInhibitedChanged();
}
Q_EMIT externalInhibitionsChanged();
return m_highestInhibitionCookie;
}
void ServerPrivate::onServiceOwnershipLost(const QString &serviceName)
{
if (serviceName != notificationServiceName()) {
return;
}
qCDebug(NOTIFICATIONMANAGER) << "Lost ownership of" << serviceName << "service";
disconnect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceUnregistered, this, &ServerPrivate::onServiceOwnershipLost);
disconnect(this, &ServerPrivate::inhibitedChanged, this, &ServerPrivate::onInhibitedChanged);
QDBusConnection::systemBus().disconnect({},
{},
QStringLiteral("org.kde.BroadcastNotifications"),
QStringLiteral("Notify"),
this,
SLOT(onBroadcastNotification(QMap<QString, QVariant>)));
m_valid = false;
Q_EMIT validChanged();
Q_EMIT serviceOwnershipLost();
}
void ServerPrivate::onInhibitionServiceUnregistered(const QString &serviceName)
{
qCDebug(NOTIFICATIONMANAGER) << "Inhibition service unregistered" << serviceName;
const QList<uint> cookies = m_inhibitionServices.keys(serviceName);
if (cookies.isEmpty()) {
qCInfo(NOTIFICATIONMANAGER) << "Unknown inhibition service unregistered" << serviceName;
return;
}
// We do lookups in there again...
for (uint cookie : cookies) {
UnInhibit(cookie);
}
}
void ServerPrivate::onInhibitedChanged()
{
// Q_EMIT DBus change signal...
QDBusMessage signal =
QDBusMessage::createSignal(notificationServicePath(), QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("PropertiesChanged"));
signal.setArguments({
notificationServiceInterface(),
QVariantMap{
// updated
{QStringLiteral("Inhibited"), inhibited()},
},
QStringList() // invalidated
});
QDBusConnection::sessionBus().send(signal);
}
void ServerPrivate::UnInhibit(uint cookie)
{
qCDebug(NOTIFICATIONMANAGER) << "Request release inhibition for cookie" << cookie;
const QString service = m_inhibitionServices.value(cookie);
if (service.isEmpty()) {
qCInfo(NOTIFICATIONMANAGER) << "Requested to release inhibition with cookie" << cookie << "that doesn't exist";
// TODO if called from dbus raise error
return;
}
m_inhibitionWatcher->removeWatchedService(service);
m_externalInhibitions.remove(cookie);
m_inhibitionServices.remove(cookie);
if (m_externalInhibitions.isEmpty()) {
Q_EMIT externalInhibitedChanged();
}
Q_EMIT externalInhibitionsChanged();
}
QList<Inhibition> ServerPrivate::externalInhibitions() const
{
return m_externalInhibitions.values();
}
bool ServerPrivate::inhibited() const
{
return m_inhibited;
}
void ServerPrivate::setInhibited(bool inhibited)
{
if (m_inhibited != inhibited) {
m_inhibited = inhibited;
Q_EMIT inhibitedChanged();
}
}
bool ServerPrivate::externalInhibited() const
{
return !m_externalInhibitions.isEmpty();
}
void ServerPrivate::clearExternalInhibitions()
{
if (m_externalInhibitions.isEmpty()) {
return;
}
m_inhibitionWatcher->setWatchedServices(QStringList()); // remove all watches
m_inhibitionServices.clear();
m_externalInhibitions.clear();
Q_EMIT externalInhibitedChanged();
Q_EMIT externalInhibitionsChanged();
}
void ServerPrivate::RegisterWatcher()
{
m_notificationWatchers->addWatchedService(message().service());
}
void ServerPrivate::UnRegisterWatcher()
{
m_notificationWatchers->removeWatchedService(message().service());
}
void ServerPrivate::InvokeAction(uint id, const QString &actionKey)
{
Q_EMIT ActionInvoked(id, actionKey);
}
|