Plasma-workspace

server.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "server.h"
8#include "server_p.h"
9#include "serverinfo.h"
10
11#include "notification.h"
12#include "notification_p.h"
13
14#include "debug.h"
15
16#include <KStartupInfo>
17#include <KWaylandExtras>
18#include <KWindowSystem>
19#include <QDebug>
20
21using namespace NotificationManager;
22
23Server::Server(QObject *parent)
24 : QObject(parent)
25 , d(new ServerPrivate(this))
26{
27 connect(d.get(), &ServerPrivate::validChanged, this, &Server::validChanged);
28 connect(d.get(), &ServerPrivate::inhibitedChanged, this, [this] {
29 Q_EMIT inhibitedChanged(inhibited());
30 });
31 connect(d.get(), &ServerPrivate::externalInhibitedChanged, this, [this] {
32 Q_EMIT inhibitedByApplicationChanged(inhibitedByApplication());
33 });
34 connect(d.get(), &ServerPrivate::externalInhibitionsChanged, this, &Server::inhibitionApplicationsChanged);
35 connect(d.get(), &ServerPrivate::serviceOwnershipLost, this, &Server::serviceOwnershipLost);
36}
37
38Server::~Server() = default;
39
40Server &Server::self()
41{
42 static Server s_self;
43 return s_self;
44}
45
46Server *Server::create(QQmlEngine *, QJSEngine *)
47{
49 return &Server::self();
50}
51
53{
54 return d->init();
55}
56
57bool Server::isValid() const
58{
59 return d->m_valid;
60}
61
63{
64 return d->currentOwner();
65}
66
67void Server::closeNotification(uint notificationId, CloseReason reason)
68{
69 Q_EMIT notificationRemoved(notificationId, reason);
70 Q_EMIT d->NotificationClosed(notificationId, static_cast<uint>(reason)); // tell on DBus
71}
72
73void Server::invokeAction(uint notificationId,
74 const QString &actionName,
75 const QString &xdgActivationAppId,
76 Notifications::InvokeBehavior behavior,
77 QWindow *window)
78{
80 const quint32 launchedSerial = KWaylandExtras::lastInputSerial(window);
81 auto conn = std::make_shared<QMetaObject::Connection>();
82 *conn = connect(KWaylandExtras::self(),
84 this,
85 [this, actionName, notificationId, launchedSerial, conn, behavior](quint32 serial, const QString &token) {
86 if (serial == launchedSerial) {
87 disconnect(*conn);
88 Q_EMIT d->ActivationToken(notificationId, token);
89 Q_EMIT d->ActionInvoked(notificationId, actionName);
90
91 if (behavior & Notifications::Close) {
92 Q_EMIT d->CloseNotification(notificationId);
93 }
94 }
95 });
96 KWaylandExtras::requestXdgActivationToken(window, launchedSerial, xdgActivationAppId);
97 } else {
98 KStartupInfoId startupId;
99 startupId.initId();
100
101 Q_EMIT d->ActivationToken(notificationId, QString::fromUtf8(startupId.id()));
102
103 Q_EMIT d->ActionInvoked(notificationId, actionName);
104 if (behavior & Notifications::Close) {
105 Q_EMIT d->CloseNotification(notificationId);
106 }
107 }
108}
109
110void Server::reply(const QString &dbusService, uint notificationId, const QString &text, Notifications::InvokeBehavior behavior)
111{
112 d->sendReplyText(dbusService, notificationId, text, behavior);
113}
114
115uint Server::add(const Notification &notification)
116{
117 return d->add(notification);
118}
119
121{
122 return d->inhibited();
123}
124
125void Server::setInhibited(bool inhibited)
126{
127 d->setInhibited(inhibited);
128}
129
131{
132 return d->externalInhibited();
133}
134
135QStringList Server::inhibitionApplications() const
136{
137 QStringList applications;
138 const auto inhibitions = d->externalInhibitions();
139 applications.reserve(inhibitions.count());
140 for (const auto &inhibition : inhibitions) {
141 applications.append(!inhibition.applicationName.isEmpty() ? inhibition.applicationName : inhibition.desktopEntry);
142 }
143 return applications;
144}
145
146QStringList Server::inhibitionReasons() const
147{
148 QStringList reasons;
149 const auto inhibitions = d->externalInhibitions();
150 reasons.reserve(inhibitions.count());
151 for (const auto &inhibition : inhibitions) {
152 reasons.append(inhibition.reason);
153 }
154 return reasons;
155}
156
158{
159 d->clearExternalInhibitions();
160}
161
162#include "moc_server.cpp"
void initId(const QByteArray &id="")
const QByteArray & id() const
void xdgActivationTokenArrived(int serial, const QString &token)
static Q_INVOKABLE void requestXdgActivationToken(QWindow *win, uint32_t serial, const QString &app_id)
static Q_INVOKABLE quint32 lastInputSerial(QWindow *window)
static bool isPlatformWayland()
Represents a single notification.
Information about the notification server.
Definition serverinfo.h:27
A notification DBus server.
Definition server.h:30
void setInhibited(bool inhibited)
Whether notifications are currently effectively inhibited.
Definition server.cpp:125
CloseReason
The reason a notification was closed.
Definition server.h:69
void clearInhibitions()
Remove all inhibitions.
Definition server.cpp:157
bool isValid() const
Whether the notification service could be registered.
Definition server.cpp:57
NotificationManager::ServerInfo * currentOwner
Information about the current owner of the Notification service.
Definition server.h:51
void invokeAction(uint id, const QString &actionName, const QString &xdgActivationToken, Notifications::InvokeBehavior behavior, QWindow *window)
Sends an action invocation request.
Definition server.cpp:73
bool inhibited
Whether notifications are currently inhibited.
Definition server.h:61
void serviceOwnershipLost()
Emitted when the ownership of the Notification DBus Service is lost.
bool inhibitedByApplication() const
Whether an application requested to inhibit notifications.
Definition server.cpp:130
void closeNotification(uint id, CloseReason reason)
Sends a notification closed event.
Definition server.cpp:67
void reply(const QString &dbusService, uint id, const QString &text, Notifications::InvokeBehavior behavior)
Sends a notification reply text.
Definition server.cpp:110
uint add(const Notification &notification)
Adds a notification.
Definition server.cpp:115
void notificationRemoved(uint id, CloseReason reason)
Emitted when a notification got removed (closed)
void validChanged()
Emitted when the notification service validity changes, because it successfully registered the servic...
void inhibitionApplicationsChanged()
Emitted when the list of applications holding a notification inhibition changes.
bool init()
Registers the Notification Service on DBus.
Definition server.cpp:52
void setObjectOwnership(QObject *object, ObjectOwnership ownership)
void append(QList< T > &&value)
void reserve(qsizetype size)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QString fromUtf8(QByteArrayView str)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 12:00:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.