• 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
qgpgmesignencryptjob.cpp
Go to the documentation of this file.
1 /*
2  qgpgmesignencryptjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004, 2007 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 along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  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 #include "qgpgmesignencryptjob.h"
34 
35 #include "ui/messagebox.h"
36 
37 #include <qgpgme/dataprovider.h>
38 
39 #include <gpgme++/context.h>
40 #include <gpgme++/data.h>
41 #include <gpgme++/key.h>
42 #include <gpgme++/exception.h>
43 
44 #include <klocale.h>
45 
46 #include <QBuffer>
47 
48 #ifndef Q_MOC_RUN
49 #include <boost/weak_ptr.hpp>
50 #endif
51 
52 #include <cassert>
53 
54 using namespace Kleo;
55 using namespace GpgME;
56 using namespace boost;
57 
58 QGpgMESignEncryptJob::QGpgMESignEncryptJob( Context * context )
59  : mixin_type( context ),
60  mOutputIsBase64Encoded( false )
61 {
62  lateInitialization();
63 }
64 
65 QGpgMESignEncryptJob::~QGpgMESignEncryptJob() {}
66 
67 void QGpgMESignEncryptJob::setOutputIsBase64Encoded( bool on ) {
68  mOutputIsBase64Encoded = on;
69 }
70 
71 static QGpgMESignEncryptJob::result_type sign_encrypt( Context * ctx, QThread * thread, const std::vector<Key> & signers, const std::vector<Key> & recipients, const weak_ptr<QIODevice> & plainText_, const weak_ptr<QIODevice> & cipherText_, bool alwaysTrust, bool outputIsBsse64Encoded ) {
72  const shared_ptr<QIODevice> & plainText = plainText_.lock();
73  const shared_ptr<QIODevice> & cipherText = cipherText_.lock();
74 
75  const _detail::ToThreadMover ctMover( cipherText, thread );
76  const _detail::ToThreadMover ptMover( plainText, thread );
77 
78  QGpgME::QIODeviceDataProvider in( plainText );
79  const Data indata( &in );
80 
81  const Context::EncryptionFlags eflags =
82  alwaysTrust ? Context::AlwaysTrust : Context::None ;
83 
84  ctx->clearSigningKeys();
85  Q_FOREACH( const Key & signer, signers )
86  if ( !signer.isNull() )
87  if ( const Error err = ctx->addSigningKey( signer ) )
88  return make_tuple( SigningResult( err ), EncryptionResult(), QByteArray(), QString(), Error() );
89 
90  if ( !cipherText ) {
91  QGpgME::QByteArrayDataProvider out;
92  Data outdata( &out );
93 
94  if ( outputIsBsse64Encoded )
95  outdata.setEncoding( Data::Base64Encoding );
96 
97  const std::pair<SigningResult, EncryptionResult> res = ctx->signAndEncrypt( recipients, indata, outdata, eflags );
98  Error ae;
99  const QString log = _detail::audit_log_as_html( ctx, ae );
100  return make_tuple( res.first, res.second, out.data(), log, ae );
101  } else {
102  QGpgME::QIODeviceDataProvider out( cipherText );
103  Data outdata( &out );
104 
105  if ( outputIsBsse64Encoded )
106  outdata.setEncoding( Data::Base64Encoding );
107 
108  const std::pair<SigningResult, EncryptionResult> res = ctx->signAndEncrypt( recipients, indata, outdata, eflags );
109  Error ae;
110  const QString log = _detail::audit_log_as_html( ctx, ae );
111  return make_tuple( res.first, res.second, QByteArray(), log, ae );
112  }
113 
114 }
115 
116 static QGpgMESignEncryptJob::result_type sign_encrypt_qba( Context * ctx, const std::vector<Key> & signers, const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust, bool outputIsBsse64Encoded ) {
117  const shared_ptr<QBuffer> buffer( new QBuffer );
118  buffer->setData( plainText );
119  if ( !buffer->open( QIODevice::ReadOnly ) )
120  assert( !"This should never happen: QBuffer::open() failed" );
121  return sign_encrypt( ctx, 0, signers, recipients, buffer, shared_ptr<QIODevice>(), alwaysTrust, outputIsBsse64Encoded );
122 }
123 
124 Error QGpgMESignEncryptJob::start( const std::vector<Key> & signers, const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust ) {
125  run( boost::bind( &sign_encrypt_qba, _1, signers, recipients, plainText, alwaysTrust, mOutputIsBase64Encoded ) );
126  return Error();
127 }
128 
129 void QGpgMESignEncryptJob::start( const std::vector<Key> & signers, const std::vector<Key> & recipients, const shared_ptr<QIODevice> & plainText, const shared_ptr<QIODevice> & cipherText, bool alwaysTrust ) {
130  run( boost::bind( &sign_encrypt, _1, _2, signers, recipients, _3, _4, alwaysTrust, mOutputIsBase64Encoded ), plainText, cipherText );
131 }
132 
133 std::pair<SigningResult,EncryptionResult> QGpgMESignEncryptJob::exec( const std::vector<Key> & signers, const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust, QByteArray & cipherText ) {
134  const result_type r = sign_encrypt_qba( context(), signers, recipients, plainText, alwaysTrust, mOutputIsBase64Encoded );
135  cipherText = get<2>( r );
136  resultHook( r );
137  return mResult;
138 }
139 
140 void QGpgMESignEncryptJob::showErrorDialog( QWidget * parent, const QString & caption ) const {
141  if ( ( mResult.first.error() && !mResult.first.error().isCanceled() ) ||
142  ( mResult.second.error() && !mResult.second.error().isCanceled() ) )
143  MessageBox::error( parent, mResult.first, mResult.second, this, caption );
144 }
145 
146 void QGpgMESignEncryptJob::resultHook( const result_type & tuple ) {
147  mResult = std::make_pair( get<0>( tuple ), get<1>( tuple ) );
148 }
149 
QWidget
Kleo::QGpgMESignEncryptJob::resultHook
void resultHook(const result_type &r)
Definition: qgpgmesignencryptjob.cpp:146
Kleo::QGpgMESignEncryptJob::QGpgMESignEncryptJob
QGpgMESignEncryptJob(GpgME::Context *context)
Definition: qgpgmesignencryptjob.cpp:58
QByteArray
Kleo::_detail::ThreadedJobMixin< SignEncryptJob, boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::context
GpgME::Context * context() const
Definition: threadedjobmixin.h:178
Kleo::MessageBox::error
static void error(QWidget *parent, const GpgME::SigningResult &result, const Kleo::Job *job, const QString &caption, KMessageBox::Options options=KMessageBox::Notify)
Kleo::QGpgMESignEncryptJob::exec
std::pair< GpgME::SigningResult, GpgME::EncryptionResult > exec(const std::vector< GpgME::Key > &signers, const std::vector< GpgME::Key > &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
Definition: qgpgmesignencryptjob.cpp:133
QBuffer
Kleo::QGpgMESignEncryptJob::showErrorDialog
void showErrorDialog(QWidget *parent, const QString &caption) const
Definition: qgpgmesignencryptjob.cpp:140
qgpgmesignencryptjob.h
boost::shared_ptr
Definition: checksumdefinition.h:46
Kleo::QGpgMESignEncryptJob::start
GpgME::Error start(const std::vector< GpgME::Key > &signers, const std::vector< GpgME::Key > &recipients, const QByteArray &plainText, bool alwaysTrust)
Kleo::_detail::ToThreadMover
Definition: threadedjobmixin.h:77
sign_encrypt_qba
static QGpgMESignEncryptJob::result_type sign_encrypt_qba(Context *ctx, const std::vector< Key > &signers, const std::vector< Key > &recipients, const QByteArray &plainText, bool alwaysTrust, bool outputIsBsse64Encoded)
Definition: qgpgmesignencryptjob.cpp:116
Kleo::_detail::ThreadedJobMixin< SignEncryptJob, boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::lateInitialization
void lateInitialization()
Definition: threadedjobmixin.h:146
Kleo::_detail::audit_log_as_html
QString audit_log_as_html(GpgME::Context *ctx, GpgME::Error &err)
messagebox.h
QString
sign_encrypt
static QGpgMESignEncryptJob::result_type sign_encrypt(Context *ctx, QThread *thread, const std::vector< Key > &signers, const std::vector< Key > &recipients, const weak_ptr< QIODevice > &plainText_, const weak_ptr< QIODevice > &cipherText_, bool alwaysTrust, bool outputIsBsse64Encoded)
Definition: qgpgmesignencryptjob.cpp:71
Kleo::_detail::ThreadedJobMixin< SignEncryptJob, boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::result_type
boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > result_type
Definition: threadedjobmixin.h:117
Kleo::_detail::ThreadedJobMixin< SignEncryptJob, boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::run
void run(const T_binder &func)
Definition: threadedjobmixin.h:153
Kleo::QGpgMESignEncryptJob::setOutputIsBase64Encoded
void setOutputIsBase64Encoded(bool on)
Definition: qgpgmesignencryptjob.cpp:67
Kleo::QGpgMESignEncryptJob::~QGpgMESignEncryptJob
~QGpgMESignEncryptJob()
Definition: qgpgmesignencryptjob.cpp:65
QThread
Kleo::_detail::ThreadedJobMixin< SignEncryptJob, boost::tuple< GpgME::SigningResult, GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >
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