KDECore
kjob.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kjob.h"
00023 #include "kjob_p.h"
00024
00025 #include "kjobuidelegate.h"
00026
00027 #include <kglobal.h>
00028 #include <QEventLoop>
00029 #include <QMap>
00030 #include <QMetaType>
00031 #include <QTimer>
00032
00033 bool KJobPrivate::_k_kjobUnitEnumRegistered = false;
00034 KJobPrivate::KJobPrivate()
00035 : q_ptr(0), uiDelegate(0), error(KJob::NoError),
00036 progressUnit(KJob::Bytes), percentage(0),
00037 suspended(false), capabilities(KJob::NoCapabilities),
00038 speedTimer(0), isAutoDelete(true), isFinished(false)
00039 {
00040 if (!_k_kjobUnitEnumRegistered) {
00041 _k_kjobUnitEnumRegistered = qRegisterMetaType<KJob::Unit>("KJob::Unit");
00042 }
00043 }
00044
00045 KJobPrivate::~KJobPrivate()
00046 {
00047 }
00048
00049 KJob::KJob(QObject *parent)
00050 : QObject(parent), d_ptr(new KJobPrivate)
00051 {
00052 d_ptr->q_ptr = this;
00053
00054 KGlobal::ref();
00055 }
00056
00057 KJob::KJob(KJobPrivate &dd, QObject *parent)
00058 : QObject(parent), d_ptr(&dd)
00059 {
00060 d_ptr->q_ptr = this;
00061
00062 KGlobal::ref();
00063 }
00064
00065 KJob::~KJob()
00066 {
00067 delete d_ptr->speedTimer;
00068 delete d_ptr->uiDelegate;
00069 delete d_ptr;
00070
00071 KGlobal::deref();
00072 }
00073
00074 void KJob::setUiDelegate( KJobUiDelegate *delegate )
00075 {
00076 Q_D(KJob);
00077 if ( delegate == 0 || delegate->setJob( this ) )
00078 {
00079 delete d->uiDelegate;
00080 d->uiDelegate = delegate;
00081
00082 if ( d->uiDelegate )
00083 {
00084 d->uiDelegate->connectJob( this );
00085 }
00086 }
00087 }
00088
00089 KJobUiDelegate *KJob::uiDelegate() const
00090 {
00091 return d_func()->uiDelegate;
00092 }
00093
00094 KJob::Capabilities KJob::capabilities() const
00095 {
00096 return d_func()->capabilities;
00097 }
00098
00099 bool KJob::isSuspended() const
00100 {
00101 return d_func()->suspended;
00102 }
00103
00104 bool KJob::kill( KillVerbosity verbosity )
00105 {
00106 if ( doKill() )
00107 {
00108 setError( KilledJobError );
00109
00110 if ( verbosity!=Quietly )
00111 {
00112 emitResult();
00113 }
00114 else
00115 {
00116
00117 emit finished(this);
00118
00119 if ( isAutoDelete() )
00120 deleteLater();
00121 }
00122
00123 return true;
00124 }
00125 else
00126 {
00127 return false;
00128 }
00129 }
00130
00131 bool KJob::suspend()
00132 {
00133 Q_D(KJob);
00134 if ( !d->suspended )
00135 {
00136 if ( doSuspend() )
00137 {
00138 d->suspended = true;
00139 emit suspended(this);
00140
00141 return true;
00142 }
00143 }
00144
00145 return false;
00146 }
00147
00148 bool KJob::resume()
00149 {
00150 Q_D(KJob);
00151 if ( d->suspended )
00152 {
00153 if ( doResume() )
00154 {
00155 d->suspended = false;
00156 emit resumed(this);
00157
00158 return true;
00159 }
00160 }
00161
00162 return false;
00163 }
00164
00165 bool KJob::doKill()
00166 {
00167 return false;
00168 }
00169
00170 bool KJob::doSuspend()
00171 {
00172 return false;
00173 }
00174
00175 bool KJob::doResume()
00176 {
00177 return false;
00178 }
00179
00180 void KJob::setCapabilities( KJob::Capabilities capabilities )
00181 {
00182 Q_D(KJob);
00183 d->capabilities = capabilities;
00184 }
00185
00186 bool KJob::exec()
00187 {
00188 Q_D(KJob);
00189
00190
00191
00192
00193
00194 const bool wasAutoDelete = isAutoDelete();
00195 setAutoDelete( false );
00196 QEventLoop loop( this );
00197
00198 connect( this, SIGNAL( result( KJob* ) ),
00199 &loop, SLOT( quit() ) );
00200 start();
00201 if( !d->isFinished ) {
00202 loop.exec(QEventLoop::ExcludeUserInputEvents);
00203 }
00204
00205 if ( wasAutoDelete ) {
00206 deleteLater();
00207 }
00208 return ( d->error == NoError );
00209 }
00210
00211 int KJob::error() const
00212 {
00213 return d_func()->error;
00214 }
00215
00216 QString KJob::errorText() const
00217 {
00218 return d_func()->errorText;
00219 }
00220
00221 QString KJob::errorString() const
00222 {
00223 return d_func()->errorText;
00224 }
00225
00226 qulonglong KJob::processedAmount(Unit unit) const
00227 {
00228 return d_func()->processedAmount[unit];
00229 }
00230
00231 qulonglong KJob::totalAmount(Unit unit) const
00232 {
00233 return d_func()->totalAmount[unit];
00234 }
00235
00236 unsigned long KJob::percent() const
00237 {
00238 return d_func()->percentage;
00239 }
00240
00241 void KJob::setError( int errorCode )
00242 {
00243 Q_D(KJob);
00244 d->error = errorCode;
00245 }
00246
00247 void KJob::setErrorText( const QString &errorText )
00248 {
00249 Q_D(KJob);
00250 d->errorText = errorText;
00251 }
00252
00253 void KJob::setProcessedAmount(Unit unit, qulonglong amount)
00254 {
00255 Q_D(KJob);
00256 bool should_emit = (d->processedAmount[unit] != amount);
00257
00258 d->processedAmount[unit] = amount;
00259
00260 if ( should_emit )
00261 {
00262 emit processedAmount(this, unit, amount);
00263 if (unit==d->progressUnit) {
00264 emit processedSize(this, amount);
00265 emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00266 }
00267 }
00268 }
00269
00270 void KJob::setTotalAmount(Unit unit, qulonglong amount)
00271 {
00272 Q_D(KJob);
00273 bool should_emit = (d->totalAmount[unit] != amount);
00274
00275 d->totalAmount[unit] = amount;
00276
00277 if ( should_emit )
00278 {
00279 emit totalAmount(this, unit, amount);
00280 if (unit==d->progressUnit) {
00281 emit totalSize(this, amount);
00282 emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00283 }
00284 }
00285 }
00286
00287 void KJob::setPercent( unsigned long percentage )
00288 {
00289 Q_D(KJob);
00290 if ( d->percentage!=percentage )
00291 {
00292 d->percentage = percentage;
00293 emit percent( this, percentage );
00294 }
00295 }
00296
00297 void KJob::emitResult()
00298 {
00299 Q_D(KJob);
00300 d->isFinished = true;
00301
00302 emit finished( this );
00303
00304 emit result( this );
00305
00306 if ( isAutoDelete() )
00307 deleteLater();
00308 }
00309
00310 void KJob::emitPercent( qulonglong processedAmount, qulonglong totalAmount )
00311 {
00312 Q_D(KJob);
00313
00314 if (totalAmount) {
00315 unsigned long oldPercentage = d->percentage;
00316 d->percentage = (unsigned long)(( (float)(processedAmount) / (float)(totalAmount) ) * 100.0);
00317 if ( d->percentage != oldPercentage ) {
00318 emit percent( this, d->percentage );
00319 }
00320 }
00321 }
00322
00323 void KJob::emitSpeed(unsigned long value)
00324 {
00325 Q_D(KJob);
00326 if (!d->speedTimer) {
00327 d->speedTimer = new QTimer(this);
00328 connect(d->speedTimer, SIGNAL(timeout()), SLOT(_k_speedTimeout()));
00329 }
00330
00331 emit speed(this, value);
00332 d->speedTimer->start(5000);
00333 }
00334
00335 void KJobPrivate::_k_speedTimeout()
00336 {
00337 Q_Q(KJob);
00338
00339
00340 emit q->speed(q, 0);
00341 speedTimer->stop();
00342 }
00343
00344 bool KJob::isAutoDelete() const
00345 {
00346 Q_D(const KJob);
00347 return d->isAutoDelete;
00348 }
00349
00350 void KJob::setAutoDelete( bool autodelete )
00351 {
00352 Q_D(KJob);
00353 d->isAutoDelete = autodelete;
00354 }
00355
00356 #include "kjob.moc"