35 #ifndef __QGPGME_THREADEDJOBMIXING_H__
36 #define __QGPGME_THREADEDJOBMIXING_H__
39 #include <QMutexLocker>
44 #ifdef BUILDING_QGPGME
46 # include "interfaces/progressprovider.h"
48 # include <gpgme++/context.h>
49 # include <gpgme++/interfaces/progressprovider.h>
62 QString 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);
94 template <
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;
106 T_result result()
const
108 const QMutexLocker locker(&m_mutex);
113 void run() Q_DECL_OVERRIDE {
114 const QMutexLocker locker(&m_mutex);
115 m_result = m_function();
118 mutable QMutex m_mutex;
119 std::function<T_result()> m_function;
123 template <
typename T_base,
typename T_result = std::tuple<GpgME::Error, QString, GpgME::Error> >
128 typedef T_result result_type;
131 static_assert(std::tuple_size<T_result>::value > 2,
132 "Result tuple too small");
133 static_assert(std::is_same <
134 typename std::tuple_element <
135 std::tuple_size<T_result>::value - 2,
140 "Second to last result type not a QString");
141 static_assert(std::is_same <
142 typename std::tuple_element <
143 std::tuple_size<T_result>::value - 1,
148 "Last result type not a GpgME::Error");
151 : T_base(
nullptr), m_ctx(ctx), m_thread(), m_auditLog(), m_auditLogError()
155 void lateInitialization()
158 QObject::connect(&m_thread, &QThread::finished,
this,
159 &mixin_type::slotFinished);
160 m_ctx->setProgressProvider(
this);
161 QGpgME::g_context_map.insert(
this, m_ctx.get());
166 QGpgME::g_context_map.remove(
this);
169 template <
typename T_binder>
170 void run(
const T_binder &func)
172 m_thread.setFunction(std::bind(func, this->context()));
175 template <
typename T_binder>
176 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io)
179 io->moveToThread(&m_thread);
185 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io)));
188 template <
typename T_binder>
189 void run(
const T_binder &func,
const std::shared_ptr<QIODevice> &io1,
const std::shared_ptr<QIODevice> &io2)
192 io1->moveToThread(&m_thread);
195 io2->moveToThread(&m_thread);
201 m_thread.setFunction(std::bind(func, this->context(), this->thread(), std::weak_ptr<QIODevice>(io1), std::weak_ptr<QIODevice>(io2)));
204 GpgME::Context *context()
const
209 virtual void resultHook(
const result_type &) {}
213 const T_result r = m_thread.result();
214 m_auditLog = std::get < std::tuple_size<T_result>::value - 2 > (r);
215 m_auditLogError = std::get < std::tuple_size<T_result>::value - 1 > (r);
221 void slotCancel() Q_DECL_OVERRIDE {
224 m_ctx->cancelPendingOperation();
227 QString auditLogAsHtml()
const Q_DECL_OVERRIDE
231 GpgME::Error auditLogError()
const Q_DECL_OVERRIDE
233 return m_auditLogError;
235 void showProgress(
const char * ,
236 int ,
int current,
int total) Q_DECL_OVERRIDE {
241 QMetaObject::invokeMethod(
this,
"progress", Qt::QueuedConnection,
243 Q_ARG(QString, QString()),
248 template <
typename T1,
typename T2>
249 void doEmitResult(
const std::tuple<T1, T2> &tuple)
251 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple));
254 template <
typename T1,
typename T2,
typename T3>
255 void doEmitResult(
const std::tuple<T1, T2, T3> &tuple)
257 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple));
260 template <
typename T1,
typename T2,
typename T3,
typename T4>
261 void doEmitResult(
const std::tuple<T1, T2, T3, T4> &tuple)
263 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple));
266 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
267 void doEmitResult(
const std::tuple<T1, T2, T3, T4, T5> &tuple)
269 Q_EMIT this->result(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple), std::get<3>(tuple), std::get<4>(tuple));
273 std::shared_ptr<GpgME::Context> m_ctx;
276 GpgME::Error m_auditLogError;