35#ifndef __QGPGME_THREADEDJOBMIXING_H__
36#define __QGPGME_THREADEDJOBMIXING_H__
39#include <QMutexLocker>
46# include "interfaces/progressprovider.h"
48# include <gpgme++/context.h>
49# include <gpgme++/interfaces/progressprovider.h>
62QString audit_log_as_html(GpgME::Context *ctx, GpgME::Error &err);
66 const QList<QByteArray> m_list;
67 mutable const char **m_patterns;
75 const char **patterns()
const;
80 QObject *
const m_object;
81 QThread *
const m_thread;
83 ToThreadMover(QObject *o, QThread *t) : m_object(o), m_thread(t) {}
84 ToThreadMover(QObject &o, QThread *t) : m_object(&o), m_thread(t) {}
85 ToThreadMover(
const std::shared_ptr<QObject> &o, QThread *t) : m_object(o.get()), m_thread(t) {}
88 if (m_object && m_thread) {
89 m_object->moveToThread(m_thread);
94template <
typename T_result>
98 explicit Thread(QObject *parent = Q_NULLPTR) : QThread(parent) {}
100 void setFunction(
const std::function<T_result()> &function)
102 const QMutexLocker locker(&m_mutex);
103 m_function = function;
108 const QMutexLocker locker(&m_mutex);
109 return static_cast<bool>(m_function);
112 T_result result()
const
114 const QMutexLocker locker(&m_mutex);
119 void run()
override {
120 const QMutexLocker locker(&m_mutex);
121 m_result = m_function();
124 mutable QMutex m_mutex;
125 std::function<T_result()> m_function;
129template <
typename T_base,
typename T_result = std::tuple<GpgME::Error, QString, GpgME::Error> >
134 typedef T_result result_type;
138 Q_ASSERT(m_thread.hasFunction() &&
"Call setWorkerFunction() before run()");
143 static_assert(std::tuple_size<T_result>::value > 2,
144 "Result tuple too small");
145 static_assert(std::is_same <
146 typename std::tuple_element <
147 std::tuple_size<T_result>::value - 2,
152 "Second to last result type not a QString");
153 static_assert(std::is_same <
154 typename std::tuple_element <
155 std::tuple_size<T_result>::value - 1,
160 "Last result type not a GpgME::Error");
163 : T_base(
nullptr), m_ctx(ctx), m_thread(), m_auditLog(), m_auditLogError()
167 void lateInitialization()
170 QObject::connect(&m_thread, &QThread::finished,
this,
171 &mixin_type::slotFinished);
172 m_ctx->setProgressProvider(
this);
173 QGpgME::g_context_map.insert(
this, m_ctx.get());
178 QGpgME::g_context_map.remove(
this);
181 template <
typename T_binder>
182 void setWorkerFunction(
const T_binder &func)
184 m_thread.setFunction([
this, func]() {
return func(this->context()); });
188 template <
typename T_binder>
189 void run(
const T_binder &func)
191 m_thread.setFunction(std::bind(func, this->context()));
194 template <
typename T_binder>
195 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io)
198 io->moveToThread(&m_thread);
204 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io)));
207 template <
typename T_binder>
208 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io1,
const std::shared_ptr<QIODevice> &io2)
211 io1->moveToThread(&m_thread);
214 io2->moveToThread(&m_thread);
220 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io1), std::weak_ptr<QIODevice>(io2)));
225 GpgME::Context *context()
const
230 virtual void resultHook(
const result_type &) {}
234 const T_result r = m_thread.result();
235 m_auditLog = std::get < std::tuple_size<T_result>::value - 2 > (r);
236 m_auditLogError = std::get < std::tuple_size<T_result>::value - 1 > (r);
242 void slotCancel()
override {
245 m_ctx->cancelPendingOperation();
248 QString auditLogAsHtml()
const override
252 GpgME::Error auditLogError()
const override
254 return m_auditLogError;
256 void showProgress(
const char *what,
257 int type,
int current,
int total)
override {
258 QMetaObject::invokeMethod(
this, [
this, current, total]() {
259 Q_EMIT this->jobProgress(current, total);
260 }, Qt::QueuedConnection);
261 const QString what_ = QString::fromUtf8(what);
262 QMetaObject::invokeMethod(
this, [
this, what_, type, current, total]() {
263 Q_EMIT this->rawProgress(what_, type, current, total);
264 }, Qt::QueuedConnection);
265 QMetaObject::invokeMethod(
this, [
this, what_, current, total]() {
267 QT_WARNING_DISABLE_DEPRECATED
268 Q_EMIT this->progress(what_, current, total);
270 }, Qt::QueuedConnection);
273 template <
typename T1,
typename T2>
274 void doEmitResult(
const std::tuple<T1, T2> &tuple)
276 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple));
279 template <
typename T1,
typename T2,
typename T3>
280 void doEmitResult(
const std::tuple<T1, T2, T3> &tuple)
282 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple));
285 template <
typename T1,
typename T2,
typename T3,
typename T4>
286 void doEmitResult(
const std::tuple<T1, T2, T3, T4> &tuple)
288 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple));
291 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
292 void doEmitResult(
const std::tuple<T1, T2, T3, T4, T5> &tuple)
294 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple), std::get<4>(tuple));
298 std::shared_ptr<GpgME::Context> m_ctx;
301 GpgME::Error m_auditLogError;
Definition: threadedjobmixin.h:65
Definition: threadedjobmixin.h:96
Definition: threadedjobmixin.h:131
Definition: threadedjobmixin.h:79