• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • jobs
kuiserverjobtracker.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
3 Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18
19*/
20
21#include "kuiserverjobtracker.h"
22#include "kuiserverjobtracker_p.h"
23
24#include "jobviewiface.h"
25#include "jobviewifacev2.h"
26
27#include <klocale.h>
28#include <kdebug.h>
29#include <ktoolinvocation.h>
30#include <kcomponentdata.h>
31#include <kaboutdata.h>
32#include <kjob.h>
33
34K_GLOBAL_STATIC(KSharedUiServerProxy, serverProxy)
35
36class KUiServerJobTracker::Private
37{
38public:
39 Private(KUiServerJobTracker *parent)
40 : q(parent)
41 {
42 }
43
44 KUiServerJobTracker *const q;
45
46 void _k_killJob();
47
48 QHash<KJob*, org::kde::JobViewV2*> progressJobView;
49};
50
51void KUiServerJobTracker::Private::_k_killJob()
52{
53 org::kde::JobViewV2 *jobView = qobject_cast<org::kde::JobViewV2*>(q->sender());
54
55 if (jobView) {
56 KJob *job = progressJobView.key(jobView);
57
58 if (job)
59 job->kill(KJob::EmitResult);
60 }
61}
62
63KUiServerJobTracker::KUiServerJobTracker(QObject *parent)
64 : KJobTrackerInterface(parent), d(new Private(this))
65{
66
67}
68
69KUiServerJobTracker::~KUiServerJobTracker()
70{
71 if (!d->progressJobView.isEmpty()) {
72 qWarning() << "A KUiServerJobTracker instance contains"
73 << d->progressJobView.size() << "stalled jobs";
74 }
75
76 delete d;
77}
78
79void KUiServerJobTracker::registerJob(KJob *job)
80{
81 // Already registered job?
82 if (d->progressJobView.contains(job)) {
83 return;
84 }
85
86 KComponentData componentData = KGlobal::mainComponent();
87 QString programIconName = componentData.aboutData()->programIconName();
88
89 if (programIconName.isEmpty()) {
90 programIconName = componentData.aboutData()->appName();
91 }
92
93 QWeakPointer<KJob> jobWatch = job;
94 QDBusReply<QDBusObjectPath> reply = serverProxy->uiserver().requestView(componentData.aboutData()->programName(),
95 programIconName,
96 job->capabilities());
97
98 // If we got a valid reply, register the interface for later usage.
99 if (reply.isValid()) {
100 org::kde::JobViewV2 *jobView = new org::kde::JobViewV2("org.kde.JobViewServer",
101 reply.value().path(),
102 QDBusConnection::sessionBus());
103 if (!jobWatch) {
104 //kDebug() << "deleted out from under us when asking the server proxy for the view";
105 jobView->terminate(QString());
106 delete jobView;
107 return;
108 }
109
110 QObject::connect(jobView, SIGNAL(cancelRequested()), this,
111 SLOT(_k_killJob()));
112 QObject::connect(jobView, SIGNAL(suspendRequested()), job,
113 SLOT(suspend()));
114 QObject::connect(jobView, SIGNAL(resumeRequested()), job,
115 SLOT(resume()));
116
117 QVariant destUrl = job->property("destUrl");
118 if (destUrl.isValid()) {
119 jobView->setDestUrl(QDBusVariant(destUrl));
120 }
121
122 if (!jobWatch) {
123 //kDebug() << "deleted out from under us when creating the dbus interface";
124 jobView->terminate(QString());
125 delete jobView;
126 return;
127 }
128
129 d->progressJobView.insert(job, jobView);
130 } else if (!jobWatch) {
131 qWarning() << "Uh-oh...KUiServerJobTracker was trying to forward a job, but it was deleted from under us."
132 << "kuiserver *may* have a stranded job. we can't do anything about it because the returned objectPath is invalid.";
133 return;
134 }
135
136 KJobTrackerInterface::registerJob(job);
137}
138
139void KUiServerJobTracker::unregisterJob(KJob *job)
140{
141 KJobTrackerInterface::unregisterJob(job);
142
143 if (!d->progressJobView.contains(job)) {
144 return;
145 }
146
147 org::kde::JobViewV2 *jobView = d->progressJobView.take(job);
148
149 if (job->error()) {
150 jobView->terminate(job->errorText());
151 } else {
152 jobView->terminate(QString());
153 }
154
155 delete jobView;
156}
157
158void KUiServerJobTracker::finished(KJob *job)
159{
160 if (!d->progressJobView.contains(job)) {
161 return;
162 }
163
164 org::kde::JobViewV2 *jobView = d->progressJobView.take(job);
165
166 if (job->error()) {
167 jobView->terminate(job->errorText());
168 } else {
169 jobView->terminate(QString());
170 }
171}
172
173void KUiServerJobTracker::suspended(KJob *job)
174{
175 if (!d->progressJobView.contains(job)) {
176 return;
177 }
178
179 org::kde::JobViewV2 *jobView = d->progressJobView[job];
180
181 jobView->setSuspended(true);
182}
183
184void KUiServerJobTracker::resumed(KJob *job)
185{
186 if (!d->progressJobView.contains(job)) {
187 return;
188 }
189
190 org::kde::JobViewV2 *jobView = d->progressJobView[job];
191
192 jobView->setSuspended(false);
193}
194
195void KUiServerJobTracker::description(KJob *job, const QString &title,
196 const QPair<QString, QString> &field1,
197 const QPair<QString, QString> &field2)
198{
199 if (!d->progressJobView.contains(job)) {
200 return;
201 }
202
203 org::kde::JobViewV2 *jobView = d->progressJobView[job];
204
205 jobView->setInfoMessage(title);
206
207 if (field1.first.isNull() || field1.second.isNull()) {
208 jobView->clearDescriptionField(0);
209 } else {
210 jobView->setDescriptionField(0, field1.first, field1.second);
211 }
212
213 if (field2.first.isNull() || field2.second.isNull()) {
214 jobView->clearDescriptionField(1);
215 } else {
216 jobView->setDescriptionField(1, field2.first, field2.second);
217 }
218}
219
220void KUiServerJobTracker::infoMessage(KJob *job, const QString &plain, const QString &rich)
221{
222 Q_UNUSED(rich)
223
224 if (!d->progressJobView.contains(job)) {
225 return;
226 }
227
228 org::kde::JobViewV2 *jobView = d->progressJobView[job];
229
230 jobView->setInfoMessage(plain);
231}
232
233void KUiServerJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
234{
235 if (!d->progressJobView.contains(job)) {
236 return;
237 }
238
239 org::kde::JobViewV2 *jobView = d->progressJobView[job];
240
241 switch (unit) {
242 case KJob::Bytes:
243 jobView->setTotalAmount(amount, "bytes");
244 break;
245 case KJob::Files:
246 jobView->setTotalAmount(amount, "files");
247 break;
248 case KJob::Directories:
249 jobView->setTotalAmount(amount, "dirs");
250 break;
251 default:
252 break;
253 }
254}
255
256void KUiServerJobTracker::processedAmount(KJob *job, KJob::Unit unit, qulonglong amount)
257{
258 if (!d->progressJobView.contains(job)) {
259 return;
260 }
261
262 org::kde::JobViewV2 *jobView = d->progressJobView[job];
263
264 switch (unit) {
265 case KJob::Bytes:
266 jobView->setProcessedAmount(amount, "bytes");
267 break;
268 case KJob::Files:
269 jobView->setProcessedAmount(amount, "files");
270 break;
271 case KJob::Directories:
272 jobView->setProcessedAmount(amount, "dirs");
273 break;
274 default:
275 break;
276 }
277}
278
279void KUiServerJobTracker::percent(KJob *job, unsigned long percent)
280{
281 if (!d->progressJobView.contains(job)) {
282 return;
283 }
284
285 org::kde::JobViewV2 *jobView = d->progressJobView[job];
286
287 jobView->setPercent(percent);
288}
289
290void KUiServerJobTracker::speed(KJob *job, unsigned long value)
291{
292 if (!d->progressJobView.contains(job)) {
293 return;
294 }
295
296 org::kde::JobViewV2 *jobView = d->progressJobView[job];
297
298 jobView->setSpeed(value);
299}
300
301KSharedUiServerProxy::KSharedUiServerProxy()
302 : m_uiserver("org.kde.JobViewServer", "/JobViewServer", QDBusConnection::sessionBus())
303{
304 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.JobViewServer"))
305 {
306 QString error;
307 int ret = KToolInvocation::startServiceByDesktopPath("kuiserver.desktop",
308 QStringList(), &error);
309 if (ret > 0)
310 {
311 kError() << "Couldn't start kuiserver from kuiserver.desktop: " << error;
312 }
313 }
314
315 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.JobViewServer"))
316 kDebug() << "The dbus name org.kde.JobViewServer is STILL NOT REGISTERED, even after starting kuiserver. Should not happen.";
317 else
318 kDebug() << "kuiserver registered";
319}
320
321KSharedUiServerProxy::~KSharedUiServerProxy()
322{
323}
324
325org::kde::JobViewServer &KSharedUiServerProxy::uiserver()
326{
327 return m_uiserver;
328}
329
330#include "kuiserverjobtracker.moc"
331#include "kuiserverjobtracker_p.moc"
KAboutData::programName
QString programName() const
KAboutData::programIconName
QString programIconName() const
KAboutData::appName
QString appName() const
KComponentData
KComponentData::aboutData
const KAboutData * aboutData() const
KJobTrackerInterface
KJobTrackerInterface::registerJob
virtual void registerJob(KJob *job)
KJobTrackerInterface::unregisterJob
virtual void unregisterJob(KJob *job)
KJob
KJob::Unit
Unit
KJob::Files
Files
KJob::Bytes
Bytes
KJob::Directories
Directories
KJob::EmitResult
EmitResult
KJob::error
int error() const
KJob::kill
bool kill(KillVerbosity verbosity=Quietly)
KJob::capabilities
Capabilities capabilities() const
KJob::errorText
QString errorText() const
KToolInvocation::startServiceByDesktopPath
static int startServiceByDesktopPath(const QString &_name, const QString &URL, QString *error=0, QString *serviceName=0, int *pid=0, const QByteArray &startup_id=QByteArray(), bool noWait=false)
KUiServerJobTracker
The interface to implement to track the progresses of a job.
Definition: kuiserverjobtracker.h:32
KUiServerJobTracker::speed
virtual void speed(KJob *job, unsigned long value)
Definition: kuiserverjobtracker.cpp:290
KUiServerJobTracker::percent
virtual void percent(KJob *job, unsigned long percent)
Definition: kuiserverjobtracker.cpp:279
KUiServerJobTracker::~KUiServerJobTracker
virtual ~KUiServerJobTracker()
Destroys a KJobTrackerInterface.
Definition: kuiserverjobtracker.cpp:69
KUiServerJobTracker::suspended
virtual void suspended(KJob *job)
Definition: kuiserverjobtracker.cpp:173
KUiServerJobTracker::description
virtual void description(KJob *job, const QString &title, const QPair< QString, QString > &field1, const QPair< QString, QString > &field2)
Definition: kuiserverjobtracker.cpp:195
KUiServerJobTracker::totalAmount
virtual void totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
Definition: kuiserverjobtracker.cpp:233
KUiServerJobTracker::infoMessage
virtual void infoMessage(KJob *job, const QString &plain, const QString &rich)
Definition: kuiserverjobtracker.cpp:220
KUiServerJobTracker::finished
virtual void finished(KJob *job)
The following slots are inherited from KJobTrackerInterface.
Definition: kuiserverjobtracker.cpp:158
KUiServerJobTracker::processedAmount
virtual void processedAmount(KJob *job, KJob::Unit unit, qulonglong amount)
Definition: kuiserverjobtracker.cpp:256
KUiServerJobTracker::resumed
virtual void resumed(KJob *job)
Definition: kuiserverjobtracker.cpp:184
KUiServerJobTracker::KUiServerJobTracker
KUiServerJobTracker(QObject *parent=0)
Creates a new KJobTrackerInterface.
Definition: kuiserverjobtracker.cpp:63
QHash
QObject
QPair
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kDebug
#define kDebug
kaboutdata.h
kcomponentdata.h
kdebug.h
kjob.h
klocale.h
ktoolinvocation.h
kuiserverjobtracker.h
KGlobal::mainComponent
const KComponentData & mainComponent()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal