Plasma-workspace

serverinfo.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 "serverinfo.h"
8
9#include "server_p.h" // for notificationServiceName
10
11#include "debug.h"
12
13#include <QDBusConnection>
14#include <QDBusConnectionInterface>
15#include <QDBusPendingCallWatcher>
16#include <QDBusServiceWatcher>
17#include <QDebug>
18
19using namespace NotificationManager;
20
21class Q_DECL_HIDDEN ServerInfo::Private
22{
23public:
24 Private(ServerInfo *q);
25 ~Private();
26
27 void setStatus(ServerInfo::Status status);
28 void setServerInformation(const QString &vendor, const QString &name, const QString &version, const QString &specVersion);
29
30 void updateServerInformation();
31
32 ServerInfo *q;
33
34 ServerInfo::Status status = ServerInfo::Status::Unknown;
35
36 QString vendor;
39 QString specVersion;
40};
41
42ServerInfo::Private::Private(ServerInfo *q)
43 : q(q)
44{
45}
46
47ServerInfo::Private::~Private() = default;
48
49void ServerInfo::Private::setStatus(ServerInfo::Status status)
50{
51 if (this->status != status) {
52 this->status = status;
53 Q_EMIT q->statusChanged(status);
54 }
55}
56
57void ServerInfo::Private::setServerInformation(const QString &vendor, const QString &name, const QString &version, const QString &specVersion)
58{
59 if (this->vendor != vendor) {
60 this->vendor = vendor;
61 Q_EMIT q->vendorChanged(vendor);
62 }
63 if (this->name != name) {
64 this->name = name;
65 Q_EMIT q->nameChanged(name);
66 }
67 if (this->version != version) {
68 this->version = version;
69 Q_EMIT q->versionChanged(version);
70 }
71 if (this->specVersion != specVersion) {
72 this->specVersion = specVersion;
73 Q_EMIT q->specVersionChanged(specVersion);
74 }
75}
76
77void ServerInfo::Private::updateServerInformation()
78{
79 // Check whether the service is running to avoid DBus-activating plasma_waitforname and getting stuck there.
80 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(ServerPrivate::notificationServiceName())) {
81 setStatus(ServerInfo::Status::NotRunning);
82 setServerInformation({}, {}, {}, {});
83 return;
84 }
85
86 QDBusMessage msg = QDBusMessage::createMethodCall(ServerPrivate::notificationServiceName(),
87 QStringLiteral("/org/freedesktop/Notifications"),
88 QStringLiteral("org.freedesktop.Notifications"),
89 QStringLiteral("GetServerInformation"));
90 auto call = QDBusConnection::sessionBus().asyncCall(msg);
91
92 auto *watcher = new QDBusPendingCallWatcher(call, q);
95 watcher->deleteLater();
96
97 if (reply.isError()) {
98 qCWarning(NOTIFICATIONMANAGER) << "Failed to determine notification server information" << reply.error().message();
99 // Should this still be "Running" as technically it is?
100 // But if it is not even responding to this properly, who knows what it'll to with an actual notification
101 setStatus(Status::Unknown);
102 setServerInformation({}, {}, {}, {});
103 return;
104 }
105
106 const QString name = reply.argumentAt(0).toString();
107 const QString vendor = reply.argumentAt(1).toString();
108 const QString version = reply.argumentAt(2).toString();
109 const QString specVersion = reply.argumentAt(3).toString();
110
111 setServerInformation(vendor, name, version, specVersion);
112 setStatus(ServerInfo::Status::Running);
113 });
114}
115
116ServerInfo::ServerInfo(QObject *parent)
117 : QObject(parent)
118 , d(new Private(this))
119{
120 auto *watcher =
121 new QDBusServiceWatcher(ServerPrivate::notificationServiceName(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
122 connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, [this]() {
123 d->updateServerInformation();
124 });
125
126 d->updateServerInformation();
127}
128
129ServerInfo::~ServerInfo() = default;
130
131ServerInfo::Status ServerInfo::status() const
132{
133 return d->status;
134}
135
136QString ServerInfo::vendor() const
137{
138 return d->vendor;
139}
140
141QString ServerInfo::name() const
142{
143 return d->name;
144}
145
146QString ServerInfo::version() const
147{
148 return d->version;
149}
150
151QString ServerInfo::specVersion() const
152{
153 return d->specVersion;
154}
Information about the notification server.
Definition serverinfo.h:27
Q_SCRIPTABLE CaptureState status()
QString name(StandardAction id)
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
NETWORKMANAGERQT_EXPORT QString version()
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
QDBusConnection sessionBus()
QString message() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void finished(QDBusPendingCallWatcher *self)
QVariant argumentAt(int index) const const
QDBusError error() const const
bool isError() const const
void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:19:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.