• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kjob.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
00003                        David Faure <faure@kde.org>
00004     Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
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     // Don't exit while this job is running
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     // Don't exit while this job is running
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             // If we are displaying a progress dialog, remove it first.
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     QEventLoop loop( this );
00190 
00191     connect( this, SIGNAL( result( KJob* ) ),
00192              &loop, SLOT( quit() ) );
00193     start();
00194     if( !d->isFinished ) {
00195         loop.exec();
00196     }
00197 
00198     return ( d->error == NoError );
00199 }
00200 
00201 int KJob::error() const
00202 {
00203     return d_func()->error;
00204 }
00205 
00206 QString KJob::errorText() const
00207 {
00208     return d_func()->errorText;
00209 }
00210 
00211 QString KJob::errorString() const
00212 {
00213     return d_func()->errorText;
00214 }
00215 
00216 qulonglong KJob::processedAmount(Unit unit) const
00217 {
00218     return d_func()->processedAmount[unit];
00219 }
00220 
00221 qulonglong KJob::totalAmount(Unit unit) const
00222 {
00223     return d_func()->totalAmount[unit];
00224 }
00225 
00226 unsigned long KJob::percent() const
00227 {
00228     return d_func()->percentage;
00229 }
00230 
00231 void KJob::setError( int errorCode )
00232 {
00233     Q_D(KJob);
00234     d->error = errorCode;
00235 }
00236 
00237 void KJob::setErrorText( const QString &errorText )
00238 {
00239     Q_D(KJob);
00240     d->errorText = errorText;
00241 }
00242 
00243 void KJob::setProcessedAmount(Unit unit, qulonglong amount)
00244 {
00245     Q_D(KJob);
00246     bool should_emit = (d->processedAmount[unit] != amount);
00247 
00248     d->processedAmount[unit] = amount;
00249 
00250     if ( should_emit )
00251     {
00252         emit processedAmount(this, unit, amount);
00253         if (unit==d->progressUnit) {
00254             emit processedSize(this, amount);
00255             emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00256         }
00257     }
00258 }
00259 
00260 void KJob::setTotalAmount(Unit unit, qulonglong amount)
00261 {
00262     Q_D(KJob);
00263     bool should_emit = (d->totalAmount[unit] != amount);
00264 
00265     d->totalAmount[unit] = amount;
00266 
00267     if ( should_emit )
00268     {
00269         emit totalAmount(this, unit, amount);
00270         if (unit==d->progressUnit) {
00271             emit totalSize(this, amount);
00272             emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00273         }
00274     }
00275 }
00276 
00277 void KJob::setPercent( unsigned long percentage )
00278 {
00279     Q_D(KJob);
00280     if ( d->percentage!=percentage )
00281     {
00282         d->percentage = percentage;
00283         emit percent( this, percentage );
00284     }
00285 }
00286 
00287 void KJob::emitResult()
00288 {
00289     Q_D(KJob);
00290     d->isFinished = true;
00291     // If we are displaying a progress dialog, remove it first.
00292     emit finished( this );
00293 
00294     emit result( this );
00295 
00296     if ( isAutoDelete() )
00297         deleteLater();
00298 }
00299 
00300 void KJob::emitPercent( qulonglong processedAmount, qulonglong totalAmount )
00301 {
00302     Q_D(KJob);
00303     // calculate percents
00304     if (totalAmount) {
00305         unsigned long oldPercentage = d->percentage;
00306         d->percentage = (unsigned long)(( (float)(processedAmount) / (float)(totalAmount) ) * 100.0);
00307         if ( d->percentage != oldPercentage ) {
00308             emit percent( this, d->percentage );
00309         }
00310     }
00311 }
00312 
00313 void KJob::emitSpeed(unsigned long value)
00314 {
00315     Q_D(KJob);
00316     if (!d->speedTimer) {
00317         d->speedTimer = new QTimer(this);
00318         connect(d->speedTimer, SIGNAL(timeout()), SLOT(_k_speedTimeout()));
00319     }
00320 
00321     emit speed(this, value);
00322     d->speedTimer->start(5000);   // 5 seconds interval should be enough
00323 }
00324 
00325 void KJobPrivate::_k_speedTimeout()
00326 {
00327     Q_Q(KJob);
00328     // send 0 and stop the timer
00329     // timer will be restarted only when we receive another speed event
00330     emit q->speed(q, 0);
00331     speedTimer->stop();
00332 }
00333 
00334 bool KJob::isAutoDelete() const
00335 {
00336     Q_D(const KJob);
00337     return d->isAutoDelete;
00338 }
00339 
00340 void KJob::setAutoDelete( bool autodelete )
00341 {
00342     Q_D(KJob);
00343     d->isAutoDelete = autodelete;
00344 }
00345 
00346 #include "kjob.moc"

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal