KJobWidgets

kuiserverjobtracker.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2008 Rafael Fernández López <ereslibre@kde.org>
4 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kuiserverjobtracker.h"
10#include "kuiserverjobtracker_p.h"
11
12#include "debug.h"
13#include "jobviewiface.h"
14
15#include <KJob>
16
17#include <QtGlobal>
18#include <QApplication>
19#include <QDBusConnection>
20#include <QIcon>
21
22Q_GLOBAL_STATIC(KSharedUiServerProxy, serverProxy)
23
24class Q_DECL_HIDDEN KUiServerJobTracker::Private
25{
26public:
27 Private(KUiServerJobTracker *parent)
28 : q(parent)
29 {
30 }
31
32 KUiServerJobTracker *const q;
33
34 void _k_killJob();
35
36 static void updateDestUrl(KJob *job, org::kde::JobViewV2 *jobView);
37
39
40 QMetaObject::Connection serverRegisteredConnection;
41};
42
43void KUiServerJobTracker::Private::_k_killJob()
44{
45 org::kde::JobViewV2 *jobView = qobject_cast<org::kde::JobViewV2 *>(q->sender());
46
47 if (jobView) {
48 KJob *job = progressJobView.key(jobView);
49
50 if (job) {
51 job->kill(KJob::EmitResult);
52 }
53 }
54}
55
56void KUiServerJobTracker::Private::updateDestUrl(KJob *job, org::kde::JobViewV2 *jobView)
57{
58 const QVariant destUrl = job->property("destUrl");
59 if (destUrl.isValid()) {
60 jobView->setDestUrl(QDBusVariant(destUrl));
61 }
62}
63
69
71{
72 if (!d->progressJobView.isEmpty()) {
73 qWarning() << "A KUiServerJobTracker instance contains" << d->progressJobView.size() << "stalled jobs";
74 }
75
76 qDeleteAll(d->progressJobView);
77}
78
80{
81 // Already registered job?
82 if (d->progressJobView.contains(job)) {
83 return;
84 }
85
86 // Watch the server registering/unregistering and re-register the jobs as needed
87 if (!d->serverRegisteredConnection) {
88 d->serverRegisteredConnection = connect(serverProxy(), &KSharedUiServerProxy::serverRegistered, this, [this]() {
89 // Remember the list of jobs to re-register and then delete the old ones
90 const QList<KJob *> staleJobs = d->progressJobView.keys();
91
92 qDeleteAll(d->progressJobView);
93 d->progressJobView.clear();
94
95 for (KJob *job : staleJobs) {
96 registerJob(job);
97 }
98 });
99 }
100
102 // This will only work if main() used QIcon::fromTheme.
103 QString programIconName = QApplication::windowIcon().name();
104
105 if (programIconName.isEmpty()) {
106 programIconName = appName;
107 }
108
109 QPointer<KJob> jobWatch = job;
110 QDBusReply<QDBusObjectPath> reply = serverProxy()->uiserver()->requestView(appName, programIconName, job->capabilities());
111
112 // If we got a valid reply, register the interface for later usage.
113 if (reply.isValid()) {
114 org::kde::JobViewV2 *jobView = new org::kde::JobViewV2(QStringLiteral("org.kde.JobViewServer"), reply.value().path(), QDBusConnection::sessionBus());
115 if (!jobWatch) {
116 // qCDebug(KJOBWIDGETS) << "deleted out from under us when asking the server proxy for the view";
117 jobView->terminate(QString());
118 delete jobView;
119 return;
120 }
121
122 QObject::connect(jobView, SIGNAL(cancelRequested()), this, SLOT(_k_killJob()));
123 QObject::connect(jobView, &org::kde::JobViewV2::suspendRequested, job, &KJob::suspend);
124 QObject::connect(jobView, &org::kde::JobViewV2::resumeRequested, job, &KJob::resume);
125
126 d->updateDestUrl(job, jobView);
127
128 if (!jobWatch) {
129 // qCDebug(KJOBWIDGETS) << "deleted out from under us when creating the dbus interface";
130 jobView->terminate(QString());
131 delete jobView;
132 return;
133 }
134
135 d->progressJobView.insert(job, jobView);
136 } else if (!jobWatch) {
137 qWarning() << "Uh-oh...KUiServerJobTracker was trying to forward a job, but it was deleted from under us."
138 << "kuiserver *may* have a stranded job. we can't do anything about it because the returned objectPath is invalid.";
139 return;
140 }
141
143}
144
146{
148
149 if (!d->progressJobView.contains(job)) {
150 return;
151 }
152
153 org::kde::JobViewV2 *jobView = d->progressJobView.take(job);
154
155 d->updateDestUrl(job, jobView);
156
157 jobView->setError(job->error());
158
159 if (job->error()) {
160 jobView->terminate(job->errorText());
161 } else {
162 jobView->terminate(QString());
163 }
164
165 delete jobView;
166}
167
169{
170 if (!d->progressJobView.contains(job)) {
171 return;
172 }
173
174 org::kde::JobViewV2 *jobView = d->progressJobView.take(job);
175
176 d->updateDestUrl(job, jobView);
177
178 jobView->setError(job->error());
179
180 if (job->error()) {
181 jobView->terminate(job->errorText());
182 } else {
183 jobView->terminate(QString());
184 }
185}
186
187void KUiServerJobTracker::suspended(KJob *job)
188{
189 if (!d->progressJobView.contains(job)) {
190 return;
191 }
192
193 org::kde::JobViewV2 *jobView = d->progressJobView[job];
194
195 jobView->setSuspended(true);
196}
197
198void KUiServerJobTracker::resumed(KJob *job)
199{
200 if (!d->progressJobView.contains(job)) {
201 return;
202 }
203
204 org::kde::JobViewV2 *jobView = d->progressJobView[job];
205
206 jobView->setSuspended(false);
207}
208
209void KUiServerJobTracker::description(KJob *job, const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2)
210{
211 if (!d->progressJobView.contains(job)) {
212 return;
213 }
214
215 org::kde::JobViewV2 *jobView = d->progressJobView[job];
216
217 jobView->setInfoMessage(title);
218
219 if (field1.first.isNull() || field1.second.isNull()) {
220 jobView->clearDescriptionField(0);
221 } else {
222 jobView->setDescriptionField(0, field1.first, field1.second);
223 }
224
225 if (field2.first.isNull() || field2.second.isNull()) {
226 jobView->clearDescriptionField(1);
227 } else {
228 jobView->setDescriptionField(1, field2.first, field2.second);
229 }
230}
231
232void KUiServerJobTracker::infoMessage(KJob *job, const QString &message)
233{
234 if (!d->progressJobView.contains(job)) {
235 return;
236 }
237
238 org::kde::JobViewV2 *jobView = d->progressJobView[job];
239
240 jobView->setInfoMessage(message);
241}
242
243void KUiServerJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
244{
245 if (!d->progressJobView.contains(job)) {
246 return;
247 }
248
249 org::kde::JobViewV2 *jobView = d->progressJobView[job];
250
251 switch (unit) {
252 case KJob::Bytes:
253 jobView->setTotalAmount(amount, QStringLiteral("bytes"));
254 break;
255 case KJob::Files:
256 jobView->setTotalAmount(amount, QStringLiteral("files"));
257 break;
259 jobView->setTotalAmount(amount, QStringLiteral("dirs"));
260 break;
261 case KJob::Items:
262 jobView->setTotalAmount(amount, QStringLiteral("items"));
263 break;
264 case KJob::UnitsCount:
265 Q_UNREACHABLE();
266 break;
267 }
268}
269
270void KUiServerJobTracker::processedAmount(KJob *job, KJob::Unit unit, qulonglong amount)
271{
272 if (!d->progressJobView.contains(job)) {
273 return;
274 }
275
276 org::kde::JobViewV2 *jobView = d->progressJobView[job];
277
278 jobView->setElapsedTime(job->elapsedTime());
279
280 switch (unit) {
281 case KJob::Bytes:
282 jobView->setProcessedAmount(amount, QStringLiteral("bytes"));
283 break;
284 case KJob::Files:
285 jobView->setProcessedAmount(amount, QStringLiteral("files"));
286 break;
288 jobView->setProcessedAmount(amount, QStringLiteral("dirs"));
289 break;
290 case KJob::Items:
291 jobView->setProcessedAmount(amount, QStringLiteral("items"));
292 break;
293 case KJob::UnitsCount:
294 Q_UNREACHABLE();
295 break;
296 }
297}
298
299void KUiServerJobTracker::percent(KJob *job, unsigned long percent)
300{
301 if (!d->progressJobView.contains(job)) {
302 return;
303 }
304
305 org::kde::JobViewV2 *jobView = d->progressJobView[job];
306
307 jobView->setPercent(percent);
308}
309
310void KUiServerJobTracker::speed(KJob *job, unsigned long value)
311{
312 if (!d->progressJobView.contains(job)) {
313 return;
314 }
315
316 org::kde::JobViewV2 *jobView = d->progressJobView[job];
317
318 jobView->setSpeed(value);
319}
320
321KSharedUiServerProxy::KSharedUiServerProxy()
322 : m_uiserver(new org::kde::JobViewServer(QStringLiteral("org.kde.JobViewServer"), QStringLiteral("/JobViewServer"), QDBusConnection::sessionBus()))
323 , m_watcher(new QDBusServiceWatcher(QStringLiteral("org.kde.JobViewServer"), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange))
324{
326 if (!bus->isServiceRegistered(QStringLiteral("org.kde.JobViewServer"))) {
327 QDBusReply<void> reply = bus->startService(QStringLiteral("org.kde.kuiserver"));
328 if (!reply.isValid()) {
329 qCCritical(KJOBWIDGETS) << "Couldn't start kuiserver from org.kde.kuiserver.service:" << reply.error();
330 return;
331 }
332
333 if (!bus->isServiceRegistered(QStringLiteral("org.kde.JobViewServer"))) {
334 qCDebug(KJOBWIDGETS) << "The dbus name org.kde.JobViewServer is STILL NOT REGISTERED, even after starting kuiserver. Should not happen.";
335 return;
336 }
337
338 qCDebug(KJOBWIDGETS) << "kuiserver registered";
339 } else {
340 qCDebug(KJOBWIDGETS) << "kuiserver found";
341 }
342
343 connect(m_watcher.get(), &QDBusServiceWatcher::serviceOwnerChanged, this, &KSharedUiServerProxy::uiserverOwnerChanged);
344
345 // cleanup early enough to avoid issues with dbus at application exit
346 // see e.g. https://phabricator.kde.org/D2545
347 qAddPostRoutine([]() {
348 serverProxy->m_uiserver.reset();
349 serverProxy->m_watcher.reset();
350 });
351}
352
353KSharedUiServerProxy::~KSharedUiServerProxy()
354{
355}
356
357org::kde::JobViewServer *KSharedUiServerProxy::uiserver()
358{
359 return m_uiserver.get();
360}
361
362void KSharedUiServerProxy::uiserverOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
363{
364 Q_UNUSED(serviceName);
365 Q_UNUSED(oldOwner);
366
367 if (!newOwner.isEmpty()) { // registered
368 Q_EMIT serverRegistered();
369 } else if (newOwner.isEmpty()) { // unregistered
370 Q_EMIT serverUnregistered();
371 }
372}
373
374#include "moc_kuiserverjobtracker.cpp"
375#include "moc_kuiserverjobtracker_p.cpp"
virtual void registerJob(KJob *job)
virtual void unregisterJob(KJob *job)
bool resume()
bool suspend()
qint64 elapsedTime() const
int error() const
Capabilities capabilities() const
QString errorText() const
bool kill(KJob::KillVerbosity verbosity=KJob::Quietly)
The interface to implement to track the progresses of a job.
void unregisterJob(KJob *job) override
Unregister a job from this tracker.
KUiServerJobTracker(QObject *parent=nullptr)
Creates a new KJobTrackerInterface.
void registerJob(KJob *job) override
Register a new job in this tracker.
void finished(KJob *job) override
The following slots are inherited from KJobTrackerInterface.
~KUiServerJobTracker() override
Destroys a KJobTrackerInterface.
QDBusConnectionInterface * interface() const const
QDBusConnection sessionBus()
QDBusReply< bool > isServiceRegistered(const QString &serviceName) const const
QDBusReply< void > startService(const QString &name)
const QDBusError & error()
bool isValid() const const
void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
Key key(const T &value) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
QObject * sender() const const
bool isEmpty() const const
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.