Changeset View
Changeset View
Standalone View
Standalone View
libnotificationmanager/serverinfo.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "serverinfo.h" | ||||
22 | | ||||
23 | #include "server_p.h" // for notificationServiceName | ||||
24 | | ||||
25 | #include "debug.h" | ||||
26 | | ||||
27 | #include <QDBusConnection> | ||||
28 | #include <QDBusConnectionInterface> | ||||
29 | #include <QDBusPendingCallWatcher> | ||||
30 | #include <QDBusServiceWatcher> | ||||
31 | #include <QDebug> | ||||
32 | | ||||
33 | using namespace NotificationManager; | ||||
34 | | ||||
35 | class Q_DECL_HIDDEN ServerInfo::Private | ||||
36 | { | ||||
37 | public: | ||||
38 | Private(ServerInfo *q); | ||||
39 | ~Private(); | ||||
40 | | ||||
41 | void setStatus(ServerInfo::Status status); | ||||
42 | void setServerInformation(const QString &vendor, | ||||
43 | const QString &name, | ||||
44 | const QString &version, | ||||
45 | const QString &specVersion); | ||||
46 | | ||||
47 | void updateServerInformation(); | ||||
48 | | ||||
49 | ServerInfo *q; | ||||
50 | | ||||
51 | ServerInfo::Status status = ServerInfo::Status::Unknown; | ||||
52 | | ||||
53 | QString vendor; | ||||
54 | QString name; | ||||
55 | QString version; | ||||
56 | QString specVersion; | ||||
57 | }; | ||||
58 | | ||||
59 | ServerInfo::Private::Private(ServerInfo *q) | ||||
60 | : q(q) | ||||
61 | { | ||||
62 | | ||||
63 | } | ||||
64 | | ||||
65 | ServerInfo::Private::~Private() = default; | ||||
66 | | ||||
67 | void ServerInfo::Private::setStatus(ServerInfo::Status status) | ||||
68 | { | ||||
69 | if (this->status != status) { | ||||
70 | this->status = status; | ||||
71 | emit q->statusChanged(status); | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | void ServerInfo::Private::setServerInformation(const QString &vendor, | ||||
76 | const QString &name, | ||||
77 | const QString &version, | ||||
78 | const QString &specVersion) | ||||
79 | { | ||||
80 | if (this->vendor != vendor) { | ||||
81 | this->vendor = vendor; | ||||
82 | emit q->vendorChanged(vendor); | ||||
83 | } | ||||
84 | if (this->name != name) { | ||||
85 | this->name = name; | ||||
86 | emit q->nameChanged(name); | ||||
87 | } | ||||
88 | if (this->version != version) { | ||||
89 | this->version = version; | ||||
90 | emit q->versionChanged(version); | ||||
91 | } | ||||
92 | if (this->specVersion != specVersion) { | ||||
93 | this->specVersion = specVersion; | ||||
94 | emit q->specVersionChanged(specVersion); | ||||
95 | } | ||||
96 | } | ||||
97 | | ||||
98 | void ServerInfo::Private::updateServerInformation() | ||||
99 | { | ||||
100 | // Check whether the service is running to avoid DBus-activating plasma_waitforname and getting stuck there. | ||||
101 | if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(ServerPrivate::notificationServiceName())) { | ||||
102 | setStatus(ServerInfo::Status::NotRunning); | ||||
103 | setServerInformation({}, {}, {}, {}); | ||||
104 | return; | ||||
105 | } | ||||
106 | | ||||
107 | QDBusMessage msg = QDBusMessage::createMethodCall(ServerPrivate::notificationServiceName(), | ||||
108 | QStringLiteral("/org/freedesktop/Notifications"), | ||||
109 | QStringLiteral("org.freedesktop.Notifications"), | ||||
110 | QStringLiteral("GetServerInformation")); | ||||
111 | auto call = QDBusConnection::sessionBus().asyncCall(msg); | ||||
112 | | ||||
113 | auto *watcher = new QDBusPendingCallWatcher(call, q); | ||||
114 | QObject::connect(watcher, &QDBusPendingCallWatcher::finished, q, [this](QDBusPendingCallWatcher *watcher) { | ||||
115 | QDBusPendingReply<QString, QString, QString, QString> reply = *watcher; | ||||
116 | watcher->deleteLater(); | ||||
117 | | ||||
118 | if (reply.isError()) { | ||||
119 | qCWarning(NOTIFICATIONMANAGER) << "Failed to determine notification server information" << reply.error().message(); | ||||
120 | // Should this still be "Running" as technically it is? | ||||
121 | // But if it is not even responding to this properly, who knows what it'll to with an actual notification | ||||
122 | setStatus(Status::Unknown); | ||||
123 | setServerInformation({}, {}, {}, {}); | ||||
124 | return; | ||||
125 | } | ||||
126 | | ||||
127 | const QString name = reply.argumentAt(0).toString(); | ||||
128 | const QString vendor = reply.argumentAt(1).toString(); | ||||
129 | const QString version = reply.argumentAt(2).toString(); | ||||
130 | const QString specVersion = reply.argumentAt(3).toString(); | ||||
131 | | ||||
132 | setServerInformation(vendor, name, version, specVersion); | ||||
133 | setStatus(ServerInfo::Status::Running); | ||||
134 | }); | ||||
135 | } | ||||
136 | | ||||
137 | ServerInfo::ServerInfo(QObject *parent) | ||||
138 | : QObject(parent) | ||||
139 | , d(new Private(this)) | ||||
140 | { | ||||
141 | auto *watcher = new QDBusServiceWatcher(ServerPrivate::notificationServiceName(), | ||||
142 | QDBusConnection::sessionBus(), | ||||
143 | QDBusServiceWatcher::WatchForOwnerChange, this); | ||||
144 | connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, [this]() { | ||||
145 | d->updateServerInformation(); | ||||
146 | }); | ||||
147 | | ||||
148 | d->updateServerInformation(); | ||||
149 | } | ||||
150 | | ||||
151 | ServerInfo::~ServerInfo() = default; | ||||
152 | | ||||
153 | ServerInfo::Status ServerInfo::status() const | ||||
154 | { | ||||
155 | return d->status; | ||||
156 | } | ||||
157 | | ||||
158 | QString ServerInfo::vendor() const | ||||
159 | { | ||||
160 | return d->vendor; | ||||
161 | } | ||||
162 | | ||||
163 | QString ServerInfo::name() const | ||||
164 | { | ||||
165 | return d->name; | ||||
166 | } | ||||
167 | | ||||
168 | QString ServerInfo::version() const | ||||
169 | { | ||||
170 | return d->version; | ||||
171 | } | ||||
172 | | ||||
173 | QString ServerInfo::specVersion() const | ||||
174 | { | ||||
175 | return d->specVersion; | ||||
176 | } |