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

KDECore

  • kdecore
  • jobs
kjob.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
3 David Faure <faure@kde.org>
4 Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20*/
21
22#include "kjob.h"
23#include "kjob_p.h"
24
25#include "kjobuidelegate.h"
26
27#include <kglobal.h>
28#include <QEventLoop>
29#include <QMap>
30#include <QMetaType>
31#include <QTimer>
32
33bool KJobPrivate::_k_kjobUnitEnumRegistered = false;
34KJobPrivate::KJobPrivate()
35 : q_ptr(0), uiDelegate(0), error(KJob::NoError),
36 progressUnit(KJob::Bytes), percentage(0),
37 suspended(false), capabilities(KJob::NoCapabilities),
38 speedTimer(0), isAutoDelete(true), eventLoop(0), isFinished(false)
39{
40 if (!_k_kjobUnitEnumRegistered) {
41 _k_kjobUnitEnumRegistered = qRegisterMetaType<KJob::Unit>("KJob::Unit");
42 }
43}
44
45KJobPrivate::~KJobPrivate()
46{
47}
48
49KJob::KJob(QObject *parent)
50 : QObject(parent), d_ptr(new KJobPrivate)
51{
52 d_ptr->q_ptr = this;
53 // Don't exit while this job is running
54 KGlobal::ref();
55}
56
57KJob::KJob(KJobPrivate &dd, QObject *parent)
58 : QObject(parent), d_ptr(&dd)
59{
60 d_ptr->q_ptr = this;
61 // Don't exit while this job is running
62 KGlobal::ref();
63}
64
65KJob::~KJob()
66{
67 if (!d_ptr->isFinished) {
68 emit finished(this);
69 }
70
71 delete d_ptr->speedTimer;
72 delete d_ptr->uiDelegate;
73 delete d_ptr;
74
75 KGlobal::deref();
76}
77
78void KJob::setUiDelegate( KJobUiDelegate *delegate )
79{
80 Q_D(KJob);
81 if ( delegate == 0 || delegate->setJob( this ) )
82 {
83 delete d->uiDelegate;
84 d->uiDelegate = delegate;
85
86 if ( d->uiDelegate )
87 {
88 d->uiDelegate->connectJob( this );
89 }
90 }
91}
92
93KJobUiDelegate *KJob::uiDelegate() const
94{
95 return d_func()->uiDelegate;
96}
97
98KJob::Capabilities KJob::capabilities() const
99{
100 return d_func()->capabilities;
101}
102
103bool KJob::isSuspended() const
104{
105 return d_func()->suspended;
106}
107
108bool KJob::kill( KillVerbosity verbosity )
109{
110 Q_D(KJob);
111 if ( doKill() )
112 {
113 setError( KilledJobError );
114
115 if ( verbosity!=Quietly )
116 {
117 emitResult();
118 }
119 else
120 {
121 // If we are displaying a progress dialog, remove it first.
122 d->isFinished = true;
123 emit finished(this);
124
125 if ( isAutoDelete() )
126 deleteLater();
127 }
128
129 return true;
130 }
131 else
132 {
133 return false;
134 }
135}
136
137bool KJob::suspend()
138{
139 Q_D(KJob);
140 if ( !d->suspended )
141 {
142 if ( doSuspend() )
143 {
144 d->suspended = true;
145 emit suspended(this);
146
147 return true;
148 }
149 }
150
151 return false;
152}
153
154bool KJob::resume()
155{
156 Q_D(KJob);
157 if ( d->suspended )
158 {
159 if ( doResume() )
160 {
161 d->suspended = false;
162 emit resumed(this);
163
164 return true;
165 }
166 }
167
168 return false;
169}
170
171bool KJob::doKill()
172{
173 return false;
174}
175
176bool KJob::doSuspend()
177{
178 return false;
179}
180
181bool KJob::doResume()
182{
183 return false;
184}
185
186void KJob::setCapabilities( KJob::Capabilities capabilities )
187{
188 Q_D(KJob);
189 d->capabilities = capabilities;
190}
191
192bool KJob::exec()
193{
194 Q_D(KJob);
195 // Usually this job would delete itself, via deleteLater() just after
196 // emitting result() (unless configured otherwise). Since we use an event
197 // loop below, that event loop will process the deletion event and we'll
198 // have been deleted when exec() returns. This crashes, so temporarily
199 // suspend autodeletion and manually do it afterwards.
200 const bool wasAutoDelete = isAutoDelete();
201 setAutoDelete( false );
202
203 Q_ASSERT( ! d->eventLoop );
204
205 QEventLoop loop( this );
206 d->eventLoop = &loop;
207
208 start();
209 if( !d->isFinished ) {
210 d->eventLoop->exec(QEventLoop::ExcludeUserInputEvents);
211 }
212 d->eventLoop = 0;
213
214 if ( wasAutoDelete ) {
215 deleteLater();
216 }
217 return ( d->error == NoError );
218}
219
220int KJob::error() const
221{
222 return d_func()->error;
223}
224
225QString KJob::errorText() const
226{
227 return d_func()->errorText;
228}
229
230QString KJob::errorString() const
231{
232 return d_func()->errorText;
233}
234
235qulonglong KJob::processedAmount(Unit unit) const
236{
237 return d_func()->processedAmount[unit];
238}
239
240qulonglong KJob::totalAmount(Unit unit) const
241{
242 return d_func()->totalAmount[unit];
243}
244
245unsigned long KJob::percent() const
246{
247 return d_func()->percentage;
248}
249
250void KJob::setError( int errorCode )
251{
252 Q_D(KJob);
253 d->error = errorCode;
254}
255
256void KJob::setErrorText( const QString &errorText )
257{
258 Q_D(KJob);
259 d->errorText = errorText;
260}
261
262void KJob::setProcessedAmount(Unit unit, qulonglong amount)
263{
264 Q_D(KJob);
265 bool should_emit = (d->processedAmount[unit] != amount);
266
267 d->processedAmount[unit] = amount;
268
269 if ( should_emit )
270 {
271 emit processedAmount(this, unit, amount);
272 if (unit==d->progressUnit) {
273 emit processedSize(this, amount);
274 emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
275 }
276 }
277}
278
279void KJob::setTotalAmount(Unit unit, qulonglong amount)
280{
281 Q_D(KJob);
282 bool should_emit = (d->totalAmount[unit] != amount);
283
284 d->totalAmount[unit] = amount;
285
286 if ( should_emit )
287 {
288 emit totalAmount(this, unit, amount);
289 if (unit==d->progressUnit) {
290 emit totalSize(this, amount);
291 emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
292 }
293 }
294}
295
296void KJob::setPercent( unsigned long percentage )
297{
298 Q_D(KJob);
299 if ( d->percentage!=percentage )
300 {
301 d->percentage = percentage;
302 emit percent( this, percentage );
303 }
304}
305
306void KJob::emitResult()
307{
308 Q_D(KJob);
309 d->isFinished = true;
310
311 if ( d->eventLoop ) {
312 d->eventLoop->quit();
313 }
314
315 // If we are displaying a progress dialog, remove it first.
316 emit finished( this );
317
318 emit result( this );
319
320 if ( isAutoDelete() )
321 deleteLater();
322}
323
324void KJob::emitPercent( qulonglong processedAmount, qulonglong totalAmount )
325{
326 Q_D(KJob);
327 // calculate percents
328 if (totalAmount) {
329 unsigned long oldPercentage = d->percentage;
330 d->percentage = (unsigned long)(( (float)(processedAmount) / (float)(totalAmount) ) * 100.0);
331 if ( d->percentage != oldPercentage ) {
332 emit percent( this, d->percentage );
333 }
334 }
335}
336
337void KJob::emitSpeed(unsigned long value)
338{
339 Q_D(KJob);
340 if (!d->speedTimer) {
341 d->speedTimer = new QTimer(this);
342 connect(d->speedTimer, SIGNAL(timeout()), SLOT(_k_speedTimeout()));
343 }
344
345 emit speed(this, value);
346 d->speedTimer->start(5000); // 5 seconds interval should be enough
347}
348
349void KJobPrivate::_k_speedTimeout()
350{
351 Q_Q(KJob);
352 // send 0 and stop the timer
353 // timer will be restarted only when we receive another speed event
354 emit q->speed(q, 0);
355 speedTimer->stop();
356}
357
358bool KJob::isAutoDelete() const
359{
360 Q_D(const KJob);
361 return d->isAutoDelete;
362}
363
364void KJob::setAutoDelete( bool autodelete )
365{
366 Q_D(KJob);
367 d->isAutoDelete = autodelete;
368}
369
370#include "kjob.moc"
KJobPrivate
Definition: kjob_p.h:36
KJobPrivate::q_ptr
KJob * q_ptr
Definition: kjob_p.h:41
KJobPrivate::uiDelegate
KJobUiDelegate * uiDelegate
Definition: kjob_p.h:43
KJobPrivate::isFinished
bool isFinished
Definition: kjob_p.h:60
KJobPrivate::~KJobPrivate
virtual ~KJobPrivate()
Definition: kjob.cpp:45
KJobPrivate::KJobPrivate
KJobPrivate()
Definition: kjob.cpp:34
KJobPrivate::_k_speedTimeout
void _k_speedTimeout()
Definition: kjob.cpp:349
KJobPrivate::speedTimer
QTimer * speedTimer
Definition: kjob_p.h:52
KJobPrivate::_k_kjobUnitEnumRegistered
static bool _k_kjobUnitEnumRegistered
Definition: kjob_p.h:58
KJobUiDelegate
The base class for all KJob UI delegate.
Definition: kjobuidelegate.h:40
KJob
The base class for all jobs.
Definition: kjob.h:85
KJob::resumed
void resumed(KJob *job)
Emitted when the job is resumed.
KJob::exec
bool exec()
Executes the job synchronously.
Definition: kjob.cpp:192
KJob::setErrorText
void setErrorText(const QString &errorText)
Sets the error text.
Definition: kjob.cpp:256
KJob::Unit
Unit
Definition: kjob.h:91
KJob::resume
bool resume()
Resumes this job.
Definition: kjob.cpp:154
KJob::start
virtual void start()=0
Starts the job asynchronously.
KJob::errorString
virtual QString errorString() const
A human-readable error message.
Definition: kjob.cpp:230
KJob::suspend
bool suspend()
Suspends this job.
Definition: kjob.cpp:137
KJob::setTotalAmount
void setTotalAmount(Unit unit, qulonglong amount)
Sets the total size.
Definition: kjob.cpp:279
KJob::emitResult
void emitResult()
Utility function to emit the result signal, and suicide this job.
Definition: kjob.cpp:306
KJob::d_ptr
KJobPrivate *const d_ptr
Definition: kjob.h:627
KJob::totalAmount
qulonglong totalAmount(Unit unit) const
Returns the total amount of a given unit for this job.
Definition: kjob.cpp:240
KJob::KillVerbosity
KillVerbosity
Definition: kjob.h:168
KJob::Quietly
@ Quietly
Definition: kjob.h:168
KJob::error
int error() const
Returns the error code, if there has been an error.
Definition: kjob.cpp:220
KJob::processedSize
void processedSize(KJob *job, qulonglong size)
Regularly emitted to show the progress of this job (current data size in bytes for transfers,...
KJob::kill
bool kill(KillVerbosity verbosity=Quietly)
Aborts this job.
Definition: kjob.cpp:108
KJob::result
void result(KJob *job)
Emitted when the job is finished (except when killed with KJob::Quietly).
KJob::NoError
@ NoError
Definition: kjob.h:257
KJob::KilledJobError
@ KilledJobError
Definition: kjob.h:259
KJob::doKill
virtual bool doKill()
Aborts this job quietly.
Definition: kjob.cpp:171
KJob::isSuspended
bool isSuspended() const
Returns if the job was suspended with the suspend() call.
Definition: kjob.cpp:103
KJob::doSuspend
virtual bool doSuspend()
Suspends this job.
Definition: kjob.cpp:176
KJob::finished
void finished(KJob *job)
Emitted when the job is finished, in any case.
KJob::processedAmount
qulonglong processedAmount(Unit unit) const
Returns the processed amount of a given unit for this job.
Definition: kjob.cpp:235
KJob::setPercent
void setPercent(unsigned long percentage)
Sets the overall progress of the job.
Definition: kjob.cpp:296
KJob::percent
unsigned long percent() const
Returns the overall progress of this job.
Definition: kjob.cpp:245
KJob::capabilities
Capabilities capabilities() const
Returns the capabilities of this job.
Definition: kjob.cpp:98
KJob::setError
void setError(int errorCode)
Sets the error code.
Definition: kjob.cpp:250
KJob::uiDelegate
KJobUiDelegate * uiDelegate() const
Retrieves the delegate attached to this job.
Definition: kjob.cpp:93
KJob::isAutoDelete
bool isAutoDelete() const
Returns whether this job automatically deletes itself once the job is finished.
Definition: kjob.cpp:358
KJob::processedAmount
void processedAmount(KJob *job, KJob::Unit unit, qulonglong amount)
Regularly emitted to show the progress of this job by giving the current amount.
KJob::totalSize
void totalSize(KJob *job, qulonglong size)
Emitted when we know the size of this job (data size in bytes for transfers, number of entries for li...
KJob::setCapabilities
void setCapabilities(Capabilities capabilities)
Sets the capabilities for this job.
Definition: kjob.cpp:186
KJob::suspended
void suspended(KJob *job)
Emitted when the job is suspended.
KJob::totalAmount
void totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
Emitted when we know the amount the job will have to process.
KJob::KJob
KJob(QObject *parent=0)
Creates a new KJob object.
Definition: kjob.cpp:49
KJob::emitPercent
void emitPercent(qulonglong processedAmount, qulonglong totalAmount)
Utility function for inherited jobs.
Definition: kjob.cpp:324
KJob::doResume
virtual bool doResume()
Resumes this job.
Definition: kjob.cpp:181
KJob::setAutoDelete
void setAutoDelete(bool autodelete)
set the auto-delete property of the job.
Definition: kjob.cpp:364
KJob::errorText
QString errorText() const
Returns the error text if there has been an error.
Definition: kjob.cpp:225
KJob::setProcessedAmount
void setProcessedAmount(Unit unit, qulonglong amount)
Sets the processed size.
Definition: kjob.cpp:262
KJob::~KJob
virtual ~KJob()
Destroys a KJob object.
Definition: kjob.cpp:65
KJob::setUiDelegate
void setUiDelegate(KJobUiDelegate *delegate)
Attach a UI delegate to this job.
Definition: kjob.cpp:78
KJob::emitSpeed
void emitSpeed(unsigned long speed)
Utility function for inherited jobs.
Definition: kjob.cpp:337
KJob::speed
void speed(KJob *job, unsigned long speed)
Emitted to display information about the speed of this job.
QObject
QString
kglobal.h
kjob.h
kjob_p.h
kjobuidelegate.h
timeout
int timeout
Definition: kkernel_mac.cpp:46
setError
static void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error, const QString &errorString)
Definition: ksocketfactory.cpp:44
KGlobal::deref
void deref()
Tells KGlobal that one operation such as those described in ref() just finished.
Definition: kglobal.cpp:326
KGlobal::ref
void ref()
Tells KGlobal about one more operations that should be finished before the application exits.
Definition: kglobal.cpp:321
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.

KDECore

Skip menu "KDECore"
  • 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