35#ifndef __QGPGME_THREADEDJOBMIXING_H__
36#define __QGPGME_THREADEDJOBMIXING_H__
39#include <QMutexLocker>
44#include <gpgme++/context.h>
45#include <gpgme++/interfaces/progressprovider.h>
57QString audit_log_as_html(GpgME::Context *ctx, GpgME::Error &err);
61 const QList<QByteArray> m_list;
62 mutable const char **m_patterns;
70 const char **patterns()
const;
75 QObject *
const m_object;
76 QThread *
const m_thread;
78 ToThreadMover(QObject *o, QThread *t) : m_object(o), m_thread(t) {}
79 ToThreadMover(QObject &o, QThread *t) : m_object(&o), m_thread(t) {}
80 ToThreadMover(
const std::shared_ptr<QObject> &o, QThread *t) : m_object(o.get()), m_thread(t) {}
83 if (m_object && m_thread) {
84 m_object->moveToThread(m_thread);
89template <
typename T_result>
93 explicit Thread(QObject *parent =
nullptr) : QThread(parent) {}
95 void setFunction(
const std::function<T_result()> &function)
97 const QMutexLocker locker(&m_mutex);
98 m_function = function;
103 const QMutexLocker locker(&m_mutex);
104 return static_cast<bool>(m_function);
107 T_result result()
const
109 const QMutexLocker locker(&m_mutex);
114 void run()
override {
115 const QMutexLocker locker(&m_mutex);
116 m_result = m_function();
119 mutable QMutex m_mutex;
120 std::function<T_result()> m_function;
124template <
typename T_base,
typename T_result = std::tuple<GpgME::Error, QString, GpgME::Error> >
129 typedef T_result result_type;
133 Q_ASSERT(m_thread.hasFunction() &&
"Call setWorkerFunction() before run()");
138 static_assert(std::tuple_size<T_result>::value > 2,
139 "Result tuple too small");
140 static_assert(std::is_same <
141 typename std::tuple_element <
142 std::tuple_size<T_result>::value - 2,
147 "Second to last result type not a QString");
148 static_assert(std::is_same <
149 typename std::tuple_element <
150 std::tuple_size<T_result>::value - 1,
155 "Last result type not a GpgME::Error");
158 : T_base(
nullptr), m_ctx(ctx), m_thread(), m_auditLog(), m_auditLogError()
162 void lateInitialization()
165 QObject::connect(&m_thread, &QThread::finished,
this,
166 &mixin_type::slotFinished);
167 m_ctx->setProgressProvider(
this);
168 QGpgME::g_context_map.insert(
this, m_ctx.get());
173 QGpgME::g_context_map.remove(
this);
176 template <
typename T_binder>
177 void setWorkerFunction(
const T_binder &func)
179 m_thread.setFunction([
this, func]() {
return func(this->context()); });
183 template <
typename T_binder>
184 void run(
const T_binder &func)
186 m_thread.setFunction(std::bind(func, this->context()));
189 template <
typename T_binder>
190 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io)
193 io->moveToThread(&m_thread);
199 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io)));
202 template <
typename T_binder>
203 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io1,
const std::shared_ptr<QIODevice> &io2)
206 io1->moveToThread(&m_thread);
209 io2->moveToThread(&m_thread);
215 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io1), std::weak_ptr<QIODevice>(io2)));
220 GpgME::Context *context()
const
225 virtual void resultHook(
const result_type &) {}
229 const T_result r = m_thread.result();
230 m_auditLog = std::get < std::tuple_size<T_result>::value - 2 > (r);
231 m_auditLogError = std::get < std::tuple_size<T_result>::value - 1 > (r);
237 void slotCancel()
override {
240 m_ctx->cancelPendingOperation();
243 QString auditLogAsHtml()
const override
247 GpgME::Error auditLogError()
const override
249 return m_auditLogError;
251 void showProgress(
const char *what,
252 int type,
int current,
int total)
override {
253 QMetaObject::invokeMethod(
this, [
this, current, total]() {
254 Q_EMIT this->jobProgress(current, total);
255 }, Qt::QueuedConnection);
256 const QString what_ = QString::fromUtf8(what);
257 QMetaObject::invokeMethod(
this, [
this, what_, type, current, total]() {
258 Q_EMIT this->rawProgress(what_, type, current, total);
259 }, Qt::QueuedConnection);
260 QMetaObject::invokeMethod(
this, [
this, what_, current, total]() {
262 QT_WARNING_DISABLE_DEPRECATED
263 Q_EMIT this->progress(what_, current, total);
265 }, Qt::QueuedConnection);
268 template <
typename T1,
typename T2>
269 void doEmitResult(
const std::tuple<T1, T2> &tuple)
271 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple));
274 template <
typename T1,
typename T2,
typename T3>
275 void doEmitResult(
const std::tuple<T1, T2, T3> &tuple)
277 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple));
280 template <
typename T1,
typename T2,
typename T3,
typename T4>
281 void doEmitResult(
const std::tuple<T1, T2, T3, T4> &tuple)
283 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple));
286 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
287 void doEmitResult(
const std::tuple<T1, T2, T3, T4, T5> &tuple)
289 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple), std::get<4>(tuple));
293 std::shared_ptr<GpgME::Context> m_ctx;
296 GpgME::Error m_auditLogError;
Definition: threadedjobmixin.h:60
Definition: threadedjobmixin.h:91
Definition: threadedjobmixin.h:126
Definition: threadedjobmixin.h:74