• Skip to content
  • Skip to link menu
KDE 4.1 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)
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     loop.exec();
00195 
00196     return ( d->error == NoError );
00197 }
00198 
00199 int KJob::error() const
00200 {
00201     return d_func()->error;
00202 }
00203 
00204 QString KJob::errorText() const
00205 {
00206     return d_func()->errorText;
00207 }
00208 
00209 QString KJob::errorString() const
00210 {
00211     return d_func()->errorText;
00212 }
00213 
00214 qulonglong KJob::processedAmount(Unit unit) const
00215 {
00216     return d_func()->processedAmount[unit];
00217 }
00218 
00219 qulonglong KJob::totalAmount(Unit unit) const
00220 {
00221     return d_func()->totalAmount[unit];
00222 }
00223 
00224 unsigned long KJob::percent() const
00225 {
00226     return d_func()->percentage;
00227 }
00228 
00229 void KJob::setError( int errorCode )
00230 {
00231     Q_D(KJob);
00232     d->error = errorCode;
00233 }
00234 
00235 void KJob::setErrorText( const QString &errorText )
00236 {
00237     Q_D(KJob);
00238     d->errorText = errorText;
00239 }
00240 
00241 void KJob::setProcessedAmount(Unit unit, qulonglong amount)
00242 {
00243     Q_D(KJob);
00244     bool should_emit = (d->processedAmount[unit] != amount);
00245 
00246     d->processedAmount[unit] = amount;
00247 
00248     if ( should_emit )
00249     {
00250         emit processedAmount(this, unit, amount);
00251         if (unit==d->progressUnit) {
00252             emit processedSize(this, amount);
00253             emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00254         }
00255     }
00256 }
00257 
00258 void KJob::setTotalAmount(Unit unit, qulonglong amount)
00259 {
00260     Q_D(KJob);
00261     bool should_emit = (d->totalAmount[unit] != amount);
00262 
00263     d->totalAmount[unit] = amount;
00264 
00265     if ( should_emit )
00266     {
00267         emit totalAmount(this, unit, amount);
00268         if (unit==d->progressUnit) {
00269             emit totalSize(this, amount);
00270             emitPercent(d->processedAmount[unit], d->totalAmount[unit]);
00271         }
00272     }
00273 }
00274 
00275 void KJob::setPercent( unsigned long percentage )
00276 {
00277     Q_D(KJob);
00278     if ( d->percentage!=percentage )
00279     {
00280         d->percentage = percentage;
00281         emit percent( this, percentage );
00282     }
00283 }
00284 
00285 void KJob::emitResult()
00286 {
00287     // If we are displaying a progress dialog, remove it first.
00288     emit finished( this );
00289 
00290     emit result( this );
00291 
00292     if ( isAutoDelete() )
00293         deleteLater();
00294 }
00295 
00296 void KJob::emitPercent( qulonglong processedAmount, qulonglong totalAmount )
00297 {
00298     Q_D(KJob);
00299     // calculate percents
00300     if (totalAmount) {
00301         unsigned long oldPercentage = d->percentage;
00302         d->percentage = (unsigned long)(( (float)(processedAmount) / (float)(totalAmount) ) * 100.0);
00303         if ( d->percentage != oldPercentage ) {
00304             emit percent( this, d->percentage );
00305         }
00306     }
00307 }
00308 
00309 void KJob::emitSpeed(unsigned long value)
00310 {
00311     Q_D(KJob);
00312     if (!d->speedTimer) {
00313         d->speedTimer = new QTimer(this);
00314         connect(d->speedTimer, SIGNAL(timeout()), SLOT(_k_speedTimeout()));
00315     }
00316 
00317     emit speed(this, value);
00318     d->speedTimer->start(5000);   // 5 seconds interval should be enough
00319 }
00320 
00321 void KJobPrivate::_k_speedTimeout()
00322 {
00323     Q_Q(KJob);
00324     // send 0 and stop the timer
00325     // timer will be restarted only when we receive another speed event
00326     emit q->speed(q, 0);
00327     speedTimer->stop();
00328 }
00329 
00330 bool KJob::isAutoDelete() const
00331 {
00332     Q_D(const KJob);
00333     return d->isAutoDelete;
00334 }
00335 
00336 void KJob::setAutoDelete( bool autodelete )
00337 {
00338     Q_D(KJob);
00339     d->isAutoDelete = autodelete;
00340 }
00341 
00342 #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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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