diff options
author | kh1 <karsten.heimrich@nokia.com> | 2012-02-14 22:10:24 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@nokia.com> | 2012-02-15 15:56:18 +0100 |
commit | 0be49ce19440387132b3a922c74748a9cfdc176a (patch) | |
tree | cea9fdc2ddfe3007081c1583af2376bb0ad1642d /installerbuilder | |
parent | 619885013462b075fca6237d54f9a4a0897b8b89 (diff) |
Extend and fix KDJob.
- Supersedes http://codereview.qt-project.org/#change,14386
- Extend the class with a timeout, once the timeout gets hit
the execution will be canceled.
- Fix delete later, as a subclass can emit finished without the
super class and than delete later wouldn't be called at all.
- We can't use a single shot timer here, as we would hit a possible
timeout once we use the class as stack object. Always restart the
timer. A value of -1 will stop the timeout timer.
Change-Id: Ib8aef07209557e1ad362759ce16930aebbd9bdf3
Reviewed-by: Tim Jenssen <tim.jenssen@nokia.com>
Diffstat (limited to 'installerbuilder')
-rw-r--r-- | installerbuilder/libinstaller/3rdparty/kdtools/kdjob.cpp | 89 | ||||
-rw-r--r-- | installerbuilder/libinstaller/3rdparty/kdtools/kdjob.h | 33 |
2 files changed, 94 insertions, 28 deletions
diff --git a/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.cpp b/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.cpp index 4038d4344..a6b423dd7 100644 --- a/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.cpp +++ b/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.cpp @@ -22,16 +22,34 @@ #include "kdjob.h" -#include <QEventLoop> +#include <QtCore/QDebug> +#include <QtCore/QEventLoop> +#include <QtCore/QTimer> + + +// -- KDJob::Private class KDJob::Private { KDJob *const q; + public: explicit Private(KDJob *qq) - : q(qq), error(KDJob::NoError), caps(KDJob::NoCapabilities), autoDelete(true), - totalAmount(100), processedAmount(0) - {} + : q(qq) + , error(KDJob::NoError) + , caps(KDJob::NoCapabilities) + , autoDelete(true) + , totalAmount(100) + , processedAmount(0) + , m_timeout(-1) + { + connect(&m_timer, SIGNAL(timeout()), q, SLOT(cancel())); + } + + ~Private() + { + m_timer.stop(); + } void delayedStart() { @@ -43,21 +61,34 @@ public: { QEventLoop loop; q->connect(q, sig, &loop, SLOT(quit())); + + if (m_timeout >= 0) + m_timer.start(m_timeout); + else + m_timer.stop(); + loop.exec(); } int error; QString errorString; KDJob::Capabilities caps; - bool autoDelete : 1; + bool autoDelete; quint64 totalAmount; quint64 processedAmount; + int m_timeout; + QTimer m_timer; }; + +// -- KDJob + KDJob::KDJob(QObject *parent) : QObject(parent), d(new Private(this)) -{} +{ + connect(this, SIGNAL(finished(KDJob*)), this, SLOT(onFinished())); +} KDJob::~KDJob() { @@ -87,8 +118,6 @@ QString KDJob::errorString() const void KDJob::emitFinished() { emit finished(this); - if (d->autoDelete) - deleteLater(); } void KDJob::emitFinishedWithError(int error, const QString &errorString) @@ -138,14 +167,18 @@ void KDJob::start() QMetaObject::invokeMethod(this, "delayedStart", Qt::QueuedConnection); } -void KDJob::doCancel() -{ -} - void KDJob::cancel() { - doCancel(); - setError(Canceled); + if (d->caps & Cancelable) { + doCancel(); + if (error() == NoError) { + setError(Canceled); + setErrorString(tr("Canceled")); + } + emitFinished(); + } else { + qDebug() << "The current job can not be canceled, missing \"Cancelable\" capability!"; + } } quint64 KDJob::totalAmount() const @@ -153,7 +186,8 @@ quint64 KDJob::totalAmount() const return d->totalAmount; } -quint64 KDJob::processedAmount() const { +quint64 KDJob::processedAmount() const +{ return d->processedAmount; } @@ -165,6 +199,25 @@ void KDJob::setTotalAmount(quint64 amount) emit progress(this, d->processedAmount, d->totalAmount); } +/*! + Returns the timeout in milliseconds before the jobs cancel slot gets triggered. A return value of -1 + means there is currently no timeout used for the job. +*/ +int KDJob::timeout() const +{ + return d->m_timeout; +} + +/*! + Sets the timeout in \a milliseconds before the jobs cancel slot gets triggered. \note Only jobs that + have the \bold KDJob::Cancelable capability can be canceled by an timeout. A value of -1 will stop the + timeout mechanism. +*/ +void KDJob::setTimeout(int milliseconds) +{ + d->m_timeout = milliseconds; +} + void KDJob::setProcessedAmount(quint64 amount) { if (d->processedAmount == amount) @@ -173,4 +226,10 @@ void KDJob::setProcessedAmount(quint64 amount) emit progress(this, d->processedAmount, d->totalAmount); } +void KDJob::onFinished() +{ + if (d->autoDelete) + deleteLater(); +} + #include "moc_kdjob.cpp" diff --git a/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.h b/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.h index 0421b2112..37f441526 100644 --- a/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.h +++ b/installerbuilder/libinstaller/3rdparty/kdtools/kdjob.h @@ -25,11 +25,14 @@ #include "kdtoolsglobal.h" -#include <QObject> +#include <QtCore/QObject> class KDTOOLS_EXPORT KDJob : public QObject { Q_OBJECT + class Private; + + Q_PROPERTY(int timeout READ timeout WRITE setTimeout) Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete) public: @@ -37,14 +40,14 @@ public: ~KDJob(); enum Error { - NoError=0, - Canceled=1, - UserDefinedError=128 + NoError = 0, + Canceled = 1, + UserDefinedError = 128 }; enum Capability { - NoCapabilities=0x0, - Cancelable=0x1 + NoCapabilities = 0x0, + Cancelable = 0x1 }; Q_DECLARE_FLAGS(Capabilities, Capability) @@ -64,23 +67,26 @@ public: quint64 totalAmount() const; quint64 processedAmount() const; + int timeout() const; + void setTimeout(int milliseconds); + public Q_SLOTS: void start(); void cancel(); Q_SIGNALS: - void infoMessage(KDJob *, const QString &); + void started(KDJob *job); + void finished(KDJob *job); -Q_SIGNALS: - void started(KDJob *); - void finished(KDJob *); + void infoMessage(KDJob *job, const QString &message); void progress(KDJob *job, quint64 processed, quint64 total); protected: virtual void doStart() = 0; - virtual void doCancel(); + virtual void doCancel() = 0; void setCapabilities(Capabilities c); + void setTotalAmount(quint64 amount); void setProcessedAmount(quint64 amount); @@ -90,9 +96,10 @@ protected: void emitFinished(); void emitFinishedWithError(int error, const QString &errorString); +private Q_SLOTS: + void onFinished(); + private: - class Private; - friend class ::KDJob::Private; Private *d; Q_PRIVATE_SLOT(d, void delayedStart()) }; |