From 18c2c0b2509069951829bb0bdd9b39dc467f5e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Tue, 3 Jan 2023 12:29:48 +0100 Subject: [PATCH] qt: Support deferred start of jobs * lang/qt/src/job.cpp, lang/qt/src/job.h (Job::startNow): New method. * lang/qt/src/job_p.h (JobPrivate::start): New pure virtual method. * lang/qt/src/qgpgmechangeexpiryjob.cpp (QGpgMEChangeExpiryJobPrivate::start): New. * lang/qt/src/qgpgmeencryptjob.cpp (QGpgMEEncryptJobPrivate::start): New. * lang/qt/src/qgpgmeimportjob.cpp (QGpgMEImportJobPrivate::start): New. * lang/qt/src/qgpgmelistallkeysjob.cpp (QGpgMEListAllKeysJobPrivate::start): New. * lang/qt/src/qgpgmesignencryptjob.cpp (QGpgMESignEncryptJobPrivate::start): New. * lang/qt/src/threadedjobmixin.h (Thread::hasFunction): New method. (ThreadedJobMixin::run, ThreadedJobMixin::setWorkerFunction): New methods. -- startNow() starts a deferred job for which the worker function has been set before. GnuPG-bug-id: 6323 --- NEWS | 1 + lang/qt/src/job.cpp | 7 +++++++ lang/qt/src/job.h | 7 +++++++ lang/qt/src/job_p.h | 2 ++ lang/qt/src/qgpgmechangeexpiryjob.cpp | 6 ++++++ lang/qt/src/qgpgmeencryptjob.cpp | 6 ++++++ lang/qt/src/qgpgmeimportjob.cpp | 6 ++++++ lang/qt/src/qgpgmelistallkeysjob.cpp | 6 ++++++ lang/qt/src/qgpgmesignencryptjob.cpp | 6 ++++++ lang/qt/src/threadedjobmixin.h | 18 ++++++++++++++++++ 10 files changed, 65 insertions(+) diff --git a/NEWS b/NEWS index c5f457a3..1094b77a 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,7 @@ Noteworthy changes in version 1.18.1 (unreleased) qt: ListAllKeysJob::Options NEW. qt: ListAllKeysJob::setOptions NEW. qt: ListAllKeysJob::options NEW. + qt: Job::startNow NEW. Noteworthy changes in version 1.18.0 (2022-08-10) diff --git a/lang/qt/src/job.cpp b/lang/qt/src/job.cpp index 98f408b4..85de2ce7 100644 --- a/lang/qt/src/job.cpp +++ b/lang/qt/src/job.cpp @@ -137,6 +137,13 @@ GpgME::Context *QGpgME::Job::context(QGpgME::Job *job) return QGpgME::g_context_map.value (job, nullptr); } +void QGpgME::Job::startNow() +{ + auto d = getJobPrivate(this); + Q_ASSERT(d && "This Job class has no JobPrivate class"); + d->start(); +} + #define make_job_subclass_ext(x,y) \ QGpgME::x::x( QObject * parent ) : y( parent ) {} \ QGpgME::x::~x() {} diff --git a/lang/qt/src/job.h b/lang/qt/src/job.h index dc383600..8b974b4f 100644 --- a/lang/qt/src/job.h +++ b/lang/qt/src/job.h @@ -95,6 +95,13 @@ public: */ static GpgME::Context *context(Job *job); + /** Starts a deferred job. + * + * The job needs to have been prepared for a deferred start by calling the + * startLater() function instead of the start() function of a leaf class. + */ + void startNow(); + public Q_SLOTS: virtual void slotCancel() = 0; diff --git a/lang/qt/src/job_p.h b/lang/qt/src/job_p.h index 5a77b75b..ff99d6ea 100644 --- a/lang/qt/src/job_p.h +++ b/lang/qt/src/job_p.h @@ -46,6 +46,8 @@ class JobPrivate { public: virtual ~JobPrivate() {} + + virtual void start() = 0; }; // Setter and getters for the externally stored pimpl instances of jobs diff --git a/lang/qt/src/qgpgmechangeexpiryjob.cpp b/lang/qt/src/qgpgmechangeexpiryjob.cpp index 6bffbc87..4d928bd9 100644 --- a/lang/qt/src/qgpgmechangeexpiryjob.cpp +++ b/lang/qt/src/qgpgmechangeexpiryjob.cpp @@ -64,6 +64,12 @@ public: } ~QGpgMEChangeExpiryJobPrivate() override = default; + +private: + void start() override + { + q->run(); + } }; } diff --git a/lang/qt/src/qgpgmeencryptjob.cpp b/lang/qt/src/qgpgmeencryptjob.cpp index ae33db1d..54066fd0 100644 --- a/lang/qt/src/qgpgmeencryptjob.cpp +++ b/lang/qt/src/qgpgmeencryptjob.cpp @@ -70,6 +70,12 @@ public: } ~QGpgMEEncryptJobPrivate() override = default; + +private: + void start() override + { + q->run(); + } }; } diff --git a/lang/qt/src/qgpgmeimportjob.cpp b/lang/qt/src/qgpgmeimportjob.cpp index e3fd251b..2da54582 100644 --- a/lang/qt/src/qgpgmeimportjob.cpp +++ b/lang/qt/src/qgpgmeimportjob.cpp @@ -65,6 +65,12 @@ public: } ~QGpgMEImportJobPrivate() override = default; + +private: + void start() override + { + q->run(); + } }; } diff --git a/lang/qt/src/qgpgmelistallkeysjob.cpp b/lang/qt/src/qgpgmelistallkeysjob.cpp index 4bd7d8ba..78ffaded 100644 --- a/lang/qt/src/qgpgmelistallkeysjob.cpp +++ b/lang/qt/src/qgpgmelistallkeysjob.cpp @@ -75,6 +75,12 @@ public: } ~QGpgMEListAllKeysJobPrivate() override = default; + +private: + void start() override + { + q->run(); + } }; } diff --git a/lang/qt/src/qgpgmesignencryptjob.cpp b/lang/qt/src/qgpgmesignencryptjob.cpp index 4a1082a9..cc331ed5 100644 --- a/lang/qt/src/qgpgmesignencryptjob.cpp +++ b/lang/qt/src/qgpgmesignencryptjob.cpp @@ -71,6 +71,12 @@ public: } ~QGpgMESignEncryptJobPrivate() override = default; + +private: + void start() override + { + q->run(); + } }; } diff --git a/lang/qt/src/threadedjobmixin.h b/lang/qt/src/threadedjobmixin.h index ef30ab36..f50ac43d 100644 --- a/lang/qt/src/threadedjobmixin.h +++ b/lang/qt/src/threadedjobmixin.h @@ -103,6 +103,12 @@ public: m_function = function; } + bool hasFunction() + { + const QMutexLocker locker(&m_mutex); + return static_cast(m_function); + } + T_result result() const { const QMutexLocker locker(&m_mutex); @@ -127,6 +133,12 @@ public: typedef ThreadedJobMixin mixin_type; typedef T_result result_type; + void run() + { + Q_ASSERT(m_thread.hasFunction() && "Call setWorkerFunction() before run()"); + m_thread.start(); + } + protected: static_assert(std::tuple_size::value > 2, "Result tuple too small"); @@ -166,6 +178,12 @@ protected: QGpgME::g_context_map.remove(this); } + template + void setWorkerFunction(const T_binder &func) + { + m_thread.setFunction([this, func]() { return func(this->context()); }); + } + template void run(const T_binder &func) {