• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkleo

  • sources
  • kde-4.14
  • kdepim
  • libkleo
  • backends
  • qgpgme
threadedjobmixin.h
Go to the documentation of this file.
1 /*
2  threadedjobmixin.h
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #ifndef __KLEO_THREADEDJOBMIXING_H__
34 #define __KLEO_THREADEDJOBMIXING_H__
35 
36 #include "qgpgmeprogresstokenmapper.h"
37 
38 #include <QMutex>
39 #include <QMutexLocker>
40 #include <QThread>
41 #include <QString>
42 #include <QIODevice>
43 
44 #include <gpgme++/context.h>
45 #include <gpgme++/interfaces/progressprovider.h>
46 
47 #ifndef Q_MOC_RUN
48 #include <boost/shared_ptr.hpp>
49 #include <boost/weak_ptr.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/function.hpp>
52 #include <boost/tuple/tuple.hpp>
53 #include <boost/utility/enable_if.hpp>
54 #include <boost/type_traits/is_same.hpp>
55 #endif
56 
57 #include <cassert>
58 
59 namespace Kleo {
60 namespace _detail {
61 
62  QString audit_log_as_html( GpgME::Context * ctx, GpgME::Error & err );
63 
64  class PatternConverter {
65  const QList<QByteArray> m_list;
66  mutable const char ** m_patterns;
67  public:
68  explicit PatternConverter( const QByteArray & ba );
69  explicit PatternConverter( const QString & s );
70  explicit PatternConverter( const QList<QByteArray> & lba );
71  explicit PatternConverter( const QStringList & sl );
72  ~PatternConverter();
73 
74  const char ** patterns() const;
75  };
76 
77  class ToThreadMover {
78  QObject * const m_object;
79  QThread * const m_thread;
80  public:
81  ToThreadMover( QObject * o, QThread * t ) : m_object( o ), m_thread( t ) {}
82  ToThreadMover( QObject & o, QThread * t ) : m_object( &o ), m_thread( t ) {}
83  ToThreadMover( const boost::shared_ptr<QObject> & o, QThread * t ) : m_object( o.get() ), m_thread( t ) {}
84  ~ToThreadMover() { if ( m_object && m_thread ) m_object->moveToThread( m_thread ); }
85  };
86 
87  template <typename T_result>
88  class Thread : public QThread {
89  public:
90  explicit Thread( QObject * parent=0 ) : QThread( parent ) {}
91 
92  void setFunction( const boost::function<T_result()> & function ) {
93  const QMutexLocker locker( &m_mutex );
94  m_function = function;
95  }
96 
97  T_result result() const {
98  const QMutexLocker locker( &m_mutex );
99  return m_result;
100  }
101 
102  private:
103  /* reimp */ void run() {
104  const QMutexLocker locker( &m_mutex );
105  m_result = m_function();
106  }
107  private:
108  mutable QMutex m_mutex;
109  boost::function<T_result()> m_function;
110  T_result m_result;
111  };
112 
113  template <typename T_base, typename T_result=boost::tuple<GpgME::Error,QString,GpgME::Error> >
114  class ThreadedJobMixin : public T_base, public GpgME::ProgressProvider {
115  public:
116  typedef ThreadedJobMixin<T_base, T_result> mixin_type;
117  typedef T_result result_type;
118 
119  protected:
120  BOOST_STATIC_ASSERT(( boost::tuples::length<T_result>::value > 2 ));
121  BOOST_STATIC_ASSERT((
122  boost::is_same<
123  typename boost::tuples::element<
124  boost::tuples::length<T_result>::value - 2,
125  T_result
126  >::type,
127  QString
128  >::value
129  ));
130  BOOST_STATIC_ASSERT((
131  boost::is_same<
132  typename boost::tuples::element<
133  boost::tuples::length<T_result>::value - 1,
134  T_result
135  >::type,
136  GpgME::Error
137  >::value
138  ));
139 
140  explicit ThreadedJobMixin( GpgME::Context * ctx )
141  : T_base( 0 ), m_ctx( ctx ), m_thread(), m_auditLog(), m_auditLogError()
142  {
143 
144  }
145 
146  void lateInitialization() {
147  assert( m_ctx );
148  QObject::connect( &m_thread, SIGNAL(finished()), this, SLOT(slotFinished()) );
149  m_ctx->setProgressProvider( this );
150  }
151 
152  template <typename T_binder>
153  void run( const T_binder & func ) {
154  m_thread.setFunction( boost::bind( func, this->context() ) );
155  m_thread.start();
156  }
157  template <typename T_binder>
158  void run( const T_binder & func, const boost::shared_ptr<QIODevice> & io ) {
159  if ( io ) io->moveToThread( &m_thread );
160  // the arguments passed here to the functor are stored in a QThread, and are not
161  // necessarily destroyed (living outside the UI thread) at the time the result signal
162  // is emitted and the signal receiver wants to clean up IO devices.
163  // To avoid such races, we pass weak_ptr's to the functor.
164  m_thread.setFunction( boost::bind( func, this->context(), this->thread(), boost::weak_ptr<QIODevice>( io ) ) );
165  m_thread.start();
166  }
167  template <typename T_binder>
168  void run( const T_binder & func, const boost::shared_ptr<QIODevice> & io1, const boost::shared_ptr<QIODevice> & io2 ) {
169  if ( io1 ) io1->moveToThread( &m_thread );
170  if ( io2 ) io2->moveToThread( &m_thread );
171  // the arguments passed here to the functor are stored in a QThread, and are not
172  // necessarily destroyed (living outside the UI thread) at the time the result signal
173  // is emitted and the signal receiver wants to clean up IO devices.
174  // To avoid such races, we pass weak_ptr's to the functor.
175  m_thread.setFunction( boost::bind( func, this->context(), this->thread(), boost::weak_ptr<QIODevice>( io1 ), boost::weak_ptr<QIODevice>( io2 ) ) );
176  m_thread.start();
177  }
178  GpgME::Context * context() const { return m_ctx.get(); }
179 
180  virtual void resultHook( const result_type & ) {}
181 
182  void slotFinished() {
183  const T_result r = m_thread.result();
184  m_auditLog = boost::get<boost::tuples::length<T_result>::value-2>( r );
185  m_auditLogError = boost::get<boost::tuples::length<T_result>::value-1>( r );
186  resultHook( r );
187  emit this->done();
188  doEmitResult( r );
189  this->deleteLater();
190  }
191  /* reimp */ void slotCancel() {
192  if ( m_ctx ) m_ctx->cancelPendingOperation();
193  }
194  /* reimp */ QString auditLogAsHtml() const { return m_auditLog; }
195  /* reimp */ GpgME::Error auditLogError() const { return m_auditLogError; }
196  /* reimp */ void showProgress( const char * what, int type, int current, int total ) {
197  // will be called from the thread exec'ing the operation, so
198  // just bounce everything to the owning thread:
199  // ### hope this is thread-safe (meta obj is const, and
200  // ### portEvent is thread-safe, so should be ok)
201  QMetaObject::invokeMethod( this, "progress", Qt::QueuedConnection,
202  Q_ARG( QString, QGpgMEProgressTokenMapper::map( what, type ) ),
203  Q_ARG( int, current ),
204  Q_ARG( int, total ) );
205  }
206  private:
207  template <typename T1, typename T2>
208  void doEmitResult( const boost::tuple<T1,T2> & tuple ) {
209  emit this->result( boost::get<0>( tuple ), boost::get<1>( tuple ) );
210  }
211 
212  template <typename T1, typename T2, typename T3>
213  void doEmitResult( const boost::tuple<T1,T2,T3> & tuple ) {
214  emit this->result( boost::get<0>( tuple ), boost::get<1>( tuple ), boost::get<2>( tuple ) );
215  }
216 
217  template <typename T1, typename T2, typename T3, typename T4>
218  void doEmitResult( const boost::tuple<T1,T2,T3,T4> & tuple ) {
219  emit this->result( boost::get<0>( tuple ), boost::get<1>( tuple ), boost::get<2>( tuple ), boost::get<3>( tuple ) );
220  }
221 
222  template <typename T1, typename T2, typename T3, typename T4, typename T5>
223  void doEmitResult( const boost::tuple<T1,T2,T3,T4,T5> & tuple ) {
224  emit this->result( boost::get<0>( tuple ), boost::get<1>( tuple ), boost::get<2>( tuple ), boost::get<3>( tuple ), boost::get<4>( tuple ) );
225  }
226 
227  private:
228  boost::shared_ptr<GpgME::Context> m_ctx;
229  Thread<T_result> m_thread;
230  QString m_auditLog;
231  GpgME::Error m_auditLogError;
232  };
233 
234 }
235 }
236 
237 #endif /* __KLEO_THREADEDJOBMIXING_H__ */
238 
QMutex
Kleo::_detail::Thread::Thread
Thread(QObject *parent=0)
Definition: threadedjobmixin.h:90
Kleo::_detail::ThreadedJobMixin::run
void run(const T_binder &func, const boost::shared_ptr< QIODevice > &io1, const boost::shared_ptr< QIODevice > &io2)
Definition: threadedjobmixin.h:168
Kleo::_detail::PatternConverter
Definition: threadedjobmixin.h:64
QByteArray
Kleo::_detail::ThreadedJobMixin::context
GpgME::Context * context() const
Definition: threadedjobmixin.h:178
QObject::moveToThread
void moveToThread(QThread *targetThread)
Kleo::_detail::ThreadedJobMixin::slotCancel
void slotCancel()
Definition: threadedjobmixin.h:191
Kleo::_detail::Thread::result
T_result result() const
Definition: threadedjobmixin.h:97
Kleo::_detail::ThreadedJobMixin::ThreadedJobMixin
ThreadedJobMixin(GpgME::Context *ctx)
Definition: threadedjobmixin.h:140
Kleo::QGpgMEProgressTokenMapper::map
QString map(const char *token, int subtoken)
Definition: qgpgmeprogresstokenmapper.cpp:93
Kleo::_detail::ThreadedJobMixin::resultHook
virtual void resultHook(const result_type &)
Definition: threadedjobmixin.h:180
Kleo::_detail::ThreadedJobMixin::slotFinished
void slotFinished()
Definition: threadedjobmixin.h:182
Kleo::_detail::ThreadedJobMixin::showProgress
void showProgress(const char *what, int type, int current, int total)
Definition: threadedjobmixin.h:196
Kleo::_detail::Thread::setFunction
void setFunction(const boost::function< T_result()> &function)
Definition: threadedjobmixin.h:92
Kleo::_detail::ThreadedJobMixin::auditLogError
GpgME::Error auditLogError() const
Definition: threadedjobmixin.h:195
boost::shared_ptr
Definition: checksumdefinition.h:46
Kleo::_detail::ToThreadMover
Definition: threadedjobmixin.h:77
QObject
Kleo::_detail::ThreadedJobMixin::lateInitialization
void lateInitialization()
Definition: threadedjobmixin.h:146
Kleo::_detail::audit_log_as_html
QString audit_log_as_html(GpgME::Context *ctx, GpgME::Error &err)
Kleo::_detail::PatternConverter::patterns
const char ** patterns() const
Definition: threadedjobmixin.cpp:89
Kleo::_detail::ToThreadMover::ToThreadMover
ToThreadMover(const boost::shared_ptr< QObject > &o, QThread *t)
Definition: threadedjobmixin.h:83
Kleo::_detail::PatternConverter::PatternConverter
PatternConverter(const QByteArray &ba)
Definition: threadedjobmixin.cpp:80
Kleo::_detail::ThreadedJobMixin::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT((boost::tuples::length< T_result >::value > 2))
QString
QList< QByteArray >
Kleo::_detail::PatternConverter::~PatternConverter
~PatternConverter()
Definition: threadedjobmixin.cpp:99
qgpgmeprogresstokenmapper.h
QStringList
Kleo::_detail::ThreadedJobMixin::result_type
T_result result_type
Definition: threadedjobmixin.h:117
Kleo::_detail::ThreadedJobMixin::run
void run(const T_binder &func)
Definition: threadedjobmixin.h:153
Kleo::_detail::ThreadedJobMixin::auditLogAsHtml
QString auditLogAsHtml() const
Definition: threadedjobmixin.h:194
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
Kleo::_detail::ToThreadMover::~ToThreadMover
~ToThreadMover()
Definition: threadedjobmixin.h:84
T_base
Kleo::_detail::ToThreadMover::ToThreadMover
ToThreadMover(QObject *o, QThread *t)
Definition: threadedjobmixin.h:81
QMutexLocker
Kleo::_detail::ToThreadMover::ToThreadMover
ToThreadMover(QObject &o, QThread *t)
Definition: threadedjobmixin.h:82
Kleo::_detail::Thread
Definition: threadedjobmixin.h:88
QThread
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
Kleo::_detail::ThreadedJobMixin
Definition: threadedjobmixin.h:114
Kleo::_detail::ThreadedJobMixin::run
void run(const T_binder &func, const boost::shared_ptr< QIODevice > &io)
Definition: threadedjobmixin.h:158
Kleo::_detail::ThreadedJobMixin::mixin_type
ThreadedJobMixin< T_base, T_result > mixin_type
Definition: threadedjobmixin.h:116
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal